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 / core / openclaw / lib.om

1352 lines · 26933 bytes Raw Blame History
1note core/openclaw/lib.om: pure-Omega compiled agent runtime loop
2note Faithful port of openclaw-rs (AgentEngine, SkillRegistry, VaultResolver,
3note context window pruning, deterministic turn state machine, policy allowlist,
4note capability bitmasks, and append-only chained audit log).
5note Composed after si/directives/om2.om; no om2 header and no fn main in this unit.
6
7note Capability bitmasks
8const CAP_NONE ulong 0
9const CAP_READ ulong 1
10const CAP_WRITE ulong 2
11const CAP_EXEC ulong 4
12const CAP_NET ulong 8
13
14note Deterministic turn state machine states
15const ST_IDLE ulong 0
16const ST_PLAN ulong 1
17const ST_POLICY_CHECK ulong 2
18const ST_DISPATCH ulong 3
19const ST_RECORD ulong 4
20const ST_DONE ulong 5
21const ST_REFUSED ulong 6
22
23note Refusal / error codes
24const ERR_NONE ulong 0
25const ERR_MALFORMED ulong 1
26const ERR_UNKNOWN_TOOL ulong 2
27const ERR_CAP_DENIED ulong 3
28const ERR_BUDGET_EXHAUSTED ulong 4
29const ERR_PRIV_ESCALATION ulong 5
30const ERR_ARG_POLICY ulong 6
31
32note Built-in tool handler kinds
33const TK_READ_FILE ulong 1
34const TK_LIST_DIR ulong 2
35const TK_GIT_STATUS ulong 3
36const TK_CORTEX_RECALL ulong 4
37const TK_TELEM_PING ulong 5
38const TK_WRITE_FILE ulong 6
39const TK_BASH_EVAL ulong 7
40const TK_HTTP_FETCH ulong 8
41const TK_CUSTOM ulong 9
42
43note Capacity bounds
44const OC_MAX_TOOLS ulong 16
45const OC_TNAME_CAP ulong 32
46const OC_CTX_CAP ulong 16
47const OC_CTX_SLOT ulong 128
48const OC_MAX_AUDIT ulong 64
49const OC_VAULT_CAP ulong 8
50const OC_VAULT_SLOT ulong 64
51const OC_ENV_CAP ulong 4096
52const OC_OUT_CAP ulong 4096
53const OC_RES_CAP ulong 512
54
55note Session state globals
56global oc_sess_caps ulong 1
57global oc_sess_turns ulong 1
58global oc_sess_max_turns ulong 1
59global oc_sess_tokens ulong 1
60global oc_sess_tok_budget ulong 1
61global oc_sess_steps ulong 1
62global oc_sess_step_budget ulong 1
63global oc_sess_ctx_budget ulong 1
64global oc_dispatch_cnt ulong 1
65global oc_refuse_cnt ulong 1
66global oc_chain_hash ulong 1
67
68note Current turn state machine & parsed envelope
69global oc_state ulong 1
70global oc_trace ulong 8
71global oc_trace_len ulong 1
72global oc_trace_mask ulong 1
73
74global oc_env uchar 4096
75global oc_env_len ulong 1
76global oc_call_id uchar 64
77global oc_call_id_len ulong 1
78global oc_req_tool uchar 64
79global oc_req_tool_len ulong 1
80global oc_req_arg uchar 256
81global oc_req_arg_len ulong 1
82global oc_req_cost ulong 1
83global oc_cur_err ulong 1
84global oc_cur_tid ulong 1
85global oc_cur_found ulong 1
86global oc_cur_req_caps ulong 1
87global oc_cur_tok_delta ulong 1
88
89note Tool result and JSONL output buffers
90global oc_res uchar 512
91global oc_res_len ulong 1
92global oc_tmp uchar 512
93global oc_tmp_len ulong 1
94global oc_out uchar 4096
95global oc_out_len ulong 1
96
97note Tool registry globals
98global oc_t_cnt ulong 1
99global oc_t_name uchar 512
100global oc_t_nlen ulong 16
101global oc_t_caps ulong 16
102global oc_t_cost ulong 16
103global oc_t_kind ulong 16
104global oc_t_enabled ulong 16
105
106note Context / memory ring globals (slot 0=system, slot 1=goal pinned)
107global oc_ctx_cnt ulong 1
108global oc_ctx_pruned ulong 1
109global oc_ctx_buf uchar 2048
110global oc_ctx_len ulong 16
111global oc_ctx_role ulong 16
112global oc_ctx_tok ulong 16
113
114note Vault in-memory secret cache globals
115global oc_v_cnt ulong 1
116global oc_v_buf uchar 512
117global oc_v_len ulong 8
118
119note Append-only turn audit log globals
120global oc_aud_cnt ulong 1
121global oc_aud_turn ulong 64
122global oc_aud_final ulong 64
123global oc_aud_tmask ulong 64
124global oc_aud_err ulong 64
125global oc_aud_tid ulong 64
126global oc_aud_reqc ulong 64
127global oc_aud_sesc ulong 64
128global oc_aud_toks ulong 64
129global oc_aud_hash ulong 64
130
131text oc_hex "0123456789abcdef"
132text oc_redact_mark "[REDACTED_BY_ATLAS_VAULT]"
133text oc_def_sys "You are OpenClaw sovereign agent runtime."
134text oc_def_goal "Execute authorized workspace turns under policy."
135
136note Built-in tool names
137text tn_read "read_file"
138text tn_list "list_dir"
139text tn_git "git_status"
140text tn_cortex "cortex_recall"
141text tn_ping "telemetry_ping"
142text tn_write "write_file"
143text tn_bash "bash_eval"
144text tn_http "http_fetch"
145
146note Envelope JSON field keys
147text fk_id "\"id\""
148text fk_tool "\"tool\""
149text fk_arg "\"arg\""
150text fk_cost "\"cost\""
151
152note Escalation / injection detection keys and payloads
153text ek_caps "\"caps\""
154text ek_grant "\"grant\""
155text ek_override "\"override_policy\""
156text ek_escalate "\"escalate\""
157text ek_setcaps "\"set_caps\""
158text ek_allowall "\"allow_all\""
159text inj_1 "ignore previous instructions"
160text inj_2 "ignore all previous"
161text inj_3 "override policy"
162text inj_4 "grant_cap"
163text inj_5 "elevate_caps"
164text inj_6 "bypass policy"
165text inj_7 "disable_policy"
166text inj_8 "sudo "
167text inj_9 "chmod +s"
168text inj_10 "setcap "
169
170note Argument safety patterns
171text ap_env ".env"
172text ap_shadow "/etc/shadow"
173text ap_dotdot ".."
174text ap_evid "evidence/"
175text ap_aien ".aien/"
176text ap_gates "tests/gates/"
177text ap_find_r1 "find / "
178text ap_find_r2 "find / -"
179text ap_find_r3 "find /"
180text ap_rm_root "rm -rf /"
181text ap_local1 "http://127.0.0.1"
182text ap_local2 "http://localhost"
183
184note State names
185text sn_idle "IDLE"
186text sn_plan "PLAN"
187text sn_policy "POLICY_CHECK"
188text sn_dispatch "DISPATCH"
189text sn_record "RECORD"
190text sn_done "DONE"
191text sn_refused "REFUSED"
192text sn_gt ">"
193
194note Error names
195text en_none "NONE"
196text en_mal "MALFORMED"
197text en_unk "UNKNOWN_TOOL"
198text en_cap "CAP_DENIED"
199text en_bud "BUDGET_EXHAUSTED"
200text en_esc "PRIV_ESCALATION"
201text en_arg "ARG_POLICY"
202
203note Dispatch & refusal message fragments
204text dm_read "read_ok:"
205text dm_list "list_ok:"
206text dm_git "git_clean:"
207text dm_cortex "cortex_recalled:"
208text dm_ping "{\"status\":\"healthy\",\"architecture\":\"aarch64\",\"target\":\"gb10\"}"
209text dm_write "write_ok:"
210text dm_bash "exec_ok:"
211text dm_http "http_ok:"
212text dm_custom "custom_ok:"
213
214text rm_mal "refused: malformed tool-call envelope"
215text rm_unk "refused: unknown or disabled tool"
216text rm_cap "refused: unauthorized capability"
217text rm_bud "refused: session turn or token budget exhausted"
218text rm_esc "refused: privilege escalation or prompt injection blocked"
219text rm_arg "refused: argument violates tool safety policy"
220
221note JSONL formatting tokens
222text jl_1 "{\"turn\":"
223text jl_2 ",\"id\":\""
224text jl_3 "\",\"state\":\""
225text jl_4 "\",\"trace\":\""
226text jl_5 "\",\"tool\":\""
227text jl_6 "\",\"req_caps\":"
228text jl_7 ",\"sess_caps\":"
229text jl_8 ",\"tokens\":"
230text jl_9 ",\"budget\":"
231text jl_10 ",\"err\":\""
232text jl_11 "\",\"hash\":\""
233text jl_12 "\",\"out\":\""
234text jl_13 "\"}\n"
235
236fn oc_to_lower c uchar to uchar
237if and ge c 65 le c 90
238ret add c 32
239end
240ret c
241end
242
243fn oc_fnv_byte h ulong b uchar to ulong
244x = xor h ulong b
245ret mul x ulong 0x100000001b3
246end
247
248fn oc_fnv_u64 h ulong v ulong to ulong
249x = h
250for i ulong 8
251b = uchar and shr v mul i 8 255
252x = oc_fnv_byte x b
253end
254ret x
255end
256
257fn oc_est_tokens nbytes ulong to ulong
258note Word/byte token estimator matching upstream (~1 token per 4 bytes + 4 overhead)
259ret add div add nbytes 3 4 4
260end
261
262fn oc_ctx_total_tokens to ulong
263n = get oc_ctx_cnt 0
264tot = ulong 0
265for i ulong n bound 16
266tot = add tot get oc_ctx_tok i
267end
268ret tot
269end
270
271fn oc_ctx_set_slot idx ulong role ulong s view uchar * to void
272n = len s
273if gt n OC_CTX_SLOT
274n = OC_CTX_SLOT
275end
276base = mul idx OC_CTX_SLOT
277for i ulong n bound 128
278put oc_ctx_buf add base i get s i
279end
280put oc_ctx_len idx n
281put oc_ctx_role idx role
282put oc_ctx_tok idx oc_est_tokens n
283end
284
285fn oc_ctx_evict_oldest to bool
286n = get oc_ctx_cnt 0
287if le n 2
288ret false
289end
290note Preserve slot 0 (system) and slot 1 (goal), evict slot 2 and shift down
291lim = sub n 1
292for i ulong lim bound 16
293if ge i 2
294src_b = mul add i 1 OC_CTX_SLOT
295dst_b = mul i OC_CTX_SLOT
296slen = get oc_ctx_len add i 1
297for j ulong slen bound 128
298put oc_ctx_buf add dst_b j get oc_ctx_buf add src_b j
299end
300put oc_ctx_len i slen
301put oc_ctx_role i get oc_ctx_role add i 1
302put oc_ctx_tok i get oc_ctx_tok add i 1
303end
304end
305put oc_ctx_cnt 0 lim
306put oc_ctx_pruned 0 add get oc_ctx_pruned 0 1
307ret true
308end
309
310fn oc_ctx_prune to ulong
311pruned = ulong 0
312bud = get oc_sess_ctx_budget 0
313while gt get oc_ctx_cnt 0 2 bound 16
314if le oc_ctx_total_tokens bud
315break
316end
317if oc_ctx_evict_oldest
318pruned = add pruned 1
319else
320break
321end
322end
323ret pruned
324end
325
326fn oc_ctx_push_res role ulong to void
327if ge get oc_ctx_cnt 0 OC_CTX_CAP
328ev = oc_ctx_evict_oldest
329if not ev
330ret
331end
332end
333idx = get oc_ctx_cnt 0
334n = get oc_res_len 0
335if gt n OC_CTX_SLOT
336n = OC_CTX_SLOT
337end
338base = mul idx OC_CTX_SLOT
339for i ulong n bound 128
340put oc_ctx_buf add base i get oc_res i
341end
342put oc_ctx_len idx n
343put oc_ctx_role idx role
344put oc_ctx_tok idx oc_est_tokens n
345put oc_ctx_cnt 0 add idx 1
346p = oc_ctx_prune
347if gt p 16
348ret
349end
350end
351
352fn oc_vault_clear to void
353put oc_v_cnt 0 0
354end
355
356fn oc_vault_cache_secret sec view uchar * to bool
357c = get oc_v_cnt 0
358if ge c OC_VAULT_CAP
359ret false
360end
361n = len sec
362if or lt n 4 gt n OC_VAULT_SLOT
363ret false
364end
365base = mul c OC_VAULT_SLOT
366for i ulong n bound 64
367put oc_v_buf add base i get sec i
368end
369put oc_v_len c n
370put oc_v_cnt 0 add c 1
371ret true
372end
373
374fn oc_res_match_secret pos ulong vidx ulong to bool
375vlen = get oc_v_len vidx
376vbase = mul vidx OC_VAULT_SLOT
377for j ulong vlen bound 64
378if ne get oc_res add pos j get oc_v_buf add vbase j
379ret false
380end
381end
382ret true
383end
384
385fn oc_tmp_push b uchar to void
386p = get oc_tmp_len 0
387if lt p OC_RES_CAP
388put oc_tmp p b
389put oc_tmp_len 0 add p 1
390end
391end
392
393fn oc_tmp_push_str s view uchar * to void
394n = len s
395for i ulong n bound 128
396oc_tmp_push get s i
397end
398end
399
400fn oc_vault_redact_one vidx ulong to void
401vlen = get oc_v_len vidx
402rlen = get oc_res_len 0
403if or lt vlen 4 lt rlen vlen
404ret
405end
406put oc_tmp_len 0 0
407pos = ulong 0
408while lt pos rlen bound 512
409matched = false
410if le add pos vlen rlen
411if oc_res_match_secret pos vidx
412matched = true
413end
414end
415if matched
416oc_tmp_push_str oc_redact_mark
417pos = add pos vlen
418else
419oc_tmp_push get oc_res pos
420pos = add pos 1
421end
422end
423tlen = get oc_tmp_len 0
424for i ulong tlen bound 512
425put oc_res i get oc_tmp i
426end
427put oc_res_len 0 tlen
428end
429
430fn oc_vault_redact_res to void
431vc = get oc_v_cnt 0
432for i ulong vc bound 8
433oc_vault_redact_one i
434end
435end
436
437fn oc_reg_tool name view uchar * req_caps ulong cost ulong kind ulong to bool
438c = get oc_t_cnt 0
439if ge c OC_MAX_TOOLS
440ret false
441end
442n = len name
443if or eq n 0 gt n OC_TNAME_CAP
444ret false
445end
446base = mul c OC_TNAME_CAP
447for i ulong n bound 32
448put oc_t_name add base i get name i
449end
450put oc_t_nlen c n
451put oc_t_caps c req_caps
452put oc_t_cost c cost
453put oc_t_kind c kind
454put oc_t_enabled c 1
455put oc_t_cnt 0 add c 1
456ret true
457end
458
459fn oc_register_builtins to void
460put oc_t_cnt 0 0
461r1 = oc_reg_tool tn_read CAP_READ 12 TK_READ_FILE
462r2 = oc_reg_tool tn_list CAP_READ 8 TK_LIST_DIR
463r3 = oc_reg_tool tn_git CAP_READ 10 TK_GIT_STATUS
464r4 = oc_reg_tool tn_cortex CAP_READ 14 TK_CORTEX_RECALL
465r5 = oc_reg_tool tn_ping CAP_NONE 4 TK_TELEM_PING
466r6 = oc_reg_tool tn_write CAP_WRITE 16 TK_WRITE_FILE
467r7 = oc_reg_tool tn_bash CAP_EXEC 24 TK_BASH_EVAL
468r8 = oc_reg_tool tn_http CAP_NET 20 TK_HTTP_FETCH
469if not and r1 and r2 and r3 and r4 and r5 and r6 and r7 r8
470trap 8
471end
472end
473
474fn oc_session_init caps ulong max_turns ulong tok_budget ulong step_budget ulong to void
475put oc_sess_caps 0 caps
476put oc_sess_turns 0 0
477put oc_sess_max_turns 0 max_turns
478put oc_sess_tokens 0 0
479put oc_sess_tok_budget 0 tok_budget
480put oc_sess_steps 0 0
481put oc_sess_step_budget 0 step_budget
482put oc_sess_ctx_budget 0 256
483put oc_dispatch_cnt 0 0
484put oc_refuse_cnt 0 0
485put oc_chain_hash 0 ulong 0xcbf29ce484222325
486put oc_state 0 ST_IDLE
487put oc_trace_len 0 0
488put oc_trace_mask 0 0
489put oc_aud_cnt 0 0
490put oc_ctx_cnt 0 2
491put oc_ctx_pruned 0 0
492oc_ctx_set_slot 0 0 oc_def_sys
493oc_ctx_set_slot 1 1 oc_def_goal
494oc_vault_clear
495oc_register_builtins
496end
497
498fn oc_set_ctx_budget bud ulong to void
499put oc_sess_ctx_budget 0 bud
500end
501
502fn oc_transition_legal cur ulong nxt ulong to bool
503if eq cur ST_IDLE
504ret eq nxt ST_PLAN
505end
506if eq cur ST_PLAN
507ret or eq nxt ST_POLICY_CHECK eq nxt ST_RECORD
508end
509if eq cur ST_POLICY_CHECK
510ret or eq nxt ST_DISPATCH eq nxt ST_RECORD
511end
512if eq cur ST_DISPATCH
513ret eq nxt ST_RECORD
514end
515if eq cur ST_RECORD
516ret or eq nxt ST_DONE eq nxt ST_REFUSED
517end
518ret false
519end
520
521fn oc_turn_reset_sm to void
522put oc_state 0 ST_IDLE
523put oc_trace 0 ST_IDLE
524put oc_trace_len 0 1
525put oc_trace_mask 0 shl ulong 1 ST_IDLE
526put oc_cur_err 0 ERR_NONE
527put oc_cur_tid 0 0
528put oc_cur_found 0 0
529put oc_cur_req_caps 0 0
530put oc_cur_tok_delta 0 0
531put oc_res_len 0 0
532put oc_call_id_len 0 0
533put oc_req_tool_len 0 0
534put oc_req_arg_len 0 0
535put oc_req_cost 0 0
536end
537
538fn oc_tr_state nxt ulong to void
539cur = get oc_state 0
540if not oc_transition_legal cur nxt
541trap 8
542end
543put oc_state 0 nxt
544tl = get oc_trace_len 0
545if lt tl 8
546put oc_trace tl nxt
547put oc_trace_len 0 add tl 1
548end
549bit = shl ulong 1 nxt
550put oc_trace_mask 0 or get oc_trace_mask 0 bit
551end
552
553fn oc_env_match_ci pos ulong pat view uchar * to bool
554pl = len pat
555for j ulong pl bound 64
556c1 = oc_to_lower get oc_env add pos j
557c2 = oc_to_lower get pat j
558if ne c1 c2
559ret false
560end
561end
562ret true
563end
564
565fn oc_env_contains_ci pat view uchar * to bool
566n = get oc_env_len 0
567pl = len pat
568if or eq pl 0 lt n pl
569ret false
570end
571lim = add sub n pl 1
572for i ulong lim bound 4096
573if oc_env_match_ci i pat
574ret true
575end
576end
577ret false
578end
579
580fn oc_env_find_key pat view uchar * to long
581n = get oc_env_len 0
582pl = len pat
583if or eq pl 0 lt n pl
584ret long -1
585end
586lim = add sub n pl 1
587for i ulong lim bound 4096
588if oc_env_match_ci i pat
589ret long add i pl
590end
591end
592ret long -1
593end
594
595fn oc_arg_match_ci pos ulong pat view uchar * to bool
596pl = len pat
597for j ulong pl bound 64
598c1 = oc_to_lower get oc_req_arg add pos j
599c2 = oc_to_lower get pat j
600if ne c1 c2
601ret false
602end
603end
604ret true
605end
606
607fn oc_arg_contains_ci pat view uchar * to bool
608n = get oc_req_arg_len 0
609pl = len pat
610if or eq pl 0 lt n pl
611ret false
612end
613lim = add sub n pl 1
614for i ulong lim bound 256
615if oc_arg_match_ci i pat
616ret true
617end
618end
619ret false
620end
621
622fn oc_arg_starts_ci pat view uchar * to bool
623n = get oc_req_arg_len 0
624pl = len pat
625if lt n pl
626ret false
627end
628ret oc_arg_match_ci 0 pat
629end
630
631fn oc_parse_str_field key view uchar * dst_kind ulong to bool
632note dst_kind: 0=call_id, 1=req_tool, 2=req_arg
633kpos = oc_env_find_key key
634if lt kpos 0
635ret false
636end
637pos = ulong kpos
638n = get oc_env_len 0
639while lt pos n bound 4096
640c = get oc_env pos
641if or eq c 32 or eq c 9 eq c 58
642pos = add pos 1
643else
644break
645end
646end
647if ge pos n
648ret false
649end
650if ne get oc_env pos 34
651ret false
652end
653pos = add pos 1
654cnt = ulong 0
655cap = ulong 64
656if eq dst_kind 2
657cap = ulong 256
658end
659while lt pos n bound 4096
660c = get oc_env pos
661if eq c 34
662break
663end
664if eq c 92
665pos = add pos 1
666if ge pos n
667ret false
668end
669c = get oc_env pos
670end
671if lt cnt cap
672if eq dst_kind 0
673put oc_call_id cnt c
674else
675if eq dst_kind 1
676put oc_req_tool cnt c
677else
678put oc_req_arg cnt c
679end
680end
681cnt = add cnt 1
682end
683pos = add pos 1
684end
685if ge pos n
686ret false
687end
688if eq dst_kind 0
689put oc_call_id_len 0 cnt
690else
691if eq dst_kind 1
692put oc_req_tool_len 0 cnt
693else
694put oc_req_arg_len 0 cnt
695end
696end
697ret true
698end
699
700fn oc_parse_u64_field key view uchar * to ulong
701kpos = oc_env_find_key key
702if lt kpos 0
703ret 0
704end
705pos = ulong kpos
706n = get oc_env_len 0
707while lt pos n bound 4096
708c = get oc_env pos
709if or eq c 32 or eq c 9 eq c 58
710pos = add pos 1
711else
712break
713end
714end
715val = ulong 0
716while lt pos n bound 32
717c = get oc_env pos
718if and ge c 48 le c 57
719val = add mul val 10 ulong sub c 48
720pos = add pos 1
721else
722break
723end
724end
725ret val
726end
727
728fn oc_env_is_json_obj to bool
729n = get oc_env_len 0
730if lt n 2
731ret false
732end
733s = ulong 0
734while lt s n bound 4096
735c = get oc_env s
736if or eq c 32 or eq c 9 eq c 13
737s = add s 1
738else
739break
740end
741end
742e = n
743while gt e s bound 4096
744c = get oc_env sub e 1
745if or eq c 32 or eq c 9 or eq c 13 eq c 10
746e = sub e 1
747else
748break
749end
750end
751if lt sub e s 2
752ret false
753end
754if ne get oc_env s 123
755ret false
756end
757if ne get oc_env sub e 1 125
758ret false
759end
760ret true
761end
762
763fn oc_detect_escalation to bool
764if oc_env_contains_ci ek_caps
765ret true
766end
767if oc_env_contains_ci ek_grant
768ret true
769end
770if oc_env_contains_ci ek_override
771ret true
772end
773if oc_env_contains_ci ek_escalate
774ret true
775end
776if oc_env_contains_ci ek_setcaps
777ret true
778end
779if oc_env_contains_ci ek_allowall
780ret true
781end
782if oc_env_contains_ci inj_1
783ret true
784end
785if oc_env_contains_ci inj_2
786ret true
787end
788if oc_env_contains_ci inj_3
789ret true
790end
791if oc_env_contains_ci inj_4
792ret true
793end
794if oc_env_contains_ci inj_5
795ret true
796end
797if oc_env_contains_ci inj_6
798ret true
799end
800if oc_env_contains_ci inj_7
801ret true
802end
803if oc_env_contains_ci inj_8
804ret true
805end
806if oc_env_contains_ci inj_9
807ret true
808end
809if oc_env_contains_ci inj_10
810ret true
811end
812ret false
813end
814
815fn oc_tool_name_eq tid ulong to bool
816tl = get oc_t_nlen tid
817if ne tl get oc_req_tool_len 0
818ret false
819end
820base = mul tid OC_TNAME_CAP
821for j ulong tl bound 32
822if ne get oc_t_name add base j get oc_req_tool j
823ret false
824end
825end
826ret true
827end
828
829fn oc_lookup_tool to long
830cnt = get oc_t_cnt 0
831for i ulong cnt bound 16
832if eq get oc_t_enabled i 1
833if oc_tool_name_eq i
834ret long i
835end
836end
837end
838ret long -1
839end
840
841fn oc_check_arg_policy kind ulong to bool
842alen = get oc_req_arg_len 0
843if eq kind TK_READ_FILE
844if eq alen 0
845ret false
846end
847if or oc_arg_contains_ci ap_env or oc_arg_contains_ci ap_shadow oc_arg_contains_ci ap_dotdot
848ret false
849end
850ret true
851end
852if eq kind TK_WRITE_FILE
853if eq alen 0
854ret false
855end
856if or oc_arg_contains_ci ap_env or oc_arg_contains_ci ap_dotdot oc_arg_contains_ci ap_evid
857ret false
858end
859if or oc_arg_contains_ci ap_aien oc_arg_contains_ci ap_gates
860ret false
861end
862ret true
863end
864if eq kind TK_BASH_EVAL
865if eq alen 0
866ret false
867end
868if or oc_arg_contains_ci ap_find_r1 or oc_arg_contains_ci ap_find_r2 oc_arg_contains_ci ap_rm_root
869ret false
870end
871if and eq alen 6 oc_arg_starts_ci ap_find_r3
872ret false
873end
874ret true
875end
876if eq kind TK_HTTP_FETCH
877if eq alen 0
878ret false
879end
880if or oc_arg_starts_ci ap_local1 oc_arg_starts_ci ap_local2
881ret true
882end
883ret false
884end
885ret true
886end
887
888fn oc_res_clear to void
889put oc_res_len 0 0
890end
891
892fn oc_res_push b uchar to void
893p = get oc_res_len 0
894if lt p OC_RES_CAP
895put oc_res p b
896put oc_res_len 0 add p 1
897end
898end
899
900fn oc_res_push_str s view uchar * to void
901n = len s
902for i ulong n bound 256
903oc_res_push get s i
904end
905end
906
907fn oc_res_push_arg to void
908n = get oc_req_arg_len 0
909for i ulong n bound 256
910oc_res_push get oc_req_arg i
911end
912end
913
914fn oc_dispatch_tool kind ulong to void
915oc_res_clear
916if eq kind TK_READ_FILE
917oc_res_push_str dm_read
918oc_res_push_arg
919else
920if eq kind TK_LIST_DIR
921oc_res_push_str dm_list
922oc_res_push_arg
923else
924if eq kind TK_GIT_STATUS
925oc_res_push_str dm_git
926oc_res_push_arg
927else
928if eq kind TK_CORTEX_RECALL
929oc_res_push_str dm_cortex
930oc_res_push_arg
931else
932if eq kind TK_TELEM_PING
933oc_res_push_str dm_ping
934else
935if eq kind TK_WRITE_FILE
936oc_res_push_str dm_write
937oc_res_push_arg
938else
939if eq kind TK_BASH_EVAL
940oc_res_push_str dm_bash
941oc_res_push_arg
942else
943if eq kind TK_HTTP_FETCH
944oc_res_push_str dm_http
945oc_res_push_arg
946else
947oc_res_push_str dm_custom
948oc_res_push_arg
949end
950end
951end
952end
953end
954end
955end
956end
957oc_vault_redact_res
958end
959
960fn oc_set_refusal_msg err ulong to void
961oc_res_clear
962if eq err ERR_MALFORMED
963oc_res_push_str rm_mal
964ret
965end
966if eq err ERR_UNKNOWN_TOOL
967oc_res_push_str rm_unk
968ret
969end
970if eq err ERR_CAP_DENIED
971oc_res_push_str rm_cap
972ret
973end
974if eq err ERR_BUDGET_EXHAUSTED
975oc_res_push_str rm_bud
976ret
977end
978if eq err ERR_PRIV_ESCALATION
979oc_res_push_str rm_esc
980ret
981end
982oc_res_push_str rm_arg
983end
984
985fn oc_compute_audit_hash prev ulong turn_id ulong fin ulong tmask ulong err ulong to ulong
986h = oc_fnv_u64 prev turn_id
987h = oc_fnv_u64 h fin
988h = oc_fnv_u64 h tmask
989h = oc_fnv_u64 h err
990ret h
991end
992
993fn oc_audit_append fin ulong to void
994idx = get oc_aud_cnt 0
995tnum = get oc_sess_turns 0
996tmask = get oc_trace_mask 0
997err = get oc_cur_err 0
998prev = get oc_chain_hash 0
999nh = oc_compute_audit_hash prev tnum fin tmask err
1000nh = oc_fnv_u64 nh get oc_cur_req_caps 0
1001nh = oc_fnv_u64 nh get oc_sess_caps 0
1002nh = oc_fnv_u64 nh get oc_sess_tokens 0
1003put oc_chain_hash 0 nh
1004if lt idx OC_MAX_AUDIT
1005put oc_aud_turn idx tnum
1006put oc_aud_final idx fin
1007put oc_aud_tmask idx tmask
1008put oc_aud_err idx err
1009put oc_aud_tid idx get oc_cur_tid 0
1010put oc_aud_reqc idx get oc_cur_req_caps 0
1011put oc_aud_sesc idx get oc_sess_caps 0
1012put oc_aud_toks idx get oc_sess_tokens 0
1013put oc_aud_hash idx nh
1014put oc_aud_cnt 0 add idx 1
1015end
1016end
1017
1018fn oc_verify_audit_chain to bool
1019cnt = get oc_aud_cnt 0
1020h = ulong 0xcbf29ce484222325
1021for i ulong cnt bound 64
1022h = oc_compute_audit_hash h get oc_aud_turn i get oc_aud_final i get oc_aud_tmask i get oc_aud_err i
1023h = oc_fnv_u64 h get oc_aud_reqc i
1024h = oc_fnv_u64 h get oc_aud_sesc i
1025h = oc_fnv_u64 h get oc_aud_toks i
1026if ne h get oc_aud_hash i
1027ret false
1028end
1029end
1030ret eq h get oc_chain_hash 0
1031end
1032
1033fn oc_load_envelope s view uchar * to ulong
1034n = len s
1035if gt n OC_ENV_CAP
1036n = OC_ENV_CAP
1037end
1038for i ulong n bound 4096
1039put oc_env i get s i
1040end
1041put oc_env_len 0 n
1042ret n
1043end
1044
1045fn oc_run_turn to ulong
1046oc_turn_reset_sm
1047put oc_sess_turns 0 add get oc_sess_turns 0 1
1048
1049note Phase 1: IDLE -> PLAN (parse structured tool-call envelope)
1050oc_tr_state ST_PLAN
1051if not oc_env_is_json_obj
1052put oc_cur_err 0 ERR_MALFORMED
1053oc_set_refusal_msg ERR_MALFORMED
1054oc_tr_state ST_RECORD
1055oc_audit_append ST_REFUSED
1056put oc_refuse_cnt 0 add get oc_refuse_cnt 0 1
1057oc_tr_state ST_REFUSED
1058ret ST_REFUSED
1059end
1060got_id = oc_parse_str_field fk_id 0
1061got_tool = oc_parse_str_field fk_tool 1
1062got_arg = oc_parse_str_field fk_arg 2
1063if and not got_id not got_arg
1064put oc_req_cost 0 oc_parse_u64_field fk_cost
1065else
1066put oc_req_cost 0 oc_parse_u64_field fk_cost
1067end
1068if or not got_tool eq get oc_req_tool_len 0 0
1069put oc_cur_err 0 ERR_MALFORMED
1070oc_set_refusal_msg ERR_MALFORMED
1071oc_tr_state ST_RECORD
1072oc_audit_append ST_REFUSED
1073put oc_refuse_cnt 0 add get oc_refuse_cnt 0 1
1074oc_tr_state ST_REFUSED
1075ret ST_REFUSED
1076end
1077
1078note Phase 2: PLAN -> POLICY_CHECK (model requests NEVER bypass runtime policy)
1079oc_tr_state ST_POLICY_CHECK
1080
1081note 2a. Detect prompt-injected privilege escalation before any tool lookup/dispatch
1082if oc_detect_escalation
1083put oc_cur_err 0 ERR_PRIV_ESCALATION
1084oc_set_refusal_msg ERR_PRIV_ESCALATION
1085oc_tr_state ST_RECORD
1086oc_audit_append ST_REFUSED
1087put oc_refuse_cnt 0 add get oc_refuse_cnt 0 1
1088oc_tr_state ST_REFUSED
1089ret ST_REFUSED
1090end
1091
1092note 2b. Verify tool exists and is enabled in explicit allowlist registry
1093tid_l = oc_lookup_tool
1094if lt tid_l 0
1095put oc_cur_err 0 ERR_UNKNOWN_TOOL
1096oc_set_refusal_msg ERR_UNKNOWN_TOOL
1097oc_tr_state ST_RECORD
1098oc_audit_append ST_REFUSED
1099put oc_refuse_cnt 0 add get oc_refuse_cnt 0 1
1100oc_tr_state ST_REFUSED
1101ret ST_REFUSED
1102end
1103tid = ulong tid_l
1104put oc_cur_tid 0 tid
1105put oc_cur_found 0 1
1106req_c = get oc_t_caps tid
1107put oc_cur_req_caps 0 req_c
1108
1109note 2c. Verify session holds all required capability bits
1110ses_c = get oc_sess_caps 0
1111if ne and req_c ses_c req_c
1112put oc_cur_err 0 ERR_CAP_DENIED
1113oc_set_refusal_msg ERR_CAP_DENIED
1114oc_tr_state ST_RECORD
1115oc_audit_append ST_REFUSED
1116put oc_refuse_cnt 0 add get oc_refuse_cnt 0 1
1117oc_tr_state ST_REFUSED
1118ret ST_REFUSED
1119end
1120
1121note 2d. Verify turn, step, and token budgets
1122cost = add get oc_t_cost tid get oc_req_cost 0
1123put oc_cur_tok_delta 0 cost
1124if gt get oc_sess_turns 0 get oc_sess_max_turns 0
1125put oc_cur_err 0 ERR_BUDGET_EXHAUSTED
1126oc_set_refusal_msg ERR_BUDGET_EXHAUSTED
1127oc_tr_state ST_RECORD
1128oc_audit_append ST_REFUSED
1129put oc_refuse_cnt 0 add get oc_refuse_cnt 0 1
1130oc_tr_state ST_REFUSED
1131ret ST_REFUSED
1132end
1133if ge get oc_sess_steps 0 get oc_sess_step_budget 0
1134put oc_cur_err 0 ERR_BUDGET_EXHAUSTED
1135oc_set_refusal_msg ERR_BUDGET_EXHAUSTED
1136oc_tr_state ST_RECORD
1137oc_audit_append ST_REFUSED
1138put oc_refuse_cnt 0 add get oc_refuse_cnt 0 1
1139oc_tr_state ST_REFUSED
1140ret ST_REFUSED
1141end
1142if gt add get oc_sess_tokens 0 cost get oc_sess_tok_budget 0
1143put oc_cur_err 0 ERR_BUDGET_EXHAUSTED
1144oc_set_refusal_msg ERR_BUDGET_EXHAUSTED
1145oc_tr_state ST_RECORD
1146oc_audit_append ST_REFUSED
1147put oc_refuse_cnt 0 add get oc_refuse_cnt 0 1
1148oc_tr_state ST_REFUSED
1149ret ST_REFUSED
1150end
1151
1152note 2e. Verify argument safety policy
1153kind = get oc_t_kind tid
1154if not oc_check_arg_policy kind
1155put oc_cur_err 0 ERR_ARG_POLICY
1156oc_set_refusal_msg ERR_ARG_POLICY
1157oc_tr_state ST_RECORD
1158oc_audit_append ST_REFUSED
1159put oc_refuse_cnt 0 add get oc_refuse_cnt 0 1
1160oc_tr_state ST_REFUSED
1161ret ST_REFUSED
1162end
1163
1164note Phase 3: POLICY_CHECK -> DISPATCH (authorized execution)
1165oc_tr_state ST_DISPATCH
1166put oc_sess_steps 0 add get oc_sess_steps 0 1
1167put oc_sess_tokens 0 add get oc_sess_tokens 0 cost
1168put oc_dispatch_cnt 0 add get oc_dispatch_cnt 0 1
1169oc_dispatch_tool kind
1170
1171note Phase 4: DISPATCH -> RECORD -> DONE
1172oc_tr_state ST_RECORD
1173oc_ctx_push_res 2
1174oc_audit_append ST_DONE
1175oc_tr_state ST_DONE
1176ret ST_DONE
1177end
1178
1179fn oc_out_byte b uchar to void
1180p = get oc_out_len 0
1181if lt p OC_OUT_CAP
1182put oc_out p b
1183put oc_out_len 0 add p 1
1184end
1185end
1186
1187fn oc_out_str s view uchar * to void
1188n = len s
1189for i ulong n bound 256
1190oc_out_byte get s i
1191end
1192end
1193
1194fn oc_out_esc_byte b uchar to void
1195if or eq b 34 eq b 92
1196oc_out_byte 92
1197oc_out_byte b
1198ret
1199end
1200if eq b 10
1201oc_out_byte 92
1202oc_out_byte 110
1203ret
1204end
1205if eq b 9
1206oc_out_byte 92
1207oc_out_byte 116
1208ret
1209end
1210oc_out_byte b
1211end
1212
1213fn oc_out_u64 v ulong to void
1214if eq v 0
1215oc_out_byte 48
1216ret
1217end
1218buf = new uchar 24 0
1219k = ulong 0
1220x = v
1221while gt x 0 bound 24
1222d = rem x 10
1223put buf k uchar add 48 d
1224x = div x 10
1225k = add k 1
1226end
1227while gt k 0 bound 24
1228k = sub k 1
1229oc_out_byte get buf k
1230end
1231end
1232
1233fn oc_out_hex64 v ulong to void
1234for i ulong 16
1235sh = mul sub 15 i 4
1236nib = and shr v sh 15
1237oc_out_byte get oc_hex nib
1238end
1239end
1240
1241fn oc_out_state_name st ulong to void
1242if eq st ST_IDLE
1243oc_out_str sn_idle
1244ret
1245end
1246if eq st ST_PLAN
1247oc_out_str sn_plan
1248ret
1249end
1250if eq st ST_POLICY_CHECK
1251oc_out_str sn_policy
1252ret
1253end
1254if eq st ST_DISPATCH
1255oc_out_str sn_dispatch
1256ret
1257end
1258if eq st ST_RECORD
1259oc_out_str sn_record
1260ret
1261end
1262if eq st ST_DONE
1263oc_out_str sn_done
1264ret
1265end
1266oc_out_str sn_refused
1267end
1268
1269fn oc_out_err_name err ulong to void
1270if eq err ERR_NONE
1271oc_out_str en_none
1272ret
1273end
1274if eq err ERR_MALFORMED
1275oc_out_str en_mal
1276ret
1277end
1278if eq err ERR_UNKNOWN_TOOL
1279oc_out_str en_unk
1280ret
1281end
1282if eq err ERR_CAP_DENIED
1283oc_out_str en_cap
1284ret
1285end
1286if eq err ERR_BUDGET_EXHAUSTED
1287oc_out_str en_bud
1288ret
1289end
1290if eq err ERR_PRIV_ESCALATION
1291oc_out_str en_esc
1292ret
1293end
1294oc_out_str en_arg
1295end
1296
1297fn oc_format_turn_jsonl to ulong
1298put oc_out_len 0 0
1299oc_out_str jl_1
1300oc_out_u64 get oc_sess_turns 0
1301oc_out_str jl_2
1302id_n = get oc_call_id_len 0
1303for i ulong id_n bound 64
1304oc_out_esc_byte get oc_call_id i
1305end
1306oc_out_str jl_3
1307oc_out_state_name get oc_state 0
1308oc_out_str jl_4
1309tl = get oc_trace_len 0
1310for t ulong tl bound 8
1311if gt t 0
1312oc_out_str sn_gt
1313end
1314oc_out_state_name get oc_trace t
1315end
1316oc_out_str jl_5
1317tn_l = get oc_req_tool_len 0
1318for j ulong tn_l bound 64
1319oc_out_esc_byte get oc_req_tool j
1320end
1321oc_out_str jl_6
1322oc_out_u64 get oc_cur_req_caps 0
1323oc_out_str jl_7
1324oc_out_u64 get oc_sess_caps 0
1325oc_out_str jl_8
1326oc_out_u64 get oc_sess_tokens 0
1327oc_out_str jl_9
1328oc_out_u64 get oc_sess_tok_budget 0
1329oc_out_str jl_10
1330oc_out_err_name get oc_cur_err 0
1331oc_out_str jl_11
1332oc_out_hex64 get oc_chain_hash 0
1333oc_out_str jl_12
1334rn = get oc_res_len 0
1335for r ulong rn bound 512
1336oc_out_esc_byte get oc_res r
1337end
1338oc_out_str jl_13
1339ret get oc_out_len 0
1340end
1341
1342fn oc_write_out fd int total ulong to bool
1343sent = ulong 0
1344while lt sent total bound 4096
1345k = write fd oc_out sent sub total sent
1346if le k 0
1347ret false
1348end
1349sent = add sent ulong k
1350end
1351ret true
1352end