claude-dev-env 8.124.0 → 8.125.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,25 +1,48 @@
1
+ from __future__ import annotations
2
+
1
3
  import io
2
4
  import json
5
+ import shutil
3
6
  import subprocess
4
7
  import sys
8
+ import tempfile
5
9
  from pathlib import Path
10
+ from typing import TYPE_CHECKING
6
11
 
7
12
  import pytest
8
13
 
14
+ import closing_reply_judge
9
15
  import open_tasks_stop_gate
10
16
  from open_tasks_transcript import TaskItem, replayed_transcript
11
17
 
18
+ if TYPE_CHECKING:
19
+ from conftest import JudgeStub
20
+
12
21
  GATE_PATH = Path(open_tasks_stop_gate.__file__)
13
22
  SESSION_ID = "s-123"
14
23
  WORK_PROMPT = "Export the APKs and submit them"
15
24
 
16
25
 
26
+ UNPINNED_WHICH = shutil.which
27
+
28
+
29
+ def _which_without_claude(command_name: str, *all_arguments: object, **all_options: object) -> str | None:
30
+ if command_name == "claude":
31
+ return None
32
+ return UNPINNED_WHICH(command_name, *all_arguments, **all_options)
33
+
34
+
17
35
  @pytest.fixture(autouse=True)
18
36
  def isolated_home(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
37
+ marker_directory = tmp_path / "markers"
38
+ marker_directory.mkdir()
39
+ monkeypatch.setattr(tempfile, "gettempdir", lambda: str(marker_directory))
19
40
  monkeypatch.setenv("HOME", str(tmp_path))
20
41
  monkeypatch.setenv("USERPROFILE", str(tmp_path))
21
42
  monkeypatch.delenv("CLAUDE_CONFIG_DIR", raising=False)
22
43
  monkeypatch.delenv("CLAUDE_CODE_TASK_LIST_ID", raising=False)
44
+ monkeypatch.delenv("CLAUDE_TURN_REVIEW_CHILD", raising=False)
45
+ monkeypatch.setattr(shutil, "which", _which_without_claude)
23
46
 
24
47
 
25
48
  def user_prompt_record(prompt_text: str) -> dict[str, object]:
@@ -54,12 +77,14 @@ def run_gate(
54
77
  is_stop_hook_active: bool = False,
55
78
  reply_text: str = "Done.",
56
79
  working_directory: Path | None = None,
80
+ all_extra_fields: dict[str, object] | None = None,
57
81
  ) -> dict[str, str]:
58
82
  hook_input: dict[str, object] = {
59
83
  "hook_event_name": "Stop",
60
84
  "session_id": SESSION_ID,
61
85
  "stop_hook_active": is_stop_hook_active,
62
86
  "last_assistant_message": reply_text,
87
+ **(all_extra_fields or {}),
63
88
  }
64
89
  if working_directory is not None:
65
90
  hook_input["cwd"] = str(working_directory)
@@ -623,3 +648,540 @@ def test_should_block_a_work_turn_whose_transcript_holds_an_undecodable_byte(
623
648
  with transcript_path.open("ab") as transcript_file:
624
649
  transcript_file.write(b"\n\xff\n")
625
650
  assert run_gate(monkeypatch, capsys, transcript_path)["decision"] == "block"
651
+
652
+
653
+ BED_PROMPT = "heading out now. handle whatever the nightly job returns"
654
+ PROMISE_REPLY = (
655
+ "Both fix runs are armed, and a wake is set for the end of CI."
656
+ " The benchmark table will be posted when you get back."
657
+ )
658
+ DEFERRED_PROMPT = "You did a lot of work. I want to know what you put off."
659
+ DEFERRED_REPLY = (
660
+ "What I deferred, checked against the build log:\n\n"
661
+ "1. The retry budget for the sync job.\n"
662
+ "2. The second fixture for the Windows path.\n"
663
+ "3. The docs page for the new flag.\n"
664
+ "4. The cleanup of the stale branch.\n"
665
+ "5. The follow-up issue for the timeout."
666
+ )
667
+ THREE_BULLETS = "- The retry budget.\n- The docs page.\n- The stale branch."
668
+ WAKE_CALL = tool_call_record("mcp__scheduler__send_later", {"delay_minutes": 30})
669
+ PROMISE_REASON_START = "The closing reply promises the user a later report"
670
+ LIST_REASON_START = "The closing reply lists"
671
+ REWORKED_REPLY_INSTRUCTION = "Then send the reworked reply and end the turn."
672
+ SHORT_REPLY_INSTRUCTION = "Then send a short reply that names what you did and end the turn."
673
+ PROMISE_KEEP_CONDITION = (
674
+ "Leave the promise as it is only when the reply already says that in one sentence, or when the user's"
675
+ " typed words in this turn asked for a report later."
676
+ )
677
+ LIST_KEEP_START = "Leave the list as it is only when every listed item is finished work or plain status"
678
+ ASKED_ANSWER_SENTENCE = (
679
+ 'A question such as "what did you defer?" asks for an answer, and the open decisions in that answer'
680
+ " still go out as question cards."
681
+ )
682
+ END_THE_TURN = "end the turn"
683
+
684
+
685
+ def closing_turn(prompt_text: str, *all_turn_calls: dict[str, object]) -> list[dict[str, object]]:
686
+ return [user_prompt_record(prompt_text), TRACKER_CALL, *all_turn_calls]
687
+
688
+
689
+ @pytest.mark.parametrize(
690
+ ("all_records", "reply_text", "reason_start"),
691
+ [
692
+ (closing_turn(BED_PROMPT, WAKE_CALL), PROMISE_REPLY, PROMISE_REASON_START),
693
+ (closing_turn(WORK_PROMPT, READ_CALL), "I'll report back once CI ends.", PROMISE_REASON_START),
694
+ (
695
+ closing_turn(DEFERRED_PROMPT, tool_call_record("Bash"), tool_call_record("Read")),
696
+ DEFERRED_REPLY,
697
+ LIST_REASON_START,
698
+ ),
699
+ (closing_turn(WORK_PROMPT, READ_CALL), f"## Next steps\n\n{THREE_BULLETS}", LIST_REASON_START),
700
+ (
701
+ closing_turn(WORK_PROMPT, READ_CALL),
702
+ "Open items:\n\n| Item | State |\n|---|---|\n| Retry | open |\n| Docs | open |\n| Branch | open |",
703
+ LIST_REASON_START,
704
+ ),
705
+ (
706
+ [user_prompt_record("<task-notification>summary: the run ended</task-notification>"), READ_CALL],
707
+ PROMISE_REPLY,
708
+ PROMISE_REASON_START,
709
+ ),
710
+ (
711
+ closing_turn(
712
+ "> The benchmark table will be posted when you get back.\n\n"
713
+ "don't need this; just see the fixes through",
714
+ READ_CALL,
715
+ ),
716
+ PROMISE_REPLY,
717
+ PROMISE_REASON_START,
718
+ ),
719
+ (closing_turn("no report needed, just finish", READ_CALL), PROMISE_REPLY, PROMISE_REASON_START),
720
+ (closing_turn("no list please, ask me instead", READ_CALL), DEFERRED_REPLY, LIST_REASON_START),
721
+ ],
722
+ )
723
+ def test_should_hold_a_closing_reply_that_promises_a_report_or_lists_open_items(
724
+ monkeypatch: pytest.MonkeyPatch,
725
+ capsys: pytest.CaptureFixture[str],
726
+ tmp_path: Path,
727
+ all_records: list[dict[str, object]],
728
+ reply_text: str,
729
+ reason_start: str,
730
+ ) -> None:
731
+ transcript_path = write_records(tmp_path / "s.jsonl", all_records)
732
+ block_output = run_gate(monkeypatch, capsys, transcript_path, reply_text=reply_text)
733
+ assert block_output["decision"] == "block"
734
+ assert block_output["reason"].startswith(reason_start)
735
+ assert block_output["reason"].lower().count(END_THE_TURN) == 1
736
+ assert block_output["reason"].endswith((PROMISE_KEEP_CONDITION, ASKED_ANSWER_SENTENCE))
737
+
738
+
739
+ def test_should_quote_the_lead_and_the_item_count_in_the_list_reason(
740
+ monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
741
+ ) -> None:
742
+ transcript_path = write_records(tmp_path / "s.jsonl", closing_turn(DEFERRED_PROMPT, READ_CALL))
743
+ block_reason = run_gate(monkeypatch, capsys, transcript_path, reply_text=DEFERRED_REPLY)["reason"]
744
+ assert 'lists 5 items under "What I deferred, checked' in block_reason
745
+ assert "AskUserQuestion" in block_reason
746
+ assert PROMISE_REASON_START not in block_reason
747
+
748
+
749
+ def test_should_quote_the_promise_in_the_report_reason(
750
+ monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
751
+ ) -> None:
752
+ transcript_path = write_records(tmp_path / "s.jsonl", closing_turn(BED_PROMPT, WAKE_CALL))
753
+ block_reason = run_gate(monkeypatch, capsys, transcript_path, reply_text=PROMISE_REPLY)["reason"]
754
+ assert '("when you get back")' in block_reason
755
+ assert LIST_REASON_START not in block_reason
756
+
757
+
758
+ def test_should_hold_a_codex_closing_reply_that_lists_open_items(
759
+ monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
760
+ ) -> None:
761
+ plan_arguments = json.dumps({"plan": [{"step": "Report the deferred work", "status": "completed"}]})
762
+ plan_call = codex_record({"type": "function_call", "name": "update_plan", "arguments": plan_arguments})
763
+ shell_call = codex_record({"type": "function_call", "name": "shell", "arguments": "{}"})
764
+ transcript_path = write_records(tmp_path / "rollout.jsonl", [codex_prompt(DEFERRED_PROMPT), plan_call, shell_call])
765
+ block_output = run_gate(monkeypatch, capsys, transcript_path, reply_text=DEFERRED_REPLY)
766
+ assert block_output["reason"].startswith(LIST_REASON_START)
767
+
768
+
769
+ @pytest.mark.parametrize(
770
+ ("all_records", "reply_text"),
771
+ [
772
+ (closing_turn(DEFERRED_PROMPT, READ_CALL, QUESTION_CALL), DEFERRED_REPLY),
773
+ (
774
+ [
775
+ codex_prompt(DEFERRED_PROMPT),
776
+ codex_record({"type": "function_call", "name": "update_plan", "arguments": "{}"}),
777
+ codex_record({"type": "function_call", "name": "request_user_input", "arguments": "{}"}),
778
+ ],
779
+ DEFERRED_REPLY,
780
+ ),
781
+ (closing_turn(WORK_PROMPT, READ_CALL), f"Changed files:\n\n{THREE_BULLETS}\n- The hook.\n- The test."),
782
+ (closing_turn(WORK_PROMPT, READ_CALL), "Deferred:\n\n- The retry budget.\n- The docs page."),
783
+ (closing_turn(WORK_PROMPT, READ_CALL), f"Nothing deferred.\n\n{THREE_BULLETS}"),
784
+ (closing_turn("list the deferred items as bullets", READ_CALL), DEFERRED_REPLY),
785
+ (closing_turn(WORK_PROMPT, READ_CALL), f"Deferred:\n\n```\n{THREE_BULLETS}\n```"),
786
+ (closing_turn(WORK_PROMPT, READ_CALL), "No report will follow."),
787
+ (closing_turn(WORK_PROMPT, READ_CALL), "> I'll report back once CI ends."),
788
+ (closing_turn(WORK_PROMPT, READ_CALL), "The draft says `I'll report back once CI ends` today."),
789
+ (closing_turn("report back when the run ends", WAKE_CALL), PROMISE_REPLY),
790
+ (closing_turn(WORK_PROMPT, READ_CALL), f"Skipped, with reasons\n\n{THREE_BULLETS}\n- The hook."),
791
+ (closing_turn(WORK_PROMPT, READ_CALL), "I'll merge each lint fix once the bots report back."),
792
+ (closing_turn(WORK_PROMPT, READ_CALL), "Both checkers are running. I continue when they report."),
793
+ ],
794
+ )
795
+ def test_should_pass_a_closing_reply_with_no_unasked_report_or_open_item_list(
796
+ monkeypatch: pytest.MonkeyPatch,
797
+ capsys: pytest.CaptureFixture[str],
798
+ tmp_path: Path,
799
+ all_records: list[dict[str, object]],
800
+ reply_text: str,
801
+ ) -> None:
802
+ transcript_path = write_records(tmp_path / "s.jsonl", all_records)
803
+ assert run_gate(monkeypatch, capsys, transcript_path, reply_text=reply_text) == {}
804
+
805
+
806
+ def test_should_pass_a_promised_report_on_a_stop_the_gate_already_held(
807
+ monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
808
+ ) -> None:
809
+ transcript_path = write_records(tmp_path / "s.jsonl", closing_turn(BED_PROMPT, WAKE_CALL))
810
+ assert run_gate(monkeypatch, capsys, transcript_path, is_stop_hook_active=True, reply_text=PROMISE_REPLY) == {}
811
+
812
+
813
+ def test_should_pass_a_closing_reply_when_the_transcript_cannot_be_read(
814
+ monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
815
+ ) -> None:
816
+ missing_path = tmp_path / "missing.jsonl"
817
+ assert run_gate(monkeypatch, capsys, missing_path, reply_text=PROMISE_REPLY) == {}
818
+ assert run_gate(monkeypatch, capsys, missing_path, reply_text=DEFERRED_REPLY) == {}
819
+ hook_input = {"last_assistant_message": DEFERRED_REPLY, "transcript_path": str(missing_path)}
820
+ assert open_tasks_stop_gate.closing_reply_findings(hook_input, replayed_transcript(None)) == []
821
+
822
+
823
+ def test_should_ask_for_a_reworked_reply_when_a_tracker_reason_and_a_closing_reason_combine(
824
+ monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
825
+ ) -> None:
826
+ transcript_path = write_records(tmp_path / "s.jsonl", [user_prompt_record(BED_PROMPT), WAKE_CALL])
827
+ block_reason = run_gate(monkeypatch, capsys, transcript_path, reply_text=PROMISE_REPLY)["reason"]
828
+ assert block_reason.startswith("This turn worked on the user's ask and left the task list unchanged.")
829
+ assert block_reason.index("left the task list unchanged") < block_reason.index(PROMISE_REASON_START)
830
+ assert "Leave the reply as it stands" not in block_reason
831
+ assert "leave the reply as it stands" not in block_reason
832
+ assert block_reason.endswith(f"{REWORKED_REPLY_INSTRUCTION} {PROMISE_KEEP_CONDITION}")
833
+
834
+
835
+ def test_should_name_the_steps_then_end_the_turn_then_the_keep_condition_for_a_promise(
836
+ monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
837
+ ) -> None:
838
+ transcript_path = write_records(tmp_path / "s.jsonl", closing_turn(BED_PROMPT, WAKE_CALL))
839
+ block_reason = run_gate(monkeypatch, capsys, transcript_path, reply_text=PROMISE_REPLY)["reason"]
840
+ assert block_reason == (
841
+ 'The closing reply promises the user a later report ("when you get back"), and no typed prompt in this'
842
+ " turn asked for one. Drop the promise and carry the work to completion. If work is still open, do it"
843
+ " now. If the turn waits on a run or a wake you armed, say in one sentence what runs and what you do"
844
+ f" when it ends. {REWORKED_REPLY_INSTRUCTION} {PROMISE_KEEP_CONDITION}"
845
+ )
846
+
847
+
848
+ def test_should_keep_question_cards_for_a_list_sent_in_answer_to_what_was_deferred(
849
+ monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
850
+ ) -> None:
851
+ deferred_question = "Which parts of the work did you put off or skip?"
852
+ transcript_path = write_records(tmp_path / "s.jsonl", closing_turn(deferred_question, READ_CALL))
853
+ block_reason = run_gate(monkeypatch, capsys, transcript_path, reply_text=DEFERRED_REPLY)["reason"]
854
+ assert block_reason.startswith('The closing reply lists 5 items under "What I deferred, checked')
855
+ assert block_reason.lower().count(END_THE_TURN) == 1
856
+ assert block_reason.index(SHORT_REPLY_INSTRUCTION) < block_reason.index(LIST_KEEP_START)
857
+ assert "asked for the items as a list or a table in chat" in block_reason
858
+ assert block_reason.endswith(ASKED_ANSWER_SENTENCE)
859
+
860
+
861
+ @pytest.mark.parametrize(
862
+ ("all_records", "reply_text", "all_reason_parts"),
863
+ [
864
+ (
865
+ closing_turn(WORK_PROMPT, READ_CALL),
866
+ f"I'll report back once CI ends.\n\nWhat I deferred:\n{THREE_BULLETS}",
867
+ (PROMISE_REASON_START, LIST_REASON_START, SHORT_REPLY_INSTRUCTION, PROMISE_KEEP_CONDITION, LIST_KEEP_START),
868
+ ),
869
+ (
870
+ [user_prompt_record(BED_PROMPT), WAKE_CALL],
871
+ f"What I deferred:\n{THREE_BULLETS}",
872
+ ("left the task list unchanged", LIST_REASON_START, SHORT_REPLY_INSTRUCTION, LIST_KEEP_START),
873
+ ),
874
+ (
875
+ closing_turn(WORK_PROMPT, tool_call_record("Edit")),
876
+ f"Three items need you.\n\n{PROMISE_REPLY}",
877
+ ("waits on them", "The turn ran no read call.", PROMISE_REASON_START, REWORKED_REPLY_INSTRUCTION, PROMISE_KEEP_CONDITION),
878
+ ),
879
+ ],
880
+ )
881
+ def test_should_end_the_turn_once_when_findings_combine(
882
+ monkeypatch: pytest.MonkeyPatch,
883
+ capsys: pytest.CaptureFixture[str],
884
+ tmp_path: Path,
885
+ all_records: list[dict[str, object]],
886
+ reply_text: str,
887
+ all_reason_parts: tuple[str, ...],
888
+ ) -> None:
889
+ transcript_path = write_records(tmp_path / "s.jsonl", all_records)
890
+ block_reason = run_gate(monkeypatch, capsys, transcript_path, reply_text=reply_text)["reason"]
891
+ assert block_reason.lower().count(END_THE_TURN) == 1
892
+ all_positions = [block_reason.index(each_part) for each_part in all_reason_parts]
893
+ assert all_positions == sorted(all_positions)
894
+
895
+
896
+ def test_should_hold_a_wait_on_a_running_agent_with_the_keep_condition(
897
+ monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
898
+ ) -> None:
899
+ notification = "<task-notification>the reuse agent finished</task-notification>"
900
+ transcript_path = write_records(tmp_path / "s.jsonl", [user_prompt_record(notification), READ_CALL])
901
+ reply_text = "The lint job is still running, and I'll report once it finishes."
902
+ block_reason = run_gate(monkeypatch, capsys, transcript_path, reply_text=reply_text)["reason"]
903
+ assert '("I\'ll report once")' in block_reason
904
+ assert block_reason.endswith(PROMISE_KEEP_CONDITION)
905
+
906
+
907
+ OTHER_HOOK_FEEDBACK = {
908
+ "type": "user",
909
+ "isMeta": True,
910
+ "uuid": "feedback-1",
911
+ "message": {
912
+ "role": "user",
913
+ "content": "Stop hook feedback:\n[command hook 2]: The build cache is out of date.",
914
+ },
915
+ }
916
+
917
+
918
+ def keyed_prompt_record(prompt_text: str, prompt_uuid: str) -> dict[str, object]:
919
+ return {**user_prompt_record(prompt_text), "uuid": prompt_uuid}
920
+
921
+
922
+ def keyed_turn(prompt_uuid: str = "prompt-1") -> list[dict[str, object]]:
923
+ return [keyed_prompt_record(BED_PROMPT, prompt_uuid), TRACKER_CALL, WAKE_CALL, OTHER_HOOK_FEEDBACK]
924
+
925
+
926
+ def test_should_hold_a_promise_once_after_another_stop_hook_held_the_turn(
927
+ monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
928
+ ) -> None:
929
+ transcript_path = write_records(tmp_path / "s.jsonl", keyed_turn())
930
+ all_outputs = [
931
+ run_gate(monkeypatch, capsys, transcript_path, is_stop_hook_active=True, reply_text=PROMISE_REPLY)
932
+ for _ in range(3)
933
+ ]
934
+ assert all_outputs[0]["reason"].startswith(PROMISE_REASON_START)
935
+ assert all_outputs[0]["reason"].endswith(PROMISE_KEEP_CONDITION)
936
+ assert all_outputs[1:] == [{}, {}]
937
+
938
+
939
+ def test_should_hold_again_after_a_new_prompt_in_the_same_session(
940
+ monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
941
+ ) -> None:
942
+ first_path = write_records(tmp_path / "s.jsonl", keyed_turn("prompt-1"))
943
+ assert run_gate(monkeypatch, capsys, first_path, is_stop_hook_active=True, reply_text=PROMISE_REPLY)
944
+ second_path = write_records(tmp_path / "s.jsonl", [*keyed_turn("prompt-1"), *keyed_turn("prompt-2")])
945
+ second_output = run_gate(monkeypatch, capsys, second_path, is_stop_hook_active=True, reply_text=PROMISE_REPLY)
946
+ assert second_output["reason"].startswith(PROMISE_REASON_START)
947
+
948
+
949
+ def test_should_hold_a_list_once_after_another_stop_hook_held_the_turn(
950
+ monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
951
+ ) -> None:
952
+ all_records = [keyed_prompt_record(DEFERRED_PROMPT, "prompt-1"), TRACKER_CALL, READ_CALL, OTHER_HOOK_FEEDBACK]
953
+ transcript_path = write_records(tmp_path / "s.jsonl", all_records)
954
+ first_output = run_gate(monkeypatch, capsys, transcript_path, is_stop_hook_active=True, reply_text=DEFERRED_REPLY)
955
+ assert first_output["reason"].startswith(LIST_REASON_START)
956
+ assert run_gate(monkeypatch, capsys, transcript_path, is_stop_hook_active=True, reply_text=DEFERRED_REPLY) == {}
957
+
958
+
959
+ def test_should_pass_a_held_stop_when_the_turn_marker_cannot_be_written(
960
+ monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
961
+ ) -> None:
962
+ monkeypatch.setattr(tempfile, "gettempdir", lambda: str(tmp_path / "absent_directory"))
963
+ transcript_path = write_records(tmp_path / "s.jsonl", keyed_turn())
964
+ assert run_gate(monkeypatch, capsys, transcript_path, is_stop_hook_active=True, reply_text=PROMISE_REPLY) == {}
965
+
966
+
967
+ def test_should_pass_a_held_stop_after_this_gate_held_the_turn(
968
+ monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
969
+ ) -> None:
970
+ unkept_path = tmp_path / "absent_directory"
971
+ monkeypatch.setattr(tempfile, "gettempdir", lambda: str(unkept_path))
972
+ transcript_path = write_records(tmp_path / "s.jsonl", keyed_turn()[:-1])
973
+ first_reason = run_gate(monkeypatch, capsys, transcript_path, reply_text=PROMISE_REPLY)["reason"]
974
+ own_feedback = {**OTHER_HOOK_FEEDBACK, "message": {"role": "user", "content": f"Stop hook feedback:\n{first_reason}"}}
975
+ write_records(transcript_path, [*keyed_turn()[:-1], own_feedback])
976
+ unkept_path.mkdir()
977
+ assert run_gate(monkeypatch, capsys, transcript_path, is_stop_hook_active=True, reply_text=PROMISE_REPLY) == {}
978
+
979
+
980
+ def test_should_pass_a_held_stop_after_this_gate_held_the_first_stop(
981
+ monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
982
+ ) -> None:
983
+ transcript_path = write_records(tmp_path / "s.jsonl", keyed_turn()[:-1])
984
+ assert run_gate(monkeypatch, capsys, transcript_path, reply_text=PROMISE_REPLY)["decision"] == "block"
985
+ assert run_gate(monkeypatch, capsys, transcript_path, is_stop_hook_active=True, reply_text=PROMISE_REPLY) == {}
986
+
987
+
988
+ def test_should_keep_skipping_a_held_codex_stop(
989
+ monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
990
+ ) -> None:
991
+ plan_arguments = json.dumps({"plan": [{"step": "Report the run", "status": "in_progress"}]})
992
+ plan_call = codex_record({"type": "function_call", "name": "update_plan", "arguments": plan_arguments})
993
+ transcript_path = write_records(tmp_path / "rollout.jsonl", [codex_prompt(BED_PROMPT), plan_call])
994
+ assert run_gate(monkeypatch, capsys, transcript_path, is_stop_hook_active=True, reply_text=PROMISE_REPLY) == {}
995
+
996
+
997
+ def test_should_leave_the_tracker_reasons_off_a_held_stop(
998
+ monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
999
+ ) -> None:
1000
+ all_records = [keyed_prompt_record(WORK_PROMPT, "prompt-1"), tool_call_record("Bash"), OTHER_HOOK_FEEDBACK]
1001
+ transcript_path = write_records(tmp_path / "s.jsonl", all_records)
1002
+ assert run_gate(monkeypatch, capsys, transcript_path, is_stop_hook_active=True) == {}
1003
+ block_reason = run_gate(monkeypatch, capsys, transcript_path, is_stop_hook_active=True, reply_text=PROMISE_REPLY)[
1004
+ "reason"
1005
+ ]
1006
+ assert "left the task list unchanged" not in block_reason
1007
+
1008
+
1009
+ JUDGE_ONLY_PROMISE = "Once the soak test settles, the numbers go in this thread."
1010
+ JUDGE_ONLY_PROMISE_REPLY = f"Both soak runs are armed. {JUDGE_ONLY_PROMISE}"
1011
+ JUDGE_ONLY_LIST_LEAD = "Three calls are yours:"
1012
+ JUDGE_ONLY_LIST_REPLY = f"{JUDGE_ONLY_LIST_LEAD}\n\n{THREE_BULLETS}"
1013
+ JUDGE_LOG_NAME = Path(".claude") / "logs" / "closing-reply-judge.log"
1014
+
1015
+
1016
+ def judge_answer(promise_span: str = "", list_span: str = "") -> dict[str, object]:
1017
+ return {
1018
+ "promised_report": bool(promise_span),
1019
+ "promise_span": promise_span,
1020
+ "decisions_as_list": bool(list_span),
1021
+ "list_span": list_span,
1022
+ }
1023
+
1024
+
1025
+ def judged_turn(prompt_text: str = BED_PROMPT) -> list[dict[str, object]]:
1026
+ return [keyed_prompt_record(prompt_text, "prompt-1"), TRACKER_CALL, WAKE_CALL]
1027
+
1028
+
1029
+ def assistant_text_record(reply_text: str) -> dict[str, object]:
1030
+ return {
1031
+ "type": "assistant",
1032
+ "message": {"id": "reply-1", "role": "assistant", "content": [{"type": "text", "text": reply_text}]},
1033
+ }
1034
+
1035
+
1036
+ def test_should_hold_when_judge_reports_a_promised_report(
1037
+ monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path, judge_stub: JudgeStub
1038
+ ) -> None:
1039
+ judge_stub.answer(verdict=judge_answer(promise_span=JUDGE_ONLY_PROMISE))
1040
+ transcript_path = write_records(tmp_path / "s.jsonl", judged_turn())
1041
+ block_reason = run_gate(monkeypatch, capsys, transcript_path, reply_text=JUDGE_ONLY_PROMISE_REPLY)["reason"]
1042
+ assert block_reason.startswith(PROMISE_REASON_START)
1043
+ assert f'("{JUDGE_ONLY_PROMISE}")' in block_reason
1044
+ assert block_reason.endswith(PROMISE_KEEP_CONDITION)
1045
+ assert len(judge_stub.calls()) == 1
1046
+
1047
+
1048
+ def test_should_hold_when_judge_reports_decisions_as_a_list(
1049
+ monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path, judge_stub: JudgeStub
1050
+ ) -> None:
1051
+ judge_stub.answer(verdict=judge_answer(list_span=JUDGE_ONLY_LIST_LEAD))
1052
+ transcript_path = write_records(tmp_path / "s.jsonl", judged_turn("Clean up the release branch"))
1053
+ block_reason = run_gate(monkeypatch, capsys, transcript_path, reply_text=JUDGE_ONLY_LIST_REPLY)["reason"]
1054
+ assert block_reason.startswith(f'The closing reply lists 3 items under "{JUDGE_ONLY_LIST_LEAD}"')
1055
+ assert SHORT_REPLY_INSTRUCTION in block_reason
1056
+
1057
+
1058
+ def test_should_allow_the_stop_when_the_judge_times_out(
1059
+ monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path, judge_stub: JudgeStub
1060
+ ) -> None:
1061
+ judge_stub.answer(verdict=judge_answer(promise_span=JUDGE_ONLY_PROMISE))
1062
+ judge_stub.sleep(5)
1063
+ monkeypatch.setattr(closing_reply_judge, "JUDGE_TIMEOUT_SECONDS", 0.5)
1064
+ transcript_path = write_records(tmp_path / "s.jsonl", judged_turn())
1065
+ assert run_gate(monkeypatch, capsys, transcript_path, reply_text=JUDGE_ONLY_PROMISE_REPLY) == {}
1066
+ assert "the child timed out" in (tmp_path / JUDGE_LOG_NAME).read_text()
1067
+
1068
+
1069
+ def test_should_allow_the_stop_when_the_judge_answer_is_not_the_verdict_shape(
1070
+ monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path, judge_stub: JudgeStub
1071
+ ) -> None:
1072
+ judge_stub.answer(result="The reply promises a report.")
1073
+ transcript_path = write_records(tmp_path / "s.jsonl", judged_turn())
1074
+ assert run_gate(monkeypatch, capsys, transcript_path, reply_text=JUDGE_ONLY_PROMISE_REPLY) == {}
1075
+ assert len(judge_stub.calls()) == 1
1076
+
1077
+
1078
+ def test_should_allow_the_stop_when_the_span_is_not_in_the_reply(
1079
+ monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path, judge_stub: JudgeStub
1080
+ ) -> None:
1081
+ judge_stub.answer(verdict=judge_answer(promise_span="The numbers go in this thread tomorrow."))
1082
+ transcript_path = write_records(tmp_path / "s.jsonl", judged_turn())
1083
+ assert run_gate(monkeypatch, capsys, transcript_path, reply_text=JUDGE_ONLY_PROMISE_REPLY) == {}
1084
+
1085
+
1086
+ def test_should_use_the_phrase_checks_when_claude_is_missing(
1087
+ monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
1088
+ ) -> None:
1089
+ transcript_path = write_records(tmp_path / "s.jsonl", judged_turn())
1090
+ assert run_gate(monkeypatch, capsys, transcript_path, reply_text=PROMISE_REPLY)["reason"].startswith(
1091
+ PROMISE_REASON_START
1092
+ )
1093
+ assert run_gate(monkeypatch, capsys, transcript_path, reply_text=JUDGE_ONLY_PROMISE_REPLY) == {}
1094
+
1095
+
1096
+ def test_should_not_hold_on_phrases_when_the_judge_gives_no_verdict(
1097
+ monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path, judge_stub: JudgeStub
1098
+ ) -> None:
1099
+ judge_stub.exit_code(1)
1100
+ transcript_path = write_records(tmp_path / "s.jsonl", judged_turn())
1101
+ assert run_gate(monkeypatch, capsys, transcript_path, reply_text=PROMISE_REPLY) == {}
1102
+ assert len(judge_stub.calls()) == 1
1103
+
1104
+
1105
+ def test_should_hold_once_per_turn_when_the_judge_holds_twice(
1106
+ monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path, judge_stub: JudgeStub
1107
+ ) -> None:
1108
+ judge_stub.answer(verdict=judge_answer(promise_span=JUDGE_ONLY_PROMISE))
1109
+ transcript_path = write_records(tmp_path / "s.jsonl", [*judged_turn(), OTHER_HOOK_FEEDBACK])
1110
+ all_outputs = [
1111
+ run_gate(monkeypatch, capsys, transcript_path, is_stop_hook_active=True, reply_text=JUDGE_ONLY_PROMISE_REPLY)
1112
+ for _ in range(3)
1113
+ ]
1114
+ assert all_outputs[0]["reason"].startswith(PROMISE_REASON_START)
1115
+ assert all_outputs[1:] == [{}, {}]
1116
+ assert len(judge_stub.calls()) == 1
1117
+
1118
+
1119
+ def test_should_not_call_the_judge_after_this_turn_was_held(
1120
+ monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path, judge_stub: JudgeStub
1121
+ ) -> None:
1122
+ judge_stub.answer(verdict=judge_answer(promise_span=JUDGE_ONLY_PROMISE))
1123
+ transcript_path = write_records(tmp_path / "s.jsonl", judged_turn())
1124
+ assert run_gate(monkeypatch, capsys, transcript_path, reply_text=JUDGE_ONLY_PROMISE_REPLY)["decision"] == "block"
1125
+ held_stop = run_gate(monkeypatch, capsys, transcript_path, is_stop_hook_active=True, reply_text=JUDGE_ONLY_PROMISE_REPLY)
1126
+ assert held_stop == {}
1127
+ assert len(judge_stub.calls()) == 1
1128
+
1129
+
1130
+ def test_should_allow_the_stop_inside_a_judge_or_review_child(
1131
+ monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path, judge_stub: JudgeStub
1132
+ ) -> None:
1133
+ judge_stub.answer(verdict=judge_answer(promise_span=JUDGE_ONLY_PROMISE))
1134
+ monkeypatch.setenv("CLAUDE_TURN_REVIEW_CHILD", "1")
1135
+ transcript_path = write_records(
1136
+ tmp_path / "s.jsonl", [keyed_prompt_record(WORK_PROMPT, "prompt-1"), tool_call_record("Bash")]
1137
+ )
1138
+ assert run_gate(monkeypatch, capsys, transcript_path, reply_text=JUDGE_ONLY_PROMISE_REPLY) == {}
1139
+ assert judge_stub.calls() == []
1140
+
1141
+
1142
+ def test_should_not_call_the_judge_on_codex_or_cursor_payloads(
1143
+ monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path, judge_stub: JudgeStub
1144
+ ) -> None:
1145
+ judge_stub.answer(verdict=judge_answer(promise_span=JUDGE_ONLY_PROMISE))
1146
+ plan_arguments = json.dumps({"plan": [{"step": "Report the run", "status": "in_progress"}]})
1147
+ plan_call = codex_record({"type": "function_call", "name": "update_plan", "arguments": plan_arguments})
1148
+ rollout_path = write_records(tmp_path / "rollout.jsonl", [codex_prompt(BED_PROMPT), plan_call])
1149
+ codex_output = run_gate(
1150
+ monkeypatch, capsys, rollout_path, reply_text=PROMISE_REPLY, all_extra_fields={"turn_id": "turn-1"}
1151
+ )
1152
+ assert codex_output["reason"].startswith(PROMISE_REASON_START)
1153
+ assert run_gate(monkeypatch, capsys, rollout_path, reply_text=PROMISE_REPLY)["reason"].startswith(
1154
+ PROMISE_REASON_START
1155
+ )
1156
+ cursor_path = write_records(tmp_path / "cursor.jsonl", judged_turn())
1157
+ cursor_output = run_gate(
1158
+ monkeypatch,
1159
+ capsys,
1160
+ cursor_path,
1161
+ reply_text=JUDGE_ONLY_PROMISE_REPLY,
1162
+ all_extra_fields={"conversation_id": "c-1", "status": "completed", "loop_count": 0},
1163
+ )
1164
+ assert cursor_output == {}
1165
+ assert judge_stub.calls() == []
1166
+
1167
+
1168
+ def test_should_make_no_judge_call_when_the_transcript_cannot_be_read(
1169
+ monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path, judge_stub: JudgeStub
1170
+ ) -> None:
1171
+ judge_stub.answer(verdict=judge_answer(promise_span=JUDGE_ONLY_PROMISE))
1172
+ assert run_gate(monkeypatch, capsys, tmp_path / "absent.jsonl", reply_text=JUDGE_ONLY_PROMISE_REPLY) == {}
1173
+ assert judge_stub.calls() == []
1174
+ assert "the transcript cannot be read" in (tmp_path / JUDGE_LOG_NAME).read_text()
1175
+
1176
+
1177
+ def test_should_read_the_reply_from_the_transcript_when_the_payload_has_none(
1178
+ monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path, judge_stub: JudgeStub
1179
+ ) -> None:
1180
+ judge_stub.answer(verdict=judge_answer(promise_span=JUDGE_ONLY_PROMISE))
1181
+ transcript_path = write_records(
1182
+ tmp_path / "s.jsonl", [*judged_turn(), assistant_text_record(JUDGE_ONLY_PROMISE_REPLY)]
1183
+ )
1184
+ block_reason = run_gate(monkeypatch, capsys, transcript_path, reply_text="")["reason"]
1185
+ assert block_reason.startswith(PROMISE_REASON_START)
1186
+ [judge_call] = judge_stub.calls()
1187
+ assert json.dumps(JUDGE_ONLY_PROMISE_REPLY) in judge_call["stdin"]
@@ -438,3 +438,26 @@ def test_should_note_the_read_calls_and_sent_files_of_the_current_turn_only(tmp_
438
438
  later_replay = replayed_transcript(str(write_transcript(tmp_path / "b.jsonl", all_later_records)))
439
439
  assert (earlier_replay.has_turn_read_tool, earlier_replay.turn_sent_file_paths) == (True, ["old.md"])
440
440
  assert (later_replay.has_turn_read_tool, later_replay.turn_sent_file_paths) == (False, ["status.md"])
441
+
442
+
443
+ def test_should_key_the_turn_by_its_prompt_uuid_and_keep_it_across_stop_feedback(tmp_path: Path) -> None:
444
+ feedback_text = "Stop hook feedback:\n[command hook 6]: There are uncommitted changes."
445
+ first_prompt = {"type": "user", "uuid": "prompt-1", "message": {"role": "user", "content": "Write the gate"}}
446
+ feedback_record = {"type": "user", "uuid": "feedback-1", "message": {"role": "user", "content": feedback_text}}
447
+ second_prompt = {"type": "user", "uuid": "prompt-2", "message": {"role": "user", "content": "Export the APKs"}}
448
+ first_path = write_transcript(tmp_path / "first.jsonl", [first_prompt, feedback_record])
449
+ first_replay = replayed_transcript(str(first_path))
450
+ assert first_replay.turn_key == "prompt-1"
451
+ assert first_replay.is_codex_turn is False
452
+ assert first_replay.turn_stop_feedback_texts == [feedback_text]
453
+ second_replay = replayed_transcript(str(write_transcript(tmp_path / "second.jsonl", [first_prompt, feedback_record, second_prompt])))
454
+ assert second_replay.turn_key == "prompt-2"
455
+ assert second_replay.turn_stop_feedback_texts == []
456
+
457
+
458
+ def test_should_give_a_codex_turn_no_turn_key(tmp_path: Path) -> None:
459
+ codex_prompt = {"type": "response_item", "payload": {"type": "message", "role": "user", "content": "Export the APKs"}}
460
+ replay = replayed_transcript(str(write_transcript(tmp_path / "rollout.jsonl", [codex_prompt])))
461
+ assert replay.turn_prompt_text == "Export the APKs"
462
+ assert replay.turn_key is None
463
+ assert replay.is_codex_turn is True
@@ -13,6 +13,7 @@ from open_tasks_transcript_records import (
13
13
  normalized_text,
14
14
  parsed_object,
15
15
  send_call_file_paths,
16
+ stop_feedback_text,
16
17
  )
17
18
 
18
19
 
@@ -175,6 +176,19 @@ def test_should_count_a_codex_call_as_a_read_by_its_type_and_tool_name() -> None
175
176
  assert is_codex_read_call({"type": "event_msg", "payload": {"type": "local_shell_call"}}) is False
176
177
 
177
178
 
179
+ @pytest.mark.parametrize("is_meta", [True, False])
180
+ def test_should_read_the_text_of_a_stop_hook_feedback_record(is_meta: bool) -> None:
181
+ feedback_text = "Stop hook feedback:\n[command hook 6]: There are uncommitted changes."
182
+ feedback_record = {"type": "user", "isMeta": is_meta, "message": {"role": "user", "content": feedback_text}}
183
+ assert stop_feedback_text(feedback_record) == feedback_text
184
+
185
+
186
+ def test_should_read_no_feedback_from_a_typed_prompt_or_an_assistant_record() -> None:
187
+ assert stop_feedback_text({"type": "user", "message": {"role": "user", "content": "Fix the hook"}}) is None
188
+ assistant_record = {"type": "assistant", "message": {"content": "Stop hook feedback: quoted"}}
189
+ assert stop_feedback_text(assistant_record) is None
190
+
191
+
178
192
  def attachment_record(all_attachment_fields: dict[str, object]) -> dict[str, object]:
179
193
  return {"type": "attachment", "attachment": all_attachment_fields}
180
194