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/aienux/crates/aienos-agent-state/agent_state_test.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 / aienux / crates / aienos-agent-state / agent_state_test.om` 1385 lines ยท 25674 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note aienux/crates/aienos-agent-state/agent_state_test.om: Behavioral self-test for aienos-agent-state. 3 note Ports every unit test from id.rs, history.rs, lineage.rs, and manager.rs. 4 note Compose with: si/directives/om2.om om/tls/sha2.om aienux/crates/aienos-agent-state/agent_state.om aienux/crates/aienos-agent-state/agent_state_test.om 5 6 text ast_seed_alpha "agent-alpha" 7 text ast_seed_beta "agent-beta" 8 text ast_seed_spark "spark-agent" 9 text ast_seed_lineage "lineage-test" 10 text ast_seed_sovereign "aienos-sovereign-agent" 11 text ast_seed_fork_cnt "fork-counter-reboot-test" 12 text ast_seed_canonical "canonical-checkpoint-test" 13 text ast_seed_fork_gap "fork-gap-test" 14 text ast_seed_idempotent "idempotent-provisioning-test" 15 text ast_seed_fail_closed "fail-closed-restore-test" 16 text ast_seed_roundtrip "exact-round-trip-test" 17 text ast_seed_rereg_forks "re-register-keeps-forks-test" 18 text ast_seed_absent "absent-agent" 19 text ast_seed_other "other" 20 text ast_seed_suspend "suspend-test-agent" 21 22 note Shared tree IDs for build_tree helper: 0=agent, 1=root, 2=child0, 3=child1, 4=grandchild 23 global ast_tree_ids uchar 160 24 25 fn ast_is_hex_char c uchar to bool 26 if ge c 48 27 if le c 57 28 ret true 29 end 30 end 31 if ge c 97 32 if le c 102 33 ret true 34 end 35 end 36 ret false 37 end 38 39 fn ast_tree_get idx ulong dst edit uchar * to void 40 off = mul idx 32 41 for i ulong 32 42 put dst i get ast_tree_ids add off i 43 end 44 end 45 46 fn ast_tree_put idx ulong src view uchar * to void 47 off = mul idx 32 48 for i ulong 32 49 put ast_tree_ids add off i get src i 50 end 51 end 52 53 fn ast_build_tree mgr ulong seed view uchar * to int 54 aid = new uchar 32 0 55 rid = new uchar 32 0 56 c0 = new uchar 32 0 57 c1 = new uchar 32 0 58 gc = new uchar 32 0 59 as_agent_id_from_seed seed aid 60 rc = as_mgr_register_agent mgr aid 100 rid 61 if ne rc AS_OK 62 ret 1 63 end 64 t1 = new uint 3 0 65 put t1 0 1 66 put t1 1 2 67 put t1 2 3 68 rc = as_mgr_append_tokens mgr rid t1 69 if ne rc AS_OK 70 ret 2 71 end 72 rc = as_mgr_fork_branch mgr rid c0 73 if ne rc AS_OK 74 ret 3 75 end 76 t2 = new uint 1 4 77 rc = as_mgr_append_tokens mgr c0 t2 78 if ne rc AS_OK 79 ret 4 80 end 81 eref = new uchar 16 9 82 rc = as_mgr_add_epistemic_ref mgr c0 eref 83 if ne rc AS_OK 84 ret 5 85 end 86 rc = as_mgr_fork_branch mgr rid c1 87 if ne rc AS_OK 88 ret 6 89 end 90 rc = as_mgr_fork_branch mgr c0 gc 91 if ne rc AS_OK 92 ret 7 93 end 94 t3 = new uint 2 0 95 put t3 0 5 96 put t3 1 6 97 rc = as_mgr_append_tokens mgr gc t3 98 if ne rc AS_OK 99 ret 8 100 end 101 ast_tree_put 0 aid 102 ast_tree_put 1 rid 103 ast_tree_put 2 c0 104 ast_tree_put 3 c1 105 ast_tree_put 4 gc 106 ret 0 107 end 108 109 note Prepare a valid tree checkpoint in as_snap_* and sanity-check untampered restore 110 fn ast_prep_tamper_snap to int 111 as_mgr_init 0 1 112 if ne ast_build_tree 0 ast_seed_fail_closed 0 113 ret 1 114 end 115 aid = new uchar 32 0 116 ast_tree_get 0 aid 117 buf = new uchar 16384 0 118 n = as_mgr_export_checkpoint 0 aid buf 119 if le n 0 120 ret 2 121 end 122 rc = as_mgr_restore_from_checkpoint 1 buf ulong n 2 123 if ne rc AS_OK 124 ret 3 125 end 126 rc = as_decode_checkpoint_to_snap buf ulong n 127 if ne rc AS_OK 128 ret 4 129 end 130 ret 0 131 end 132 133 fn ast_reseal_and_restore to int 134 buf = new uchar 16384 0 135 n = as_seal_snapshot buf 136 if le n 0 137 ret AS_ERR_SERIALIZATION 138 end 139 ret as_mgr_restore_from_checkpoint 1 buf ulong n 2 140 end 141 142 note === 1. id.rs tests === 143 144 fn t_id to int 145 id1 = new uchar 32 0 146 id2 = new uchar 32 0 147 id3 = new uchar 32 0 148 as_agent_id_from_seed ast_seed_alpha id1 149 as_agent_id_from_seed ast_seed_alpha id2 150 as_agent_id_from_seed ast_seed_beta id3 151 if not as_id_eq id1 id2 152 ret 1 153 end 154 if as_id_eq id1 id3 155 ret 2 156 end 157 hex = new uchar 64 0 158 as_id_to_hex id1 hex 159 for i ulong 64 160 if not ast_is_hex_char get hex i 161 ret 3 162 end 163 end 164 agent = new uchar 32 0 165 root = new uchar 32 0 166 ch0 = new uchar 32 0 167 ch1 = new uchar 32 0 168 ch0r = new uchar 32 0 169 as_agent_id_from_seed ast_seed_spark agent 170 as_branch_id_root agent root 171 as_branch_id_derive root 0 ch0 172 as_branch_id_derive root 1 ch1 173 as_branch_id_derive root 0 ch0r 174 if not as_id_eq ch0 ch0r 175 ret 4 176 end 177 if as_id_eq ch0 ch1 178 ret 5 179 end 180 if as_id_eq root ch0 181 ret 6 182 end 183 ret 0 184 end 185 186 note === 2. history.rs tests === 187 188 fn t_history to int 189 as_th_new 0 190 as_th_new 1 191 if not as_th_is_empty 0 192 ret 7 193 end 194 toks = new uint 3 0 195 put toks 0 101 196 put toks 1 202 197 put toks 2 303 198 if ne as_th_append_tokens 0 toks AS_OK 199 ret 8 200 end 201 if ne as_th_append_tokens 1 toks AS_OK 202 ret 9 203 end 204 if ne as_th_count 0 3 205 ret 10 206 end 207 if as_th_is_empty 0 208 ret 10 209 end 210 h1 = new uchar 32 0 211 h2 = new uchar 32 0 212 as_th_rolling_hash 0 h1 213 as_th_rolling_hash 1 h2 214 if not as_id_eq h1 h2 215 ret 11 216 end 217 if ne as_th_get_token 0 0 101 218 ret 12 219 end 220 if ne as_th_get_token 0 1 202 221 ret 12 222 end 223 if ne as_th_get_token 0 2 303 224 ret 12 225 end 226 as_th_fork 2 0 227 if ne as_th_append 2 404 AS_OK 228 ret 13 229 end 230 as_th_rolling_hash 2 h2 231 if as_id_eq h1 h2 232 ret 13 233 end 234 if ne as_th_count 0 3 235 ret 13 236 end 237 if ne as_th_count 2 4 238 ret 13 239 end 240 ret 0 241 end 242 243 note === 3. lineage.rs tests === 244 245 fn t_lineage to int 246 agent = new uchar 32 0 247 rb = new uchar 32 0 248 b1 = new uchar 32 0 249 b2 = new uchar 32 0 250 as_agent_id_from_seed ast_seed_lineage agent 251 as_branch_id_root agent rb 252 as_lin_new_root 0 agent 253 if ne as_lin_depth_of 0 0 254 ret 14 255 end 256 as_branch_id_derive rb 1 b1 257 if ne as_lin_extend 1 0 b1 AS_OK 258 ret 15 259 end 260 if ne as_lin_depth_of 1 1 261 ret 15 262 end 263 if not as_lin_contains 1 b1 264 ret 16 265 end 266 as_branch_id_derive b1 2 b2 267 if ne as_lin_extend 2 1 b2 AS_OK 268 ret 17 269 end 270 if ne as_lin_depth_of 2 2 271 ret 17 272 end 273 if not as_lin_contains 2 b1 274 ret 18 275 end 276 if not as_lin_contains 2 b2 277 ret 19 278 end 279 if as_lin_contains 2 rb 280 ret 20 281 end 282 as_epi_new 0 283 if not as_epi_is_empty 0 284 ret 21 285 end 286 r1 = new uchar 16 0xaa 287 r2 = new uchar 16 0xbb 288 if ne as_epi_insert 0 r1 AS_OK 289 ret 21 290 end 291 if ne as_epi_insert 0 r1 AS_OK 292 ret 21 293 end 294 if ne as_epi_count 0 1 295 ret 21 296 end 297 if ne as_epi_insert 0 r2 AS_OK 298 ret 21 299 end 300 if ne as_epi_count 0 2 301 ret 21 302 end 303 if not as_epi_contains 0 r1 304 ret 21 305 end 306 if not as_epi_contains 0 r2 307 ret 21 308 end 309 ret 0 310 end 311 312 note === 4. test_checkpoint_reboot_and_reconstruction === 313 314 fn t_reboot_verify_root aid view uchar * rb view uchar * to int 315 ridx = as_mgr_find_branch 1 rb 316 if lt ridx 0 317 ret 31 318 end 319 rslot = as_br_slot 1 ulong ridx 320 roff = mul rslot 32 321 for i ulong 32 322 if ne get as_br_agent_id add roff i get aid i 323 ret 31 324 end 325 end 326 if ne get as_br_tok_cnt rslot 50 327 ret 32 328 end 329 ret 0 330 end 331 332 fn t_reboot_verify_child rb view uchar * cb view uchar * to int 333 cidx = as_mgr_find_branch 1 cb 334 if lt cidx 0 335 ret 33 336 end 337 cslot = as_br_slot 1 ulong cidx 338 if ne get as_br_has_parent cslot 1 339 ret 33 340 end 341 coff = mul cslot 32 342 for i ulong 32 343 if ne get as_br_parent_id add coff i get rb i 344 ret 33 345 end 346 end 347 if ne get as_br_tok_cnt cslot 52 348 ret 34 349 end 350 ref_id = new uchar 16 7 351 if not as_mgr_branch_epi_contains 1 ulong cidx ref_id 352 ret 34 353 end 354 if ne get as_br_inc_id cslot 2 355 ret 35 356 end 357 if ne get as_br_inc_state cslot AS_COMPUTE_EVICTED 358 ret 35 359 end 360 if ne get as_br_inc_alloc_tok cslot 0 361 ret 35 362 end 363 if le as_mgr_resume 1 cb 0 364 ret 36 365 end 366 if ne as_mgr_reconstruct_physical 1 cb AS_OK 367 ret 37 368 end 369 if ne get as_br_inc_state cslot AS_COMPUTE_DECODING 370 ret 37 371 end 372 if ne get as_br_inc_alloc_tok cslot 52 373 ret 37 374 end 375 if ne get as_br_inc_kv_has cslot 1 376 ret 37 377 end 378 if ne get as_br_inc_kv_ref cslot add ulong 0xdead0000 52 379 ret 37 380 end 381 ret 0 382 end 383 384 fn t_reboot_verify aid view uchar * rb view uchar * cb view uchar * to int 385 rc = t_reboot_verify_root aid rb 386 if ne rc 0 387 ret rc 388 end 389 ret t_reboot_verify_child rb cb 390 end 391 392 fn t_reboot to int 393 as_mgr_init 0 1 394 aid = new uchar 32 0 395 rb = new uchar 32 0 396 cb = new uchar 32 0 397 as_agent_id_from_seed ast_seed_sovereign aid 398 if ne as_mgr_register_agent 0 aid 100 rb AS_OK 399 ret 22 400 end 401 t50 = new uint 50 0 402 for i ulong 50 403 put t50 i uint add i 1 404 end 405 if ne as_mgr_append_tokens 0 rb t50 AS_OK 406 ret 23 407 end 408 if ne as_mgr_fork_branch 0 rb cb AS_OK 409 ret 24 410 end 411 t2 = new uint 2 0 412 put t2 0 51 413 put t2 1 52 414 if ne as_mgr_append_tokens 0 cb t2 AS_OK 415 ret 25 416 end 417 ref_id = new uchar 16 7 418 if ne as_mgr_add_epistemic_ref 0 cb ref_id AS_OK 419 ret 26 420 end 421 buf = new uchar 16384 0 422 n = as_mgr_export_checkpoint 0 aid buf 423 if le n 0 424 ret 27 425 end 426 ch = new uchar 32 0 427 if ne as_mgr_checkpoint 0 aid ch AS_OK 428 ret 28 429 end 430 if as_id_is_zero ch 431 ret 29 432 end 433 for i ulong 32 434 if ne get ch i get buf add 8 i 435 ret 29 436 end 437 end 438 if ne as_mgr_restore_from_checkpoint 1 buf ulong n 2 AS_OK 439 ret 30 440 end 441 ret t_reboot_verify aid rb cb 442 end 443 444 note === 5. restore_rebuilds_fork_counters_without_reusing_branch_ids === 445 446 fn t_fork_counters_phase2 to int 447 aid = new uchar 32 0 448 rb = new uchar 32 0 449 c0 = new uchar 32 0 450 c1 = new uchar 32 0 451 c2 = new uchar 32 0 452 c3 = new uchar 32 0 453 ast_tree_get 0 aid 454 ast_tree_get 1 rb 455 ast_tree_get 2 c0 456 ast_tree_get 3 c1 457 ast_tree_get 4 c2 458 buf = new uchar 16384 0 459 n2 = as_mgr_export_checkpoint 1 aid buf 460 if le n2 0 461 ret 43 462 end 463 if ne as_mgr_restore_from_checkpoint 0 buf ulong n2 3 AS_OK 464 ret 43 465 end 466 if ne as_mgr_fork_branch 0 rb c3 AS_OK 467 ret 44 468 end 469 if as_id_eq c3 c0 470 ret 45 471 end 472 if as_id_eq c3 c1 473 ret 45 474 end 475 if as_id_eq c3 c2 476 ret 45 477 end 478 i2 = as_mgr_find_branch 0 c2 479 if lt i2 0 480 ret 46 481 end 482 s2 = as_br_slot 0 ulong i2 483 if ne get as_br_has_parent s2 1 484 ret 46 485 end 486 o2 = mul s2 32 487 for i ulong 32 488 if ne get as_br_parent_id add o2 i get rb i 489 ret 46 490 end 491 end 492 ret 0 493 end 494 495 fn t_fork_counters to int 496 as_mgr_init 0 1 497 aid = new uchar 32 0 498 rb = new uchar 32 0 499 c0 = new uchar 32 0 500 c1 = new uchar 32 0 501 c2 = new uchar 32 0 502 as_agent_id_from_seed ast_seed_fork_cnt aid 503 if ne as_mgr_register_agent 0 aid 100 rb AS_OK 504 ret 38 505 end 506 if ne as_mgr_fork_branch 0 rb c0 AS_OK 507 ret 38 508 end 509 t2 = new uint 2 0 510 put t2 0 11 511 put t2 1 12 512 if ne as_mgr_append_tokens 0 c0 t2 AS_OK 513 ret 38 514 end 515 if ne as_mgr_fork_branch 0 rb c1 AS_OK 516 ret 38 517 end 518 buf = new uchar 16384 0 519 n = as_mgr_export_checkpoint 0 aid buf 520 if le n 0 521 ret 39 522 end 523 if ne as_mgr_restore_from_checkpoint 1 buf ulong n 2 AS_OK 524 ret 39 525 end 526 if ne as_mgr_fork_branch 1 rb c2 AS_OK 527 ret 40 528 end 529 if as_id_eq c2 c0 530 ret 41 531 end 532 if as_id_eq c2 c1 533 ret 41 534 end 535 i0 = as_mgr_find_branch 1 c0 536 if lt i0 0 537 ret 42 538 end 539 if ne get as_br_tok_cnt as_br_slot 1 ulong i0 2 540 ret 42 541 end 542 ast_tree_put 0 aid 543 ast_tree_put 1 rb 544 ast_tree_put 2 c0 545 ast_tree_put 3 c1 546 ast_tree_put 4 c2 547 ret t_fork_counters_phase2 548 end 549 550 note === 6. checkpoint_bytes_are_stable_across_restore === 551 552 fn t_stable_bytes to int 553 as_mgr_init 0 7 554 aid = new uchar 32 0 555 rb = new uchar 32 0 556 ch = new uchar 32 0 557 as_agent_id_from_seed ast_seed_canonical aid 558 if ne as_mgr_register_agent 0 aid 100 rb AS_OK 559 ret 47 560 end 561 tok = new uint 1 0 562 for t ulong 24 563 if ne as_mgr_fork_branch 0 rb ch AS_OK 564 ret 48 565 end 566 put tok 0 uint t 567 if ne as_mgr_append_tokens 0 ch tok AS_OK 568 ret 48 569 end 570 end 571 b1 = new uchar 16384 0 572 b2 = new uchar 16384 0 573 n1 = as_mgr_export_checkpoint 0 aid b1 574 if le n1 0 575 ret 49 576 end 577 if ne as_mgr_restore_from_checkpoint 1 b1 ulong n1 7 AS_OK 578 ret 50 579 end 580 n2 = as_mgr_export_checkpoint 1 aid b2 581 if ne n1 n2 582 ret 51 583 end 584 for i ulong ulong n1 bound 16384 585 if ne get b1 i get b2 i 586 ret 51 587 end 588 end 589 ret 0 590 end 591 592 note === 7. restore_refuses_checkpoint_without_durable_identity === 593 594 fn t_no_identity to int 595 as_snap_clear 596 put as_snap_inc 0 5 597 buf = new uchar 4096 0 598 n = as_seal_snapshot buf 599 if le n 0 600 ret 52 601 end 602 rc = as_mgr_restore_from_checkpoint 1 buf ulong n 6 603 if ne rc AS_ERR_CKPT_NO_AGENT 604 ret 53 605 end 606 ret 0 607 end 608 609 note === 8. restore_rejects_noncontiguous_persisted_fork_indexes === 610 611 fn t_noncontiguous to int 612 as_mgr_init 0 1 613 aid = new uchar 32 0 614 rb = new uchar 32 0 615 c0 = new uchar 32 0 616 c1 = new uchar 32 0 617 as_agent_id_from_seed ast_seed_fork_gap aid 618 if ne as_mgr_register_agent 0 aid 100 rb AS_OK 619 ret 54 620 end 621 if ne as_mgr_fork_branch 0 rb c0 AS_OK 622 ret 54 623 end 624 if ne as_mgr_fork_branch 0 rb c1 AS_OK 625 ret 54 626 end 627 buf = new uchar 16384 0 628 n = as_mgr_export_checkpoint 0 aid buf 629 if le n 0 630 ret 55 631 end 632 if ne as_decode_checkpoint_to_snap buf ulong n AS_OK 633 ret 55 634 end 635 sb0 = as_snap_find_branch c0 636 if lt sb0 0 637 ret 56 638 end 639 as_snap_remove_branch ulong sb0 640 if ne ast_reseal_and_restore AS_ERR_CKPT_NONCONTIGUOUS_FORK 641 ret 57 642 end 643 ret 0 644 end 645 646 note === 9. re_registering_an_existing_identity_does_not_reset_its_root_branch === 647 648 fn t_reregister to int 649 as_mgr_init 0 1 650 aid = new uchar 32 0 651 rb = new uchar 32 0 652 rb2 = new uchar 32 0 653 as_agent_id_from_seed ast_seed_idempotent aid 654 if ne as_mgr_register_agent 0 aid 100 rb AS_OK 655 ret 58 656 end 657 toks = new uint 6 0 658 put toks 0 4 659 put toks 1 8 660 put toks 2 15 661 put toks 3 16 662 put toks 4 23 663 put toks 5 42 664 if ne as_mgr_append_tokens 0 rb toks AS_OK 665 ret 59 666 end 667 if ne as_mgr_register_agent 0 aid 999 rb2 AS_OK 668 ret 60 669 end 670 if not as_id_eq rb rb2 671 ret 60 672 end 673 idx = as_mgr_find_branch 0 rb 674 if lt idx 0 675 ret 61 676 end 677 slot = as_br_slot 0 ulong idx 678 if ne get as_br_created_at slot 100 679 ret 61 680 end 681 if ne get as_br_tok_cnt slot 6 682 ret 62 683 end 684 tb = mul slot AS_MAX_TOKENS 685 for i ulong 6 686 if ne get as_br_tok_id add tb i get toks i 687 ret 62 688 end 689 end 690 ret 0 691 end 692 693 note === 10. restore_round_trips_branches_and_next_fork_indexes_exactly === 694 695 fn t_roundtrip_check_pair pid view uchar * next_idx ulong to int 696 exp = new uchar 32 0 697 f1 = new uchar 32 0 698 f0 = new uchar 32 0 699 as_branch_id_derive pid next_idx exp 700 if ne as_mgr_fork_branch 1 pid f1 AS_OK 701 ret 1 702 end 703 if not as_id_eq f1 exp 704 ret 2 705 end 706 if ne as_mgr_fork_branch 0 pid f0 AS_OK 707 ret 3 708 end 709 if not as_id_eq f0 exp 710 ret 4 711 end 712 ret 0 713 end 714 715 fn t_roundtrip_check_restored to int 716 bid = new uchar 32 0 717 for sb ulong 4 718 off = mul sb 32 719 for j ulong 32 720 put bid j get as_snap_br_id add off j 721 end 722 mi = as_mgr_find_branch 1 bid 723 if lt mi 0 724 ret 68 725 end 726 mslot = as_br_slot 1 ulong mi 727 if not as_br_eq_snap mslot sb 728 ret 68 729 end 730 if ne get as_br_inc_id mslot 2 731 ret 69 732 end 733 if ne get as_br_inc_state mslot AS_COMPUTE_EVICTED 734 ret 69 735 end 736 end 737 ret 0 738 end 739 740 fn t_roundtrip_check_gc to int 741 c0 = new uchar 32 0 742 gc = new uchar 32 0 743 ast_tree_get 2 c0 744 ast_tree_get 4 gc 745 gci = as_mgr_find_branch 1 gc 746 if lt gci 0 747 ret 70 748 end 749 gcslot = as_br_slot 1 ulong gci 750 if ne get as_br_has_parent gcslot 1 751 ret 70 752 end 753 if ne get as_br_lin_depth gcslot 2 754 ret 70 755 end 756 if ne get as_br_lin_cnt gcslot 2 757 ret 70 758 end 759 ga0 = mul gcslot mul AS_MAX_DEPTH 32 760 ga1 = add ga0 32 761 for j ulong 32 762 if ne get as_br_lin_anc add ga0 j get c0 j 763 ret 71 764 end 765 if ne get as_br_lin_anc add ga1 j get gc j 766 ret 71 767 end 768 end 769 ret 0 770 end 771 772 fn t_roundtrip to int 773 as_mgr_init 0 1 774 if ne ast_build_tree 0 ast_seed_roundtrip 0 775 ret 63 776 end 777 aid = new uchar 32 0 778 ast_tree_get 0 aid 779 buf = new uchar 16384 0 780 n = as_mgr_export_checkpoint 0 aid buf 781 if le n 0 782 ret 64 783 end 784 if ne as_mgr_restore_from_checkpoint 1 buf ulong n 2 AS_OK 785 ret 65 786 end 787 if ne as_decode_checkpoint_to_snap buf ulong n AS_OK 788 ret 66 789 end 790 if ne get as_snap_branch_cnt 0 4 791 ret 67 792 end 793 rc = t_roundtrip_check_restored 794 if ne rc 0 795 ret rc 796 end 797 rc = t_roundtrip_check_gc 798 if ne rc 0 799 ret rc 800 end 801 rb = new uchar 32 0 802 c0 = new uchar 32 0 803 c1 = new uchar 32 0 804 ast_tree_get 1 rb 805 ast_tree_get 2 c0 806 ast_tree_get 3 c1 807 if ne t_roundtrip_check_pair rb 2 0 808 ret 72 809 end 810 if ne t_roundtrip_check_pair c0 1 0 811 ret 72 812 end 813 if ne t_roundtrip_check_pair c1 0 0 814 ret 72 815 end 816 if ne t_roundtrip_check_restored 0 817 ret 73 818 end 819 gc = new uchar 32 0 820 ast_tree_get 4 gc 821 gcexp = new uchar 32 0 822 gcch = new uchar 32 0 823 as_branch_id_derive gc 0 gcexp 824 if ne as_mgr_fork_branch 1 gc gcch AS_OK 825 ret 74 826 end 827 if not as_id_eq gcch gcexp 828 ret 74 829 end 830 ret 0 831 end 832 833 note === 11. re_registering_keeps_forked_branches_and_fork_indexes === 834 835 fn t_reregister_tree to int 836 as_mgr_init 0 1 837 if ne ast_build_tree 0 ast_seed_rereg_forks 0 838 ret 75 839 end 840 aid = new uchar 32 0 841 rb = new uchar 32 0 842 rb2 = new uchar 32 0 843 ast_tree_get 0 aid 844 ast_tree_get 1 rb 845 b1 = new uchar 16384 0 846 b2 = new uchar 16384 0 847 n1 = as_mgr_export_checkpoint 0 aid b1 848 if le n1 0 849 ret 76 850 end 851 if ne as_mgr_register_agent 0 aid 999 rb2 AS_OK 852 ret 77 853 end 854 if not as_id_eq rb rb2 855 ret 77 856 end 857 n2 = as_mgr_export_checkpoint 0 aid b2 858 if ne n1 n2 859 ret 78 860 end 861 for i ulong ulong n1 bound 16384 862 if ne get b1 i get b2 i 863 ret 78 864 end 865 end 866 ch = new uchar 32 0 867 exp = new uchar 32 0 868 as_branch_id_derive rb 2 exp 869 if ne as_mgr_fork_branch 0 rb ch AS_OK 870 ret 79 871 end 872 if not as_id_eq ch exp 873 ret 79 874 end 875 n3 = as_mgr_export_checkpoint 0 aid b1 876 if le n3 0 877 ret 80 878 end 879 if ne as_mgr_restore_from_checkpoint 1 b1 ulong n3 2 AS_OK 880 ret 80 881 end 882 if ne as_mgr_register_agent 1 aid 1234 rb2 AS_OK 883 ret 80 884 end 885 if not as_id_eq rb rb2 886 ret 80 887 end 888 if ne as_decode_checkpoint_to_snap b1 ulong n3 AS_OK 889 ret 80 890 end 891 if ne get as_mgr_branch_cnt 1 get as_snap_branch_cnt 0 892 ret 80 893 end 894 for sb ulong get as_snap_branch_cnt 0 bound 32 895 if not as_br_eq_snap as_br_slot 1 sb sb 896 ret 80 897 end 898 end 899 as_branch_id_derive rb 3 exp 900 if ne as_mgr_fork_branch 1 rb ch AS_OK 901 ret 81 902 end 903 if not as_id_eq ch exp 904 ret 81 905 end 906 ret 0 907 end 908 909 note === 12. Fail-closed tamper tests (basic cases) === 910 911 fn t_tamper_basic_a to int 912 note 12a. duplicate agent identity 913 if ne ast_prep_tamper_snap 0 914 ret 82 915 end 916 aid = new uchar 32 0 917 ast_tree_get 0 aid 918 acnt = get as_snap_agent_cnt 0 919 aoff = mul acnt 32 920 for i ulong 32 921 put as_snap_agents add aoff i get aid i 922 end 923 put as_snap_agent_cnt 0 add acnt 1 924 if ne ast_reseal_and_restore AS_ERR_CKPT_DUP_AGENT 925 ret 83 926 end 927 928 note 12b. branch refers to an absent agent identity 929 if ne ast_prep_tamper_snap 0 930 ret 84 931 end 932 stranger = new uchar 32 0 933 as_agent_id_from_seed ast_seed_absent stranger 934 if ne as_snap_push_new_root stranger 1 AS_OK 935 ret 84 936 end 937 if ne ast_reseal_and_restore AS_ERR_CKPT_ABSENT_AGENT 938 ret 85 939 end 940 941 note 12c. duplicate branch identity 942 if ne ast_prep_tamper_snap 0 943 ret 86 944 end 945 c1 = new uchar 32 0 946 ast_tree_get 3 c1 947 sb1 = as_snap_find_branch c1 948 if lt sb1 0 949 ret 86 950 end 951 bcnt = get as_snap_branch_cnt 0 952 as_snap_copy_branch bcnt ulong sb1 953 put as_snap_branch_cnt 0 add bcnt 1 954 if ne ast_reseal_and_restore AS_ERR_CKPT_DUP_BRANCH 955 ret 87 956 end 957 ret 0 958 end 959 960 fn t_tamper_basic_b to int 961 note 12d. missing root branch 962 if ne ast_prep_tamper_snap 0 963 ret 88 964 end 965 rb = new uchar 32 0 966 c0 = new uchar 32 0 967 c1 = new uchar 32 0 968 ast_tree_get 1 rb 969 ast_tree_get 2 c0 970 ast_tree_get 3 c1 971 sbr = as_snap_find_branch rb 972 if lt sbr 0 973 ret 88 974 end 975 as_snap_remove_branch ulong sbr 976 if ne ast_reseal_and_restore AS_ERR_CKPT_MISSING_ROOT 977 ret 89 978 end 979 980 note 12e. parentless non-root branch 981 if ne ast_prep_tamper_snap 0 982 ret 90 983 end 984 sb1 = as_snap_find_branch c1 985 put as_snap_br_has_parent ulong sb1 0 986 if ne ast_reseal_and_restore AS_ERR_CKPT_UNEXPECTED_ROOT 987 ret 91 988 end 989 990 note 12f. branch with missing parent 991 if ne ast_prep_tamper_snap 0 992 ret 92 993 end 994 sb0 = as_snap_find_branch c0 995 as_snap_remove_branch ulong sb0 996 if ne ast_reseal_and_restore AS_ERR_CKPT_MISSING_PARENT 997 ret 93 998 end 999 1000 note 12g. child id not derived from fork index 1001 if ne ast_prep_tamper_snap 0 1002 ret 94 1003 end 1004 bogus = new uchar 32 0 1005 as_branch_id_derive rb 7 bogus 1006 sb1 = as_snap_find_branch c1 1007 o1 = mul ulong sb1 32 1008 for i ulong 32 1009 put as_snap_br_id add o1 i get bogus i 1010 end 1011 put as_snap_br_lin_cnt ulong sb1 1 1012 la1 = mul ulong sb1 mul AS_MAX_DEPTH 32 1013 for i ulong 32 1014 put as_snap_br_lin_anc add la1 i get bogus i 1015 end 1016 if ne ast_reseal_and_restore AS_ERR_CKPT_NONCONTIGUOUS_FORK 1017 ret 95 1018 end 1019 ret 0 1020 end 1021 1022 fn t_tamper_basic to int 1023 rc = t_tamper_basic_a 1024 if ne rc 0 1025 ret rc 1026 end 1027 ret t_tamper_basic_b 1028 end 1029 1030 note === 13. restore_rejects_inconsistent_root_branch (all 5 tampers) === 1031 1032 fn t_tamper_root to int 1033 rb = new uchar 32 0 1034 other = new uchar 32 0 1035 as_agent_id_from_seed ast_seed_other other 1036 1037 note 13a. root.lineage.depth = 1 1038 if ne ast_prep_tamper_snap 0 1039 ret 120 1040 end 1041 ast_tree_get 1 rb 1042 sbr = ulong as_snap_find_branch rb 1043 put as_snap_br_lin_depth sbr 1 1044 if ne ast_reseal_and_restore AS_ERR_CKPT_INCONSISTENT_ROOT 1045 ret 121 1046 end 1047 1048 note 13b. root.lineage.ancestor_branches.push(root.branch_id) 1049 if ne ast_prep_tamper_snap 0 1050 ret 120 1051 end 1052 sbr = ulong as_snap_find_branch rb 1053 la = mul sbr mul AS_MAX_DEPTH 32 1054 for i ulong 32 1055 put as_snap_br_lin_anc add la i get rb i 1056 end 1057 put as_snap_br_lin_cnt sbr 1 1058 if ne ast_reseal_and_restore AS_ERR_CKPT_INCONSISTENT_ROOT 1059 ret 122 1060 end 1061 1062 note 13c. root.lineage.root_agent = other 1063 if ne ast_prep_tamper_snap 0 1064 ret 120 1065 end 1066 sbr = ulong as_snap_find_branch rb 1067 ro = mul sbr 32 1068 for i ulong 32 1069 put as_snap_br_lin_root add ro i get other i 1070 end 1071 if ne ast_reseal_and_restore AS_ERR_CKPT_INCONSISTENT_ROOT 1072 ret 123 1073 end 1074 1075 note 13d. root.parent_branch_id = Some(root.branch_id) 1076 if ne ast_prep_tamper_snap 0 1077 ret 120 1078 end 1079 sbr = ulong as_snap_find_branch rb 1080 ro = mul sbr 32 1081 put as_snap_br_has_parent sbr 1 1082 for i ulong 32 1083 put as_snap_br_parent_id add ro i get rb i 1084 end 1085 if ne ast_reseal_and_restore AS_ERR_CKPT_INCONSISTENT_ROOT 1086 ret 124 1087 end 1088 1089 note 13e. root branch id of agent A carrying agent B's identity 1090 if ne ast_prep_tamper_snap 0 1091 ret 120 1092 end 1093 acnt = get as_snap_agent_cnt 0 1094 aoff = mul acnt 32 1095 for i ulong 32 1096 put as_snap_agents add aoff i get other i 1097 end 1098 put as_snap_agent_cnt 0 add acnt 1 1099 if ne as_snap_push_new_root other 1 AS_OK 1100 ret 125 1101 end 1102 sbr = ulong as_snap_find_branch rb 1103 ro = mul sbr 32 1104 for i ulong 32 1105 put as_snap_br_agent_id add ro i get other i 1106 end 1107 if ne ast_reseal_and_restore AS_ERR_CKPT_INCONSISTENT_ROOT 1108 ret 126 1109 end 1110 ret 0 1111 end 1112 1113 note === 14. restore_rejects_inconsistent_lineage (all 5 tampers) === 1114 1115 fn t_tamper_lineage_a to int 1116 gc = new uchar 32 0 1117 other = new uchar 32 0 1118 as_agent_id_from_seed ast_seed_other other 1119 1120 note 14a. grandchild.lineage.depth = 3 1121 if ne ast_prep_tamper_snap 0 1122 ret 130 1123 end 1124 ast_tree_get 4 gc 1125 sbg = ulong as_snap_find_branch gc 1126 put as_snap_br_lin_depth sbg 3 1127 if ne ast_reseal_and_restore AS_ERR_CKPT_INCONSISTENT_LINEAGE 1128 ret 131 1129 end 1130 1131 note 14b. grandchild.lineage.ancestor_branches.remove(0) 1132 if ne ast_prep_tamper_snap 0 1133 ret 130 1134 end 1135 sbg = ulong as_snap_find_branch gc 1136 la0 = mul sbg mul AS_MAX_DEPTH 32 1137 la1 = add la0 32 1138 for i ulong 32 1139 put as_snap_br_lin_anc add la0 i get as_snap_br_lin_anc add la1 i 1140 end 1141 put as_snap_br_lin_cnt sbg 1 1142 if ne ast_reseal_and_restore AS_ERR_CKPT_INCONSISTENT_LINEAGE 1143 ret 132 1144 end 1145 1146 note 14c. grandchild.lineage.root_agent = other 1147 if ne ast_prep_tamper_snap 0 1148 ret 130 1149 end 1150 sbg = ulong as_snap_find_branch gc 1151 go = mul sbg 32 1152 for i ulong 32 1153 put as_snap_br_lin_root add go i get other i 1154 end 1155 if ne ast_reseal_and_restore AS_ERR_CKPT_INCONSISTENT_LINEAGE 1156 ret 133 1157 end 1158 ret 0 1159 end 1160 1161 fn t_tamper_lineage_b to int 1162 c0 = new uchar 32 0 1163 c1 = new uchar 32 0 1164 gc = new uchar 32 0 1165 other = new uchar 32 0 1166 as_agent_id_from_seed ast_seed_other other 1167 1168 note 14d. parent/child cycle child0 <-> child1 1169 if ne ast_prep_tamper_snap 0 1170 ret 130 1171 end 1172 ast_tree_get 2 c0 1173 ast_tree_get 3 c1 1174 ast_tree_get 4 gc 1175 sb0 = ulong as_snap_find_branch c0 1176 sb1 = ulong as_snap_find_branch c1 1177 o0 = mul sb0 32 1178 o1 = mul sb1 32 1179 put as_snap_br_has_parent sb0 1 1180 put as_snap_br_has_parent sb1 1 1181 for i ulong 32 1182 put as_snap_br_parent_id add o0 i get c1 i 1183 put as_snap_br_parent_id add o1 i get c0 i 1184 end 1185 if ne ast_reseal_and_restore AS_ERR_CKPT_INCONSISTENT_LINEAGE 1186 ret 134 1187 end 1188 1189 note 14e. child of another agent's branch 1190 if ne ast_prep_tamper_snap 0 1191 ret 130 1192 end 1193 acnt = get as_snap_agent_cnt 0 1194 aoff = mul acnt 32 1195 for i ulong 32 1196 put as_snap_agents add aoff i get other i 1197 end 1198 put as_snap_agent_cnt 0 add acnt 1 1199 if ne as_snap_push_new_root other 1 AS_OK 1200 ret 135 1201 end 1202 sbg = ulong as_snap_find_branch gc 1203 go = mul sbg 32 1204 for i ulong 32 1205 put as_snap_br_agent_id add go i get other i 1206 end 1207 if ne ast_reseal_and_restore AS_ERR_CKPT_INCONSISTENT_LINEAGE 1208 ret 136 1209 end 1210 ret 0 1211 end 1212 1213 fn t_tamper_lineage to int 1214 rc = t_tamper_lineage_a 1215 if ne rc 0 1216 ret rc 1217 end 1218 ret t_tamper_lineage_b 1219 end 1220 1221 note === 15. test_suspend_resume_lifecycle_and_tamper_rejection + ABI errors === 1222 1223 fn t_suspend_errors aid view uchar * to int 1224 unk = new uchar 32 0xee 1225 ch = new uchar 32 0 1226 tok = new uint 1 77 1227 eref = new uchar 16 1 1228 buf = new uchar 16384 0 1229 if ne as_mgr_fork_branch 0 unk ch AS_ERR_PARENT_BRANCH_NOT_FOUND 1230 ret 147 1231 end 1232 if ne as_mgr_suspend 0 unk AS_ERR_BRANCH_NOT_FOUND 1233 ret 148 1234 end 1235 if ne as_mgr_resume 0 unk long AS_ERR_BRANCH_NOT_FOUND 1236 ret 149 1237 end 1238 if ne as_mgr_reconstruct_physical 0 unk AS_ERR_BRANCH_NOT_FOUND 1239 ret 150 1240 end 1241 if ne as_mgr_append_tokens 0 unk tok AS_ERR_BRANCH_NOT_FOUND 1242 ret 151 1243 end 1244 if ne as_mgr_add_epistemic_ref 0 unk eref AS_ERR_BRANCH_NOT_FOUND 1245 ret 151 1246 end 1247 if ne as_mgr_export_checkpoint 0 unk buf long AS_ERR_AGENT_NOT_FOUND 1248 ret 152 1249 end 1250 if ne as_mgr_checkpoint 0 unk ch AS_ERR_AGENT_NOT_FOUND 1251 ret 152 1252 end 1253 note Tamper detection on checkpoint restore (corrupt payload byte -> ChecksumMismatch) 1254 n = as_mgr_export_checkpoint 0 aid buf 1255 if le n 0 1256 ret 153 1257 end 1258 lidx = sub ulong n 5 1259 put buf lidx xor get buf lidx 0x55 1260 if ne as_mgr_restore_from_checkpoint 1 buf ulong n 2 AS_ERR_CHECKSUM_MISMATCH 1261 ret 154 1262 end 1263 note Corrupt magic -> DeserializationError 1264 put buf lidx xor get buf lidx 0x55 1265 put buf 0 xor get buf 0 0x01 1266 if ne as_mgr_restore_from_checkpoint 1 buf ulong n 2 AS_ERR_DESERIALIZATION 1267 ret 155 1268 end 1269 note Truncated checkpoint -> DeserializationError 1270 put buf 0 xor get buf 0 0x01 1271 if ne as_mgr_restore_from_checkpoint 1 buf 10 2 AS_ERR_DESERIALIZATION 1272 ret 156 1273 end 1274 ret 0 1275 end 1276 1277 fn t_suspend_resume to int 1278 as_mgr_init 0 1 1279 aid = new uchar 32 0 1280 rb = new uchar 32 0 1281 as_agent_id_from_seed ast_seed_suspend aid 1282 if ne as_mgr_register_agent 0 aid 100 rb AS_OK 1283 ret 140 1284 end 1285 note Suspend active branch 1286 if ne as_mgr_suspend 0 rb AS_OK 1287 ret 141 1288 end 1289 idx = as_mgr_find_branch 0 rb 1290 slot = as_br_slot 0 ulong idx 1291 if ne get as_br_inc_state slot AS_COMPUTE_SUSPENDED 1292 ret 142 1293 end 1294 note Suspending already suspended branch must fail 1295 if ne as_mgr_suspend 0 rb AS_ERR_ALREADY_SUSPENDED 1296 ret 143 1297 end 1298 note Appending tokens while suspended preserves suspended state and kv alloc 1299 tok = new uint 1 77 1300 if ne as_mgr_append_tokens 0 rb tok AS_OK 1301 ret 144 1302 end 1303 if ne get as_br_tok_cnt slot 1 1304 ret 144 1305 end 1306 if ne get as_br_inc_state slot AS_COMPUTE_SUSPENDED 1307 ret 144 1308 end 1309 if ne get as_br_inc_alloc_tok slot 0 1310 ret 144 1311 end 1312 note Resume returns new sequence ID and transitions to Idle 1313 seq = as_mgr_resume 0 rb 1314 if le seq 0 1315 ret 145 1316 end 1317 if ne get as_br_inc_state slot AS_COMPUTE_IDLE 1318 ret 146 1319 end 1320 ret t_suspend_errors aid 1321 end 1322 1323 fn main to int 1324 rc = t_id 1325 if ne rc 0 1326 ret rc 1327 end 1328 rc = t_history 1329 if ne rc 0 1330 ret rc 1331 end 1332 rc = t_lineage 1333 if ne rc 0 1334 ret rc 1335 end 1336 rc = t_reboot 1337 if ne rc 0 1338 ret rc 1339 end 1340 rc = t_fork_counters 1341 if ne rc 0 1342 ret rc 1343 end 1344 rc = t_stable_bytes 1345 if ne rc 0 1346 ret rc 1347 end 1348 rc = t_no_identity 1349 if ne rc 0 1350 ret rc 1351 end 1352 rc = t_noncontiguous 1353 if ne rc 0 1354 ret rc 1355 end 1356 rc = t_reregister 1357 if ne rc 0 1358 ret rc 1359 end 1360 rc = t_roundtrip 1361 if ne rc 0 1362 ret rc 1363 end 1364 rc = t_reregister_tree 1365 if ne rc 0 1366 ret rc 1367 end 1368 rc = t_tamper_basic 1369 if ne rc 0 1370 ret rc 1371 end 1372 rc = t_tamper_root 1373 if ne rc 0 1374 ret rc 1375 end 1376 rc = t_tamper_lineage 1377 if ne rc 0 1378 ret rc 1379 end 1380 rc = t_suspend_resume 1381 if ne rc 0 1382 ret rc 1383 end 1384 ret 0 1385 end