Repository navigation
Expand file tree
/
Copy pathtest_codex_parity.py
More file actions
922 lines (820 loc) · 54.7 KB
/
Copy pathtest_codex_parity.py
File metadata and controls
922 lines (820 loc) · 54.7 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
# Copyright (c) 2026 Firefly Software Solutions Inc.
# SPDX-License-Identifier: Apache-2.0
"""Reproductions of the Codex parity failures found live with Codex CLI 0.167.0 on 2026-11-09.
Each test failed before the fix of the bug it names. The record shapes are copied from a real Codex 0.167.0 rollout
(~/.codex/sessions/2026/11/09/rollout-...-01a1c151-....jsonl) and from the hook schemas the Codex binary embeds
(stop.command.input requires last_assistant_message).
The classes after "the same scenario on both agents" run each scenario once as a Claude Code session and once as a
Codex session, each in a sandbox Mac of its own (tests/test_acceptance_crew_sessions.py: an agent host process, every
hook as `dev hook <event> --agent claude|codex` with that agent's payload, every `dev` command as a child process,
behind the net of tests/_sandbox.py), and assert the same outcome, or the difference devkit documents for Codex.
Run: python3 -m unittest tests.test_codex_parity -v (and with /usr/bin/python3, for Python 3.9)
"""
import contextlib
import io
import json
import os
import re
import select
import subprocess
import sys
import tempfile
import time
import unittest
from pathlib import Path
from unittest import mock
REPO = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(REPO / "lib"))
sys.path.insert(0, str(REPO / "tests"))
from devkit import bugs, canary, hooks, lessonevents, lessonrules, lessons, scope # noqa: E402
from devkit import progress as pg # noqa: E402
from test_acceptance_crew_sessions import GIT_ID, HOOK_BOOT, Host, Mac # noqa: E402 (the hermetic net)
from test_bugs_detect import BugState # noqa: E402
from test_codex_launch import CLAUDE_LEAD, HELPER, LEAD, TANK, TRINITY, CodexCrew # noqa: E402
from test_language_neutral_hooks import HookCase # noqa: E402
from test_lessons_hooks import LessonHookCase # noqa: E402
from test_progress import TempState # noqa: E402
from test_session_coordination import codex_types # noqa: E402
BLOCK = ("**[Firefly> DevKit]** proj · `main` · Run requested steps\n\n- **Done:** ran the steps.\n"
"- **Check:** ✓ git log shows the seed commit\n- **Next:** nothing.")
def _codex_rollout(cmd: str, final: str) -> str:
"""A one-turn Codex 0.167.0 rollout: the prompt, one code-mode `exec` call running `cmd`, the final message."""
recs = [
{"type": "session_meta", "payload": {"cli_version": "0.167.0", "originator": "codex_exec"}},
{"type": "event_msg", "payload": {"type": "user_message", "message": "run the steps"}},
{"type": "response_item", "payload": {
"type": "custom_tool_call", "status": "completed", "call_id": "call_1", "name": "exec",
"input": "text(await tools.exec_command({cmd:" + json.dumps(cmd) + ",max_output_tokens:1000}));\n"}},
{"type": "response_item", "payload": {"type": "custom_tool_call_output", "call_id": "call_1",
"output": [{"type": "input_text", "text": "Script completed\n"}]}},
{"type": "response_item", "payload": {"type": "message", "role": "assistant", "status": "completed",
"content": [{"type": "output_text", "text": final}]}},
{"type": "event_msg", "payload": {"type": "task_complete", "last_agent_message": final}},
]
fd, path = tempfile.mkstemp(suffix=".jsonl")
with os.fdopen(fd, "w") as fh:
fh.write("".join(json.dumps(r) + "\n" for r in recs))
return path
class CodexTranscriptTests(unittest.TestCase):
def setUp(self):
self.path = _codex_rollout("git commit -m 'docs: add notes'", BLOCK)
self.addCleanup(os.unlink, self.path)
def test_stop_counts_a_codex_turn_that_opens_with_the_status_block_as_development(self):
# Live: `dev agents test codex` -> "Stop hook + instructions (canary): no canary record";
# hooks.log "stop codex: not development work (no signal); devkit stays out" although the reply opened
# with the block. The Stop payload carries the reply in last_assistant_message.
data = {"hook_event_name": "Stop", "transcript_path": self.path, "last_assistant_message": BLOCK,
"stop_hook_active": False}
self.assertTrue(scope.decide(data, {"top": "/repo"}))
def test_turn_actions_sees_the_shell_command_of_a_code_mode_exec_call(self):
# Codex 0.167.0 (code_mode_host on by default) runs shell commands inside its `exec` JavaScript tool.
self.assertEqual(scope.turn_actions(self.path)["commands"], ["git commit -m 'docs: add notes'"])
def test_last_assistant_text_reads_a_codex_rollout(self):
# SubagentStop and the shown-decisions record fall back to the transcript when the payload has no text.
self.assertEqual(canary.last_assistant_text(self.path), BLOCK)
class CodexStopTests(unittest.TestCase):
# Live (probe A): a Codex turn that committed ended without what the rules ask and was not sent back. Here the
# Stop hook runs its own classifier (scope.decide is not replaced), on a 0.167 rollout and payload
def setUp(self):
self.state = TempState()
self.state.__enter__()
self.addCleanup(self.state.__exit__, None, None, None)
self.path = _codex_rollout("git commit -m 'docs: add notes'", BLOCK)
self.addCleanup(os.unlink, self.path)
def test_a_codex_turn_without_the_progress_line_is_reminded_at_the_next_prompt(self):
(pg.SESSIONS / "codex-c1.json").write_text(json.dumps({
"agent": "codex", "session": "c1", "started": time.time() - 600, "turn_started": time.time() - 60}))
with mock.patch.dict(os.environ, {"DEVKIT_SESSION": "codex:c1"}):
pg.start("Hotfix", plan=["plan=10m", "api=20m"])
data = {"session_id": "c1", "turn_id": "t1", "hook_event_name": "Stop", "cwd": self.state.tmp.name,
"model": "gpt-6", "permission_mode": "bypassPermissions", "transcript_path": self.path,
"last_assistant_message": BLOCK, "stop_hook_active": False}
out = io.StringIO()
with mock.patch.object(hooks, "_session_file", lambda a, sid: pg.SESSIONS / f"{a}-{sid}.json"), \
mock.patch.object(hooks, "STATE", Path(self.state.tmp.name)), \
mock.patch.object(hooks, "_continuity", lambda *a, **k: None), \
mock.patch.object(hooks, "_spawn_sync", lambda args: None), \
mock.patch.object(canary, "LOG", Path(self.state.tmp.name) / "canary.jsonl"), \
mock.patch("sys.stdin", io.StringIO(json.dumps(data))), contextlib.redirect_stdout(out):
hooks.stop("codex")
sent = json.loads(out.getvalue() or "{}")
self.assertNotIn("decision", sent) # the developer read it: the next prompt reminds it instead
with mock.patch.object(hooks, "_session_file", lambda a, sid: pg.SESSIONS / f"{a}-{sid}.json"):
due = hooks._presentation_due("codex", "c1")
self.assertEqual([n["rule"] for n in due], ["progress"])
self.assertIn("- **Progress:** 0 of 2 steps · now 1. plan · ", due[0]["text"])
class CodexFailedShellTests(BugState, LessonHookCase):
"""Codex has no PostToolUseFailure event: a failed command reaches devkit only as PostToolUse, whose tool_response
is the command's output alone. Its exit code is in the rollout: the CommandExecution item whose id is the hook's
tool_use_id (captured with Codex 0.167.0 on 2026-11-09)."""
CMD = "python3 -c 'import nosuchmod'"
OUTPUT = ("Traceback (most recent call last):\n File \"<string>\", line 1, in <module>\n import nosuchmod\n"
"ModuleNotFoundError: No module named 'nosuchmod'\n")
def setUp(self):
super().setUp()
self.bug_state()
self.rollout = self.root / "rollout.jsonl"
self._rollout("0.167.0")
def _rollout(self, version):
# a rollout's first records as Codex writes them: its session_meta, which names the CLI version, then the turn
self.rollout.write_text("".join(json.dumps(r) + "\n" for r in (
{"type": "session_meta", "payload": {"id": "c1", "cli_version": version, "originator": "codex_exec"}},
{"type": "event_msg", "payload": {"type": "task_started", "turn_id": "t1"}})))
def _record(self, n, status="failed", code=1, rollout=None):
# the rollout's item for the command, as Codex 0.167.0 wrote it
item = {"type": "CommandExecution", "id": f"exec-5f125236-dd03-45f9-8fd3-db673fc2b9b{n}", "process_id": "59976",
"command": ["/bin/zsh", "-lc", self.CMD], "cwd": "file:///repo",
"parsed_cmd": [{"type": "unknown", "cmd": self.CMD}], "source": "unified_exec_startup",
"status": status, "aggregated_output": self.OUTPUT, "exit_code": code,
"duration": {"secs": 0, "nanos": 3333}}
with (rollout or 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, n, output=None, **member):
# the captured PostToolUse payload of that command; `member`: a crew member's fields (agent_id, agent_type,
# its own transcript_path) beside the main session's session_id
payload = dict({"cwd": str(self.root), "hook_event_name": "PostToolUse", "model": "gpt-6.1-sol",
"permission_mode": "bypassPermissions", "session_id": "c1",
"tool_input": {"command": self.CMD}, "tool_name": "Bash",
"tool_response": self.OUTPUT if output is None else output,
"tool_use_id": f"exec-5f125236-dd03-45f9-8fd3-db673fc2b9b{n}",
"transcript_path": str(self.rollout), "turn_id": "t1"}, **member)
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 _ledger_ok(self):
f = bugs.BUGS / "ledger" / "c1.jsonl"
ledger = [json.loads(x) for x in f.read_text().splitlines()] if f.exists() else []
return [x["ok"] for x in ledger if x.get("tool") == "Bash"]
def _output_lesson(self, shown=True, rule=False):
hooks._session_file("codex", "c1").write_text(json.dumps({"project": "acme/api"}))
les = lessons.add("acme/api", "Check the installed module before importing", cause="The module is absent",
prevention="Check installed modules before importing", happened=self.OUTPUT,
errors=["Another specific import error" if rule else "No module named 'nosuchmod'"])
lid = les["meta"]["id"]
if shown:
lessons.shown([lid], "session", "c1", "main", count=False)
if rule:
return lid, lessonrules.propose(lessons.find(lid), error="No module named 'nosuchmod'")
return lid
def test_deferred_failure_counts_prior_exposure_once_after_actual_failure(self):
lid = self._output_lesson()
self._post(1)
self.assertEqual(lessonevents.drain_recurrences(), 0)
self._record(1)
self._record(2, status="completed", code=0)
self._post(2, output="done")
self.assertEqual(lessonevents.drain_recurrences(), 1)
self._post(1)
self.assertEqual(lessonevents.drain_recurrences(), 0)
self.assertEqual(lessons.find(lid)["meta"]["occurrences"], 2)
def test_deferred_failure_uses_exposure_even_after_its_outcome_was_resolved(self):
lid = self._output_lesson()
lessonevents.on_end("c1", "main")
self._post(1)
self._record(1)
self._stop()
self.assertEqual(lessonevents.drain_recurrences(), 1)
self.assertEqual(lessons.find(lid)["meta"]["occurrences"], 2)
def test_deferred_rule_only_failure_records_one_hit_and_recurrence(self):
lid, rule = self._output_lesson(rule=True)
# The rule's error is in the recorded mistake, but is not one of the lesson's error anchors.
self.assertNotIn(lid, [x["meta"]["id"] for x in lessons.for_output("acme/api", self.OUTPUT)])
self._post(1)
self._post(1) # replay while pending must preserve the first exposure snapshot
self.assertEqual(lessonevents.drain_recurrences(), 0)
self._record(1)
self._stop()
self._stop()
self.assertEqual(lessonevents.drain_recurrences(), 1)
self._post(1) # a settled call delivered again must not increment the rule counter either
hits = json.loads(lessonrules.HITS.read_text())
self.assertEqual((hits[rule["id"]]["hits"], hits[rule["id"]]["misses"]), (1, 1))
def test_pending_advice_and_same_call_exposure_never_become_prior_exposure(self):
lid = self._output_lesson(shown=False)
self._post(1)
self._post(1) # pending replay must not count the advice of the first delivery
self._record(1)
self._stop()
self._post(1) # later delivery cannot turn this call's own advice into prior exposure
self.assertEqual(lessonevents.drain_recurrences(), 0)
same = "exec-5f125236-dd03-45f9-8fd3-db673fc2b9b2"
hooks._session_file("codex", "c1").write_text(json.dumps({
"project": "acme/api", "lessons_by_call": {same: [lid]}}))
self._post(2)
self._record(2)
self._stop()
self.assertEqual(lessonevents.drain_recurrences(), 0)
self.assertEqual(lessons.find(lid)["meta"]["occurrences"], 1)
def test_deferred_evidence_matches_beyond_the_display_budget(self):
first = self._output_lesson()
ids = [first] + [self.lesson(f"Check missing module variant {n}", errors=["No module named 'nosuchmod'"],
new=True, why="Distinct import environments share this diagnostic")
for n in range(hooks.LESSON_HITS_MAX + 2)]
lessons.shown(ids, "session", "c1", "main", count=False)
self._post(1)
evidence = json.loads(hooks._codex_pending("c1").read_text())["calls"][0]["lesson_evidence"]
self.assertEqual(len(evidence["eligible"]), hooks.LESSON_HITS_MAX)
self.assertTrue(all(re.fullmatch(r"L-[0-9A-Za-z]{1,40}", lid) for lid in evidence["eligible"]))
self._record(1)
self._stop()
self.assertEqual(lessonevents.drain_recurrences(), hooks.LESSON_HITS_MAX)
self.assertEqual(sum(lessons.find(lid)["meta"]["occurrences"] == 2 for lid in ids), hooks.LESSON_HITS_MAX)
def test_deferred_success_expiry_and_unexecuted_outcomes_have_no_recurrence(self):
lid, rule = self._output_lesson(rule=True)
for n, status, code in ((1, "completed", 0), (2, "declined", None), (3, "interrupted", None),
(4, "failed", None)):
self._post(n)
self._record(n, status=status, code=code)
self._stop()
self._post(5)
with mock.patch.object(hooks, "CODEX_SETTLE_S", 0):
self._stop()
self.assertEqual(lessonevents.drain_recurrences(), 0)
self.assertEqual(lessons.find(lid)["meta"]["occurrences"], 1)
hits = json.loads(lessonrules.HITS.read_text()) if lessonrules.HITS.exists() else {}
self.assertNotIn(rule["id"], hits)
def test_immediate_unexecuted_results_are_advisory_despite_matching_error_text(self):
lid, rule = self._output_lesson(rule=True)
for n, status, code in ((1, "declined", None), (2, "failed", None), (3, "interrupted", 1)):
self._record(n, status=status, code=code)
self._post(n)
self.assertEqual(lessonevents.drain_recurrences(), 0)
self.assertEqual(lessons.find(lid)["meta"]["occurrences"], 1)
hits = json.loads(lessonrules.HITS.read_text()) if lessonrules.HITS.exists() else {}
self.assertNotIn(rule["id"], hits)
def test_the_rollout_record_decides_a_codex_commands_outcome_never_its_output(self):
self._record(1)
self._post(1) # failed; its output is a plain traceback
self._record(2, status="completed", code=0)
self._post(2, output="Exit code: 1\nTraceback (most recent call last):\nValueError: printed by the test\n")
self.assertEqual(self._ledger_ok(), [False, True])
def test_the_same_codex_failure_twice_stops_the_patch_loop(self):
self._record(1)
self._post(1)
self._record(2)
out = self._post(2)
self.assertIn(hooks.ROOT_CAUSE_STOP, out)
# Codex's PostToolUse output schema accepts only its own event name
self.assertEqual(json.loads(out)["hookSpecificOutput"]["hookEventName"], "PostToolUse")
def test_a_command_whose_record_is_not_written_yet_is_settled_at_the_next_call(self):
self._post(1) # its item is not on disk yet: unknown, never a success
self.assertEqual(self._ledger_ok(), [])
self._record(1)
self._record(2)
out = self._post(2)
self.assertEqual(self._ledger_ok(), [False, False])
self.assertIn(hooks.ROOT_CAUSE_STOP, out) # the second identical failure, counted with the settled first
def _stop(self):
payload = {"cwd": str(self.root), "hook_event_name": "Stop", "model": "gpt-6.1-sol",
"permission_mode": "bypassPermissions", "session_id": "c1", "stop_hook_active": False,
"last_assistant_message": BLOCK, "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), \
mock.patch.object(hooks, "_spawn", lambda *a, **k: None): # no real `dev` in the background
hooks.run("stop", "codex")
return out.getvalue()
def test_a_command_whose_record_comes_late_is_settled_at_stop(self):
self._record(1)
self._post(1) # the first failure, known at once
self._post(2) # the same failure again, its record not on disk yet
self._record(2)
self.assertNotIn(hooks.ROOT_CAUSE_STOP, self._stop()) # Stop writes the ledger line; it cannot tell the agent
self.assertEqual(self._ledger_ok(), [False, False])
self._record(3)
# the next identical failure counts the streak from the ledger, and the paragraph reaches the agent now
self.assertIn(hooks.ROOT_CAUSE_STOP, self._post(3))
def test_a_members_hook_never_settles_the_main_sessions_command(self):
# a Codex member's calls carry the main session's session_id beside its own agent_id and rollout (captured):
# the main session's late record is not settled in the member's hook, so its paragraph never goes there
lid = self._output_lesson()
scout_rollout = self.root / "scout.jsonl"
scout = {"agent_id": "01a1c178-64c7-7ac1-81c6-ec8b852e1772", "agent_type": "scout", "model": "gpt-6-luna",
"transcript_path": str(scout_rollout)}
self._record(1)
self._post(1) # the main session's first failure
self.assertEqual(lessonevents.drain_recurrences(), 1)
self._post(2) # the same failure again, its record not on disk yet
self._record(2)
self._record(3, status="completed", code=0, rollout=scout_rollout)
self.assertNotIn(hooks.ROOT_CAUSE_STOP, self._post(3, **scout))
self.assertEqual(self._ledger_ok(), [False, True]) # the member's own line; the main session's still waits
self.assertEqual(lessonevents.drain_recurrences(), 0)
self._record(4, status="completed", code=0)
self.assertIn(hooks.ROOT_CAUSE_STOP, self._post(4)) # the main session's next call settles it and is told
self.assertEqual(self._ledger_ok(), [False, True, False, True])
self.assertEqual(lessonevents.drain_recurrences(), 1)
self.assertEqual(lessons.find(lid)["meta"]["occurrences"], 3)
self.assertEqual({e["agent"] for e in lessonevents._events("c1") if e["event"] == "missed"}, {"main"})
def test_a_rollout_without_command_records_gets_each_line_at_once(self):
# a Codex older than 0.167 writes no CommandExecution items: waiting for one would lose every ledger line
self._rollout("0.160.0")
self._post(1)
self.assertEqual(self._ledger_ok(), [True]) # a PostToolUse is a success, as before Codex wrote the items
self._record(2) # once the tail shows the items, a missing one is waited for whatever the version says
self._post(2)
self._post(3)
self.assertEqual(self._ledger_ok(), [True, False])
def test_a_settle_reads_the_rollout_once_whatever_the_calls_it_waits_for(self):
for n in (1, 2, 3):
self._post(n) # three calls whose items are not written yet
for n in (1, 2, 3, 4):
self._record(n)
with mock.patch.object(hooks, "codex_rollout", wraps=hooks.codex_rollout) as read:
self._post(4)
self.assertEqual(read.call_count, 1) # the three settled calls and the new one share one read of the tail
self.assertEqual(self._ledger_ok(), [False, False, False, False])
def test_a_waiting_call_keeps_neither_its_output_nor_a_secret_in_its_command(self):
# the pending record keeps what settling needs (the error key, devkit's marker lines, the lessons the output
# recalled), so the paragraph still names the lesson; the output and a token in the command stay out of it
hooks._session_file("codex", "c1").write_text(json.dumps({"project": "acme/api"}))
lid = self.lesson("Imported a module the environment lacks", errors=["No module named 'nosuchmod'"])
token = "ghp_" + "A1b2C3d4E5f6" * 3 # the shape of a GitHub token, built here so no literal looks like one
self.CMD = f"GH_TOKEN={token} python3 -c 'import nosuchmod'"
self._record(1)
self._post(1)
self._post(2) # the same failure again, its record not written yet
kept = hooks._codex_pending("c1").read_text()
self.assertNotIn("ModuleNotFoundError", kept)
self.assertNotIn("A1b2C3d4", kept)
evidence = json.loads(kept)["calls"][0]["lesson_evidence"]
self.assertEqual(evidence, {"project": "acme/api", "eligible": [lid], "rules": []})
self.assertNotIn("No module named", json.dumps(evidence))
self._record(2)
self._record(3, status="completed", code=0)
ctx = json.loads(self._post(3))["hookSpecificOutput"]["additionalContext"]
self.assertIn(hooks.ROOT_CAUSE_STOP, ctx)
self.assertIn(f"- Lesson {lid} (recorded by", ctx.split(hooks.ROOT_CAUSE_STOP, 1)[1])
def test_a_command_never_recorded_leaves_no_ledger_line_and_is_logged(self):
self._post(1)
self._record(2, status="completed", code=0)
with mock.patch.object(hooks, "CODEX_SETTLE_S", 0): # the first call's wait is over
self._post(2)
self.assertEqual(self._ledger_ok(), [True]) # the second call only
log = (self.root / "logs" / "hooks.log").read_text()
self.assertIn("exec-5f125236-dd03-45f9-8fd3-db673fc2b9b1", log)
class CodexHookIdentityTests(HookCase):
"""A Codex started from a Claude Code terminal (or a crew member's shell) inherits that session's variables."""
def setUp(self):
super().setUp()
env = mock.patch.dict(os.environ, {"DEVKIT_AGENT": "claude", "CLAUDECODE": "1", "DEVKIT_FROM_AGENT": "a_tank"})
env.start()
self.addCleanup(env.stop)
self.launcher = dict(os.environ) # DEVKIT_SESSION=claude:s1 comes from HookCase
def _call(self, **member):
# PreToolUse as Codex 0.167 sends it (captured): a member's call carries agent_id and agent_type beside the
# main session's session_id -> the call's ledger entry
payload = dict({"session_id": "c1", "turn_id": "t1", "hook_event_name": "PreToolUse", "tool_name": "Bash",
"tool_use_id": "exec-5f125236-dd03-45f9-8fd3-db673fc2b9b0", "cwd": str(self.root),
"model": "gpt-6.1-sol", "permission_mode": "bypassPermissions", "transcript_path": None,
"tool_input": {"command": "git checkout -b codex/parity-probe"}}, **member)
out = io.StringIO()
with mock.patch("sys.stdin", io.StringIO(json.dumps(payload))), contextlib.redirect_stdout(out):
hooks.run("pre-tool", "codex")
self.assertNotIn("permissionDecision", out.getvalue())
return pg.call_entry(payload)
def test_a_codex_hooks_records_carry_codex_and_its_own_session(self):
entry = self._call()
self.assertEqual((entry["agent"], entry["session"], entry["caller"]), ("codex", "c1", "main"))
self.assertEqual(dict(os.environ), self.launcher) # the caller's own variables are left as they were
def test_a_codex_members_records_carry_the_main_session_and_its_own_lane(self):
aid = "01a1c178-64c7-7ac1-81c6-ec8b852e1772" # the captured scout's agent_id
entry = self._call(agent_id=aid, agent_type="scout")
self.assertEqual((entry["agent"], entry["session"], entry["caller"]), ("codex", "c1", aid))
class HookFailOpenTests(BugState, HookCase):
def setUp(self):
super().setUp()
self.bug_state()
def test_a_hook_run_with_no_stdin_fails_open(self):
# a harness that closes the hook's stdin (sys.stdin is None): the hook logs the error and returns; it never
# crashes the agent's session, and the caller's variables are left as they were
before = dict(os.environ)
with mock.patch("sys.stdin", None):
try:
code = hooks.run("post-tool", "codex")
except Exception as e:
self.fail(f"the hook raised {e!r}")
self.assertEqual(code, 0)
self.assertIn("post-tool codex error", (self.root / "logs" / "hooks.log").read_text())
self.assertEqual(dict(os.environ), before)
# --------------------------------------------------------------------------- the same scenario on both agents
#
# Each test below runs one scenario as a Claude Code session and as a Codex session (a subTest per agent, each in a
# fresh sandbox Mac) and asserts the outcome written down for each. The outcome is the same on both agents, or the
# difference devkit documents for Codex: Codex fires no hook before a spawn_agent call, so a launch over the crew's
# limits is marked and stopped once its agent starts instead of refused (lib/devkit/codexlaunch.py); and Codex has no
# background wake-up, so the wait a turn must leave runs in the foreground (watch.stop_gate).
AGENTS = ("claude", "codex")
NAMES = {"tank": "Tank", "trinity": "Trinity", "sparks": "Sparks"}
RELEASE = "dev release --version 26.11.9"
SECOND = {"claude": "c0ffee00-1234-4abc-8def-001122334455", "codex": "01a1c178-64c7-7ac1-81c6-ec8b852e1772"}
STUB = "TO" + "DO" # the marker the stub gate looks for, built here so this file holds none
def _iso(at: float) -> str:
return time.strftime("%Y-%m-%dT%H:%M:%S", time.gmtime(at)) + f".{int(at * 1000) % 1000:03d}Z"
class Parity(CodexCrew):
# A tech lead's session as each agent runs it, in a fresh sandbox Mac per agent (fresh). The crew, the shell
# calls, the typed lines and the member's stop go through the hooks the way that agent sends them.
def fresh(self, progress=None, **settings):
# a new sandbox Mac (tests/test_acceptance_crew_sessions.Mac) and the developer's devkit configuration
Mac.setUp(self)
f = self.home / ".config" / "devkit" / "config.json"
f.parent.mkdir(parents=True, exist_ok=True)
f.write_text(json.dumps(dict({"progress": dict({"max_parallel": 2, "reserve": 0}, **(progress or {}))},
**settings)))
def session(self, agent: str, sid: str = "", repo=None, headless: bool = False):
# a Claude Code conversation, or a Codex thread whose rollout opens with its session_meta as Codex writes it
# (a `codex exec` run's when headless); a headless Claude Code session runs `claude -p` (its entrypoint)
repo = repo or self.api
if agent == "claude":
sid = sid or CLAUDE_LEAD
if headless:
self.hosts[f"claude-{sid}"] = Host(self, "claude", dict(self.env, CLAUDE_CODE_ENTRYPOINT="sdk-cli"))
return self.claude(sid, repo)
sid = sid or LEAD
path = self.home / ".codex" / "sessions" / "2026" / "11" / "09" / f"rollout-2026-11-09T10-00-39-{sid}.jsonl"
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(json.dumps({"timestamp": _iso(time.time()), "type": "session_meta", "payload": {
"id": sid, "session_id": sid, "cwd": str(repo), "cli_version": "0.167.0",
"originator": "codex_exec" if headless else "codex_cli_rs", "source": "exec" if headless else "cli"}})
+ "\n")
return self.codex(sid, repo)
def lead_on(self, agent: str, plan="api=30m [backend]; ui=30m [frontend]"):
# the tech lead's run, its steps assigned as in CodexCrew.lead: api to Tank, ui to Trinity
s = self.session(agent)
s.ok("progress", "start", "Invoice export", "--plan", plan)
for step, name in (("api", "Tank"), ("ui", "Trinity")):
if f"{step}=" in plan:
s.ok("progress", "assign", step, "--to", name, "--scope", f"{step}/**")
return s
@staticmethod
def kind(s, member: str) -> str:
return f"devkit:{member}" if s.agent == "claude" else member
def launch(self, s, aid: str, member: str, task: str, parent: str = "") -> dict:
# The tech lead (or member `parent`) launches a crew member. Claude Code: the Agent call's PreToolUse (which
# may refuse it, or append the member's skills to its prompt), SubagentStart, the call's result. Codex: the
# spawn_agent records in the rollouts, then SubagentStart (CodexCrew.spawn). -> {refused, told: what devkit
# added for the member}
if s.agent == "codex":
out = self.spawn(s, aid, member, task, parent=parent, depth=2 if parent else 1)
return {"refused": "", "told": self.context(out), "advice": ""}
by = {"agent_id": parent, "agent_type": "devkit:tank"} if parent else {}
call = dict(tool_name="Agent", tool_use_id=s._ids(), tool_input={
"description": f"{NAMES[member]}: {task.replace('_', ' ')}", "subagent_type": f"devkit:{member}",
"prompt": "Do the work."}, **by)
spec = s.hook("pre-tool", s.payload("PreToolUse", **call)).get("hookSpecificOutput") or {}
if spec.get("permissionDecision") == "deny":
return {"refused": spec.get("permissionDecisionReason") or "?", "told": "", "advice": ""}
s.hook("subagent-start", s.payload("SubagentStart", agent_id=aid, agent_type=f"devkit:{member}"))
s.hook("post-tool", s.payload("PostToolUse", tool_response={"agentId": aid, "status": "completed"}, **call))
return {"refused": "", "told": (spec.get("updatedInput") or {}).get("prompt") or "",
"advice": spec.get("additionalContext") or ""}
def member_stop(self, s, aid: str, member: str, text: str, again: bool = False) -> dict:
# SubagentStop as each agent sends it: Codex names the member's own rollout, Claude Code its transcript
own = self.rollout(aid) if s.agent == "codex" else s.transcript.parent / s.sid / "subagents" / f"agent-{aid}.jsonl"
return s.hook("subagent-stop", s.payload("SubagentStop", agent_id=aid, agent_type=self.kind(s, member),
stop_hook_active=again, agent_transcript_path=str(own),
last_assistant_message=text))
def edit(self, s, aid: str, member: str, path: str) -> tuple:
# a member's file write as each agent sends it: Claude Code's Write, Codex's apply_patch -> (decision, why)
if s.agent == "claude":
tool, inp = "Write", {"file_path": str(s.repo / path), "content": "export {};\n"}
else:
tool, inp = "apply_patch", {"command": f"*** Begin Patch\n*** Add File: {path}\n+export {{}};\n*** End Patch\n"}
g = s.hook("pre-tool", s.payload("PreToolUse", tool_name=tool, tool_use_id=s._ids(), tool_input=inp,
agent_id=aid, agent_type=self.kind(s, member)))
spec = g.get("hookSpecificOutput") or {}
return spec.get("permissionDecision") or "allow", spec.get("additionalContext") or ""
def shell(self, s, cmd: str, where=None, member: tuple = (), event: str = "pre-tool") -> dict:
# One shell call's hook as each agent sends it: Claude Code's Bash `command`, run in the payload's cwd (where
# its shell is); Codex's exec_command `cmd`, run in `workdir`, while the payload's cwd stays the thread's
# folder (~/Development, where the Codex app thread started). -> the hook's output
if s.agent == "claude":
tool, inp, at = "Bash", {"command": cmd}, {"cwd": str(where or s.repo)}
else:
tool, inp = "exec_command", {"cmd": cmd, "workdir": str(where or s.repo)}
at = {"cwd": str(self.home / "Development")}
who = {"agent_id": member[0], "agent_type": self.kind(s, member[1])} if member else {}
if event == "pre-tool":
return s.hook("pre-tool", s.payload("PreToolUse", tool_name=tool, tool_use_id=s._ids(), tool_input=inp,
**at, **who))
payload = s.payload("PostToolUse", tool_name=tool, tool_use_id=s._ids(), tool_input=inp,
tool_response="" if s.agent == "codex" else {"stdout": "", "stderr": ""}, **at, **who)
# the CI watch is on for this call: it joins the watcher a live lock names, and starts none
r = s.host.run([sys.executable, "-I", "-S", "-c", HOOK_BOOT, REPO / "lib", "post-tool", s.agent, self.spawns],
s.repo, env={"DEVKIT_NO_CI_WATCH": ""}, stdin=json.dumps(payload))
self.assertEqual(r["rc"], 0, r["err"][-1500:])
return {}
def verdict(self, s, cmd: str, where=None, member: tuple = ()) -> tuple:
spec = self.shell(s, cmd, where, member).get("hookSpecificOutput") or {}
return spec.get("permissionDecision") or "allow", spec.get("additionalContext") or ""
def types(self, s, text: str) -> dict:
# the developer types `text`: Claude Code's transcript record then UserPromptSubmit; Codex's UserPromptSubmit
# then the turn's records in its rollout (tests/test_session_coordination.codex_types)
return s.prompt(text) if s.agent == "claude" else codex_types(s, text)
class CrewLaunchParity(Parity):
# Codex crew launches (lib/devkit/codexlaunch.py) get what a Claude Code Agent call gets.
def test_a_launch_is_recorded_bound_to_the_step_it_names_and_holds_a_crew_slot(self):
for agent in AGENTS:
with self.subTest(agent=agent):
self.fresh()
s = self.lead_on(agent)
self.assertEqual(self.launch(s, TANK, "tank", "api")["refused"], "")
lane = s.run_doc()["agents"][TANK]
self.assertEqual((lane["binding"], lane["label"], lane["parent"], lane["depth"], lane["persona"]),
("exact", "Tank: api", "main", 1, "Tank"))
self.assertEqual((self.step(s, "api")["owner"], self.step(s, "api")["state"]), (TANK, "running"))
self.assertEqual(self.step(s, "ui")["owner"], "main") # Trinity's step waits for her own launch
self.assertEqual(self.launch_of(s, TANK).get("state"), "started")
self.assertIn("1 of 2 slots in use", s.ok("crew", "next"))
def test_a_launch_over_the_crew_limits_runs_and_says_why_it_should_not(self):
# nothing refuses a launch: Claude Code's launch carries the reason as advice; Codex's member, which no hook
# can stop before it starts, is told to stop and report, its step goes back to the tech lead, and its edits
# are advised as over the limit
rows = (("slots", {"max_parallel": 1}, (TRINITY, "trinity", "ui", ""), "ui/view.ts",
"1 of 1 crew slots are in use (Backend engineer (Tank))"),
("depth", {"max_depth": 1}, (HELPER, "sparks", "find_callers", TANK), "api/callers.ts",
"This launch would run 2 levels below the tech lead; the limit is 1 (progress.max_depth)"))
for limit, progress, (aid, member, task, parent), path, why in rows:
for agent in AGENTS:
with self.subTest(limit=limit, agent=agent):
self.fresh(progress)
s = self.lead_on(agent)
self.launch(s, TANK, "tank", "api")
got = self.launch(s, aid, member, task, parent)
self.assertEqual(got["refused"], "")
self.assertIn(why, got["advice"] if agent == "claude" else got["told"])
if agent == "codex":
self.assertIn("stop now and report", got["told"])
self.assertIn(why, s.run_doc()["agents"][aid]["over_limit"]["why"])
if limit == "slots": # the step waits for a launch of its own
self.assertEqual((self.step(s, "ui")["owner"], self.step(s, "ui")["state"]),
("main", "pending"))
decision, reason = self.edit(s, aid, member, path)
self.assertEqual(decision, "allow", reason)
self.assertIn("over the crew's limit", reason)
def test_the_step_of_a_codex_launch_over_the_slot_limit_stays_with_the_tech_lead(self):
# Codex: codexlaunch.mark_over_limit gives the step back to the tech lead, while the lane stays `running` and
# the step keeps `lost_from`; approvals.reconcile ("a lost lane seen again takes back the steps it lost") must
# not hand it to the over-limit member at the session's next hook, here the tech lead's own `git status`, or
# the step shows as running under a member told to stop and `dev crew next` offers no launch for it
self.fresh({"max_parallel": 1})
s = self.lead_on("codex")
self.launch(s, TANK, "tank", "api")
self.launch(s, TRINITY, "trinity", "ui")
self.assertIn("over_limit", s.run_doc()["agents"][TRINITY])
s.sh("git status --short") # the tech lead's next tool call
self.assertEqual((self.step(s, "ui")["owner"], self.step(s, "ui")["state"]), ("main", "pending"))
def test_the_member_gets_its_required_skills_and_report_format(self):
# appended to a Claude Code member's brief; a Codex brief is encrypted, so SubagentStart context carries it
for agent in AGENTS:
with self.subTest(agent=agent):
self.fresh()
for root in (".agents", ".claude"): # the skills installed for each agent on this Mac
for name in ("backend", "testing", "devsecops"):
f = self.home / root / "skills" / name / "SKILL.md"
f.parent.mkdir(parents=True, exist_ok=True)
f.write_text(f"---\nname: {name}\ndescription: {name}\n---\n# {name}\n")
s = self.lead_on(agent)
told = self.launch(s, TANK, "tank", "api")["told"]
self.assertIn("## devkit · required skills", told)
for name in ("backend", "testing", "devsecops"):
self.assertIn(f"- `{name}` — ", told)
self.assertIn("## devkit · your report", told)
self.assertIn("**[Firefly> DevKit · Backend engineer (Tank)]**", told)
def test_a_report_without_its_status_block_is_sent_back_once_at_the_members_stop(self):
for agent in AGENTS:
with self.subTest(agent=agent):
self.fresh()
s = self.lead_on(agent)
self.launch(s, TANK, "tank", "api")
out = self.member_stop(s, TANK, "tank", "Built the endpoint; 12 tests pass.")
self.assertEqual(out.get("decision"), "block", out)
self.assertIn("end your report with your status block first, exactly: **[Firefly> DevKit · Backend "
"engineer (Tank)]**", out.get("reason", ""))
again = self.member_stop(s, TANK, "tank", "Built the endpoint; 12 tests pass.", again=True)
self.assertNotIn("your status block first, exactly", again.get("reason", ""))
def test_the_launch_dev_crew_brief_prints_binds_the_step(self):
# the brief names the launch for each agent; the Codex task_name it prints binds the step, as the Claude Code
# description "<member>: <step>" does
for agent in AGENTS:
with self.subTest(agent=agent):
self.fresh()
s = self.lead_on(agent, plan="invoice csv=30m [backend]")
s.ok("progress", "assign", "invoice csv", "--to", "Tank")
brief = s.ok("crew", "brief", "tank", "Build the invoice CSV export", "--step", "invoice csv")
self.assertIn('Claude Code Agent tool · subagent_type: devkit:tank · description: "Tank: ', brief)
m = re.search(r"Codex {8}spawn_agent · agent_type: tank · task_name: (\S+)", brief)
self.assertIsNotNone(m, brief)
self.assertEqual(m.group(1), "invoice_csv")
self.launch(s, TANK, "tank", m.group(1))
self.assertEqual((self.step(s, "invoice csv")["owner"], s.run_doc()["agents"][TANK]["binding"]),
(TANK, "exact"))
class ShellRuleParity(Parity):
# Every hook-side shell rule reads Codex's exec_command `cmd`, in its `workdir`, as it reads Claude Code's Bash
# `command` in the shell's cwd (hooks._shell_inputs, hooks._shell_cwd).
def staged_stub(self, repo):
# a staged file that leaves unfinished work, for the stub commit gate
(repo / "export.py").write_text(f"def export():\n return None # {STUB}: write the rows\n")
subprocess.run(["git", "add", "export.py"], cwd=str(repo), check=True, capture_output=True,
env=dict(os.environ, **GIT_ID))
def test_no_shell_command_is_refused_and_the_stub_advice_reads_the_codex_cmd_as_the_bash_command(self):
team = 'dev crew set tank summary "Builds APIs" --why "shorter"'
rows = (("a release", RELEASE, (), ""), ("a team change by a member", team, (TANK, "tank"), ""),
("a hook typed through a shell", "echo '{}' | dev hook prompt --agent codex", (), ""),
("a commit of unfinished work", "git commit -m 'feat: add the export'", (),
"These lines leave unfinished work"))
for agent in AGENTS:
self.fresh(work={"mode": "guided"})
s = self.lead_on(agent)
self.launch(s, TANK, "tank", "api")
self.staged_stub(s.repo)
for what, cmd, member, advice in rows:
with self.subTest(agent=agent, call=what):
decision, told = self.verdict(s, cmd, member=member)
self.assertEqual(decision, "allow", told)
if advice:
self.assertIn(advice, told)
def test_a_push_joins_the_ci_watch_of_the_repository_it_ran_in(self):
# devkit's background watcher of the pushed commit already runs (its lock names a live process), so the
# push's PostToolUse joins it with a record for this session and starts none
lock = ("import sys, time\nsys.path.insert(0, sys.argv[1])\nfrom devkit import ciwatch\n"
"top, ref, sha, pid = sys.argv[2:6]\nciwatch.WATCH.mkdir(parents=True, exist_ok=True)\n"
"f = ciwatch.WATCH / (ciwatch._key(ciwatch.repo_id(top), ref) + '-' + sha[:7] + '.lock')\n"
"f.write_text(pid + ' ' + str(int(time.time())))\n")
branch = "feat/invoice-export"
for agent in AGENTS:
with self.subTest(agent=agent):
self.fresh()
s = self.session(agent)
git = dict(os.environ, **GIT_ID)
sha = subprocess.run(["git", "rev-parse", "HEAD"], cwd=str(s.repo), capture_output=True, text=True,
env=git, check=True).stdout.strip()
subprocess.run(["git", "update-ref", f"refs/remotes/origin/{branch}", sha], cwd=str(s.repo),
check=True, env=git) # origin has the branch at that commit: the push sent it
r = s.host.run([sys.executable, "-I", "-S", "-c", lock, REPO / "lib", s.repo, branch, sha,
s.host.proc.pid], s.repo)
self.assertEqual(r["rc"], 0, r["err"][-1500:])
self.shell(s, f"git push origin {branch}", event="post-tool")
live = [json.loads(f.read_text()) for f in sorted((self.state / "ci" / "live").glob("*.json"))]
self.assertEqual([(w["by"], w["agent"], w["session"], w["top"], w["ref"], w["sha"]) for w in live],
[("devkit", agent, s.sid, str(s.repo), branch, sha[:7])])
class TypedLineParity(Parity):
# A `settle E-…` line the developer types settles the escalation from the prompt as the hook delivers it, on both
# agents.
def test_a_settle_line_the_developer_types_settles_the_escalation(self):
for agent in AGENTS:
with self.subTest(agent=agent):
self.fresh()
s = self.session(agent)
other = self.session(agent, SECOND[agent])
out = other.ok("session", "escalate", f"@{s.sid[:8]}", "Which error format does the API return?",
"--options", "A: keep the old error body; B: RFC 9457 problem details",
"--recommend", "A")
eid = re.search(r"✓ (E-[0-9a-z]{8}) escalated", out).group(1)
echo = s.context(self.types(s, f"settle {eid}: B"))
self.assertIn(f"escalation {eid} settled in this prompt", echo)
states = {e["id"]: e for e in json.loads(other.ok("session", "escalations", "--all", "--json"))}
self.assertEqual((states[eid]["state"], states[eid]["settled"]["choice"]), ("settled", "B"))
WAIT_ASKED = "their questions and results can arrive while you are idle, and nothing wakes you"
WAIT_HOW = {"claude": "Before you end the turn, run `dev watch --wait` with Bash run_in_background",
"codex": "Codex has no background wake-up: run `dev watch --wait --timeout 10` in the foreground"}
class WaitParity(Parity):
# A turn that ends while a crew member works must leave a wait that wakes the session (watch.stop_gate). The
# documented difference: Claude Code needs `dev watch --wait` running in the background; Codex, which has no
# background wake-up, needs a registered foreground wait whose process is still running.
@contextlib.contextmanager
def live_wait(self, s):
# Run the real wait in the sandbox. The pipe signals when it has registered and reached its file-change
# wait, so the Stop assertion never races process startup or depends on a sleep.
script = (
"import select, sys\n"
"sys.path.insert(0, sys.argv[1])\n"
"from devkit import watch\n"
"def changes(seconds):\n"
" print('waiting', flush=True)\n"
" select.select([sys.stdin], [], [], seconds)\n"
" return False\n"
"sys.exit(watch.wait('codex', sys.argv[2], 'main', 1, out=sys.stderr, changes=changes))\n"
)
with subprocess.Popen([sys.executable, "-I", "-S", "-c", script, str(REPO / "lib"), s.sid],
cwd=s.repo, env=self.env, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, text=True) as proc:
try:
ready, _, _ = select.select([proc.stdout], [], [], 10)
self.assertTrue(ready, "the real wait never reached its file-change wait")
self.assertEqual(proc.stdout.readline().strip(), "waiting")
self.assertIsNone(proc.poll(), "the registered wait ended before Stop")
yield
finally:
if proc.poll() is None:
proc.terminate()
try:
_, err = proc.communicate(timeout=10)
except subprocess.TimeoutExpired:
proc.kill()
proc.communicate(timeout=10)
raise
self.assertEqual(proc.returncode, 3, err)
def turn(self, s, text: str):
# the developer's next prompt starts a turn: Claude Code's prompt record; Codex's task_started event, then
# the prompt's records
if s.agent == "codex":
with s.transcript.open("a") as fh:
fh.write(json.dumps({"timestamp": _iso(time.time()), "type": "event_msg",
"payload": {"type": "task_started", "turn_id": f"turn-{time.time_ns()}"}}) + "\n")
self.types(s, text)
def waits(self, s, background: bool):
# the session runs `dev watch --wait` in this turn, as its transcript records the call
n = time.time_ns()
if s.agent == "codex":
rec = {"timestamp": _iso(time.time()), "type": "response_item", "payload": {
"type": "function_call", "name": "exec_command", "call_id": f"call_wait_{n}",
"arguments": json.dumps({"cmd": "dev watch --wait --timeout 10"})}}
recs = [rec]
else:
use = f"toolu_wait_{n}"
inp = dict({"command": "dev watch --wait"}, **({"run_in_background": True} if background else {}))
out = f"Command running in background with ID: b{n % 10 ** 6}. Output is being written to: /tmp/w.output" \
if background else "devkit · nothing arrived"
recs = [{"type": "assistant", "sessionId": s.sid, "timestamp": _iso(time.time()), "message": {
"role": "assistant", "content": [{"type": "tool_use", "id": use, "name": "Bash", "input": inp}]}},
{"type": "user", "sessionId": s.sid, "timestamp": _iso(time.time()), "message": {
"role": "user", "content": [{"type": "tool_result", "tool_use_id": use, "content": out}]},
**({"toolUseResult": {"backgroundTaskId": f"b{n % 10 ** 6}"}} if background else {})}]
with s.transcript.open("a") as fh:
fh.write("".join(json.dumps(r) + "\n" for r in recs))
def ends(self, s) -> str:
out = s.stop("**[Firefly> DevKit]** api · `feat/invoice-export` · Invoice export\n- **Done:** launched the "
"Backend engineer (Tank).\n- **Check:** – nothing to verify yet\n- **Next:** Tank builds the API.")
return out.get("reason") or ""
def test_a_turn_that_ends_while_a_member_works_must_leave_the_wait_its_agent_can_run(self):
for agent in AGENTS:
with self.subTest(agent=agent):
self.fresh()
s = self.lead_on(agent, plan="api=30m [backend]")
self.launch(s, TANK, "tank", "api")
said = self.ends(s) # no wait at all
self.assertIn(WAIT_ASKED, said)
self.assertIn(WAIT_HOW[agent], said)
if agent == "claude":
self.turn(s, "keep going")
self.waits(s, background=False) # a foreground wait ends before the turn does: it wakes nobody
else:
self.waits(s, background=False) # Codex: a foreground wait of the turn that already ended
self.turn(s, "keep going")
self.assertIn(WAIT_HOW[agent], self.ends(s))
self.turn(s, "go on")
self.waits(s, background=agent == "claude") # the wait each agent can run
if agent == "claude":
self.assertNotIn(WAIT_ASKED, self.ends(s))
else:
self.assertIn(WAIT_ASKED, self.ends(s)) # command text alone is not an active process
self.turn(s, "keep watching")
with self.live_wait(s):
self.assertNotIn(WAIT_ASKED, self.ends(s))
self.turn(s, "the wait ended")
self.assertIn(WAIT_ASKED, self.ends(s))
S3 = "9448c4d0-aaaa-4bbb-8ccc-0123456789ab" # a Claude Code conversation in kit-consulting, another project
class SessionViewParity(Parity):
# The session views and the crew coordination of this release show and treat a Codex thread as they do a Claude
# Code conversation.
def test_status_session_ls_graph_and_dashboard_show_the_session_and_its_run(self):
for agent in AGENTS:
with self.subTest(agent=agent):
self.fresh()
s = self.lead_on(agent, plan="api=30m [backend]")
self.launch(s, TANK, "tank", "api")
s.ok("crew", "ask", "Old error format?", member=TANK, member_type=self.kind(s, "tank"))
self.assertIn("Invoice export", s.ok("status"))
k = self.session("claude", S3, self.kit) # a session of another project looks at the Mac
listed = k.ok("session", "ls")
self.assertIn(f"{agent} @{s.sid[:8]}", listed)
self.assertIn("acme/api", next(ln for ln in listed.splitlines() if f"@{s.sid[:8]}" in ln))
graph = json.loads(k.ok("crew", "graph", "--global", "--json"))
self.assertIn((agent, s.sid, "acme/api"), [(g["session"].get("agent"), g["session"]["session"],
g["session"]["project"]) for g in graph["sessions"]])
page = self.home / "dashboard.html"
k.ok("dashboard", "--export", str(page))
self.assertIn(f"M1@{s.sid[:8]}", page.read_text())
def test_carry_takes_a_run_only_from_a_session_that_ended(self):
for agent in AGENTS:
with self.subTest(agent=agent):
self.fresh()
a = self.session(agent)
a.ok("progress", "start", "Invoice export", "--plan", "api=30m [backend]")
b = self.session(agent, SECOND[agent])
r = b.dev("session", "carry", "--from", a.sid)
self.assertEqual(r["rc"], 2, r)
self.assertIn("is still live", r["err"])
a.hook("session-end", a.payload("SessionEnd", reason="exit"))
# the session's last sync, which the hook starts in the background (recorded here), ends its record
spawned = [json.loads(ln) for ln in self.spawns.read_text().splitlines() if ln.strip()]
finish = [args for args in spawned if "--finish" in args and f"{agent}:{a.sid}" in args]
self.assertEqual(len(finish), 1, spawned)
done = a.host.run(["dev", *finish[0]], self.home)
self.assertEqual(done["rc"], 0, done["err"][-1500:])
b.ok("session", "carry", "--from", a.sid)
self.assertEqual((a.has_run(), b.has_run(), b.run_doc()["goal"]), (False, True, "Invoice export"))
if __name__ == "__main__":
unittest.main()