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 / tools / trit / trit.om

1008 lines · 16555 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-only
2note trit: one native 3-valued signal per bot, read from DIR/inbox/<bot>.md and <bot>.emergency.md.
3note Built after the core of seed/trace/sha256.om in OM-2; see tools/trit/build.sh and README.md.
4note Usage: trit DIR BOT... one line "<bot> <1|2> <token>" per bot to wake.
5note Daily: trit DIR --inspect BOT... emits inspection1 only; --consume BOT TOKEN moves state silently.
6note Daily mode grants no process authority and keeps legacy codebook meanings unchanged.
7note trit DIR --ack BOT TOKEN prints the receipt "<bot> 0", then the next wake if any.
8note Silence means nothing to do (TURNS.md 12:24): a bot with no wake line prints nothing. 0 is only the
9note ack receipt. Exit: 0 completed (consume is silent), 1 no unread work, 2 usage, 3 consumption refused, 4/5 bad codebook/key, 70+ internal.
10note 2 = an unread line that verifies with the codebook (tools/codebook/codebook.om, compiled into this
11note binary) and carries a 3xx code: last '|' field ocb2:<cbsha>:<counter>:<digits>:<mac>, over
12note ocb2|cbsha|counter|lane|ts|from|to|tag|text|ref|digits. Signed lines are the 6-field form
13note ts|from|to|tag|text|ref; to must equal the BOT being read, and the lane (n or e) must match the
14note file. An ocb1 line is at most 1. Key and codebook at DIR/codebook/{key,codebook.tsv}; the key
15note must be mode 0600 (no group/other bits) and its path must not contain a boards/ segment. Counters
16note DIR/inbox/<bot>.<sender>.ctr hold "<counter> <ts>". Anything that is not fyi gives at most 1,
17note including STOP text, a stop tag, unsigned, tampered, stale, replayed and retargeted lines.
18note Token: L<offset>:<sha256 hex of bytes 0..offset>, L = n (inbox) or e (emergency lane).
19note A cursor is DIR/inbox/<bot>[.emergency].md.cur holding "<offset> <sha256 hex of bytes 0..offset>".
20const NMAX ulong 3
21const WIN ulong 60
22global dir uchar 4096
23global dn ulong 1
24global bot uchar 256
25global bn ulong 1
26global acc ulong *
27global an ulong 1
28global rej ulong 1
29global lf ulong 3
30global vk ulong 1
31global inspecting ulong 1
32global outb uchar 512
33global tk uchar 160
34global tl ulong 2
35text tinbox "inbox/"
36text tmd ".md"
37text temd ".emergency.md"
38text tcur ".cur"
39text tack "--ack"
40text tinspect "--inspect"
41text tconsume "--consume"
42text tstop "stop"
43text tstopu "STOP"
44text tfyi "fyi"
45text tdrake "drake"
46text tcbd "codebook/"
47text tcbf "codebook.tsv"
48text tkey "key"
49text trej "rejected "
50
51note Dominance 2 > 1 > 0 is the maximum of the two values.
52fn tmax a trit b trit to trit
53if gt open a open b
54ret a
55end
56ret b
57end
58
59fn pdir to void
60put pn 0 0
61for i ulong get dn 0 bound BIG
62pbyte get dir i
63end
64if gt get dn 0 0
65pbyte 47
66end
67ptext tinbox
68end
69
70fn pbase lane ulong to void
71if eq lane 1
72put vln 0 101
73else
74put vln 0 110
75end
76pdir
77for i ulong get bn 0 bound BIG
78pbyte get bot i
79end
80if eq lane 1
81ptext temd
82else
83ptext tmd
84end
85end
86
87fn loadf to bool
88put fbn 0 0
89fd = fopen pb 0 get pn 0 0 0
90if lt fd 0
91ret false
92end
93n = ulong 0
94while true bound BIG
95if lt len fb add n 4096
96e1 = enlarge fb add n 4096
97if ne e1 0
98exit 72
99end
100end
101r = read int fd fb n 4096
102if lt r 0
103exit 73
104end
105if eq r 0
106break
107end
108n = add n ulong r
109end
110e2 = fclose int fd
111put fbn 0 n
112ret true
113end
114
115fn lastnl to ulong
116i = get fbn 0
117while gt i 0 bound BIG
118if eq get fb sub i 1 10
119ret sub i 1
120end
121i = sub i 1
122end
123ret get fbn 0
124end
125
126note A content line starts with a timestamp YYYY-MM-DDT; anything else (a header) is not content.
127fn ists p ulong e ulong to bool
128if lt sub e p 11
129ret false
130end
131for i ulong 11
132c = get fb add p i
133if or eq i 4 eq i 7
134if ne c 45
135ret false
136end
137else
138if eq i 10
139if ne c 84
140ret false
141end
142else
143if eq isdig c false
144ret false
145end
146end
147end
148end
149ret true
150end
151
152fn fpre t view uchar * to bool
153s = get fs 0
154if lt sub get fs 1 s len t
155ret false
156end
157for i ulong len t bound BIG
158if ne get fb add s i get t i
159ret false
160end
161end
162ret true
163end
164
165fn npipe p ulong e ulong to ulong
166c = ulong 0
167for i ulong sub e p bound BIG
168if eq get fb add p i 124
169c = add c 1
170end
171end
172ret c
173end
174
175fn lastpipe p ulong e ulong to ulong
176i = e
177while gt i p bound BIG
178if eq get fb sub i 1 124
179ret sub i 1
180end
181i = sub i 1
182end
183ret e
184end
185
186fn startsw q ulong e ulong t view uchar * to bool
187if lt sub e q len t
188ret false
189end
190for i ulong len t bound BIG
191if ne get fb add q i get t i
192ret false
193end
194end
195ret true
196end
197
198note Exact STOP: the text field is "STOP", or starts "STOP " or "STOP:".
199fn stopx to bool
200s = get fs 0
201n = sub get fs 1 s
202if lt n 4
203ret false
204end
205for i ulong 4
206if ne get fb add s i get tstopu i
207ret false
208end
209end
210if eq n 4
211ret true
212end
213c = get fb add s 4
214if or eq c 32 eq c 58
215ret true
216end
217ret false
218end
219
220note Kind of one content line: 2 stop (tag "stop" or exact STOP text), 0 fyi (never wakes), else 1.
221note A stop wins over fyi.
222fn lnkind p ulong e ulong to ulong
223lfmt p e
224ee = get lf 0
225tg = get lf 1
226if splitf p ee tg 124
227if feq tstop
228ret 2
229end
230end
231if splitf p ee add tg 1 124
232if stopx
233ret 2
234end
235end
236if splitf p ee tg 124
237if feq tfyi
238ret 0
239end
240end
241ret 1
242end
243
244note SHA-256 of fb[s..s+n) as 64 lowercase hex bytes in hx, on the seeded core above.
245fn hfb s ulong n ulong to void
246hinit
247full = div n 64
248for b ulong full bound BIG
249o = add s mul b 64
250for i ulong 64
251put blk i get fb add o i
252end
253compress
254end
255hpadf add s mul full 64 rem n 64 n
256hhex
257end
258
259fn hpadf o ulong left ulong n ulong to void
260for i ulong 64
261put blk i 0
262end
263for j ulong left bound BIG
264put blk j get fb add o j
265end
266put blk left 128
267if ge left 56
268compress
269for i ulong 64
270put blk i 0
271end
272end
273bits = mul n 8
274for q ulong 8
275v = and shr bits mul sub 7 q 8 255
276put blk add 56 q uchar v
277end
278compress
279end
280
281note Unread starts at the cursor offset only when the cursor is well formed, inside the file, on a
282note line end, and the sha256 of every byte before it matches. Otherwise the whole file is unread.
283fn rdcur lane ulong to ulong
284pbase lane
285ptext tcur
286fd = fopen pb 0 get pn 0 0 0
287if lt fd 0
288ret 0
289end
290r = read int fd cb 0 len cb
291e2 = fclose int fd
292if lt r 66
293ret 0
294end
295n = ulong r
296off = ulong 0
297i = ulong 0
298while lt i n bound BIG
299c = get cb i
300if eq c 32
301break
302end
303if eq isdig c false
304ret 0
305end
306if gt off 100000000000000
307ret 0
308end
309off = add mul off 10 ulong sub c 48
310i = add i 1
311end
312if or eq off 0 gt off get fbn 0
313ret 0
314end
315if gt add i 65 n
316ret 0
317end
318if ne get fb sub off 1 10
319ret 0
320end
321hfb 0 off
322for j ulong 64
323if ne get hx j get cb add add i 1 j
324ret 0
325end
326end
327ret off
328end
329
330fn scann st ulong to trit
331t = tritmk 0
332p = st
333while lt p get fbn 0 bound BIG
334e = lnend p
335if eq e get fbn 0
336break
337end
338if ists p e
339t = tmax t lnlevel p e
340end
341p = add e 1
342end
343ret t
344end
345
346fn absd a ulong b ulong to ulong
347if gt a b
348ret sub a b
349end
350ret sub b a
351end
352
353fn sameid a ulong m ulong s ulong n ulong to bool
354if ne m n
355ret false
356end
357for i ulong n bound BIG
358if ne get fb add a i get fb add s i
359ret false
360end
361end
362ret true
363end
364
365fn dig2 q ulong to ulong
366a = get fb q
367b = get fb add q 1
368if or eq isdig a false eq isdig b false
369ret 100000
370end
371ret add mul ulong sub a 48 10 ulong sub b 48
372end
373
374fn days y ulong m ulong d ulong to ulong
375yy = y
376if le m 2
377yy = sub y 1
378end
379era = div yy 400
380yoe = sub yy mul era 400
381mp = add m 9
382if gt m 2
383mp = sub m 3
384end
385doy = add div add mul 153 mp 2 5 sub d 1
386q4 = div yoe 4
387c1 = div yoe 100
388doe = add mul yoe 365 q4
389doe = sub add doe doy c1
390ret add mul era 146097 doe
391end
392
393note Minutes since a fixed civil origin, plus one; 0 means the timestamp does not parse.
394fn minute p ulong e ulong to ulong
395if lt sub e p 16
396ret 0
397end
398if ne get fb add p 13 58
399ret 0
400end
401y = add mul dig2 p 100 dig2 add p 2
402mn = dig2 add p 5
403d = dig2 add p 8
404hh = dig2 add p 11
405mi = dig2 add p 14
406if or lt mn 1 gt mn 12
407ret 0
408end
409if or lt d 1 gt d 31
410ret 0
411end
412if or gt hh 23 gt mi 59
413ret 0
414end
415if lt y 1
416ret 0
417end
418dd = days y mn d
419t1 = mul hh 60
420t2 = add mul dd 1440 t1
421ret add t2 add mi 1
422end
423
424note Sender check: a slug of a-z 0-9 '-', 1..64 bytes, that is drake or has DIR/inbox/<slug>.md.
425fn okname s ulong n ulong to bool
426if or eq n 0 gt n 64
427ret false
428end
429for i ulong n bound BIG
430if eq okch get fb add s i false
431ret false
432end
433end
434if eq n len tdrake
435d = true
436for j ulong n bound BIG
437if ne get fb add s j get tdrake j
438d = false
439end
440end
441if d
442ret true
443end
444end
445pdir
446for kk ulong n bound BIG
447pbyte get fb add s kk
448end
449ptext tmd
450a = faccess pb 0 get pn 0 0
451if eq a 0
452ret true
453end
454ret false
455end
456
457fn putdec v ulong to ulong
458n = ulong 1
459x = v
460while ge x 10 bound BIG
461x = div x 10
462n = add n 1
463end
464x = v
465for i ulong n bound BIG
466put cb sub sub n 1 i add uchar rem x 10 48
467x = div x 10
468end
469ret n
470end
471
472fn wr fd int n ulong to void
473i = ulong 0
474while lt i n bound BIG
475r = write fd outb i sub n i
476if le r 0
477exit 75
478end
479i = add i ulong r
480end
481end
482
483fn rejout to void
484o = ulong 0
485for i ulong len trej
486put outb i get trej i
487end
488o = len trej
489for j ulong get bn 0 bound BIG
490put outb add o j get bot j
491end
492o = add o get bn 0
493put outb o 32
494o = add o 1
495n = putdec get rej 0
496for kk ulong n bound BIG
497put outb add o kk get cb kk
498end
499o = add o n
500put outb o 10
501wr 2 add o 1
502end
503
504fn botout to ulong
505n = get bn 0
506for i ulong n bound BIG
507put outb i get bot i
508end
509put outb n 32
510ret add n 1
511end
512
513note Wake line "<bot> <1|2> <token>"; a 0 (nothing to do) prints nothing. Returns 1 when printed.
514fn emit t trit to ulong
515if eq open t 0
516ret 0
517end
518o = botout
519if eq get inspecting 0 1
520put outb o 49
521else
522put outb o add uchar open t 48
523end
524put outb add o 1 32
525o = add o 2
526put outb o 110
527if eq get tl 0 2
528put outb o 101
529end
530o = add o 1
531m = putdec get tl 1
532for j ulong m bound BIG
533put outb add o j get cb j
534end
535o = add o m
536put outb o 58
537o = add o 1
538for kk ulong 64
539put outb add o kk get hx kk
540end
541o = add o 64
542put outb o 10
543wr 1 add o 1
544ret 1
545end
546
547fn receipt to void
548o = botout
549put outb o 48
550put outb add o 1 10
551wr 1 add o 2
552end
553
554note Token for the lane in fb: the end of its last complete line and the sha256 of all bytes before it.
555fn mktok lane ulong to void
556q = lastnl
557put tl 0 add lane 1
558put tl 1 add q 1
559hfb 0 add q 1
560end
561
562note Atomic cursor write: "<off> <hx>" into <cursor>.tmp, fsync, then renameat2 over the cursor.
563fn wrcur lane ulong off ulong to void
564n = putdec off
565put cb n 32
566for i ulong 64
567put cb add add n 1 i get hx i
568end
569put cb add n 65 10
570pbase lane
571ptext tcur
572ptext ttmp
573fd = fopen pb 0 get pn 0 577 420
574if lt fd 0
575exit 76
576end
577w2 = ulong 0
578m = add n 66
579while lt w2 m bound BIG
580r = write int fd cb w2 sub m w2
581if le r 0
582exit 77
583end
584w2 = add w2 ulong r
585end
586s1 = fsync int fd
587e2 = fclose int fd
588if ne s1 0
589exit 77
590end
591put pk 0 2
592put pk 1 0
593put pkn 0 2
594pkrec
595pbase lane
596ptext tcur
597pkrec
598rn = renames pk 0 get pkn 0 0
599if ne rn 0
600exit 77
601end
602pbase lane
603ptext tcur
604syncdir
605end
606
607note Parse TOKEN = L<offset>:<64 hex>; return the lane, the offset in tl 1, the hex at tk 2+digits.
608fn parsetok tn ulong to ulong
609lane = ulong 0
610c0 = get tk 0
611if eq c0 101
612lane = 1
613else
614if ne c0 110
615exit 3
616end
617end
618off = ulong 0
619i = ulong 1
620while lt i tn bound BIG
621c = get tk i
622if eq c 58
623break
624end
625if eq isdig c false
626exit 3
627end
628if gt i 18
629exit 3
630end
631off = add mul off 10 ulong sub c 48
632i = add i 1
633end
634if or eq i 1 ne tn add i 65
635exit 3
636end
637for j ulong 64
638if eq ishex get tk add add i 1 j false
639exit 3
640end
641end
642put tl 0 add i 1
643put tl 1 off
644ret lane
645end
646
647fn getbot a ulong to void
648p = argread a bot 0 len bot
649if lt p 1
650exit 2
651end
652if gt p 64
653exit 2
654end
655put bn 0 ulong p
656if eq get bot 0 45
657exit 2
658end
659for i ulong get bn 0 bound BIG
660if eq okch get bot i false
661exit 2
662end
663end
664end
665
666fn botarg t view uchar * to bool
667if ne get bn 0 len t
668ret false
669end
670for i ulong len t
671if ne get bot i get t i
672ret false
673end
674end
675ret true
676end
677
678note Line format: when the last '|' field is a codebook signature (ocb2: or a leftover ocb1:), it
679note is not a content field; the tag is field 3 in the 6-field "to" format, else field 2.
680fn lfmt p ulong e ulong to void
681nf = add npipe p e 1
682ee = e
683if ge nf 2
684lp = lastpipe p e
685if or startsw add lp 1 e tocb startsw add lp 1 e tocbo
686ee = lp
687nf = sub nf 1
688end
689end
690put lf 0 ee
691put lf 1 2
692if eq nf 6
693put lf 1 3
694end
695end
696
697note verified: the codebook's own verify (codebook.om verl, quiet, table mode) on fb[p..e).
698note Without DIR/codebook/codebook.tsv and DIR/codebook/key nothing verifies.
699fn verified p ulong e ulong to bool
700put vm 0 0
701put vf 0 0
702put ve 0 0
703put vc 0 0
704if eq get vk 0 1
705verx p e
706end
707ret eq get vf 0 1
708end
709
710note Level of a line whose verify already ran: 2 only for a verified line carrying a 3xx code;
711note STOP text, a stop tag and every unsigned, tampered, stale or replayed line give at most 1.
712fn lvl p ulong e ulong to trit
713if and eq get vf 0 1 eq get ve 0 1
714ret tritmk 2
715end
716if eq lnkind p e 0
717ret tritmk 0
718end
719ret tritmk 1
720end
721
722fn lnlevel p ulong e ulong to trit
723v = verified p e
724ret lvl p e
725end
726
727note Quota entries are (sender off, len, minute, counter) and only MAC-verified lines are counted,
728note once per (sender, counter): forged lines and replayed copies never use up a sender's quota.
729fn cntwin s ulong n ulong m ulong to ulong
730c = ulong 0
731for i ulong get an 0 bound BIG
732b = mul i 4
733a0 = get acc b
734a1 = get acc add b 1
735if sameid a0 a1 s n
736a2 = get acc add b 2
737if lt absd a2 m WIN
738c = add c 1
739end
740end
741end
742ret c
743end
744
745fn seenc s ulong n ulong v ulong to bool
746for i ulong get an 0 bound BIG
747b = mul i 4
748if eq get acc add b 3 v
749if sameid get acc b get acc add b 1 s n
750ret true
751end
752end
753end
754ret false
755end
756
757note Admission for the emergency lane, after verified ran on the line.
758fn admit p ulong e ulong to bool
759if eq splitf p e 1 124 false
760ret false
761end
762s = get fs 0
763n = sub get fs 1 s
764if eq okname s n false
765ret false
766end
767m = minute p e
768if eq m 0
769ret false
770end
771if eq get vm 0 0
772ret true
773end
774v = get vc 0
775if seenc s n v
776ret true
777end
778if ge cntwin s n m NMAX
779ret false
780end
781b2 = mul get an 0 4
782if lt len acc add b2 4
783e1 = enlarge acc add mul b2 2 64
784if ne e1 0
785exit 74
786end
787end
788put acc b2 s
789put acc add b2 1 n
790put acc add b2 2 m
791put acc add b2 3 v
792put an 0 add get an 0 1
793ret true
794end
795
796fn scane st ulong to trit
797put an 0 0
798t = tritmk 0
799p = ulong 0
800while lt p get fbn 0 bound BIG
801e = lnend p
802if eq e get fbn 0
803break
804end
805if ists p e
806v = verified p e
807ok = admit p e
808if ge p st
809if ok
810t = tmax t lvl p e
811else
812put rej 0 add get rej 0 1
813end
814end
815end
816p = add e 1
817end
818ret t
819end
820
821note Counter state for this bot: DIR/inbox/<bot>.<sender>.ctr, lock DIR/inbox/<bot>.ctr.lock.
822fn setsd to void
823pdir
824for i ulong get bn 0 bound BIG
825pbyte get bot i
826put rbot i get bot i
827end
828put rbn 0 get bn 0
829for j ulong get pn 0 bound BIG
830put sd j get pb j
831end
832put sdn 0 get pn 0
833put vtc 0 0
834put vtn 0 0
835end
836
837note A verified 3xx (2) in the emergency lane is reported first; an emergency-lane 1 is reported
838note only when the normal lane is quiet, so unsigned lines there cannot hide normal work.
839fn state to trit
840put rej 0 0
841put tl 0 0
842setsd
843pbase 1
844te = tritmk 0
845if loadf
846te = scane rdcur 1
847end
848if gt get rej 0 0
849rejout
850end
851if eq open te 2
852mktok 1
853ret te
854end
855pbase 0
856tn = tritmk 0
857if loadf
858tn = scann rdcur 0
859end
860if gt open tn 0
861mktok 0
862ret tn
863end
864if gt open te 0
865pbase 1
866if loadf
867mktok 1
868end
869end
870ret te
871end
872
873note --ack: under the state lock, verify-and-advance (vt = 0) every line the token covers, then
874note move the cursor. A crash between the two leaves those lines unread but stale (at most 1).
875fn doack tn ulong to void
876lane = parsetok tn
877hp = get tl 0
878off = get tl 1
879pbase lane
880if eq loadf false
881exit 3
882end
883cur = rdcur lane
884if or eq off 0 gt off get fbn 0
885exit 3
886end
887if lt off cur
888exit 3
889end
890if ne get fb sub off 1 10
891exit 3
892end
893hfb 0 off
894for j ulong 64
895if ne get hx j get tk add hp j
896exit 3
897end
898end
899if eq get vk 0 1
900ackv cur off
901hfb 0 off
902end
903wrcur lane off
904end
905
906fn ackv cur ulong off ulong to void
907setsd
908lk = lockst
909put vt 0 0
910p = cur
911while lt p off bound BIG
912e = lnend p
913if ists p e
914verx p e
915end
916p = add e 1
917end
918put vt 0 1
919unlockst lk
920end
921
922note pb = DIR/codebook/<name>.
923fn pcb t view uchar * to void
924put pn 0 0
925for i ulong get dn 0 bound BIG
926pbyte get dir i
927end
928if gt get dn 0 0
929pbyte 47
930end
931ptext tcbd
932ptext t
933end
934
935fn ldvk to void
936pcb tcbf
937a = faccess pb 0 get pn 0 0
938pcb tkey
939b = faccess pb 0 get pn 0 0
940if or ne a 0 ne b 0
941ret
942end
943pcb tcbf
944ldcbq
945pcb tkey
946ldkeyq
947put vk 0 1
948end
949
950fn main to int
951put vq 0 1
952put vt 0 1
953put ssep 0 46
954c = argcnt
955if lt c 3
956ret 2
957end
958p = argread 1 dir 0 len dir
959if lt p 0
960ret 2
961end
962put dn 0 ulong p
963q = argread 2 bot 0 len bot
964if lt q 0
965ret 2
966end
967put bn 0 ulong q
968ldvk
969consume = botarg tconsume
970if or botarg tack consume
971if ne c 5
972ret 2
973end
974getbot 3
975tn = argread 4 tk 0 len tk
976if lt tn 1
977exit 3
978end
979doack ulong tn
980if consume
981zap
982ret 0
983end
984receipt
985z = emit state
986zap
987ret 0
988end
989any = ulong 0
990first = ulong 2
991if botarg tinspect
992if lt c 4
993zap
994ret 2
995end
996put inspecting 0 1
997first = ulong 3
998end
999for a ulong sub ulong c first bound BIG
1000getbot add a first
1001any = add any emit state
1002end
1003zap
1004if eq any 0
1005ret 1
1006end
1007ret 0
1008end