Repository navigation
Expand file tree
/
Copy pathtest_codex_watch.py
More file actions
269 lines (248 loc) · 14.9 KB
/
Copy pathtest_codex_watch.py
File metadata and controls
269 lines (248 loc) · 14.9 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
# Copyright (c) 2026 Firefly Software Solutions Inc.
# SPDX-License-Identifier: Apache-2.0
# Normalized Codex command completion contracts and real crew message delivery.
import contextlib
import io
import json
import sys
from pathlib import Path
from unittest import mock
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "lib"))
sys.path.insert(0, str(Path(__file__).resolve().parent))
from devkit import bugs, hooks, progress as pg, state, watch # noqa: E402
from test_bugs_detect import BugState # noqa: E402
from test_language_neutral_hooks import HookCase # noqa: E402
from test_watch import WatchCase # noqa: E402
WAIT = "dev watch --wait --timeout 0.5"
TIMEOUT = "watch: nothing arrived in 0.5 min · arm again: dev watch --wait --timeout 10"
class CodexWatchOutcomeTests(BugState, HookCase):
def setUp(self):
super().setUp()
self.bug_state()
self.rollout = self.root / "rollout.jsonl"
self.rollout.write_text(json.dumps({"type": "session_meta", "payload": {
"id": "c1", "cli_version": "0.167.0", "originator": "codex_exec"}}) + "\n")
self.n = 0
def record(self, cmd, output, code, status="failed"):
self.n += 1
item = {"type": "CommandExecution", "id": f"exec-watch-{self.n}", "process_id": "12345",
"command": ["/bin/zsh", "-lc", cmd], "cwd": str(self.root),
"parsed_cmd": [{"type": "unknown", "cmd": cmd}], "source": "unified_exec_startup",
"status": status, "aggregated_output": output, "exit_code": code,
"duration": {"secs": 0, "nanos": 3333}}
return item
def append(self, item):
with self.rollout.open("a") as fh:
fh.write(json.dumps({"type": "event_msg", "payload": {
"type": "item_completed", "turn_id": "t1", "item": item}}) + "\n")
def post(self, item):
payload = {"cwd": str(self.root), "hook_event_name": "PostToolUse", "session_id": "c1",
"tool_input": {"cmd": item["command"][-1]}, "tool_name": "exec_command",
"tool_response": item["aggregated_output"], "tool_use_id": item["id"],
"transcript_path": str(self.rollout), "turn_id": "t1"}
out = io.StringIO()
with mock.patch("sys.stdin", io.StringIO(json.dumps(payload))), contextlib.redirect_stdout(out):
hooks.run("post-tool", "codex")
return out.getvalue()
def outcomes(self):
f = bugs.BUGS / "ledger" / "c1.jsonl"
rows = [json.loads(s) for s in f.read_text().splitlines()] if f.exists() else []
return [r["ok"] for r in rows if r.get("tool") == "exec_command"]
def exercise_waits(self, deferred):
vectors = [
(WAIT, TIMEOUT, 3, "failed"),
("DEVKIT_SPEAKER=main DEVKIT_SESSION=codex:c1 " + WAIT, TIMEOUT, 3, "completed"),
(WAIT, "watch: the session ended", 2, "failed"),
(WAIT, "watch: nothing can arrive for this session: no crew member works, no launch is pending, "
"no step waits for a slot, no other session owes it an answer and no CI run is unfinished. "
"End the turn without a wait.", 2, "failed"),
(WAIT, "watch: nothing can arrive for you: none of your questions waits for an answer. "
"End the turn without a wait.", 2, "failed"),
(WAIT, "watch: a newer wait of this session took over; this one ends", 5, "failed"),
(WAIT, "watch: stopped before anything arrived · arm again: dev watch --wait --for me --timeout 9",
3, "failed"),
(WAIT, "watch: this conversation goes on under session codex:01a1c178-64… "
"(a compaction gave it a new id) · arm again: dev watch --wait --timeout 10", 3, "failed"),
]
for cmd, output, code, status in vectors * 2:
with self.subTest(cmd=cmd, code=code, deferred=deferred):
item = self.record(cmd, output, code, status)
if deferred:
self.post(item)
self.append(item)
result = hooks._codex_settle("codex", {"session_id": "c1"})
else:
self.append(item)
result = self.post(item)
self.assertNotIn(hooks.ROOT_CAUSE_STOP, result)
self.assertIs(self.outcomes()[-1], True)
self.assertEqual(hooks.codex_rollout(self.rollout)[0][item["id"]]["exit_code"], code)
def test_expected_wait_outcomes_do_not_enter_the_failure_loop(self):
self.exercise_waits(False)
def test_deferred_wait_outcomes_settle_without_failure_streaks(self):
self.exercise_waits(True)
def test_ci_wait_controls_preserve_real_pipeline_and_parser_failures(self):
cmd = "dev ci watch --wait --branch main --sha abc1234 --timeout 0.5"
timed = ("job lint passed · CI #7\nCI still running · acme/api main @ abc1234 after 0.5 min "
"(CI #7 in_progress): this watch ended first; re-arm: dev ci watch --wait --branch main "
"--sha abc1234 --timeout 28")
cases = [
(cmd, timed, 3, True),
(cmd, "CI none · acme/api main @ abc1234: no pipeline run appeared for this commit", 4, True),
(cmd, "CI skipped · acme/api main @ abc1234 · CI #7 skipped · 0.5 min", 4, True),
(cmd, "CI failed · acme/api main @ abc1234 · CI #7 failed", 1, False),
(cmd, "CI cancelled · acme/api main @ abc1234 · CI #7 cancelled", 1, False),
(cmd, "usage: dev ci\nerror: invalid timeout", 2, False),
(cmd + "; exit 3", timed, 3, False),
("printf '%s' 'dev ci watch --wait'; exit 3", timed, 3, False),
(cmd, timed + "\nTraceback: unrelated failure", 3, False),
(cmd, "CI failed · acme/api main @ abc1234 · CI #7 failed", 4, False),
(cmd + " --bad", timed, 3, False),
]
for deferred in (False, True):
for command, output, code, expected in cases:
with self.subTest(deferred=deferred, command=command, code=code, expected=expected):
item = self.record(command, output, code)
if deferred:
self.post(item)
self.append(item)
hooks._codex_settle("codex", {"session_id": "c1"})
else:
self.append(item)
self.post(item)
self.assertIs(self.outcomes()[-1], expected)
def test_real_errors_remain_failures_after_expected_waits(self):
# The successful wait assertion makes this fail without the fix; the adversarial cases prevent a broad
# "ignore exit 2/3/5" fix from hiding a real command failure or breaking repeated-failure detection.
item = self.record(WAIT, TIMEOUT, 3)
self.append(item)
self.post(item)
self.assertEqual(self.outcomes(), [True])
for cmd, output, code, status in [
("printf '%s' 'dev watch --wait'; exit 3", TIMEOUT, 3, "failed"),
(WAIT + "; false", TIMEOUT, 1, "failed"),
(WAIT + "; exit 3", TIMEOUT, 3, "failed"),
("dev watch --wait --bad", "usage: dev watch\nerror: unrecognized arguments: --bad", 2, "failed"),
("dev watch --wait --bad", TIMEOUT, 3, "failed"),
(WAIT + " --timeout nope", TIMEOUT, 3, "failed"),
(WAIT + " --for someone", TIMEOUT, 3, "failed"),
(WAIT, TIMEOUT + "\nTraceback: unrelated failure", 3, "failed"),
(WAIT, TIMEOUT, 3, "inProgress"),
(WAIT, TIMEOUT, 2, "failed"),
(WAIT, TIMEOUT, 3, "declined"),
(WAIT, TIMEOUT, "3", "failed"),
(WAIT, "Traceback: watch failed", 3, "failed"),
("python3 -c 'raise ValueError()'", "ValueError: failed", 3, "failed"),
]:
with self.subTest(cmd=cmd, code=code, status=status):
item = self.record(cmd, output, code, status)
self.post(item) # real failures also survive deferred settlement
self.append(item)
hooks._codex_settle("codex", {"session_id": "c1"})
self.assertIs(self.outcomes()[-1], False)
item = self.record(cmd, output, code, status)
self.append(item)
result = self.post(item)
self.assertIs(self.outcomes()[-1], False)
# A diagnostic is told once per streak, so only a new failure's second attempt must repeat this notice.
self.assertIn(hooks.ROOT_CAUSE_STOP, result)
class CodexCrewDeliveryTests(WatchCase):
def test_moot_delivery_keeps_its_audit_after_successful_output(self):
self.run_with_member()
original = pg.load_run("claude", "s1")
for agent in ("codex", "claude"):
with self.subTest(agent=agent):
run = dict(original, agent=agent, messages=[])
run.pop("run_id", None)
run["agents"]["a1"]["state"] = "done"
for step in run["plan"]:
step["state"] = "done"
question = pg._post(run, "a1", "main", "May I update the contract?", self.t0,
from_name="Tank")
pg._save(run)
with mock.patch.object(pg, "_audit") as audit:
if agent == "codex":
with self.assertRaises(BrokenPipeError):
watch._messages(agent, "s1", "main", True, lambda e: True, 10,
emit=mock.Mock(side_effect=BrokenPipeError("closed")))
audit.assert_not_called()
self.assertFalse(pg.load_run(agent, "s1")["messages"][0].get("delivered"))
lines = []
events, _ = watch._messages(agent, "s1", "main", True, lambda e: True, 10,
emit=lines.append)
self.assertTrue(events[0]["line"].startswith("devkit · moot:"))
records = [call for call in audit.call_args_list if call.args[0] == "crew.message-moot"]
self.assertEqual(len(records), 1)
self.assertEqual(records[0].args[1], question["id"])
self.assertIn("holds no open step", records[0].kwargs["why"])
self.assertTrue(pg.load_run(agent, "s1")["messages"][0]["delivered"])
self.assertEqual(watch._messages(agent, "s1", "main", True, lambda e: True, 10,
emit=lines.append)[0], [])
self.assertEqual(len([c for c in audit.call_args_list
if c.args[0] == "crew.message-moot"]), 1)
def test_codex_acknowledges_only_messages_written_before_a_broken_pipe(self):
self.run_with_member()
run = dict(pg.load_run("claude", "s1"), agent="codex", messages=[])
run.pop("run_id", None)
pg._post(run, "main", "a1", "First complete instruction", self.t0, from_name="Morpheus")
pg._post(run, "main", "a1", "Second instruction must remain pending", self.t0, from_name="Morpheus")
pg._post(run, "main", "other", "Other lane only", self.t0)
pg._save(run)
lines = []
def emit(line):
if lines:
raise BrokenPipeError("reader closed")
lines.append(line)
with self.assertRaises(BrokenPipeError):
watch._deliver("codex", "s1", "a1", [], emit, "", "dev watch --wait", self.t0, self.clock.time)
self.assertIn("First complete instruction", lines[0])
stored = pg.load_run("codex", "s1")["messages"]
self.assertTrue(stored[0]["delivered"])
self.assertFalse(stored[1].get("delivered"))
self.assertFalse(stored[2].get("delivered"))
self.assertIn("Second instruction must remain pending", watch.pending("codex", "s1", "a1")[0]["line"])
def test_codex_delivers_complete_sanitized_questions_and_answers_once(self):
self.run_with_member()
# The same stored crew run is delivered on each provider; only Codex's wait rendering changes.
original = pg.load_run("claude", "s1")
for agent in ("codex", "claude"):
with self.subTest(agent=agent):
run = dict(original, agent=agent, messages=[])
run.pop("run_id", None)
text = ("Read the contract first. " * 18 + "\x1b[31m\u202e“`Do not edit Claude.`”\n"
"Keep the final constraint. api_key=sk-" + "a" * 40)
question = pg._post(run, "main", "a1", "Legacy question", self.t0, from_name="Morpheus")
answer = pg._post(run, "main", "a1", "Legacy answer", self.t0, from_name="Morpheus", kind="answer")
# Historical secrets enter through migration, never a new accepted send.
question["text"] = answer["text"] = text
for historic in (question, answer):
for field in ("schema", "body_bytes", "body_sha256"):
historic.pop(field, None)
answer["reply_to"] = "M0"
unrelated = pg._post(run, "main", "someone-else", "Other lane only", self.t0)
pg._save(run)
peek = watch.pending(agent, "s1", "a1")
self.assertEqual(len(peek), 2)
self.assertFalse(any(m.get("delivered") for m in pg.load_run(agent, "s1")["messages"]))
first = watch._collect(agent, "s1", "a1", True, only=["question"], limit=1)
line = first[0][0][0]["line"]
self.assertTrue(line.startswith("devkit · question M1 from Tech lead (Morpheus): “"), line)
# a crew message arrives whole on both agents (Claude Code too, since messages are no longer cut at
# 300 characters), and sanitized: no secret, no escape or bidi control, no backtick, one quote pair
self.assertIn("preview; read: dev crew read", line)
self.assertNotIn("sk-" + "a" * 40, line)
self.assertNotIn("\x1b", line)
self.assertNotIn("\u202e", line)
self.assertNotIn("`", line)
self.assertEqual(line.count("“"), 1)
self.assertEqual(line.count("”"), 1)
stored = {m["id"]: m for m in pg.load_run(agent, "s1")["messages"]}
self.assertTrue(stored[question["id"]]["delivered"])
self.assertFalse(stored[answer["id"]].get("delivered"))
self.assertFalse(stored[unrelated["id"]].get("delivered"))
delivered = watch._collect(agent, "s1", "a1", True, only=["question"])
line = delivered[0][0][0]["line"]
self.assertIn("answer M2 from Tech lead (Morpheus) to M0", line)
if agent == "codex":
self.assertIn("preview; read: dev crew read", line)
self.assertEqual(watch.pending(agent, "s1", "a1"), [])