interplane.Get Omega
Read in your language

sovereign-intelligence

Front door to Drake Stapleton's sovereign stack.

Brought by drakestapleton

branch main Branches Tags History

sovereign-intelligence / compiler / si.om

1164 lines · 31233 bytes Raw Blame History
1om2
2note compiler/si.om: the sovereign-intelligence front-door generator, in pure seeded Omega.
3note
4note Build with the pinned seeded compiler (compiler/README.md names the pin):
5note omc < compiler/si.om > si && chmod +x si
6note Run:
7note si INVENTORY ORDER STATE README REPOS SISH
8note INVENTORY read, typed by a person: one project per line, name TAB rid TAB description:
9note which projects the front door lists and how each is described. A line with
10note more than three fields (a typed head, branch or visibility) is refused
11note ORDER read, typed: "core TAB name" lines give the core projects in order,
12note "deny TAB name" lines name projects that are never listed,
13note one optional "lead TAB name" line names the project the README leads with
14note STATE read, generated from Radicle by tools/si-state.sh, never typed: one T line,
15note the time it was read, then per RID "D rid branch" (the default branch its
16note signed identity names), "V rid visibility", "H rid branch head" (the
17note canonical refs/heads/branch) and "P rid head file" (a landing receipt whose
18note first line names that head)
19note README read and rewritten in place between the si:lead markers, the si:agent
20note markers and the si:index markers
21note REPOS written: the public manifest, one project per line, as TSV
22note SISH written: the POSIX sh bootstrap served at https://www.aienos.com/si
23note Every inventory project that ORDER does not deny is listed, with the branch, head and
24note receipt count STATE gives for its RID. A receipt counts only when its file is named
25note NAME-HEAD12.txt (the project's name and the first 12 digits of that head), the landing
26note receipts' convention. Core projects come first, in ORDER's order; the rest follow in
27note inventory order. A listed project that STATE does not show as public with one default
28note branch and one head of it, a missing or denied core project, a duplicate name or RID, a
29note typed head, branch or visibility, or a malformed field or STATE line makes si refuse
30note with exit 1 and write nothing; the refusal names the row. The output depends only on
31note the input bytes: no clock (the time is STATE's T line), no environment.
32
33note input bytes (all four inputs, one after another) and output bytes
34global src uchar 4194304
35global srcn ulong 1
36global ob uchar 4194304
37global obn ulong 1
38note the path named by the current argument
39global pb uchar 4096
40global pbn ulong 1
41note project records, 16 cells each: offset and length of name, rid, visibility, head,
42note description (cells 0..9), kind (cell 10: 0 rest, 1 core, 2 never listed), core rank (11),
43note offset and length of the default branch (12, 13) and the head's receipts (14); 15 is unused.
44note Visibility, head, branch and receipts come from STATE, never from the inventory
45global rec ulong 16384
46global nrec ulong 1
47note core and deny names from ORDER: offset, length pairs
48global cor ulong 128
49global ncor ulong 1
50global den ulong 128
51global nden ulong 1
52note the lead project's name from ORDER: offset, length; nled is 0 or 1
53global led ulong 2
54global nled ulong 1
55note STATE lines, 8 cells each: the kind byte, offset and length of up to three fields, and
56note the field count (cell 7)
57global stl ulong 65536
58global nstl ulong 1
59note the T line's time, offset and length; ntim counts T lines
60global tim ulong 2
61global ntim ulong 1
62note how many lines the last stfind matched (nhit)
63global nhit ulong 1
64note emission order: record numbers
65global seq ulong 1024
66global nseq ulong 1
67
68text tsi "si: "
69text tnl "\n"
70text tusage "usage: si INVENTORY ORDER STATE README REPOS SISH\n"
71text toverf "output is over the 4 MiB buffer"
72text targ "cannot read a command-line argument"
73text topen "cannot open "
74text tread "cannot read "
75text tbig "input over the 4 MiB buffer: "
76text twrite "cannot write "
77text tmany "more than 1024 projects at: "
78text tcols "inventory line needs 3 tab-separated fields (name, rid, description): "
79text tbname "bad project name (letters, digits, . _ - only): "
80text tbrid "bad RID (rad:z then letters and digits): "
81text tbdesc "description has control bytes or is over 512 bytes: "
82text tbord "order line must be core, deny or lead TAB name: "
83text tmore "more than 64 core or deny names, or a second lead, at: "
84text tdupn "duplicate project name: "
85text tdupr "duplicate RID: "
86text tcmiss "core project is not in the inventory: "
87text ttyped "inventory carries a typed head, branch or visibility; those come from Radicle (tools/si-state.sh): "
88text tcpriv "core project is denied or listed twice: "
89text tmst "more than 8192 Radicle state lines at: "
90text tbst "Radicle state line is not T, D, V, H or P with its well-formed fields: "
91text tnot "Radicle state needs exactly one T line, the time it was read"
92text tnod "no default branch from a Radicle identity for: "
93text tnov "no Radicle visibility for: "
94text tdupd "two Radicle default branches or visibilities for: "
95text tpriv "listed project is not public on Radicle: "
96text tnoh "no canonical head on Radicle of the default branch of: "
97text tduph "two Radicle heads of the default branch of: "
98text ttxt ".txt"
99text tnomark "README lacks <!-- si:lead:begin -->, <!-- si:lead:end -->, <!-- si:agent:begin -->, <!-- si:agent:end -->, <!-- si:index:begin --> and <!-- si:index:end -->, in that order"
100text tdot "."
101text tdotdot ".."
102text tradz "rad:z"
103text tpublic "public"
104text tprivat "private"
105text ttab "\t"
106text tcore "core"
107text tdeny "deny"
108text tlead "lead"
109text tbar " | "
110text tbare " |"
111text trest "rest"
112text tbeg "<!-- si:index:begin -->"
113text tend "<!-- si:index:end -->"
114text tlbeg "<!-- si:lead:begin -->"
115text tlend "<!-- si:lead:end -->"
116text ld0 "`"
117text ld1 "` is being published to Radicle. Its Radicle ID appears here as soon as it is.\n"
118text ld2 "```sh\nrad clone --timeout 5min "
119text ld3 " # "
120text ld4 "\n```\n\nOn the web: [radicle.network/nodes/rosa.radicle.network/"
121text ld5 "](https://radicle.network/nodes/rosa.radicle.network/"
122text ld6 ")\n"
123text tabeg "<!-- si:agent:begin -->"
124text taend "<!-- si:agent:end -->"
125text ag1 "<https://radicle.network/nodes/rosa.radicle.network/"
126text ag2 "/tree/AGENT-PROMPT.md>\n"
127text ag3 "`'s agent prompt link appears here as soon as it is on Radicle.\n"
128text tsec "## "
129text ix1 "**"
130text ix2 " public projects**, "
131text ix3 " of them core. `compiler/si.om` lists them from Radicle as of "
132text ix3b ". For each project, *Branch* is the default branch its signed Radicle identity names and *Head* is that branch's commit on my node at that time; nobody types either. *Receipts* counts the landing receipts recorded for exactly that commit: each says the project's own checks passed on a fresh copy before it landed, and 0 means none is recorded. A head is the latest commit, not a review. Each project's home is its own Radicle repository; this table only links to them. Clone one peer-to-peer with `rad clone`, or read-only with `git clone` from its mirror on my own seed. A plain `git clone` of the mirror gets the project's Radicle default branch. Every other branch and tag, including those carried over from GitHub, is served at the mirror URL with my node ID appended. That URL has no default branch, so give `--branch` a short branch or tag name, not `refs/heads/...`:\n\n```sh\ngit clone --branch <branch-or-tag> https://www.aienos.com/rad/<RID>.git/z6MkfZDue1TbaNFi3ZzcD9uiTeduPLC52ucdFVuLKZwexxhK/ <dir>\n```\n\nFull commit ids are in [`repos.tsv`](repos.tsv).\n\n| # | Project | Radicle ID | Get it | Branch | Head | Receipts | About |\n|---:|---|---|---|---|---|---:|---|\n"
133text ix4 "| "
134text ix5 " | **"
135text ix6 "**"
136text ix7 " (core)"
137text ix8 " | `"
138text ix9 "` | `rad clone --timeout 5min "
139text ixa "`, [git mirror](https://www.aienos.com/rad/"
140text ixb ".git) | `"
141text ixc "` | `"
142text ixd " | "
143text ix10 "` | "
144text ix11 " |\n"
145text rphdr "name\trid\thead\ttier\tdescription\n"
146text shr1 "si_repo '"
147text shr2 "' '"
148text shr3 "'\n"
149
150note the bootstrap script, paragraph by paragraph; shd3 ends where the project lines go
151text sha0 "#!/bin/sh\n# sovereign-intelligence bootstrap: fetch every public project of my sovereign AI stack.\n# Generated by compiler/si.om (Omega) from Radicle state read at "
152text sha0b ": each project's\n# commit is the head of the default branch its signed Radicle identity names, at that time:\n# the latest commit, not a review. Do not edit by hand.\n# Served at https://www.aienos.com/si\n# Front door: https://interplane.io/drakestapleton/sovereign-intelligence\n#\n# curl -fsSL https://www.aienos.com/si | sh\n#\n# Settings (environment):\n# SI_DIR=path the one directory it creates (default ./sovereign-intelligence)\n# SI_ONLY=core fetch only the core projects (default all)\n# SI_HTTP=url first git-over-HTTPS seed (default https://www.aienos.com/rad, my own)\n# SI_HTTP2=url second git-over-HTTPS seed (default https://rosa.radicle.network, public)\n# SI_INSTALL_RAD=1 run the official Radicle installer when a project cannot be fetched\n# SI_RAD_TIMEOUT=t per-project rad clone timeout (default 5min)\n# No sudo. It refuses to run as root. It touches nothing outside SI_DIR,\n# except that SI_INSTALL_RAD=1 runs Radicle's own installer as you.\n# Exit status: 0 all OK, 2 a checkout does not match the manifest, 3 a project could not be fetched.\nset -eu\numask 022\n\n"
153text sha1 "SI_DIR=\"${SI_DIR:-./sovereign-intelligence}\"\nSI_ONLY=\"${SI_ONLY:-all}\"\nSI_HTTP=\"${SI_HTTP:-https://www.aienos.com/rad}\"\nSI_HTTP2=\"${SI_HTTP2:-https://rosa.radicle.network}\"\nSI_INSTALL_RAD=\"${SI_INSTALL_RAD:-0}\"\nSI_RAD_TIMEOUT=\"${SI_RAD_TIMEOUT:-5min}\"\n\n"
154text sha2 "si_say() { printf '%s\\n' \"$*\"; }\nsi_die() { printf 'si: %s\\n' \"$*\" >&2; exit 1; }\n\n"
155text sha3 "if [ \"$(id -u)\" = \"0\" ]; then si_die \"refusing to run as root; run it as your own user\"; fi\ncase \"$SI_ONLY\" in all|core) ;; *) si_die \"SI_ONLY must be all or core\" ;; esac\ncommand -v git >/dev/null 2>&1 || si_die \"git is required: https://git-scm.com\"\nsi_rad=0\nif command -v rad >/dev/null 2>&1; then si_rad=1; fi\n\n"
156text sha4 "si_say \"Sovereign Intelligence. GitHub censored me. I demand to be heard.\"\nsi_say \"\"\nsi_say \"This will:\"\nsi_say \" 1. make one directory, $SI_DIR, and put everything in it (no sudo, nothing else touched)\"\nif [ \"$SI_ONLY\" = \"core\" ]; then si_say \" 2. fetch the "
157text shb0 " core projects\"; else si_say \" 2. fetch all "
158text shc0 " public projects (SI_ONLY=core fetches only the "
159text shd0 " core ones)\"; fi\nif [ \"$si_rad\" = \"1\" ]; then\n si_say \" peer-to-peer with rad clone, falling back to git clone from $SI_HTTP, then $SI_HTTP2\"\nelse\n si_say \" with git clone from $SI_HTTP, then $SI_HTTP2 (rad not found; with Radicle it fetches peer-to-peer)\"\nfi\nsi_say \" 3. check out the commit the manifest names for each project and print OK or MISMATCH\"\nsi_say \"\"\n\n"
160text shd1 "mkdir -p -- \"$SI_DIR\"\ncd -- \"$SI_DIR\"\nsi_ok=0\nsi_bad=0\nsi_miss=0\n\n"
161text shd2 "si_check() {\n si_got=\"$(git -C \"$1\" rev-parse HEAD 2>/dev/null || true)\"\n if [ \"$si_got\" = \"$2\" ]; then\n si_say \"OK $1 $2\"\n si_ok=$((si_ok + 1))\n else\n si_say \"MISMATCH $1 manifest $2, checkout ${si_got:-none}\"\n si_bad=$((si_bad + 1))\n fi\n}\n\nsi_fresh() {\n si_tip=\"$(git -C \"$1\" rev-parse HEAD 2>/dev/null || true)\"\n if [ \"$si_tip\" != \"$2\" ] && git -C \"$1\" checkout --quiet --detach \"$2\" 2>/dev/null; then\n si_say \"$1: checked out the manifest commit; its branch has moved on to $si_tip\"\n fi\n si_check \"$1\" \"$2\"\n}\n\n"
162text shd3 "si_repo() {\n if [ \"$SI_ONLY\" = \"core\" ] && [ \"$4\" != \"core\" ]; then return 0; fi\n if [ -e \"$1\" ]; then\n si_say \"$1: already here, checking it\"\n si_check \"$1\" \"$3\"\n return 0\n fi\n if [ \"$si_rad\" = \"1\" ]; then\n si_say \"$1: rad clone $2\"\n if rad clone --timeout \"$SI_RAD_TIMEOUT\" \"$2\" \"$1\" </dev/null; then\n si_fresh \"$1\" \"$3\"\n return 0\n fi\n rm -rf -- \"./$1\"\n fi\n for si_src in \"$SI_HTTP\" \"$SI_HTTP2\"; do\n si_say \"$1: git clone $si_src/${2#rad:}.git\"\n if git clone --quiet -- \"$si_src/${2#rad:}.git\" \"$1\" </dev/null; then\n si_fresh \"$1\" \"$3\"\n return 0\n fi\n rm -rf -- \"./$1\"\n done\n si_say \"MISSING $1 $2 (could not fetch it; this Radicle ID finds it on any seed)\"\n si_miss=$((si_miss + 1))\n}\n\n"
163text she0 "\n"
164text she1 "si_say \"\"\nsi_say \"Done in $(pwd): $si_ok OK, $si_bad MISMATCH, $si_miss MISSING.\"\nif [ \"$si_miss\" -gt 0 ]; then\n if [ \"$si_rad\" = \"1\" ]; then\n si_say \"Is your Radicle node running? Start it with: rad node start\"\n si_say \"Then run this again; projects already here are only checked.\"\n else\n si_say \"\"\n si_say \"Radicle fetches the missing projects peer-to-peer: https://radicle.xyz\"\n si_say \"Its official installer, run as your own user:\"\n si_say \" curl -sSf https://radicle.xyz/install | sh\"\n if [ \"$SI_INSTALL_RAD\" = \"1\" ]; then\n si_say \"SI_INSTALL_RAD=1: running it now.\"\n curl -sSf https://radicle.xyz/install | sh\n si_say \"Next: rad auth, then rad node start, then run this again.\"\n else\n si_say \"This script does not run it unless you set SI_INSTALL_RAD=1.\"\n fi\n fi\nfi\n"
165text rlay "layers.tsv"
166text rom "compiler/rebuild.om"
167global rbo ulong 4
168text rb0 "if [ \"${SI_REBUILD:-0}\" = 1 ]; then\n mkdir -p .si-check\n cat > .si-check/rebuild.om <<'SI_OMEGA_REBUILD_SOURCE'\n"
169text rb1 "SI_OMEGA_REBUILD_SOURCE\n cat > .si-check/layers.tsv <<'SI_OMEGA_REBUILD_LAYERS'\n"
170text rb2 "SI_OMEGA_REBUILD_LAYERS\n cat > .si-check/repos.tsv <<'SI_OMEGA_REBUILD_REPOS'\n"
171text rb3 "SI_OMEGA_REBUILD_REPOS\n if omega-mv/seed/out/omc < .si-check/rebuild.om > .si-check/rebuild; then\n chmod +x .si-check/rebuild\n else\n si_die \"cannot build the rebuild checker with omega-mv/seed/out/omc\"\n fi\n exec .si-check/rebuild run .si-check/layers.tsv .si-check/repos.tsv \"$PWD\" \"$PWD/.si-check\" \"wrap=${SI_REBUILD_WRAP:-}\"\nfi\n"
172text rbfin "if [ \"$si_bad\" -gt 0 ]; then exit 2; fi\nif [ \"$si_miss\" -gt 0 ]; then exit 3; fi\nexit 0\n"
173
174fn rload t view uchar * slot ulong to void
175for i ulong len t
176put pb i get t i
177end
178put pbn 0 len t
179fd = fopen pb 0 len t 0 0
180if lt fd 0
181dief topen
182end
183s = get srcn 0
184p = s
185while lt p 4194304 bound 4194305
186k = read int fd src p sub 4194304 p
187if lt k 0
188dief tread
189end
190if eq k 0
191break
192end
193p = add p ulong k
194end
195c = fclose int fd
196if ge p 4194304
197dief tbig
198end
199put rbo slot s
200put rbo add slot 1 sub p s
201put srcn 0 p
202end
203
204note die: report m and up to 200 bytes of src from o on stderr, then exit 1
205fn die m view uchar * o ulong n ulong to void
206k = n
207if gt k 200
208k = 200
209end
210w = write 2 tsi 0 len tsi
211w = write 2 m 0 len m
212if gt k 0
213w = write 2 src o k
214end
215w = write 2 tnl 0 len tnl
216exit 1
217end
218
219note dief: report m and the current path on stderr, then exit 1
220fn dief m view uchar * to void
221w = write 2 tsi 0 len tsi
222w = write 2 m 0 len m
223w = write 2 pb 0 get pbn 0
224w = write 2 tnl 0 len tnl
225exit 1
226end
227
228note setpath: copy command-line argument ai into pb
229fn setpath ai ulong to void
230n = argread ai pb 0 4096
231if le n 0
232die targ 0 0
233end
234put pbn 0 ulong n
235end
236
237note load: append the file named by argument ai to src and return where it starts
238fn load ai ulong to ulong
239setpath ai
240fd = fopen pb 0 get pbn 0 0 0
241if lt fd 0
242dief topen
243end
244s = get srcn 0
245p = s
246while lt p 4194304 bound 4194305
247k = read int fd src p sub 4194304 p
248if lt k 0
249dief tread
250end
251if eq k 0
252break
253end
254p = add p ulong k
255end
256c = fclose int fd
257if ge p 4194304
258dief tbig
259end
260put srcn 0 p
261ret s
262end
263
264note wout: write ob to the file named by argument ai (created or truncated, mode 0644), then empty ob
265fn wout ai ulong to void
266setpath ai
267fd = fopen pb 0 get pbn 0 577 420
268if lt fd 0
269dief topen
270end
271n = get obn 0
272p = ulong 0
273while lt p n bound 4194305
274k = write int fd ob p sub n p
275if le k 0
276dief twrite
277end
278p = add p ulong k
279end
280c = fclose int fd
281if ne c 0
282dief twrite
283end
284put obn 0 0
285end
286
287note em: append a text to ob
288fn em t view uchar * to void
289p = get obn 0
290n = len t
291if gt n sub 4194304 p
292die toverf 0 0
293end
294for i ulong n
295put ob add p i get t i
296end
297put obn 0 add p n
298end
299
300note ems: append n bytes of src from o to ob
301fn ems o ulong n ulong to void
302p = get obn 0
303if gt n sub 4194304 p
304die toverf 0 0
305end
306for i ulong n
307put ob add p i get src add o i
308end
309put obn 0 add p n
310end
311
312note emb: append one byte to ob
313fn emb c uchar to void
314p = get obn 0
315if ge p 4194304
316die toverf 0 0
317end
318put ob p c
319put obn 0 add p 1
320end
321
322note emu: append v in decimal to ob
323fn emu v ulong to void
324d = new uchar 20 0
325x = v
326put d 0 uchar add rem x 10 48
327k = ulong 1
328x = div x 10
329while gt x 0 bound 20
330put d k uchar add rem x 10 48
331k = add k 1
332x = div x 10
333end
334while gt k 0 bound 20
335k = sub k 1
336emb get d k
337end
338end
339
340note eqt: do the n bytes of src at o equal the text t
341fn eqt o ulong n ulong t view uchar * to bool
342if ne n len t
343ret false
344end
345for i ulong n
346if ne get src add o i get t i
347ret false
348end
349end
350ret true
351end
352
353note eqs: are two slices of src equal
354fn eqs a ulong an ulong b ulong bn ulong to bool
355if ne an bn
356ret false
357end
358for i ulong an
359if ne get src add a i get src add b i
360ret false
361end
362end
363ret true
364end
365
366note find: first place in src from s, before e, where t starts; e when there is none
367fn find s ulong e ulong t view uchar * to ulong
368n = len t
369if lt sub e s n
370ret e
371end
372lp = sub e n
373p = s
374while le p lp bound 4194305
375if eqt p n t
376ret p
377end
378p = add p 1
379end
380ret e
381end
382
383note eol: the end of the line that starts at p (its newline, or e)
384fn eol p ulong e ulong to ulong
385q = p
386while lt q e bound 4194305
387if eq get src q 10
388ret q
389end
390q = add q 1
391end
392ret e
393end
394
395fn isaln c uchar to bool
396ret or or and ge c 48 le c 57 and ge c 65 le c 90 and ge c 97 le c 122
397end
398
399note okname: 1..64 bytes of letters, digits, dot, dash, underscore; not ".", "..", or a leading dash
400fn okname o ulong n ulong to bool
401if or eq n 0 gt n 64
402ret false
403end
404if eq get src o 45
405ret false
406end
407for i ulong n
408c = get src add o i
409if not or isaln c or eq c 46 or eq c 45 eq c 95
410ret false
411end
412end
413if eqt o n tdot
414ret false
415end
416ret not eqt o n tdotdot
417end
418
419note okrid: "rad:z" then letters and digits, 6..64 bytes in all
420fn okrid o ulong n ulong to bool
421if or lt n 6 gt n 64
422ret false
423end
424if not eqt o 5 tradz
425ret false
426end
427for i ulong sub n 5
428if not isaln get src add add o 5 i
429ret false
430end
431end
432ret true
433end
434
435note okhex: exactly 40 lowercase hex digits
436fn okhex o ulong n ulong to bool
437if ne n 40
438ret false
439end
440for i ulong n
441c = get src add o i
442if not or and ge c 48 le c 57 and ge c 97 le c 102
443ret false
444end
445end
446ret true
447end
448
449note okdesc: at most 512 bytes, none of them a control byte
450fn okdesc o ulong n ulong to bool
451if gt n 512
452ret false
453end
454for i ulong n
455c = get src add o i
456if or lt c 32 eq c 127
457ret false
458end
459end
460ret true
461end
462
463note fldo, fldn: offset and length of field k of record r
464fn fldo r ulong k ulong to ulong
465ret get rec add mul r 16 mul k 2
466end
467
468fn fldn r ulong k ulong to ulong
469ret get rec add add mul r 16 mul k 2 1
470end
471
472fn kindof r ulong to ulong
473ret get rec add mul r 16 10
474end
475
476note inventory_row: split one inventory line [p, q) into its three fields and check them; a
477note fourth field is a typed head, branch or visibility, which only STATE may give
478fn inventory_row p ulong q ulong to void
479r = get nrec 0
480if ge r 1024
481die tmany p sub q p
482end
483b = mul r 16
484k = ulong 0
485st = p
486i = p
487while le i q bound 4194305
488c = uchar 10
489if lt i q
490c = get src i
491end
492if or eq c 9 eq c 10
493if ge k 3
494die ttyped p sub q p
495end
496if eq k 2
497put rec add b 8 st
498put rec add b 9 sub i st
499else
500put rec add b mul k 2 st
501put rec add add b mul k 2 1 sub i st
502end
503k = add k 1
504st = add i 1
505end
506i = add i 1
507end
508if ne k 3
509die tcols p sub q p
510end
511if not okname get rec b get rec add b 1
512die tbname p sub q p
513end
514if not okrid get rec add b 2 get rec add b 3
515die tbrid p sub q p
516end
517if not okdesc get rec add b 8 get rec add b 9
518die tbdesc p sub q p
519end
520for j ulong 4
521put rec add add b 4 j 0
522end
523for m ulong 6
524put rec add add b 10 m 0
525end
526put nrec 0 add r 1
527end
528
529note parse_inventory: every line of [s, e) that is neither empty nor a # comment is a project
530fn parse_inventory s ulong e ulong to void
531p = s
532while lt p e bound 4194305
533q = eol p e
534if lt p q
535if ne get src p 35
536inventory_row p q
537end
538end
539p = add q 1
540end
541end
542
543note order_row: one ORDER line [p, q), "core TAB name" or "deny TAB name"
544fn order_row p ulong q ulong to void
545t = find p q ttab
546if eq t q
547die tbord p sub q p
548end
549o = add t 1
550n = sub q o
551if not okname o n
552die tbord p sub q p
553end
554if eqt p sub t p tcore
555k = get ncor 0
556if ge k 64
557die tmore p sub q p
558end
559put cor mul k 2 o
560put cor add mul k 2 1 n
561put ncor 0 add k 1
562ret
563end
564if eqt p sub t p tdeny
565k = get nden 0
566if ge k 64
567die tmore p sub q p
568end
569put den mul k 2 o
570put den add mul k 2 1 n
571put nden 0 add k 1
572ret
573end
574if eqt p sub t p tlead
575if ne get nled 0 0
576die tmore p sub q p
577end
578put led 0 o
579put led 1 n
580put nled 0 1
581ret
582end
583die tbord p sub q p
584end
585
586fn parse_order s ulong e ulong to void
587p = s
588while lt p e bound 4194305
589q = eol p e
590if lt p q
591if ne get src p 35
592order_row p q
593end
594end
595p = add q 1
596end
597end
598
599note okbranch: 1..255 bytes of letters, digits, dot, dash, underscore and slash, not starting
600note with a dash or a slash
601fn okbranch o ulong n ulong to bool
602if or eq n 0 gt n 255
603ret false
604end
605c = get src o
606if or eq c 45 eq c 47
607ret false
608end
609for i ulong n
610c = get src add o i
611if not or isaln c or eq c 46 or eq c 45 or eq c 95 eq c 47
612ret false
613end
614end
615ret true
616end
617
618note st_ok: has STATE line k its kind's fields, each well formed; a T line is also counted
619fn st_ok k ulong to bool
620b = mul k 8
621kd = get stl b
622nf = get stl add b 7
623o1 = get stl add b 1
624n1 = get stl add b 2
625o2 = get stl add b 3
626n2 = get stl add b 4
627o3 = get stl add b 5
628n3 = get stl add b 6
629if eq kd 84
630put tim 0 o1
631put tim 1 n1
632put ntim 0 add get ntim 0 1
633ret and eq nf 1 and gt n1 0 and le n1 64 okdesc o1 n1
634end
635if not and ge nf 2 okrid o1 n1
636ret false
637end
638if eq kd 68
639ret and eq nf 2 okbranch o2 n2
640end
641if eq kd 86
642ret and eq nf 2 okname o2 n2
643end
644if eq kd 72
645ret and eq nf 3 and okbranch o2 n2 okhex o3 n3
646end
647if eq kd 80
648ret and eq nf 3 and okhex o2 n2 okname o3 n3
649end
650ret false
651end
652
653note st_row: one STATE line [p, q): a kind byte, a TAB, then one to three TAB-separated fields
654fn st_row p ulong q ulong to void
655if ge get nstl 0 8192
656die tmst p sub q p
657end
658if or lt sub q p 3 ne get src add p 1 9
659die tbst p sub q p
660end
661b = mul get nstl 0 8
662put stl b ulong get src p
663nf = ulong 0
664st = add p 2
665i = st
666while le i q bound 4194305
667c = uchar 10
668if lt i q
669c = get src i
670end
671if or eq c 9 eq c 10
672if ge nf 3
673die tbst p sub q p
674end
675x = add b add 1 mul nf 2
676put stl x st
677put stl add x 1 sub i st
678nf = add nf 1
679st = add i 1
680end
681i = add i 1
682end
683put stl add b 7 nf
684k = get nstl 0
685put nstl 0 add k 1
686if not st_ok k
687die tbst p sub q p
688end
689end
690
691note parse_state: every line of [s, e) that is not empty is a STATE line; exactly one is T
692fn parse_state s ulong e ulong to void
693p = s
694while lt p e bound 4194305
695q = eol p e
696if lt p q
697st_row p q
698end
699p = add q 1
700end
701if ne get ntim 0 1
702die tnot 0 0
703end
704end
705
706note stfind: the last STATE line of kind kd whose first field is record r's RID and, when bn is
707note not 0, whose second field is [bo, bo+bn); nhit counts the matches (none: the result is nstl)
708fn stfind kd ulong r ulong bo ulong bn ulong to ulong
709ns = get nstl 0
710h = ns
711c = ulong 0
712for j ulong ns
713if stm j kd r bo bn
714h = j
715c = add c 1
716end
717end
718put nhit 0 c
719ret h
720end
721
722note stm: is STATE line j of kind kd, with record r's RID as its first field and, when bn is not
723note 0, [bo, bo+bn) as its second
724fn stm j ulong kd ulong r ulong bo ulong bn ulong to bool
725b = mul j 8
726if ne get stl b kd
727ret false
728end
729if not eqs fldo r 1 fldn r 1 get stl add b 1 get stl add b 2
730ret false
731end
732if eq bn 0
733ret true
734end
735ret eqs bo bn get stl add b 3 get stl add b 4
736end
737
738note rcpname: is [fo, fo+fl) the receipt file name NAME-HEAD12.txt for the name [no, no+nn)
739note and the head at ho
740fn rcpname fo ulong fl ulong no ulong nn ulong ho ulong to bool
741if ne fl add nn 17
742ret false
743end
744if not eqs fo nn no nn
745ret false
746end
747if ne get src add fo nn 45
748ret false
749end
750if not eqs add add fo nn 1 12 ho 12
751ret false
752end
753ret eqt add add fo nn 13 4 ttxt
754end
755
756note receipts: how many P lines name record r's RID and head with a file of the receipts'
757note naming convention
758fn receipts r ulong to ulong
759ns = get nstl 0
760c = ulong 0
761for j ulong ns
762if rcpm j r
763c = add c 1
764end
765end
766ret c
767end
768
769note rcpm: is STATE line j a P line of record r's RID and head, with a conventional file name
770fn rcpm j ulong r ulong to bool
771b = mul j 8
772if ne get stl b 80
773ret false
774end
775if not eqs fldo r 1 fldn r 1 get stl add b 1 get stl add b 2
776ret false
777end
778if not eqs fldo r 3 40 get stl add b 3 get stl add b 4
779ret false
780end
781ret rcpname get stl add b 5 get stl add b 6 fldo r 0 fldn r 0 fldo r 3
782end
783
784note resolve1: record r takes its default branch, visibility, head and receipt count from STATE;
785note a project Radicle does not show as public, with one default branch and one head of it, is
786note refused by name (name and RID)
787fn resolve1 r ulong to void
788o = fldo r 0
789n = sub add fldo r 1 fldn r 1 o
790b = mul r 16
791d = stfind 68 r 0 0
792one o n tnod tdupd
793put rec add b 12 stf d 3
794put rec add b 13 stf d 4
795v = stfind 86 r 0 0
796one o n tnov tdupd
797if not eqt stf v 3 stf v 4 tpublic
798die tpriv o n
799end
800put rec add b 4 stf v 3
801put rec add b 5 stf v 4
802h = stfind 72 r fldo r 6 fldn r 6
803one o n tnoh tduph
804put rec add b 6 stf h 5
805put rec add b 7 stf h 6
806put rec add b 14 receipts r
807end
808
809note stf: cell c of STATE line j
810fn stf j ulong c ulong to ulong
811ret get stl add mul j 8 c
812end
813
814note one: the last stfind must have matched exactly one line; none dies with m0, more with m2,
815note naming [o, o+n)
816fn one o ulong n ulong m0 view uchar * m2 view uchar * to void
817if eq get nhit 0 0
818die m0 o n
819end
820if gt get nhit 0 1
821die m2 o n
822end
823end
824
825note resolve: every project ORDER does not deny is resolved against STATE
826fn resolve to void
827nr = get nrec 0
828for r ulong nr
829if not denied r
830resolve1 r
831end
832end
833end
834
835note check_unique: no two projects share a name or a RID
836fn check_unique to void
837nr = get nrec 0
838for a ulong nr
839for b ulong a
840if samefld a b 0
841die tdupn fldo a 0 fldn a 0
842end
843if samefld a b 1
844die tdupr fldo a 1 fldn a 1
845end
846end
847end
848end
849
850note samefld: do records a and b agree on field k
851fn samefld a ulong b ulong k ulong to bool
852ao = fldo a k
853an = fldn a k
854bo = fldo b k
855bn = fldn b k
856ret eqs ao an bo bn
857end
858
859note denied: is record r named by a deny line
860fn denied r ulong to bool
861o = fldo r 0
862n = fldn r 0
863nd = get nden 0
864for j ulong nd
865dno = get den mul j 2
866dn = get den add mul j 2 1
867if eqs o n dno dn
868ret true
869end
870end
871ret false
872end
873
874note mark_hidden: denied projects get kind 2 and are never listed; resolve has already refused
875note every other project that is not public
876fn mark_hidden to void
877nr = get nrec 0
878for r ulong nr
879kd = ulong 0
880if denied r
881kd = 2
882end
883put rec add mul r 16 10 kd
884end
885end
886
887note order_core: each core name must be a listed public project; they lead, in ORDER's order
888fn order_core to void
889nr = get nrec 0
890nc = get ncor 0
891for j ulong nc
892co = get cor mul j 2
893cn = get cor add mul j 2 1
894hit = nr
895for r ulong nr
896if eqs fldo r 0 fldn r 0 co cn
897hit = r
898end
899end
900if eq hit nr
901die tcmiss co cn
902end
903if ne kindof hit 0
904die tcpriv co cn
905end
906put rec add mul hit 16 10 1
907put rec add mul hit 16 11 j
908put seq j hit
909end
910put nseq 0 nc
911end
912
913note order_rest: the other public projects follow, in inventory order
914fn order_rest to void
915nr = get nrec 0
916k = get nseq 0
917for r ulong nr
918if eq kindof r 0
919put seq k r
920k = add k 1
921end
922end
923put nseq 0 k
924end
925
926note emit_desc: a description for a Markdown table cell, with | escaped
927fn emit_desc r ulong to void
928o = fldo r 4
929n = fldn r 4
930for i ulong n
931c = get src add o i
932if eq c 124
933emb 92
934end
935emb c
936end
937end
938
939note gen_index: the README project table
940fn gen_index to void
941n = get nseq 0
942em ix1
943emu n
944em ix2
945emu get ncor 0
946em ix3
947ems get tim 0 get tim 1
948em ix3b
949for i ulong n
950r = get seq i
951em ix4
952emu add i 1
953em ix5
954ems fldo r 0 fldn r 0
955em ix6
956if eq kindof r 1
957em ix7
958end
959em ix8
960ems fldo r 1 fldn r 1
961em ix9
962ems fldo r 1 fldn r 1
963em ixa
964ems add fldo r 1 4 sub fldn r 1 4
965em ixb
966ems fldo r 6 fldn r 6
967em ixc
968ems fldo r 3 7
969em ix10
970emu get rec add mul r 16 14
971em ixd
972emit_desc r
973em ix11
974end
975end
976
977note findlead: the listed public project named [o, o+n), or nrec when there is none
978fn findlead o ulong n ulong to ulong
979nr = get nrec 0
980hit = nr
981for r ulong nr
982if lt kindof r 2
983if eqs fldo r 0 fldn r 0 o n
984hit = r
985end
986end
987end
988ret hit
989end
990
991note gen_lead: the clone line of the lead project, or a note that it is not on Radicle yet
992fn gen_lead to void
993if eq get nled 0 0
994ret
995end
996lo = get led 0
997ln = get led 1
998r = findlead lo ln
999if eq r get nrec 0
1000em ld0
1001ems lo ln
1002em ld1
1003ret
1004end
1005em ld2
1006ems fldo r 1 fldn r 1
1007em ld3
1008ems lo ln
1009em ld4
1010ems fldo r 1 fldn r 1
1011em ld5
1012ems fldo r 1 fldn r 1
1013em ld6
1014end
1015
1016note gen_agent: the link to the lead project's AGENT-PROMPT.md, or a note that it is not on Radicle yet
1017fn gen_agent to void
1018if eq get nled 0 0
1019ret
1020end
1021lo = get led 0
1022ln = get led 1
1023r = findlead lo ln
1024if eq r get nrec 0
1025em ld0
1026ems lo ln
1027em ag3
1028ret
1029end
1030em ag1
1031ems fldo r 1 fldn r 1
1032em ag2
1033end
1034
1035note mark: where t first starts in src [s, e); a README without it dies
1036fn mark s ulong e ulong t view uchar * to ulong
1037y = find s e t
1038if eq y e
1039die tnomark 0 0
1040end
1041ret y
1042end
1043
1044note gen_readme: README [rs, re) with the lead block, the agent link and the index put between their markers
1045fn gen_readme rs ulong re ulong to void
1046be = add mark rs re tlbeg len tlbeg
1047x = mark be re tlend
1048be2 = add mark x re tabeg len tabeg
1049x2 = mark be2 re taend
1050be3 = add mark x2 re tbeg len tbeg
1051x3 = mark be3 re tend
1052ems rs sub be rs
1053emb 10
1054gen_lead
1055ems x sub be2 x
1056emb 10
1057gen_agent
1058ems x2 sub be3 x2
1059emb 10
1060gen_index
1061ems x3 sub re x3
1062end
1063
1064note gen_repos: the manifest, name TAB rid TAB head TAB tier TAB description
1065fn gen_repos to void
1066em rphdr
1067n = get nseq 0
1068for i ulong n
1069r = get seq i
1070ems fldo r 0 fldn r 0
1071emb 9
1072ems fldo r 1 fldn r 1
1073emb 9
1074ems fldo r 3 fldn r 3
1075emb 9
1076if eq kindof r 1
1077em tcore
1078else
1079em trest
1080end
1081emb 9
1082ems fldo r 4 fldn r 4
1083emb 10
1084end
1085end
1086
1087note gen_sh: the bootstrap script, one si_repo line per listed project
1088fn gen_sh to void
1089em sha0
1090ems get tim 0 get tim 1
1091em sha0b
1092em sha1
1093em sha2
1094em sha3
1095em sha4
1096emu get ncor 0
1097em shb0
1098emu get nseq 0
1099em shc0
1100emu get ncor 0
1101em shd0
1102em shd1
1103em shd2
1104em shd3
1105n = get nseq 0
1106for i ulong n
1107r = get seq i
1108em shr1
1109ems fldo r 0 fldn r 0
1110em shr2
1111ems fldo r 1 fldn r 1
1112em shr2
1113ems fldo r 3 fldn r 3
1114em shr2
1115if eq kindof r 1
1116em tcore
1117else
1118em trest
1119end
1120em shr3
1121end
1122em she0
1123em rb0
1124ems get rbo 0 get rbo 1
1125em rb1
1126ems get rbo 2 get rbo 3
1127em rb2
1128gen_repos
1129em rb3
1130em she1
1131em rbfin
1132end
1133
1134fn main to int
1135if ne argcnt 7
1136w = write 2 tusage 0 len tusage
1137ret 2
1138end
1139ins = load 1
1140ine = get srcn 0
1141ors = load 2
1142ore = get srcn 0
1143vs = load 3
1144ve = get srcn 0
1145rs = load 4
1146re = get srcn 0
1147rload rom 0
1148rload rlay 2
1149parse_inventory ins ine
1150parse_order ors ore
1151parse_state vs ve
1152check_unique
1153resolve
1154mark_hidden
1155order_core
1156order_rest
1157gen_readme rs re
1158wout 4
1159gen_repos
1160wout 5
1161gen_sh
1162wout 6
1163ret 0
1164end