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.om

2086 lines · 48316 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/crates/aienos-agent-state/agent_state.om: Pure-Omega AIENOS Agent State ABI and Execution Incarnation Substrate.
3note Converted from aienux crates/aienos-agent-state/src/ (abi.rs, id.rs, incarnation.rs, history.rs, lineage.rs, manager.rs, lib.rs).
4note Compose with: si/directives/om2.om om/tls/sha2.om aienux/crates/aienos-agent-state/agent_state.om ...
5
6const AS_MAX_MGRS ulong 2
7const AS_MAX_AGENTS ulong 8
8const AS_MAX_BRANCHES ulong 32
9const AS_MAX_SLOTS ulong 64
10const AS_MAX_TOKENS ulong 64
11const AS_MAX_DEPTH ulong 16
12const AS_MAX_EPISTEMIC ulong 8
13const AS_MAX_TH ulong 4
14const AS_MAX_LIN ulong 4
15const AS_MAX_EPI ulong 4
16
17const AS_COMPUTE_IDLE uchar 0
18const AS_COMPUTE_PREFILLING uchar 1
19const AS_COMPUTE_DECODING uchar 2
20const AS_COMPUTE_SUSPENDED uchar 3
21const AS_COMPUTE_EVICTED uchar 4
22
23const AS_OK int 0
24const AS_ERR_AGENT_NOT_FOUND int -1
25const AS_ERR_BRANCH_NOT_FOUND int -2
26const AS_ERR_PARENT_BRANCH_NOT_FOUND int -3
27const AS_ERR_ALREADY_SUSPENDED int -4
28const AS_ERR_NOT_SUSPENDED int -5
29const AS_ERR_SERIALIZATION int -6
30const AS_ERR_DESERIALIZATION int -7
31const AS_ERR_CHECKSUM_MISMATCH int -8
32const AS_ERR_RECONSTRUCTION_FAILED int -9
33const AS_ERR_CAPACITY int -10
34
35note Distinct InvalidCheckpoint reason codes matching Rust StateError::InvalidCheckpoint
36const AS_ERR_CKPT_NO_AGENT int -20
37const AS_ERR_CKPT_DUP_AGENT int -21
38const AS_ERR_CKPT_ABSENT_AGENT int -22
39const AS_ERR_CKPT_DUP_BRANCH int -23
40const AS_ERR_CKPT_MISSING_ROOT int -24
41const AS_ERR_CKPT_INCONSISTENT_ROOT int -25
42const AS_ERR_CKPT_UNEXPECTED_ROOT int -26
43const AS_ERR_CKPT_MISSING_PARENT int -27
44const AS_ERR_CKPT_INCONSISTENT_LINEAGE int -28
45const AS_ERR_CKPT_NONCONTIGUOUS_FORK int -29
46
47text as_dom_agent "AIENOS_AGENT_GENESIS_v1:"
48text as_dom_root "AIENOS_ROOT_BRANCH_v1:"
49text as_dom_child "AIENOS_CHILD_BRANCH_v1:"
50text as_dom_token "AIENOS_TOKEN_GENESIS_v1"
51text as_ckpt_magic "AIENAST1"
52text as_hex_digits "0123456789abcdef"
53
54note Standalone TokenHistory pool (AS_MAX_TH = 4)
55global as_th_cnt ulong 4
56global as_th_rhash uchar 128
57global as_th_tseq ulong 256
58global as_th_tid uint 256
59global as_th_thash uchar 8192
60
61note Standalone Lineage pool (AS_MAX_LIN = 4)
62global as_lin_root uchar 128
63global as_lin_depth ulong 4
64global as_lin_cnt ulong 4
65global as_lin_anc uchar 2048
66
67note Standalone EpistemicRefs pool (AS_MAX_EPI = 4)
68global as_epi_cnt ulong 4
69global as_epi_ids uchar 512
70
71note AgentStateManager pool (AS_MAX_MGRS = 2, AS_MAX_BRANCHES = 32 per manager -> 64 slots)
72global as_mgr_inc ulong 2
73global as_mgr_next_seq uint 2
74global as_mgr_agent_cnt ulong 2
75global as_mgr_agents uchar 512
76global as_mgr_branch_cnt ulong 2
77
78global as_br_id uchar 2048
79global as_br_has_parent uchar 64
80global as_br_parent_id uchar 2048
81global as_br_agent_id uchar 2048
82global as_br_created_at ulong 64
83global as_br_child_cnt ulong 64
84
85global as_br_tok_cnt ulong 64
86global as_br_rhash uchar 2048
87global as_br_tok_seq ulong 4096
88global as_br_tok_id uint 4096
89global as_br_tok_hash uchar 131072
90
91global as_br_lin_root uchar 2048
92global as_br_lin_depth ulong 64
93global as_br_lin_cnt ulong 64
94global as_br_lin_anc uchar 32768
95
96global as_br_epi_cnt ulong 64
97global as_br_epi_ids uchar 8192
98
99global as_br_inc_id ulong 64
100global as_br_inc_seq uint 64
101global as_br_inc_kv_has uchar 64
102global as_br_inc_kv_ref ulong 64
103global as_br_inc_acc_has uchar 64
104global as_br_inc_acc_ref ulong 64
105global as_br_inc_state uchar 64
106global as_br_inc_alloc_tok ulong 64
107
108note Staging AgentSnapshot (used by export, decode, tamper inspection, and restore validation)
109global as_snap_inc ulong 1
110global as_snap_agent_cnt ulong 1
111global as_snap_agents uchar 256
112global as_snap_branch_cnt ulong 1
113
114global as_snap_br_id uchar 1024
115global as_snap_br_has_parent uchar 32
116global as_snap_br_parent_id uchar 1024
117global as_snap_br_agent_id uchar 1024
118global as_snap_br_created_at ulong 32
119
120global as_snap_br_tok_cnt ulong 32
121global as_snap_br_rhash uchar 1024
122global as_snap_br_tok_seq ulong 2048
123global as_snap_br_tok_id uint 2048
124global as_snap_br_tok_hash uchar 65536
125
126global as_snap_br_lin_root uchar 1024
127global as_snap_br_lin_depth ulong 32
128global as_snap_br_lin_cnt ulong 32
129global as_snap_br_lin_anc uchar 16384
130
131global as_snap_br_epi_cnt ulong 32
132global as_snap_br_epi_ids uchar 4096
133
134note Restore validation sorted order and child counters
135global as_val_order ulong 32
136global as_val_child_cnt ulong 32
137
138note === Byte encoding helpers (big-endian) ===
139
140fn as_put_u32_be buf edit uchar * off ulong v uint to void
141put buf off uchar and shr v 24 0xff
142put buf add off 1 uchar and shr v 16 0xff
143put buf add off 2 uchar and shr v 8 0xff
144put buf add off 3 uchar and v 0xff
145end
146
147fn as_put_u64_be buf edit uchar * off ulong v ulong to void
148for i ulong 8
149sh = mul sub 7 i 8
150put buf add off i uchar and shr v sh 0xff
151end
152end
153
154fn as_get_u32_be buf view uchar * off ulong to uint
155b0 = uint get buf off
156b1 = uint get buf add off 1
157b2 = uint get buf add off 2
158b3 = uint get buf add off 3
159hi = or shl b0 24 shl b1 16
160lo = or shl b2 8 b3
161ret or hi lo
162end
163
164fn as_get_u64_be buf view uchar * off ulong to ulong
165acc = ulong 0
166for i ulong 8
167b = ulong get buf add off i
168sh = mul sub 7 i 8
169acc = or acc shl b sh
170end
171ret acc
172end
173
174note === Cryptographic ID helpers (id.rs) ===
175
176fn as_id_eq a view uchar * b view uchar * to bool
177for i ulong 32
178if ne get a i get b i
179ret false
180end
181end
182ret true
183end
184
185fn as_id_cmp a view uchar * b view uchar * to int
186for i ulong 32
187va = get a i
188vb = get b i
189if lt va vb
190ret -1
191end
192if gt va vb
193ret 1
194end
195end
196ret 0
197end
198
199fn as_id_is_zero a view uchar * to bool
200for i ulong 32
201if ne get a i 0
202ret false
203end
204end
205ret true
206end
207
208fn as_rec16_eq a view uchar * b view uchar * to bool
209for i ulong 16
210if ne get a i get b i
211ret false
212end
213end
214ret true
215end
216
217fn as_id_to_hex id view uchar * dst edit uchar * to void
218for i ulong 32
219b = ulong get id i
220hi = and shr b 4 0xf
221lo = and b 0xf
222put dst mul i 2 get as_hex_digits hi
223put dst add mul i 2 1 get as_hex_digits lo
224end
225end
226
227fn as_agent_id_from_seed seed view uchar * dst edit uchar * to void
228ctx = new ulong 40 0
229sha256_init ctx
230sha256_update ctx as_dom_agent
231if gt len seed 0
232sha256_update ctx seed
233end
234sha256_final ctx dst
235end
236
237fn as_branch_id_root agent_id view uchar * dst edit uchar * to void
238ctx = new ulong 40 0
239sha256_init ctx
240sha256_update ctx as_dom_root
241sha256_update ctx part agent_id 0 32
242sha256_final ctx dst
243end
244
245fn as_branch_id_derive parent view uchar * branch_index ulong dst edit uchar * to void
246ctx = new ulong 40 0
247ibuf = new uchar 8 0
248as_put_u64_be ibuf 0 branch_index
249sha256_init ctx
250sha256_update ctx as_dom_child
251sha256_update ctx part parent 0 32
252sha256_update ctx ibuf
253sha256_final ctx dst
254end
255
256note === TokenHistory primitives and standalone pool (history.rs) ===
257
258fn as_token_genesis_hash dst edit uchar * to void
259dlen = sha2_digest 256 as_dom_token dst
260if ne dlen 32
261trap 8
262end
263end
264
265fn as_token_step_hash prev_hash view uchar * seq ulong token_id uint dst edit uchar * to void
266ctx = new ulong 40 0
267sbuf = new uchar 8 0
268tbuf = new uchar 4 0
269as_put_u64_be sbuf 0 seq
270as_put_u32_be tbuf 0 token_id
271sha256_init ctx
272sha256_update ctx part prev_hash 0 32
273sha256_update ctx sbuf
274sha256_update ctx tbuf
275sha256_final ctx dst
276end
277
278fn as_th_new slot ulong to void
279put as_th_cnt slot 0
280gh = new uchar 32 0
281as_token_genesis_hash gh
282base = mul slot 32
283for i ulong 32
284put as_th_rhash add base i get gh i
285end
286end
287
288fn as_th_count slot ulong to ulong
289ret get as_th_cnt slot
290end
291
292fn as_th_is_empty slot ulong to bool
293ret eq get as_th_cnt slot 0
294end
295
296fn as_th_rolling_hash slot ulong dst edit uchar * to void
297base = mul slot 32
298for i ulong 32
299put dst i get as_th_rhash add base i
300end
301end
302
303fn as_th_get_token slot ulong idx ulong to uint
304base = mul slot AS_MAX_TOKENS
305ret get as_th_tid add base idx
306end
307
308fn as_th_append slot ulong token_id uint to int
309cnt = get as_th_cnt slot
310if ge cnt AS_MAX_TOKENS
311ret AS_ERR_CAPACITY
312end
313prev = new uchar 32 0
314nh = new uchar 32 0
315rbase = mul slot 32
316for i ulong 32
317put prev i get as_th_rhash add rbase i
318end
319as_token_step_hash prev cnt token_id nh
320tbase = add mul slot AS_MAX_TOKENS cnt
321put as_th_tseq tbase cnt
322put as_th_tid tbase token_id
323hbase = mul tbase 32
324for i ulong 32
325put as_th_thash add hbase i get nh i
326put as_th_rhash add rbase i get nh i
327end
328put as_th_cnt slot add cnt 1
329ret AS_OK
330end
331
332fn as_th_append_tokens slot ulong tokens view uint * to int
333n = len tokens
334cnt = get as_th_cnt slot
335if gt add cnt n AS_MAX_TOKENS
336ret AS_ERR_CAPACITY
337end
338for i ulong n bound 64
339rc = as_th_append slot get tokens i
340if ne rc AS_OK
341ret rc
342end
343end
344ret AS_OK
345end
346
347fn as_th_copy_tok dst_slot ulong src_slot ulong k ulong to void
348dt = add mul dst_slot AS_MAX_TOKENS k
349st = add mul src_slot AS_MAX_TOKENS k
350put as_th_tseq dt get as_th_tseq st
351put as_th_tid dt get as_th_tid st
352dh = mul dt 32
353sh = mul st 32
354for j ulong 32
355put as_th_thash add dh j get as_th_thash add sh j
356end
357end
358
359fn as_th_fork dst_slot ulong src_slot ulong to void
360cnt = get as_th_cnt src_slot
361put as_th_cnt dst_slot cnt
362drb = mul dst_slot 32
363srb = mul src_slot 32
364for i ulong 32
365put as_th_rhash add drb i get as_th_rhash add srb i
366end
367for k ulong cnt bound 64
368as_th_copy_tok dst_slot src_slot k
369end
370end
371
372note === Standalone Lineage and EpistemicRefs pools (lineage.rs) ===
373
374fn as_lin_new_root slot ulong root_agent view uchar * to void
375base = mul slot 32
376for i ulong 32
377put as_lin_root add base i get root_agent i
378end
379put as_lin_depth slot 0
380put as_lin_cnt slot 0
381end
382
383fn as_lin_depth_of slot ulong to ulong
384ret get as_lin_depth slot
385end
386
387fn as_lin_copy_anc dst_slot ulong src_slot ulong k ulong to void
388da = add mul dst_slot mul AS_MAX_DEPTH 32 mul k 32
389sa = add mul src_slot mul AS_MAX_DEPTH 32 mul k 32
390for j ulong 32
391put as_lin_anc add da j get as_lin_anc add sa j
392end
393end
394
395fn as_lin_extend dst_slot ulong src_slot ulong branch_id view uchar * to int
396cnt = get as_lin_cnt src_slot
397if ge cnt AS_MAX_DEPTH
398ret AS_ERR_CAPACITY
399end
400dr = mul dst_slot 32
401sr = mul src_slot 32
402for i ulong 32
403put as_lin_root add dr i get as_lin_root add sr i
404end
405for k ulong cnt bound 16
406as_lin_copy_anc dst_slot src_slot k
407end
408noff = add mul dst_slot mul AS_MAX_DEPTH 32 mul cnt 32
409for j ulong 32
410put as_lin_anc add noff j get branch_id j
411end
412put as_lin_cnt dst_slot add cnt 1
413put as_lin_depth dst_slot add get as_lin_depth src_slot 1
414ret AS_OK
415end
416
417fn as_lin_contains slot ulong branch_id view uchar * to bool
418cnt = get as_lin_cnt slot
419base = mul slot mul AS_MAX_DEPTH 32
420for k ulong cnt bound 16
421off = add base mul k 32
422same = true
423for j ulong 32
424if ne get as_lin_anc add off j get branch_id j
425same = false
426end
427end
428if same
429ret true
430end
431end
432ret false
433end
434
435fn as_epi_new slot ulong to void
436put as_epi_cnt slot 0
437end
438
439fn as_epi_count slot ulong to ulong
440ret get as_epi_cnt slot
441end
442
443fn as_epi_is_empty slot ulong to bool
444ret eq get as_epi_cnt slot 0
445end
446
447fn as_epi_contains slot ulong rec_id view uchar * to bool
448cnt = get as_epi_cnt slot
449base = mul slot mul AS_MAX_EPISTEMIC 16
450for k ulong cnt bound 8
451off = add base mul k 16
452same = true
453for j ulong 16
454if ne get as_epi_ids add off j get rec_id j
455same = false
456end
457end
458if same
459ret true
460end
461end
462ret false
463end
464
465fn as_epi_insert slot ulong rec_id view uchar * to int
466if as_epi_contains slot rec_id
467ret AS_OK
468end
469cnt = get as_epi_cnt slot
470if ge cnt AS_MAX_EPISTEMIC
471ret AS_ERR_CAPACITY
472end
473off = add mul slot mul AS_MAX_EPISTEMIC 16 mul cnt 16
474for j ulong 16
475put as_epi_ids add off j get rec_id j
476end
477put as_epi_cnt slot add cnt 1
478ret AS_OK
479end
480
481note === Manager slot helpers (incarnation.rs and manager.rs) ===
482
483fn as_br_slot mgr ulong bidx ulong to ulong
484ret add mul mgr AS_MAX_BRANCHES bidx
485end
486
487fn as_br_inc_init slot ulong inc_id ulong seq uint to void
488put as_br_inc_id slot inc_id
489put as_br_inc_seq slot seq
490put as_br_inc_kv_has slot 0
491put as_br_inc_kv_ref slot 0
492put as_br_inc_acc_has slot 0
493put as_br_inc_acc_ref slot 0
494put as_br_inc_state slot AS_COMPUTE_IDLE
495put as_br_inc_alloc_tok slot 0
496end
497
498fn as_br_inc_evict slot ulong to void
499put as_br_inc_kv_has slot 0
500put as_br_inc_kv_ref slot 0
501put as_br_inc_acc_has slot 0
502put as_br_inc_acc_ref slot 0
503put as_br_inc_alloc_tok slot 0
504put as_br_inc_state slot AS_COMPUTE_EVICTED
505end
506
507fn as_mgr_init mgr ulong incarnation_id ulong to void
508put as_mgr_inc mgr incarnation_id
509put as_mgr_next_seq mgr 1
510put as_mgr_agent_cnt mgr 0
511put as_mgr_branch_cnt mgr 0
512end
513
514fn as_mgr_has_agent mgr ulong agent_id view uchar * to bool
515cnt = get as_mgr_agent_cnt mgr
516base = mul mgr mul AS_MAX_AGENTS 32
517for a ulong cnt bound 8
518off = add base mul a 32
519same = true
520for i ulong 32
521if ne get as_mgr_agents add off i get agent_id i
522same = false
523end
524end
525if same
526ret true
527end
528end
529ret false
530end
531
532fn as_mgr_find_branch mgr ulong branch_id view uchar * to long
533cnt = get as_mgr_branch_cnt mgr
534for b ulong cnt bound 32
535slot = as_br_slot mgr b
536off = mul slot 32
537same = true
538for i ulong 32
539if ne get as_br_id add off i get branch_id i
540same = false
541end
542end
543if same
544ret long b
545end
546end
547ret -1
548end
549
550fn as_br_init_root slot ulong agent_id view uchar * root_id view uchar * ts ulong to void
551off = mul slot 32
552for i ulong 32
553put as_br_id add off i get root_id i
554put as_br_parent_id add off i 0
555put as_br_agent_id add off i get agent_id i
556put as_br_lin_root add off i get agent_id i
557end
558put as_br_has_parent slot 0
559put as_br_created_at slot ts
560put as_br_child_cnt slot 0
561put as_br_tok_cnt slot 0
562gh = new uchar 32 0
563as_token_genesis_hash gh
564for i ulong 32
565put as_br_rhash add off i get gh i
566end
567put as_br_lin_depth slot 0
568put as_br_lin_cnt slot 0
569put as_br_epi_cnt slot 0
570end
571
572fn as_br_copy_tok dst_slot ulong src_slot ulong k ulong to void
573dt = add mul dst_slot AS_MAX_TOKENS k
574st = add mul src_slot AS_MAX_TOKENS k
575put as_br_tok_seq dt get as_br_tok_seq st
576put as_br_tok_id dt get as_br_tok_id st
577dh = mul dt 32
578sh = mul st 32
579for j ulong 32
580put as_br_tok_hash add dh j get as_br_tok_hash add sh j
581end
582end
583
584fn as_br_copy_history dst_slot ulong src_slot ulong to void
585cnt = get as_br_tok_cnt src_slot
586put as_br_tok_cnt dst_slot cnt
587dr = mul dst_slot 32
588sr = mul src_slot 32
589for i ulong 32
590put as_br_rhash add dr i get as_br_rhash add sr i
591end
592for k ulong cnt bound 64
593as_br_copy_tok dst_slot src_slot k
594end
595end
596
597fn as_br_copy_lin_anc dst_slot ulong src_slot ulong k ulong to void
598da = add mul dst_slot mul AS_MAX_DEPTH 32 mul k 32
599sa = add mul src_slot mul AS_MAX_DEPTH 32 mul k 32
600for j ulong 32
601put as_br_lin_anc add da j get as_br_lin_anc add sa j
602end
603end
604
605fn as_br_copy_epi_one dst_slot ulong src_slot ulong k ulong to void
606doff = add mul dst_slot mul AS_MAX_EPISTEMIC 16 mul k 16
607soff = add mul src_slot mul AS_MAX_EPISTEMIC 16 mul k 16
608for j ulong 16
609put as_br_epi_ids add doff j get as_br_epi_ids add soff j
610end
611end
612
613fn as_br_copy_epi dst_slot ulong src_slot ulong to void
614ecnt = get as_br_epi_cnt src_slot
615put as_br_epi_cnt dst_slot ecnt
616for k ulong ecnt bound 8
617as_br_copy_epi_one dst_slot src_slot k
618end
619end
620
621fn as_br_copy_lin_and_epi dst_slot ulong src_slot ulong child_id view uchar * to void
622dr = mul dst_slot 32
623sr = mul src_slot 32
624for i ulong 32
625put as_br_lin_root add dr i get as_br_lin_root add sr i
626end
627lcnt = get as_br_lin_cnt src_slot
628for k ulong lcnt bound 16
629as_br_copy_lin_anc dst_slot src_slot k
630end
631noff = add mul dst_slot mul AS_MAX_DEPTH 32 mul lcnt 32
632for j ulong 32
633put as_br_lin_anc add noff j get child_id j
634end
635put as_br_lin_cnt dst_slot add lcnt 1
636put as_br_lin_depth dst_slot add get as_br_lin_depth src_slot 1
637as_br_copy_epi dst_slot src_slot
638end
639
640fn as_br_fork_slot dst_slot ulong src_slot ulong child_id view uchar * ts ulong to void
641doff = mul dst_slot 32
642soff = mul src_slot 32
643for i ulong 32
644put as_br_id add doff i get child_id i
645put as_br_parent_id add doff i get as_br_id add soff i
646put as_br_agent_id add doff i get as_br_agent_id add soff i
647end
648put as_br_has_parent dst_slot 1
649put as_br_created_at dst_slot ts
650put as_br_child_cnt dst_slot 0
651as_br_copy_history dst_slot src_slot
652as_br_copy_lin_and_epi dst_slot src_slot child_id
653end
654
655fn as_mgr_register_agent mgr ulong agent_id view uchar * ts ulong dst_branch edit uchar * to int
656root_id = new uchar 32 0
657as_branch_id_root agent_id root_id
658for i ulong 32
659put dst_branch i get root_id i
660end
661if as_mgr_has_agent mgr agent_id
662found = as_mgr_find_branch mgr root_id
663if ge found 0
664ret AS_OK
665end
666else
667acnt = get as_mgr_agent_cnt mgr
668if ge acnt AS_MAX_AGENTS
669ret AS_ERR_CAPACITY
670end
671aoff = add mul mgr mul AS_MAX_AGENTS 32 mul acnt 32
672for i ulong 32
673put as_mgr_agents add aoff i get agent_id i
674end
675put as_mgr_agent_cnt mgr add acnt 1
676end
677bcnt = get as_mgr_branch_cnt mgr
678if ge bcnt AS_MAX_BRANCHES
679ret AS_ERR_CAPACITY
680end
681slot = as_br_slot mgr bcnt
682as_br_init_root slot agent_id root_id ts
683seq = get as_mgr_next_seq mgr
684put as_mgr_next_seq mgr add seq 1
685as_br_inc_init slot get as_mgr_inc mgr seq
686put as_mgr_branch_cnt mgr add bcnt 1
687ret AS_OK
688end
689
690fn as_br_append_one_token slot ulong token_id uint to int
691cnt = get as_br_tok_cnt slot
692if ge cnt AS_MAX_TOKENS
693ret AS_ERR_CAPACITY
694end
695prev = new uchar 32 0
696nh = new uchar 32 0
697rbase = mul slot 32
698for i ulong 32
699put prev i get as_br_rhash add rbase i
700end
701as_token_step_hash prev cnt token_id nh
702tidx = add mul slot AS_MAX_TOKENS cnt
703put as_br_tok_seq tidx cnt
704put as_br_tok_id tidx token_id
705hbase = mul tidx 32
706for i ulong 32
707put as_br_tok_hash add hbase i get nh i
708put as_br_rhash add rbase i get nh i
709end
710put as_br_tok_cnt slot add cnt 1
711ret AS_OK
712end
713
714fn as_mgr_append_tokens mgr ulong branch_id view uchar * tokens view uint * to int
715bidx = as_mgr_find_branch mgr branch_id
716if lt bidx 0
717ret AS_ERR_BRANCH_NOT_FOUND
718end
719slot = as_br_slot mgr ulong bidx
720n = len tokens
721cur = get as_br_tok_cnt slot
722if gt add cur n AS_MAX_TOKENS
723ret AS_ERR_CAPACITY
724end
725for i ulong n bound 64
726rc = as_br_append_one_token slot get tokens i
727if ne rc AS_OK
728ret rc
729end
730end
731st = get as_br_inc_state slot
732if ne st AS_COMPUTE_SUSPENDED
733if ne st AS_COMPUTE_EVICTED
734put as_br_inc_alloc_tok slot get as_br_tok_cnt slot
735put as_br_inc_state slot AS_COMPUTE_DECODING
736end
737end
738ret AS_OK
739end
740
741fn as_br_epi_eq_one slot ulong k ulong rec_id view uchar * to bool
742off = add mul slot mul AS_MAX_EPISTEMIC 16 mul k 16
743for j ulong 16
744if ne get as_br_epi_ids add off j get rec_id j
745ret false
746end
747end
748ret true
749end
750
751fn as_mgr_branch_epi_contains mgr ulong bidx ulong rec_id view uchar * to bool
752slot = as_br_slot mgr bidx
753cnt = get as_br_epi_cnt slot
754for k ulong cnt bound 8
755if as_br_epi_eq_one slot k rec_id
756ret true
757end
758end
759ret false
760end
761
762fn as_mgr_add_epistemic_ref mgr ulong branch_id view uchar * rec_id view uchar * to int
763bidx = as_mgr_find_branch mgr branch_id
764if lt bidx 0
765ret AS_ERR_BRANCH_NOT_FOUND
766end
767if as_mgr_branch_epi_contains mgr ulong bidx rec_id
768ret AS_OK
769end
770slot = as_br_slot mgr ulong bidx
771cnt = get as_br_epi_cnt slot
772if ge cnt AS_MAX_EPISTEMIC
773ret AS_ERR_CAPACITY
774end
775off = add mul slot mul AS_MAX_EPISTEMIC 16 mul cnt 16
776for j ulong 16
777put as_br_epi_ids add off j get rec_id j
778end
779put as_br_epi_cnt slot add cnt 1
780ret AS_OK
781end
782
783fn as_mgr_fork_branch mgr ulong parent_id view uchar * dst_child edit uchar * to int
784pidx = as_mgr_find_branch mgr parent_id
785if lt pidx 0
786ret AS_ERR_PARENT_BRANCH_NOT_FOUND
787end
788pslot = as_br_slot mgr ulong pidx
789bcnt = get as_mgr_branch_cnt mgr
790if ge bcnt AS_MAX_BRANCHES
791ret AS_ERR_CAPACITY
792end
793if ge get as_br_lin_cnt pslot AS_MAX_DEPTH
794ret AS_ERR_CAPACITY
795end
796cidx = get as_br_child_cnt pslot
797put as_br_child_cnt pslot add cidx 1
798cid = new uchar 32 0
799as_branch_id_derive parent_id cidx cid
800for i ulong 32
801put dst_child i get cid i
802end
803cslot = as_br_slot mgr bcnt
804as_br_fork_slot cslot pslot cid add 1000 cidx
805seq = get as_mgr_next_seq mgr
806put as_mgr_next_seq mgr add seq 1
807as_br_inc_init cslot get as_mgr_inc mgr seq
808put as_mgr_branch_cnt mgr add bcnt 1
809ret AS_OK
810end
811
812fn as_mgr_suspend mgr ulong branch_id view uchar * to int
813bidx = as_mgr_find_branch mgr branch_id
814if lt bidx 0
815ret AS_ERR_BRANCH_NOT_FOUND
816end
817slot = as_br_slot mgr ulong bidx
818if eq get as_br_inc_state slot AS_COMPUTE_SUSPENDED
819ret AS_ERR_ALREADY_SUSPENDED
820end
821put as_br_inc_state slot AS_COMPUTE_SUSPENDED
822ret AS_OK
823end
824
825fn as_mgr_resume mgr ulong branch_id view uchar * to long
826bidx = as_mgr_find_branch mgr branch_id
827if lt bidx 0
828ret long AS_ERR_BRANCH_NOT_FOUND
829end
830slot = as_br_slot mgr ulong bidx
831seq = get as_mgr_next_seq mgr
832put as_mgr_next_seq mgr add seq 1
833put as_br_inc_seq slot seq
834if eq get as_br_inc_state slot AS_COMPUTE_SUSPENDED
835put as_br_inc_state slot AS_COMPUTE_IDLE
836end
837ret long seq
838end
839
840fn as_mgr_reconstruct_physical mgr ulong branch_id view uchar * to int
841bidx = as_mgr_find_branch mgr branch_id
842if lt bidx 0
843ret AS_ERR_BRANCH_NOT_FOUND
844end
845slot = as_br_slot mgr ulong bidx
846tlen = get as_br_tok_cnt slot
847put as_br_inc_alloc_tok slot tlen
848put as_br_inc_kv_has slot 1
849put as_br_inc_kv_ref slot add 0xdead0000 tlen
850put as_br_inc_state slot AS_COMPUTE_DECODING
851ret AS_OK
852end
853
854note === Snapshot staging helpers (copy manager <-> snapshot, encode/decode/seal, validate) ===
855
856fn as_snap_clear to void
857put as_snap_inc 0 0
858put as_snap_agent_cnt 0 0
859put as_snap_branch_cnt 0 0
860end
861
862fn as_br_to_snap_tok sb ulong slot ulong k ulong to void
863st = add mul sb AS_MAX_TOKENS k
864bt = add mul slot AS_MAX_TOKENS k
865put as_snap_br_tok_seq st get as_br_tok_seq bt
866put as_snap_br_tok_id st get as_br_tok_id bt
867sh = mul st 32
868bh = mul bt 32
869for j ulong 32
870put as_snap_br_tok_hash add sh j get as_br_tok_hash add bh j
871end
872end
873
874fn as_br_to_snap_hist sb ulong slot ulong to void
875tcnt = get as_br_tok_cnt slot
876put as_snap_br_tok_cnt sb tcnt
877sr = mul sb 32
878br = mul slot 32
879for i ulong 32
880put as_snap_br_rhash add sr i get as_br_rhash add br i
881end
882for k ulong tcnt bound 64
883as_br_to_snap_tok sb slot k
884end
885end
886
887fn as_br_to_snap_lin_one sb ulong slot ulong k ulong to void
888soff = add mul sb mul AS_MAX_DEPTH 32 mul k 32
889boff = add mul slot mul AS_MAX_DEPTH 32 mul k 32
890for j ulong 32
891put as_snap_br_lin_anc add soff j get as_br_lin_anc add boff j
892end
893end
894
895fn as_br_to_snap_lin sb ulong slot ulong to void
896sr = mul sb 32
897br = mul slot 32
898for i ulong 32
899put as_snap_br_lin_root add sr i get as_br_lin_root add br i
900end
901put as_snap_br_lin_depth sb get as_br_lin_depth slot
902lcnt = get as_br_lin_cnt slot
903put as_snap_br_lin_cnt sb lcnt
904for k ulong lcnt bound 16
905as_br_to_snap_lin_one sb slot k
906end
907end
908
909fn as_br_to_snap_epi_one sb ulong slot ulong k ulong to void
910soff = add mul sb mul AS_MAX_EPISTEMIC 16 mul k 16
911boff = add mul slot mul AS_MAX_EPISTEMIC 16 mul k 16
912for j ulong 16
913put as_snap_br_epi_ids add soff j get as_br_epi_ids add boff j
914end
915end
916
917fn as_br_to_snap_epi sb ulong slot ulong to void
918ecnt = get as_br_epi_cnt slot
919put as_snap_br_epi_cnt sb ecnt
920for k ulong ecnt bound 8
921as_br_to_snap_epi_one sb slot k
922end
923end
924
925fn as_br_to_snap sb ulong slot ulong to void
926so = mul sb 32
927bo = mul slot 32
928for i ulong 32
929put as_snap_br_id add so i get as_br_id add bo i
930put as_snap_br_parent_id add so i get as_br_parent_id add bo i
931put as_snap_br_agent_id add so i get as_br_agent_id add bo i
932end
933put as_snap_br_has_parent sb get as_br_has_parent slot
934put as_snap_br_created_at sb get as_br_created_at slot
935as_br_to_snap_hist sb slot
936as_br_to_snap_lin sb slot
937as_br_to_snap_epi sb slot
938end
939
940fn as_snap_to_br_tok slot ulong sb ulong k ulong to void
941bt = add mul slot AS_MAX_TOKENS k
942st = add mul sb AS_MAX_TOKENS k
943put as_br_tok_seq bt get as_snap_br_tok_seq st
944put as_br_tok_id bt get as_snap_br_tok_id st
945bh = mul bt 32
946sh = mul st 32
947for j ulong 32
948put as_br_tok_hash add bh j get as_snap_br_tok_hash add sh j
949end
950end
951
952fn as_snap_to_br_hist slot ulong sb ulong to void
953tcnt = get as_snap_br_tok_cnt sb
954put as_br_tok_cnt slot tcnt
955br = mul slot 32
956sr = mul sb 32
957for i ulong 32
958put as_br_rhash add br i get as_snap_br_rhash add sr i
959end
960for k ulong tcnt bound 64
961as_snap_to_br_tok slot sb k
962end
963end
964
965fn as_snap_to_br_lin_one slot ulong sb ulong k ulong to void
966boff = add mul slot mul AS_MAX_DEPTH 32 mul k 32
967soff = add mul sb mul AS_MAX_DEPTH 32 mul k 32
968for j ulong 32
969put as_br_lin_anc add boff j get as_snap_br_lin_anc add soff j
970end
971end
972
973fn as_snap_to_br_lin slot ulong sb ulong to void
974br = mul slot 32
975sr = mul sb 32
976for i ulong 32
977put as_br_lin_root add br i get as_snap_br_lin_root add sr i
978end
979put as_br_lin_depth slot get as_snap_br_lin_depth sb
980lcnt = get as_snap_br_lin_cnt sb
981put as_br_lin_cnt slot lcnt
982for k ulong lcnt bound 16
983as_snap_to_br_lin_one slot sb k
984end
985end
986
987fn as_snap_to_br_epi_one slot ulong sb ulong k ulong to void
988boff = add mul slot mul AS_MAX_EPISTEMIC 16 mul k 16
989soff = add mul sb mul AS_MAX_EPISTEMIC 16 mul k 16
990for j ulong 16
991put as_br_epi_ids add boff j get as_snap_br_epi_ids add soff j
992end
993end
994
995fn as_snap_to_br_epi slot ulong sb ulong to void
996ecnt = get as_snap_br_epi_cnt sb
997put as_br_epi_cnt slot ecnt
998for k ulong ecnt bound 8
999as_snap_to_br_epi_one slot sb k
1000end
1001end
1002
1003fn as_snap_to_br slot ulong sb ulong to void
1004bo = mul slot 32
1005so = mul sb 32
1006for i ulong 32
1007put as_br_id add bo i get as_snap_br_id add so i
1008put as_br_parent_id add bo i get as_snap_br_parent_id add so i
1009put as_br_agent_id add bo i get as_snap_br_agent_id add so i
1010end
1011put as_br_has_parent slot get as_snap_br_has_parent sb
1012put as_br_created_at slot get as_snap_br_created_at sb
1013as_snap_to_br_hist slot sb
1014as_snap_to_br_lin slot sb
1015as_snap_to_br_epi slot sb
1016end
1017
1018fn as_br_id_cmp_slots s1 ulong s2 ulong to int
1019o1 = mul s1 32
1020o2 = mul s2 32
1021for i ulong 32
1022a = get as_br_id add o1 i
1023b = get as_br_id add o2 i
1024if lt a b
1025ret -1
1026end
1027if gt a b
1028ret 1
1029end
1030end
1031ret 0
1032end
1033
1034fn as_snap_br_id_cmp sb1 ulong sb2 ulong to int
1035o1 = mul sb1 32
1036o2 = mul sb2 32
1037for i ulong 32
1038a = get as_snap_br_id add o1 i
1039b = get as_snap_br_id add o2 i
1040if lt a b
1041ret -1
1042end
1043if gt a b
1044ret 1
1045end
1046end
1047ret 0
1048end
1049
1050fn as_snap_find_branch branch_id view uchar * to long
1051cnt = get as_snap_branch_cnt 0
1052for sb ulong cnt bound 32
1053off = mul sb 32
1054same = true
1055for i ulong 32
1056if ne get as_snap_br_id add off i get branch_id i
1057same = false
1058end
1059end
1060if same
1061ret long sb
1062end
1063end
1064ret -1
1065end
1066
1067fn as_snap_copy_tok dst_b ulong src_b ulong k ulong to void
1068dt = add mul dst_b AS_MAX_TOKENS k
1069st = add mul src_b AS_MAX_TOKENS k
1070put as_snap_br_tok_seq dt get as_snap_br_tok_seq st
1071put as_snap_br_tok_id dt get as_snap_br_tok_id st
1072dh = mul dt 32
1073sh = mul st 32
1074for j ulong 32
1075put as_snap_br_tok_hash add dh j get as_snap_br_tok_hash add sh j
1076end
1077end
1078
1079fn as_snap_copy_branch_hist dst_b ulong src_b ulong to void
1080tcnt = get as_snap_br_tok_cnt src_b
1081put as_snap_br_tok_cnt dst_b tcnt
1082for k ulong tcnt bound 64
1083as_snap_copy_tok dst_b src_b k
1084end
1085end
1086
1087fn as_snap_copy_lin_one dst_b ulong src_b ulong k ulong to void
1088doff = add mul dst_b mul AS_MAX_DEPTH 32 mul k 32
1089soff = add mul src_b mul AS_MAX_DEPTH 32 mul k 32
1090for j ulong 32
1091put as_snap_br_lin_anc add doff j get as_snap_br_lin_anc add soff j
1092end
1093end
1094
1095fn as_snap_copy_branch_lin dst_b ulong src_b ulong to void
1096lcnt = get as_snap_br_lin_cnt src_b
1097put as_snap_br_lin_cnt dst_b lcnt
1098for k ulong lcnt bound 16
1099as_snap_copy_lin_one dst_b src_b k
1100end
1101end
1102
1103fn as_snap_copy_epi_one dst_b ulong src_b ulong k ulong to void
1104doff = add mul dst_b mul AS_MAX_EPISTEMIC 16 mul k 16
1105soff = add mul src_b mul AS_MAX_EPISTEMIC 16 mul k 16
1106for j ulong 16
1107put as_snap_br_epi_ids add doff j get as_snap_br_epi_ids add soff j
1108end
1109end
1110
1111fn as_snap_copy_branch_epi dst_b ulong src_b ulong to void
1112ecnt = get as_snap_br_epi_cnt src_b
1113put as_snap_br_epi_cnt dst_b ecnt
1114for k ulong ecnt bound 8
1115as_snap_copy_epi_one dst_b src_b k
1116end
1117end
1118
1119fn as_snap_copy_branch dst_b ulong src_b ulong to void
1120doff = mul dst_b 32
1121soff = mul src_b 32
1122for i ulong 32
1123put as_snap_br_id add doff i get as_snap_br_id add soff i
1124put as_snap_br_parent_id add doff i get as_snap_br_parent_id add soff i
1125put as_snap_br_agent_id add doff i get as_snap_br_agent_id add soff i
1126put as_snap_br_rhash add doff i get as_snap_br_rhash add soff i
1127put as_snap_br_lin_root add doff i get as_snap_br_lin_root add soff i
1128end
1129put as_snap_br_has_parent dst_b get as_snap_br_has_parent src_b
1130put as_snap_br_created_at dst_b get as_snap_br_created_at src_b
1131put as_snap_br_lin_depth dst_b get as_snap_br_lin_depth src_b
1132as_snap_copy_branch_hist dst_b src_b
1133as_snap_copy_branch_lin dst_b src_b
1134as_snap_copy_branch_epi dst_b src_b
1135end
1136
1137fn as_snap_remove_branch sb ulong to void
1138cnt = get as_snap_branch_cnt 0
1139if ge sb cnt
1140ret
1141end
1142last = sub cnt 1
1143if lt sb last
1144for idx ulong sub last sb bound 32
1145dst_b = add sb idx
1146as_snap_copy_branch dst_b add dst_b 1
1147end
1148end
1149put as_snap_branch_cnt 0 last
1150end
1151
1152fn as_snap_push_new_root agent_id view uchar * ts ulong to int
1153cnt = get as_snap_branch_cnt 0
1154if ge cnt AS_MAX_BRANCHES
1155ret AS_ERR_CAPACITY
1156end
1157rid = new uchar 32 0
1158gh = new uchar 32 0
1159as_branch_id_root agent_id rid
1160as_token_genesis_hash gh
1161off = mul cnt 32
1162for i ulong 32
1163put as_snap_br_id add off i get rid i
1164put as_snap_br_parent_id add off i 0
1165put as_snap_br_agent_id add off i get agent_id i
1166put as_snap_br_rhash add off i get gh i
1167put as_snap_br_lin_root add off i get agent_id i
1168end
1169put as_snap_br_has_parent cnt 0
1170put as_snap_br_created_at cnt ts
1171put as_snap_br_tok_cnt cnt 0
1172put as_snap_br_lin_depth cnt 0
1173put as_snap_br_lin_cnt cnt 0
1174put as_snap_br_epi_cnt cnt 0
1175put as_snap_branch_cnt 0 add cnt 1
1176ret AS_OK
1177end
1178
1179fn as_snap_encode_br_hdr buf edit uchar * pos ulong sb ulong to long
1180cap = len buf
1181if gt add pos 141 cap
1182ret long AS_ERR_SERIALIZATION
1183end
1184off = mul sb 32
1185for i ulong 32
1186put buf add pos i get as_snap_br_id add off i
1187end
1188pos = add pos 32
1189put buf pos get as_snap_br_has_parent sb
1190pos = add pos 1
1191for i ulong 32
1192put buf add pos i get as_snap_br_parent_id add off i
1193end
1194pos = add pos 32
1195for i ulong 32
1196put buf add pos i get as_snap_br_agent_id add off i
1197end
1198pos = add pos 32
1199as_put_u64_be buf pos get as_snap_br_created_at sb
1200pos = add pos 8
1201as_put_u32_be buf pos uint get as_snap_br_tok_cnt sb
1202pos = add pos 4
1203for i ulong 32
1204put buf add pos i get as_snap_br_rhash add off i
1205end
1206ret long add pos 32
1207end
1208
1209fn as_snap_encode_br_toks buf edit uchar * pos ulong sb ulong to long
1210cap = len buf
1211tcnt = get as_snap_br_tok_cnt sb
1212if gt add pos mul tcnt 44 cap
1213ret long AS_ERR_SERIALIZATION
1214end
1215tb = mul sb AS_MAX_TOKENS
1216for k ulong tcnt bound 64
1217as_put_u64_be buf pos get as_snap_br_tok_seq add tb k
1218pos = add pos 8
1219as_put_u32_be buf pos get as_snap_br_tok_id add tb k
1220pos = add pos 4
1221hb = mul add tb k 32
1222for j ulong 32
1223put buf add pos j get as_snap_br_tok_hash add hb j
1224end
1225pos = add pos 32
1226end
1227ret long pos
1228end
1229
1230fn as_snap_encode_one_anc buf edit uchar * pos ulong sb ulong k ulong to void
1231aoff = add mul sb mul AS_MAX_DEPTH 32 mul k 32
1232for j ulong 32
1233put buf add pos j get as_snap_br_lin_anc add aoff j
1234end
1235end
1236
1237fn as_snap_encode_br_lin buf edit uchar * pos ulong sb ulong to long
1238lcnt = get as_snap_br_lin_cnt sb
1239if gt add pos add 44 mul lcnt 32 len buf
1240ret long AS_ERR_SERIALIZATION
1241end
1242off = mul sb 32
1243for i ulong 32
1244put buf add pos i get as_snap_br_lin_root add off i
1245end
1246pos = add pos 32
1247as_put_u64_be buf pos get as_snap_br_lin_depth sb
1248pos = add pos 8
1249as_put_u32_be buf pos uint lcnt
1250pos = add pos 4
1251for k ulong lcnt bound 16
1252as_snap_encode_one_anc buf pos sb k
1253pos = add pos 32
1254end
1255ret long pos
1256end
1257
1258fn as_snap_encode_one_epi buf edit uchar * pos ulong sb ulong k ulong to void
1259eoff = add mul sb mul AS_MAX_EPISTEMIC 16 mul k 16
1260for j ulong 16
1261put buf add pos j get as_snap_br_epi_ids add eoff j
1262end
1263end
1264
1265fn as_snap_encode_br_epi buf edit uchar * pos ulong sb ulong to long
1266ecnt = get as_snap_br_epi_cnt sb
1267if gt add pos add 4 mul ecnt 16 len buf
1268ret long AS_ERR_SERIALIZATION
1269end
1270as_put_u32_be buf pos uint ecnt
1271pos = add pos 4
1272for k ulong ecnt bound 8
1273as_snap_encode_one_epi buf pos sb k
1274pos = add pos 16
1275end
1276ret long pos
1277end
1278
1279fn as_snap_encode_br_body buf edit uchar * pos ulong sb ulong to long
1280p1 = as_snap_encode_br_toks buf pos sb
1281if lt p1 0
1282ret p1
1283end
1284p2 = as_snap_encode_br_lin buf ulong p1 sb
1285if lt p2 0
1286ret p2
1287end
1288ret as_snap_encode_br_epi buf ulong p2 sb
1289end
1290
1291fn as_seal_snap_agents buf edit uchar * pos ulong to long
1292acnt = get as_snap_agent_cnt 0
1293if gt add pos add 4 mul acnt 32 len buf
1294ret long AS_ERR_SERIALIZATION
1295end
1296as_put_u32_be buf pos uint acnt
1297pos = add pos 4
1298for a ulong acnt bound 8
1299aoff = mul a 32
1300for i ulong 32
1301put buf add pos i get as_snap_agents add aoff i
1302end
1303pos = add pos 32
1304end
1305ret long pos
1306end
1307
1308fn as_seal_snapshot buf edit uchar * to long
1309if lt len buf 60
1310ret long AS_ERR_SERIALIZATION
1311end
1312for i ulong 8
1313put buf i get as_ckpt_magic i
1314end
1315as_put_u64_be buf 44 get as_snap_inc 0
1316pa = as_seal_snap_agents buf 52
1317if lt pa 0
1318ret pa
1319end
1320pos = ulong pa
1321if gt add pos 4 len buf
1322ret long AS_ERR_SERIALIZATION
1323end
1324bcnt = get as_snap_branch_cnt 0
1325as_put_u32_be buf pos uint bcnt
1326pos = add pos 4
1327for sb ulong bcnt bound 32
1328p1 = as_snap_encode_br_hdr buf pos sb
1329if lt p1 0
1330ret p1
1331end
1332p2 = as_snap_encode_br_body buf ulong p1 sb
1333if lt p2 0
1334ret p2
1335end
1336pos = ulong p2
1337end
1338plen = sub pos 44
1339as_put_u32_be buf 40 uint plen
1340dig = new uchar 32 0
1341if ne sha2_digest 256 part buf 44 plen dig 32
1342ret long AS_ERR_SERIALIZATION
1343end
1344for i ulong 32
1345put buf add 8 i get dig i
1346end
1347ret long pos
1348end
1349
1350fn as_snap_decode_br_hdr buf view uchar * pos ulong total ulong sb ulong to long
1351if gt add pos 141 total
1352ret long AS_ERR_DESERIALIZATION
1353end
1354off = mul sb 32
1355for i ulong 32
1356put as_snap_br_id add off i get buf add pos i
1357end
1358pos = add pos 32
1359hp = get buf pos
1360if gt hp 1
1361ret long AS_ERR_DESERIALIZATION
1362end
1363put as_snap_br_has_parent sb hp
1364pos = add pos 1
1365for i ulong 32
1366put as_snap_br_parent_id add off i get buf add pos i
1367end
1368pos = add pos 32
1369for i ulong 32
1370put as_snap_br_agent_id add off i get buf add pos i
1371end
1372pos = add pos 32
1373put as_snap_br_created_at sb as_get_u64_be buf pos
1374pos = add pos 8
1375tcnt = ulong as_get_u32_be buf pos
1376if gt tcnt AS_MAX_TOKENS
1377ret long AS_ERR_DESERIALIZATION
1378end
1379put as_snap_br_tok_cnt sb tcnt
1380pos = add pos 4
1381for i ulong 32
1382put as_snap_br_rhash add off i get buf add pos i
1383end
1384ret long add pos 32
1385end
1386
1387fn as_snap_decode_br_toks buf view uchar * pos ulong total ulong sb ulong to long
1388tcnt = get as_snap_br_tok_cnt sb
1389if gt add pos mul tcnt 44 total
1390ret long AS_ERR_DESERIALIZATION
1391end
1392tb = mul sb AS_MAX_TOKENS
1393for k ulong tcnt bound 64
1394put as_snap_br_tok_seq add tb k as_get_u64_be buf pos
1395pos = add pos 8
1396put as_snap_br_tok_id add tb k as_get_u32_be buf pos
1397pos = add pos 4
1398hb = mul add tb k 32
1399for j ulong 32
1400put as_snap_br_tok_hash add hb j get buf add pos j
1401end
1402pos = add pos 32
1403end
1404ret long pos
1405end
1406
1407fn as_snap_decode_one_anc buf view uchar * pos ulong sb ulong k ulong to void
1408aoff = add mul sb mul AS_MAX_DEPTH 32 mul k 32
1409for j ulong 32
1410put as_snap_br_lin_anc add aoff j get buf add pos j
1411end
1412end
1413
1414fn as_snap_decode_br_lin buf view uchar * pos ulong total ulong sb ulong to long
1415if gt add pos 44 total
1416ret long AS_ERR_DESERIALIZATION
1417end
1418off = mul sb 32
1419for i ulong 32
1420put as_snap_br_lin_root add off i get buf add pos i
1421end
1422pos = add pos 32
1423put as_snap_br_lin_depth sb as_get_u64_be buf pos
1424pos = add pos 8
1425lcnt = ulong as_get_u32_be buf pos
1426pos = add pos 4
1427if gt lcnt AS_MAX_DEPTH
1428ret long AS_ERR_DESERIALIZATION
1429end
1430if gt add pos mul lcnt 32 total
1431ret long AS_ERR_DESERIALIZATION
1432end
1433put as_snap_br_lin_cnt sb lcnt
1434for k ulong lcnt bound 16
1435as_snap_decode_one_anc buf pos sb k
1436pos = add pos 32
1437end
1438ret long pos
1439end
1440
1441fn as_snap_decode_one_epi buf view uchar * pos ulong sb ulong k ulong to void
1442eoff = add mul sb mul AS_MAX_EPISTEMIC 16 mul k 16
1443for j ulong 16
1444put as_snap_br_epi_ids add eoff j get buf add pos j
1445end
1446end
1447
1448fn as_snap_decode_br_epi buf view uchar * pos ulong total ulong sb ulong to long
1449if gt add pos 4 total
1450ret long AS_ERR_DESERIALIZATION
1451end
1452ecnt = ulong as_get_u32_be buf pos
1453pos = add pos 4
1454if gt ecnt AS_MAX_EPISTEMIC
1455ret long AS_ERR_DESERIALIZATION
1456end
1457if gt add pos mul ecnt 16 total
1458ret long AS_ERR_DESERIALIZATION
1459end
1460put as_snap_br_epi_cnt sb ecnt
1461for k ulong ecnt bound 8
1462as_snap_decode_one_epi buf pos sb k
1463pos = add pos 16
1464end
1465ret long pos
1466end
1467
1468fn as_snap_decode_br_lin_epi buf view uchar * pos ulong total ulong sb ulong to long
1469p1 = as_snap_decode_br_lin buf pos total sb
1470if lt p1 0
1471ret p1
1472end
1473ret as_snap_decode_br_epi buf ulong p1 total sb
1474end
1475
1476fn as_decode_ckpt_envelope buf view uchar * total ulong to int
1477if lt total 44
1478ret AS_ERR_DESERIALIZATION
1479end
1480if gt total len buf
1481ret AS_ERR_DESERIALIZATION
1482end
1483for i ulong 8
1484if ne get buf i get as_ckpt_magic i
1485ret AS_ERR_DESERIALIZATION
1486end
1487end
1488plen = ulong as_get_u32_be buf 40
1489if ne add 44 plen total
1490ret AS_ERR_DESERIALIZATION
1491end
1492dig = new uchar 32 0
1493if ne sha2_digest 256 part buf 44 plen dig 32
1494ret AS_ERR_DESERIALIZATION
1495end
1496for i ulong 32
1497if ne get buf add 8 i get dig i
1498ret AS_ERR_CHECKSUM_MISMATCH
1499end
1500end
1501ret AS_OK
1502end
1503
1504fn as_decode_snap_agents buf view uchar * pos ulong total ulong to long
1505if gt add pos 12 total
1506ret long AS_ERR_DESERIALIZATION
1507end
1508put as_snap_inc 0 as_get_u64_be buf pos
1509pos = add pos 8
1510acnt = ulong as_get_u32_be buf pos
1511pos = add pos 4
1512if gt acnt AS_MAX_AGENTS
1513ret long AS_ERR_DESERIALIZATION
1514end
1515if gt add pos add mul acnt 32 4 total
1516ret long AS_ERR_DESERIALIZATION
1517end
1518put as_snap_agent_cnt 0 acnt
1519for a ulong acnt bound 8
1520aoff = mul a 32
1521for i ulong 32
1522put as_snap_agents add aoff i get buf add pos i
1523end
1524pos = add pos 32
1525end
1526ret long pos
1527end
1528
1529fn as_decode_checkpoint_to_snap buf view uchar * total ulong to int
1530rc = as_decode_ckpt_envelope buf total
1531if ne rc AS_OK
1532ret rc
1533end
1534pa = as_decode_snap_agents buf 44 total
1535if lt pa 0
1536ret int pa
1537end
1538pos = ulong pa
1539bcnt = ulong as_get_u32_be buf pos
1540pos = add pos 4
1541if gt bcnt AS_MAX_BRANCHES
1542ret AS_ERR_DESERIALIZATION
1543end
1544put as_snap_branch_cnt 0 bcnt
1545for sb ulong bcnt bound 32
1546p1 = as_snap_decode_br_hdr buf pos total sb
1547if lt p1 0
1548ret int p1
1549end
1550p2 = as_snap_decode_br_toks buf ulong p1 total sb
1551if lt p2 0
1552ret int p2
1553end
1554p3 = as_snap_decode_br_lin_epi buf ulong p2 total sb
1555if lt p3 0
1556ret int p3
1557end
1558pos = ulong p3
1559end
1560if ne pos total
1561ret AS_ERR_DESERIALIZATION
1562end
1563ret AS_OK
1564end
1565
1566note === Checkpoint export & validation ===
1567
1568fn as_br_matches_agent slot ulong agent_id view uchar * to bool
1569boff = mul slot 32
1570for i ulong 32
1571if ne get as_br_agent_id add boff i get agent_id i
1572ret false
1573end
1574end
1575ret true
1576end
1577
1578fn as_sort_val_order_br mcnt ulong to void
1579for i ulong mcnt bound 32
1580for j ulong mcnt bound 32
1581if lt add j 1 mcnt
1582s1 = get as_val_order j
1583s2 = get as_val_order add j 1
1584if gt as_br_id_cmp_slots s1 s2 0
1585put as_val_order j s2
1586put as_val_order add j 1 s1
1587end
1588end
1589end
1590end
1591end
1592
1593fn as_mgr_export_checkpoint mgr ulong agent_id view uchar * dst_buf edit uchar * to long
1594if not as_mgr_has_agent mgr agent_id
1595ret long AS_ERR_AGENT_NOT_FOUND
1596end
1597as_snap_clear
1598put as_snap_inc 0 get as_mgr_inc mgr
1599put as_snap_agent_cnt 0 1
1600for i ulong 32
1601put as_snap_agents i get agent_id i
1602end
1603bcnt = get as_mgr_branch_cnt mgr
1604mcnt = ulong 0
1605for b ulong bcnt bound 32
1606slot = as_br_slot mgr b
1607if as_br_matches_agent slot agent_id
1608put as_val_order mcnt slot
1609mcnt = add mcnt 1
1610end
1611end
1612as_sort_val_order_br mcnt
1613put as_snap_branch_cnt 0 mcnt
1614for k ulong mcnt bound 32
1615as_br_to_snap k get as_val_order k
1616end
1617ret as_seal_snapshot dst_buf
1618end
1619
1620fn as_mgr_checkpoint mgr ulong agent_id view uchar * dst_hash edit uchar * to int
1621tmp = new uchar 65536 0
1622n = as_mgr_export_checkpoint mgr agent_id tmp
1623if lt n 0
1624ret int n
1625end
1626for i ulong 32
1627put dst_hash i get tmp add 8 i
1628end
1629ret AS_OK
1630end
1631
1632note === Restore validation helpers (exact Rust order) ===
1633
1634fn as_snap_has_agent agent_id view uchar * limit ulong to bool
1635for a ulong limit bound 8
1636aoff = mul a 32
1637same = true
1638for i ulong 32
1639if ne get as_snap_agents add aoff i get agent_id i
1640same = false
1641end
1642end
1643if same
1644ret true
1645end
1646end
1647ret false
1648end
1649
1650fn as_val_find_sorted target view uchar * bcnt ulong to long
1651for i ulong bcnt bound 32
1652sb = get as_val_order i
1653off = mul sb 32
1654same = true
1655for j ulong 32
1656if ne get as_snap_br_id add off j get target j
1657same = false
1658end
1659end
1660if same
1661ret long i
1662end
1663end
1664ret -1
1665end
1666
1667fn as_val_agents_and_branches to int
1668acnt = get as_snap_agent_cnt 0
1669if eq acnt 0
1670ret AS_ERR_CKPT_NO_AGENT
1671end
1672aid = new uchar 32 0
1673for a ulong acnt bound 8
1674aoff = mul a 32
1675for i ulong 32
1676put aid i get as_snap_agents add aoff i
1677end
1678if as_snap_has_agent aid a
1679ret AS_ERR_CKPT_DUP_AGENT
1680end
1681end
1682bcnt = get as_snap_branch_cnt 0
1683for sb ulong bcnt bound 32
1684boff = mul sb 32
1685for i ulong 32
1686put aid i get as_snap_br_agent_id add boff i
1687end
1688if not as_snap_has_agent aid acnt
1689ret AS_ERR_CKPT_ABSENT_AGENT
1690end
1691for prev ulong sb bound 32
1692if eq as_snap_br_id_cmp prev sb 0
1693ret AS_ERR_CKPT_DUP_BRANCH
1694end
1695end
1696put as_val_order sb sb
1697end
1698note Sort as_val_order ascending by branch_id (matching Rust BTreeMap)
1699for i ulong bcnt bound 32
1700for j ulong bcnt bound 32
1701if lt add j 1 bcnt
1702s1 = get as_val_order j
1703s2 = get as_val_order add j 1
1704if gt as_snap_br_id_cmp s1 s2 0
1705put as_val_order j s2
1706put as_val_order add j 1 s1
1707end
1708end
1709end
1710end
1711ret AS_OK
1712end
1713
1714fn as_val_one_root a ulong bcnt ulong to int
1715aid = new uchar 32 0
1716rid = new uchar 32 0
1717aoff = mul a 32
1718for i ulong 32
1719put aid i get as_snap_agents add aoff i
1720end
1721as_branch_id_root aid rid
1722rpos = as_val_find_sorted rid bcnt
1723if lt rpos 0
1724ret AS_ERR_CKPT_MISSING_ROOT
1725end
1726rsb = get as_val_order ulong rpos
1727roff = mul rsb 32
1728for i ulong 32
1729if ne get as_snap_br_agent_id add roff i get aid i
1730ret AS_ERR_CKPT_INCONSISTENT_ROOT
1731end
1732if ne get as_snap_br_lin_root add roff i get aid i
1733ret AS_ERR_CKPT_INCONSISTENT_ROOT
1734end
1735end
1736if ne get as_snap_br_has_parent rsb 0
1737ret AS_ERR_CKPT_INCONSISTENT_ROOT
1738end
1739if ne get as_snap_br_lin_depth rsb 0
1740ret AS_ERR_CKPT_INCONSISTENT_ROOT
1741end
1742if ne get as_snap_br_lin_cnt rsb 0
1743ret AS_ERR_CKPT_INCONSISTENT_ROOT
1744end
1745ret AS_OK
1746end
1747
1748fn as_val_roots to int
1749acnt = get as_snap_agent_cnt 0
1750bcnt = get as_snap_branch_cnt 0
1751for a ulong acnt bound 8
1752rc = as_val_one_root a bcnt
1753if ne rc AS_OK
1754ret rc
1755end
1756end
1757ret AS_OK
1758end
1759
1760fn as_val_child_anc_eq sb ulong psb ulong k ulong to bool
1761boff = add mul sb mul AS_MAX_DEPTH 32 mul k 32
1762poff = add mul psb mul AS_MAX_DEPTH 32 mul k 32
1763for j ulong 32
1764if ne get as_snap_br_lin_anc add boff j get as_snap_br_lin_anc add poff j
1765ret false
1766end
1767end
1768ret true
1769end
1770
1771fn as_val_child_lineage sb ulong psb ulong to int
1772boff = mul sb 32
1773poff = mul psb 32
1774for i ulong 32
1775if ne get as_snap_br_agent_id add boff i get as_snap_br_agent_id add poff i
1776ret AS_ERR_CKPT_INCONSISTENT_LINEAGE
1777end
1778if ne get as_snap_br_lin_root add boff i get as_snap_br_lin_root add poff i
1779ret AS_ERR_CKPT_INCONSISTENT_LINEAGE
1780end
1781end
1782pdepth = get as_snap_br_lin_depth psb
1783if eq pdepth 0xffffffffffffffff
1784ret AS_ERR_CKPT_INCONSISTENT_LINEAGE
1785end
1786if ne get as_snap_br_lin_depth sb add pdepth 1
1787ret AS_ERR_CKPT_INCONSISTENT_LINEAGE
1788end
1789pcnt = get as_snap_br_lin_cnt psb
1790if ne get as_snap_br_lin_cnt sb add pcnt 1
1791ret AS_ERR_CKPT_INCONSISTENT_LINEAGE
1792end
1793for k ulong pcnt bound 16
1794if not as_val_child_anc_eq sb psb k
1795ret AS_ERR_CKPT_INCONSISTENT_LINEAGE
1796end
1797end
1798last_off = add mul sb mul AS_MAX_DEPTH 32 mul pcnt 32
1799for j ulong 32
1800if ne get as_snap_br_lin_anc add last_off j get as_snap_br_id add boff j
1801ret AS_ERR_CKPT_INCONSISTENT_LINEAGE
1802end
1803end
1804ret AS_OK
1805end
1806
1807fn as_val_one_branch i ulong bcnt ulong to int
1808sb = get as_val_order i
1809off = mul sb 32
1810tmp = new uchar 32 0
1811exp = new uchar 32 0
1812if eq get as_snap_br_has_parent sb 0
1813for j ulong 32
1814put tmp j get as_snap_br_agent_id add off j
1815end
1816as_branch_id_root tmp exp
1817for j ulong 32
1818if ne get as_snap_br_id add off j get exp j
1819ret AS_ERR_CKPT_UNEXPECTED_ROOT
1820end
1821end
1822ret AS_OK
1823end
1824for j ulong 32
1825put tmp j get as_snap_br_parent_id add off j
1826end
1827ppos = as_val_find_sorted tmp bcnt
1828if lt ppos 0
1829ret AS_ERR_CKPT_MISSING_PARENT
1830end
1831psb = get as_val_order ulong ppos
1832rc = as_val_child_lineage sb psb
1833if ne rc AS_OK
1834ret rc
1835end
1836put as_val_child_cnt ulong ppos add get as_val_child_cnt ulong ppos 1
1837ret AS_OK
1838end
1839
1840fn as_val_parents_and_lineage to int
1841bcnt = get as_snap_branch_cnt 0
1842for i ulong bcnt bound 32
1843put as_val_child_cnt i 0
1844end
1845for i ulong bcnt bound 32
1846rc = as_val_one_branch i bcnt
1847if ne rc AS_OK
1848ret rc
1849end
1850end
1851ret AS_OK
1852end
1853
1854fn as_val_one_fork pid view uchar * cidx ulong bcnt ulong to int
1855cid = new uchar 32 0
1856as_branch_id_derive pid cidx cid
1857cpos = as_val_find_sorted cid bcnt
1858if lt cpos 0
1859ret AS_ERR_CKPT_NONCONTIGUOUS_FORK
1860end
1861csb = get as_val_order ulong cpos
1862if eq get as_snap_br_has_parent csb 0
1863ret AS_ERR_CKPT_NONCONTIGUOUS_FORK
1864end
1865coff = mul csb 32
1866for j ulong 32
1867if ne get as_snap_br_parent_id add coff j get pid j
1868ret AS_ERR_CKPT_NONCONTIGUOUS_FORK
1869end
1870end
1871ret AS_OK
1872end
1873
1874fn as_val_parent_forks i ulong bcnt ulong to int
1875ccnt = get as_val_child_cnt i
1876if eq ccnt 0
1877ret AS_OK
1878end
1879pid = new uchar 32 0
1880psb = get as_val_order i
1881poff = mul psb 32
1882for j ulong 32
1883put pid j get as_snap_br_id add poff j
1884end
1885for cidx ulong ccnt bound 32
1886rc = as_val_one_fork pid cidx bcnt
1887if ne rc AS_OK
1888ret rc
1889end
1890end
1891ret AS_OK
1892end
1893
1894fn as_val_contiguous_forks to int
1895bcnt = get as_snap_branch_cnt 0
1896for i ulong bcnt bound 32
1897rc = as_val_parent_forks i bcnt
1898if ne rc AS_OK
1899ret rc
1900end
1901end
1902ret AS_OK
1903end
1904
1905fn as_mgr_commit_snap_agents mgr ulong to void
1906acnt = get as_snap_agent_cnt 0
1907put as_mgr_agent_cnt mgr acnt
1908mabase = mul mgr mul AS_MAX_AGENTS 32
1909for a ulong acnt bound 8
1910aoff = mul a 32
1911doff = add mabase aoff
1912for j ulong 32
1913put as_mgr_agents add doff j get as_snap_agents add aoff j
1914end
1915end
1916end
1917
1918fn as_mgr_restore_from_checkpoint mgr ulong ckpt view uchar * ckpt_len ulong new_inc ulong to int
1919rc = as_decode_checkpoint_to_snap ckpt ckpt_len
1920if ne rc AS_OK
1921ret rc
1922end
1923rc = as_val_agents_and_branches
1924if ne rc AS_OK
1925ret rc
1926end
1927rc = as_val_roots
1928if ne rc AS_OK
1929ret rc
1930end
1931rc = as_val_parents_and_lineage
1932if ne rc AS_OK
1933ret rc
1934end
1935rc = as_val_contiguous_forks
1936if ne rc AS_OK
1937ret rc
1938end
1939as_mgr_init mgr new_inc
1940as_mgr_commit_snap_agents mgr
1941bcnt = get as_snap_branch_cnt 0
1942put as_mgr_branch_cnt mgr bcnt
1943for i ulong bcnt bound 32
1944sb = get as_val_order i
1945slot = as_br_slot mgr i
1946as_snap_to_br slot sb
1947put as_br_child_cnt slot get as_val_child_cnt i
1948seq = get as_mgr_next_seq mgr
1949put as_mgr_next_seq mgr add seq 1
1950as_br_inc_init slot new_inc seq
1951as_br_inc_evict slot
1952end
1953ret AS_OK
1954end
1955
1956note === Deep equality helper comparing a branch slot with a snapshot branch ===
1957
1958fn as_br_eq_snap_tok slot ulong sb ulong k ulong to bool
1959bt = add mul slot AS_MAX_TOKENS k
1960st = add mul sb AS_MAX_TOKENS k
1961if ne get as_br_tok_seq bt get as_snap_br_tok_seq st
1962ret false
1963end
1964if ne get as_br_tok_id bt get as_snap_br_tok_id st
1965ret false
1966end
1967bh = mul bt 32
1968sh = mul st 32
1969for j ulong 32
1970if ne get as_br_tok_hash add bh j get as_snap_br_tok_hash add sh j
1971ret false
1972end
1973end
1974ret true
1975end
1976
1977fn as_br_eq_snap_hist slot ulong sb ulong to bool
1978tcnt = get as_br_tok_cnt slot
1979if ne tcnt get as_snap_br_tok_cnt sb
1980ret false
1981end
1982bo = mul slot 32
1983so = mul sb 32
1984for i ulong 32
1985if ne get as_br_rhash add bo i get as_snap_br_rhash add so i
1986ret false
1987end
1988end
1989for k ulong tcnt bound 64
1990if not as_br_eq_snap_tok slot sb k
1991ret false
1992end
1993end
1994ret true
1995end
1996
1997fn as_br_eq_snap_lin_one slot ulong sb ulong k ulong to bool
1998boff = add mul slot mul AS_MAX_DEPTH 32 mul k 32
1999soff = add mul sb mul AS_MAX_DEPTH 32 mul k 32
2000for j ulong 32
2001if ne get as_br_lin_anc add boff j get as_snap_br_lin_anc add soff j
2002ret false
2003end
2004end
2005ret true
2006end
2007
2008fn as_br_eq_snap_lin slot ulong sb ulong to bool
2009bo = mul slot 32
2010so = mul sb 32
2011for i ulong 32
2012if ne get as_br_lin_root add bo i get as_snap_br_lin_root add so i
2013ret false
2014end
2015end
2016if ne get as_br_lin_depth slot get as_snap_br_lin_depth sb
2017ret false
2018end
2019lcnt = get as_br_lin_cnt slot
2020if ne lcnt get as_snap_br_lin_cnt sb
2021ret false
2022end
2023for k ulong lcnt bound 16
2024if not as_br_eq_snap_lin_one slot sb k
2025ret false
2026end
2027end
2028ret true
2029end
2030
2031fn as_br_eq_snap_epi_one slot ulong sb ulong k ulong to bool
2032boff = add mul slot mul AS_MAX_EPISTEMIC 16 mul k 16
2033soff = add mul sb mul AS_MAX_EPISTEMIC 16 mul k 16
2034for j ulong 16
2035if ne get as_br_epi_ids add boff j get as_snap_br_epi_ids add soff j
2036ret false
2037end
2038end
2039ret true
2040end
2041
2042fn as_br_eq_snap_epi slot ulong sb ulong to bool
2043ecnt = get as_br_epi_cnt slot
2044if ne ecnt get as_snap_br_epi_cnt sb
2045ret false
2046end
2047for k ulong ecnt bound 8
2048if not as_br_eq_snap_epi_one slot sb k
2049ret false
2050end
2051end
2052ret true
2053end
2054
2055fn as_br_eq_snap_lin_epi slot ulong sb ulong to bool
2056if not as_br_eq_snap_lin slot sb
2057ret false
2058end
2059ret as_br_eq_snap_epi slot sb
2060end
2061
2062fn as_br_eq_snap slot ulong sb ulong to bool
2063if ne get as_br_has_parent slot get as_snap_br_has_parent sb
2064ret false
2065end
2066if ne get as_br_created_at slot get as_snap_br_created_at sb
2067ret false
2068end
2069bo = mul slot 32
2070so = mul sb 32
2071for i ulong 32
2072if ne get as_br_id add bo i get as_snap_br_id add so i
2073ret false
2074end
2075if ne get as_br_parent_id add bo i get as_snap_br_parent_id add so i
2076ret false
2077end
2078if ne get as_br_agent_id add bo i get as_snap_br_agent_id add so i
2079ret false
2080end
2081end
2082if not as_br_eq_snap_hist slot sb
2083ret false
2084end
2085ret as_br_eq_snap_lin_epi slot sb
2086end