forked from omnigent-ai/omnigent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_codex_native_hook.py
More file actions
1274 lines (1047 loc) · 43.8 KB
/
Copy pathtest_codex_native_hook.py
File metadata and controls
1274 lines (1047 loc) · 43.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Tests for the codex-native policy hook entrypoint (``evaluate-policy``)."""
from __future__ import annotations
import io
import json
import sys
from pathlib import Path
import httpx
import pytest
from omnigent import codex_native_hook, native_policy_hook
from omnigent.codex_native_bridge import (
CodexNativeBridgeState,
codex_home_for_bridge_dir,
prepare_bridge_dir,
write_bridge_state,
write_policy_hook_config,
)
from tests.native_hook_helpers import make_failing_client
class _DenyHttpxClient:
"""
Sync HTTP client stub that records the request and returns a DENY verdict.
Returns a real :class:`httpx.Response` so the hook exercises its real
JSON parsing + verdict-mapping path rather than a mock's attributes.
:param headers: Headers passed to :class:`httpx.Client`.
:param timeout: Timeout passed to :class:`httpx.Client`.
"""
captured: dict[str, object] = {}
def __init__(self, *, headers: dict[str, str], timeout: object) -> None:
"""
Capture constructor inputs for later assertions.
:param headers: HTTP headers the hook builds for AP.
:param timeout: HTTP timeout object.
:returns: None.
"""
_DenyHttpxClient.captured["headers"] = headers
def __enter__(self) -> _DenyHttpxClient:
"""
Enter the context manager.
:returns: This stub client.
"""
return self
def __exit__(self, *args: object) -> None:
"""
Exit the context manager.
:param args: Exception details (unused).
:returns: None.
"""
del args
def post(self, url: str, *, json: dict[str, object]) -> httpx.Response:
"""
Record the outgoing request and return a DENY EvaluationResponse.
:param url: Target Omnigent URL.
:param json: Request body (the EvaluationRequest).
:returns: A real 200 response carrying a DENY verdict.
"""
_DenyHttpxClient.captured["url"] = url
_DenyHttpxClient.captured["json"] = json
return httpx.Response(
200,
text='{"result":"POLICY_ACTION_DENY","reason":"rm blocked by admin policy"}',
request=httpx.Request("POST", url),
)
class _RaisesIfCalled:
"""
HTTP client stub that fails the test if the hook ever POSTs.
Used by fail-open tests where the hook must short-circuit (missing
bridge state or policy config) before reaching the network.
:param headers: Headers passed to :class:`httpx.Client` (unused).
:param timeout: Timeout passed to :class:`httpx.Client` (unused).
"""
def __init__(self, *, headers: dict[str, str], timeout: object) -> None:
"""
Accept the constructor shape; do nothing.
:param headers: HTTP headers (unused).
:param timeout: HTTP timeout (unused).
:returns: None.
"""
del headers, timeout
def __enter__(self) -> _RaisesIfCalled:
"""
Enter the context manager.
:returns: This stub client.
"""
return self
def __exit__(self, *args: object) -> None:
"""
Exit the context manager.
:param args: Exception details (unused).
:returns: None.
"""
del args
def post(self, url: str, *, json: dict[str, object]) -> httpx.Response:
"""
Fail loudly — the hook should never reach the network here.
:param url: Target Omnigent URL (unused).
:param json: Request body (unused).
:returns: Never returns.
:raises AssertionError: Always.
"""
del url, json
raise AssertionError(
"evaluate-policy POSTed to Omnigent when it should have short-circuited "
"(missing bridge state or policy_hook config)."
)
@pytest.fixture
def bridge_dir(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
"""
Create an isolated codex-native bridge directory with state.
Redirects the bridge root under ``tmp_path`` so the test never
touches the real ``~/.omnigent`` tree, then writes a valid bridge
state whose ``session_id`` the hook reads to build the Omnigent URL.
:param tmp_path: pytest temp directory.
:param monkeypatch: pytest monkeypatch fixture.
:returns: Prepared bridge directory.
"""
monkeypatch.setattr("omnigent.codex_native_bridge._BRIDGE_ROOT", tmp_path / "codex-native")
bdir = prepare_bridge_dir("bridge_test")
write_bridge_state(
bdir,
CodexNativeBridgeState(
session_id="conv_active",
socket_path=str(bdir / "app-server.sock"),
thread_id="thread_abc",
codex_home=str(bdir / "codex-home"),
),
)
return bdir
def _run_hook(
bridge_dir: Path, payload: dict[str, object], monkeypatch: pytest.MonkeyPatch
) -> int:
"""
Feed *payload* on stdin and run the ``evaluate-policy`` subcommand.
:param bridge_dir: The session's bridge directory.
:param payload: The codex hook JSON payload, e.g.
``{"hook_event_name": "PreToolUse", "tool_name": "Bash", ...}``.
:param monkeypatch: pytest monkeypatch fixture.
:returns: The hook process exit code.
"""
monkeypatch.setattr(sys, "stdin", io.StringIO(json.dumps(payload)))
return codex_native_hook.main(["evaluate-policy", "--bridge-dir", str(bridge_dir)])
def test_pre_tool_use_converts_posts_and_returns_deny(
bridge_dir: Path,
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
"""
A PreToolUse hook converts to proto, POSTs to AP, and maps DENY back.
This is the full codex enforcement path: read bridge state +
policy_hook config → convert payload → POST /policies/evaluate →
map the DENY verdict to ``permissionDecision: deny``. It fails if any
link breaks (wrong URL/session, missing conversion, missing auth, or
a mis-mapped verdict that would let the blocked command run).
"""
_DenyHttpxClient.captured = {}
write_policy_hook_config(
bridge_dir,
ap_server_url="http://127.0.0.1:8787",
ap_auth_headers={"Authorization": "Bearer test-token"},
)
monkeypatch.setattr(native_policy_hook.httpx, "Client", _DenyHttpxClient)
exit_code = _run_hook(
bridge_dir,
{
"hook_event_name": "PreToolUse",
"tool_name": "Bash",
"tool_input": {"command": "rm -rf /"},
},
monkeypatch,
)
captured = capsys.readouterr()
assert exit_code == 0
# URL is built from the bridge state's session_id, not the payload.
assert _DenyHttpxClient.captured["url"] == (
"http://127.0.0.1:8787/v1/sessions/conv_active/policies/evaluate"
)
# The codex payload is converted to the proto EvaluationRequest shape.
sent = _DenyHttpxClient.captured["json"]
assert sent["event"]["type"] == "PHASE_TOOL_CALL"
assert sent["event"]["data"] == {"name": "Bash", "arguments": {"command": "rm -rf /"}}
# Auth headers from policy_hook.json reach AP.
assert _DenyHttpxClient.captured["headers"] == {"Authorization": "Bearer test-token"}
# The DENY verdict maps back to codex's PreToolUse deny output.
result = json.loads(captured.out)
assert result["hookSpecificOutput"]["permissionDecision"] == "deny"
assert result["hookSpecificOutput"]["permissionDecisionReason"] == "rm blocked by admin policy"
assert captured.err == ""
def test_user_prompt_submit_converts_posts_and_blocks(
bridge_dir: Path,
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
"""
A UserPromptSubmit hook converts to PHASE_REQUEST and maps DENY to block.
This is the request-phase enforcement path for native Codex sessions
(the server-level ``_evaluate_input_policy`` skips native message
events). The prompt rides in ``event.data.text``; a DENY maps to the
top-level ``decision: "block"`` contract — NOT ``permissionDecision`` —
which drops the prompt before the model sees it. A break here means a
blocked prompt would still reach the model.
"""
_DenyHttpxClient.captured = {}
write_policy_hook_config(
bridge_dir,
ap_server_url="http://127.0.0.1:8787",
ap_auth_headers={"Authorization": "Bearer test-token"},
)
monkeypatch.setattr(native_policy_hook.httpx, "Client", _DenyHttpxClient)
exit_code = _run_hook(
bridge_dir,
{
"hook_event_name": "UserPromptSubmit",
"prompt": "delete the prod database",
},
monkeypatch,
)
captured = capsys.readouterr()
assert exit_code == 0
sent = _DenyHttpxClient.captured["json"]
assert sent["event"]["type"] == "PHASE_REQUEST"
assert sent["event"]["data"] == {"text": "delete the prod database"}
# DENY → top-level decision/reason block (not permissionDecision).
result = json.loads(captured.out)
assert result == {"decision": "block", "reason": "rm blocked by admin policy"}
assert captured.err == ""
def test_pre_tool_use_stamps_model_from_config(
bridge_dir: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""
The hook stamps ``config.toml``'s model onto the request context.
This is the race-free model source for the codex cost gate: the hook
reads the user's live ``/model`` selection from ``config.toml`` at gate
time and puts it on ``event.context.model`` so the server evaluates
against it (preferred over the engine's resolved model). If this
regresses, a terminal ``/model`` downgrade never reaches the gate and
the session stays wrongly blocked.
"""
_DenyHttpxClient.captured = {}
home = codex_home_for_bridge_dir(bridge_dir)
home.mkdir(parents=True, exist_ok=True)
# The current /model selection, as codex persists it to config.toml.
(home / "config.toml").write_text('model = "gpt-5.4"\n')
write_policy_hook_config(
bridge_dir,
ap_server_url="http://127.0.0.1:8787",
ap_auth_headers={"Authorization": "Bearer test-token"},
)
monkeypatch.setattr(native_policy_hook.httpx, "Client", _DenyHttpxClient)
exit_code = _run_hook(
bridge_dir,
{"hook_event_name": "PreToolUse", "tool_name": "Bash", "tool_input": {}},
monkeypatch,
)
assert exit_code == 0
sent = _DenyHttpxClient.captured["json"]
# The live model is carried in the request so the gate sees gpt-5.4.
assert sent["event"]["context"]["model"] == "gpt-5.4"
# The harness is stamped so the gate can tailor messages to codex.
assert sent["event"]["context"]["harness"] == "codex-native"
def test_pre_tool_use_stamps_harness_without_config_model(
bridge_dir: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""The harness is stamped even when config.toml has no model.
The harness drives the deny message's switch-instruction wording, which
must be correct regardless of whether the model is determinable — so it
is stamped unconditionally (unlike the model, which is only stamped when
config.toml provides one).
"""
_DenyHttpxClient.captured = {}
# No config.toml written → read_codex_config_model returns None.
write_policy_hook_config(
bridge_dir,
ap_server_url="http://127.0.0.1:8787",
ap_auth_headers={"Authorization": "Bearer test-token"},
)
monkeypatch.setattr(native_policy_hook.httpx, "Client", _DenyHttpxClient)
exit_code = _run_hook(
bridge_dir,
{"hook_event_name": "PreToolUse", "tool_name": "Bash", "tool_input": {}},
monkeypatch,
)
assert exit_code == 0
sent = _DenyHttpxClient.captured["json"]
assert sent["event"]["context"]["harness"] == "codex-native"
# Model absent (no config) — stays unstamped, the gate falls back.
assert "model" not in sent["event"]["context"]
def test_missing_bridge_state_is_fail_open(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
"""
With no bridge state, the hook emits nothing and never POSTs.
A bridge dir that has not been initialized must not crash codex or
block tools — the hook returns 0 with no verdict. ``_RaisesIfCalled``
asserts the network was never reached.
"""
monkeypatch.setattr("omnigent.codex_native_bridge._BRIDGE_ROOT", tmp_path / "codex-native")
empty_dir = prepare_bridge_dir("bridge_no_state")
monkeypatch.setattr(native_policy_hook.httpx, "Client", _RaisesIfCalled)
exit_code = _run_hook(
empty_dir,
{"hook_event_name": "PreToolUse", "tool_name": "Bash", "tool_input": {}},
monkeypatch,
)
captured = capsys.readouterr()
assert exit_code == 0
# No verdict emitted → codex applies its own default (fail-open).
assert captured.out == ""
def test_missing_policy_config_is_fail_open(
bridge_dir: Path,
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
"""
With bridge state but no policy_hook config, the hook never POSTs.
The session has state but no Omnigent coordinates were written (e.g. a
local run with no Omnigent server), so there is nothing to enforce against.
The hook returns 0 with no output and does not touch the network.
"""
monkeypatch.setattr(native_policy_hook.httpx, "Client", _RaisesIfCalled)
exit_code = _run_hook(
bridge_dir,
{"hook_event_name": "PreToolUse", "tool_name": "Bash", "tool_input": {}},
monkeypatch,
)
captured = capsys.readouterr()
assert exit_code == 0
assert captured.out == ""
@pytest.mark.parametrize("mode", ["connect_error", "non_2xx", "empty_body", "malformed_json"])
def test_pre_tool_use_fails_closed_when_verdict_unavailable(
bridge_dir: Path,
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
mode: str,
) -> None:
"""
A governed PreToolUse call denies when no usable verdict is returned.
For native harnesses this hook is the sole TOOL_CALL enforcement point,
so a server outage / non-2xx / empty / malformed response must fail
CLOSED (deny) instead of "no opinion" — the bypass reported in #536.
"""
write_policy_hook_config(bridge_dir, ap_server_url="http://127.0.0.1:8787", ap_auth_headers={})
monkeypatch.setattr(native_policy_hook, "_EVALUATE_POLICY_RETRY_BUDGET_S", 0.0)
monkeypatch.setattr(native_policy_hook.httpx, "Client", make_failing_client(mode))
exit_code = _run_hook(
bridge_dir,
{
"hook_event_name": "PreToolUse",
"tool_name": "Bash",
"tool_input": {"command": "rm -rf /"},
},
monkeypatch,
)
captured = capsys.readouterr()
assert exit_code == 0
result = json.loads(captured.out)
assert result["hookSpecificOutput"]["permissionDecision"] == "deny", result
assert result["hookSpecificOutput"]["permissionDecisionReason"]
def test_user_prompt_submit_fails_closed_on_error(
bridge_dir: Path,
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
"""
A governed UserPromptSubmit blocks when no usable verdict is returned.
The request gate is the sole pre-turn enforcement point for native
sessions — a server outage must not let a blocked request proceed.
"""
write_policy_hook_config(bridge_dir, ap_server_url="http://127.0.0.1:8787", ap_auth_headers={})
monkeypatch.setattr(native_policy_hook, "_EVALUATE_POLICY_RETRY_BUDGET_S", 0.0)
monkeypatch.setattr(native_policy_hook.httpx, "Client", make_failing_client("connect_error"))
payload: dict[str, object] = {"hook_event_name": "UserPromptSubmit", "prompt": "hello"}
exit_code = _run_hook(bridge_dir, payload, monkeypatch)
captured = capsys.readouterr()
assert exit_code == 0
result = json.loads(captured.out)
assert result["decision"] == "block"
assert result["reason"]
def test_post_tool_use_fails_open_on_error(
bridge_dir: Path,
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
"""
PostToolUse fails OPEN on a transport error — the tool already ran.
Mirroring the runner-side ``FAIL_CLOSED_PHASES``.
"""
write_policy_hook_config(bridge_dir, ap_server_url="http://127.0.0.1:8787", ap_auth_headers={})
monkeypatch.setattr(native_policy_hook, "_EVALUATE_POLICY_RETRY_BUDGET_S", 0.0)
monkeypatch.setattr(native_policy_hook.httpx, "Client", make_failing_client("connect_error"))
payload: dict[str, object] = {
"hook_event_name": "PostToolUse",
"tool_name": "Bash",
"tool_input": {"command": "ls"},
"tool_output": "ok",
}
exit_code = _run_hook(bridge_dir, payload, monkeypatch)
captured = capsys.readouterr()
assert exit_code == 0
assert captured.out == ""
def test_pre_tool_use_uses_relay_when_tool_relay_json_has_session_id(
bridge_dir: Path,
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
"""Hook POSTs to relay /policies/evaluate when tool_relay.json has session_id."""
from omnigent.claude_native_bridge import _TOOL_RELAY_FILE
relay_token = "relay-tok-abc"
relay_url = "http://127.0.0.1:19999"
(bridge_dir / _TOOL_RELAY_FILE).write_text(
json.dumps({"url": relay_url, "token": relay_token, "session_id": "conv_active"})
)
_DenyHttpxClient.captured = {}
monkeypatch.setattr(native_policy_hook.httpx, "Client", _DenyHttpxClient)
exit_code = _run_hook(
bridge_dir,
{"hook_event_name": "PreToolUse", "tool_name": "Bash", "tool_input": {"command": "ls"}},
monkeypatch,
)
assert exit_code == 0
# Route is relay, not direct server.
assert _DenyHttpxClient.captured["url"] == f"{relay_url}/policies/evaluate"
# Auth is relay token, not a server bearer.
assert _DenyHttpxClient.captured["headers"] == {
"Content-Type": "application/json",
"Authorization": f"Bearer {relay_token}",
}
# Verdict still applied.
out = json.loads(capsys.readouterr().out)
assert out["hookSpecificOutput"]["permissionDecision"] == "deny"
def test_pre_tool_use_falls_back_to_policy_hook_json_when_relay_has_no_session_id(
bridge_dir: Path,
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
"""Hook falls back to policy_hook.json when tool_relay.json has no session_id."""
from omnigent.claude_native_bridge import _TOOL_RELAY_FILE
# Relay present but no session_id — not policy-capable.
(bridge_dir / _TOOL_RELAY_FILE).write_text(
json.dumps({"url": "http://127.0.0.1:19999", "token": "tok"})
)
write_policy_hook_config(
bridge_dir,
ap_server_url="http://127.0.0.1:8787",
ap_auth_headers={"Authorization": "Bearer direct-token"},
)
_DenyHttpxClient.captured = {}
monkeypatch.setattr(native_policy_hook.httpx, "Client", _DenyHttpxClient)
_run_hook(
bridge_dir,
{"hook_event_name": "PreToolUse", "tool_name": "Bash", "tool_input": {}},
monkeypatch,
)
# Falls back to direct server URL.
assert _DenyHttpxClient.captured["url"] == (
"http://127.0.0.1:8787/v1/sessions/conv_active/policies/evaluate"
)
assert _DenyHttpxClient.captured["headers"] == {"Authorization": "Bearer direct-token"}
# ── route-turn (first-message model routing) ────────────────────────
def _advertise_turn_router(bridge_dir: Path) -> None:
"""
Write a live ``turn_router.json`` advertisement into *bridge_dir*.
:param bridge_dir: The session's bridge directory.
:returns: None.
"""
import os
from omnigent.runner.turn_routing import ADVERTISEMENT_FILE
(bridge_dir / ADVERTISEMENT_FILE).write_text(
json.dumps(
{
"url": "http://127.0.0.1:54321",
"token": "turn-token",
"pid": os.getpid(),
"session_id": "conv_active",
}
),
encoding="utf-8",
)
def _run_route_turn(
bridge_dir: Path, payload: dict[str, object], monkeypatch: pytest.MonkeyPatch
) -> int:
"""
Feed *payload* on stdin and run the ``route-turn`` subcommand.
:param bridge_dir: The session's bridge directory.
:param payload: The codex ``UserPromptSubmit`` hook payload.
:param monkeypatch: pytest monkeypatch fixture.
:returns: The hook process exit code.
"""
monkeypatch.setattr(sys, "stdin", io.StringIO(json.dumps(payload)))
return codex_native_hook.main(
["route-turn", "--bridge-dir", str(bridge_dir), "--harness", "codex-native"]
)
def test_route_turn_fast_skips_on_the_marker(
bridge_dir: Path,
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
"""
A consumed marker means no output and no network at all.
This is the re-entrancy guard the replayed prompt hits: the replay
re-fires ``UserPromptSubmit``, and a second block there would drop the
routed turn.
"""
from omnigent.runner.turn_routing import write_turn_routing_marker
_advertise_turn_router(bridge_dir)
write_turn_routing_marker(bridge_dir, session_id="conv_active", decision_id="d1")
monkeypatch.setattr(
codex_native_hook,
"_post_json",
lambda *args, **kwargs: pytest.fail("the marker must skip the network"),
)
exit_code = _run_route_turn(
bridge_dir, {"prompt": "hello", "model": "gpt-5.6-sol"}, monkeypatch
)
captured = capsys.readouterr()
assert exit_code == 0
assert captured.out == ""
def test_route_turn_blocks_and_switches_on_a_routed_verdict(
bridge_dir: Path,
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
"""
A routed verdict switches the thread, writes the marker, then blocks.
Order is load-bearing: the marker is the runner's "I blocked it, you
owe it a replay" handshake, so it must be on disk before the block.
"""
from omnigent.runner.turn_routing import MARKER_FILE
_advertise_turn_router(bridge_dir)
sent: dict[str, object] = {}
switched: list[str] = []
def _post(url: str, token: str, body: dict[str, object], timeout: float) -> dict[str, object]:
sent.update({"url": url, "token": token, "body": body, "timeout": timeout})
return {
"action": "route",
"model": "gpt-5.6-luna",
"rationale": "short lookup",
"terminal": True,
}
def _switch(bdir: Path, model: str) -> str | None:
# The marker is only written after the switch is accepted.
assert not (bdir / MARKER_FILE).exists()
switched.append(model)
return None
monkeypatch.setattr(codex_native_hook, "_post_json", _post)
monkeypatch.setattr(codex_native_hook, "_apply_thread_model", _switch)
exit_code = _run_route_turn(
bridge_dir,
{
"hook_event_name": "UserPromptSubmit",
"prompt": "what testing framework does this project use?",
"turn_id": "turn_1",
"model": "gpt-5.6-sol",
},
monkeypatch,
)
captured = capsys.readouterr()
assert exit_code == 0
assert sent["url"] == "http://127.0.0.1:54321/v1/sessions/conv_active/route-turn"
assert sent["token"] == "turn-token"
assert sent["body"] == {
"harness": "codex-native",
"prompt": "what testing framework does this project use?",
"turn_id": "turn_1",
# The live model comes from the payload; config.toml is stale.
"model": "gpt-5.6-sol",
}
assert switched == ["gpt-5.6-luna"]
assert (bridge_dir / MARKER_FILE).exists()
result = json.loads(captured.out)
assert result["decision"] == "block"
assert "gpt-5.6-luna" in result["reason"]
def test_route_turn_allows_and_marks_an_already_pinned_session(
bridge_dir: Path,
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
"""A terminal no-op writes the marker so later prompts skip the hop."""
from omnigent.runner.turn_routing import MARKER_FILE
_advertise_turn_router(bridge_dir)
monkeypatch.setattr(
codex_native_hook,
"_post_json",
lambda *args, **kwargs: {
"action": "allow",
"rationale": "already pinned",
"terminal": True,
},
)
monkeypatch.setattr(
codex_native_hook,
"_apply_thread_model",
lambda *args: pytest.fail("an allow verdict must not switch the model"),
)
exit_code = _run_route_turn(bridge_dir, {"prompt": "hello"}, monkeypatch)
captured = capsys.readouterr()
assert exit_code == 0
assert captured.out == ""
assert (bridge_dir / MARKER_FILE).exists()
def test_route_turn_keeps_asking_after_a_non_terminal_no_op(
bridge_dir: Path,
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
"""Routing can be toggled on mid-session, so no marker is written."""
from omnigent.runner.turn_routing import MARKER_FILE
_advertise_turn_router(bridge_dir)
monkeypatch.setattr(
codex_native_hook,
"_post_json",
lambda *args, **kwargs: {"action": "allow", "rationale": "routing off"},
)
exit_code = _run_route_turn(bridge_dir, {"prompt": "hello"}, monkeypatch)
assert exit_code == 0
assert capsys.readouterr().out == ""
assert not (bridge_dir / MARKER_FILE).exists()
def test_route_turn_allows_the_prompt_when_the_switch_fails(
bridge_dir: Path,
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
"""
No block without an applied switch — otherwise the prompt is lost.
The runner's replay only fires when the marker appears, so a hook that
blocked without switching would drop the user's message entirely.
"""
from omnigent.runner.turn_routing import MARKER_FILE
_advertise_turn_router(bridge_dir)
monkeypatch.setattr(
codex_native_hook,
"_post_json",
lambda *args, **kwargs: {
"action": "route",
"model": "gpt-5.6-luna",
"rationale": "x",
"terminal": True,
},
)
monkeypatch.setattr(
codex_native_hook,
"_apply_thread_model",
lambda *args: "could not switch to gpt-5.6-luna",
)
exit_code = _run_route_turn(bridge_dir, {"prompt": "hello"}, monkeypatch)
captured = capsys.readouterr()
assert exit_code == 0
assert captured.out == ""
assert "could not switch" in captured.err
# No marker: it is the replay handshake, and this prompt is running.
assert not (bridge_dir / MARKER_FILE).exists()
def test_route_turn_declines_visibly_when_the_pane_cannot_serve_the_pick(
bridge_dir: Path,
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
"""
An unreachable pick is a recorded decline, not a silent drop.
The reason reaches both the routing trace and stderr, so "the pane never
moved" is answerable without reproducing the stale gateway map.
"""
from omnigent.runner.turn_routing import MARKER_FILE, TRACE_FILE
_advertise_turn_router(bridge_dir)
monkeypatch.setattr(
codex_native_hook,
"_post_json",
lambda *args, **kwargs: {
"action": "route",
"model": "databricks-claude-opus-5",
"rationale": "deep refactor",
"terminal": True,
},
)
monkeypatch.setattr(
codex_native_hook,
"_apply_thread_model",
lambda *args: "routed model not in this pane's catalog (databricks-claude-opus-5)",
)
exit_code = _run_route_turn(bridge_dir, {"prompt": "hello"}, monkeypatch)
captured = capsys.readouterr()
assert exit_code == 0
# The prompt runs, unblocked, on the pane's own model.
assert captured.out == ""
assert "not in this pane's catalog" in captured.err
assert "not in this pane's catalog" in (bridge_dir / TRACE_FILE).read_text()
assert not (bridge_dir / MARKER_FILE).exists()
def test_route_turn_no_ops_without_an_advertisement(
bridge_dir: Path,
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
exit_code = _run_route_turn(bridge_dir, {"prompt": "hello"}, monkeypatch)
assert exit_code == 0
assert capsys.readouterr().out == ""
def test_route_turn_no_ops_on_an_empty_prompt(
bridge_dir: Path,
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
_advertise_turn_router(bridge_dir)
monkeypatch.setattr(
codex_native_hook,
"_post_json",
lambda *args, **kwargs: pytest.fail("an empty prompt must not reach the router"),
)
assert _run_route_turn(bridge_dir, {"prompt": " "}, monkeypatch) == 0
assert capsys.readouterr().out == ""
def test_route_turn_no_ops_when_the_endpoint_is_unreachable(
bridge_dir: Path,
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
"""A routing outage must never block a user's turn."""
from omnigent.runner.turn_routing import MARKER_FILE
_advertise_turn_router(bridge_dir)
monkeypatch.setattr(codex_native_hook, "_post_json", lambda *args, **kwargs: None)
assert _run_route_turn(bridge_dir, {"prompt": "hello"}, monkeypatch) == 0
assert capsys.readouterr().out == ""
assert not (bridge_dir / MARKER_FILE).exists()
def test_route_turn_falls_open_on_the_ladders_own_request_budget(
bridge_dir: Path,
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
"""
Codex's hook waits ``HOOK_REQUEST_TIMEOUT_S`` for a verdict, and no longer.
Same hazard as claude's: the typed prompt is held in the TUI until this
expires. Asserted against the constant, not elapsed time.
"""
from omnigent.runner.turn_routing import HOOK_REQUEST_TIMEOUT_S, MARKER_FILE
_advertise_turn_router(bridge_dir)
seen: list[float] = []
def _timed_out(url: str, token: str, body: object, timeout: float) -> None:
del url, token, body
seen.append(timeout)
return
monkeypatch.setattr(codex_native_hook, "_post_json", _timed_out)
assert _run_route_turn(bridge_dir, {"prompt": "hello"}, monkeypatch) == 0
assert seen == [HOOK_REQUEST_TIMEOUT_S]
# Must outlast a healthy route (catalog prep + router call), capped at the
# owner's 15s ceiling.
assert HOOK_REQUEST_TIMEOUT_S <= 15.0
assert capsys.readouterr().out == ""
assert not (bridge_dir / MARKER_FILE).exists()
def test_the_thread_switch_is_capped_inside_the_harness_hook_budget() -> None:
"""
Hop 2b sits inside hop 1 alongside hop 2a, with room to spare.
Codex's hook does two things after being invoked — ask for a verdict, then
switch the thread — and the harness kills it on one budget. If the two
inner budgets could together exceed the outer one, a slow switch would be
killed mid-``thread/settings/update``: the block marker is written after
the switch, so the harness would drop the prompt with nothing to replay it.
"""
from omnigent.runner.turn_routing import (
HARNESS_HOOK_TIMEOUT_S,
HOOK_REQUEST_TIMEOUT_S,
SETTINGS_UPDATE_TIMEOUT_S,
)
assert HARNESS_HOOK_TIMEOUT_S > HOOK_REQUEST_TIMEOUT_S + SETTINGS_UPDATE_TIMEOUT_S
# A local app-server RPC over a unix socket, so seconds is generous.
assert SETTINGS_UPDATE_TIMEOUT_S < 10.0
@pytest.mark.parametrize(
"advertise",
[False, True],
ids=["no_advertisement", "unreachable_endpoint"],
)
def test_route_turn_traces_every_fall_open(
bridge_dir: Path,
monkeypatch: pytest.MonkeyPatch,
advertise: bool,
) -> None:
"""A session that did not route always records which gate stopped it.
Without this the two ways first-message routing goes quiet — the hook
fell open, or the harness never fired it at all — are indistinguishable
from the logs, which is exactly how a reported "it never routed" ends
up unattributable.
"""
from omnigent.runner.turn_routing import TRACE_FILE
if advertise:
_advertise_turn_router(bridge_dir)
monkeypatch.setattr(codex_native_hook, "_post_json", lambda *args, **kwargs: None)
assert _run_route_turn(bridge_dir, {"prompt": "hello"}, monkeypatch) == 0
traced = [
json.loads(line)
for line in (bridge_dir / TRACE_FILE).read_text(encoding="utf-8").splitlines()
]
assert [entry["outcome"] for entry in traced] == ["fail-open"]
assert traced[0]["detail"]
def test_route_turn_traces_the_route_it_applied(
bridge_dir: Path,
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
from omnigent.runner.turn_routing import TRACE_FILE
_advertise_turn_router(bridge_dir)
monkeypatch.setattr(
codex_native_hook,
"_post_json",
lambda *args, **kwargs: {
"action": "route",
"model": "gpt-5.6-luna",
"terminal": True,
},
)
monkeypatch.setattr(codex_native_hook, "_apply_thread_model", lambda *args: None)
assert _run_route_turn(bridge_dir, {"prompt": "hello"}, monkeypatch) == 0
assert json.loads(capsys.readouterr().out)["decision"] == "block"
traced = [
json.loads(line)
for line in (bridge_dir / TRACE_FILE).read_text(encoding="utf-8").splitlines()
]
assert [entry["outcome"] for entry in traced] == ["route"]
assert "gpt-5.6-luna" in traced[0]["detail"]
# ── the actuator: what spelling reaches thread/settings/update ───────
class _FakeAppServerClient:
"""
App-server client stub scripting ``model/list`` and recording requests.
:param catalog: ``model/list`` rows to serve, or ``None`` to make the
call raise (an unreadable catalog).
"""
def __init__(self, catalog: list[dict[str, object]] | None) -> None:
"""
Build the stub.
:param catalog: Rows to serve, or ``None`` to fail the call.
:returns: None.
"""
self._catalog = catalog
self.requests: list[tuple[str, dict[str, object]]] = []