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 / protocol / synapse / core / changeset.om

966 lines · 19874 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note protocol/synapse/core/changeset.om: content-addressed changeset model, canonical serialization,
3note SHA-256 identity computation (via om/tls/sha2.om), repository-relative path validation,
4note exact & directory-prefix path overlap detector, and shared arena/encoding utilities.
5note Composed OM-2 library unit (no om2 directive, no main).
6
7note ======== status & refusal codes ========
8const SYN_OK ulong 0
9const SYN_ERR_MALFORMED ulong 1
10const SYN_ERR_LEASE_CONFLICT ulong 2
11const SYN_ERR_NOT_FOUND ulong 3
12const SYN_ERR_CYCLE ulong 4
13const SYN_ERR_NOT_HOLDER ulong 5
14const SYN_ERR_LEASE_CLOSED ulong 6
15const SYN_ERR_SELF_REVIEW ulong 7
16const SYN_ERR_STALE_HEAD ulong 8
17const SYN_ERR_REVIEW_BLOCKED ulong 9
18const SYN_ERR_NON_FF ulong 10
19const SYN_ERR_STATE_INVALID ulong 11
20const SYN_ERR_LEDGER_TAMPER ulong 12
21const SYN_ERR_CAPACITY ulong 13
22const SYN_ERR_DUPLICATE ulong 14
23const SYN_ERR_VERDICT_INVALID ulong 15
24
25const SYN_NONE ulong 18446744073709551615
26const SYN_STR_CAP ulong 262144
27
28note ======== changeset states ========
29const CS_STATE_DRAFT ulong 0
30const CS_STATE_PROPOSED ulong 1
31const CS_STATE_REVIEWED ulong 2
32const CS_STATE_LANDED ulong 3
33const CS_STATE_ABANDONED ulong 4
34
35note ======== changeset capacities & meta layout ========
36const CS_MAX ulong 256
37const CS_MAX_PATHS ulong 16
38const CS_PATH_POOL ulong 4096
39const CS_FIELDS ulong 12
40
41const CSF_AUTH_OFF ulong 0
42const CSF_AUTH_LEN ulong 1
43const CSF_BR_OFF ulong 2
44const CSF_BR_LEN ulong 3
45const CSF_STATE ulong 4
46const CSF_PSTART ulong 5
47const CSF_PCNT ulong 6
48const CSF_TS ulong 7
49const CSF_PIDX ulong 8
50const CSF_SUM_OFF ulong 9
51const CSF_SUM_LEN ulong 10
52const CSF_USED ulong 11
53
54note ======== globals ========
55global g_syn_str uchar 262144
56global g_syn_strn ulong 4
57
58global g_cs_id uchar 8192
59global g_cs_parent uchar 8192
60global g_cs_meta ulong 3072
61global g_cs_paths ulong 8192
62global g_cs_hdr ulong 16
63
64note staging area for building one changeset cleanly without exceeding 6 parameter slots
65global g_cs_stg_par uchar 32
66global g_cs_stg_meta ulong 16
67global g_cs_stg_paths ulong 32
68
69text t_cs_dom "SYNAPSE_CHANGESET_V1"
70text t_hex "0123456789abcdef"
71text t_st_draft "DRAFT"
72text t_st_prop "PROPOSED"
73text t_st_rev "REVIEWED"
74text t_st_land "LANDED"
75text t_st_abnd "ABANDONED"
76text t_br_main "main"
77
78note ======== shared byte, hex, arena & I/O helpers ========
79
80fn syn_arena_reset to void
81put g_syn_strn 0 0
82end
83
84fn syn_arena_put s view uchar * to ulong
85n = len s
86cur = get g_syn_strn 0
87if gt add cur n SYN_STR_CAP
88ret SYN_NONE
89end
90for i ulong n
91put g_syn_str add cur i get s i
92end
93put g_syn_strn 0 add cur n
94ret cur
95end
96
97fn syn_arena_eq ao ulong al ulong bo ulong bl ulong to bool
98if ne al bl
99ret false
100end
101for i ulong al
102if ne get g_syn_str add ao i get g_syn_str add bo i
103ret false
104end
105end
106ret true
107end
108
109fn syn_arena_eq_view ao ulong al ulong v view uchar * to bool
110if ne al len v
111ret false
112end
113for i ulong al
114if ne get g_syn_str add ao i get v i
115ret false
116end
117end
118ret true
119end
120
121note lexicographic compare of two arena strings: 0 eq, 1 a<b, 2 a>b
122fn syn_arena_cmp ao ulong al ulong bo ulong bl ulong to ulong
123m = al
124if lt bl m
125m = bl
126end
127for i ulong m
128ca = get g_syn_str add ao i
129cb = get g_syn_str add bo i
130if lt ca cb
131ret 1
132end
133if gt ca cb
134ret 2
135end
136end
137if lt al bl
138ret 1
139end
140if gt al bl
141ret 2
142end
143ret 0
144end
145
146fn syn_arena_copy ao ulong al ulong dst edit uchar * to ulong
147lim = al
148if gt lim len dst
149lim = len dst
150end
151for i ulong lim
152put dst i get g_syn_str add ao i
153end
154ret lim
155end
156
157fn syn_digest_eq a view uchar * b view uchar * to bool
158if ne len a 32
159ret false
160end
161if ne len b 32
162ret false
163end
164for i ulong 32
165if ne get a i get b i
166ret false
167end
168end
169ret true
170end
171
172fn syn_digest_is_zero d view uchar * to bool
173if ne len d 32
174ret false
175end
176for i ulong 32
177if ne get d i 0
178ret false
179end
180end
181ret true
182end
183
184fn syn_put_u16_le dst edit uchar * off ulong v ulong to void
185put dst off uchar and v 0xff
186put dst add off 1 uchar and shr v 8 0xff
187end
188
189fn syn_put_u32_le dst edit uchar * off ulong v ulong to void
190put dst off uchar and v 0xff
191put dst add off 1 uchar and shr v 8 0xff
192put dst add off 2 uchar and shr v 16 0xff
193put dst add off 3 uchar and shr v 24 0xff
194end
195
196fn syn_put_u64_le dst edit uchar * off ulong v ulong to void
197for i ulong 8
198put dst add off i uchar and shr v mul i 8 0xff
199end
200end
201
202fn syn_sha_u16 ctx edit ulong * v ulong to void
203b = new uchar 2 0
204syn_put_u16_le b 0 v
205sha256_update ctx b
206end
207
208fn syn_sha_u32 ctx edit ulong * v ulong to void
209b = new uchar 4 0
210syn_put_u32_le b 0 v
211sha256_update ctx b
212end
213
214fn syn_sha_u64 ctx edit ulong * v ulong to void
215b = new uchar 8 0
216syn_put_u64_le b 0 v
217sha256_update ctx b
218end
219
220fn syn_sha_arena ctx edit ulong * off ulong slen ulong to void
221blk = new uchar 256 0
222pos = ulong 0
223while lt pos slen bound 4096
224k = sub slen pos
225if gt k 256
226k = 256
227end
228for i ulong k
229put blk i get g_syn_str add add off pos i
230end
231sha256_update ctx part blk 0 k
232pos = add pos k
233end
234end
235
236fn syn_sha_arena_str ctx edit ulong * off ulong slen ulong to void
237syn_sha_u32 ctx slen
238if gt slen 0
239syn_sha_arena ctx off slen
240end
241end
242
243fn syn_hex_val c uchar to ulong
244if and ge c 48 le c 57
245ret ulong sub c 48
246end
247if and ge c 97 le c 102
248ret add ulong sub c 97 10
249end
250ret 99
251end
252
253fn syn_hex_encode src view uchar * dst edit uchar * to void
254n = len src
255for i ulong n
256b = get src i
257hi = ulong shr b 4
258lo = ulong and b 15
259put dst mul i 2 get t_hex hi
260put dst add mul i 2 1 get t_hex lo
261end
262end
263
264fn syn_hex_decode src view uchar * dst edit uchar * to bool
265if ne len src 64
266ret false
267end
268if lt len dst 32
269ret false
270end
271for i ulong 32
272hi = syn_hex_val get src mul i 2
273lo = syn_hex_val get src add mul i 2 1
274if or gt hi 15 gt lo 15
275ret false
276end
277put dst i uchar or shl hi 4 lo
278end
279ret true
280end
281
282fn syn_write_all fd int buf view uchar * n ulong to bool
283pos = ulong 0
284while lt pos n bound 65536
285k = write fd buf pos sub n pos
286if le k 0
287ret false
288end
289pos = add pos ulong k
290end
291ret true
292end
293
294fn syn_write_text fd int t view uchar * to void
295ok = syn_write_all fd t len t
296if ok
297ret
298end
299end
300
301fn syn_write_arena fd int off ulong slen ulong to void
302blk = new uchar 256 0
303pos = ulong 0
304while lt pos slen bound 4096
305k = sub slen pos
306if gt k 256
307k = 256
308end
309for i ulong k
310put blk i get g_syn_str add add off pos i
311end
312ok = syn_write_all fd blk k
313if eq ok false
314ret
315end
316pos = add pos k
317end
318end
319
320fn syn_write_u64 fd int v ulong to void
321buf = new uchar 24 0
322if eq v 0
323put buf 0 48
324ok = syn_write_all fd buf 1
325if ok
326ret
327end
328ret
329end
330tmp = new uchar 24 0
331cnt = ulong 0
332x = v
333while gt x 0 bound 32
334d = rem x 10
335put tmp cnt uchar add 48 d
336x = div x 10
337cnt = add cnt 1
338end
339for i ulong cnt
340put buf i get tmp sub sub cnt 1 i
341end
342ok2 = syn_write_all fd buf cnt
343if ok2
344ret
345end
346end
347
348fn syn_write_hex32 fd int d view uchar * to void
349hx = new uchar 64 0
350syn_hex_encode d hx
351ok = syn_write_all fd hx 64
352if ok
353ret
354end
355end
356
357note ======== path validation & overlap detection ========
358
359note Validates repository-relative POSIX path per Synapse validate_repo_relative_path:
360note 1..=256 bytes, no leading/trailing '/', no '//', no '\\', no NUL, no '.', '..', '.git' segments.
361fn cs_path_valid p view uchar * to bool
362n = len p
363if or eq n 0 gt n 256
364ret false
365end
366if or eq get p 0 47 eq get p sub n 1 47
367ret false
368end
369seg_start = ulong 0
370for i ulong add n 1
371is_sep = false
372if eq i n
373is_sep = true
374else
375c = get p i
376if or eq c 0 eq c 92
377ret false
378end
379if eq c 47
380is_sep = true
381end
382end
383if is_sep
384seg_len = sub i seg_start
385if eq seg_len 0
386ret false
387end
388if eq seg_len 1
389if eq get p seg_start 46
390ret false
391end
392end
393if eq seg_len 2
394if and eq get p seg_start 46 eq get p add seg_start 1 46
395ret false
396end
397end
398if eq seg_len 4
399if eq get p seg_start 46
400if eq get p add seg_start 1 103
401if eq get p add seg_start 2 105
402if eq get p add seg_start 3 116
403ret false
404end
405end
406end
407end
408end
409seg_start = add i 1
410end
411end
412ret true
413end
414
415note Path overlap between two arena paths:
416note 0 = disjoint, 1 = exact match, 2 = directory prefix overlap (a is dir prefix of b or b of a)
417fn cs_paths_overlap_arena ao ulong al ulong bo ulong bl ulong to ulong
418if syn_arena_eq ao al bo bl
419ret 1
420end
421if lt al bl
422if eq get g_syn_str add bo al 47
423if syn_arena_eq ao al bo al
424ret 2
425end
426end
427end
428if lt bl al
429if eq get g_syn_str add ao bl 47
430if syn_arena_eq ao bl bo bl
431ret 2
432end
433end
434end
435ret 0
436end
437
438note Path overlap between two uchar views (0 disjoint, 1 exact, 2 dir prefix)
439fn cs_paths_overlap_view a view uchar * b view uchar * to ulong
440al = len a
441bl = len b
442if eq al bl
443for i ulong al
444if ne get a i get b i
445ret 0
446end
447end
448ret 1
449end
450if lt al bl
451if eq get b al 47
452for i ulong al
453if ne get a i get b i
454ret 0
455end
456end
457ret 2
458end
459ret 0
460end
461if eq get a bl 47
462for i ulong bl
463if ne get a i get b i
464ret 0
465end
466end
467ret 2
468end
469ret 0
470end
471
472note ======== changeset table accessors ========
473
474fn cs_reset to void
475put g_cs_hdr 0 0
476put g_cs_hdr 1 0
477put g_cs_stg_meta 0 0
478end
479
480fn cs_count to ulong
481ret get g_cs_hdr 0
482end
483
484fn cs_mget idx ulong f ulong to ulong
485ret get g_cs_meta add mul idx CS_FIELDS f
486end
487
488fn cs_mput idx ulong f ulong v ulong to void
489put g_cs_meta add mul idx CS_FIELDS f v
490end
491
492fn cs_get_id idx ulong dst edit uchar * to void
493base = mul idx 32
494for i ulong 32
495put dst i get g_cs_id add base i
496end
497end
498
499fn cs_get_parent idx ulong dst edit uchar * to void
500base = mul idx 32
501for i ulong 32
502put dst i get g_cs_parent add base i
503end
504end
505
506fn cs_get_state idx ulong to ulong
507ret cs_mget idx CSF_STATE
508end
509
510fn cs_get_parent_idx idx ulong to ulong
511ret cs_mget idx CSF_PIDX
512end
513
514fn cs_get_author_off idx ulong to ulong
515ret cs_mget idx CSF_AUTH_OFF
516end
517
518fn cs_get_author_len idx ulong to ulong
519ret cs_mget idx CSF_AUTH_LEN
520end
521
522fn cs_get_branch_off idx ulong to ulong
523ret cs_mget idx CSF_BR_OFF
524end
525
526fn cs_get_branch_len idx ulong to ulong
527ret cs_mget idx CSF_BR_LEN
528end
529
530fn cs_get_ts idx ulong to ulong
531ret cs_mget idx CSF_TS
532end
533
534fn cs_get_path_cnt idx ulong to ulong
535ret cs_mget idx CSF_PCNT
536end
537
538fn cs_get_path_off idx ulong pi ulong to ulong
539slot = add cs_mget idx CSF_PSTART pi
540ret get g_cs_paths mul slot 2
541end
542
543fn cs_get_path_len idx ulong pi ulong to ulong
544slot = add cs_mget idx CSF_PSTART pi
545ret get g_cs_paths add mul slot 2 1
546end
547
548fn cs_find_by_id id view uchar * to ulong
549if ne len id 32
550ret SYN_NONE
551end
552cnt = get g_cs_hdr 0
553for i ulong cnt
554base = mul i 32
555eq_ok = true
556for j ulong 32
557if ne get g_cs_id add base j get id j
558eq_ok = false
559break
560end
561end
562if eq_ok
563ret i
564end
565end
566ret SYN_NONE
567end
568
569note Pairwise path overlap between changesets idx_a and idx_b: 0 disjoint, 1 exact, 2 prefix
570fn cs_changesets_overlap idx_a ulong idx_b ulong to ulong
571na = cs_mget idx_a CSF_PCNT
572nb = cs_mget idx_b CSF_PCNT
573best = ulong 0
574for i ulong na
575ao = cs_get_path_off idx_a i
576al = cs_get_path_len idx_a i
577for j ulong nb
578bo = cs_get_path_off idx_b j
579bl = cs_get_path_len idx_b j
580ov = cs_paths_overlap_arena ao al bo bl
581if eq ov 1
582ret 1
583end
584if eq ov 2
585best = 2
586end
587end
588end
589ret best
590end
591
592note ======== canonical serialization & SHA-256 identity ========
593
594note Computes domain-separated SHA-256 over canonical serialization of changeset idx into out_id[32].
595note Layout: "SYNAPSE_CHANGESET_V1\0" | u16_le(1) | parent_id[32] | str(author) | str(branch) |
596note str(summary) | u64_le(created_ts) | u32_le(path_cnt) | str(path_i)* (sorted)
597fn cs_compute_id idx ulong dst edit uchar * to void
598ctx = new ulong 40 0
599par = new uchar 32 0
600nul = new uchar 1 0
601sha256_init ctx
602sha256_update ctx t_cs_dom
603sha256_update ctx nul
604syn_sha_u16 ctx 1
605cs_get_parent idx par
606sha256_update ctx par
607syn_sha_arena_str ctx cs_mget idx CSF_AUTH_OFF cs_mget idx CSF_AUTH_LEN
608syn_sha_arena_str ctx cs_mget idx CSF_BR_OFF cs_mget idx CSF_BR_LEN
609syn_sha_arena_str ctx cs_mget idx CSF_SUM_OFF cs_mget idx CSF_SUM_LEN
610syn_sha_u64 ctx cs_mget idx CSF_TS
611pcnt = cs_mget idx CSF_PCNT
612syn_sha_u32 ctx pcnt
613for i ulong pcnt
614syn_sha_arena_str ctx cs_get_path_off idx i cs_get_path_len idx i
615end
616sha256_final ctx dst
617end
618
619note Helper to append bytes to a flat buffer at *pos, returning new pos (or SYN_NONE on overflow).
620fn cs_buf_put_bytes dst edit uchar * pos ulong src view uchar * to ulong
621n = len src
622if gt add pos n len dst
623ret SYN_NONE
624end
625for i ulong n
626put dst add pos i get src i
627end
628ret add pos n
629end
630
631fn cs_buf_put_arena_str dst edit uchar * pos ulong off ulong slen ulong to ulong
632if gt add add pos 4 slen len dst
633ret SYN_NONE
634end
635syn_put_u32_le dst pos slen
636p = add pos 4
637for i ulong slen
638put dst add p i get g_syn_str add off i
639end
640ret add p slen
641end
642
643note Serializes changeset idx into canonical bytes in dst; returns byte length or SYN_NONE if dst too small.
644fn cs_canonical_serialize idx ulong dst edit uchar * to ulong
645par = new uchar 32 0
646pos = ulong 0
647pos = cs_buf_put_bytes dst pos t_cs_dom
648if eq pos SYN_NONE
649ret SYN_NONE
650end
651if ge add pos 3 len dst
652ret SYN_NONE
653end
654put dst pos 0
655pos = add pos 1
656syn_put_u16_le dst pos 1
657pos = add pos 2
658cs_get_parent idx par
659pos = cs_buf_put_bytes dst pos par
660if eq pos SYN_NONE
661ret SYN_NONE
662end
663pos = cs_buf_put_arena_str dst pos cs_mget idx CSF_AUTH_OFF cs_mget idx CSF_AUTH_LEN
664if eq pos SYN_NONE
665ret SYN_NONE
666end
667pos = cs_buf_put_arena_str dst pos cs_mget idx CSF_BR_OFF cs_mget idx CSF_BR_LEN
668if eq pos SYN_NONE
669ret SYN_NONE
670end
671pos = cs_buf_put_arena_str dst pos cs_mget idx CSF_SUM_OFF cs_mget idx CSF_SUM_LEN
672if eq pos SYN_NONE
673ret SYN_NONE
674end
675if gt add pos 12 len dst
676ret SYN_NONE
677end
678syn_put_u64_le dst pos cs_mget idx CSF_TS
679pos = add pos 8
680pcnt = cs_mget idx CSF_PCNT
681syn_put_u32_le dst pos pcnt
682pos = add pos 4
683for i ulong pcnt
684pos = cs_buf_put_arena_str dst pos cs_get_path_off idx i cs_get_path_len idx i
685if eq pos SYN_NONE
686ret SYN_NONE
687end
688end
689ret pos
690end
691
692note ======== changeset staging & creation ========
693
694fn cs_stage_begin author view uchar * branch view uchar * ts ulong to ulong
695if or eq len author 0 gt len author 128
696ret SYN_ERR_MALFORMED
697end
698if or eq len branch 0 gt len branch 128
699ret SYN_ERR_MALFORMED
700end
701ao = syn_arena_put author
702if eq ao SYN_NONE
703ret SYN_ERR_CAPACITY
704end
705bo = syn_arena_put branch
706if eq bo SYN_NONE
707ret SYN_ERR_CAPACITY
708end
709put g_cs_stg_meta 0 1
710put g_cs_stg_meta 1 ao
711put g_cs_stg_meta 2 len author
712put g_cs_stg_meta 3 bo
713put g_cs_stg_meta 4 len branch
714put g_cs_stg_meta 5 ts
715put g_cs_stg_meta 6 0
716put g_cs_stg_meta 7 0
717put g_cs_stg_meta 8 0
718for i ulong 32
719put g_cs_stg_par i 0
720end
721ret SYN_OK
722end
723
724fn cs_stage_set_parent par view uchar * to ulong
725if ne len par 32
726ret SYN_ERR_MALFORMED
727end
728for i ulong 32
729put g_cs_stg_par i get par i
730end
731ret SYN_OK
732end
733
734fn cs_stage_set_summary summ view uchar * to ulong
735if gt len summ 512
736ret SYN_ERR_MALFORMED
737end
738so = syn_arena_put summ
739if eq so SYN_NONE
740ret SYN_ERR_CAPACITY
741end
742put g_cs_stg_meta 7 so
743put g_cs_stg_meta 8 len summ
744ret SYN_OK
745end
746
747fn cs_stage_shift_right cnt ulong ins ulong to void
748k = cnt
749while gt k ins bound 32
750prev = sub k 1
751v0 = get g_cs_stg_paths mul prev 2
752v1 = get g_cs_stg_paths add mul prev 2 1
753put g_cs_stg_paths mul k 2 v0
754put g_cs_stg_paths add mul k 2 1 v1
755k = prev
756end
757end
758
759note Adds a validated path into the staged path list, maintaining strict sorted order and rejecting duplicates.
760fn cs_stage_add_path p view uchar * to ulong
761if eq cs_path_valid p false
762ret SYN_ERR_MALFORMED
763end
764cnt = get g_cs_stg_meta 6
765if ge cnt CS_MAX_PATHS
766ret SYN_ERR_CAPACITY
767end
768po = syn_arena_put p
769if eq po SYN_NONE
770ret SYN_ERR_CAPACITY
771end
772pl = len p
773note find insertion index while rejecting duplicates
774ins = cnt
775for i ulong cnt
776eo = get g_cs_stg_paths mul i 2
777el = get g_cs_stg_paths add mul i 2 1
778cmp = syn_arena_cmp po pl eo el
779if eq cmp 0
780ret SYN_ERR_DUPLICATE
781end
782if eq cmp 1
783ins = i
784break
785end
786end
787cs_stage_shift_right cnt ins
788put g_cs_stg_paths mul ins 2 po
789put g_cs_stg_paths add mul ins 2 1 pl
790put g_cs_stg_meta 6 add cnt 1
791ret SYN_OK
792end
793
794fn cs_stage_write_slot idx ulong pidx ulong pused ulong pcnt ulong to void
795cs_mput idx CSF_AUTH_OFF get g_cs_stg_meta 1
796cs_mput idx CSF_AUTH_LEN get g_cs_stg_meta 2
797cs_mput idx CSF_BR_OFF get g_cs_stg_meta 3
798cs_mput idx CSF_BR_LEN get g_cs_stg_meta 4
799cs_mput idx CSF_STATE CS_STATE_DRAFT
800cs_mput idx CSF_PSTART pused
801cs_mput idx CSF_PCNT pcnt
802cs_mput idx CSF_TS get g_cs_stg_meta 5
803cs_mput idx CSF_PIDX pidx
804cs_mput idx CSF_SUM_OFF get g_cs_stg_meta 7
805cs_mput idx CSF_SUM_LEN get g_cs_stg_meta 8
806cs_mput idx CSF_USED 1
807base = mul idx 32
808for i ulong 32
809put g_cs_parent add base i get g_cs_stg_par i
810end
811for i ulong pcnt
812slot = add pused i
813v0 = get g_cs_stg_paths mul i 2
814v1 = get g_cs_stg_paths add mul i 2 1
815put g_cs_paths mul slot 2 v0
816put g_cs_paths add mul slot 2 1 v1
817end
818end
819
820note Commits staged changeset, computes SHA-256 digest, links into DAG, invalidates prior branch reviews,
821note and records CS_CREATE in the ledger. Writes new index to g_cs_hdr[2] and returns SYN_OK or error.
822fn cs_stage_commit to ulong
823par = new uchar 32 0
824id = new uchar 32 0
825if ne get g_cs_stg_meta 0 1
826ret SYN_ERR_MALFORMED
827end
828pcnt = get g_cs_stg_meta 6
829if eq pcnt 0
830ret SYN_ERR_MALFORMED
831end
832idx = get g_cs_hdr 0
833if ge idx CS_MAX
834ret SYN_ERR_CAPACITY
835end
836pused = get g_cs_hdr 1
837if gt add pused pcnt CS_PATH_POOL
838ret SYN_ERR_CAPACITY
839end
840for i ulong 32
841put par i get g_cs_stg_par i
842end
843pidx = SYN_NONE
844if eq syn_digest_is_zero par false
845pidx = cs_find_by_id par
846if eq pidx SYN_NONE
847ret SYN_ERR_NOT_FOUND
848end
849end
850cs_stage_write_slot idx pidx pused pcnt
851cs_compute_id idx id
852if ne cs_find_by_id id SYN_NONE
853cs_mput idx CSF_USED 0
854ret SYN_ERR_DUPLICATE
855end
856base = mul idx 32
857for i ulong 32
858put g_cs_id add base i get id i
859end
860put g_cs_hdr 0 add idx 1
861put g_cs_hdr 1 add pused pcnt
862put g_cs_hdr 2 idx
863put g_cs_stg_meta 0 0
864if ne pidx SYN_NONE
865rc = gr_link_parent idx pidx
866if ne rc SYN_OK
867ret rc
868end
869end
870rv_invalidate_branch cs_mget idx CSF_BR_OFF cs_mget idx CSF_BR_LEN idx
871if eq syn_arena_eq_view cs_mget idx CSF_BR_OFF cs_mget idx CSF_BR_LEN t_br_main false
872br_rc = gr_branch_set_arena cs_mget idx CSF_BR_OFF cs_mget idx CSF_BR_LEN idx
873if ne br_rc SYN_OK
874ret br_rc
875end
876end
877lg_set_subject id
878seq = lg_append_arena 1 cs_mget idx CSF_TS cs_mget idx CSF_AUTH_OFF cs_mget idx CSF_AUTH_LEN cs_mget idx CSF_BR_OFF cs_mget idx CSF_BR_LEN
879if eq seq 0
880ret SYN_ERR_CAPACITY
881end
882ret SYN_OK
883end
884
885fn cs_last_idx to ulong
886ret get g_cs_hdr 2
887end
888
889note State transition rule:
890note Allowed forward edges:
891note DRAFT(0) -> PROPOSED(1) | ABANDONED(4)
892note PROPOSED(1) -> REVIEWED(2) | ABANDONED(4)
893note REVIEWED(2) -> LANDED(3) | ABANDONED(4)
894note LANDED and ABANDONED are terminal; backward and skip moves are refused.
895fn cs_state_move_allowed cur ulong nxt ulong to bool
896if eq cur CS_STATE_DRAFT
897ret or eq nxt CS_STATE_PROPOSED eq nxt CS_STATE_ABANDONED
898end
899if eq cur CS_STATE_PROPOSED
900ret or eq nxt CS_STATE_REVIEWED eq nxt CS_STATE_ABANDONED
901end
902if eq cur CS_STATE_REVIEWED
903ret or eq nxt CS_STATE_LANDED eq nxt CS_STATE_ABANDONED
904end
905ret false
906end
907
908fn cs_write_state_name fd int st ulong to void
909if eq st CS_STATE_DRAFT
910syn_write_text fd t_st_draft
911ret
912end
913if eq st CS_STATE_PROPOSED
914syn_write_text fd t_st_prop
915ret
916end
917if eq st CS_STATE_REVIEWED
918syn_write_text fd t_st_rev
919ret
920end
921if eq st CS_STATE_LANDED
922syn_write_text fd t_st_land
923ret
924end
925syn_write_text fd t_st_abnd
926end
927
928fn cs_transition idx ulong nxt ulong ts ulong to ulong
929id = new uchar 32 0
930if ge idx get g_cs_hdr 0
931ret SYN_ERR_NOT_FOUND
932end
933cur = cs_mget idx CSF_STATE
934if eq cs_state_move_allowed cur nxt false
935ret SYN_ERR_STATE_INVALID
936end
937cs_mput idx CSF_STATE nxt
938cs_get_id idx id
939lg_set_subject id
940if eq nxt CS_STATE_PROPOSED
941seq = lg_append_view 2 ts cs_mget idx CSF_AUTH_OFF cs_mget idx CSF_AUTH_LEN t_st_prop
942if eq seq 0
943ret SYN_ERR_CAPACITY
944end
945ret SYN_OK
946end
947if eq nxt CS_STATE_REVIEWED
948seq = lg_append_view 2 ts cs_mget idx CSF_AUTH_OFF cs_mget idx CSF_AUTH_LEN t_st_rev
949if eq seq 0
950ret SYN_ERR_CAPACITY
951end
952ret SYN_OK
953end
954if eq nxt CS_STATE_LANDED
955seq = lg_append_view 2 ts cs_mget idx CSF_AUTH_OFF cs_mget idx CSF_AUTH_LEN t_st_land
956if eq seq 0
957ret SYN_ERR_CAPACITY
958end
959ret SYN_OK
960end
961seq = lg_append_view 2 ts cs_mget idx CSF_AUTH_OFF cs_mget idx CSF_AUTH_LEN t_st_abnd
962if eq seq 0
963ret SYN_ERR_CAPACITY
964end
965ret SYN_OK
966end