claude-dev-env 8.116.5 → 8.117.1
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.
- package/bin/ever-registered-hooks.mjs +2 -0
- package/bin/ever-registered-hooks.test.mjs +4 -0
- package/bin/install-constants.mjs +3 -1
- package/bin/install.cursor-rules.test.mjs +64 -2
- package/bin/install.mjs +15 -2
- package/bin/install.prune.test.mjs +92 -0
- package/bin/install.test.mjs +1 -0
- package/docs/model-choice/escalation.md +3 -3
- package/docs/model-choice/subagents-and-fan-out.md +2 -2
- package/docs/model-choice/which-model.md +1 -1
- package/docs/references/working-style.md +1 -0
- package/docs/rule-guides/question-presentation.md +6 -0
- package/docs/rule-guides/session-title.md +8 -9
- package/docs/rule-guides/subagent-model-effort.md +13 -13
- package/hooks/advisory/owned_pr_review_loop.py +2 -3
- package/hooks/blocking/bash_post_call_dispatcher.py +17 -9
- package/hooks/blocking/bash_pre_tool_use_dispatcher.py +12 -8
- package/hooks/blocking/final_reply_gate.py +5 -9
- package/hooks/blocking/open_tasks_stop_gate.py +9 -20
- package/hooks/blocking/open_tasks_transcript.py +8 -3
- package/hooks/blocking/pii_prevention_blocker_parts/repository_exemption.py +7 -2
- package/hooks/blocking/question_brief_gate.py +382 -0
- package/hooks/blocking/step_note_gate.py +5 -2
- package/hooks/blocking/task_subject_length_gate.py +111 -0
- package/hooks/blocking/test_bash_post_call_dispatcher.py +36 -1
- package/hooks/blocking/test_bash_pre_tool_use_dispatcher.py +57 -0
- package/hooks/blocking/test_final_reply_gate.py +26 -2
- package/hooks/blocking/test_open_tasks_stop_gate.py +21 -11
- package/hooks/blocking/test_question_brief_gate.py +916 -0
- package/hooks/blocking/test_step_note_gate.py +26 -0
- package/hooks/blocking/test_task_subject_length_gate.py +221 -0
- package/hooks/blocking/test_turn_review_child.py +166 -0
- package/hooks/blocking/test_turn_review_claim.py +194 -0
- package/hooks/blocking/test_turn_review_findings.py +124 -1
- package/hooks/blocking/test_turn_review_gate.py +1312 -10
- package/hooks/blocking/test_turn_review_repositories.py +65 -0
- package/hooks/blocking/test_turn_review_snapshot.py +8 -0
- package/hooks/blocking/test_turn_review_state.py +132 -0
- package/hooks/blocking/test_turn_review_transcript.py +125 -0
- package/hooks/blocking/tests/test_repository_exemption.py +11 -0
- package/hooks/blocking/turn_review_child.py +145 -0
- package/hooks/blocking/turn_review_claim.py +306 -0
- package/hooks/blocking/turn_review_findings.py +173 -13
- package/hooks/blocking/turn_review_gate.py +259 -262
- package/hooks/blocking/turn_review_repositories.py +58 -0
- package/hooks/blocking/turn_review_snapshot.py +5 -1
- package/hooks/blocking/turn_review_state.py +260 -0
- package/hooks/blocking/turn_review_transcript.py +158 -0
- package/hooks/features/bash-dispatchers.md +1 -3
- package/hooks/features/conduct-gates.md +19 -9
- package/hooks/features/lifecycle-cleanup.md +1 -1
- package/hooks/features/spawn-routing.md +1 -1
- package/hooks/hooks.json +24 -11
- package/hooks/hooks_constants/final_reply_gate_constants.py +0 -3
- package/hooks/hooks_constants/hook_time_budget.py +61 -0
- package/hooks/hooks_constants/hosted_hook_runner.py +51 -21
- package/hooks/hooks_constants/hosted_hook_runner_constants.py +3 -0
- package/hooks/hooks_constants/nested_project_hooks_constants.py +8 -0
- package/hooks/hooks_constants/once_per_session_constants.py +1 -0
- package/hooks/hooks_constants/open_tasks_stop_gate_constants.py +0 -4
- package/hooks/hooks_constants/owned_pr_review_loop_constants.py +0 -4
- package/hooks/hooks_constants/pre_tool_use_context_runner.py +23 -3
- package/hooks/hooks_constants/pre_tool_use_context_runner_constants.py +5 -0
- package/hooks/hooks_constants/pre_tool_use_stdin.py +1 -1
- package/hooks/hooks_constants/question_brief_gate_constants.py +66 -0
- package/hooks/hooks_constants/session_title_constants.py +8 -29
- package/hooks/hooks_constants/stop_hook_hold.py +54 -0
- package/hooks/hooks_constants/stop_hook_hold_constants.py +9 -0
- package/hooks/hooks_constants/subagent_effort_levels.json +6 -0
- package/hooks/hooks_constants/subagent_effort_route_constants.py +20 -19
- package/hooks/hooks_constants/task_subject_length_gate_constants.py +43 -0
- package/hooks/hooks_constants/test_hook_time_budget.py +104 -0
- package/hooks/hooks_constants/test_hosted_hook_runner.py +12 -0
- package/hooks/hooks_constants/test_pre_tool_use_context_runner.py +16 -0
- package/hooks/hooks_constants/test_pre_tool_use_stdin.py +6 -0
- package/hooks/hooks_constants/test_stop_hook_hold.py +40 -0
- package/hooks/hooks_constants/thread_spawn_pace_hook_constants.py +1 -2
- package/hooks/hooks_constants/turn_review_gate_constants.py +246 -12
- package/hooks/lifecycle/nested_project_hooks.py +34 -4
- package/hooks/lifecycle/test_nested_project_hooks.py +52 -0
- package/hooks/once_per_session.py +33 -4
- package/hooks/open_task_asks.py +1 -1
- package/hooks/owned_pr_session_record.py +1 -76
- package/hooks/routing/subagent_effort_route.py +107 -13
- package/hooks/routing/subagent_effort_route_hook.py +9 -8
- package/hooks/routing/test_subagent_effort_route.py +201 -14
- package/hooks/routing/test_subagent_effort_route_hook.py +56 -0
- package/hooks/routing/test_thread_spawn_pace_hook.py +32 -0
- package/hooks/routing/thread_spawn_pace_hook.py +3 -3
- package/hooks/session/background_silence_prompt.py +1 -1
- package/hooks/session/test_background_silence_prompt.py +8 -0
- package/hooks/session/test_tracking_nudge.py +125 -2
- package/hooks/session/tracking_nudge.py +28 -18
- package/hooks/session_title_transcript.py +216 -0
- package/hooks/test_once_per_session.py +10 -0
- package/hooks/test_owned_pr_session_record.py +0 -26
- package/hooks/test_session_title_transcript.py +142 -0
- package/hooks/validation/post_tool_use_dispatcher.py +38 -21
- package/hooks/validation/test_post_tool_use_dispatcher.py +43 -0
- package/package.json +1 -1
- package/rules/question-presentation.md +4 -4
- package/rules/subagent-model-effort.md +1 -1
- package/scripts/account_broker_support.py +24 -5
- package/scripts/claude_account_worker.py +4 -3
- package/scripts/dev_env_scripts_constants/account_broker_constants.py +1 -0
- package/scripts/policy_lint/adapter_configuration.py +12 -11
- package/scripts/policy_lint/config/approved_test_pairs.py +27 -0
- package/scripts/policy_lint/config/constants.py +2 -2
- package/scripts/test_account_broker_support.py +43 -1
- package/scripts/test_claude_account_worker.py +1 -0
- package/scripts/tests/test_adapter_configuration.py +10 -2
- package/scripts/tests/test_policy_lint_rules_pairing.py +24 -0
- package/hooks/blocking/owned_pr_merge_stop_gate.py +0 -185
- package/hooks/blocking/session_title_turn_gate.py +0 -417
- package/hooks/blocking/test_owned_pr_merge_stop_gate.py +0 -254
- package/hooks/blocking/test_session_title_turn_gate.py +0 -396
- package/hooks/hooks_constants/owned_pr_merge_stop_gate_constants.py +0 -40
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import io
|
|
2
2
|
import json
|
|
3
|
+
import os
|
|
4
|
+
import subprocess
|
|
5
|
+
import sys
|
|
3
6
|
from collections.abc import Callable
|
|
4
7
|
from pathlib import Path
|
|
5
8
|
|
|
@@ -259,3 +262,26 @@ def test_should_allow_a_call_whose_message_outgrows_the_tail_window(
|
|
|
259
262
|
exit_code, _ = run_gate(monkeypatch, capsys, transcript, "call_1")
|
|
260
263
|
|
|
261
264
|
assert exit_code == 0
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
@pytest.mark.parametrize(
|
|
268
|
+
"stdin_text",
|
|
269
|
+
["", "{not json", "[1]", '{"tool_use_id": "call_1", "transcript_path": 7}'],
|
|
270
|
+
)
|
|
271
|
+
def test_should_exit_zero_silently_on_unreadable_stdin_while_the_on_flag_is_set(
|
|
272
|
+
tmp_path: Path, stdin_text: str
|
|
273
|
+
) -> None:
|
|
274
|
+
home_directory = tmp_path / "home"
|
|
275
|
+
(home_directory / ".claude").mkdir(parents=True)
|
|
276
|
+
(home_directory / ".claude" / ".step-notes-on").write_text("", encoding="utf-8")
|
|
277
|
+
completed = subprocess.run(
|
|
278
|
+
[sys.executable, step_note_gate.__file__],
|
|
279
|
+
input=stdin_text,
|
|
280
|
+
capture_output=True,
|
|
281
|
+
text=True,
|
|
282
|
+
env={**os.environ, "HOME": str(home_directory), "USERPROFILE": str(home_directory)},
|
|
283
|
+
timeout=30,
|
|
284
|
+
check=False,
|
|
285
|
+
)
|
|
286
|
+
|
|
287
|
+
assert (completed.returncode, completed.stdout, completed.stderr) == (0, "", "")
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
"""Behavior tests for the task subject length gate."""
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import subprocess
|
|
5
|
+
import sys
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
import pytest
|
|
9
|
+
|
|
10
|
+
_BLOCKING_DIR = Path(__file__).resolve().parent
|
|
11
|
+
_HOOKS_ROOT = _BLOCKING_DIR.parent
|
|
12
|
+
for each_sys_path_entry in (str(_BLOCKING_DIR), str(_HOOKS_ROOT)):
|
|
13
|
+
if each_sys_path_entry not in sys.path:
|
|
14
|
+
sys.path.insert(0, each_sys_path_entry)
|
|
15
|
+
|
|
16
|
+
import task_subject_length_gate
|
|
17
|
+
|
|
18
|
+
HOOK_SCRIPT = _BLOCKING_DIR / "task_subject_length_gate.py"
|
|
19
|
+
HOOKS_CONFIG = _HOOKS_ROOT / "hooks.json"
|
|
20
|
+
SUBJECT_LIMIT = 80
|
|
21
|
+
|
|
22
|
+
LONG_STATUS_SUBJECT = (
|
|
23
|
+
"Open the automation scripts pull request for the turn review quick eval tool after the"
|
|
24
|
+
" third pre-PR passes run finishes and the separate checker confirms the pick rule repair"
|
|
25
|
+
" on fifteen named cases and twenty thousand random ones"
|
|
26
|
+
)
|
|
27
|
+
ALL_SHORT_SUBJECTS = [
|
|
28
|
+
"Me: open the automation scripts pull request (last check running)",
|
|
29
|
+
"You: one live try on your PC, then claude-dev-env pull request 2345 can merge",
|
|
30
|
+
"Handed off: three end-of-reply checks left to build (card is ready)",
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _run_hook_text(stdin_text: str) -> subprocess.CompletedProcess[str]:
|
|
35
|
+
return subprocess.run(
|
|
36
|
+
[sys.executable, str(HOOK_SCRIPT)],
|
|
37
|
+
input=stdin_text,
|
|
38
|
+
capture_output=True,
|
|
39
|
+
text=True,
|
|
40
|
+
check=False,
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _run_hook(tool_name: str, tool_input: object) -> subprocess.CompletedProcess[str]:
|
|
45
|
+
return _run_hook_text(
|
|
46
|
+
json.dumps(
|
|
47
|
+
{"hook_event_name": "PreToolUse", "tool_name": tool_name, "tool_input": tool_input}
|
|
48
|
+
)
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _deny_reason(completed: subprocess.CompletedProcess[str]) -> str:
|
|
53
|
+
assert completed.returncode == 0
|
|
54
|
+
specific_output = json.loads(completed.stdout)["hookSpecificOutput"]
|
|
55
|
+
assert specific_output["hookEventName"] == "PreToolUse"
|
|
56
|
+
assert specific_output["permissionDecision"] == "deny"
|
|
57
|
+
return specific_output["permissionDecisionReason"]
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _assert_silent_pass(completed: subprocess.CompletedProcess[str]) -> None:
|
|
61
|
+
assert completed.returncode == 0
|
|
62
|
+
assert completed.stdout == ""
|
|
63
|
+
assert completed.stderr == ""
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def test_should_deny_a_task_create_subject_that_carries_status_detail() -> None:
|
|
67
|
+
reason = _deny_reason(_run_hook("TaskCreate", {"subject": LONG_STATUS_SUBJECT}))
|
|
68
|
+
|
|
69
|
+
assert f"{len(LONG_STATUS_SUBJECT)} characters" in reason
|
|
70
|
+
assert str(SUBJECT_LIMIT) in reason
|
|
71
|
+
for each_actor in ("Me", "You", "Handed off"):
|
|
72
|
+
assert each_actor in reason
|
|
73
|
+
assert "description" in reason
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
@pytest.mark.parametrize("subject", ALL_SHORT_SUBJECTS)
|
|
77
|
+
def test_should_pass_a_short_subject_that_starts_with_who_acts(subject: str) -> None:
|
|
78
|
+
_assert_silent_pass(_run_hook("TaskCreate", {"subject": subject, "description": "x" * 900}))
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def test_should_pass_a_subject_at_the_limit() -> None:
|
|
82
|
+
_assert_silent_pass(_run_hook("TaskCreate", {"subject": "Me: " + "x" * (SUBJECT_LIMIT - 4)}))
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def test_should_deny_a_subject_one_character_over_the_limit() -> None:
|
|
86
|
+
reason = _deny_reason(_run_hook("TaskCreate", {"subject": "Me: " + "x" * (SUBJECT_LIMIT - 3)}))
|
|
87
|
+
|
|
88
|
+
assert f"{SUBJECT_LIMIT + 1} characters" in reason
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def test_should_pass_a_task_update_that_only_changes_status() -> None:
|
|
92
|
+
_assert_silent_pass(_run_hook("TaskUpdate", {"taskId": "3", "status": "completed"}))
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def test_should_deny_a_task_update_that_sets_a_long_subject() -> None:
|
|
96
|
+
reason = _deny_reason(_run_hook("TaskUpdate", {"taskId": "3", "subject": LONG_STATUS_SUBJECT}))
|
|
97
|
+
|
|
98
|
+
assert f"{len(LONG_STATUS_SUBJECT)} characters" in reason
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def test_should_deny_a_todo_write_with_one_long_item_and_name_it() -> None:
|
|
102
|
+
reason = _deny_reason(
|
|
103
|
+
_run_hook(
|
|
104
|
+
"TodoWrite",
|
|
105
|
+
{
|
|
106
|
+
"todos": [
|
|
107
|
+
{"content": ALL_SHORT_SUBJECTS[0], "status": "completed"},
|
|
108
|
+
{"content": LONG_STATUS_SUBJECT, "status": "in_progress"},
|
|
109
|
+
]
|
|
110
|
+
},
|
|
111
|
+
)
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
assert LONG_STATUS_SUBJECT[:40] in reason
|
|
115
|
+
assert ALL_SHORT_SUBJECTS[0] not in reason
|
|
116
|
+
assert f"{len(LONG_STATUS_SUBJECT)} characters" in reason
|
|
117
|
+
assert "item 2 " in reason
|
|
118
|
+
for each_actor in ("Me", "You", "Handed off"):
|
|
119
|
+
assert each_actor in reason
|
|
120
|
+
assert "description" in reason
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def test_should_name_the_first_of_two_long_todo_items() -> None:
|
|
124
|
+
first_long_item = "First " + "a" * SUBJECT_LIMIT
|
|
125
|
+
second_long_item = "Second " + "b" * (SUBJECT_LIMIT + 20)
|
|
126
|
+
reason = _deny_reason(
|
|
127
|
+
_run_hook(
|
|
128
|
+
"TodoWrite",
|
|
129
|
+
{
|
|
130
|
+
"todos": [
|
|
131
|
+
{"content": first_long_item, "status": "pending"},
|
|
132
|
+
{"content": second_long_item, "status": "pending"},
|
|
133
|
+
]
|
|
134
|
+
},
|
|
135
|
+
)
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
assert "item 1 " in reason
|
|
139
|
+
assert f"{len(first_long_item)} characters" in reason
|
|
140
|
+
assert "Second" not in reason
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def test_should_pass_a_todo_item_at_the_limit() -> None:
|
|
144
|
+
_assert_silent_pass(
|
|
145
|
+
_run_hook(
|
|
146
|
+
"TodoWrite", {"todos": [{"content": "c" * SUBJECT_LIMIT, "status": "pending"}]}
|
|
147
|
+
)
|
|
148
|
+
)
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def test_should_deny_a_todo_item_one_character_over_the_limit() -> None:
|
|
152
|
+
reason = _deny_reason(
|
|
153
|
+
_run_hook(
|
|
154
|
+
"TodoWrite", {"todos": [{"content": "c" * (SUBJECT_LIMIT + 1), "status": "pending"}]}
|
|
155
|
+
)
|
|
156
|
+
)
|
|
157
|
+
|
|
158
|
+
assert f"{SUBJECT_LIMIT + 1} characters" in reason
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def test_should_pass_a_todo_write_with_short_items() -> None:
|
|
162
|
+
all_todos = [
|
|
163
|
+
{"content": each_subject, "status": "pending"} for each_subject in ALL_SHORT_SUBJECTS
|
|
164
|
+
]
|
|
165
|
+
_assert_silent_pass(_run_hook("TodoWrite", {"todos": all_todos}))
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def test_should_pass_silently_for_another_tool() -> None:
|
|
169
|
+
_assert_silent_pass(_run_hook("Write", {"subject": LONG_STATUS_SUBJECT}))
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def test_should_pass_silently_for_stdin_that_is_not_json() -> None:
|
|
173
|
+
_assert_silent_pass(_run_hook_text("not json {"))
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
@pytest.mark.parametrize("tool_input", ["subject text", ["a"], None])
|
|
177
|
+
def test_should_pass_silently_when_tool_input_is_not_an_object(tool_input: object) -> None:
|
|
178
|
+
_assert_silent_pass(_run_hook("TaskCreate", tool_input))
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
@pytest.mark.parametrize(
|
|
182
|
+
"tool_input",
|
|
183
|
+
[
|
|
184
|
+
{"subject": 42},
|
|
185
|
+
{"todos": "not a list"},
|
|
186
|
+
{"todos": ["plain string", {"content": 7}, {"status": "pending"}]},
|
|
187
|
+
],
|
|
188
|
+
)
|
|
189
|
+
def test_should_pass_silently_for_malformed_fields(tool_input: object) -> None:
|
|
190
|
+
_assert_silent_pass(
|
|
191
|
+
_run_hook("TodoWrite" if "todos" in tool_input else "TaskCreate", tool_input)
|
|
192
|
+
)
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
def test_long_subject_decision_should_deny_a_long_subject_and_pass_a_short_one() -> None:
|
|
196
|
+
decision = task_subject_length_gate.long_subject_decision(
|
|
197
|
+
{"tool_name": "TaskCreate", "tool_input": {"subject": LONG_STATUS_SUBJECT}}
|
|
198
|
+
)
|
|
199
|
+
|
|
200
|
+
assert decision is not None
|
|
201
|
+
assert decision["hookSpecificOutput"]["permissionDecision"] == "deny"
|
|
202
|
+
assert (
|
|
203
|
+
task_subject_length_gate.long_subject_decision(
|
|
204
|
+
{"tool_name": "TaskCreate", "tool_input": {"subject": ALL_SHORT_SUBJECTS[1]}}
|
|
205
|
+
)
|
|
206
|
+
is None
|
|
207
|
+
)
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
def test_should_be_registered_before_each_task_list_tool() -> None:
|
|
211
|
+
registrations = json.loads(HOOKS_CONFIG.read_text(encoding="utf-8"))
|
|
212
|
+
all_gated_tool_names: set[str] = set()
|
|
213
|
+
for each_group in registrations["hooks"]["PreToolUse"]:
|
|
214
|
+
all_commands = [each_hook.get("command", "") for each_hook in each_group["hooks"]]
|
|
215
|
+
if any(
|
|
216
|
+
"hooks/blocking/task_subject_length_gate.py" in each_command
|
|
217
|
+
for each_command in all_commands
|
|
218
|
+
):
|
|
219
|
+
all_gated_tool_names.update(each_group["matcher"].split("|"))
|
|
220
|
+
|
|
221
|
+
assert {"TaskCreate", "TaskUpdate", "TodoWrite"} <= all_gated_tool_names
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import sys
|
|
3
|
+
import tempfile
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
import pytest
|
|
7
|
+
|
|
8
|
+
from hooks_constants.turn_review_gate_constants import REVIEW_FAILURE_DIAGNOSTIC_TEMPLATE
|
|
9
|
+
from turn_review_child import (
|
|
10
|
+
_child_stdout,
|
|
11
|
+
_ChildRun,
|
|
12
|
+
_judge_command,
|
|
13
|
+
_review_command,
|
|
14
|
+
_review_environment,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
PARENT_SESSION_VARIABLES = (
|
|
18
|
+
"CLAUDE_CODE_SESSION_ID",
|
|
19
|
+
"CLAUDE_CODE_REMOTE_SESSION_ID",
|
|
20
|
+
"CLAUDE_CODE_MESSAGING_SOCKET",
|
|
21
|
+
"CLAUDE_CODE_MESSAGING_TOKEN",
|
|
22
|
+
"CLAUDE_SESSION_INGRESS_TOKEN_FILE",
|
|
23
|
+
"CLAUDE_CODE_POST_FOR_SESSION_INGRESS_V2",
|
|
24
|
+
"CLAUDE_CODE_TEE_SDK_STDOUT",
|
|
25
|
+
)
|
|
26
|
+
CHILD_REPORT_SOURCE = """import json
|
|
27
|
+
import os
|
|
28
|
+
import sys
|
|
29
|
+
|
|
30
|
+
print(json.dumps({
|
|
31
|
+
"cwd": os.getcwd(),
|
|
32
|
+
"stdin": sys.stdin.read(),
|
|
33
|
+
"marker": os.environ.get("CLAUDE_TURN_REVIEW_CHILD"),
|
|
34
|
+
"parent_session": os.environ.get("CLAUDE_CODE_SESSION_ID"),
|
|
35
|
+
}))
|
|
36
|
+
"""
|
|
37
|
+
FAILING_CHILD_SOURCE = """import sys
|
|
38
|
+
|
|
39
|
+
print("partial review")
|
|
40
|
+
sys.exit(3)
|
|
41
|
+
"""
|
|
42
|
+
SLOW_CHILD_SOURCE = """import time
|
|
43
|
+
|
|
44
|
+
time.sleep(5)
|
|
45
|
+
print("late review")
|
|
46
|
+
"""
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
@pytest.fixture(autouse=True)
|
|
50
|
+
def isolated_home(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
|
|
51
|
+
home_directory = tmp_path / "home"
|
|
52
|
+
home_directory.mkdir()
|
|
53
|
+
state_directory = tmp_path / "state"
|
|
54
|
+
state_directory.mkdir()
|
|
55
|
+
monkeypatch.setenv("HOME", str(home_directory))
|
|
56
|
+
monkeypatch.setenv("USERPROFILE", str(home_directory))
|
|
57
|
+
monkeypatch.setattr(tempfile, "tempdir", str(state_directory))
|
|
58
|
+
return home_directory
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
@pytest.fixture
|
|
62
|
+
def checkout(tmp_path: Path) -> Path:
|
|
63
|
+
repository_root = tmp_path / "app"
|
|
64
|
+
repository_root.mkdir()
|
|
65
|
+
return repository_root
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def child_run(
|
|
69
|
+
tmp_path: Path, repository_root: Path, child_source: str, prompt_text: str | None
|
|
70
|
+
) -> _ChildRun:
|
|
71
|
+
child_script = tmp_path / "child.py"
|
|
72
|
+
child_script.write_text(child_source)
|
|
73
|
+
return _ChildRun(
|
|
74
|
+
repository_root,
|
|
75
|
+
[sys.executable, str(child_script)],
|
|
76
|
+
prompt_text,
|
|
77
|
+
REVIEW_FAILURE_DIAGNOSTIC_TEMPLATE,
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def log_lines(home_directory: Path) -> list[str]:
|
|
82
|
+
log_path = home_directory / ".claude" / "logs" / "turn-review.log"
|
|
83
|
+
return log_path.read_text().splitlines() if log_path.exists() else []
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def test_should_build_the_review_command_for_the_range_with_read_only_options(
|
|
87
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
88
|
+
) -> None:
|
|
89
|
+
stub_directory = tmp_path / "bin"
|
|
90
|
+
stub_directory.mkdir()
|
|
91
|
+
stub_claude = stub_directory / "claude"
|
|
92
|
+
stub_claude.write_text("")
|
|
93
|
+
stub_claude.chmod(0o755)
|
|
94
|
+
monkeypatch.setenv("PATH", str(stub_directory))
|
|
95
|
+
review_command = _review_command("aaa111", "bbb222")
|
|
96
|
+
assert review_command[:7] == [
|
|
97
|
+
str(stub_claude),
|
|
98
|
+
"-p",
|
|
99
|
+
"/code-review low aaa111..bbb222",
|
|
100
|
+
"--model",
|
|
101
|
+
"sonnet",
|
|
102
|
+
"--effort",
|
|
103
|
+
"high",
|
|
104
|
+
]
|
|
105
|
+
assert review_command[review_command.index("--tools") + 1] == "Read,Grep,Glob,Bash"
|
|
106
|
+
assert review_command[review_command.index("--permission-mode") + 1] == "dontAsk"
|
|
107
|
+
assert json.loads(review_command[review_command.index("--settings") + 1]) == {
|
|
108
|
+
"disableAllHooks": True
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def test_should_build_the_judge_command_with_no_prompt_argument(
|
|
113
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
114
|
+
) -> None:
|
|
115
|
+
monkeypatch.setenv("PATH", str(tmp_path))
|
|
116
|
+
judge_command = _judge_command()
|
|
117
|
+
assert judge_command[:6] == ["claude", "-p", "--model", "sonnet", "--effort", "high"]
|
|
118
|
+
assert "--no-session-persistence" in judge_command
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def test_should_drop_every_parent_session_variable_from_the_child_environment(
|
|
122
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
123
|
+
) -> None:
|
|
124
|
+
for each_name in PARENT_SESSION_VARIABLES:
|
|
125
|
+
monkeypatch.setenv(each_name, "parent-value")
|
|
126
|
+
monkeypatch.setenv("TURN_REVIEW_KEPT_VARIABLE", "kept")
|
|
127
|
+
child_environment = _review_environment()
|
|
128
|
+
assert [
|
|
129
|
+
each_name for each_name in PARENT_SESSION_VARIABLES if each_name in child_environment
|
|
130
|
+
] == []
|
|
131
|
+
assert child_environment["CLAUDE_TURN_REVIEW_CHILD"] == "1"
|
|
132
|
+
assert child_environment["TURN_REVIEW_KEPT_VARIABLE"] == "kept"
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def test_should_run_the_child_in_the_checkout_with_the_prompt_on_stdin(
|
|
136
|
+
tmp_path: Path, checkout: Path, monkeypatch: pytest.MonkeyPatch
|
|
137
|
+
) -> None:
|
|
138
|
+
monkeypatch.setenv("CLAUDE_CODE_SESSION_ID", "parent-session")
|
|
139
|
+
child_output = _child_stdout(
|
|
140
|
+
child_run(tmp_path, checkout, CHILD_REPORT_SOURCE, "judge data"), 30
|
|
141
|
+
)
|
|
142
|
+
assert child_output is not None
|
|
143
|
+
assert json.loads(child_output) == {
|
|
144
|
+
"cwd": str(checkout.resolve()),
|
|
145
|
+
"stdin": "judge data",
|
|
146
|
+
"marker": "1",
|
|
147
|
+
"parent_session": None,
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def test_should_give_no_output_and_log_a_child_that_exits_with_an_error(
|
|
152
|
+
tmp_path: Path, checkout: Path, isolated_home: Path
|
|
153
|
+
) -> None:
|
|
154
|
+
assert _child_stdout(child_run(tmp_path, checkout, FAILING_CHILD_SOURCE, None), 30) is None
|
|
155
|
+
assert [each_line.split(" ", 1)[1] for each_line in log_lines(isolated_home)] == [
|
|
156
|
+
"[turn_review_gate] review of app did not finish: exit 3"
|
|
157
|
+
]
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def test_should_give_no_output_and_log_a_child_that_runs_out_of_time(
|
|
161
|
+
tmp_path: Path, checkout: Path, isolated_home: Path
|
|
162
|
+
) -> None:
|
|
163
|
+
assert _child_stdout(child_run(tmp_path, checkout, SLOW_CHILD_SOURCE, None), 0.5) is None
|
|
164
|
+
assert [each_line.split(" ", 1)[1] for each_line in log_lines(isolated_home)] == [
|
|
165
|
+
"[turn_review_gate] review of app did not finish: timed out"
|
|
166
|
+
]
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import subprocess
|
|
2
|
+
import tempfile
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
import pytest
|
|
6
|
+
|
|
7
|
+
from hooks_constants.turn_review_gate_constants import BLOCK_REASON_TEMPLATE
|
|
8
|
+
from turn_review_claim import (
|
|
9
|
+
_claim_locked_stop,
|
|
10
|
+
_is_wake_prompt,
|
|
11
|
+
_JudgeRequest,
|
|
12
|
+
_judge_requests,
|
|
13
|
+
_locked_stop_wake,
|
|
14
|
+
_prompt_baselines,
|
|
15
|
+
)
|
|
16
|
+
from turn_review_findings import JudgeVerdict
|
|
17
|
+
from turn_review_snapshot import working_tree_snapshot
|
|
18
|
+
from turn_review_state import _Finding, _load_state, _ReviewState, _save_state
|
|
19
|
+
|
|
20
|
+
SESSION_ID = "claim-session"
|
|
21
|
+
BASE_COMMIT = "a" * 40
|
|
22
|
+
TURN_COMMIT = "b" * 40
|
|
23
|
+
FINDING_TEXT = "src/service.py:3 - The diff drops the retry."
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@pytest.fixture(autouse=True)
|
|
27
|
+
def isolated_home(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
|
|
28
|
+
home_directory = tmp_path / "home"
|
|
29
|
+
home_directory.mkdir()
|
|
30
|
+
state_directory = tmp_path / "state"
|
|
31
|
+
state_directory.mkdir()
|
|
32
|
+
monkeypatch.setenv("HOME", str(home_directory))
|
|
33
|
+
monkeypatch.setenv("USERPROFILE", str(home_directory))
|
|
34
|
+
monkeypatch.setattr(tempfile, "tempdir", str(state_directory))
|
|
35
|
+
return home_directory
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def git(repository: Path, *all_arguments: str) -> str:
|
|
39
|
+
return subprocess.run(
|
|
40
|
+
["git", *all_arguments], cwd=repository, check=True, capture_output=True, text=True
|
|
41
|
+
).stdout.strip()
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@pytest.fixture
|
|
45
|
+
def repository(tmp_path: Path) -> Path:
|
|
46
|
+
repository_root = tmp_path / "app"
|
|
47
|
+
repository_root.mkdir()
|
|
48
|
+
git(repository_root, "init", "-q", "-b", "main")
|
|
49
|
+
git(repository_root, "config", "user.email", "test@example.com")
|
|
50
|
+
git(repository_root, "config", "user.name", "Test")
|
|
51
|
+
(repository_root / "service.py").write_text("call()\n")
|
|
52
|
+
git(repository_root, "add", "-A")
|
|
53
|
+
git(repository_root, "commit", "-q", "-m", "start")
|
|
54
|
+
return repository_root
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def log_text(home_directory: Path) -> str:
|
|
58
|
+
log_path = home_directory / ".claude" / "logs" / "turn-review.log"
|
|
59
|
+
return log_path.read_text() if log_path.exists() else ""
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def test_should_count_a_prompt_inside_the_wake_window_or_a_task_notification_as_the_wake() -> None:
|
|
63
|
+
woken_state = _ReviewState(last_wake_at=1000.0)
|
|
64
|
+
assert _is_wake_prompt(woken_state, "next task", 1009.9) is True
|
|
65
|
+
assert _is_wake_prompt(woken_state, "next task", 1010.0) is False
|
|
66
|
+
assert (
|
|
67
|
+
_is_wake_prompt(_ReviewState(), " <task-notification>done</task-notification>", 0.0)
|
|
68
|
+
is True
|
|
69
|
+
)
|
|
70
|
+
assert _is_wake_prompt(_ReviewState(), "next task", 0.0) is False
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def test_should_keep_the_saved_baseline_only_while_the_turn_is_open(repository: Path) -> None:
|
|
74
|
+
saved_baselines = {str(repository): BASE_COMMIT}
|
|
75
|
+
open_state = _ReviewState(all_baselines=saved_baselines, is_turn_open=True)
|
|
76
|
+
assert _prompt_baselines(open_state, [repository]) == saved_baselines
|
|
77
|
+
closed_state = _ReviewState(all_baselines=saved_baselines, is_turn_open=False)
|
|
78
|
+
fresh_commit = _prompt_baselines(closed_state, [repository])[str(repository)]
|
|
79
|
+
assert fresh_commit != BASE_COMMIT
|
|
80
|
+
assert git(repository, "rev-parse", f"{fresh_commit}^") == git(repository, "rev-parse", "HEAD")
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def test_should_judge_only_an_open_finding_in_a_reviewed_checkout(
|
|
84
|
+
repository: Path, isolated_home: Path
|
|
85
|
+
) -> None:
|
|
86
|
+
open_finding = _Finding(FINDING_TEXT, str(repository), BASE_COMMIT, TURN_COMMIT)
|
|
87
|
+
review_state = _ReviewState(
|
|
88
|
+
all_findings={
|
|
89
|
+
"TR-1": open_finding,
|
|
90
|
+
"TR-2": _Finding(FINDING_TEXT, str(repository), BASE_COMMIT, TURN_COMMIT, "dismissed"),
|
|
91
|
+
"TR-3": _Finding(FINDING_TEXT, "/elsewhere/lib", BASE_COMMIT, TURN_COMMIT),
|
|
92
|
+
}
|
|
93
|
+
)
|
|
94
|
+
all_reasons = {
|
|
95
|
+
"TR-1": "The caller retries.",
|
|
96
|
+
"TR-2": "Asked again.",
|
|
97
|
+
"TR-3": "Other checkout.",
|
|
98
|
+
"TR-9": "Unknown.",
|
|
99
|
+
}
|
|
100
|
+
assert _judge_requests(review_state, all_reasons, [repository], False) == [
|
|
101
|
+
_JudgeRequest("TR-1", open_finding, "The caller retries.")
|
|
102
|
+
]
|
|
103
|
+
assert _judge_requests(review_state, {"TR-1": "The caller retries."}, [repository], True) == []
|
|
104
|
+
logged_text = log_text(isolated_home)
|
|
105
|
+
assert "Dismiss TR-2 ignored: the finding is dismissed" in logged_text
|
|
106
|
+
assert (
|
|
107
|
+
"Dismiss TR-3 ignored: the finding is in a checkout this stop does not review"
|
|
108
|
+
in logged_text
|
|
109
|
+
)
|
|
110
|
+
assert "Dismiss TR-9 ignored: the finding is not a finding of this session" in logged_text
|
|
111
|
+
assert "Dismiss TR-1 waits: the hold cap is reached, so no judge ran" in logged_text
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def test_should_claim_the_turn_range_once_and_move_the_baseline(repository: Path) -> None:
|
|
115
|
+
base_commit = working_tree_snapshot(repository, None)
|
|
116
|
+
assert base_commit is not None
|
|
117
|
+
_save_state(
|
|
118
|
+
SESSION_ID,
|
|
119
|
+
_ReviewState(
|
|
120
|
+
all_baselines={str(repository): base_commit}, blocks_in_a_row=2, is_turn_open=True
|
|
121
|
+
),
|
|
122
|
+
)
|
|
123
|
+
(repository / "service.py").write_text("call(1)\n")
|
|
124
|
+
first_claim = _claim_locked_stop(SESSION_ID, [repository], {})
|
|
125
|
+
claimed_state = _load_state(SESSION_ID)
|
|
126
|
+
turn_commit = claimed_state.all_baselines[str(repository)]
|
|
127
|
+
assert turn_commit != base_commit
|
|
128
|
+
assert first_claim.all_turn_ranges == [(repository, base_commit, turn_commit)]
|
|
129
|
+
assert claimed_state.is_turn_open is False
|
|
130
|
+
assert claimed_state.blocks_in_a_row == 2
|
|
131
|
+
second_claim = _claim_locked_stop(SESSION_ID, [repository], {})
|
|
132
|
+
assert second_claim.all_turn_ranges == []
|
|
133
|
+
assert _load_state(SESSION_ID).blocks_in_a_row == 0
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def test_should_wake_with_the_rejections_in_id_order_and_the_new_findings(tmp_path: Path) -> None:
|
|
137
|
+
repository_root = tmp_path / "app"
|
|
138
|
+
_save_state(
|
|
139
|
+
SESSION_ID,
|
|
140
|
+
_ReviewState(
|
|
141
|
+
blocks_in_a_row=1,
|
|
142
|
+
all_findings={
|
|
143
|
+
"TR-2": _Finding(FINDING_TEXT, str(repository_root), BASE_COMMIT, TURN_COMMIT),
|
|
144
|
+
"TR-10": _Finding(FINDING_TEXT, str(repository_root), BASE_COMMIT, TURN_COMMIT),
|
|
145
|
+
},
|
|
146
|
+
),
|
|
147
|
+
)
|
|
148
|
+
all_verdicts = {
|
|
149
|
+
"TR-10": JudgeVerdict(is_approved=False, reason="Nothing retries.\n"),
|
|
150
|
+
"TR-2": JudgeVerdict(is_approved=False, reason="The diff still drops it. "),
|
|
151
|
+
}
|
|
152
|
+
wake_text = _locked_stop_wake(
|
|
153
|
+
SESSION_ID,
|
|
154
|
+
[(repository_root, BASE_COMMIT, TURN_COMMIT, ["src/service.py:9 - The update is lost."])],
|
|
155
|
+
all_verdicts,
|
|
156
|
+
)
|
|
157
|
+
assert wake_text == (
|
|
158
|
+
"Dismissal of TR-2 rejected: The diff still drops it. Fix it.\n"
|
|
159
|
+
"Dismissal of TR-10 rejected: Nothing retries. Fix it.\n\n"
|
|
160
|
+
+ BLOCK_REASON_TEMPLATE.format(
|
|
161
|
+
all_findings="app:\n[TR-11] src/service.py:9 - The update is lost."
|
|
162
|
+
)
|
|
163
|
+
)
|
|
164
|
+
saved_state = _load_state(SESSION_ID)
|
|
165
|
+
assert {each_id: each.status for each_id, each in saved_state.all_findings.items()} == {
|
|
166
|
+
"TR-2": "upheld",
|
|
167
|
+
"TR-10": "upheld",
|
|
168
|
+
"TR-11": "open",
|
|
169
|
+
}
|
|
170
|
+
assert saved_state.blocks_in_a_row == 2
|
|
171
|
+
assert saved_state.last_wake_at is not None
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def test_should_dismiss_quietly_on_an_approval_and_keep_a_capped_hold_count(
|
|
175
|
+
tmp_path: Path, isolated_home: Path
|
|
176
|
+
) -> None:
|
|
177
|
+
approval = {"TR-1": JudgeVerdict(is_approved=True, reason="The caller retries.")}
|
|
178
|
+
for each_blocks, each_expected_blocks in ((2, 0), (3, 3)):
|
|
179
|
+
_save_state(
|
|
180
|
+
SESSION_ID,
|
|
181
|
+
_ReviewState(
|
|
182
|
+
blocks_in_a_row=each_blocks,
|
|
183
|
+
all_findings={
|
|
184
|
+
"TR-1": _Finding(FINDING_TEXT, str(tmp_path), BASE_COMMIT, TURN_COMMIT)
|
|
185
|
+
},
|
|
186
|
+
),
|
|
187
|
+
)
|
|
188
|
+
assert _locked_stop_wake(SESSION_ID, [], approval) is None
|
|
189
|
+
saved_state = _load_state(SESSION_ID)
|
|
190
|
+
assert saved_state.all_findings["TR-1"].status == "dismissed"
|
|
191
|
+
assert saved_state.all_findings["TR-1"].judge_reason == "The caller retries."
|
|
192
|
+
assert saved_state.blocks_in_a_row == each_expected_blocks
|
|
193
|
+
assert saved_state.last_wake_at is None
|
|
194
|
+
assert "[turn_review_gate] TR-1 dismissed: The caller retries." in log_text(isolated_home)
|