interplane.Get Omega
Read in your language

aegis-runtime-mv

Native Rust agent runtime: Axum WebSocket/SSE/REST gateway, heartbeat scheduler, dependency-aware task workflow, SQLite storage. Attachment of AienOS. Pre-alpha.

Brought by drakestapleton

branch main Branches Tags History

aegis-runtime-mv / om / main.om

654 lines · 11872 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note core/aegis/main.om: Standalone AEGIS runtime CLI entrypoint over stdin/stdout (pure OM-2).
3note Compose with:
4note si/directives/om2.om om/tls/sha2.om core/aegis/dag.om core/aegis/heartbeat.om
5note core/aegis/policy.om core/aegis/journal.om core/aegis/gateway.om core/aegis/main.om
6note
7note Line commands (stdin):
8note INIT <max_conc> <agent_ttl> <lease_ttl>
9note TOKEN <32..64_ascii_token>
10note AGENT <agent_id> <cap>
11note REQ <METHOD> <PATH> <TOKEN_OR_-> [BODY...]
12note LEASE <task_id> <agent_id>
13note RENEW <task_id> <agent_id> <gen>
14note DONE <task_id> <agent_id> <gen>
15note FAIL <task_id> <agent_id> <gen> <fail_code>
16note TICK <tick>
17note VERIFY
18note QUIT
19
20text cli_cmd_init "INIT"
21text cli_cmd_tok "TOKEN"
22text cli_cmd_agent "AGENT"
23text cli_cmd_req "REQ"
24text cli_cmd_lease "LEASE"
25text cli_cmd_renew "RENEW"
26text cli_cmd_done "DONE"
27text cli_cmd_fail "FAIL"
28text cli_cmd_tick "TICK"
29text cli_cmd_verify "VERIFY"
30text cli_cmd_quit "QUIT"
31text cli_dash "-"
32text cli_ok "OK\n"
33text cli_err "ERR "
34text cli_status "STATUS "
35text cli_leased "LEASED gen="
36text cli_tick_out "TICK "
37text cli_recl " reclaimed="
38text cli_chain_ok "CHAIN OK seq="
39text cli_chain_err "CHAIN ERR "
40text cli_nl "\n"
41text cli_jrn_cli "cli"
42
43global cli_line uchar 1024
44global cli_inbuf uchar 1024
45global cli_outbuf uchar 256
46
47fn cli_write_all buf view uchar * n ulong to void
48off = ulong 0
49while lt off n bound 1024
50k = write 1 buf off sub n off
51if le k 0
52break
53end
54off = add off ulong k
55end
56end
57
58fn cli_write_u64 v ulong to void
59buf = new uchar 24 0
60if eq v 0
61put buf 0 uchar 48
62cli_write_all part buf 0 1 1
63ret
64end
65tmp = new uchar 24 0
66cnt = ulong 0
67x = v
68while gt x 0 bound 24
69put tmp cnt uchar add 48 rem x 10
70cnt = add cnt 1
71x = div x 10
72end
73for i ulong cnt bound 24
74put buf i get tmp sub sub cnt 1 i
75end
76cli_write_all part buf 0 cnt cnt
77end
78
79fn cli_print_err code ulong to void
80cli_write_all cli_err len cli_err
81cli_write_u64 code
82cli_write_all cli_nl len cli_nl
83end
84
85fn cli_write_gw_resp to void
86n = gw_resp_len
87off = ulong 0
88while lt off n bound 1024
89k = write 1 gw_resp off sub n off
90if le k 0
91break
92end
93off = add off ulong k
94end
95cli_write_all cli_nl len cli_nl
96end
97
98note Skip spaces starting at pos in cli_line[0..llen).
99fn cli_skip_sp pos ulong llen ulong to ulong
100p = pos
101while lt p llen bound 1024
102if or eq get cli_line p 32 eq get cli_line p 9
103p = add p 1
104else
105break
106end
107end
108ret p
109end
110
111note Find end of whitespace-delimited token starting at pos.
112fn cli_tok_end pos ulong llen ulong to ulong
113p = pos
114while lt p llen bound 1024
115c = get cli_line p
116if or eq c 32 or eq c 9 or eq c 10 eq c 13
117break
118end
119p = add p 1
120end
121ret p
122end
123
124fn cli_tok_eq start ulong endp ulong s view uchar * to bool
125tlen = sub endp start
126if ne tlen len s
127ret false
128end
129for i ulong tlen bound 64
130if ne get cli_line add start i get s i
131ret false
132end
133end
134ret true
135end
136
137note Parse decimal u64 from cli_line[start..endp). Writes value to dst[0]; returns true iff valid.
138fn cli_parse_u64 start ulong endp ulong dst edit ulong 1 to bool
139put dst 0 0
140if ge start endp
141ret false
142end
143n = sub endp start
144if gt n 19
145ret false
146end
147v = ulong 0
148for i ulong n bound 20
149c = get cli_line add start i
150if or lt c 48 gt c 57
151ret false
152end
153v = add mul v 10 ulong sub c 48
154end
155put dst 0 v
156ret true
157end
158
159fn cli_exec_init p0 ulong llen ulong to void
160a1 = new ulong 1 0
161a2 = new ulong 1 0
162a3 = new ulong 1 0
163p = cli_skip_sp p0 llen
164e1 = cli_tok_end p llen
165if not cli_parse_u64 p e1 a1
166cli_print_err 1
167ret
168end
169p = cli_skip_sp e1 llen
170e2 = cli_tok_end p llen
171if not cli_parse_u64 p e2 a2
172cli_print_err 1
173ret
174end
175p = cli_skip_sp e2 llen
176e3 = cli_tok_end p llen
177if not cli_parse_u64 p e3 a3
178cli_print_err 1
179ret
180end
181dag_reset
182jrn_reset
183gw_reset
184rc = hb_init get a1 0 get a2 0 get a3 0
185if ne rc HB_OK
186cli_print_err rc
187ret
188end
189cli_write_all cli_ok len cli_ok
190end
191
192fn cli_exec_token p0 ulong llen ulong to void
193p = cli_skip_sp p0 llen
194e = cli_tok_end p llen
195tlen = sub e p
196if or eq tlen 0 gt tlen 64
197cli_print_err POL_DENY_BAD_TOKEN
198ret
199end
200tb = new uchar 64 0
201for i ulong tlen bound 64
202put tb i get cli_line add p i
203end
204rc = pol_set_operator_token part tb 0 tlen
205if ne rc POL_ALLOW
206cli_print_err rc
207ret
208end
209cli_write_all cli_ok len cli_ok
210end
211
212fn cli_exec_agent p0 ulong llen ulong to void
213a1 = new ulong 1 0
214a2 = new ulong 1 0
215p = cli_skip_sp p0 llen
216e1 = cli_tok_end p llen
217if not cli_parse_u64 p e1 a1
218cli_print_err 1
219ret
220end
221p = cli_skip_sp e1 llen
222e2 = cli_tok_end p llen
223if not cli_parse_u64 p e2 a2
224cli_print_err 1
225ret
226end
227rc = hb_register_agent get a1 0 get a2 0
228if ne rc HB_OK
229cli_print_err rc
230ret
231end
232cli_write_all cli_ok len cli_ok
233end
234
235fn cli_req_stage1 p0 ulong llen ulong to long
236mb = new uchar 16 0
237pb = new uchar 128 0
238p = cli_skip_sp p0 llen
239e1 = cli_tok_end p llen
240mlen = sub e1 p
241if or eq mlen 0 gt mlen 16
242cli_print_err 400
243ret -1
244end
245for i ulong mlen bound 16
246put mb i get cli_line add p i
247end
248p = cli_skip_sp e1 llen
249e2 = cli_tok_end p llen
250plen = sub e2 p
251if or eq plen 0 gt plen 128
252cli_print_err 400
253ret -1
254end
255for i ulong plen bound 128
256put pb i get cli_line add p i
257end
258rt = gw_req_begin part mb 0 mlen part pb 0 plen
259if gt rt 999
260cli_print_err 400
261ret -1
262end
263ret long e2
264end
265
266fn cli_req_stage2 p0 ulong llen ulong to long
267tb = new uchar 64 0
268p = cli_skip_sp p0 llen
269e3 = cli_tok_end p llen
270tlen = sub e3 p
271if gt tlen 64
272cli_print_err 401
273ret -1
274end
275if cli_tok_eq p e3 cli_dash
276tlen = 0
277else
278for i ulong tlen bound 64
279put tb i get cli_line add p i
280end
281end
282gw_req_auth 0 ROLE_OPERATOR part tb 0 tlen true
283ret long e3
284end
285
286fn cli_exec_req p0 ulong llen ulong to void
287bb = new uchar 512 0
288n1 = cli_req_stage1 p0 llen
289if lt n1 0
290ret
291end
292n2 = cli_req_stage2 ulong n1 llen
293if lt n2 0
294ret
295end
296p = cli_skip_sp ulong n2 llen
297blen = sub llen p
298if gt blen 512
299blen = 512
300end
301for i ulong blen bound 512
302put bb i get cli_line add p i
303end
304st = gw_req_dispatch part bb 0 blen
305cli_write_all cli_status len cli_status
306cli_write_u64 st
307cli_write_all cli_nl len cli_nl
308cli_write_gw_resp
309end
310
311fn cli_lease_apply tid ulong aid ulong to void
312gen = new ulong 1 0
313rdy = dag_solve_ready
314if gt rdy DAG_MAX_TASKS
315cli_print_err 1
316ret
317end
318if ne dag_get_state tid TASK_READY
319cli_print_err DAG_ERR_INVALID_TRANSITION
320ret
321end
322lrc = hb_acquire_lease tid aid gen
323if ne lrc HB_OK
324cli_print_err lrc
325ret
326end
327src = dag_start_task tid
328if ne src DAG_OK
329rl = hb_release_lease tid aid get gen 0
330if gt rl 999
331cli_print_err rl
332ret
333end
334cli_print_err src
335ret
336end
337j1 = jrn_append EVT_LEASE_ACQUIRED tid get gen 0 hb_current_tick cli_jrn_cli
338if ne j1 JRN_OK
339cli_print_err JRN_ERR_FULL
340ret
341end
342j2 = jrn_append EVT_TASK_STARTED tid aid hb_current_tick cli_jrn_cli
343if ne j2 JRN_OK
344cli_print_err JRN_ERR_FULL
345ret
346end
347cli_write_all cli_leased len cli_leased
348cli_write_u64 get gen 0
349cli_write_all cli_nl len cli_nl
350end
351
352fn cli_exec_lease p0 ulong llen ulong to void
353a = new ulong 1 0
354p = cli_skip_sp p0 llen
355e = cli_tok_end p llen
356if not cli_parse_u64 p e a
357cli_print_err 1
358ret
359end
360tid = get a 0
361p = cli_skip_sp e llen
362e = cli_tok_end p llen
363if not cli_parse_u64 p e a
364cli_print_err 1
365ret
366end
367cli_lease_apply tid get a 0
368end
369
370fn cli_exec_renew p0 ulong llen ulong to void
371a = new ulong 1 0
372p = cli_skip_sp p0 llen
373e = cli_tok_end p llen
374if not cli_parse_u64 p e a
375cli_print_err 1
376ret
377end
378tid = get a 0
379p = cli_skip_sp e llen
380e = cli_tok_end p llen
381if not cli_parse_u64 p e a
382cli_print_err 1
383ret
384end
385aid = get a 0
386p = cli_skip_sp e llen
387e = cli_tok_end p llen
388if not cli_parse_u64 p e a
389cli_print_err 1
390ret
391end
392rc = hb_renew_lease tid aid get a 0
393if ne rc HB_OK
394cli_print_err rc
395ret
396end
397cli_write_all cli_ok len cli_ok
398end
399
400fn cli_done_apply tid ulong aid ulong gen ulong to void
401vrc = hb_verify_lease tid aid gen
402if ne vrc HB_OK
403cli_print_err vrc
404ret
405end
406drc = dag_complete_task tid
407if ne drc DAG_OK
408cli_print_err drc
409ret
410end
411rrc = hb_release_lease tid aid gen
412if ne rrc HB_OK
413cli_print_err rrc
414ret
415end
416jrc = jrn_append EVT_TASK_DONE tid gen hb_current_tick cli_jrn_cli
417if ne jrc JRN_OK
418cli_print_err jrc
419ret
420end
421cli_write_all cli_ok len cli_ok
422end
423
424fn cli_exec_done p0 ulong llen ulong to void
425a = new ulong 1 0
426p = cli_skip_sp p0 llen
427e = cli_tok_end p llen
428if not cli_parse_u64 p e a
429cli_print_err 1
430ret
431end
432tid = get a 0
433p = cli_skip_sp e llen
434e = cli_tok_end p llen
435if not cli_parse_u64 p e a
436cli_print_err 1
437ret
438end
439aid = get a 0
440p = cli_skip_sp e llen
441e = cli_tok_end p llen
442if not cli_parse_u64 p e a
443cli_print_err 1
444ret
445end
446cli_done_apply tid aid get a 0
447end
448
449fn cli_fail_apply tid ulong aid ulong gen ulong fc ulong to void
450vrc = hb_verify_lease tid aid gen
451if ne vrc HB_OK
452cli_print_err vrc
453ret
454end
455drc = dag_fail_task tid fc
456if ne drc DAG_OK
457cli_print_err drc
458ret
459end
460rrc = hb_release_lease tid aid gen
461if ne rrc HB_OK
462cli_print_err rrc
463ret
464end
465jrc = jrn_append EVT_TASK_FAILED tid fc hb_current_tick cli_jrn_cli
466if ne jrc JRN_OK
467cli_print_err jrc
468ret
469end
470cli_write_all cli_ok len cli_ok
471end
472
473fn cli_exec_fail p0 ulong llen ulong to void
474a = new ulong 1 0
475p = cli_skip_sp p0 llen
476e = cli_tok_end p llen
477if not cli_parse_u64 p e a
478cli_print_err 1
479ret
480end
481tid = get a 0
482p = cli_skip_sp e llen
483e = cli_tok_end p llen
484if not cli_parse_u64 p e a
485cli_print_err 1
486ret
487end
488aid = get a 0
489p = cli_skip_sp e llen
490e = cli_tok_end p llen
491if not cli_parse_u64 p e a
492cli_print_err 1
493ret
494end
495gen = get a 0
496p = cli_skip_sp e llen
497e = cli_tok_end p llen
498if not cli_parse_u64 p e a
499cli_print_err 1
500ret
501end
502cli_fail_apply tid aid gen get a 0
503end
504
505fn cli_exec_tick p0 ulong llen ulong to void
506a1 = new ulong 1 0
507p = cli_skip_sp p0 llen
508e1 = cli_tok_end p llen
509if not cli_parse_u64 p e1 a1
510cli_print_err 1
511ret
512end
513tk = get a1 0
514rc = hb_tick tk
515if ne rc HB_OK
516cli_print_err rc
517ret
518end
519recl = hb_last_reclaimed
520for i ulong recl bound 128
521rtid = hb_get_reclaimed i
522if eq dag_get_state rtid TASK_RUNNING
523q = dag_requeue_task rtid
524if eq q DAG_OK
525j1 = jrn_append EVT_LEASE_RECLAIMED rtid tk tk cli_jrn_cli
526if ne j1 JRN_OK
527cli_print_err j1
528ret
529end
530end
531end
532end
533j2 = jrn_append EVT_HEARTBEAT_TICK 0 recl tk cli_jrn_cli
534if ne j2 JRN_OK
535cli_print_err j2
536ret
537end
538cli_write_all cli_tick_out len cli_tick_out
539cli_write_u64 tk
540cli_write_all cli_recl len cli_recl
541cli_write_u64 recl
542cli_write_all cli_nl len cli_nl
543end
544
545fn cli_exec_verify to void
546rc = jrn_verify_chain
547if ne rc JRN_OK
548cli_write_all cli_chain_err len cli_chain_err
549cli_write_u64 rc
550cli_write_all cli_nl len cli_nl
551ret
552end
553cli_write_all cli_chain_ok len cli_chain_ok
554cli_write_u64 jrn_last_seq
555cli_write_all cli_nl len cli_nl
556end
557
558note Process one line in cli_line[0..llen). Returns true if QUIT requested.
559fn cli_process_line llen ulong to bool
560p = cli_skip_sp 0 llen
561if ge p llen
562ret false
563end
564e = cli_tok_end p llen
565if cli_tok_eq p e cli_cmd_quit
566ret true
567end
568if cli_tok_eq p e cli_cmd_init
569cli_exec_init e llen
570ret false
571end
572if cli_tok_eq p e cli_cmd_tok
573cli_exec_token e llen
574ret false
575end
576if cli_tok_eq p e cli_cmd_agent
577cli_exec_agent e llen
578ret false
579end
580if cli_tok_eq p e cli_cmd_req
581cli_exec_req e llen
582ret false
583end
584if cli_tok_eq p e cli_cmd_lease
585cli_exec_lease e llen
586ret false
587end
588if cli_tok_eq p e cli_cmd_renew
589cli_exec_renew e llen
590ret false
591end
592if cli_tok_eq p e cli_cmd_done
593cli_exec_done e llen
594ret false
595end
596if cli_tok_eq p e cli_cmd_fail
597cli_exec_fail e llen
598ret false
599end
600if cli_tok_eq p e cli_cmd_tick
601cli_exec_tick e llen
602ret false
603end
604if cli_tok_eq p e cli_cmd_verify
605cli_exec_verify
606ret false
607end
608cli_print_err 400
609ret false
610end
611
612fn main to int
613dag_reset
614pol_reset
615jrn_reset
616gw_reset
617init_rc = hb_init 4 3 5
618if ne init_rc HB_OK
619ret 1
620end
621llen = ulong 0
622done = false
623while not done bound 4096
624k = read 0 cli_inbuf 0 1024
625if le k 0
626if gt llen 0
627q = cli_process_line llen
628if q
629done = true
630end
631end
632break
633end
634cnt = ulong k
635for i ulong cnt bound 1024
636b = get cli_inbuf i
637if eq b 10
638if cli_process_line llen
639done = true
640break
641end
642llen = 0
643else
644if ne b 13
645if lt llen 1024
646put cli_line llen b
647llen = add llen 1
648end
649end
650end
651end
652end
653ret 0
654end