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 / gateway.om

902 lines · 19317 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note core/aegis/gateway.om: AEGIS control-plane gateway router and JSON/SSE/WS formatter (pure OM-2).
3note Converted from aegis-runtime-mv/src/gateway.rs and src/auth.rs.
4note Routes: /health, /tasks, /tasks/submit, /heartbeat, /events, plus WS frame dispatch.
5note Enforces policy.om fail-closed authority gate on every request, strict input validation,
6note deterministic HTTP status codes, JSON/SSE response formatting, and journal auditing.
7
8const GW_RESP_CAP ulong 8192
9const GW_CT_JSON ulong 1
10const GW_CT_SSE ulong 2
11
12const HTTP_OK ulong 200
13const HTTP_CREATED ulong 201
14const HTTP_BAD_REQUEST ulong 400
15const HTTP_UNAUTHORIZED ulong 401
16const HTTP_FORBIDDEN ulong 403
17const HTTP_NOT_FOUND ulong 404
18const HTTP_METHOD_NOT_ALLOWED ulong 405
19const HTTP_CONFLICT ulong 409
20const HTTP_SERVICE_UNAVAILABLE ulong 503
21
22note gw_meta: [0]=status, [1]=resp_len, [2]=content_type, [3]=req_method, [4]=req_route,
23note [5]=req_actor, [6]=req_caps, [7]=req_tok_ok, [8]=req_env_ok, [9]=req_path_bad
24global gw_meta ulong 16
25global gw_resp uchar 8192
26
27text gw_m_get "GET"
28text gw_m_head "HEAD"
29text gw_m_post "POST"
30
31text gw_p_health "/health"
32text gw_p_tasks "/tasks"
33text gw_p_submit "/tasks/submit"
34text gw_p_hb "/heartbeat"
35text gw_p_events "/events"
36
37text gw_hex_digits "0123456789abcdef"
38
39text gw_st_pending "pending"
40text gw_st_ready "ready"
41text gw_st_running "running"
42text gw_st_done "done"
43text gw_st_failed "failed"
44text gw_st_blocked "blocked"
45text gw_st_cancelled "cancelled"
46text gw_st_unknown "unknown"
47
48text gw_ev_sub "task_submitted"
49text gw_ev_dep "task_dep"
50text gw_ev_start "task_started"
51text gw_ev_done "task_done"
52text gw_ev_fail "task_failed"
53text gw_ev_blk "task_blocked"
54text gw_ev_can "task_cancelled"
55text gw_ev_lacq "lease_acquired"
56text gw_ev_lrec "lease_reclaimed"
57text gw_ev_hb "heartbeat_tick"
58text gw_ev_pden "policy_denied"
59
60text gw_j_health0 "{\"status\":\"healthy\",\"engine\":\"aegis-runtime-om/0.2.0\",\"tick\":"
61text gw_j_health1 ",\"tasks\":"
62text gw_j_health2 ",\"active_leases\":"
63text gw_j_health3 ",\"journal_seq\":"
64text gw_j_health4 ",\"degraded\":"
65text gw_j_health5 ",\"zero_disk_secrets\":true}"
66
67text gw_j_true "true"
68text gw_j_false "false"
69text gw_j_tasks0 "{\"tasks\":["
70text gw_j_tasks1 "]}"
71text gw_j_titem0 "{\"id\":"
72text gw_j_titem1 ",\"state\":\""
73text gw_j_titem2 "\",\"priority\":"
74text gw_j_titem3 ",\"blocked_by\":"
75text gw_j_titem4 ",\"lease_owner\":"
76text gw_j_titem5 "}"
77
78text gw_j_sub0 "{\"status\":\"created\",\"task_id\":"
79text gw_j_sub1 ",\"state\":\""
80text gw_j_sub2 "\",\"journal_seq\":"
81text gw_j_sub3 "}"
82
83text gw_j_hb0 "{\"status\":\""
84text gw_j_hb_nom "nominal"
85text gw_j_hb_deg "degraded"
86text gw_j_hb1 "\",\"tick\":"
87text gw_j_hb2 ",\"pulse\":"
88text gw_j_hb3 ",\"reclaimed\":"
89text gw_j_hb4 ",\"active_leases\":"
90text gw_j_hb5 "}"
91
92text gw_sse_id "id: "
93text gw_sse_ev "\nevent: "
94text gw_sse_d0 "\ndata: {\"seq\":"
95text gw_sse_d1 ",\"tick\":"
96text gw_sse_d2 ",\"kind\":\""
97text gw_sse_d3 "\",\"task_id\":"
98text gw_sse_d4 ",\"aux\":"
99text gw_sse_d5 ",\"hash\":\""
100text gw_sse_d6 "\"}\n\n"
101
102text gw_err0 "{\"error\":\""
103text gw_err1 "\",\"decision_code\":"
104text gw_err2 "}"
105
106text gw_em_notok "operator_token_not_configured"
107text gw_em_unauth "operator_token_required"
108text gw_em_cap "missing_required_capability"
109text gw_em_env "unverified_action_envelope"
110text gw_em_esc "privilege_escalation_refused"
111text gw_em_cont "containment_active"
112text gw_em_route "route_not_found"
113text gw_em_meth "method_not_allowed"
114text gw_em_appr "operator_approval_required"
115text gw_em_badreq "invalid_request_payload"
116text gw_em_cycle "dependency_cycle_refused"
117text gw_em_dup "duplicate_task_id"
118text gw_em_conflict "state_conflict"
119text gw_em_full "runtime_capacity_exhausted"
120
121text gw_k_id "id"
122text gw_k_prio "prio"
123text gw_k_kind "kind"
124text gw_k_prereq "prereq"
125text gw_k_tick "tick"
126text gw_k_agent "agent"
127text gw_k_cap "cap"
128
129text gw_ws_ping "ping"
130text gw_ws_hb "heartbeat"
131text gw_ws_tasks "tasks"
132text gw_ws_pong0 "{\"type\":\"pong\",\"tick\":"
133text gw_ws_pong1 "}"
134text gw_ws_err "{\"type\":\"error\",\"error\":\"unknown_ws_frame\"}"
135text gw_jrn_gw "gw"
136
137fn gw_reset to void
138for i ulong 16
139put gw_meta i 0
140end
141end
142
143fn gw_status to ulong
144ret get gw_meta 0
145end
146
147fn gw_resp_len to ulong
148ret get gw_meta 1
149end
150
151fn gw_content_type to ulong
152ret get gw_meta 2
153end
154
155fn gw_buf_reset to void
156put gw_meta 1 0
157end
158
159fn gw_put_byte b uchar to void
160p = get gw_meta 1
161if lt p GW_RESP_CAP
162put gw_resp p b
163put gw_meta 1 add p 1
164end
165end
166
167fn gw_put_bytes s view uchar * to void
168n = len s
169for i ulong n bound 8192
170gw_put_byte get s i
171end
172end
173
174fn gw_put_u64 v ulong to void
175if eq v 0
176gw_put_byte uchar 48
177ret
178end
179digs = new uchar 24 0
180cnt = ulong 0
181x = v
182while gt x 0 bound 24
183d = rem x 10
184put digs cnt uchar add 48 d
185cnt = add cnt 1
186x = div x 10
187end
188while gt cnt 0 bound 24
189cnt = sub cnt 1
190gw_put_byte get digs cnt
191end
192end
193
194fn gw_put_hex32 h view uchar 32 to void
195for i ulong 32
196b = get h i
197hi = ulong shr b 4
198lo = ulong and b 15
199gw_put_byte get gw_hex_digits hi
200gw_put_byte get gw_hex_digits lo
201end
202end
203
204fn gw_put_task_state st ulong to void
205if eq st TASK_PENDING
206gw_put_bytes gw_st_pending
207ret
208end
209if eq st TASK_READY
210gw_put_bytes gw_st_ready
211ret
212end
213if eq st TASK_RUNNING
214gw_put_bytes gw_st_running
215ret
216end
217if eq st TASK_DONE
218gw_put_bytes gw_st_done
219ret
220end
221if eq st TASK_FAILED
222gw_put_bytes gw_st_failed
223ret
224end
225if eq st TASK_BLOCKED
226gw_put_bytes gw_st_blocked
227ret
228end
229if eq st TASK_CANCELLED
230gw_put_bytes gw_st_cancelled
231ret
232end
233gw_put_bytes gw_st_unknown
234end
235
236fn gw_put_event_kind k ulong to void
237if eq k EVT_TASK_SUBMITTED
238gw_put_bytes gw_ev_sub
239ret
240end
241if eq k EVT_TASK_DEP
242gw_put_bytes gw_ev_dep
243ret
244end
245if eq k EVT_TASK_STARTED
246gw_put_bytes gw_ev_start
247ret
248end
249if eq k EVT_TASK_DONE
250gw_put_bytes gw_ev_done
251ret
252end
253if eq k EVT_TASK_FAILED
254gw_put_bytes gw_ev_fail
255ret
256end
257if eq k EVT_TASK_BLOCKED
258gw_put_bytes gw_ev_blk
259ret
260end
261if eq k EVT_TASK_CANCELLED
262gw_put_bytes gw_ev_can
263ret
264end
265if eq k EVT_LEASE_ACQUIRED
266gw_put_bytes gw_ev_lacq
267ret
268end
269if eq k EVT_LEASE_RECLAIMED
270gw_put_bytes gw_ev_lrec
271ret
272end
273if eq k EVT_HEARTBEAT_TICK
274gw_put_bytes gw_ev_hb
275ret
276end
277if eq k EVT_POLICY_DENIED
278gw_put_bytes gw_ev_pden
279ret
280end
281gw_put_bytes gw_st_unknown
282end
283
284fn gw_set_error status ulong msg view uchar * code ulong to ulong
285gw_buf_reset
286put gw_meta 0 status
287put gw_meta 2 GW_CT_JSON
288gw_put_bytes gw_err0
289gw_put_bytes msg
290gw_put_bytes gw_err1
291gw_put_u64 code
292gw_put_bytes gw_err2
293ret status
294end
295
296fn gw_parse_method m view uchar * to ulong
297if pol_str_eq m gw_m_get
298ret METHOD_GET
299end
300if pol_str_eq m gw_m_head
301ret METHOD_HEAD
302end
303if pol_str_eq m gw_m_post
304ret METHOD_POST
305end
306ret METHOD_UNKNOWN
307end
308
309note Strict path validation: non-empty, <=128 bytes, printable ASCII without spaces/controls, no "..".
310fn gw_path_valid p view uchar * to bool
311n = len p
312if or eq n 0 gt n 128
313ret false
314end
315if ne get p 0 uchar 47
316ret false
317end
318prev_dot = false
319for i ulong n bound 128
320c = get p i
321if or lt c 0x21 gt c 0x7e
322ret false
323end
324if eq c uchar 46
325if prev_dot
326ret false
327end
328prev_dot = true
329else
330prev_dot = false
331end
332end
333ret true
334end
335
336fn gw_parse_route p view uchar * to ulong
337if not gw_path_valid p
338ret ROUTE_UNKNOWN
339end
340if pol_str_eq p gw_p_health
341ret ROUTE_HEALTH
342end
343if pol_str_eq p gw_p_tasks
344ret ROUTE_TASKS
345end
346if pol_str_eq p gw_p_submit
347ret ROUTE_TASKS_SUBMIT
348end
349if pol_str_eq p gw_p_hb
350ret ROUTE_HEARTBEAT
351end
352if pol_str_eq p gw_p_events
353ret ROUTE_EVENTS
354end
355ret ROUTE_UNKNOWN
356end
357
358note Validate request body has no NUL or unsafe control characters and fits within 512 bytes.
359fn gw_body_valid b view uchar * to bool
360n = len b
361if gt n 512
362ret false
363end
364for i ulong n bound 512
365c = get b i
366if and lt c 32 and ne c 10 and ne c 13 ne c 9
367ret false
368end
369if gt c 126
370ret false
371end
372end
373ret true
374end
375
376fn gw_is_key_delim c uchar to bool
377if eq c 32
378ret true
379end
380if eq c 44
381ret true
382end
383if eq c 123
384ret true
385end
386if eq c 34
387ret true
388end
389if eq c 38
390ret true
391end
392ret false
393end
394
395fn gw_is_val_delim c uchar to bool
396if eq c 32
397ret true
398end
399if eq c 44
400ret true
401end
402if eq c 125
403ret true
404end
405if eq c 38
406ret true
407end
408if or eq c 10 eq c 13
409ret true
410end
411ret false
412end
413
414note Check whether key matches body starting at pos, followed by '=' or '":' or ':'.
415note Returns offset right after separator, or -1 if no match.
416fn gw_match_key_at body view uchar * pos ulong key view uchar * to long
417nb = len body
418nk = len key
419if gt add pos nk nb
420ret -1
421end
422note Require boundary before key (start, space, comma, '{', '"', '&').
423if gt pos 0
424prev = get body sub pos 1
425if not gw_is_key_delim prev
426ret -1
427end
428end
429for i ulong nk bound 32
430if ne get body add pos i get key i
431ret -1
432end
433end
434p = add pos nk
435if lt p nb
436if eq get body p 34
437p = add p 1
438end
439end
440if ge p nb
441ret -1
442end
443sep = get body p
444if and ne sep 61 ne sep 58
445ret -1
446end
447p = add p 1
448while lt p nb bound 16
449if eq get body p 32
450p = add p 1
451else
452break
453end
454end
455ret long p
456end
457
458note Parse decimal u64 starting at offset p in body. Returns 1 if valid (written to dst[0]), 2 if malformed.
459fn gw_parse_u64_at body view uchar * p0 ulong dst edit ulong 1 to ulong
460nb = len body
461p = p0
462if ge p nb
463ret 2
464end
465first = get body p
466if or lt first 48 gt first 57
467ret 2
468end
469val = ulong 0
470digits = ulong 0
471while lt p nb bound 32
472c = get body p
473if and ge c 48 le c 57
474if gt digits 18
475ret 2
476end
477val = add mul val 10 ulong sub c 48
478digits = add digits 1
479p = add p 1
480else
481if gw_is_val_delim c
482break
483end
484ret 2
485end
486end
487put dst 0 val
488ret 1
489end
490
491note Extract a strict non-negative decimal u64 for `key` from `body`.
492note Returns: 0 = not found, 1 = found valid (written to dst[0]), 2 = malformed number.
493fn gw_extract_u64 body view uchar * key view uchar * dst edit ulong 1 to ulong
494put dst 0 0
495nb = len body
496for pos ulong nb bound 512
497m = gw_match_key_at body pos key
498if ge m 0
499ret gw_parse_u64_at body ulong m dst
500end
501end
502ret 0
503end
504
505note Stage 1: Parse and validate HTTP method and route path.
506fn gw_req_begin method view uchar * path view uchar * to ulong
507put gw_meta 3 gw_parse_method method
508put gw_meta 4 gw_parse_route path
509if gw_path_valid path
510put gw_meta 9 0
511else
512put gw_meta 9 1
513end
514put gw_meta 5 0
515put gw_meta 6 CAP_NONE
516put gw_meta 7 0
517put gw_meta 8 0
518ret get gw_meta 4
519end
520
521note Stage 2: Attach caller credentials and verify operator token in constant time.
522fn gw_req_auth actor_id ulong req_caps ulong token view uchar * env_ok bool to void
523put gw_meta 5 actor_id
524put gw_meta 6 req_caps
525if pol_verify_token token
526put gw_meta 7 1
527else
528put gw_meta 7 0
529end
530if env_ok
531put gw_meta 8 1
532else
533put gw_meta 8 0
534end
535end
536
537fn gw_handle_health to ulong
538gw_buf_reset
539put gw_meta 0 HTTP_OK
540put gw_meta 2 GW_CT_JSON
541gw_put_bytes gw_j_health0
542gw_put_u64 hb_current_tick
543gw_put_bytes gw_j_health1
544gw_put_u64 dag_task_count
545gw_put_bytes gw_j_health2
546gw_put_u64 hb_active_leases
547gw_put_bytes gw_j_health3
548gw_put_u64 jrn_last_seq
549gw_put_bytes gw_j_health4
550if hb_is_degraded
551gw_put_bytes gw_j_true
552else
553gw_put_bytes gw_j_false
554end
555gw_put_bytes gw_j_health5
556ret HTTP_OK
557end
558
559fn gw_handle_tasks to ulong
560gw_buf_reset
561put gw_meta 0 HTTP_OK
562put gw_meta 2 GW_CT_JSON
563gw_put_bytes gw_j_tasks0
564n = dag_task_count
565for i ulong n bound 128
566if gt i 0
567gw_put_byte uchar 44
568end
569tid = dag_slot_tid i
570gw_put_bytes gw_j_titem0
571gw_put_u64 tid
572gw_put_bytes gw_j_titem1
573gw_put_task_state dag_slot_state i
574gw_put_bytes gw_j_titem2
575gw_put_u64 dag_slot_prio i
576gw_put_bytes gw_j_titem3
577gw_put_u64 dag_slot_blocked_by i
578gw_put_bytes gw_j_titem4
579gw_put_u64 hb_lease_owner tid
580gw_put_bytes gw_j_titem5
581end
582gw_put_bytes gw_j_tasks1
583ret HTTP_OK
584end
585
586fn gw_submit_apply tid ulong prio ulong kind ulong pre ulong to ulong
587exists = dag_find_slot tid
588if lt exists 0
589rc = dag_add_task tid prio kind
590if eq rc DAG_ERR_FULL
591ret gw_set_error HTTP_SERVICE_UNAVAILABLE gw_em_full rc
592end
593if ne rc DAG_OK
594ret gw_set_error HTTP_CONFLICT gw_em_dup rc
595end
596aux = or shl kind 32 and prio 0xffffffff
597jrc = jrn_append EVT_TASK_SUBMITTED tid aux hb_current_tick gw_jrn_gw
598if ne jrc JRN_OK
599ret gw_set_error HTTP_SERVICE_UNAVAILABLE gw_em_full jrc
600end
601else
602if eq pre 0
603ret gw_set_error HTTP_CONFLICT gw_em_dup DAG_ERR_DUPLICATE_TASK
604end
605end
606if gt pre 0
607drc = dag_add_dep tid pre
608if eq drc DAG_ERR_CYCLE
609ret gw_set_error HTTP_CONFLICT gw_em_cycle drc
610end
611if ne drc DAG_OK
612ret gw_set_error HTTP_CONFLICT gw_em_conflict drc
613end
614jrc2 = jrn_append EVT_TASK_DEP tid pre hb_current_tick gw_jrn_gw
615if ne jrc2 JRN_OK
616ret gw_set_error HTTP_SERVICE_UNAVAILABLE gw_em_full jrc2
617end
618end
619rdy = dag_solve_ready
620if gt rdy DAG_MAX_TASKS
621ret gw_set_error HTTP_SERVICE_UNAVAILABLE gw_em_full DAG_ERR_FULL
622end
623gw_buf_reset
624put gw_meta 0 HTTP_CREATED
625put gw_meta 2 GW_CT_JSON
626gw_put_bytes gw_j_sub0
627gw_put_u64 tid
628gw_put_bytes gw_j_sub1
629gw_put_task_state dag_get_state tid
630gw_put_bytes gw_j_sub2
631gw_put_u64 jrn_last_seq
632gw_put_bytes gw_j_sub3
633ret HTTP_CREATED
634end
635
636fn gw_handle_submit body view uchar * to ulong
637v = new ulong 1 0
638if eq len body 0
639ret gw_set_error HTTP_BAD_REQUEST gw_em_badreq 100
640end
641r = gw_extract_u64 body gw_k_id v
642if or ne r 1 eq get v 0 0
643ret gw_set_error HTTP_BAD_REQUEST gw_em_badreq 101
644end
645tid = get v 0
646r = gw_extract_u64 body gw_k_prio v
647if eq r 2
648ret gw_set_error HTTP_BAD_REQUEST gw_em_badreq 102
649end
650prio = get v 0
651if gt prio 1000
652ret gw_set_error HTTP_BAD_REQUEST gw_em_badreq 103
653end
654r = gw_extract_u64 body gw_k_kind v
655if eq r 2
656ret gw_set_error HTTP_BAD_REQUEST gw_em_badreq 104
657end
658kind = get v 0
659r = gw_extract_u64 body gw_k_prereq v
660if eq r 2
661ret gw_set_error HTTP_BAD_REQUEST gw_em_badreq 105
662end
663pre = get v 0
664ret gw_submit_apply tid prio kind pre
665end
666
667fn gw_hb_apply tk ulong ag ulong cp ulong to ulong
668if and gt ag 0 gt cp 0
669rrc = hb_register_agent ag cp
670if and ne rrc HB_OK ne rrc HB_ERR_DUPLICATE
671ret gw_set_error HTTP_CONFLICT gw_em_conflict rrc
672end
673end
674if gt ag 0
675hrc = hb_heartbeat ag tk
676if ne hrc HB_OK
677ret gw_set_error HTTP_CONFLICT gw_em_conflict hrc
678end
679end
680trc = hb_tick tk
681if ne trc HB_OK
682ret gw_set_error HTTP_CONFLICT gw_em_conflict trc
683end
684recl = hb_last_reclaimed
685for i ulong recl bound 128
686rtid = hb_get_reclaimed i
687if eq dag_get_state rtid TASK_RUNNING
688q = dag_requeue_task rtid
689if eq q DAG_OK
690j1 = jrn_append EVT_LEASE_RECLAIMED rtid tk tk gw_jrn_gw
691if ne j1 JRN_OK
692ret gw_set_error HTTP_SERVICE_UNAVAILABLE gw_em_full j1
693end
694end
695end
696end
697j2 = jrn_append EVT_HEARTBEAT_TICK 0 recl tk gw_jrn_gw
698if ne j2 JRN_OK
699ret gw_set_error HTTP_SERVICE_UNAVAILABLE gw_em_full j2
700end
701gw_buf_reset
702put gw_meta 0 HTTP_OK
703put gw_meta 2 GW_CT_JSON
704gw_put_bytes gw_j_hb0
705if hb_is_degraded
706gw_put_bytes gw_j_hb_deg
707else
708gw_put_bytes gw_j_hb_nom
709end
710gw_put_bytes gw_j_hb1
711gw_put_u64 hb_current_tick
712gw_put_bytes gw_j_hb2
713gw_put_u64 hb_pulse_count
714gw_put_bytes gw_j_hb3
715gw_put_u64 recl
716gw_put_bytes gw_j_hb4
717gw_put_u64 hb_active_leases
718gw_put_bytes gw_j_hb5
719ret HTTP_OK
720end
721
722fn gw_handle_heartbeat body view uchar * to ulong
723v = new ulong 1 0
724if eq len body 0
725ret gw_set_error HTTP_BAD_REQUEST gw_em_badreq 200
726end
727r = gw_extract_u64 body gw_k_tick v
728if ne r 1
729ret gw_set_error HTTP_BAD_REQUEST gw_em_badreq 201
730end
731tk = get v 0
732r = gw_extract_u64 body gw_k_agent v
733if eq r 2
734ret gw_set_error HTTP_BAD_REQUEST gw_em_badreq 202
735end
736ag = get v 0
737r = gw_extract_u64 body gw_k_cap v
738if eq r 2
739ret gw_set_error HTTP_BAD_REQUEST gw_em_badreq 203
740end
741cp = get v 0
742ret gw_hb_apply tk ag cp
743end
744
745fn gw_handle_events to ulong
746gw_buf_reset
747put gw_meta 0 HTTP_OK
748put gw_meta 2 GW_CT_SSE
749h = new uchar 32 0
750n = jrn_count
751for i ulong n bound 128
752gw_put_bytes gw_sse_id
753gw_put_u64 jrn_rec_seq i
754gw_put_bytes gw_sse_ev
755gw_put_event_kind jrn_rec_kind i
756gw_put_bytes gw_sse_d0
757gw_put_u64 jrn_rec_seq i
758gw_put_bytes gw_sse_d1
759gw_put_u64 jrn_rec_tick i
760gw_put_bytes gw_sse_d2
761gw_put_event_kind jrn_rec_kind i
762gw_put_bytes gw_sse_d3
763gw_put_u64 jrn_rec_task_id i
764gw_put_bytes gw_sse_d4
765gw_put_u64 jrn_rec_aux i
766gw_put_bytes gw_sse_d5
767rc = jrn_copy_hash i h
768if eq rc JRN_OK
769gw_put_hex32 h
770end
771gw_put_bytes gw_sse_d6
772end
773ret HTTP_OK
774end
775
776note Map policy denial code to HTTP status and record EVT_POLICY_DENIED in journal.
777fn gw_reject_policy route ulong dec ulong to ulong
778jrc = jrn_append EVT_POLICY_DENIED route dec hb_current_tick gw_jrn_gw
779if gt jrc 999
780ret HTTP_SERVICE_UNAVAILABLE
781end
782if eq dec POL_DENY_NO_TOKEN_CONFIG
783ret gw_set_error HTTP_SERVICE_UNAVAILABLE gw_em_notok dec
784end
785if eq dec POL_DENY_UNAUTHORIZED
786ret gw_set_error HTTP_UNAUTHORIZED gw_em_unauth dec
787end
788if eq dec POL_DENY_MISSING_CAP
789ret gw_set_error HTTP_FORBIDDEN gw_em_cap dec
790end
791if eq dec POL_DENY_UNVERIFIED_ENV
792ret gw_set_error HTTP_FORBIDDEN gw_em_env dec
793end
794if eq dec POL_DENY_ESCALATION
795ret gw_set_error HTTP_FORBIDDEN gw_em_esc dec
796end
797if eq dec POL_DENY_CONTAINED
798ret gw_set_error HTTP_FORBIDDEN gw_em_cont dec
799end
800if eq dec POL_REQUIRE_APPROVAL
801ret gw_set_error HTTP_FORBIDDEN gw_em_appr dec
802end
803if eq dec POL_DENY_METHOD_NOT_ALLOWED
804ret gw_set_error HTTP_METHOD_NOT_ALLOWED gw_em_meth dec
805end
806ret gw_set_error HTTP_NOT_FOUND gw_em_route dec
807end
808
809note Stage 3: Authorize staged request via policy.om and route to handler.
810fn gw_req_dispatch body view uchar * to ulong
811if ne get gw_meta 9 0
812ret gw_set_error HTTP_BAD_REQUEST gw_em_badreq 300
813end
814meth = get gw_meta 3
815if eq meth METHOD_UNKNOWN
816ret gw_set_error HTTP_BAD_REQUEST gw_em_badreq 301
817end
818if not gw_body_valid body
819ret gw_set_error HTTP_BAD_REQUEST gw_em_badreq 302
820end
821route = get gw_meta 4
822actor = get gw_meta 5
823caps = get gw_meta 6
824tok_ok = ne get gw_meta 7 0
825env_ok = ne get gw_meta 8 0
826dec = pol_authorize_request meth route actor caps tok_ok env_ok
827if ne dec POL_ALLOW
828ret gw_reject_policy route dec
829end
830if eq route ROUTE_HEALTH
831ret gw_handle_health
832end
833if eq route ROUTE_EVENTS
834ret gw_handle_events
835end
836if eq route ROUTE_TASKS
837ret gw_handle_tasks
838end
839if eq route ROUTE_TASKS_SUBMIT
840ret gw_handle_submit body
841end
842if eq route ROUTE_HEARTBEAT
843ret gw_handle_heartbeat body
844end
845ret gw_set_error HTTP_NOT_FOUND gw_em_route POL_DENY_INVALID_ROUTE
846end
847
848note WebSocket control-plane frame router (requires authenticated operator/worker token).
849fn gw_ws_frame actor_id ulong req_caps ulong tok_ok bool frame view uchar * to ulong
850if not pol_is_configured
851ret gw_set_error HTTP_SERVICE_UNAVAILABLE gw_em_notok POL_DENY_NO_TOKEN_CONFIG
852end
853if not tok_ok
854ret gw_set_error HTTP_UNAUTHORIZED gw_em_unauth POL_DENY_UNAUTHORIZED
855end
856if pol_str_eq frame gw_ws_ping
857gw_buf_reset
858put gw_meta 0 HTTP_OK
859put gw_meta 2 GW_CT_JSON
860gw_put_bytes gw_ws_pong0
861gw_put_u64 hb_current_tick
862gw_put_bytes gw_ws_pong1
863ret HTTP_OK
864end
865if pol_str_eq frame gw_ws_tasks
866dec = pol_authorize_request METHOD_GET ROUTE_TASKS actor_id req_caps tok_ok true
867if ne dec POL_ALLOW
868ret gw_reject_policy ROUTE_TASKS dec
869end
870ret gw_handle_tasks
871end
872if pol_str_eq frame gw_ws_hb
873dec2 = pol_authorize_request METHOD_POST ROUTE_HEARTBEAT actor_id req_caps tok_ok true
874if ne dec2 POL_ALLOW
875ret gw_reject_policy ROUTE_HEARTBEAT dec2
876end
877trc = hb_tick add hb_current_tick 1
878if ne trc HB_OK
879ret gw_set_error HTTP_CONFLICT gw_em_conflict trc
880end
881gw_buf_reset
882put gw_meta 0 HTTP_OK
883put gw_meta 2 GW_CT_JSON
884gw_put_bytes gw_j_hb0
885gw_put_bytes gw_j_hb_nom
886gw_put_bytes gw_j_hb1
887gw_put_u64 hb_current_tick
888gw_put_bytes gw_j_hb2
889gw_put_u64 hb_pulse_count
890gw_put_bytes gw_j_hb3
891gw_put_u64 hb_last_reclaimed
892gw_put_bytes gw_j_hb4
893gw_put_u64 hb_active_leases
894gw_put_bytes gw_j_hb5
895ret HTTP_OK
896end
897gw_buf_reset
898put gw_meta 0 HTTP_BAD_REQUEST
899put gw_meta 2 GW_CT_JSON
900gw_put_bytes gw_ws_err
901ret HTTP_BAD_REQUEST
902end