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