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