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 / mcp / src / 10mcp.om

1214 lines · 27413 bytes Raw Blame History
1note synapse-mcp 10mcp.om: MCP stdio server (JSON-RPC 2.0, one message per line) over the coordination CLIs.
2note Every tool is one fixed argv executed without a shell; argument values are separate argv records.
3global gop ulong 4
4global gopl ulong 4
5global gid ulong 2
6global gbad ulong 2
7global gq ulong 4
8note gobj 0: JSON object token searched by mc_get and pu_memb2; gv 0: value token found by mc_get
9note gnm 0: offset of the tool name for mc_build (omc allows six parameter slots; a sized-* view takes two)
10global gobj ulong 1
11global gv ulong 1
12global gnm ulong 1
13global gln ulong 2
14
15text musage "usage: synapse-mcp --bin-dir DIR [--home DIR]"
16text mbindir "--bin-dir"
17text mhome "--home"
18text menvh "AIEN_COORD_HOME="
19text menvhm "HOME="
20text mdhome "/.local/state/aien-coord"
21text mnohome "synapse-mcp: no --home, AIEN_COORD_HOME or HOME"
22text mnobin "synapse-mcp: --bin-dir must be an absolute directory"
23text mtrunc "[stdout truncated at 1048576 bytes]\n"
24text mkjrpc "jsonrpc"
25text mk20 "2.0"
26text mkid "id"
27text mkmeth "method"
28text mkpar "params"
29text mkname "name"
30text mkargs "arguments"
31text mminit "initialize"
32text mmping "ping"
33text mmlist "tools/list"
34text mmcall "tools/call"
35text mrpre "{\"jsonrpc\":\"2.0\",\"id\":"
36text mridnul "null"
37text mrres ",\"result\":"
38text mrerr ",\"error\":{\"code\":"
39text mrmsg ",\"message\":\""
40text mrend "\"}}\n"
41text mrclose "}\n"
42text mrinit "{\"protocolVersion\":\"2025-06-18\",\"capabilities\":{\"tools\":{\"listChanged\":false}},\"serverInfo\":{\"name\":\"synapse-mcp\",\"version\":\"0.1.0\"},\"instructions\":\"AIEN coordination tools: claim before you work, check who holds a target, read drift findings.\"}"
43text mrempty "{}"
44text mrtext "{\"content\":[{\"type\":\"text\",\"text\":\""
45text mriserr "\"}],\"isError\":"
46text mtrue "true"
47text mfalse "false"
48text mexitc "{\"exit_code\":"
49text mwhoc "{\"who\":\""
50text mwhoe "\",\"exit_code\":"
51text mcl "}"
52text mmeparse "Parse error"
53text mmeinv "Invalid Request"
54text mmenome "Method not found"
55text mmeparm "Invalid params"
56text mmetool "Unknown tool"
57text mbadarg "Invalid params: bad or missing argument"
58text mbadunk "Invalid params: unknown argument"
59text mbadlong "Invalid params: arguments too long"
60text mbadobj "Invalid params: arguments must be an object"
61text mtoolong "synapse-mcp: request line over 1048576 bytes"
62
63text mbclaims "/synapse-claims"
64text mbmap "/synapse-map"
65text mbpulse "/synapse-pulse"
66text mbboard "/synapse-board"
67text mcclaim "claim"
68text mcrenew "renew"
69text mcrel "release"
70text mcstate "state"
71text mcwho "who"
72text mclist "list"
73text mcconf "conflicts"
74text mcver "verify"
75text mcwhere "where"
76text mctree "tree"
77text mccheck "check"
78text mcrun "run"
79text mcrender "render"
80text mfagent "--agent"
81text mfsess "--session"
82text mftarg "--target"
83text mfttl "--ttl"
84text mfmode "--mode"
85text mfstate "--state"
86text mfint "--intent"
87text mfevid "--evidence"
88text mfid "--id"
89text mfall "--all"
90text mfprefix "--agent-prefix"
91text mflimit "--limit"
92text mftext "--text"
93text mfalert "--alert"
94text mfcbin "--claims-bin"
95text mfclaims "--claims"
96text mfmap "--map"
97text mffind "--findings"
98text mfout "--out"
99text makagent "agent"
100text maksess "session"
101text maktarg "target"
102text makttl "ttl"
103text makmode "mode"
104text makstate "state"
105text makint "intent"
106text makevid "evidence"
107text makid "id"
108text makall "all"
109text maklane "lane"
110text maktask "task"
111text maklimit "limit"
112text maktext "text"
113text makalert "alert"
114text mkrepo "repo"
115text mrepoc "repo:"
116text mslboard "/board"
117text mslbcl "/board/claims.jsonl"
118text mslmap "/map.jsonl"
119text mslfind "/pulse/findings.jsonl"
120text mlisterr "synapse-mcp: synapse-claims list --all failed; board not rendered"
121text mwrerr "synapse-mcp: could not write HOME/board/claims.jsonl; board not rendered"
122
123text mtn01 "coord_where"
124text mtn02 "coord_tree"
125text mtn03 "coord_check"
126text mtn04 "coord_claim"
127text mtn05 "coord_renew"
128text mtn06 "coord_release"
129text mtn07 "coord_state"
130text mtn08 "coord_who"
131text mtn09 "coord_list"
132text mtn10 "coord_conflicts"
133text mtn11 "coord_verify"
134text mtn12 "coord_pulse"
135text mtn13 "coord_board"
136text mtn14 "coord_lane"
137
138text ml00 "{\"tools\":["
139text ml01 "{\"name\":\"coord_where\",\"description\":\"Rank aien-dev repositories for a task (synapse-map where) and show who holds claims on the top 3 (synapse-claims who repo:R).\",\"inputSchema\":{\"type\":\"object\",\"properties\":{\"task\":{\"type\":\"string\",\"description\":\"plain-words task\"},\"limit\":{\"type\":\"integer\",\"minimum\":1}},\"required\":[\"task\"],\"additionalProperties\":false}},"
140text ml02 "{\"name\":\"coord_tree\",\"description\":\"Repository parent tree (synapse-map tree).\",\"inputSchema\":{\"type\":\"object\",\"properties\":{\"text\":{\"type\":\"boolean\"}},\"additionalProperties\":false}},"
141text ml03 "{\"name\":\"coord_check\",\"description\":\"Repository map findings: orphans, missing or archived parents, duplicate purposes (synapse-map check).\",\"inputSchema\":{\"type\":\"object\",\"properties\":{},\"additionalProperties\":false}},"
142text ml04 "{\"name\":\"coord_claim\",\"description\":\"Claim a target before working on it. Exit 2 means another agent holds it; the holder is returned.\",\"inputSchema\":{\"type\":\"object\",\"properties\":{\"agent\":{\"type\":\"string\"},\"session\":{\"type\":\"string\"},\"target\":{\"type\":\"string\",\"description\":\"repo:O/R | branch:O/R#ref | path:O/R:glob | pr:O/R#N | ext:...\"},\"ttl\":{\"type\":\"integer\",\"minimum\":1},\"mode\":{\"type\":\"string\",\"enum\":[\"excl\",\"shared\"]},\"state\":{\"type\":\"string\"},\"intent\":{\"type\":\"string\"},\"evidence\":{\"type\":\"array\",\"items\":{\"type\":\"string\"}}},\"required\":[\"agent\",\"session\",\"target\"],\"additionalProperties\":false}},"
143text ml05 "{\"name\":\"coord_renew\",\"description\":\"Extend a claim lease you hold.\",\"inputSchema\":{\"type\":\"object\",\"properties\":{\"agent\":{\"type\":\"string\"},\"id\":{\"type\":\"string\"},\"ttl\":{\"type\":\"integer\",\"minimum\":1}},\"required\":[\"agent\",\"id\"],\"additionalProperties\":false}},"
144text ml06 "{\"name\":\"coord_release\",\"description\":\"Release a claim you hold, optionally with a final state and evidence.\",\"inputSchema\":{\"type\":\"object\",\"properties\":{\"agent\":{\"type\":\"string\"},\"id\":{\"type\":\"string\"},\"state\":{\"type\":\"string\"},\"evidence\":{\"type\":\"array\",\"items\":{\"type\":\"string\"}}},\"required\":[\"agent\",\"id\"],\"additionalProperties\":false}},"
145text ml07 "{\"name\":\"coord_state\",\"description\":\"Record a state change (e.g. PR_OPEN, MERGED) on a claim you hold.\",\"inputSchema\":{\"type\":\"object\",\"properties\":{\"agent\":{\"type\":\"string\"},\"id\":{\"type\":\"string\"},\"state\":{\"type\":\"string\"},\"evidence\":{\"type\":\"array\",\"items\":{\"type\":\"string\"}}},\"required\":[\"agent\",\"id\",\"state\"],\"additionalProperties\":false}},"
146text ml08 "{\"name\":\"coord_who\",\"description\":\"Active claims overlapping a target.\",\"inputSchema\":{\"type\":\"object\",\"properties\":{\"target\":{\"type\":\"string\"}},\"required\":[\"target\"],\"additionalProperties\":false}},"
147text ml09 "{\"name\":\"coord_list\",\"description\":\"Active claims (all claims with all=true).\",\"inputSchema\":{\"type\":\"object\",\"properties\":{\"all\":{\"type\":\"boolean\"}},\"additionalProperties\":false}},"
148text ml10 "{\"name\":\"coord_conflicts\",\"description\":\"Conflicting active claim pairs.\",\"inputSchema\":{\"type\":\"object\",\"properties\":{},\"additionalProperties\":false}},"
149text ml11 "{\"name\":\"coord_verify\",\"description\":\"Verify the claims ledger hash chain and signatures.\",\"inputSchema\":{\"type\":\"object\",\"properties\":{},\"additionalProperties\":false}},"
150text ml12 "{\"name\":\"coord_pulse\",\"description\":\"Reconcile claims with GitHub and live sessions; returns drift findings (synapse-pulse run).\",\"inputSchema\":{\"type\":\"object\",\"properties\":{\"alert\":{\"type\":\"boolean\"}},\"additionalProperties\":false}},"
151text ml13 "{\"name\":\"coord_board\",\"description\":\"Render the generated board (BOARD.md, index.html) from claims, map and findings.\",\"inputSchema\":{\"type\":\"object\",\"properties\":{},\"additionalProperties\":false}},"
152text ml14 "{\"name\":\"coord_lane\",\"description\":\"Agents in a lane: active claims whose agent id starts with the lane prefix. Message these agents (parent and children) instead of agent://all.\",\"inputSchema\":{\"type\":\"object\",\"properties\":{\"lane\":{\"type\":\"string\",\"description\":\"agent id prefix, e.g. Coordination\"}},\"required\":[\"lane\"],\"additionalProperties\":false}}"
153text ml99 "]}"
154
155note ======== output ========
156fn mc_flush m ulong to void
157n = syn_len m
158k = ulong 0
159while lt k n bound 999999
160w = write 1 gsp add m k sub n k
161if le w 0
162exit 70
163end
164k = add k ulong w
165end
166syn_rew m
167end
168
169fn mc_hex v ulong to uchar
170if lt v 10
171ret uchar add v 48
172end
173ret uchar add v 87
174end
175
176note mc_qb: append byte c JSON-escaped (no quotes)
177fn mc_qb c uchar to void
178if or eq c 34 eq c 92
179syn_pb 92
180syn_pb c
181ret
182end
183if eq c 10
184syn_pb 92
185syn_pb 110
186ret
187end
188if eq c 9
189syn_pb 92
190syn_pb 116
191ret
192end
193if eq c 13
194syn_pb 92
195syn_pb 114
196ret
197end
198if lt c 32
199syn_pb 92
200syn_pb 117
201syn_pb 48
202syn_pb 48
203syn_pb mc_hex shr ulong c 4
204syn_pb mc_hex and ulong c 15
205ret
206end
207syn_pb c
208end
209
210fn mc_qa a view uchar * o ulong n ulong to void
211for i ulong n
212mc_qb get a add o i
213end
214end
215
216note mc_qerr: JSON-escape the captured stderr (gerrb, gerrn 0 bytes); a global cannot be an argument
217fn mc_qerr to void
218for i ulong get gerrn 0
219mc_qb get gerrb i
220end
221end
222
223fn mc_qt t view uchar * to void
224for i ulong len t
225mc_qb get t i
226end
227end
228
229fn mc_qp o ulong n ulong to void
230for i ulong n
231mc_qb get gsp add o i
232end
233end
234
235note mc_head: response prefix with the request id (or null)
236fn mc_head to ulong
237m = syn_mark
238syn_pt mrpre
239if eq get gid 1 0
240syn_pt mridnul
241else
242syn_ps get gid 0 get gid 1
243end
244ret m
245end
246
247fn mc_err code ulong msg view uchar * to void
248m = mc_head
249syn_pt mrerr
250syn_pb 45
251syn_pd code
252syn_pt mrmsg
253mc_qt msg
254syn_pt mrend
255mc_flush m
256end
257
258note mc_errp: error with a message held in the pool at (o,n)
259fn mc_errp code ulong o ulong n ulong to void
260m = mc_head
261syn_pt mrerr
262syn_pb 45
263syn_pd code
264syn_pt mrmsg
265mc_qp o n
266syn_pt mrend
267mc_flush m
268end
269
270fn mc_okt t view uchar * to void
271m = mc_head
272syn_pt mrres
273syn_pt t
274syn_pt mrclose
275mc_flush m
276end
277
278fn mc_list to void
279m = mc_head
280syn_pt mrres
281syn_pt ml00
282syn_pt ml01
283syn_pt ml02
284syn_pt ml03
285syn_pt ml04
286syn_pt ml05
287syn_pt ml06
288syn_pt ml07
289syn_pt ml08
290syn_pt ml09
291syn_pt ml10
292syn_pt ml11
293syn_pt ml12
294syn_pt ml13
295syn_pt ml14
296syn_pt ml99
297syn_pt mrclose
298mc_flush m
299end
300
301note ======== argument records ========
302note gbad 0: 0 ok, 1 bad value (gbad 1 = key text index), 2 unknown key, 3 too long, 4 not an object
303note gq 0: arguments consumed so far
304fn mc_bad k ulong why ulong to void
305if eq get gbad 0 0
306put gbad 0 why
307put gbad 1 k
308end
309end
310
311fn mc_room n ulong to bool
312if gt add add get gavs 0 n 16 4000
313ret false
314end
315if ge get gavs 1 60
316ret false
317end
318ret true
319end
320
321note mc_val: check decoded string tmp[0..n) and add it as one record; false when refused
322fn mc_val tmp view uchar * n ulong to bool
323if or eq n 0 eq get tmp 0 45
324ret false
325end
326for i ulong n
327if eq get tmp i 0
328ret false
329end
330end
331if not mc_room n
332put gbad 0 3
333ret false
334end
335m = syn_mark
336syn_pa tmp 0 n
337syn_ap m n
338syn_rew m
339ret true
340end
341
342fn mc_flag f view uchar * to bool
343if not mc_room len f
344put gbad 0 3
345ret false
346end
347syn_at f
348ret true
349end
350
351note mc_get: gv 0 = value token for key in args object gobj 0, or jsnone; counts the key as consumed
352fn mc_get src view uchar * tape view uint * key view uchar * to void
353put gv 0 jsnone
354a = get gobj 0
355if eq a jsnone
356ret
357end
358k = js_first tape a
359while ne k jsnone bound 999999
360if js_streq src js_off tape k key
361put gq 0 add get gq 0 1
362put gv 0 add k 1
363ret
364end
365k = js_mnext tape k
366end
367end
368
369note mc_sval: string value of gv 0 into tmp; its length, or -1 when absent or refused; req: must be present
370fn mc_sval src view uchar * tape view uint * tmp edit uchar * to long
371v = get gv 0
372if eq v jsnone
373if eq get gq 1 1
374mc_bad 0 1
375end
376put gq 1 0
377ret -1
378end
379put gq 1 0
380if ne js_type tape v jsstr
381mc_bad 0 1
382ret -1
383end
384n = js_sget src js_off tape v tmp
385if lt n 0
386mc_bad 0 1
387ret -1
388end
389ret n
390end
391
392note mc_semit: string argument from mc_sval (n bytes of tmp) as option flag + value
393fn mc_semit tmp view uchar * n long flag view uchar * to void
394if lt n 0
395ret
396end
397if not mc_flag flag
398ret
399end
400mc_spos tmp n
401end
402
403note mc_spos: string argument from mc_sval as one positional record (omc e5b32567 gives an empty text a
404note wrong length, omega-mv #775, so positionals do not pass an empty flag text)
405fn mc_spos tmp view uchar * n long to void
406if lt n 0
407ret
408end
409if not mc_val tmp ulong n
410mc_bad 0 1
411end
412end
413
414note mc_int: integer argument (JSON unsigned integer >= 1) as option flag
415fn mc_int src view uchar * tape view uint * flag view uchar * to void
416v = get gv 0
417if eq v jsnone
418ret
419end
420if ne js_type tape v jsnum
421mc_bad 0 1
422ret
423end
424if not js_isu src js_off tape v
425mc_bad 0 1
426ret
427end
428x = js_tou src js_off tape v
429if or eq x 0 gt x 99999999
430mc_bad 0 1
431ret
432end
433if not mc_flag flag
434ret
435end
436m = syn_mark
437syn_pd x
438syn_ap m syn_len m
439syn_rew m
440end
441
442note mc_bool: boolean argument; true adds the flag
443fn mc_bool tape view uint * flag view uchar * to void
444v = get gv 0
445if eq v jsnone
446ret
447end
448t = js_type tape v
449if eq t jstrue
450r = mc_flag flag
451ret
452end
453if ne t jsfalse
454mc_bad 0 1
455end
456end
457
458note mc_evid: array of strings; each element becomes --evidence-style flag mfevid + value
459fn mc_evid src view uchar * tape view uint * tmp edit uchar * to void
460v = get gv 0
461if eq v jsnone
462ret
463end
464if ne js_type tape v jsarr
465mc_bad 0 1
466ret
467end
468e = js_first tape v
469while ne e jsnone bound 999999
470if ne js_type tape e jsstr
471mc_bad 0 1
472ret
473end
474n = js_sget src js_off tape e tmp
475if lt n 0
476mc_bad 0 1
477ret
478end
479if not mc_flag mfevid
480ret
481end
482if not mc_val tmp ulong n
483mc_bad 0 1
484ret
485end
486e = js_enext tape e
487end
488end
489
490note mc_req1: mark the next mc_sval as required
491fn mc_req1 to void
492put gq 1 1
493end
494
495note mc_bin: start an argv for CLI t in bin-dir, then --home H when home is set
496fn mc_bin t view uchar * home bool to void
497syn_ainit
498m = syn_mark
499syn_ps get gop 0 get gopl 0
500syn_pt t
501syn_ap m syn_len m
502syn_rew m
503if home
504syn_at mhome
505syn_ps get gop 1 get gopl 1
506m2 = sub syn_mark get gopl 1
507syn_ap m2 get gopl 1
508syn_rew m2
509end
510end
511
512note mc_done: true when the arguments were all consumed and valid, else sends -32602
513fn mc_done tape view uint * a ulong to bool
514if ne a jsnone
515if and eq get gbad 0 0 ne get gq 0 js_nkids tape a
516put gbad 0 2
517end
518end
519w = get gbad 0
520if eq w 0
521ret true
522end
523if eq w 2
524mc_err 32602 mbadunk
525ret false
526end
527if eq w 3
528mc_err 32602 mbadlong
529ret false
530end
531if eq w 4
532mc_err 32602 mbadobj
533ret false
534end
535mc_err 32602 mbadarg
536ret false
537end
538
539note ======== running ========
540note mc_runap: run gav, append stdout, exit line and (on failure) stderr, all JSON-escaped; returns exit code
541fn mc_runap obf edit uchar * to ulong
542n = syn_run obf 300000000000
543c = ulong get gxs 0
544mc_rout obf n c
545ret c
546end
547
548note mc_rout: append n bytes of stdout, the exit line and (on failure) stderr of the last syn_run
549fn mc_rout obf view uchar * n ulong c ulong to void
550mc_qnl obf n
551mc_qt mexitc
552syn_pd c
553mc_qt mcl
554mc_qb 10
555if gt get gxs 1 long n
556mc_qt mtrunc
557end
558if ne c 0
559mc_qerr
560end
561end
562
563note mc_qnl: append obf[0..n) JSON-escaped, ending with a newline when it is not empty
564note (and is eager in Omega, so the n > 0 guard is a separate if)
565fn mc_qnl obf view uchar * n ulong to void
566mc_qa obf 0 n
567if gt n 0
568if ne get obf sub n 1 10
569mc_qb 10
570end
571end
572end
573
574fn mc_tail c ulong to void
575syn_pt mriserr
576if and ne c 0 and ne c 1 ne c 2
577syn_pt mtrue
578else
579syn_pt mfalse
580end
581syn_pb 125
582syn_pb 125
583syn_pb 10
584end
585
586note mc_exec: run the prepared argv and send the tool result
587fn mc_exec to void
588obf = new uchar 1048576 0
589m = mc_head
590syn_pt mrres
591syn_pt mrtext
592c = mc_runap obf
593mc_tail c
594mc_flush m
595end
596
597note coord_where: map where, then claims who repo:R for the first three result repos
598fn mc_where src view uchar * tape edit uint * tmp edit uchar * to void
599a = get gobj 0
600mc_bin mbmap true
601syn_at mcwhere
602mc_req1
603mc_get src tape maktask
604sv = mc_sval src tape tmp
605mc_spos tmp sv
606mc_get src tape maklimit
607mc_int src tape mflimit
608if not mc_done tape a
609ret
610end
611mc_wrun tape tmp
612end
613
614note mc_wrun: second half of coord_where (split out to stay within omc's 16 registers). The who targets are
615note collected in the pool before the response starts, so they never land inside it.
616fn mc_wrun tape edit uint * tmp edit uchar * to void
617obf = new uchar 1048576 0
618n = syn_run obf 300000000000
619c = ulong get gxs 0
620r = js_init tape 65536 16
621rp = syn_mark
622k = mc_wscan part obf 0 n tape tmp
623m = mc_head
624syn_pt mrres
625syn_pt mrtext
626mc_rout obf n c
627mc_who3 obf rp k
628mc_tail c
629mc_flush m
630syn_rew rp
631end
632
633note mc_wscan: per newline-terminated line of out (first three with a repo), mc_wrepo; returns how many appended
634fn mc_wscan lns view uchar * tape edit uint * tmp edit uchar * to ulong
635n = len lns
636k = ulong 0
637s = ulong 0
638for i ulong add n 1
639nl = eq i n
640if not nl
641nl = eq get lns i 10
642end
643if nl
644if and gt i s lt k 3
645k = add k mc_wrepo part lns s sub i s tape tmp
646end
647s = add i 1
648end
649end
650ret k
651end
652
653note mc_wrepo: when line ln parses and has a string "repo", append "claims who repo:R" target; 1 if appended
654fn mc_wrepo ln view uchar * tape edit uint * tmp edit uchar * to ulong
655q = js_parse ln tape len ln
656if gt q 0
657v = pu_rmemb ln tape mkrepo
658if and ne v jsnone eq js_type tape v jsstr
659x = js_sget ln js_off tape v tmp
660if gt x 0
661syn_pt mrepoc
662syn_pa tmp 0 ulong x
663syn_pb 10
664ret 1
665end
666end
667end
668ret 0
669end
670
671note pu_rmemb: value token of key in the root object, or jsnone
672fn pu_rmemb src view uchar * tape view uint * key view uchar * to ulong
673if ne js_type tape 0 jsobj
674ret jsnone
675end
676k = js_first tape 0
677while ne k jsnone bound 999999
678if js_streq src js_off tape k key
679ret add k 1
680end
681k = js_mnext tape k
682end
683ret jsnone
684end
685
686note mc_who3: for each newline-terminated target in the pool from rp (k of them), run claims who
687fn mc_who3 obf edit uchar * rp ulong k ulong to void
688put gq 2 rp
689put gq 3 0
690for j ulong k
691p = get gq 2
692e = syn_find p 4096 0 10
693mc_bin mbclaims true
694syn_at mcwho
695syn_ap p e
696put gq 2 add add p e 1
697mc_who1 obf p e
698end
699end
700
701fn mc_who1 obf edit uchar * p ulong e ulong to void
702n = syn_run obf 300000000000
703c = ulong get gxs 0
704mc_qnl obf n
705mc_qt mwhoc
706mc_qp p e
707mc_qt mwhoe
708syn_pd c
709mc_qt mcl
710mc_qb 10
711if ne c 0
712mc_qerr
713end
714end
715
716note coord_board: claims list --all into HOME/board/claims.jsonl, then board render
717fn mc_board tape view uint * a ulong to void
718if not mc_done tape a
719ret
720end
721obf = new uchar 1048576 0
722m = mc_head
723syn_pt mrres
724syn_pt mrtext
725mc_bin mbclaims true
726syn_at mclist
727syn_at mfall
728n = syn_run obf 300000000000
729c = ulong get gxs 0
730if or ne c 0 gt get gxs 1 long n
731mc_qt mlisterr
732mc_qb 10
733mc_qerr
734mc_tail 70
735mc_flush m
736ret
737end
738d = syn_mark
739syn_ps get gop 1 get gopl 1
740syn_pt mslboard
741r = syn_mkdir d syn_len d
742syn_rew d
743if not mc_bwrite obf n
744mc_qt mwrerr
745mc_qb 10
746mc_tail 70
747mc_flush m
748ret
749end
750mc_bin mbboard false
751syn_at mcrender
752mc_barg mfclaims mslbcl
753mc_barg mfmap mslmap
754mc_barg mffind mslfind
755mc_barg mfout mslboard
756c = mc_runap obf
757mc_tail c
758mc_flush m
759end
760
761note mc_bpath: HOME + suffix t appended to the pool; gq 2, 3 = offset, length. The pool also holds the
762note response being built, so every caller rewinds to gq 2 once the path is used.
763fn mc_bpath t view uchar * to void
764p = syn_mark
765syn_ps get gop 1 get gopl 1
766syn_pt t
767put gq 2 p
768put gq 3 syn_len p
769end
770
771note mc_barg: argv records flag f and HOME + suffix t
772fn mc_barg f view uchar * t view uchar * to void
773syn_at f
774mc_bpath t
775syn_ap get gq 2 get gq 3
776syn_rew get gq 2
777end
778
779note mc_bwrite: write obf[0..n) to HOME/board/claims.jsonl (temp file, sync, rename); false on any failure
780fn mc_bwrite obf view uchar * n ulong to bool
781mc_bpath mslbcl
782fd = syn_wbeg get gq 2 get gq 3
783if lt fd 0
784syn_rew get gq 2
785ret false
786end
787w = write int fd obf 0 n
788r = syn_wend fd get gq 2 get gq 3
789syn_rew get gq 2
790ret and eq w long n eq r 0
791end
792
793note ======== tools/call ========
794fn mc_call src view uchar * tape edit uint * tmp edit uchar * to void
795p = get gobj 0
796put gbad 0 0
797put gq 0 0
798put gq 1 0
799if or eq p jsnone ne js_type tape p jsobj
800mc_err 32602 mmeparm
801ret
802end
803put gobj 0 p
804nt = pu_memb2 src tape mkname
805if or eq nt jsnone ne js_type tape nt jsstr
806mc_err 32602 mmeparm
807ret
808end
809a = pu_memb2 src tape mkargs
810put gobj 0 a
811if ne a jsnone
812if ne js_type tape a jsobj
813mc_err 32602 mbadobj
814ret
815end
816end
817o = js_off tape nt
818if js_streq src o mtn01
819mc_where src tape tmp
820ret
821end
822if js_streq src o mtn13
823mc_board tape a
824ret
825end
826put gnm 0 o
827if not mc_build src tape tmp
828mc_err 32602 mmetool
829ret
830end
831if mc_done tape a
832mc_exec
833end
834end
835
836note pu_memb2: value token of key in object gobj 0, or jsnone
837fn pu_memb2 src view uchar * tape view uint * key view uchar * to ulong
838o = get gobj 0
839k = js_first tape o
840while ne k jsnone bound 999999
841if js_streq src js_off tape k key
842ret add k 1
843end
844k = js_mnext tape k
845end
846ret jsnone
847end
848
849note mc_build: argv for the single-exec tools; false for an unknown tool name
850fn mc_build src view uchar * tape view uint * tmp edit uchar * to bool
851o = get gnm 0
852if js_streq src o mtn02
853mc_bin mbmap true
854syn_at mctree
855mc_get src tape maktext
856mc_bool tape mftext
857ret true
858end
859if js_streq src o mtn03
860mc_bin mbmap true
861syn_at mccheck
862ret true
863end
864if js_streq src o mtn04
865mc_bin mbclaims true
866syn_at mcclaim
867mc_req1
868mc_get src tape makagent
869sv = mc_sval src tape tmp
870mc_semit tmp sv mfagent
871mc_req1
872mc_get src tape maksess
873sv = mc_sval src tape tmp
874mc_semit tmp sv mfsess
875mc_req1
876mc_get src tape maktarg
877sv = mc_sval src tape tmp
878mc_semit tmp sv mftarg
879mc_get src tape makttl
880mc_int src tape mfttl
881mc_get src tape makmode
882sv = mc_sval src tape tmp
883mc_semit tmp sv mfmode
884mc_get src tape makstate
885sv = mc_sval src tape tmp
886mc_semit tmp sv mfstate
887mc_get src tape makint
888sv = mc_sval src tape tmp
889mc_semit tmp sv mfint
890mc_get src tape makevid
891mc_evid src tape tmp
892ret true
893end
894if js_streq src o mtn05
895mc_bin mbclaims true
896syn_at mcrenew
897mc_req1
898mc_get src tape makagent
899sv = mc_sval src tape tmp
900mc_semit tmp sv mfagent
901mc_req1
902mc_get src tape makid
903sv = mc_sval src tape tmp
904mc_semit tmp sv mfid
905mc_get src tape makttl
906mc_int src tape mfttl
907ret true
908end
909if js_streq src o mtn06
910mc_bin mbclaims true
911syn_at mcrel
912mc_req1
913mc_get src tape makagent
914sv = mc_sval src tape tmp
915mc_semit tmp sv mfagent
916mc_req1
917mc_get src tape makid
918sv = mc_sval src tape tmp
919mc_semit tmp sv mfid
920mc_get src tape makstate
921sv = mc_sval src tape tmp
922mc_semit tmp sv mfstate
923mc_get src tape makevid
924mc_evid src tape tmp
925ret true
926end
927if js_streq src o mtn07
928mc_bin mbclaims true
929syn_at mcstate
930mc_req1
931mc_get src tape makagent
932sv = mc_sval src tape tmp
933mc_semit tmp sv mfagent
934mc_req1
935mc_get src tape makid
936sv = mc_sval src tape tmp
937mc_semit tmp sv mfid
938mc_req1
939mc_get src tape makstate
940sv = mc_sval src tape tmp
941mc_semit tmp sv mfstate
942mc_get src tape makevid
943mc_evid src tape tmp
944ret true
945end
946if js_streq src o mtn08
947mc_bin mbclaims true
948syn_at mcwho
949mc_req1
950mc_get src tape maktarg
951sv = mc_sval src tape tmp
952mc_spos tmp sv
953ret true
954end
955if js_streq src o mtn09
956mc_bin mbclaims true
957syn_at mclist
958mc_get src tape makall
959mc_bool tape mfall
960ret true
961end
962if js_streq src o mtn10
963mc_bin mbclaims true
964syn_at mcconf
965ret true
966end
967if js_streq src o mtn11
968mc_bin mbclaims true
969syn_at mcver
970ret true
971end
972if js_streq src o mtn12
973mc_bin mbpulse true
974syn_at mcrun
975syn_at mfcbin
976m = syn_mark
977syn_ps get gop 0 get gopl 0
978syn_pt mbclaims
979syn_ap m syn_len m
980syn_rew m
981mc_get src tape makalert
982mc_bool tape mfalert
983ret true
984end
985if js_streq src o mtn14
986mc_bin mbclaims true
987syn_at mclist
988syn_at mfprefix
989mc_req1
990mc_get src tape maklane
991sv = mc_sval src tape tmp
992mc_spos tmp sv
993ret true
994end
995ret false
996end
997
998note ======== one request line ========
999fn mc_req src view uchar * tape edit uint * tmp edit uchar * to void
1000base = syn_mark
1001put gid 1 0
1002r = js_parse src tape len src
1003if lt r 0
1004mc_err 32700 mmeparse
1005ret
1006end
1007if ne js_type tape 0 jsobj
1008mc_err 32600 mmeinv
1009ret
1010end
1011iv = pu_rmemb src tape mkid
1012if ne iv jsnone
1013t = js_type tape iv
1014if and and ne t jsstr ne t jsnum ne t jsnull
1015mc_err 32600 mmeinv
1016ret
1017end
1018m = syn_mark
1019syn_pa src js_off tape iv js_span tape iv
1020put gid 0 m
1021put gid 1 js_span tape iv
1022end
1023mv = pu_rmemb src tape mkmeth
1024jv = pu_rmemb src tape mkjrpc
1025ok = and ne mv jsnone ne jv jsnone
1026if ok
1027ok = and eq js_type tape mv jsstr eq js_type tape jv jsstr
1028end
1029if ok
1030ok = js_streq src js_off tape jv mk20
1031end
1032if eq iv jsnone
1033syn_rew base
1034ret
1035end
1036if not ok
1037mc_err 32600 mmeinv
1038syn_rew base
1039ret
1040end
1041o = js_off tape mv
1042if js_streq src o mminit
1043mc_okt mrinit
1044else
1045if js_streq src o mmping
1046mc_okt mrempty
1047else
1048if js_streq src o mmlist
1049mc_list
1050else
1051if js_streq src o mmcall
1052pv = pu_rmemb src tape mkpar
1053put gobj 0 pv
1054mc_call src tape tmp
1055else
1056mc_err 32601 mmenome
1057end
1058end
1059end
1060end
1061syn_rew base
1062end
1063
1064note ======== main ========
1065note mc_pre: b starts with t (caller checks the length)
1066fn mc_pre b view uchar * t view uchar * to bool
1067for j ulong len t
1068if ne get b j get t j
1069ret false
1070end
1071end
1072ret true
1073end
1074
1075note mc_envopt: option i = the environment value after prefix t; false when no variable has that prefix
1076fn mc_envopt i ulong t view uchar * to bool
1077b = new uchar 8192 0
1078for e ulong ulong envcnt
1079n = envread e b 0 len b
1080if gt n long len t
1081if mc_pre b t
1082m = syn_mark
1083syn_pa b len t sub ulong n len t
1084put gop i m
1085put gopl i syn_len m
1086ret true
1087end
1088end
1089end
1090ret false
1091end
1092
1093fn mc_args to void
1094n = ulong argcnt
1095i = ulong 1
1096while lt i n bound 1000
1097o = syn_arg i
1098l = ulong get gxs 2
1099k = ulong 99
1100if syn_seq o l mbindir
1101k = 0
1102end
1103if syn_seq o l mhome
1104k = 1
1105end
1106if or eq k 99 ge add i 1 n
1107syn_die musage 64
1108end
1109v = syn_arg add i 1
1110vl = ulong get gxs 2
1111if or eq vl 0 ne get gsp v 47
1112syn_die musage 64
1113end
1114put gop k v
1115put gopl k vl
1116i = add i 2
1117end
1118if eq get gopl 0 0
1119syn_die mnobin 64
1120end
1121if eq get gopl 1 0
1122if not mc_envopt 1 menvh
1123if not mc_envopt 1 menvhm
1124syn_die mnohome 64
1125end
1126syn_pt mdhome
1127put gopl 1 syn_len get gop 1
1128end
1129end
1130if eq get gopl 1 0
1131syn_die mnohome 64
1132end
1133end
1134
1135fn mc_loop to void
1136buf = new uchar 1048576 0
1137tape = new uint 524288 0
1138tmp = new uchar 65536 0
1139put gln 0 0
1140put gln 1 0
1141eof = false
1142note bound is 1..999999 per loop; 4295 x 999999 reads keeps the original 4294967295 limit
1143while not eof bound 4295
1144while not eof bound 999999
1145eof = mc_chunk buf tape tmp
1146end
1147end
1148bl = get gln 0
1149if and gt bl 0 eq get gln 1 0
1150mc_line part buf 0 bl tape tmp
1151end
1152end
1153
1154note mc_chunk: read one block of stdin and answer each complete line; true at end of input
1155note gln 0: bytes of the pending line in buf; gln 1: 1 when that line overflowed buf
1156fn mc_chunk buf edit uchar * tape edit uint * tmp edit uchar * to bool
1157rd = new uchar 65536 0
1158eof = false
1159r = read 0 rd 0 len rd
1160if le r 0
1161eof = true
1162r = 0
1163end
1164for i ulong ulong r
1165c = get rd i
1166bl = get gln 0
1167if eq c 10
1168if eq get gln 1 1
1169put gid 1 0
1170mc_err 32700 mmeparse
1171else
1172mc_line part buf 0 bl tape tmp
1173end
1174put gln 0 0
1175put gln 1 0
1176else
1177if lt bl len buf
1178put buf bl c
1179put gln 0 add bl 1
1180else
1181put gln 1 1
1182end
1183end
1184end
1185ret eof
1186end
1187
1188note mc_line: strip a trailing CR, skip blank lines, parse and answer
1189fn mc_line buf view uchar * tape edit uint * tmp edit uchar * to void
1190n = len buf
1191if gt n 0
1192if eq get buf sub n 1 13
1193n = sub n 1
1194end
1195end
1196blank = true
1197for i ulong n
1198c = get buf i
1199if and and ne c 32 ne c 9 ne c 13
1200blank = false
1201end
1202end
1203if blank
1204ret
1205end
1206r = js_init tape 983040 64
1207mc_req part buf 0 n tape tmp
1208end
1209
1210fn main to int
1211mc_args
1212mc_loop
1213ret 0
1214end