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 / aienux / crates / aienos-agent-state / agent_state_test.om

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