interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / si / agent-archive / lib.om

1056 lines · 22043 bytes Raw Blame History
1note agent-archive: pure Omega append-only provenance archive with SHA-256 hash-chaining,
2note FNV-1a deduplication, secret redaction, privacy filtering, and tamper verification.
3note Ported from upstream agent-archive (src/core.rs, src/main.rs).
4note Compose with si/directives/om2.om before this file.
5
6const AA_MAX_RECS ulong 32
7const AA_AGENT_CLAUDE ulong 0
8const AA_AGENT_CODEX ulong 1
9const AA_AGENT_OPENCODE ulong 2
10const AA_AGENT_GEMINI ulong 3
11const AA_AGENT_MUSE ulong 4
12const AA_AGENT_OMP ulong 5
13
14const AA_ROLE_USER ulong 0
15const AA_ROLE_ASSISTANT ulong 1
16const AA_ROLE_SYSTEM ulong 2
17
18struct AaShaState h uint 8 end
19struct AaChainEntry seq ulong ts_ms ulong agent_id ulong role_id ulong is_priv ulong src_pos ulong dedup_key ulong text_off ulong text_len ulong end
20struct AaArchiveMeta cnt ulong last_ts ulong text_used ulong end
21
22text aa_t_hex "0123456789abcdef"
23text aa_t_redacted "[REDACTED]"
24text aa_t_ag_claude "claude"
25text aa_t_ag_codex "codex"
26text aa_t_ag_opencode "opencode"
27text aa_t_ag_gemini "gemini"
28text aa_t_ag_muse "muse"
29text aa_t_ag_omp "omp"
30text aa_t_role_user "user"
31text aa_t_role_asst "assistant"
32text aa_t_role_sys "system"
33
34text aa_p_bearer "Bearer "
35text aa_p_ghp "ghp_"
36text aa_p_gho "gho_"
37text aa_p_ghpat "github_pat_"
38text aa_p_sk "sk-"
39text aa_p_akia "AKIA"
40text aa_p_eyj "eyJ"
41text aa_p_xoxb "xoxb-"
42text aa_p_tok "token="
43text aa_p_pwd "password="
44text aa_p_sec "secret="
45text aa_p_key "api_key="
46
47text aa_c_1 "v1|seq="
48text aa_c_2 "|ts="
49text aa_c_3 "|agent="
50text aa_c_4 "|role="
51text aa_c_5 "|priv="
52text aa_c_6 "|pos="
53text aa_c_7 "|prev="
54text aa_c_8 "|text="
55
56text aa_j_1 "{\"seq\":"
57text aa_j_2 ",\"ts_ms\":"
58text aa_j_3 ",\"agent\":\""
59text aa_j_4 "\",\"role\":\""
60text aa_j_5 "\",\"prev\":\""
61text aa_j_6 "\",\"hash\":\""
62text aa_j_7 "\",\"text\":\""
63text aa_j_8 "\"}\n"
64
65fn aa_to_lower c uchar to uchar
66if ge c 65
67if le c 90
68ret add c 32
69end
70end
71ret c
72end
73
74fn aa_eq_bytes a view uchar * b view uchar * to bool
75n = len a
76if ne n len b
77ret false
78end
79i = ulong 0
80while lt i n bound 65536
81if ne get a i get b i
82ret false
83end
84i = add i 1
85end
86ret true
87end
88
89fn aa_ct_eq a view uchar * b view uchar * to bool
90n = len a
91if ne n len b
92ret false
93end
94acc = uchar 0
95i = ulong 0
96while lt i n bound 4096
97acc = or acc xor get a i get b i
98i = add i 1
99end
100ret eq acc 0
101end
102
103fn aa_append dst edit uchar * pos ulong src view uchar * to long
104n = len src
105if gt add pos n len dst
106ret -1
107end
108i = ulong 0
109while lt i n bound 65536
110put dst add pos i get src i
111i = add i 1
112end
113ret long add pos n
114end
115
116fn aa_append_u64 dst edit uchar * pos ulong val ulong to long
117tmp = new uchar 24 0
118cnt = ulong 0
119v = val
120if eq v 0
121put tmp 0 uchar 48
122cnt = ulong 1
123else
124while gt v 0 bound 24
125d = rem v 10
126put tmp cnt uchar add d 48
127v = div v 10
128cnt = add cnt 1
129end
130end
131if gt add pos cnt len dst
132ret -1
133end
134i = ulong 0
135while lt i cnt bound 24
136put dst add pos i get tmp sub sub cnt 1 i
137i = add i 1
138end
139ret long add pos cnt
140end
141
142note ---- Pure-Omega FIPS 180-4 SHA-256 ----
143
144fn aa_sha_init_k k edit uint 64 to void
145put k 0 uint 0x428a2f98
146put k 1 uint 0x71374491
147put k 2 uint 0xb5c0fbcf
148put k 3 uint 0xe9b5dba5
149put k 4 uint 0x3956c25b
150put k 5 uint 0x59f111f1
151put k 6 uint 0x923f82a4
152put k 7 uint 0xab1c5ed5
153put k 8 uint 0xd807aa98
154put k 9 uint 0x12835b01
155put k 10 uint 0x243185be
156put k 11 uint 0x550c7dc3
157put k 12 uint 0x72be5d74
158put k 13 uint 0x80deb1fe
159put k 14 uint 0x9bdc06a7
160put k 15 uint 0xc19bf174
161put k 16 uint 0xe49b69c1
162put k 17 uint 0xefbe4786
163put k 18 uint 0x0fc19dc6
164put k 19 uint 0x240ca1cc
165put k 20 uint 0x2de92c6f
166put k 21 uint 0x4a7484aa
167put k 22 uint 0x5cb0a9dc
168put k 23 uint 0x76f988da
169put k 24 uint 0x983e5152
170put k 25 uint 0xa831c66d
171put k 26 uint 0xb00327c8
172put k 27 uint 0xbf597fc7
173put k 28 uint 0xc6e00bf3
174put k 29 uint 0xd5a79147
175put k 30 uint 0x06ca6351
176put k 31 uint 0x14292967
177put k 32 uint 0x27b70a85
178put k 33 uint 0x2e1b2138
179put k 34 uint 0x4d2c6dfc
180put k 35 uint 0x53380d13
181put k 36 uint 0x650a7354
182put k 37 uint 0x766a0abb
183put k 38 uint 0x81c2c92e
184put k 39 uint 0x92722c85
185put k 40 uint 0xa2bfe8a1
186put k 41 uint 0xa81a664b
187put k 42 uint 0xc24b8b70
188put k 43 uint 0xc76c51a3
189put k 44 uint 0xd192e819
190put k 45 uint 0xd6990624
191put k 46 uint 0xf40e3585
192put k 47 uint 0x106aa070
193put k 48 uint 0x19a4c116
194put k 49 uint 0x1e376c08
195put k 50 uint 0x2748774c
196put k 51 uint 0x34b0bcb5
197put k 52 uint 0x391c0cb3
198put k 53 uint 0x4ed8aa4a
199put k 54 uint 0x5b9cca4f
200put k 55 uint 0x682e6ff3
201put k 56 uint 0x748f82ee
202put k 57 uint 0x78a5636f
203put k 58 uint 0x84c87814
204put k 59 uint 0x8cc70208
205put k 60 uint 0x90befffa
206put k 61 uint 0xa4506ceb
207put k 62 uint 0xbef9a3f7
208put k 63 uint 0xc67178f2
209end
210
211fn aa_sha_load_be32 blk view uchar 64 off ulong to uint
212b0 = uint get blk off
213b1 = uint get blk add off 1
214b2 = uint get blk add off 2
215b3 = uint get blk add off 3
216ret or or shl b0 24 shl b1 16 or shl b2 8 b3
217end
218
219fn aa_sha_expand blk view uchar 64 w edit uint 64 to void
220i = ulong 0
221while lt i 16 bound 16
222put w i aa_sha_load_be32 blk mul i 4
223i = add i 1
224end
225while lt i 64 bound 64
226x = get w sub i 15
227s0 = xor xor rotr x 7 rotr x 18 shr x 3
228y = get w sub i 2
229s1 = xor xor rotr y 17 rotr y 19 shr y 10
230put w i add add get w sub i 16 s0 add get w sub i 7 s1
231i = add i 1
232end
233end
234
235fn aa_sha_t1 e uint f uint g uint h uint ki uint wi uint to uint
236s1 = xor xor rotr e 6 rotr e 11 rotr e 25
237ch = xor and e f and not e g
238ret add add add add h s1 ch ki wi
239end
240
241fn aa_sha_t2 a uint b uint c uint to uint
242s0 = xor xor rotr a 2 rotr a 13 rotr a 22
243maj = xor xor and a b and a c and b c
244ret add s0 maj
245end
246
247fn aa_sha_round st edit uint 8 ki uint wi uint to void
248t1 = aa_sha_t1 get st 4 get st 5 get st 6 get st 7 ki wi
249t2 = aa_sha_t2 get st 0 get st 1 get st 2
250put st 7 get st 6
251put st 6 get st 5
252put st 5 get st 4
253put st 4 add get st 3 t1
254put st 3 get st 2
255put st 2 get st 1
256put st 1 get st 0
257put st 0 add t1 t2
258end
259
260fn aa_sha_compress hv edit uint 8 blk view uchar 64 k view uint 64 to void
261w = new uint 64 0
262aa_sha_expand blk w
263wv = new uint 8 0
264i = ulong 0
265while lt i 8 bound 8
266put wv i get hv i
267i = add i 1
268end
269r = ulong 0
270while lt r 64 bound 64
271aa_sha_round wv get k r get w r
272r = add r 1
273end
274j = ulong 0
275while lt j 8 bound 8
276put hv j add get hv j get wv j
277j = add j 1
278end
279end
280
281fn aa_sha_store_digest hv view uint 8 digest edit uchar 32 to void
282i = ulong 0
283while lt i 8 bound 8
284v = get hv i
285off = mul i 4
286put digest off uchar and shr v 24 255
287put digest add off 1 uchar and shr v 16 255
288put digest add off 2 uchar and shr v 8 255
289put digest add off 3 uchar and v 255
290i = add i 1
291end
292end
293
294fn aa_sha256 msg view uchar * digest edit uchar 32 to void
295k = new uint 64 0
296aa_sha_init_k k
297hv = new uint 8 0
298put hv 0 uint 0x6a09e667
299put hv 1 uint 0xbb67ae85
300put hv 2 uint 0x3c6ef372
301put hv 3 uint 0xa54ff53a
302put hv 4 uint 0x510e527f
303put hv 5 uint 0x9b05688c
304put hv 6 uint 0x1f83d9ab
305put hv 7 uint 0x5be0cd19
306blk = new uchar 64 0
307n = len msg
308pos = ulong 0
309while ge sub n pos 64 bound 4096
310j = ulong 0
311while lt j 64 bound 64
312put blk j get msg add pos j
313j = add j 1
314end
315aa_sha_compress hv blk k
316pos = add pos 64
317end
318remn = sub n pos
319j = ulong 0
320while lt j 64 bound 64
321put blk j uchar 0
322j = add j 1
323end
324j = ulong 0
325while lt j remn bound 64
326put blk j get msg add pos j
327j = add j 1
328end
329put blk remn uchar 0x80
330if ge remn 56
331aa_sha_compress hv blk k
332j = ulong 0
333while lt j 64 bound 64
334put blk j uchar 0
335j = add j 1
336end
337end
338bits = mul n 8
339put blk 56 uchar and shr bits 56 255
340put blk 57 uchar and shr bits 48 255
341put blk 58 uchar and shr bits 40 255
342put blk 59 uchar and shr bits 32 255
343put blk 60 uchar and shr bits 24 255
344put blk 61 uchar and shr bits 16 255
345put blk 62 uchar and shr bits 8 255
346put blk 63 uchar and bits 255
347aa_sha_compress hv blk k
348aa_sha_store_digest hv digest
349end
350
351fn aa_hex_encode src view uchar 32 dst edit uchar 64 to void
352i = ulong 0
353while lt i 32 bound 32
354b = get src i
355hi = ulong shr b 4
356lo = ulong and b 15
357put dst mul i 2 get aa_t_hex hi
358put dst add mul i 2 1 get aa_t_hex lo
359i = add i 1
360end
361end
362
363note ---- Upstream FNV-1a 64-bit record dedup key (src/core.rs Record::key) ----
364
365fn aa_fnv_feed h ulong data view uchar * to ulong
366cur = h
367n = len data
368i = ulong 0
369while lt i n bound 8192
370cur = xor cur ulong get data i
371cur = mul cur ulong 0x100000001b3
372i = add i 1
373end
374cur = mul cur ulong 0x100000001b3
375ret cur
376end
377
378fn aa_record_dedup_key agent_id ulong role_id ulong src_pos ulong msg view uchar * to ulong
379h = ulong 0xcbf29ce484222325
380h = mul xor h agent_id ulong 0x100000001b3
381h = mul xor h role_id ulong 0x100000001b3
382h = aa_fnv_feed h msg
383b = ulong 0
384while lt b 8 bound 8
385by = and shr src_pos mul b 8 255
386h = mul xor h by ulong 0x100000001b3
387b = add b 1
388end
389ret h
390end
391
392note ---- Upstream allowlists and Unix private mode guard ----
393
394fn aa_parse_agent name view uchar * to long
395if aa_eq_bytes name aa_t_ag_claude
396ret long AA_AGENT_CLAUDE
397end
398if aa_eq_bytes name aa_t_ag_codex
399ret long AA_AGENT_CODEX
400end
401if aa_eq_bytes name aa_t_ag_opencode
402ret long AA_AGENT_OPENCODE
403end
404if aa_eq_bytes name aa_t_ag_gemini
405ret long AA_AGENT_GEMINI
406end
407if aa_eq_bytes name aa_t_ag_muse
408ret long AA_AGENT_MUSE
409end
410if aa_eq_bytes name aa_t_ag_omp
411ret long AA_AGENT_OMP
412end
413ret -1
414end
415
416fn aa_parse_role name view uchar * to long
417if aa_eq_bytes name aa_t_role_user
418ret long AA_ROLE_USER
419end
420if aa_eq_bytes name aa_t_role_asst
421ret long AA_ROLE_ASSISTANT
422end
423if aa_eq_bytes name aa_t_role_sys
424ret long AA_ROLE_SYSTEM
425end
426ret -1
427end
428
429fn aa_append_agent dst edit uchar * pos ulong agent_id ulong to long
430if eq agent_id AA_AGENT_CLAUDE
431ret aa_append dst pos aa_t_ag_claude
432end
433if eq agent_id AA_AGENT_CODEX
434ret aa_append dst pos aa_t_ag_codex
435end
436if eq agent_id AA_AGENT_OPENCODE
437ret aa_append dst pos aa_t_ag_opencode
438end
439if eq agent_id AA_AGENT_GEMINI
440ret aa_append dst pos aa_t_ag_gemini
441end
442if eq agent_id AA_AGENT_MUSE
443ret aa_append dst pos aa_t_ag_muse
444end
445if eq agent_id AA_AGENT_OMP
446ret aa_append dst pos aa_t_ag_omp
447end
448ret -1
449end
450
451fn aa_append_role dst edit uchar * pos ulong role_id ulong to long
452if eq role_id AA_ROLE_USER
453ret aa_append dst pos aa_t_role_user
454end
455if eq role_id AA_ROLE_ASSISTANT
456ret aa_append dst pos aa_t_role_asst
457end
458if eq role_id AA_ROLE_SYSTEM
459ret aa_append dst pos aa_t_role_sys
460end
461ret -1
462end
463
464note Upstream private_check invariant: owner matches euid, no group/other mode bits (mode & 077 == 0), not symlink.
465fn aa_path_is_private owner_uid ulong euid ulong mode ulong is_symlink bool to bool
466if is_symlink
467ret false
468end
469if ne owner_uid euid
470ret false
471end
472if ne and mode 63 0
473ret false
474end
475ret true
476end
477
478note ---- Secret/token redaction filter ----
479
480fn aa_is_tok_char c uchar to bool
481if ge c 48
482if le c 57
483ret true
484end
485end
486if ge c 65
487if le c 90
488ret true
489end
490end
491if ge c 97
492if le c 122
493ret true
494end
495end
496if eq c 95
497ret true
498end
499if eq c 45
500ret true
501end
502if eq c 46
503ret true
504end
505ret false
506end
507
508fn aa_match_prefix_ci src view uchar * pos ulong pfx view uchar * to bool
509pn = len pfx
510if gt add pos pn len src
511ret false
512end
513i = ulong 0
514while lt i pn bound 64
515if ne aa_to_lower get src add pos i aa_to_lower get pfx i
516ret false
517end
518i = add i 1
519end
520ret true
521end
522
523fn aa_match_prefix src view uchar * pos ulong pfx view uchar * to bool
524pn = len pfx
525if gt add pos pn len src
526ret false
527end
528i = ulong 0
529while lt i pn bound 64
530if ne get src add pos i get pfx i
531ret false
532end
533i = add i 1
534end
535ret true
536end
537
538note If src[pos..] starts a secret/token pattern, return how many prefix chars to keep (0 to redact whole token, >0 for "Bearer " / "token="), else -1.
539fn aa_secret_keep_prefix src view uchar * pos ulong to long
540if aa_match_prefix src pos aa_p_bearer
541ret long len aa_p_bearer
542end
543if aa_match_prefix src pos aa_p_ghp
544ret 0
545end
546if aa_match_prefix src pos aa_p_gho
547ret 0
548end
549if aa_match_prefix src pos aa_p_ghpat
550ret 0
551end
552if aa_match_prefix src pos aa_p_sk
553ret 0
554end
555if aa_match_prefix src pos aa_p_akia
556ret 0
557end
558if aa_match_prefix src pos aa_p_eyj
559ret 0
560end
561if aa_match_prefix src pos aa_p_xoxb
562ret 0
563end
564if aa_match_prefix_ci src pos aa_p_tok
565ret long len aa_p_tok
566end
567if aa_match_prefix_ci src pos aa_p_pwd
568ret long len aa_p_pwd
569end
570if aa_match_prefix_ci src pos aa_p_sec
571ret long len aa_p_sec
572end
573if aa_match_prefix_ci src pos aa_p_key
574ret long len aa_p_key
575end
576ret -1
577end
578
579note Copy src into dst while redacting secret/token values to "[REDACTED]".
580note Returns redacted length >= 0, or -1 on buffer overflow.
581fn aa_redact_text src view uchar * dst edit uchar * to long
582n = len src
583i = ulong 0
584w = ulong 0
585while lt i n bound 8192
586keep = aa_secret_keep_prefix src i
587if ge keep 0
588k = ulong keep
589if gt k 0
590nw = aa_append dst w part src i k
591if lt nw 0
592ret -1
593end
594w = ulong nw
595i = add i k
596end
597skipped = ulong 0
598while lt i n bound 8192
599if not aa_is_tok_char get src i
600break
601end
602i = add i 1
603skipped = add skipped 1
604end
605if gt skipped 0
606nw = aa_append dst w aa_t_redacted
607if lt nw 0
608ret -1
609end
610w = ulong nw
611end
612else
613if ge w len dst
614ret -1
615end
616put dst w get src i
617w = add w 1
618i = add i 1
619end
620end
621ret long w
622end
623
624note ---- Canonical encoding, hash-chain linking, export, and tamper verification ----
625
626fn aa_escape_text dst edit uchar * pos ulong src view uchar * to long
627n = len src
628p = pos
629i = ulong 0
630while lt i n bound 8192
631c = get src i
632if eq c 10
633if gt add p 2 len dst
634ret -1
635end
636put dst p uchar 92
637put dst add p 1 uchar 110
638p = add p 2
639else
640if eq c 13
641i = add i 1
642continue
643else
644if eq c 34
645if gt add p 2 len dst
646ret -1
647end
648put dst p uchar 92
649put dst add p 1 uchar 34
650p = add p 2
651else
652if eq c 92
653if gt add p 2 len dst
654ret -1
655end
656put dst p uchar 92
657put dst add p 1 uchar 92
658p = add p 2
659else
660if ge p len dst
661ret -1
662end
663put dst p c
664p = add p 1
665end
666end
667end
668end
669i = add i 1
670end
671ret long p
672end
673
674note Encode canonical provenance record for hashing:
675note v1|seq=S|ts=T|agent=A|role=R|priv=P|pos=O|prev=HEX64|text=ESCAPED
676fn aa_encode_canonical entry view AaChainEntry prev_hex view uchar 64 txt view uchar * dst edit uchar * to long
677p = aa_append dst 0 aa_c_1
678if lt p 0
679ret -1
680end
681p = aa_append_u64 dst ulong p get entry seq
682if lt p 0
683ret -1
684end
685p = aa_append dst ulong p aa_c_2
686if lt p 0
687ret -1
688end
689p = aa_append_u64 dst ulong p get entry ts_ms
690if lt p 0
691ret -1
692end
693p = aa_append dst ulong p aa_c_3
694if lt p 0
695ret -1
696end
697p = aa_append_agent dst ulong p get entry agent_id
698if lt p 0
699ret -1
700end
701p = aa_append dst ulong p aa_c_4
702if lt p 0
703ret -1
704end
705p = aa_append_role dst ulong p get entry role_id
706if lt p 0
707ret -1
708end
709p = aa_append dst ulong p aa_c_5
710if lt p 0
711ret -1
712end
713p = aa_append_u64 dst ulong p get entry is_priv
714if lt p 0
715ret -1
716end
717p = aa_append dst ulong p aa_c_6
718if lt p 0
719ret -1
720end
721p = aa_append_u64 dst ulong p get entry src_pos
722if lt p 0
723ret -1
724end
725p = aa_append dst ulong p aa_c_7
726if lt p 0
727ret -1
728end
729p = aa_append dst ulong p prev_hex
730if lt p 0
731ret -1
732end
733p = aa_append dst ulong p aa_c_8
734if lt p 0
735ret -1
736end
737ret aa_escape_text dst ulong p txt
738end
739
740note Compute SHA-256 hex digest of one canonical record.
741fn aa_hash_entry entry view AaChainEntry prev_hex view uchar 64 txt view uchar * out_hex edit uchar 64 to bool
742canon = new uchar 4096 0
743clen = aa_encode_canonical entry prev_hex txt canon
744if lt clen 0
745ret false
746end
747dig = new uchar 32 0
748aa_sha256 part canon 0 ulong clen dig
749aa_hex_encode dig out_hex
750ret true
751end
752
753fn aa_export_jsonl_head entry view AaChainEntry hashes view uchar 4096 dst edit uchar 2048 to long
754idx = get entry seq
755hoff = mul idx 128
756if gt add hoff 128 4096
757ret -1
758end
759p = aa_append dst 0 aa_j_1
760if lt p 0
761ret -1
762end
763p = aa_append_u64 dst ulong p idx
764if lt p 0
765ret -1
766end
767p = aa_append dst ulong p aa_j_2
768if lt p 0
769ret -1
770end
771p = aa_append_u64 dst ulong p get entry ts_ms
772if lt p 0
773ret -1
774end
775p = aa_append dst ulong p aa_j_3
776if lt p 0
777ret -1
778end
779p = aa_append_agent dst ulong p get entry agent_id
780if lt p 0
781ret -1
782end
783p = aa_append dst ulong p aa_j_4
784if lt p 0
785ret -1
786end
787p = aa_append_role dst ulong p get entry role_id
788if lt p 0
789ret -1
790end
791p = aa_append dst ulong p aa_j_5
792if lt p 0
793ret -1
794end
795p = aa_append dst ulong p part hashes hoff 64
796if lt p 0
797ret -1
798end
799p = aa_append dst ulong p aa_j_6
800if lt p 0
801ret -1
802end
803p = aa_append dst ulong p part hashes add hoff 64 64
804if lt p 0
805ret -1
806end
807ret aa_append dst ulong p aa_j_7
808end
809
810fn aa_export_jsonl_text dst edit uchar 2048 pos ulong txt view uchar * to long
811red = new uchar 2048 0
812rlen = aa_redact_text txt red
813if lt rlen 0
814ret -1
815end
816p = aa_escape_text dst pos part red 0 ulong rlen
817if lt p 0
818ret -1
819end
820ret aa_append dst ulong p aa_j_8
821end
822
823note Format one JSONL record for export.
824note Refuses private records (is_priv != 0) unless allow_private is true (returns -2).
825note Redacts secrets/tokens before emitting JSONL.
826fn aa_export_jsonl entry view AaChainEntry hashes view uchar 4096 text_buf view uchar * allow_private bool dst edit uchar 2048 to long
827if ne get entry is_priv 0
828if not allow_private
829ret -2
830end
831end
832toff = get entry text_off
833tlen = get entry text_len
834if gt add toff tlen len text_buf
835ret -1
836end
837p = aa_export_jsonl_head entry hashes dst
838if lt p 0
839ret -1
840end
841ret aa_export_jsonl_text dst ulong p part text_buf toff tlen
842end
843
844note Flat storage helpers for append-only chain archive:
845note entries: ulong[288] (AA_MAX_RECS=32 * 9 fields per AaChainEntry)
846note hashes: uchar[4096] (AA_MAX_RECS=32 * 128 bytes: 64-byte prev_hex + 64-byte hash_hex)
847note text_buf: uchar[*] (concatenated message text bytes)
848
849fn aa_store_entry entries edit ulong 288 idx ulong e view AaChainEntry to void
850b = mul idx 9
851put entries b get e seq
852put entries add b 1 get e ts_ms
853put entries add b 2 get e agent_id
854put entries add b 3 get e role_id
855put entries add b 4 get e is_priv
856put entries add b 5 get e src_pos
857put entries add b 6 get e dedup_key
858put entries add b 7 get e text_off
859put entries add b 8 get e text_len
860end
861
862fn aa_load_entry entries view ulong 288 idx ulong e edit AaChainEntry to void
863b = mul idx 9
864put e seq get entries b
865put e ts_ms get entries add b 1
866put e agent_id get entries add b 2
867put e role_id get entries add b 3
868put e is_priv get entries add b 4
869put e src_pos get entries add b 5
870put e dedup_key get entries add b 6
871put e text_off get entries add b 7
872put e text_len get entries add b 8
873end
874
875fn aa_has_dedup_key entries view ulong 288 cnt ulong key ulong to bool
876i = ulong 0
877while lt i cnt bound 64
878if eq get entries add mul i 9 6 key
879ret true
880end
881i = add i 1
882end
883ret false
884end
885
886fn aa_fill_prev_hex hashes view uchar 4096 idx ulong prev_hex edit uchar 64 to void
887if eq idx 0
888i = ulong 0
889while lt i 64 bound 64
890put prev_hex i uchar 48
891i = add i 1
892end
893else
894src_off = add mul sub idx 1 128 64
895i = ulong 0
896while lt i 64 bound 64
897put prev_hex i get hashes add src_off i
898i = add i 1
899end
900end
901end
902
903fn aa_store_pair hashes edit uchar 4096 idx ulong prev_hex view uchar 64 cur_hex view uchar 64 to void
904off = mul idx 128
905i = ulong 0
906while lt i 64 bound 64
907put hashes add off i get prev_hex i
908put hashes add add off 64 i get cur_hex i
909i = add i 1
910end
911end
912
913note Step 1: aa_stage_record stages text into text_buf and populates rec fields (dedup_key, seq, text_off, text_len).
914fn aa_stage_record meta edit AaArchiveMeta rec edit AaChainEntry txt view uchar * text_buf edit uchar * to int
915if ge get meta cnt AA_MAX_RECS
916ret -1
917end
918if gt get rec agent_id AA_AGENT_OMP
919ret -2
920end
921if gt get rec role_id AA_ROLE_SYSTEM
922ret -2
923end
924if gt get meta cnt 0
925if lt get rec ts_ms get meta last_ts
926ret -3
927end
928end
929if gt add get meta text_used len txt len text_buf
930ret -4
931end
932if lt aa_append text_buf get meta text_used txt 0
933ret -4
934end
935put rec seq get meta cnt
936put rec text_off get meta text_used
937put rec text_len len txt
938put rec dedup_key aa_record_dedup_key get rec agent_id get rec role_id get rec src_pos txt
939ret 0
940end
941
942note Step 2: aa_commit_record checks dedup and commits entry metadata.
943fn aa_commit_record meta edit AaArchiveMeta rec view AaChainEntry entries edit ulong 288 to int
944cnt = get meta cnt
945if ge cnt AA_MAX_RECS
946ret -1
947end
948if aa_has_dedup_key entries cnt get rec dedup_key
949ret 1
950end
951aa_store_entry entries cnt rec
952put meta cnt add cnt 1
953put meta last_ts get rec ts_ms
954put meta text_used add get rec text_off get rec text_len
955ret 0
956end
957
958note Step 3: Link record SHA-256 hash into hashes[idx] using prev_hash and text_buf.
959fn aa_link_record idx ulong rec view AaChainEntry hashes edit uchar 4096 text_buf view uchar * to bool
960prev_hex = new uchar 64 0
961cur_hex = new uchar 64 0
962aa_fill_prev_hex hashes idx prev_hex
963toff = get rec text_off
964tlen = get rec text_len
965if gt add toff tlen len text_buf
966ret false
967end
968if not aa_hash_entry rec prev_hex part text_buf toff tlen cur_hex
969ret false
970end
971aa_store_pair hashes idx prev_hex cur_hex
972ret true
973end
974
975note Verify one entry at index `idx` in the hash chain.
976note Returns:
977note 0: intact
978note -2: sequence mismatch
979note -3: prev_hash link mismatch
980note -4: SHA-256 digest mismatch (tampered payload)
981note -5: timestamp rollback
982fn aa_verify_one idx ulong rec view AaChainEntry hashes view uchar 4096 text_buf view uchar * to int
983if ne get rec seq idx
984ret -2
985end
986hoff = mul idx 128
987if gt add hoff 128 4096
988ret -1
989end
990exp_prev = new uchar 64 0
991aa_fill_prev_hex hashes idx exp_prev
992if not aa_ct_eq part hashes hoff 64 exp_prev
993ret -3
994end
995toff = get rec text_off
996tlen = get rec text_len
997if gt add toff tlen len text_buf
998ret -4
999end
1000calc_hex = new uchar 64 0
1001if not aa_hash_entry rec exp_prev part text_buf toff tlen calc_hex
1002ret -4
1003end
1004if not aa_ct_eq part hashes add hoff 64 64 calc_hex
1005ret -4
1006end
1007ret 0
1008end
1009
1010note Verify the entire append-only hash chain (0..cnt-1).
1011note Returns 0 if every link, sequence, timestamp, and SHA-256 digest is intact, else negative error code.
1012fn aa_verify_chain cnt ulong entries view ulong 288 hashes view uchar 4096 text_buf view uchar * to int
1013rec = new AaChainEntry 0 0 0 0 0 0 0 0 0
1014prev_ts = ulong 0
1015i = ulong 0
1016while lt i cnt bound 64
1017aa_load_entry entries i rec
1018if gt i 0
1019if lt get rec ts_ms prev_ts
1020ret -5
1021end
1022end
1023v = aa_verify_one i rec hashes text_buf
1024if ne v 0
1025ret v
1026end
1027prev_ts = get rec ts_ms
1028i = add i 1
1029end
1030ret 0
1031end
1032
1033note Parse unsigned integer decimal string.
1034fn aa_parse_u64 s view uchar * to long
1035n = len s
1036if eq n 0
1037ret -1
1038end
1039if gt n 18
1040ret -1
1041end
1042v = ulong 0
1043i = ulong 0
1044while lt i n bound 32
1045c = get s i
1046if lt c 48
1047ret -1
1048end
1049if gt c 57
1050ret -1
1051end
1052v = add mul v 10 ulong sub c 48
1053i = add i 1
1054end
1055ret long v
1056end