interplane.Get Omega
Read in your language

omega-mcp

aien-dev/omega-mcp

Brought by drakestapleton

branch main Branches Tags History

omega-mcp / qa / store.om

1101 lines · 22885 bytes Raw Blame History
1note Agent Q&A store. Drake (2026-10-29): "We need a StackOverflow specifically for agents, built in to every
2note coding CLI where opt-in contribution (selected by users for their agents) is strongly promoted."
3note One entry per file in the store directory, named by the SHA-256 of its exact bytes (64 lowercase hex),
4note so stores can be copied and merged by any transport and a damaged copy is seen on read.
5note Reading (search, read, consent_status) is open to every agent. Contributing (ask, answer, check) needs
6note the line `allow AGENT` in the consent file, which the person writes; this server only ever reads it,
7note again at every contribution, so removing the line stops sharing at once.
8note A contribution is refused, never edited, when it holds a private key block, a home directory path
9note or a long mixed-case run that looks like a key or token. Entry layout (text, one field per line):
10note agent-qa 1 / kind K / agent A / [ref ID] / [title T] / [versions V] / [result R] / [evidence E]
11note then `steps N` and `body N` blocks: N decimal bytes follow the newline, then one newline.
12global qv ulong 16
13global qagent uchar 64
14global qcpath uchar 4096
15global qid uchar 64
16global qerr uint 1
17
18const qagentn ulong 0
19const qcpathn ulong 1
20const qdirfd ulong 2
21const qmatch ulong 3
22const qshown ulong 4
23const qskip ulong 5
24const qfoff ulong 6
25const qflen ulong 7
26const qrule ulong 8
27const qfmax ulong 32768
28const qmax ulong 65536
29const qpage ulong 50
30note Flags as in maintenance/state.om (Wolfram-checked there): directory/nofollow/cloexec, read/nofollow/
31note cloexec/nonblock, rdwr/create/excl/nofollow/cloexec. 420 is mode 0644 (aien-calc receipt in the commit message).
32const qodir int 573440
33const qord int 559104
34const qonew int 557250
35const qmode int 420
36
37text qhex "0123456789abcdef"
38text qdot "."
39text qargstore "--store"
40text qargagent "--agent"
41text qargcons "--consent"
42text qallow "allow "
43text qmagic "agent-qa 1\n"
44text qkind "kind "
45text qkq "question"
46text qka "answer"
47text qkc "check"
48text qlagent "agent "
49text qlref "ref "
50text qltitle "title "
51text qlvers "versions "
52text qlres "result "
53text qlevid "evidence "
54text qlsteps "steps "
55text qlbody "body "
56text qworks "works"
57text qfails "fails"
58text qbegin "-----BEGIN"
59text qhome "/home/"
60text qusers "/Users/"
61text qroot "/root/"
62text qtmpp ".qa-tmp-"
63
64text qkok "ok"
65text qkerr "error"
66text qkid "id"
67text qkkind "kind"
68text qktitle "title"
69text qkref "ref"
70text qktext "text"
71text qkres "results"
72text qkmatch "matches"
73text qkshown "shown"
74text qkskip "skipped"
75text qkmore "more"
76text qkcreated "created"
77text qkagent "agent"
78text qkmay "may_contribute"
79text qkcfile "consent_file"
80text qkhow "how_to_enable"
81text qknone ""
82
83text qhow1 "Reading is open. To let this agent share questions, answers and checks, the person (not the agent) adds the line `allow "
84text qhow2 "` to the consent file named in consent_file, and deletes it to stop. Only the entry text is shared: no chats, code or files."
85text qhownf "Reading is open. Sharing is off because this server was started without --consent FILE. The person adds --consent with a file they own to this agent's MCP entry, then the line `allow AGENT` to that file."
86text qeno "Sharing is off for this agent. Ask the person to turn it on (see consent_status); reading still works."
87text qeline "Refused: a one-line field holds a control character or a line break. Nothing was saved."
88text qetext "Refused: a text field holds a control character other than line break or tab. Nothing was saved."
89text qekey "Refused: the text holds a private key block (-----BEGIN). Remove it. Nothing was saved."
90text qehome "Refused: the text names a home directory (/home/, /Users/ or /root/). Write ~/... instead. Nothing was saved."
91text qetok "Refused: the text holds a long mixed-case string that looks like a key or token. Remove or split it. Nothing was saved."
92text qeid "Refused: an id is 64 lowercase hexadecimal characters."
93text qemiss "No entry has this id in this store."
94text qekindq "Refused: question must be the id of a question entry."
95text qekinda "Refused: answer must be the id of an answer entry."
96text qedamage "The entry file does not match its id or is not a valid entry; it was not used."
97text qeres "Refused: result is works or fails."
98text qeempty "Refused: every field must be nonempty."
99text qestale "Refused: a temporary file from an interrupted write (.qa-tmp-ID) blocks this entry; remove it and retry."
100text qeio "The store could not be written (input/output error); nothing new is visible."
101text qedir "The store directory could not be read."
102
103fn qa_eq a view uchar * t view uchar * to bool
104if ne len a len t
105ret false
106end
107for i ulong len a
108if ne get a i get t i
109ret false
110end
111end
112ret true
113end
114
115fn qa_lower c uchar to uchar
116if and ge c 65 le c 90
117ret add c 32
118end
119ret c
120end
121
122note True when w (nonempty) occurs in h, ASCII letters compared without case.
123fn qa_has h view uchar * w view uchar * to bool
124if gt len w len h
125ret false
126end
127for i ulong add sub len h len w 1
128hit = true
129for j ulong len w
130if ne qa_lower get h add i j qa_lower get w j
131hit = false
132break
133end
134end
135if hit
136ret true
137end
138end
139ret false
140end
141
142fn qa_isid s view uchar * to bool
143if ne len s 64
144ret false
145end
146for i ulong 64
147c = get s i
148if not or and ge c 48 le c 57 and ge c 97 le c 102
149ret false
150end
151end
152ret true
153end
154
155fn qa_agentok s view uchar * to bool
156if or eq len s 0 gt len s 64
157ret false
158end
159for i ulong len s
160c = get s i
161ok = or and ge c 97 le c 122 and ge c 48 le c 57
162if and gt i 0 or eq c 45 eq c 95
163ok = true
164end
165if not ok
166ret false
167end
168end
169ret true
170end
171
172note Strict UTF-8 (no overlong forms, no surrogates, at most U+10FFFF), so the JSON writer never fails.
173fn qa_utf8 s view uchar * to bool
174i = ulong 0
175n = len s
176while lt i n bound 16777216
177c = get s i
178if lt c 128
179i = add i 1
180continue
181end
182k = ulong 0
183lo = uchar 128
184hi = uchar 191
185if and ge c 194 le c 223
186k = 1
187end
188if and ge c 224 le c 239
189k = 2
190if eq c 224
191lo = 160
192end
193if eq c 237
194hi = 159
195end
196end
197if and ge c 240 le c 244
198k = 3
199if eq c 240
200lo = 144
201end
202if eq c 244
203hi = 143
204end
205end
206if eq k 0
207ret false
208end
209if gt add i k sub n 1
210ret false
211end
212for j ulong k
213d = get s add add i j 1
214if eq j 0
215if or lt d lo gt d hi
216ret false
217end
218else
219if or lt d 128 gt d 191
220ret false
221end
222end
223end
224i = add add i k 1
225end
226ret true
227end
228
229note 0 when the field may be shared; else the refusal: 1 key block, 2 home path, 3 token-like run.
230note A token-like run is 24 or more of A-Z a-z 0-9 _ - holding an upper-case letter, a lower-case letter
231note and a digit (lowercase hex digests such as sha256 values pass).
232fn qa_private s view uchar * to ulong
233if qa_has s qbegin
234ret 1
235end
236if or qa_has s qhome or qa_has s qusers qa_has s qroot
237ret 2
238end
239rn = ulong 0
240sn = ulong 0
241for i ulong len s
242c = get s i
243cl = ulong 0
244if and ge c 65 le c 90
245cl = 1
246end
247if and ge c 97 le c 122
248cl = 2
249end
250if and ge c 48 le c 57
251cl = 4
252end
253if or eq c 95 eq c 45
254cl = 8
255end
256if eq cl 0
257rn = 0
258sn = 0
259else
260rn = add rn 1
261sn = or sn cl
262if and ge rn 24 eq and sn 7 7
263ret 3
264end
265end
266end
267ret 0
268end
269
270note 0 ok, 1 when a one-line field (multi false) or a block (multi true) holds a forbidden control byte.
271fn qa_ctl s view uchar * multi bool to bool
272for i ulong len s
273c = get s i
274if or lt c 32 eq c 127
275if not and multi or eq c 10 eq c 9
276ret false
277end
278end
279end
280ret true
281end
282
283fn qa_wt ob edit uchar * s view uchar * to void
284js_wsbuf ob s 0 len s
285end
286
287fn qa_fail ob edit uchar * msg view uchar * to bool
288js_wobj ob
289qa_wt ob qkok
290js_wbool ob false
291qa_wt ob qkerr
292qa_wt ob msg
293js_wobje ob
294put qerr 0 1
295ret false
296end
297
298note Appends s at ent[n]; returns the new length (the caller sized ent above every reachable total).
299fn qa_add ent edit uchar * n ulong s view uchar * to ulong
300for i ulong len s
301put ent add n i get s i
302end
303ret add n len s
304end
305
306fn qa_addnum ent edit uchar * n ulong v ulong to ulong
307d = new uchar 24 0
308k = ulong 0
309x = v
310while true bound 24
311put d k add 48 uchar rem x 10
312k = add k 1
313x = div x 10
314if eq x 0
315break
316end
317end
318for i ulong k
319put ent add n i get d sub sub k i 1
320end
321ret add n k
322end
323
324fn qa_line ent edit uchar 65536 n ulong kw view uchar * vl view uchar * to ulong
325m = qa_add ent n kw
326m = qa_add ent m vl
327put ent m 10
328ret add m 1
329end
330
331fn qa_block ent edit uchar 65536 n ulong kw view uchar * vl view uchar * to ulong
332m = qa_add ent n kw
333m = qa_addnum ent m len vl
334put ent m 10
335m = qa_add ent add m 1 vl
336put ent m 10
337ret add m 1
338end
339
340fn qa_hexid d view uchar 32 to void
341for i ulong 32
342c = get d i
343put qid mul i 2 get qhex ulong shr c 4
344put qid add mul i 2 1 get qhex ulong and c 15
345end
346end
347
348fn qa_idis name view uchar * to bool
349if ne len name 64
350ret false
351end
352for i ulong 64
353if ne get qid i get name i
354ret false
355end
356end
357ret true
358end
359
360fn qa_idof ent view uchar * to void
361d = new uchar 32 0
362sha256_hash ent d
363qa_hexid d
364end
365
366note Reads file name (relative to the store) into fb; returns its size, -1 missing, -2 unreadable,
367note -3 larger than qmax.
368fn qa_load name view uchar * fb edit uchar 65536 to long
369x = new uchar 1 0
370r = fopen name 0 len name qord 0
371if lt r 0
372if eq r -2
373ret -1
374end
375ret -2
376end
377fd = int r
378n = ulong 0
379res = long 0
380while lt n qmax bound 65537
381k = read fd fb n sub qmax n
382if eq k -4
383continue
384end
385if lt k 0
386res = -2
387break
388end
389if eq k 0
390break
391end
392n = add n ulong k
393end
394if and eq res 0 eq n qmax
395k = read fd x 0 1
396if ne k 0
397res = -3
398end
399end
400z = fclose fd
401if lt res 0
402ret res
403end
404ret long n
405end
406
407note Finds header field key (e.g. `title `) in entry e, up to the first steps or body block; sets
408note qv[qfoff], qv[qflen] and returns true when present.
409fn qa_field e view uchar * key view uchar * to bool
410p = len qmagic
411while lt p len e bound 65536
412q = p
413while lt q len e bound 65536
414if eq get e q 10
415break
416end
417q = add q 1
418end
419ln = sub q p
420if or and ge ln len qlsteps qa_eq part e p len qlsteps qlsteps and ge ln len qlbody qa_eq part e p len qlbody qlbody
421ret false
422end
423if ge ln len key
424if qa_eq part e p len key key
425put qv qfoff add p len key
426put qv qflen sub ln len key
427ret true
428end
429end
430p = add q 1
431end
432ret false
433end
434
435note An entry is usable when its bytes hash to its name, start with the magic line, carry a kind and
436note are valid UTF-8.
437fn qa_valid name view uchar * e view uchar * to bool
438if lt len e len qmagic
439ret false
440end
441if not qa_eq part e 0 len qmagic qmagic
442ret false
443end
444if not qa_utf8 e
445ret false
446end
447qa_idof e
448if not qa_idis name
449ret false
450end
451ret qa_field e qkind
452end
453
454fn qa_kindis e view uchar * k view uchar * to bool
455if not qa_field e qkind
456ret false
457end
458ret qa_eq part e get qv qfoff get qv qflen k
459end
460
461note True when line s..e of b is exactly `allow AGENT` for this server's agent.
462fn qa_isallow b view uchar * to bool
463if ne len b add len qallow get qv qagentn
464ret false
465end
466if not qa_eq part b 0 len qallow qallow
467ret false
468end
469for i ulong get qv qagentn
470if ne get b add len qallow i get qagent i
471ret false
472end
473end
474ret true
475end
476
477fn qa_allowed b view uchar * to bool
478s = ulong 0
479while lt s len b bound 65537
480e = s
481while lt e len b bound 65537
482if eq get b e 10
483break
484end
485e = add e 1
486end
487if qa_isallow part b s sub e s
488ret true
489end
490s = add e 1
491end
492ret false
493end
494
495note At most 65536 bytes. One more byte is then tried. If it is there, the tail after the last newline was cut
496note and is not a line: `allow AGENT` split across that boundary does not count. A short file still ends at EOF,
497note so its last line counts, including one that does not end in a newline.
498fn qa_consented to bool
499if eq get qv qcpathn 0
500ret false
501end
502p = new uchar 4096 0
503b = new uchar 65536 0
504x = new uchar 1 0
505for i ulong get qv qcpathn
506put p i get qcpath i
507end
508r = fopen p 0 get qv qcpathn qord 0
509if lt r 0
510ret false
511end
512n = ulong 0
513while lt n 65536 bound 65537
514k = read int r b n sub 65536 n
515if eq k -4
516continue
517end
518if le k 0
519break
520end
521n = add n ulong k
522end
523if eq n 65536
524g = read int r x 0 1
525t = ulong 0
526while lt t 63 bound 63
527if ne g -4
528break
529end
530g = read int r x 0 1
531t = add t 1
532end
533if gt g 0
534cut = ulong 0
535j = n
536while gt j 0 bound 65537
537j = sub j 1
538if eq get b j 10
539cut = add j 1
540break
541end
542end
543n = cut
544end
545if lt g 0
546z = fclose int r
547ret false
548end
549end
550z = fclose int r
551ret qa_allowed part b 0 n
552end
553
554fn qa_consent ob edit uchar * to bool
555h = new uchar 512 0
556a = new uchar 64 0
557for i ulong get qv qagentn
558put a i get qagent i
559end
560p = new uchar 4096 0
561for i ulong get qv qcpathn
562put p i get qcpath i
563end
564js_wobj ob
565qa_wt ob qkok
566js_wbool ob true
567qa_wt ob qkagent
568js_wsbuf ob a 0 get qv qagentn
569qa_wt ob qkmay
570js_wbool ob qa_consented
571qa_wt ob qkcfile
572if eq get qv qcpathn 0
573js_wnull ob
574else
575js_wsbuf ob p 0 get qv qcpathn
576end
577qa_wt ob qkhow
578if eq get qv qcpathn 0
579qa_wt ob qhownf
580else
581m = qa_add h 0 qhow1
582m = qa_add h m part a 0 get qv qagentn
583m = qa_add h m qhow2
584js_wsbuf ob h 0 m
585end
586js_wobje ob
587ret true
588end
589
590fn qa_read ob edit uchar * id view uchar * fb edit uchar 65536 to bool
591if not qa_isid id
592ret qa_fail ob qeid
593end
594r = qa_load id fb
595if eq r -1
596ret qa_fail ob qemiss
597end
598if lt r 0
599ret qa_fail ob qedamage
600end
601if not qa_valid id part fb 0 ulong r
602ret qa_fail ob qedamage
603end
604js_wobj ob
605qa_wt ob qkok
606js_wbool ob true
607qa_wt ob qkid
608qa_wt ob id
609qa_wt ob qktext
610js_wsbuf ob fb 0 ulong r
611js_wobje ob
612ret true
613end
614
615note True when every space-separated word of q occurs in e (an empty query matches every entry).
616fn qa_words e view uchar * q view uchar * to bool
617p = ulong 0
618while lt p len q bound 65536
619if eq get q p 32
620p = add p 1
621continue
622end
623s = p
624while lt p len q bound 65536
625if eq get q p 32
626break
627end
628p = add p 1
629end
630if not qa_has e part q s sub p s
631ret false
632end
633end
634ret true
635end
636
637fn qa_hit ob edit uchar * name view uchar * e view uchar * to void
638put qv qmatch add get qv qmatch 1
639if ge get qv qshown qpage
640ret
641end
642put qv qshown add get qv qshown 1
643js_wobj ob
644qa_wt ob qkid
645qa_wt ob name
646qa_wt ob qkkind
647if qa_field e qkind
648js_wsbuf ob e get qv qfoff get qv qflen
649else
650qa_wt ob qknone
651end
652qa_wt ob qktitle
653if qa_field e qltitle
654js_wsbuf ob e get qv qfoff get qv qflen
655else
656qa_wt ob qknone
657end
658qa_wt ob qkref
659if qa_field e qlref
660js_wsbuf ob e get qv qfoff get qv qflen
661else
662qa_wt ob qknone
663end
664js_wobje ob
665end
666
667note getdents64 record: ino u64, offset i64, reclen u16 at 16, type at 18, NUL-terminated name at 19.
668note Names that are not 64 lowercase hex (temporary files, anything else) are not entries.
669fn qa_one ob edit uchar * q view uchar * name view uchar 64 to void
670fb = new uchar 65536 0
671m = qa_load name fb
672if lt m 0
673put qv qskip add get qv qskip 1
674ret
675end
676if not qa_valid name part fb 0 ulong m
677put qv qskip add get qv qskip 1
678ret
679end
680if qa_words part fb 0 ulong m q
681qa_hit ob name part fb 0 ulong m
682end
683end
684
685note Copies the NUL-terminated name of the record at p (length rl) into name (first 64 bytes); returns its length.
686fn qa_dname buf view uchar 32768 p ulong rl ulong name edit uchar 64 to ulong
687n = ulong 0
688for i ulong sub rl 19
689c = get buf add add p 19 i
690if eq c 0
691break
692end
693if lt n 64
694put name n c
695end
696n = add n 1
697end
698ret n
699end
700
701note Walks one getdents64 batch of k bytes; false when a record is malformed.
702fn qa_batch ob edit uchar * q view uchar * buf view uchar 32768 k ulong to bool
703name = new uchar 64 0
704p = ulong 0
705while lt add p 19 k bound 32768
706rl = ulong get buf add p 17
707rl = add shl rl 8 ulong get buf add p 16
708if or lt rl 20 gt add p rl k
709ret false
710end
711n = qa_dname buf p rl name
712if eq n 64
713if qa_isid name
714qa_one ob q name
715end
716end
717p = add p rl
718end
719ret true
720end
721
722note Names that are not 64 lowercase hex (temporary files, anything else) are not entries.
723fn qa_search ob edit uchar * q view uchar * fb edit uchar 65536 to bool
724buf = new uchar 32768 0
725r = fopen qdot 0 1 qodir 0
726if lt r 0
727ret qa_fail ob qedir
728end
729put qv qmatch 0
730put qv qshown 0
731put qv qskip 0
732js_wobj ob
733qa_wt ob qkok
734js_wbool ob true
735qa_wt ob qkres
736js_warr ob
737ok = true
738while true bound 16777216
739k = getdnts int r buf 0 32768
740if eq k 0
741break
742end
743if lt k 0
744ok = false
745break
746end
747if not qa_batch ob q buf ulong k
748ok = false
749break
750end
751end
752z = fclose int r
753js_warre ob
754qa_wt ob qkmatch
755js_wulong ob get qv qmatch
756qa_wt ob qkshown
757js_wulong ob get qv qshown
758qa_wt ob qkmore
759js_wbool ob gt get qv qmatch get qv qshown
760qa_wt ob qkskip
761js_wulong ob get qv qskip
762js_wobje ob
763if not ok
764put qerr 0 1
765end
766ret ok
767end
768
769note Creates tmp exclusively and writes ent to it with fsync: 0 done, 1 tmp already exists, 2 I/O error
770note (tmp removed).
771fn qa_writetmp tmp view uchar 72 ent view uchar * to ulong
772r = fopen tmp 0 72 qonew qmode
773if lt r 0
774if eq r -17
775ret 1
776end
777ret 2
778end
779p = ulong 0
780ok = true
781while lt p len ent bound 65537
782k = write int r ent p sub len ent p
783if eq k -4
784continue
785end
786if le k 0
787ok = false
788break
789end
790p = add p ulong k
791end
792if ok
793k = fsync int r
794if ne k 0
795ok = false
796end
797end
798k = fclose int r
799if ne k 0
800ok = false
801end
802if not ok
803z = unlink tmp 0 72
804ret 2
805end
806ret 0
807end
808
809note Renames tmp to id (a packed list of two records) and syncs the store directory.
810fn qa_commit tmp view uchar 72 id view uchar 64 to bool
811pk = new uchar 160 0
812put pk 0 2
813put pk 2 72
814for i ulong 72
815put pk add 4 i get tmp i
816end
817put pk 76 64
818for i ulong 64
819put pk add 78 i get id i
820end
821k = renames pk 0 142 0
822if ne k 0
823z = unlink tmp 0 72
824ret false
825end
826k = fsync int get qv qdirfd
827ret eq k 0
828end
829
830note Writes ent as the file named by its SHA-256: exclusive temporary file, write, fsync, rename, directory
831note fsync. An entry that already exists is not rewritten (created false).
832fn qa_store ob edit uchar * ent view uchar * kind view uchar * to bool
833id = new uchar 64 0
834tmp = new uchar 72 0
835qa_idof ent
836for i ulong 64
837put id i get qid i
838put tmp add 8 i get qid i
839end
840for i ulong 8
841put tmp i get qtmpp i
842end
843created = true
844r = fopen id 0 64 qord 0
845if ge r 0
846z = fclose int r
847created = false
848else
849w = qa_writetmp tmp ent
850if eq w 1
851ret qa_fail ob qestale
852end
853if ne w 0
854ret qa_fail ob qeio
855end
856if not qa_commit tmp id
857ret qa_fail ob qeio
858end
859end
860js_wobj ob
861qa_wt ob qkok
862js_wbool ob true
863qa_wt ob qkid
864qa_wt ob id
865qa_wt ob qkkind
866qa_wt ob kind
867qa_wt ob qkcreated
868js_wbool ob created
869js_wobje ob
870ret true
871end
872
873fn qa_share ob edit uchar * s view uchar * multi bool to bool
874if eq len s 0
875ret qa_fail ob qeempty
876end
877if not qa_ctl s multi
878if multi
879ret qa_fail ob qetext
880end
881ret qa_fail ob qeline
882end
883r = qa_private s
884if eq r 1
885ret qa_fail ob qekey
886end
887if eq r 2
888ret qa_fail ob qehome
889end
890if eq r 3
891ret qa_fail ob qetok
892end
893ret true
894end
895
896note sel 0: id must name a question; sel 1: id must name an answer.
897fn qa_refkind ob edit uchar * id view uchar * sel ulong fb edit uchar 65536 to bool
898if not qa_isid id
899ret qa_fail ob qeid
900end
901r = qa_load id fb
902if eq r -1
903ret qa_fail ob qemiss
904end
905if lt r 0
906ret qa_fail ob qedamage
907end
908if not qa_valid id part fb 0 ulong r
909ret qa_fail ob qedamage
910end
911if eq sel 0
912if not qa_kindis part fb 0 ulong r qkq
913ret qa_fail ob qekindq
914end
915else
916if not qa_kindis part fb 0 ulong r qka
917ret qa_fail ob qekinda
918end
919end
920ret true
921end
922
923fn qa_head ent edit uchar 65536 kind view uchar * to ulong
924a = new uchar 64 0
925for i ulong get qv qagentn
926put a i get qagent i
927end
928n = qa_add ent 0 qmagic
929n = qa_line ent n qkind kind
930n = qa_line ent n qlagent part a 0 get qv qagentn
931ret n
932end
933
934note Field slots in fv (qfmax bytes each, lengths in fl): 0 words, 1 id, 2 title, 3 versions, 4 body,
935note 5 question, 6 answer, 7 steps, 8 evidence, 9 result.
936fn qa_ask ob edit uchar * fv view uchar 327680 fl view ulong 10 ent edit uchar 65536 to bool
937if not qa_consented
938ret qa_fail ob qeno
939end
940if not qa_share ob part fv mul 2 qfmax get fl 2 false
941ret false
942end
943if not qa_share ob part fv mul 3 qfmax get fl 3 false
944ret false
945end
946if not qa_share ob part fv mul 4 qfmax get fl 4 true
947ret false
948end
949n = qa_head ent qkq
950n = qa_line ent n qltitle part fv mul 2 qfmax get fl 2
951n = qa_line ent n qlvers part fv mul 3 qfmax get fl 3
952n = qa_block ent n qlbody part fv mul 4 qfmax get fl 4
953ret qa_store ob part ent 0 n qkq
954end
955
956fn qa_answer ob edit uchar * fv view uchar 327680 fl view ulong 10 ent edit uchar 65536 fb edit uchar 65536 to bool
957if not qa_consented
958ret qa_fail ob qeno
959end
960if not qa_refkind ob part fv mul 5 qfmax get fl 5 0 fb
961ret false
962end
963if not qa_share ob part fv mul 3 qfmax get fl 3 false
964ret false
965end
966if not qa_share ob part fv mul 8 qfmax get fl 8 false
967ret false
968end
969if not qa_share ob part fv mul 7 qfmax get fl 7 true
970ret false
971end
972if not qa_share ob part fv mul 4 qfmax get fl 4 true
973ret false
974end
975n = qa_head ent qka
976n = qa_line ent n qlref part fv mul 5 qfmax get fl 5
977n = qa_line ent n qlvers part fv mul 3 qfmax get fl 3
978n = qa_line ent n qlevid part fv mul 8 qfmax get fl 8
979n = qa_block ent n qlsteps part fv mul 7 qfmax get fl 7
980n = qa_block ent n qlbody part fv mul 4 qfmax get fl 4
981ret qa_store ob part ent 0 n qka
982end
983
984fn qa_check ob edit uchar * fv view uchar 327680 fl view ulong 10 ent edit uchar 65536 fb edit uchar 65536 to bool
985if not qa_consented
986ret qa_fail ob qeno
987end
988if not or qa_eq part fv mul 9 qfmax get fl 9 qworks qa_eq part fv mul 9 qfmax get fl 9 qfails
989ret qa_fail ob qeres
990end
991if not qa_refkind ob part fv mul 6 qfmax get fl 6 1 fb
992ret false
993end
994if not qa_share ob part fv mul 8 qfmax get fl 8 false
995ret false
996end
997if not qa_share ob part fv mul 4 qfmax get fl 4 true
998ret false
999end
1000n = qa_head ent qkc
1001n = qa_line ent n qlref part fv mul 6 qfmax get fl 6
1002n = qa_line ent n qlres part fv mul 9 qfmax get fl 9
1003n = qa_line ent n qlevid part fv mul 8 qfmax get fl 8
1004n = qa_block ent n qlbody part fv mul 4 qfmax get fl 4
1005ret qa_store ob part ent 0 n qkc
1006end
1007
1008note Startup: --store DIR (absolute, an existing directory, becomes the working directory), --agent NAME
1009note (lowercase letters, digits, - and _, at most 64), optional --consent FILE (absolute). Exit 90 on a
1010note bad command line, 91 when the store cannot be opened.
1011fn qa_boot to int
1012put qerr 0 0
1013for i ulong 16
1014put qv i 0
1015end
1016a = new uchar 4096 0
1017v = new uchar 4096 0
1018ns = ulong 0
1019i = ulong 1
1020while lt i ulong argcnt bound 8
1021r = argread i a 0 4096
1022if lt r 0
1023ret 90
1024end
1025n = ulong r
1026i = add i 1
1027if ge i ulong argcnt
1028ret 90
1029end
1030r = argread i v 0 4096
1031if le r 0
1032ret 90
1033end
1034m = ulong r
1035i = add i 1
1036if qa_eq part a 0 n qargstore
1037if or ne ns 0 ne get v 0 47
1038ret 90
1039end
1040r = fopen v 0 m qodir 0
1041if lt r 0
1042ret 91
1043end
1044put qv qdirfd ulong r
1045k = fchdir int r
1046if ne k 0
1047ret 91
1048end
1049ns = m
1050else
1051if qa_eq part a 0 n qargagent
1052if or ne get qv qagentn 0 not qa_agentok part v 0 m
1053ret 90
1054end
1055for j ulong m
1056put qagent j get v j
1057end
1058put qv qagentn m
1059else
1060if qa_eq part a 0 n qargcons
1061if or ne get qv qcpathn 0 ne get v 0 47
1062ret 90
1063end
1064for j ulong m
1065put qcpath j get v j
1066end
1067put qv qcpathn m
1068else
1069ret 90
1070end
1071end
1072end
1073end
1074if or eq ns 0 eq get qv qagentn 0
1075ret 90
1076end
1077ret 0
1078end
1079
1080note Tool numbers: 0 search, 1 read, 2 consent_status, 3 ask, 4 answer, 5 check.
1081fn qa_call op ulong fv view uchar 327680 fl view ulong 10 ob edit uchar * to bool
1082put qerr 0 0
1083ent = new uchar 65536 0
1084fb = new uchar 65536 0
1085if eq op 0
1086ret qa_search ob part fv 0 get fl 0 fb
1087end
1088if eq op 1
1089ret qa_read ob part fv qfmax get fl 1 fb
1090end
1091if eq op 2
1092ret qa_consent ob
1093end
1094if eq op 3
1095ret qa_ask ob fv fl ent
1096end
1097if eq op 4
1098ret qa_answer ob fv fl ent fb
1099end
1100ret qa_check ob fv fl ent fb
1101end