interplane.Get Omega
Read in your language

aegis-runtime-mv

Native Rust agent runtime: Axum WebSocket/SSE/REST gateway, heartbeat scheduler, dependency-aware task workflow, SQLite storage. Attachment of AienOS. Pre-alpha.

Brought by drakestapleton

branch main Branches Tags History

aegis-runtime-mv / om / journal.om

459 lines · 11076 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note core/aegis/journal.om: Append-only SHA-256 hash-chained event/task journal (pure OM-2 library unit).
3note Composes with om/tls/sha2.om (FIPS 180-4 SHA-256) and core/aegis/dag.om for replay.
4note Guarantees: strict sequence monotonicity (1..N), cryptographic hash chaining from genesis,
5note replay reconstruction into DAG state (refusing tampered/torn logs), and binary wire
6note encoding/decoding with torn-record and magic/commit sentinel detection.
7
8const JRN_MAX_RECORDS ulong 128
9const JRN_PAYLOAD_MAX ulong 64
10const JRN_WIRE_REC_SIZE ulong 192
11
12const JRN_MAGIC ulong 0x4145474a
13const JRN_COMMIT_END ulong 0x454e4431
14
15note Journal event kinds
16const EVT_TASK_SUBMITTED ulong 1
17const EVT_TASK_DEP ulong 2
18const EVT_TASK_STARTED ulong 3
19const EVT_TASK_DONE ulong 4
20const EVT_TASK_FAILED ulong 5
21const EVT_TASK_BLOCKED ulong 6
22const EVT_TASK_CANCELLED ulong 7
23const EVT_LEASE_ACQUIRED ulong 8
24const EVT_LEASE_RECLAIMED ulong 9
25const EVT_HEARTBEAT_TICK ulong 10
26const EVT_POLICY_DENIED ulong 11
27
28note Journal status and integrity error codes
29const JRN_OK ulong 0
30const JRN_ERR_FULL ulong 1
31const JRN_ERR_PAYLOAD_TOO_LONG ulong 2
32const JRN_ERR_BAD_MAGIC ulong 3
33const JRN_ERR_SEQ_GAP ulong 4
34const JRN_ERR_PREV_HASH_MISMATCH ulong 5
35const JRN_ERR_HASH_MISMATCH ulong 6
36const JRN_ERR_TORN_RECORD ulong 7
37const JRN_ERR_NOT_FOUND ulong 8
38
39note jrn_meta: [0]=record_cnt, [1]=last_seq
40global jrn_meta ulong 8
41global jrn_magic ulong 128
42global jrn_seq ulong 128
43global jrn_tick ulong 128
44global jrn_kind ulong 128
45global jrn_task_id ulong 128
46global jrn_aux ulong 128
47global jrn_plen ulong 128
48global jrn_commit ulong 128
49global jrn_payload uchar 8192
50global jrn_prev_hash uchar 4096
51global jrn_hash uchar 4096
52
53text jrn_domain "AEGIS-JRN-V1"
54
55fn jrn_reset to void
56for i ulong 8
57put jrn_meta i 0
58end
59for i ulong 128
60put jrn_magic i 0
61put jrn_seq i 0
62put jrn_tick i 0
63put jrn_kind i 0
64put jrn_task_id i 0
65put jrn_aux i 0
66put jrn_plen i 0
67put jrn_commit i 0
68end
69for i ulong 8192
70put jrn_payload i 0
71end
72for i ulong 4096
73put jrn_prev_hash i 0
74put jrn_hash i 0
75end
76end
77
78fn jrn_count to ulong
79ret get jrn_meta 0
80end
81
82fn jrn_last_seq to ulong
83ret get jrn_meta 1
84end
85
86fn jrn_rec_seq idx ulong to ulong
87if ge idx get jrn_meta 0
88ret 0
89end
90ret get jrn_seq idx
91end
92
93fn jrn_rec_tick idx ulong to ulong
94if ge idx get jrn_meta 0
95ret 0
96end
97ret get jrn_tick idx
98end
99
100fn jrn_rec_kind idx ulong to ulong
101if ge idx get jrn_meta 0
102ret 0
103end
104ret get jrn_kind idx
105end
106
107fn jrn_rec_task_id idx ulong to ulong
108if ge idx get jrn_meta 0
109ret 0
110end
111ret get jrn_task_id idx
112end
113
114fn jrn_rec_aux idx ulong to ulong
115if ge idx get jrn_meta 0
116ret 0
117end
118ret get jrn_aux idx
119end
120
121fn jrn_copy_hash idx ulong dst edit uchar 32 to ulong
122if ge idx get jrn_meta 0
123ret JRN_ERR_NOT_FOUND
124end
125base = mul idx 32
126for i ulong 32
127put dst i get jrn_hash add base i
128end
129ret JRN_OK
130end
131
132fn jrn_put_be32 buf edit uchar * off ulong v ulong to void
133put buf add off 0 uchar and shr v 24 0xff
134put buf add off 1 uchar and shr v 16 0xff
135put buf add off 2 uchar and shr v 8 0xff
136put buf add off 3 uchar and v 0xff
137end
138
139fn jrn_put_be64 buf edit uchar * off ulong v ulong to void
140put buf add off 0 uchar and shr v 56 0xff
141put buf add off 1 uchar and shr v 48 0xff
142put buf add off 2 uchar and shr v 40 0xff
143put buf add off 3 uchar and shr v 32 0xff
144put buf add off 4 uchar and shr v 24 0xff
145put buf add off 5 uchar and shr v 16 0xff
146put buf add off 6 uchar and shr v 8 0xff
147put buf add off 7 uchar and v 0xff
148end
149
150fn jrn_get_be32 buf view uchar * off ulong to ulong
151b0 = ulong get buf add off 0
152b1 = ulong get buf add off 1
153b2 = ulong get buf add off 2
154b3 = ulong get buf add off 3
155ret or shl b0 24 or shl b1 16 or shl b2 8 b3
156end
157
158fn jrn_get_be64 buf view uchar * off ulong to ulong
159hi = jrn_get_be32 buf off
160lo = jrn_get_be32 buf add off 4
161ret or shl hi 32 lo
162end
163
164note Compute canonical SHA-256 digest of record idx into dst_hash[32].
165note Covers: domain(12) | magic(4) | seq(8) | tick(8) | kind(8) | task_id(8) | aux(8) |
166note plen(4) | prev_hash(32) | commit(4) | payload(plen).
167fn jrn_compute_record_hash idx ulong dst_hash edit uchar 32 to void
168ctx = new ulong 40 0
169hdr = new uchar 96 0
170pl = new uchar 64 0
171for i ulong 12
172put hdr i get jrn_domain i
173end
174jrn_put_be32 hdr 12 get jrn_magic idx
175jrn_put_be64 hdr 16 get jrn_seq idx
176jrn_put_be64 hdr 24 get jrn_tick idx
177jrn_put_be64 hdr 32 get jrn_kind idx
178jrn_put_be64 hdr 40 get jrn_task_id idx
179jrn_put_be64 hdr 48 get jrn_aux idx
180plen = get jrn_plen idx
181jrn_put_be32 hdr 56 plen
182hbase = mul idx 32
183for i ulong 32
184put hdr add 60 i get jrn_prev_hash add hbase i
185end
186jrn_put_be32 hdr 92 get jrn_commit idx
187sha256_init ctx
188sha256_update ctx part hdr 0 96
189if and gt plen 0 le plen JRN_PAYLOAD_MAX
190pbase = mul idx JRN_PAYLOAD_MAX
191for i ulong plen bound 64
192put pl i get jrn_payload add pbase i
193end
194sha256_update ctx part pl 0 plen
195end
196sha256_final ctx dst_hash
197for i ulong 40
198put ctx i 0
199end
200end
201
202fn jrn_store_payload idx ulong payload view uchar * to void
203pbase = mul idx JRN_PAYLOAD_MAX
204for i ulong JRN_PAYLOAD_MAX
205put jrn_payload add pbase i 0
206end
207plen = len payload
208for i ulong plen bound 64
209put jrn_payload add pbase i get payload i
210end
211end
212
213fn jrn_link_and_seal idx ulong to void
214dig = new uchar 32 0
215hbase = mul idx 32
216if eq idx 0
217for i ulong 32
218put jrn_prev_hash add hbase i 0
219end
220else
221prev_base = mul sub idx 1 32
222for i ulong 32
223put jrn_prev_hash add hbase i get jrn_hash add prev_base i
224end
225end
226jrn_compute_record_hash idx dig
227for i ulong 32
228put jrn_hash add hbase i get dig i
229end
230end
231
232note Append a new hash-chained event record. Returns JRN_OK or error code.
233fn jrn_append kind ulong task_id ulong aux ulong tick ulong payload view uchar * to ulong
234idx = get jrn_meta 0
235if ge idx JRN_MAX_RECORDS
236ret JRN_ERR_FULL
237end
238plen = len payload
239if gt plen JRN_PAYLOAD_MAX
240ret JRN_ERR_PAYLOAD_TOO_LONG
241end
242seq = add get jrn_meta 1 1
243put jrn_magic idx JRN_MAGIC
244put jrn_seq idx seq
245put jrn_tick idx tick
246put jrn_kind idx kind
247put jrn_task_id idx task_id
248put jrn_aux idx aux
249put jrn_plen idx plen
250put jrn_commit idx JRN_COMMIT_END
251jrn_store_payload idx payload
252jrn_link_and_seal idx
253put jrn_meta 0 add idx 1
254put jrn_meta 1 seq
255ret JRN_OK
256end
257
258note Verify a single record's prev_hash linkage against record idx-1 (or all-zero genesis).
259fn jrn_check_prev_link idx ulong to bool
260hbase = mul idx 32
261if eq idx 0
262for i ulong 32
263if ne get jrn_prev_hash add hbase i 0
264ret false
265end
266end
267ret true
268end
269prev_base = mul sub idx 1 32
270for i ulong 32
271if ne get jrn_prev_hash add hbase i get jrn_hash add prev_base i
272ret false
273end
274end
275ret true
276end
277
278note Full cryptographic chain audit: checks magic, commit sentinel, strict sequence
279note monotonicity (1..N), payload length bounds, prev_hash continuity, and SHA-256 integrity.
280fn jrn_verify_chain to ulong
281n = get jrn_meta 0
282dig = new uchar 32 0
283for i ulong n bound 128
284if ne get jrn_magic i JRN_MAGIC
285ret JRN_ERR_BAD_MAGIC
286end
287if ne get jrn_commit i JRN_COMMIT_END
288ret JRN_ERR_TORN_RECORD
289end
290if ne get jrn_seq i add i 1
291ret JRN_ERR_SEQ_GAP
292end
293if gt get jrn_plen i JRN_PAYLOAD_MAX
294ret JRN_ERR_TORN_RECORD
295end
296if not jrn_check_prev_link i
297ret JRN_ERR_PREV_HASH_MISMATCH
298end
299jrn_compute_record_hash i dig
300hbase = mul i 32
301for b ulong 32
302if ne get dig b get jrn_hash add hbase b
303ret JRN_ERR_HASH_MISMATCH
304end
305end
306end
307ret JRN_OK
308end
309
310note Replay verified journal into DAG state. Refuses to replay if chain verification fails.
311fn jrn_replay_to_dag to ulong
312chk = jrn_verify_chain
313if ne chk JRN_OK
314ret chk
315end
316dag_reset
317n = get jrn_meta 0
318for i ulong n bound 128
319k = get jrn_kind i
320tid = get jrn_task_id i
321ax = get jrn_aux i
322if eq k EVT_TASK_SUBMITTED
323prio = and ax 0xffffffff
324tkind = shr ax 32
325rc = dag_add_task tid prio tkind
326if ne rc DAG_OK
327ret JRN_ERR_TORN_RECORD
328end
329end
330if eq k EVT_TASK_DEP
331rc = dag_add_dep tid ax
332if ne rc DAG_OK
333ret JRN_ERR_TORN_RECORD
334end
335end
336if eq k EVT_TASK_STARTED
337rdy = dag_solve_ready
338if gt rdy DAG_MAX_TASKS
339ret JRN_ERR_TORN_RECORD
340end
341rc = dag_start_task tid
342if ne rc DAG_OK
343ret JRN_ERR_TORN_RECORD
344end
345end
346if eq k EVT_TASK_DONE
347rc = dag_complete_task tid
348if ne rc DAG_OK
349ret JRN_ERR_TORN_RECORD
350end
351end
352if eq k EVT_TASK_FAILED
353rdy = dag_solve_ready
354if gt rdy DAG_MAX_TASKS
355ret JRN_ERR_TORN_RECORD
356end
357rc = dag_fail_task tid ax
358if ne rc DAG_OK
359ret JRN_ERR_TORN_RECORD
360end
361end
362if eq k EVT_TASK_CANCELLED
363rc = dag_cancel_task tid
364if ne rc DAG_OK
365ret JRN_ERR_TORN_RECORD
366end
367end
368if eq k EVT_LEASE_RECLAIMED
369if eq dag_get_state tid TASK_RUNNING
370rc = dag_requeue_task tid
371if ne rc DAG_OK
372ret JRN_ERR_TORN_RECORD
373end
374end
375end
376end
377rdy2 = dag_solve_ready
378if gt rdy2 DAG_MAX_TASKS
379ret JRN_ERR_TORN_RECORD
380end
381ret JRN_OK
382end
383
384note Encode record idx into 192-byte wire format at dst[off..off+192].
385note Layout: magic(4) | seq(8) | tick(8) | kind(8) | task_id(8) | aux(8) | plen(4) |
386note payload(64) | prev_hash(32) | hash(32) | commit(4) | reserved(12) = 192 bytes.
387fn jrn_encode_record idx ulong dst edit uchar * off ulong to ulong
388if ge idx get jrn_meta 0
389ret JRN_ERR_NOT_FOUND
390end
391if gt add off JRN_WIRE_REC_SIZE len dst
392ret JRN_ERR_TORN_RECORD
393end
394jrn_put_be32 dst add off 0 get jrn_magic idx
395jrn_put_be64 dst add off 4 get jrn_seq idx
396jrn_put_be64 dst add off 12 get jrn_tick idx
397jrn_put_be64 dst add off 20 get jrn_kind idx
398jrn_put_be64 dst add off 28 get jrn_task_id idx
399jrn_put_be64 dst add off 36 get jrn_aux idx
400jrn_put_be32 dst add off 44 get jrn_plen idx
401pbase = mul idx JRN_PAYLOAD_MAX
402for i ulong JRN_PAYLOAD_MAX
403put dst add off add 48 i get jrn_payload add pbase i
404end
405hbase = mul idx 32
406for i ulong 32
407put dst add off add 112 i get jrn_prev_hash add hbase i
408put dst add off add 144 i get jrn_hash add hbase i
409end
410jrn_put_be32 dst add off 176 get jrn_commit idx
411for i ulong 12
412put dst add off add 180 i 0
413end
414ret JRN_OK
415end
416
417fn jrn_decode_one idx ulong src view uchar * off ulong to void
418put jrn_magic idx jrn_get_be32 src add off 0
419put jrn_seq idx jrn_get_be64 src add off 4
420put jrn_tick idx jrn_get_be64 src add off 12
421put jrn_kind idx jrn_get_be64 src add off 20
422put jrn_task_id idx jrn_get_be64 src add off 28
423put jrn_aux idx jrn_get_be64 src add off 36
424put jrn_plen idx jrn_get_be32 src add off 44
425pbase = mul idx JRN_PAYLOAD_MAX
426for i ulong JRN_PAYLOAD_MAX
427put jrn_payload add pbase i get src add off add 48 i
428end
429hbase = mul idx 32
430for i ulong 32
431put jrn_prev_hash add hbase i get src add off add 112 i
432put jrn_hash add hbase i get src add off add 144 i
433end
434put jrn_commit idx jrn_get_be32 src add off 176
435end
436
437note Decode a binary journal stream of nbytes from src into the journal and verify its chain.
438note Refuses truncated/torn trailing bytes (nbytes % 192 != 0), bad magic, missing commit, or hash mismatch.
439fn jrn_decode_stream src view uchar * nbytes ulong to ulong
440if gt nbytes len src
441ret JRN_ERR_TORN_RECORD
442end
443if ne rem nbytes JRN_WIRE_REC_SIZE 0
444ret JRN_ERR_TORN_RECORD
445end
446nrec = div nbytes JRN_WIRE_REC_SIZE
447if gt nrec JRN_MAX_RECORDS
448ret JRN_ERR_FULL
449end
450jrn_reset
451for idx ulong nrec bound 128
452jrn_decode_one idx src mul idx JRN_WIRE_REC_SIZE
453end
454put jrn_meta 0 nrec
455if gt nrec 0
456put jrn_meta 1 get jrn_seq sub nrec 1
457end
458ret jrn_verify_chain
459end