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 / aegis / test.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note core/aegis/test.om: Comprehensive AEGIS runtime behavioral test suite (pure OM-2). |
| 3 | note Compose with: |
| 4 | note si/directives/om2.om om/tls/sha2.om core/aegis/dag.om core/aegis/heartbeat.om |
| 5 | note core/aegis/policy.om core/aegis/journal.om core/aegis/gateway.om core/aegis/test.om |
| 6 | note |
| 7 | note Covers all 5 modules + positive workflows + all required negative/refusal controls: |
| 8 | note 1. DAG: diamond topology, priority selection, Kahn order, failure/cancel BLOCKED propagation |
| 9 | note 2. DAG NEGATIVE: self-dependency, 2-cycle, 3-cycle, duplicate task/edge, unknown prereq refused |
| 10 | note 3. Heartbeat & Lease: registration, monotonic tick, exclusive lease, renewal, release, concurrency cap |
| 11 | note 4. Heartbeat NEGATIVE: expired heartbeat reclaims lease, stale lease generation refused, non-monotonic tick refused |
| 12 | note 5. Policy & Auth: fail-closed unconfigured token, short/invalid token refused, constant-time verify, |
| 13 | note unverified envelope & replay nonce refused, privilege escalation refused, skill/cmd allowlist, emergency stop |
| 14 | note 6. Journal: SHA-256 hash chain, sequence monotonicity, replay reconstruction to DAG |
| 15 | note 7. Journal NEGATIVE: tampered payload caught, broken prev_hash caught, torn commit marker caught, |
| 16 | note truncated wire stream caught, replay refuses tampered chain |
| 17 | note 8. Gateway E2E: /health, /tasks, /tasks/submit (201 + 409 cycle + 401 unauth + 403 escalation + 400 bad), |
| 18 | note /heartbeat (tick + lease reclaim), /events (SSE with SHA-256 hashes), WS frames |
| 19 | |
| 20 | text t_pass "PASS " |
| 21 | text t_fail "FAIL " |
| 22 | text t_nl "\n" |
| 23 | text t_done_ok "AEGIS TEST SUITE PASS\n" |
| 24 | text t_done_bad "AEGIS TEST SUITE FAIL\n" |
| 25 | |
| 26 | text t_n1 "dag_diamond_priority_and_kahn" |
| 27 | text t_n2 "dag_failure_and_cancel_propagation" |
| 28 | text t_n3 "dag_negative_cycles_and_duplicates_refused" |
| 29 | text t_n4 "heartbeat_lease_lifecycle_and_concurrency_cap" |
| 30 | text t_n5 "heartbeat_negative_expiry_reclaims_and_stale_gen_refused" |
| 31 | text t_n6 "policy_auth_envelopes_escalation_and_allowlist" |
| 32 | text t_n7 "journal_sha256_chain_and_dag_replay" |
| 33 | text t_n8 "journal_negative_tamper_and_torn_records_caught" |
| 34 | text t_n9 "gateway_routes_auth_sse_ws_and_negative_controls" |
| 35 | |
| 36 | text t_tok_good "aegis_operator_token_0123456789abcdef" |
| 37 | text t_tok_bad "aegis_operator_token_0123456789abcdee" |
| 38 | text t_tok_short "too_short_token" |
| 39 | text t_tok_space "aegis_operator_token_0123456789a bcdef" |
| 40 | text t_tok_empty "" |
| 41 | |
| 42 | text t_m_get "GET" |
| 43 | text t_m_post "POST" |
| 44 | text t_p_health "/health" |
| 45 | text t_p_tasks "/tasks" |
| 46 | text t_p_submit "/tasks/submit" |
| 47 | text t_p_hb "/heartbeat" |
| 48 | text t_p_events "/events" |
| 49 | text t_p_bad "/nonexistent" |
| 50 | text t_p_trav "/tasks/../health" |
| 51 | |
| 52 | text t_b_empty "" |
| 53 | text t_b_sub1 "id=101 prio=10 kind=1" |
| 54 | text t_b_sub2 "id=102 prio=25 kind=1 prereq=101" |
| 55 | text t_b_cyc "id=101 prio=10 prereq=102" |
| 56 | text t_b_bad "id=abc prio=10" |
| 57 | text t_b_hb_reg "tick=1 agent=7 cap=2" |
| 58 | text t_b_hb_exp "tick=6" |
| 59 | |
| 60 | text t_sk_ok "read_file" |
| 61 | text t_sk_bad "Bash_eval" |
| 62 | text t_sk_unk "exec" |
| 63 | text t_cmd_ok "git status" |
| 64 | text t_cmd_rm "rm -rf / --no-preserve-root" |
| 65 | text t_cmd_semi "ls;id" |
| 66 | text t_env_p1 "payload-alpha" |
| 67 | text t_env_p2 "payload-tampered" |
| 68 | text t_jrn_p "ev-data" |
| 69 | text t_ws_ping "ping" |
| 70 | text t_ws_bad "shell_escape" |
| 71 | |
| 72 | text t_s_healthy "\"status\":\"healthy\"" |
| 73 | text t_s_created "\"status\":\"created\"" |
| 74 | text t_s_cycle "\"dependency_cycle_refused\"" |
| 75 | text t_s_sse_ev "event: " |
| 76 | text t_s_sse_hash "\"hash\":\"" |
| 77 | text t_s_pong "\"type\":\"pong\"" |
| 78 | |
| 79 | global t_fails ulong 1 |
| 80 | |
| 81 | fn t_w s view uchar * to void |
| 82 | n = len s |
| 83 | off = ulong 0 |
| 84 | while lt off n bound 256 |
| 85 | k = write 1 s off sub n off |
| 86 | if le k 0 |
| 87 | break |
| 88 | end |
| 89 | off = add off ulong k |
| 90 | end |
| 91 | end |
| 92 | |
| 93 | fn t_report name view uchar * ok bool to void |
| 94 | if ok |
| 95 | t_w t_pass |
| 96 | else |
| 97 | t_w t_fail |
| 98 | put t_fails 0 add get t_fails 0 1 |
| 99 | end |
| 100 | t_w name |
| 101 | t_w t_nl |
| 102 | end |
| 103 | |
| 104 | note Check if needle appears inside gw_resp[0..gw_resp_len). |
| 105 | fn t_resp_contains needle view uchar * to bool |
| 106 | rlen = gw_resp_len |
| 107 | nlen = len needle |
| 108 | if or eq nlen 0 gt nlen rlen |
| 109 | ret false |
| 110 | end |
| 111 | lim = add sub rlen nlen 1 |
| 112 | for i ulong lim bound 8192 |
| 113 | matched = true |
| 114 | for j ulong nlen bound 128 |
| 115 | if ne get gw_resp add i j get needle j |
| 116 | matched = false |
| 117 | break |
| 118 | end |
| 119 | end |
| 120 | if matched |
| 121 | ret true |
| 122 | end |
| 123 | end |
| 124 | ret false |
| 125 | end |
| 126 | |
| 127 | note Test 1: Diamond DAG (1 -> 2, 1 -> 3, 2 -> 4, 3 -> 4), priority selection, Kahn order. |
| 128 | fn test_dag_diamond to bool |
| 129 | dag_reset |
| 130 | if ne dag_add_task 1 10 1 DAG_OK |
| 131 | ret false |
| 132 | end |
| 133 | if ne dag_add_task 2 20 1 DAG_OK |
| 134 | ret false |
| 135 | end |
| 136 | if ne dag_add_task 3 50 1 DAG_OK |
| 137 | ret false |
| 138 | end |
| 139 | if ne dag_add_task 4 100 1 DAG_OK |
| 140 | ret false |
| 141 | end |
| 142 | if ne dag_add_dep 2 1 DAG_OK |
| 143 | ret false |
| 144 | end |
| 145 | if ne dag_add_dep 3 1 DAG_OK |
| 146 | ret false |
| 147 | end |
| 148 | if ne dag_add_dep 4 2 DAG_OK |
| 149 | ret false |
| 150 | end |
| 151 | if ne dag_add_dep 4 3 DAG_OK |
| 152 | ret false |
| 153 | end |
| 154 | if ne dag_validate_kahn DAG_OK |
| 155 | ret false |
| 156 | end |
| 157 | order = new ulong 8 0 |
| 158 | peeled = dag_topo_order order |
| 159 | if ne peeled 4 |
| 160 | ret false |
| 161 | end |
| 162 | if or ne get order 0 1 ne get order 3 4 |
| 163 | ret false |
| 164 | end |
| 165 | rdy = dag_solve_ready |
| 166 | if ne rdy 1 |
| 167 | ret false |
| 168 | end |
| 169 | if or ne dag_get_state 1 TASK_READY ne dag_get_state 2 TASK_PENDING |
| 170 | ret false |
| 171 | end |
| 172 | if ne dag_start_task 1 DAG_OK |
| 173 | ret false |
| 174 | end |
| 175 | if ne dag_complete_task 1 DAG_OK |
| 176 | ret false |
| 177 | end |
| 178 | note Now both 2 (prio 20) and 3 (prio 50) are READY; 4 is still PENDING. |
| 179 | if or ne dag_get_state 2 TASK_READY ne dag_get_state 3 TASK_READY |
| 180 | ret false |
| 181 | end |
| 182 | if ne dag_get_state 4 TASK_PENDING |
| 183 | ret false |
| 184 | end |
| 185 | note Priority selection must pick task 3 (slot 2, prio 50) ahead of task 2 (slot 1, prio 20). |
| 186 | nxt = dag_select_next |
| 187 | if ne nxt 2 |
| 188 | ret false |
| 189 | end |
| 190 | if ne dag_start_task 3 DAG_OK |
| 191 | ret false |
| 192 | end |
| 193 | if ne dag_complete_task 3 DAG_OK |
| 194 | ret false |
| 195 | end |
| 196 | note Task 4 must STILL be PENDING because task 2 is not done yet. |
| 197 | if ne dag_get_state 4 TASK_PENDING |
| 198 | ret false |
| 199 | end |
| 200 | if ne dag_select_next 1 |
| 201 | ret false |
| 202 | end |
| 203 | if ne dag_start_task 2 DAG_OK |
| 204 | ret false |
| 205 | end |
| 206 | if ne dag_complete_task 2 DAG_OK |
| 207 | ret false |
| 208 | end |
| 209 | note Now task 4 must be READY! |
| 210 | if ne dag_get_state 4 TASK_READY |
| 211 | ret false |
| 212 | end |
| 213 | if ne dag_start_task 4 DAG_OK |
| 214 | ret false |
| 215 | end |
| 216 | if ne dag_complete_task 4 DAG_OK |
| 217 | ret false |
| 218 | end |
| 219 | ret eq dag_count_state TASK_DONE 4 |
| 220 | end |
| 221 | |
| 222 | note Test 2: Transitive failure and cancellation propagation to BLOCKED downstream tasks. |
| 223 | fn test_dag_propagation to bool |
| 224 | dag_reset |
| 225 | rc = dag_add_task 10 10 1 |
| 226 | rc = or rc dag_add_task 11 10 1 |
| 227 | rc = or rc dag_add_task 12 10 1 |
| 228 | rc = or rc dag_add_task 13 10 1 |
| 229 | rc = or rc dag_add_task 14 10 1 |
| 230 | rc = or rc dag_add_task 15 10 1 |
| 231 | rc = or rc dag_add_task 16 10 1 |
| 232 | if ne rc DAG_OK |
| 233 | ret false |
| 234 | end |
| 235 | note Chain 10 -> 11 -> 12, fan-out 10 -> 13, independent 14, cancel chain 15 -> 16. |
| 236 | if ne dag_add_dep 11 10 DAG_OK |
| 237 | ret false |
| 238 | end |
| 239 | if ne dag_add_dep 12 11 DAG_OK |
| 240 | ret false |
| 241 | end |
| 242 | if ne dag_add_dep 13 10 DAG_OK |
| 243 | ret false |
| 244 | end |
| 245 | if ne dag_add_dep 16 15 DAG_OK |
| 246 | ret false |
| 247 | end |
| 248 | rdy = dag_solve_ready |
| 249 | if ne rdy 3 |
| 250 | ret false |
| 251 | end |
| 252 | if ne dag_start_task 10 DAG_OK |
| 253 | ret false |
| 254 | end |
| 255 | if ne dag_fail_task 10 77 DAG_OK |
| 256 | ret false |
| 257 | end |
| 258 | if ne dag_get_state 10 TASK_FAILED |
| 259 | ret false |
| 260 | end |
| 261 | if ne dag_get_fail_code 10 77 |
| 262 | ret false |
| 263 | end |
| 264 | note 11, 12, 13 must all be BLOCKED; independent 14 must stay READY! |
| 265 | if or ne dag_get_state 11 TASK_BLOCKED ne dag_get_state 12 TASK_BLOCKED |
| 266 | ret false |
| 267 | end |
| 268 | if or ne dag_get_state 13 TASK_BLOCKED ne dag_get_state 14 TASK_READY |
| 269 | ret false |
| 270 | end |
| 271 | if or ne dag_get_blocked_by 11 10 ne dag_get_blocked_by 12 11 |
| 272 | ret false |
| 273 | end |
| 274 | note Cancelling 15 must block downstream 16. |
| 275 | if ne dag_cancel_task 15 DAG_OK |
| 276 | ret false |
| 277 | end |
| 278 | if or ne dag_get_state 15 TASK_CANCELLED ne dag_get_state 16 TASK_BLOCKED |
| 279 | ret false |
| 280 | end |
| 281 | ret eq dag_get_blocked_by 16 15 |
| 282 | end |
| 283 | |
| 284 | note Test 3 (NEGATIVE): Self-dependency, 2-cycle, 3-cycle, duplicate task/edge, unknown prereq refused. |
| 285 | fn test_dag_negative_cycles to bool |
| 286 | dag_reset |
| 287 | if ne dag_add_task 0 1 1 DAG_ERR_EMPTY_ID |
| 288 | ret false |
| 289 | end |
| 290 | if ne dag_add_task 20 1 1 DAG_OK |
| 291 | ret false |
| 292 | end |
| 293 | if ne dag_add_task 20 1 1 DAG_ERR_DUPLICATE_TASK |
| 294 | ret false |
| 295 | end |
| 296 | if ne dag_add_dep 20 20 DAG_ERR_SELF_DEP |
| 297 | ret false |
| 298 | end |
| 299 | if ne dag_add_dep 20 999 DAG_ERR_UNKNOWN_TASK |
| 300 | ret false |
| 301 | end |
| 302 | if ne dag_add_task 21 1 1 DAG_OK |
| 303 | ret false |
| 304 | end |
| 305 | if ne dag_add_task 22 1 1 DAG_OK |
| 306 | ret false |
| 307 | end |
| 308 | if ne dag_add_dep 21 20 DAG_OK |
| 309 | ret false |
| 310 | end |
| 311 | if ne dag_add_dep 21 20 DAG_ERR_DUPLICATE_EDGE |
| 312 | ret false |
| 313 | end |
| 314 | note 2-node cycle: 20 -> 21 exists, so 20 depending on 21 MUST be refused! |
| 315 | if ne dag_add_dep 20 21 DAG_ERR_CYCLE |
| 316 | ret false |
| 317 | end |
| 318 | note 3-node cycle: add 21 -> 22, then 20 depending on 22 MUST be refused! |
| 319 | if ne dag_add_dep 22 21 DAG_OK |
| 320 | ret false |
| 321 | end |
| 322 | if ne dag_add_dep 20 22 DAG_ERR_CYCLE |
| 323 | ret false |
| 324 | end |
| 325 | note Graph must remain intact and acyclic with exactly 2 edges. |
| 326 | if ne dag_edge_count 2 |
| 327 | ret false |
| 328 | end |
| 329 | ret eq dag_validate_kahn DAG_OK |
| 330 | end |
| 331 | |
| 332 | note Test 4: Heartbeat scheduler lease lifecycle, exclusive lease, renewal, release, concurrency cap. |
| 333 | fn test_hb_lifecycle to bool |
| 334 | if ne hb_init 2 3 4 HB_OK |
| 335 | ret false |
| 336 | end |
| 337 | if ne hb_register_agent 1 2 HB_OK |
| 338 | ret false |
| 339 | end |
| 340 | if ne hb_register_agent 2 2 HB_OK |
| 341 | ret false |
| 342 | end |
| 343 | if ne hb_register_agent 1 2 HB_ERR_DUPLICATE |
| 344 | ret false |
| 345 | end |
| 346 | g1 = new ulong 1 0 |
| 347 | g2 = new ulong 1 0 |
| 348 | g3 = new ulong 1 0 |
| 349 | if ne hb_acquire_lease 501 1 g1 HB_OK |
| 350 | ret false |
| 351 | end |
| 352 | if eq get g1 0 0 |
| 353 | ret false |
| 354 | end |
| 355 | note Exclusive lease: agent 2 cannot lease task 501 while agent 1 holds it. |
| 356 | if ne hb_acquire_lease 501 2 g2 HB_ERR_ALREADY_LEASED |
| 357 | ret false |
| 358 | end |
| 359 | note Agent 2 leases task 502; now active_leases == 2 == max_concurrency. |
| 360 | if ne hb_acquire_lease 502 2 g2 HB_OK |
| 361 | ret false |
| 362 | end |
| 363 | if ne hb_active_leases 2 |
| 364 | ret false |
| 365 | end |
| 366 | note Concurrency cap: 3rd lease MUST be refused! |
| 367 | if ne hb_acquire_lease 503 1 g3 HB_ERR_CONCURRENCY_CAP |
| 368 | ret false |
| 369 | end |
| 370 | note Advance tick to 2, send heartbeats, renew lease 501 so it extends to tick 6. |
| 371 | if ne hb_tick 2 HB_OK |
| 372 | ret false |
| 373 | end |
| 374 | if ne hb_heartbeat 1 2 HB_OK |
| 375 | ret false |
| 376 | end |
| 377 | if ne hb_heartbeat 2 2 HB_OK |
| 378 | ret false |
| 379 | end |
| 380 | if ne hb_renew_lease 501 1 get g1 0 HB_OK |
| 381 | ret false |
| 382 | end |
| 383 | note Wrong owner cannot renew or release task 501. |
| 384 | if ne hb_renew_lease 501 2 get g1 0 HB_ERR_WRONG_OWNER |
| 385 | ret false |
| 386 | end |
| 387 | note Release task 502, then 503 can be leased! |
| 388 | if ne hb_release_lease 502 2 get g2 0 HB_OK |
| 389 | ret false |
| 390 | end |
| 391 | if ne hb_active_leases 1 |
| 392 | ret false |
| 393 | end |
| 394 | if ne hb_acquire_lease 503 1 g3 HB_OK |
| 395 | ret false |
| 396 | end |
| 397 | ret gt get g3 0 get g2 0 |
| 398 | end |
| 399 | |
| 400 | note Test 5 (NEGATIVE): Expired heartbeat reclaims lease; stale lease generation refused; non-monotonic tick refused. |
| 401 | fn test_hb_negative_expiry_and_stale to bool |
| 402 | if ne hb_init 4 2 5 HB_OK |
| 403 | ret false |
| 404 | end |
| 405 | if ne hb_register_agent 10 2 HB_OK |
| 406 | ret false |
| 407 | end |
| 408 | if ne hb_register_agent 20 2 HB_OK |
| 409 | ret false |
| 410 | end |
| 411 | g1 = new ulong 1 0 |
| 412 | g2 = new ulong 1 0 |
| 413 | if ne hb_acquire_lease 700 10 g1 HB_OK |
| 414 | ret false |
| 415 | end |
| 416 | note Tick 1 and 2: agent 20 heartbeats, agent 10 goes silent. |
| 417 | if ne hb_tick 1 HB_OK |
| 418 | ret false |
| 419 | end |
| 420 | if ne hb_heartbeat 20 1 HB_OK |
| 421 | ret false |
| 422 | end |
| 423 | if ne hb_tick 2 HB_OK |
| 424 | ret false |
| 425 | end |
| 426 | if ne hb_heartbeat 20 2 HB_OK |
| 427 | ret false |
| 428 | end |
| 429 | note Non-monotonic tick MUST be refused! |
| 430 | if ne hb_tick 1 HB_ERR_NON_MONOTONIC |
| 431 | ret false |
| 432 | end |
| 433 | note Tick 3: agent 10 gap = 3 - 0 = 3 > agent_ttl(2) -> EXPIRED and lease 700 reclaimed! |
| 434 | if ne hb_tick 3 HB_OK |
| 435 | ret false |
| 436 | end |
| 437 | if ne hb_agent_status 10 AGENT_EXPIRED |
| 438 | ret false |
| 439 | end |
| 440 | if ne hb_agent_status 20 AGENT_ALIVE |
| 441 | ret false |
| 442 | end |
| 443 | if or ne hb_last_reclaimed 1 ne hb_get_reclaimed 0 700 |
| 444 | ret false |
| 445 | end |
| 446 | if ne hb_active_leases 0 |
| 447 | ret false |
| 448 | end |
| 449 | if not hb_is_degraded |
| 450 | ret false |
| 451 | end |
| 452 | note Stale generation refusal #1: agent 10 tries to renew/verify/release with g1 -> REFUSED! |
| 453 | if ne hb_renew_lease 700 10 get g1 0 HB_ERR_STALE_GEN |
| 454 | ret false |
| 455 | end |
| 456 | if ne hb_verify_lease 700 10 get g1 0 HB_ERR_STALE_GEN |
| 457 | ret false |
| 458 | end |
| 459 | note Agent 20 now acquires task 700 with a strictly higher fencing generation g2 > g1! |
| 460 | if ne hb_acquire_lease 700 20 g2 HB_OK |
| 461 | ret false |
| 462 | end |
| 463 | if le get g2 0 get g1 0 |
| 464 | ret false |
| 465 | end |
| 466 | note Even if agent 10 re-registers, its old generation g1 MUST STILL be refused! |
| 467 | if ne hb_register_agent 10 2 HB_OK |
| 468 | ret false |
| 469 | end |
| 470 | if ne hb_verify_lease 700 10 get g1 0 HB_ERR_STALE_GEN |
| 471 | ret false |
| 472 | end |
| 473 | if ne hb_release_lease 700 10 get g1 0 HB_ERR_STALE_GEN |
| 474 | ret false |
| 475 | end |
| 476 | ret eq hb_verify_lease 700 20 get g2 0 HB_OK |
| 477 | end |
| 478 | |
| 479 | note Test 6: Policy gate, fail-closed auth, envelope MAC + anti-replay, escalation, allowlist, containment. |
| 480 | fn test_policy_gate to bool |
| 481 | pol_reset |
| 482 | note Fail-closed when no operator token configured: public /health allowed, protected /tasks denied! |
| 483 | if ne pol_authorize_request METHOD_GET ROUTE_HEALTH 0 CAP_NONE false false POL_ALLOW |
| 484 | ret false |
| 485 | end |
| 486 | if ne pol_authorize_request METHOD_GET ROUTE_TASKS 0 ROLE_OPERATOR true true POL_DENY_NO_TOKEN_CONFIG |
| 487 | ret false |
| 488 | end |
| 489 | note Token validation rules from auth.rs: short or space-containing tokens refused. |
| 490 | if ne pol_set_operator_token t_tok_short POL_DENY_BAD_TOKEN |
| 491 | ret false |
| 492 | end |
| 493 | if ne pol_set_operator_token t_tok_space POL_DENY_BAD_TOKEN |
| 494 | ret false |
| 495 | end |
| 496 | if ne pol_set_operator_token t_tok_good POL_ALLOW |
| 497 | ret false |
| 498 | end |
| 499 | if not pol_verify_token t_tok_good |
| 500 | ret false |
| 501 | end |
| 502 | if pol_verify_token t_tok_bad |
| 503 | ret false |
| 504 | end |
| 505 | if pol_verify_token t_tok_empty |
| 506 | ret false |
| 507 | end |
| 508 | note Register worker principal (id=5) with ROLE_WORKER (no CAP_TASK_SUBMIT or CAP_POLICY_ADMIN). |
| 509 | key = new uchar 32 0 |
| 510 | for i ulong 32 |
| 511 | put key i uchar add 0x30 i |
| 512 | end |
| 513 | if ne pol_register_principal 5 ROLE_WORKER key POL_ALLOW |
| 514 | ret false |
| 515 | end |
| 516 | note Seal and verify signed action envelope with monotonic nonce=1. |
| 517 | mac = new uchar 32 0 |
| 518 | if ne pol_seal_envelope 5 ROUTE_HEARTBEAT 1 t_env_p1 mac POL_ALLOW |
| 519 | ret false |
| 520 | end |
| 521 | if ne pol_verify_envelope 5 ROUTE_HEARTBEAT 1 t_env_p1 mac POL_ALLOW |
| 522 | ret false |
| 523 | end |
| 524 | note Replay of same nonce=1 MUST be refused! |
| 525 | if ne pol_verify_envelope 5 ROUTE_HEARTBEAT 1 t_env_p1 mac POL_DENY_UNVERIFIED_ENV |
| 526 | ret false |
| 527 | end |
| 528 | note Tampered payload with fresh nonce=2 and old mac MUST be refused! |
| 529 | if ne pol_verify_envelope 5 ROUTE_HEARTBEAT 2 t_env_p2 mac POL_DENY_UNVERIFIED_ENV |
| 530 | ret false |
| 531 | end |
| 532 | note Unverified envelope on protected route MUST be refused! |
| 533 | if ne pol_authorize_request METHOD_POST ROUTE_HEARTBEAT 5 ROLE_WORKER true false POL_DENY_UNVERIFIED_ENV |
| 534 | ret false |
| 535 | end |
| 536 | note Privilege escalation: worker (ROLE_WORKER) requesting CAP_TASK_SUBMIT or CAP_POLICY_ADMIN MUST be refused! |
| 537 | if ne pol_authorize_request METHOD_POST ROUTE_TASKS_SUBMIT 5 or ROLE_WORKER CAP_TASK_SUBMIT true true POL_DENY_ESCALATION |
| 538 | ret false |
| 539 | end |
| 540 | if ne pol_check_escalation ROLE_WORKER CAP_POLICY_ADMIN POL_DENY_ESCALATION |
| 541 | ret false |
| 542 | end |
| 543 | note Worker authorized for /heartbeat with valid token + verified envelope + within its caps. |
| 544 | if ne pol_authorize_request METHOD_POST ROUTE_HEARTBEAT 5 ROLE_WORKER true true POL_ALLOW |
| 545 | ret false |
| 546 | end |
| 547 | note Skill and command allowlist (from enforcement.rs). |
| 548 | if ne pol_check_skill t_sk_ok POL_ALLOW |
| 549 | ret false |
| 550 | end |
| 551 | if ne pol_check_skill t_sk_bad POL_DENY_ESCALATION |
| 552 | ret false |
| 553 | end |
| 554 | if ne pol_check_skill t_sk_unk POL_DENY_ESCALATION |
| 555 | ret false |
| 556 | end |
| 557 | if ne pol_check_local_cmd t_cmd_ok POL_ALLOW |
| 558 | ret false |
| 559 | end |
| 560 | if ne pol_check_local_cmd t_cmd_rm POL_DENY_ESCALATION |
| 561 | ret false |
| 562 | end |
| 563 | if ne pol_check_local_cmd t_cmd_semi POL_DENY_ESCALATION |
| 564 | ret false |
| 565 | end |
| 566 | note Emergency stop containment blocks protected routes while keeping /health readable. |
| 567 | pol_set_containment CONTAIN_EMERGENCY_STOP |
| 568 | if ne pol_authorize_request METHOD_GET ROUTE_HEALTH 0 CAP_NONE false false POL_ALLOW |
| 569 | ret false |
| 570 | end |
| 571 | if ne pol_authorize_request METHOD_POST ROUTE_HEARTBEAT 5 ROLE_WORKER true true POL_DENY_CONTAINED |
| 572 | ret false |
| 573 | end |
| 574 | pol_set_containment CONTAIN_OBSERVE |
| 575 | ret true |
| 576 | end |
| 577 | |
| 578 | note Test 7: SHA-256 hash-chained journal + replay reconstruction into DAG state. |
| 579 | fn test_journal_chain_and_replay to bool |
| 580 | jrn_reset |
| 581 | dag_reset |
| 582 | if ne jrn_append EVT_TASK_SUBMITTED 1 10 1 t_jrn_p JRN_OK |
| 583 | ret false |
| 584 | end |
| 585 | if ne jrn_append EVT_TASK_SUBMITTED 2 20 1 t_jrn_p JRN_OK |
| 586 | ret false |
| 587 | end |
| 588 | if ne jrn_append EVT_TASK_SUBMITTED 3 30 1 t_jrn_p JRN_OK |
| 589 | ret false |
| 590 | end |
| 591 | if ne jrn_append EVT_TASK_DEP 2 1 1 t_jrn_p JRN_OK |
| 592 | ret false |
| 593 | end |
| 594 | if ne jrn_append EVT_TASK_DEP 3 2 1 t_jrn_p JRN_OK |
| 595 | ret false |
| 596 | end |
| 597 | if ne jrn_append EVT_TASK_STARTED 1 7 2 t_jrn_p JRN_OK |
| 598 | ret false |
| 599 | end |
| 600 | if ne jrn_append EVT_TASK_FAILED 1 99 3 t_jrn_p JRN_OK |
| 601 | ret false |
| 602 | end |
| 603 | if or ne jrn_count 7 ne jrn_last_seq 7 |
| 604 | ret false |
| 605 | end |
| 606 | if ne jrn_verify_chain JRN_OK |
| 607 | ret false |
| 608 | end |
| 609 | note Verify hashes of record 0 and record 1 are non-zero and distinct. |
| 610 | h0 = new uchar 32 0 |
| 611 | h1 = new uchar 32 0 |
| 612 | if or ne jrn_copy_hash 0 h0 JRN_OK ne jrn_copy_hash 1 h1 JRN_OK |
| 613 | ret false |
| 614 | end |
| 615 | nz = ulong 0 |
| 616 | diff = ulong 0 |
| 617 | for i ulong 32 |
| 618 | nz = or nz ulong get h0 i |
| 619 | diff = or diff ulong xor get h0 i get h1 i |
| 620 | end |
| 621 | if or eq nz 0 eq diff 0 |
| 622 | ret false |
| 623 | end |
| 624 | note Replay into fresh DAG and confirm task 1=FAILED, task 2=BLOCKED, task 3=BLOCKED! |
| 625 | if ne jrn_replay_to_dag JRN_OK |
| 626 | ret false |
| 627 | end |
| 628 | if ne dag_get_state 1 TASK_FAILED |
| 629 | ret false |
| 630 | end |
| 631 | if or ne dag_get_state 2 TASK_BLOCKED ne dag_get_state 3 TASK_BLOCKED |
| 632 | ret false |
| 633 | end |
| 634 | ret true |
| 635 | end |
| 636 | |
| 637 | note Test 8 (NEGATIVE): Tampered payload, broken chain, torn commit sentinel, and truncated wire stream caught! |
| 638 | fn test_journal_negative_tamper_and_torn to bool |
| 639 | jrn_reset |
| 640 | if ne jrn_append EVT_TASK_SUBMITTED 40 5 1 t_jrn_p JRN_OK |
| 641 | ret false |
| 642 | end |
| 643 | if ne jrn_append EVT_TASK_SUBMITTED 41 6 2 t_jrn_p JRN_OK |
| 644 | ret false |
| 645 | end |
| 646 | if ne jrn_verify_chain JRN_OK |
| 647 | ret false |
| 648 | end |
| 649 | note Encode intact 2-record wire stream (384 bytes) for wire-level tests. |
| 650 | wire = new uchar 384 0 |
| 651 | if ne jrn_encode_record 0 wire 0 JRN_OK |
| 652 | ret false |
| 653 | end |
| 654 | if ne jrn_encode_record 1 wire 192 JRN_OK |
| 655 | ret false |
| 656 | end |
| 657 | note Negative 1: flip 1 byte in record 0 payload -> JRN_ERR_HASH_MISMATCH + replay REFUSED! |
| 658 | orig = get jrn_payload 0 |
| 659 | put jrn_payload 0 xor orig 0x01 |
| 660 | if ne jrn_verify_chain JRN_ERR_HASH_MISMATCH |
| 661 | ret false |
| 662 | end |
| 663 | if ne jrn_replay_to_dag JRN_ERR_HASH_MISMATCH |
| 664 | ret false |
| 665 | end |
| 666 | put jrn_payload 0 orig |
| 667 | if ne jrn_verify_chain JRN_OK |
| 668 | ret false |
| 669 | end |
| 670 | note Negative 2: corrupt sequence monotonicity -> JRN_ERR_SEQ_GAP! |
| 671 | put jrn_seq 1 99 |
| 672 | if ne jrn_verify_chain JRN_ERR_SEQ_GAP |
| 673 | ret false |
| 674 | end |
| 675 | put jrn_seq 1 2 |
| 676 | note Negative 3: torn commit sentinel -> JRN_ERR_TORN_RECORD! |
| 677 | put jrn_commit 1 0 |
| 678 | if ne jrn_verify_chain JRN_ERR_TORN_RECORD |
| 679 | ret false |
| 680 | end |
| 681 | put jrn_commit 1 JRN_COMMIT_END |
| 682 | note Negative 4: break prev_hash link on record 1 -> JRN_ERR_PREV_HASH_MISMATCH! |
| 683 | orig_ph = get jrn_prev_hash 32 |
| 684 | put jrn_prev_hash 32 xor orig_ph 0xff |
| 685 | if ne jrn_verify_chain JRN_ERR_PREV_HASH_MISMATCH |
| 686 | ret false |
| 687 | end |
| 688 | put jrn_prev_hash 32 orig_ph |
| 689 | note Negative 5: truncated wire stream (370 bytes, not multiple of 192) -> JRN_ERR_TORN_RECORD! |
| 690 | if ne jrn_decode_stream wire 370 JRN_ERR_TORN_RECORD |
| 691 | ret false |
| 692 | end |
| 693 | note Positive wire round-trip of full 384 bytes succeeds and verifies! |
| 694 | if ne jrn_decode_stream wire 384 JRN_OK |
| 695 | ret false |
| 696 | end |
| 697 | ret eq jrn_count 2 |
| 698 | end |
| 699 | |
| 700 | note Test 9: Gateway E2E routes (/health, /tasks, /tasks/submit, /heartbeat, /events, WS) + negative controls. |
| 701 | fn test_gateway_e2e to bool |
| 702 | dag_reset |
| 703 | pol_reset |
| 704 | jrn_reset |
| 705 | gw_reset |
| 706 | if ne hb_init 4 2 5 HB_OK |
| 707 | ret false |
| 708 | end |
| 709 | if ne pol_set_operator_token t_tok_good POL_ALLOW |
| 710 | ret false |
| 711 | end |
| 712 | key = new uchar 32 0 |
| 713 | for i ulong 32 |
| 714 | put key i uchar add 0x50 i |
| 715 | end |
| 716 | if ne pol_register_principal 8 ROLE_WORKER key POL_ALLOW |
| 717 | ret false |
| 718 | end |
| 719 | note 1) Public GET /health -> 200 OK with healthy JSON. |
| 720 | rt = gw_req_begin t_m_get t_p_health |
| 721 | gw_req_auth 0 CAP_NONE t_tok_empty false |
| 722 | if ne gw_req_dispatch t_b_empty HTTP_OK |
| 723 | ret false |
| 724 | end |
| 725 | if not t_resp_contains t_s_healthy |
| 726 | ret false |
| 727 | end |
| 728 | note 2) NEGATIVE: POST /health -> 405 Method Not Allowed. |
| 729 | rt = gw_req_begin t_m_post t_p_health |
| 730 | gw_req_auth 0 ROLE_OPERATOR t_tok_good true |
| 731 | if ne gw_req_dispatch t_b_empty HTTP_METHOD_NOT_ALLOWED |
| 732 | ret false |
| 733 | end |
| 734 | note 3) NEGATIVE: Unauthorized GET /tasks (no token) -> 401 Unauthorized. |
| 735 | rt = gw_req_begin t_m_get t_p_tasks |
| 736 | gw_req_auth 0 ROLE_OPERATOR t_tok_empty true |
| 737 | if ne gw_req_dispatch t_b_empty HTTP_UNAUTHORIZED |
| 738 | ret false |
| 739 | end |
| 740 | note 4) NEGATIVE: Unauthorized POST /tasks/submit (bad token) -> 401 Unauthorized. |
| 741 | rt = gw_req_begin t_m_post t_p_submit |
| 742 | gw_req_auth 0 ROLE_OPERATOR t_tok_bad true |
| 743 | if ne gw_req_dispatch t_b_sub1 HTTP_UNAUTHORIZED |
| 744 | ret false |
| 745 | end |
| 746 | note 5) NEGATIVE: Privilege escalation on POST /tasks/submit by worker principal 8 -> 403 Forbidden. |
| 747 | rt = gw_req_begin t_m_post t_p_submit |
| 748 | gw_req_auth 8 or ROLE_WORKER CAP_TASK_SUBMIT t_tok_good true |
| 749 | if ne gw_req_dispatch t_b_sub1 HTTP_FORBIDDEN |
| 750 | ret false |
| 751 | end |
| 752 | note 6) NEGATIVE: Bad body on POST /tasks/submit -> 400 Bad Request. |
| 753 | rt = gw_req_begin t_m_post t_p_submit |
| 754 | gw_req_auth 0 ROLE_OPERATOR t_tok_good true |
| 755 | if ne gw_req_dispatch t_b_bad HTTP_BAD_REQUEST |
| 756 | ret false |
| 757 | end |
| 758 | note 7) Positive POST /tasks/submit for 101 and 102 (102 depends on 101) -> 201 Created. |
| 759 | rt = gw_req_begin t_m_post t_p_submit |
| 760 | gw_req_auth 0 ROLE_OPERATOR t_tok_good true |
| 761 | if ne gw_req_dispatch t_b_sub1 HTTP_CREATED |
| 762 | ret false |
| 763 | end |
| 764 | if not t_resp_contains t_s_created |
| 765 | ret false |
| 766 | end |
| 767 | rt = gw_req_begin t_m_post t_p_submit |
| 768 | gw_req_auth 0 ROLE_OPERATOR t_tok_good true |
| 769 | if ne gw_req_dispatch t_b_sub2 HTTP_CREATED |
| 770 | ret false |
| 771 | end |
| 772 | note 8) NEGATIVE: Cyclic dependency 101 -> 102 via POST /tasks/submit -> 409 Conflict (dependency_cycle_refused)! |
| 773 | rt = gw_req_begin t_m_post t_p_submit |
| 774 | gw_req_auth 0 ROLE_OPERATOR t_tok_good true |
| 775 | if ne gw_req_dispatch t_b_cyc HTTP_CONFLICT |
| 776 | ret false |
| 777 | end |
| 778 | if not t_resp_contains t_s_cycle |
| 779 | ret false |
| 780 | end |
| 781 | note 9) Authorized GET /tasks -> 200 OK. |
| 782 | rt = gw_req_begin t_m_get t_p_tasks |
| 783 | gw_req_auth 0 ROLE_OPERATOR t_tok_good true |
| 784 | if ne gw_req_dispatch t_b_empty HTTP_OK |
| 785 | ret false |
| 786 | end |
| 787 | note 10) POST /heartbeat registers agent 7 at tick 1, lease task 101, then tick 6 reclaims lease! |
| 788 | rt = gw_req_begin t_m_post t_p_hb |
| 789 | gw_req_auth 0 ROLE_OPERATOR t_tok_good true |
| 790 | if ne gw_req_dispatch t_b_hb_reg HTTP_OK |
| 791 | ret false |
| 792 | end |
| 793 | gen = new ulong 1 0 |
| 794 | if ne hb_acquire_lease 101 7 gen HB_OK |
| 795 | ret false |
| 796 | end |
| 797 | if ne dag_start_task 101 DAG_OK |
| 798 | ret false |
| 799 | end |
| 800 | rt = gw_req_begin t_m_post t_p_hb |
| 801 | gw_req_auth 0 ROLE_OPERATOR t_tok_good true |
| 802 | if ne gw_req_dispatch t_b_hb_exp HTTP_OK |
| 803 | ret false |
| 804 | end |
| 805 | note Reclaimed lease must have returned task 101 to READY! |
| 806 | if ne dag_get_state 101 TASK_READY |
| 807 | ret false |
| 808 | end |
| 809 | note 11) Public GET /events -> 200 OK SSE stream with event lines and SHA-256 hash fields. |
| 810 | rt = gw_req_begin t_m_get t_p_events |
| 811 | gw_req_auth 0 CAP_NONE t_tok_empty false |
| 812 | if ne gw_req_dispatch t_b_empty HTTP_OK |
| 813 | ret false |
| 814 | end |
| 815 | if ne gw_content_type GW_CT_SSE |
| 816 | ret false |
| 817 | end |
| 818 | if not and t_resp_contains t_s_sse_ev t_resp_contains t_s_sse_hash |
| 819 | ret false |
| 820 | end |
| 821 | note 12) Unknown route -> 404, traversal path -> 400. |
| 822 | rt = gw_req_begin t_m_get t_p_bad |
| 823 | gw_req_auth 0 ROLE_OPERATOR t_tok_good true |
| 824 | if ne gw_req_dispatch t_b_empty HTTP_NOT_FOUND |
| 825 | ret false |
| 826 | end |
| 827 | rt = gw_req_begin t_m_get t_p_trav |
| 828 | gw_req_auth 0 ROLE_OPERATOR t_tok_good true |
| 829 | if ne gw_req_dispatch t_b_empty HTTP_BAD_REQUEST |
| 830 | ret false |
| 831 | end |
| 832 | note 13) WS frame routing: unauthorized refused (401), ping allowed (200), unknown refused (400). |
| 833 | if ne gw_ws_frame 0 ROLE_OPERATOR false t_ws_ping HTTP_UNAUTHORIZED |
| 834 | ret false |
| 835 | end |
| 836 | if ne gw_ws_frame 0 ROLE_OPERATOR true t_ws_ping HTTP_OK |
| 837 | ret false |
| 838 | end |
| 839 | if not t_resp_contains t_s_pong |
| 840 | ret false |
| 841 | end |
| 842 | if ne gw_ws_frame 0 ROLE_OPERATOR true t_ws_bad HTTP_BAD_REQUEST |
| 843 | ret false |
| 844 | end |
| 845 | note Final check: entire journal chain logged during E2E run is cryptographically valid! |
| 846 | if gt rt 999 |
| 847 | ret false |
| 848 | end |
| 849 | ret eq jrn_verify_chain JRN_OK |
| 850 | end |
| 851 | |
| 852 | fn main to int |
| 853 | put t_fails 0 0 |
| 854 | t_report t_n1 test_dag_diamond |
| 855 | t_report t_n2 test_dag_propagation |
| 856 | t_report t_n3 test_dag_negative_cycles |
| 857 | t_report t_n4 test_hb_lifecycle |
| 858 | t_report t_n5 test_hb_negative_expiry_and_stale |
| 859 | t_report t_n6 test_policy_gate |
| 860 | t_report t_n7 test_journal_chain_and_replay |
| 861 | t_report t_n8 test_journal_negative_tamper_and_torn |
| 862 | t_report t_n9 test_gateway_e2e |
| 863 | if eq get t_fails 0 0 |
| 864 | t_w t_done_ok |
| 865 | ret 0 |
| 866 | end |
| 867 | t_w t_done_bad |
| 868 | ret 1 |
| 869 | end |