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 / claims / src / 40_state.om

630 lines · 10799 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2note Claim table, id hash table, target canonical form, overlap rules, ledger loader/checker and the
3note list / holder JSON writers.
4
5note claim cells (nf per claim in gcl)
6const cid ulong 0
7const cag ulong 1
8const cagl ulong 2
9const cse ulong 3
10const csel ulong 4
11const ckind ulong 5
12const ctg ulong 6
13const ctgl ulong 7
14const cmode ulong 8
15const cst ulong 9
16const cin ulong 11
17const cinl ulong 12
18const cexp ulong 13
19const csince ulong 14
20const cseq ulong 15
21const cor ulong 16
22const corl ulong 17
23const cev ulong 18
24const cevl ulong 19
25const cclo ulong 20
26
27text mnf "not found"
28text mnh "not holder"
29text mna "not active"
30text mchain "ledger integrity failure; run verify"
31text mtab "claim table full"
32text mio "ledger read error"
33
34fn c_get i ulong f ulong to ulong
35ret get gcl add mul i nf f
36end
37
38fn c_put i ulong f ulong v ulong to void
39put gcl add mul i nf f v
40end
41
42note ======== id hash table: key = 16-hex id as ulong, value = claim index + 1 ========
43
44fn ht_slot k ulong to ulong
45p = and k htmask
46while true bound 270000
47v = get ght p
48if eq v 0
49ret p
50end
51if eq get ghk p k
52ret p
53end
54p = and add p 1 htmask
55end
56ret 0
57end
58
59fn ht_find k ulong to ulong
60p = ht_slot k
61v = get ght p
62if eq v 0
63ret ncl
64end
65ret sub v 1
66end
67
68note ht_put: false when the key is already present
69fn ht_put k ulong i ulong to bool
70p = ht_slot k
71if ne get ght p 0
72ret false
73end
74put ght p add i 1
75put ghk p k
76ret true
77end
78
79note hex16 of s[o..o+16) -> ulong (input already checked lowercase hex)
80fn hx_u s edit uchar * o ulong to ulong
81v = ulong 0
82for i ulong 16
83v = or shl v 4 hexv get s add o i
84end
85ret v
86end
87
88fn o_id v ulong to void
89for i ulong 16
90o_b hexd and shr v sub 60 mul i 4 15
91end
92end
93
94note ======== targets ========
95
96note kind code (0 repo 1 branch 2 path 3 pr 4 ext) of the kind word before the first ':', or tbad
97fn tg_kind s edit uchar * o ulong n ulong to ulong
98k = ulong 0
99while lt k n bound 300
100if eq get s add o k 58
101break
102end
103k = add k 1
104end
105if ge k n
106ret tbad
107end
108w = tw_find part s o k wall 13 5
109if eq w tbad
110ret tbad
111end
112ret sub w 13
113end
114
115fn rp_ch c uchar to bool
116if and ge c 97 le c 122
117ret true
118end
119if and ge c 48 le c 57
120ret true
121end
122ret or or eq c 46 eq c 95 eq c 45
123end
124
125note tg_ext: appends the lowercased ext scheme of s[o+i..o+n); returns the index of its ':' or tbad
126fn tg_ext s edit uchar * o ulong n ulong i0 ulong to ulong
127i = i0
128scl = ulong 0
129while lt i n bound 300
130c = lower get s add o i
131if eq c 58
132break
133end
134if eq or rp_ch c eq c 43 false
135ret tbad
136end
137ar_b c
138scl = add scl 1
139i = add i 1
140end
141if or or eq scl 0 ge i n eq add i 1 n
142ret tbad
143end
144ret i
145end
146
147note tg_own: appends the lowercased OWNER/REPO of s[o+i..o+n); returns the index after it or tbad
148fn tg_own s edit uchar * o ulong n ulong i0 ulong to ulong
149i = i0
150sl = ulong 0
151rl = ulong 0
152while lt i n bound 300
153c = lower get s add o i
154if or eq c 35 eq c 58
155break
156end
157if eq c 47
158sl = add sl 1
159if eq rl 0
160ret tbad
161end
162if eq get s add o sub i 1 47
163ret tbad
164end
165else
166if eq rp_ch c false
167ret tbad
168end
169end
170ar_b c
171rl = add rl 1
172i = add i 1
173end
174if ne sl 1
175ret tbad
176end
177if eq get s add o sub i 1 47
178ret tbad
179end
180ret i
181end
182
183note tg_sfx: s[o+i..o+n) is a valid branch/pr/path suffix for kind kd (separator plus non-empty rest)
184fn tg_sfx s edit uchar * o ulong n ulong i ulong kd ulong to bool
185if ge add i 1 n
186ret false
187end
188c = get s add o i
189if and eq kd 2 ne c 58
190ret false
191end
192if and ne kd 2 ne c 35
193ret false
194end
195if eq kd 3
196for j ulong sub sub n i 1
197d = get s add add o i add j 1
198if or lt d 48 gt d 57
199ret false
200end
201end
202end
203ret true
204end
205
206note tg_canon: canonical copy of target s[o..o+n) appended to the arena; returns its offset (length
207note in gx[xpos]) and the kind in gx[xkind]; tbad (arena unchanged) when the target is invalid.
208fn tg_canon s edit uchar * o ulong n ulong to ulong
209if eq sf_ok part s o n false
210ret tbad
211end
212kd = tg_kind s o n
213if eq kd tbad
214ret tbad
215end
216put gx xkind kd
217st = get gx xarn
218i = add tw_len wall add kd 13 1
219ar_lnv s o i
220if eq kd 4
221i = tg_ext s o n i
222else
223i = tg_own s o n i
224end
225if eq i tbad
226put gx xarn st
227ret tbad
228end
229if eq kd 0
230if lt i n
231put gx xarn st
232ret tbad
233end
234else
235if ne kd 4
236if eq tg_sfx s o n i kd false
237put gx xarn st
238ret tbad
239end
240end
241end
242ar_lnv s add o i sub n i
243put gx xpos sub get gx xarn st
244ret st
245end
246
247note end of the owner/repo part of arena target t (kind code k, kind word skipped)
248fn tg_rend k ulong t ulong n ulong to ulong
249i = add tw_len wall add k 13 1
250while lt i n bound 300
251c = ar_g add t i
252if or eq c 35 eq c 58
253ret i
254end
255i = add i 1
256end
257ret n
258end
259
260note literal prefix (bytes before the first '*') length of the glob part from offset g
261fn gl_pre t ulong g ulong n ulong to ulong
262i = g
263while lt i n bound 300
264if eq ar_g add t i 42
265ret sub i g
266end
267i = add i 1
268end
269ret sub n g
270end
271
272note ov_path: 3 when the literal glob prefixes of two path targets (repo part ending at ea/eb) agree, else 0
273fn ov_path ta ulong ea ulong la ulong tb ulong eb ulong lb ulong to ulong
274pa = gl_pre ta add ea 1 la
275pb = gl_pre tb add eb 1 lb
276m = pa
277if lt pb m
278m = pb
279end
280ga = add ta add ea 1
281gb = add tb add eb 1
282if ar_eq ga m gb m
283ret 3
284end
285ret 0
286end
287
288note ov: overlap reason of two canonical arena targets: 0 none 1 same_target 2 repo_covers
289note 3 path_overlap 4 ext_casefold
290fn ov ka ulong ta ulong la ulong kb ulong tb ulong lb ulong to ulong
291if or eq ka 4 eq kb 4
292if ne ka kb
293ret 0
294end
295if ar_eq ta la tb lb
296ret 1
297end
298if ar_eqci ta la tb lb
299ret 4
300end
301ret 0
302end
303if ar_eq ta la tb lb
304ret 1
305end
306ea = tg_rend ka ta la
307eb = tg_rend kb tb lb
308sa = add tw_len wall add ka 13 1
309sb = add tw_len wall add kb 13 1
310if eq ar_eq add ta sa sub ea sa add tb sb sub eb sb false
311ret 0
312end
313if or eq ka 0 eq kb 0
314ret 2
315end
316if or ne ka 2 ne kb 2
317ret 0
318end
319ret ov_path ta ea la tb eb lb
320end
321
322fn c_act i ulong to bool
323if ne c_get i cclo 0
324ret false
325end
326ret gt c_get i cexp get gx xnow
327end
328
329note ======== ledger loading ========
330
331note paths (filled by main): gpth holds home-relative strings; gx[xpled]/gx[xpledl] etc. index it
332global gpth uchar 16384
333
334fn ar_eqs a ulong an ulong s edit uchar * o ulong n ulong to bool
335if ne an n
336ret false
337end
338for i ulong n
339if ne ar_g add a i get s add o i
340ret false
341end
342end
343ret true
344end
345
346note append the line's evidence (gp qev) to claim i's evidence, joined by ',', capped at evcap
347fn ld_ev i ulong s edit uchar * to void
348n = get gp qevl
349if eq n 0
350ret
351end
352ol = c_get i cevl
353add1 = ulong 0
354if gt ol 0
355add1 = 1
356end
357if gt add add ol add1 n evcap
358ret
359end
360st = get gx xarn
361ar_cpv c_get i cev ol
362if gt ol 0
363ar_b 44
364end
365ar_lnv s get gp qev n
366c_put i cev st
367c_put i cevl add add ol add1 n
368end
369
370note ld_apply: the scanned line s (gp cells) becomes claim state. 0 ok, 4 id error, 6 malformed.
371fn ld_apply s edit uchar * to ulong
372op = get gp qop
373id = hx_u s get gp qid
374kd = sub get gp qkind 13
375if eq op 0
376if ge get gx xnc ncl
377die 70 mtab
378end
379i = get gx xnc
380st = get gx xarn
381t = tg_canon s get gp qtg get gp qtgl
382if eq t tbad
383ret 6
384end
385if eq ar_eqs t get gx xpos s get gp qtg get gp qtgl false
386put gx xarn st
387ret 6
388end
389if ne get gx xkind kd
390put gx xarn st
391ret 6
392end
393if eq ht_put id i false
394put gx xarn st
395ret 4
396end
397put gx xnc add i 1
398c_put i cid id
399c_put i ckind kd
400c_put i ctg t
401c_put i ctgl get gp qtgl
402c_put i cag ar_ln s get gp qag get gp qagl
403c_put i cagl get gp qagl
404c_put i cse ar_ln s get gp qse get gp qsel
405c_put i csel get gp qsel
406c_put i cmode sub get gp qmode 18
407c_put i cst sub get gp qst 5
408c_put i cin ar_ln s get gp qin get gp qinl
409c_put i cinl get gp qinl
410c_put i cexp get gp qexp
411c_put i csince get gp qts
412c_put i cseq get gp qseq
413c_put i cor ar_ln s get gp qor get gp qorl
414c_put i corl get gp qorl
415c_put i cev ar_ln s get gp qev get gp qevl
416c_put i cevl get gp qevl
417c_put i cclo 0
418ret 0
419end
420i = ht_find id
421if eq i ncl
422ret 4
423end
424if ne c_get i cclo 0
425ret 6
426end
427if ne op 4
428if eq ar_eqs c_get i cag c_get i cagl s get gp qag get gp qagl false
429ret 6
430end
431end
432c_put i cseq get gp qseq
433c_put i cexp get gp qexp
434c_put i cst sub get gp qst 5
435if eq op 2
436c_put i cclo 1
437end
438if eq op 4
439c_put i cclo 2
440end
441ld_ev i s
442ret 0
443end
444
445note ld_line: chain checks against xseq / glh, then apply. 0 ok, else wwhy index + 1
446note (1 malformed, 2 seq, 3 prev, 5 id).
447fn ld_line s edit uchar * n ulong to ulong
448if eq pl_line s n false
449ret 1
450end
451if ne get gp qseq add get gx xseq 1
452ret 2
453end
454p = get gp qprev
455for i ulong 64
456if ne get s add p i get glh i
457ret 3
458end
459end
460r = ld_apply s
461if eq r 4
462ret 5
463end
464if eq r 6
465ret 1
466end
467h = get gp qhash
468for j ulong 64
469put glh j get s add h j
470end
471put gx xseq get gp qseq
472put gx xevn add get gx xevn 1
473ret 0
474end
475
476fn glh_zero to void
477for i ulong 64
478put glh i 48
479end
480end
481
482note ld_open: open the ledger read-only; absent -> -1
483fn ld_open to long
484fd = fopen gpth get gx xpled get gx xpledl 524288 0
485if eq fd -2
486ret -1
487end
488if lt fd 0
489die 70 mio
490end
491ret fd
492end
493
494fn ld_reset to void
495put gx xnc 0
496put gx xseq 0
497put gx xevn 0
498glh_zero
499for i ulong 262144
500put ght i 0
501end
502end
503
504note ld_all: load the whole ledger into the claim table (any failure: die 3)
505fn ld_all s edit uchar * to void
506ld_reset
507fd = ld_open
508if lt fd 0
509ret
510end
511r_open fd
512while true bound 999999
513n = r_line s
514if eq n -1
515break
516end
517if lt n 0
518die 3 mchain
519end
520if ne ld_line s ulong n 0
521die 3 mchain
522end
523end
524c = fclose int fd
525if ne get gx xreof 1
526die 70 mio
527end
528end
529
530note ======== JSON writers ========
531
532fn o_kq t view uchar * first bool to void
533o_key t first
534o_q
535end
536
537text jid "id"
538text jag "agent"
539text jse "session"
540text jkind "kind"
541text jtg "target"
542text jmode "mode"
543text jst "state"
544text jin "intent"
545text jexp "expires"
546text jact "active"
547text jsin "since"
548text jlseq "last_seq"
549text jor "origin"
550text jev "evidence"
551
552fn o_tsq s ulong to void
553o_q
554o_ts s
555o_q
556end
557
558fn o_arq off ulong n ulong to void
559o_q
560o_ar off n
561o_q
562end
563
564note the `list` object of claim i
565fn o_claim i ulong to void
566o_kq jid true
567o_id c_get i cid
568o_q
569o_key jag false
570o_arq c_get i cag c_get i cagl
571o_key jse false
572o_arq c_get i cse c_get i csel
573o_key jkind false
574o_q
575o_wd wall add c_get i ckind 13
576o_q
577o_key jtg false
578o_arq c_get i ctg c_get i ctgl
579o_key jmode false
580o_q
581o_wd wall add c_get i cmode 18
582o_q
583o_key jst false
584o_q
585o_wd wall add c_get i cst 5
586o_q
587o_key jin false
588o_arq c_get i cin c_get i cinl
589o_key jexp false
590o_tsq c_get i cexp
591o_key jact false
592if c_act i
593o_t ttrue
594else
595o_t tfalse
596end
597o_key jsin false
598o_tsq c_get i csince
599o_key jlseq false
600o_u c_get i cseq
601o_key jor false
602o_arq c_get i cor c_get i corl
603o_key jev false
604o_b 91
605o_ar c_get i cev c_get i cevl
606o_b 93
607o_b 125
608end
609
610note the conflict `holder` object of claim i
611fn o_hold i ulong to void
612o_kq jid true
613o_id c_get i cid
614o_q
615o_key jag false
616o_arq c_get i cag c_get i cagl
617o_key jse false
618o_arq c_get i cse c_get i csel
619o_key jtg false
620o_arq c_get i ctg c_get i ctgl
621o_key jmode false
622o_q
623o_wd wall add c_get i cmode 18
624o_q
625o_key jexp false
626o_tsq c_get i cexp
627o_key jin false
628o_arq c_get i cin c_get i cinl
629o_b 125
630end