claude-dev-env 8.116.5 → 8.117.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.
- package/bin/ever-registered-hooks.mjs +2 -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_findings.py +124 -1
- package/hooks/blocking/test_turn_review_gate.py +1312 -10
- package/hooks/blocking/test_turn_review_snapshot.py +8 -0
- package/hooks/blocking/tests/test_repository_exemption.py +11 -0
- package/hooks/blocking/turn_review_findings.py +173 -13
- package/hooks/blocking/turn_review_gate.py +878 -105
- package/hooks/blocking/turn_review_snapshot.py +5 -1
- 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 +6 -27
- 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/thread_spawn_pace_hook.py +3 -3
- package/hooks/session/background_silence_prompt.py +1 -1
- package/hooks/session/test_tracking_nudge.py +114 -2
- package/hooks/session/tracking_nudge.py +28 -18
- package/hooks/session_title_transcript.py +195 -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 +111 -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/claude_account_worker.py +4 -3
- package/scripts/policy_lint/adapter_configuration.py +12 -11
- package/scripts/policy_lint/config/constants.py +2 -2
- package/scripts/test_claude_account_worker.py +1 -0
- package/scripts/tests/test_adapter_configuration.py +10 -2
- 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
|
@@ -0,0 +1,916 @@
|
|
|
1
|
+
import io
|
|
2
|
+
import json
|
|
3
|
+
import os
|
|
4
|
+
import subprocess
|
|
5
|
+
import sys
|
|
6
|
+
import threading
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
import pytest
|
|
10
|
+
|
|
11
|
+
import question_brief_gate
|
|
12
|
+
from hooks_constants.question_brief_gate_constants import (
|
|
13
|
+
MAXIMUM_BRIEF_LIST_ITEM_COUNT,
|
|
14
|
+
MAXIMUM_BRIEF_WORD_COUNT,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
GATE_SCRIPT = Path(__file__).resolve().parent / "question_brief_gate.py"
|
|
18
|
+
PACKAGE_ROOT = Path(__file__).resolve().parent.parent.parent
|
|
19
|
+
QUESTION_ID = "toolu_question_now"
|
|
20
|
+
EARLIER_QUESTION_ID = "toolu_question_earlier"
|
|
21
|
+
SHORT_POLL_LIMIT_SECONDS = 0.3
|
|
22
|
+
LATE_WRITE_DELAY_SECONDS = 0.1
|
|
23
|
+
FIFO_RUN_TIMEOUT_SECONDS = 5
|
|
24
|
+
BASH_CALL_ID = "toolu_bash"
|
|
25
|
+
REVIEW_ITEM_COUNT = MAXIMUM_BRIEF_LIST_ITEM_COUNT + 15
|
|
26
|
+
EARLIER_TURN_ITEM_COUNT = MAXIMUM_BRIEF_LIST_ITEM_COUNT + 5
|
|
27
|
+
CURRENT_TURN_ITEM_COUNT = MAXIMUM_BRIEF_LIST_ITEM_COUNT + 1
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def prompt_entry(prompt_text: str) -> dict[str, object]:
|
|
31
|
+
return {"type": "user", "message": {"role": "user", "content": prompt_text}}
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def meta_entry(prompt_text: str) -> dict[str, object]:
|
|
35
|
+
return {"type": "user", "isMeta": True, "message": {"role": "user", "content": prompt_text}}
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def queued_entry(prompt_text: str) -> dict[str, object]:
|
|
39
|
+
return {"type": "attachment", "attachment": {"type": "queued_command", "prompt": prompt_text}}
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def text_entry(text: str, stop_reason: str = "tool_use") -> dict[str, object]:
|
|
43
|
+
return {
|
|
44
|
+
"type": "assistant",
|
|
45
|
+
"message": {
|
|
46
|
+
"id": "msg_a",
|
|
47
|
+
"role": "assistant",
|
|
48
|
+
"stop_reason": stop_reason,
|
|
49
|
+
"content": [{"type": "text", "text": text}],
|
|
50
|
+
},
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def stop_summary_entry() -> dict[str, object]:
|
|
55
|
+
return {"type": "system", "subtype": "stop_hook_summary"}
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def question_entry(tool_use_id: str) -> dict[str, object]:
|
|
59
|
+
question_input = {"questions": [{"question": "Which one?", "header": "Pick", "options": []}]}
|
|
60
|
+
return {
|
|
61
|
+
"type": "assistant",
|
|
62
|
+
"message": {
|
|
63
|
+
"id": "msg_a",
|
|
64
|
+
"role": "assistant",
|
|
65
|
+
"stop_reason": "tool_use",
|
|
66
|
+
"content": [
|
|
67
|
+
{
|
|
68
|
+
"type": "tool_use",
|
|
69
|
+
"id": tool_use_id,
|
|
70
|
+
"name": "AskUserQuestion",
|
|
71
|
+
"input": question_input,
|
|
72
|
+
}
|
|
73
|
+
],
|
|
74
|
+
},
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def result_entry(tool_use_id: object, is_error: bool) -> dict[str, object]:
|
|
79
|
+
result_block = {
|
|
80
|
+
"type": "tool_result",
|
|
81
|
+
"tool_use_id": tool_use_id,
|
|
82
|
+
"content": "answer",
|
|
83
|
+
"is_error": is_error,
|
|
84
|
+
}
|
|
85
|
+
return {"type": "user", "message": {"role": "user", "content": [result_block]}}
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def tool_call_entry(tool_use_id: str, tool_name: str) -> dict[str, object]:
|
|
89
|
+
return {
|
|
90
|
+
"type": "assistant",
|
|
91
|
+
"message": {
|
|
92
|
+
"id": "msg_b",
|
|
93
|
+
"role": "assistant",
|
|
94
|
+
"stop_reason": "tool_use",
|
|
95
|
+
"content": [
|
|
96
|
+
{
|
|
97
|
+
"type": "tool_use",
|
|
98
|
+
"id": tool_use_id,
|
|
99
|
+
"name": tool_name,
|
|
100
|
+
"input": {"command": "git status"},
|
|
101
|
+
}
|
|
102
|
+
],
|
|
103
|
+
},
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def held_stop_feedback_entry() -> dict[str, object]:
|
|
108
|
+
return meta_entry("Stop hook feedback:\nThe reply ends with a question in plain text.")
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def stop_hook_error_entry() -> dict[str, object]:
|
|
112
|
+
return {
|
|
113
|
+
"type": "attachment",
|
|
114
|
+
"attachment": {
|
|
115
|
+
"type": "hook_blocking_error",
|
|
116
|
+
"hookEvent": "Stop",
|
|
117
|
+
"hookName": "Stop",
|
|
118
|
+
"toolUseID": "toolu_stop",
|
|
119
|
+
"blockingError": {"blockingError": "The reply ends with a question in plain text."},
|
|
120
|
+
},
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def task_notification_entry() -> dict[str, object]:
|
|
125
|
+
return prompt_entry("<task-notification>\n<task-id>a1</task-id>\n</task-notification>")
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def words(count: int) -> str:
|
|
129
|
+
return " ".join(["word"] * count) + "."
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def bullets(count: int) -> str:
|
|
133
|
+
return "\n".join(f"- Item {each_index} needs a call." for each_index in range(count))
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def table(row_count: int) -> str:
|
|
137
|
+
all_rows = [f"| {each_index} | Row {each_index} |" for each_index in range(row_count)]
|
|
138
|
+
return "\n".join(["| # | What |", "|---|---|", *all_rows])
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def verdict(all_entries: list[dict[str, object]]) -> str | None:
|
|
142
|
+
brief = question_brief_gate.brief_before_question(all_entries, QUESTION_ID)
|
|
143
|
+
assert brief is not None
|
|
144
|
+
return question_brief_gate.refusal_reason(brief)
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def write_transcript(tmp_path: Path, all_entries: list[object]) -> Path:
|
|
148
|
+
transcript_path = tmp_path / "transcript.jsonl"
|
|
149
|
+
transcript_path.write_text(
|
|
150
|
+
"\n".join(json.dumps(each_entry) for each_entry in all_entries) + "\n", encoding="utf-8"
|
|
151
|
+
)
|
|
152
|
+
return transcript_path
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def question_payload(transcript_path: object, **extra_payload: object) -> dict[str, object]:
|
|
156
|
+
return {
|
|
157
|
+
"hook_event_name": "PreToolUse",
|
|
158
|
+
"tool_name": "AskUserQuestion",
|
|
159
|
+
"tool_use_id": QUESTION_ID,
|
|
160
|
+
"transcript_path": transcript_path,
|
|
161
|
+
"tool_input": {"questions": []},
|
|
162
|
+
**extra_payload,
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
def run_main(
|
|
167
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], stdin_bytes: bytes
|
|
168
|
+
) -> tuple[int, str]:
|
|
169
|
+
monkeypatch.setattr(
|
|
170
|
+
question_brief_gate, "TRANSCRIPT_POLL_LIMIT_SECONDS", SHORT_POLL_LIMIT_SECONDS
|
|
171
|
+
)
|
|
172
|
+
monkeypatch.setattr(sys, "stdin", io.TextIOWrapper(io.BytesIO(stdin_bytes), encoding="utf-8"))
|
|
173
|
+
exit_code = question_brief_gate.main()
|
|
174
|
+
return exit_code, capsys.readouterr().err
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def run_gate_on(
|
|
178
|
+
tmp_path: Path,
|
|
179
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
180
|
+
capsys: pytest.CaptureFixture[str],
|
|
181
|
+
all_entries: list[dict[str, object]],
|
|
182
|
+
) -> tuple[int, str]:
|
|
183
|
+
transcript_path = write_transcript(tmp_path, all_entries)
|
|
184
|
+
payload_bytes = json.dumps(question_payload(str(transcript_path))).encode()
|
|
185
|
+
return run_main(monkeypatch, capsys, payload_bytes)
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def item_count_refusal(item_count: int) -> str:
|
|
189
|
+
return f"runs {item_count} list or table items"
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def refuse_to_wait(seconds: float) -> None:
|
|
193
|
+
raise AssertionError(f"the gate waited {seconds} seconds on a transcript it cannot use")
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def long_brief_entries() -> list[dict[str, object]]:
|
|
197
|
+
return [
|
|
198
|
+
prompt_entry("Fix it."),
|
|
199
|
+
text_entry(table(MAXIMUM_BRIEF_LIST_ITEM_COUNT + 15)),
|
|
200
|
+
question_entry(QUESTION_ID),
|
|
201
|
+
]
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
@pytest.mark.parametrize(
|
|
205
|
+
"brief_text",
|
|
206
|
+
[
|
|
207
|
+
words(40),
|
|
208
|
+
words(MAXIMUM_BRIEF_WORD_COUNT),
|
|
209
|
+
bullets(MAXIMUM_BRIEF_LIST_ITEM_COUNT),
|
|
210
|
+
table(MAXIMUM_BRIEF_LIST_ITEM_COUNT),
|
|
211
|
+
"Two choices.\n\n- **Keep:** the gate stays.\n- **Drop:** the gate goes.",
|
|
212
|
+
"```\n" + bullets(MAXIMUM_BRIEF_LIST_ITEM_COUNT + 10) + "\n```",
|
|
213
|
+
"~~~\n" + table(MAXIMUM_BRIEF_LIST_ITEM_COUNT + 10) + "\n~~~",
|
|
214
|
+
],
|
|
215
|
+
)
|
|
216
|
+
def test_should_pass_a_short_brief(brief_text: str) -> None:
|
|
217
|
+
assert (
|
|
218
|
+
verdict([prompt_entry("Fix it."), text_entry(brief_text), question_entry(QUESTION_ID)])
|
|
219
|
+
is None
|
|
220
|
+
)
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
@pytest.mark.parametrize(
|
|
224
|
+
("brief_text", "expected_fragment"),
|
|
225
|
+
[
|
|
226
|
+
(
|
|
227
|
+
words(MAXIMUM_BRIEF_WORD_COUNT + 1),
|
|
228
|
+
f"{MAXIMUM_BRIEF_WORD_COUNT + 1} words, over the {MAXIMUM_BRIEF_WORD_COUNT}-word limit",
|
|
229
|
+
),
|
|
230
|
+
(
|
|
231
|
+
bullets(MAXIMUM_BRIEF_LIST_ITEM_COUNT + 1),
|
|
232
|
+
f"{MAXIMUM_BRIEF_LIST_ITEM_COUNT + 1} list or table items, over the {MAXIMUM_BRIEF_LIST_ITEM_COUNT}-item limit",
|
|
233
|
+
),
|
|
234
|
+
(
|
|
235
|
+
table(MAXIMUM_BRIEF_LIST_ITEM_COUNT + 1),
|
|
236
|
+
f"{MAXIMUM_BRIEF_LIST_ITEM_COUNT + 1} list or table items, over the {MAXIMUM_BRIEF_LIST_ITEM_COUNT}-item limit",
|
|
237
|
+
),
|
|
238
|
+
(
|
|
239
|
+
"\n".join(
|
|
240
|
+
f"{each_index}. Step {each_index}."
|
|
241
|
+
for each_index in range(MAXIMUM_BRIEF_LIST_ITEM_COUNT + 1)
|
|
242
|
+
),
|
|
243
|
+
f"{MAXIMUM_BRIEF_LIST_ITEM_COUNT + 1} list or table items",
|
|
244
|
+
),
|
|
245
|
+
(
|
|
246
|
+
"```\nfenced line\n```\n" + bullets(MAXIMUM_BRIEF_LIST_ITEM_COUNT + 1),
|
|
247
|
+
f"{MAXIMUM_BRIEF_LIST_ITEM_COUNT + 1} list or table items",
|
|
248
|
+
),
|
|
249
|
+
],
|
|
250
|
+
)
|
|
251
|
+
def test_should_refuse_a_long_brief_naming_the_limit_it_passed(
|
|
252
|
+
brief_text: str, expected_fragment: str
|
|
253
|
+
) -> None:
|
|
254
|
+
refusal = verdict(
|
|
255
|
+
[prompt_entry("Fix it."), text_entry(brief_text), question_entry(QUESTION_ID)]
|
|
256
|
+
)
|
|
257
|
+
assert refusal is not None
|
|
258
|
+
assert expected_fragment in refusal
|
|
259
|
+
assert "Shorten the brief" in refusal
|
|
260
|
+
assert "split the question" in refusal
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
def test_should_name_only_the_word_limit_when_only_the_words_run_over() -> None:
|
|
264
|
+
refusal = verdict(
|
|
265
|
+
[
|
|
266
|
+
prompt_entry("Fix it."),
|
|
267
|
+
text_entry(words(MAXIMUM_BRIEF_WORD_COUNT + 1)),
|
|
268
|
+
question_entry(QUESTION_ID),
|
|
269
|
+
]
|
|
270
|
+
)
|
|
271
|
+
assert refusal is not None
|
|
272
|
+
assert "-item limit" not in refusal
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
def test_should_add_up_text_blocks_across_tool_calls_in_one_turn() -> None:
|
|
276
|
+
half_limit = MAXIMUM_BRIEF_WORD_COUNT // 2
|
|
277
|
+
all_entries = [
|
|
278
|
+
prompt_entry("Fix it."),
|
|
279
|
+
text_entry(words(half_limit)),
|
|
280
|
+
result_entry("toolu_bash", False),
|
|
281
|
+
text_entry(words(half_limit + 10)),
|
|
282
|
+
question_entry(QUESTION_ID),
|
|
283
|
+
]
|
|
284
|
+
assert f"{2 * half_limit + 10} words" in (verdict(all_entries) or "")
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
@pytest.mark.parametrize(
|
|
288
|
+
"turn_start_entries",
|
|
289
|
+
[
|
|
290
|
+
[
|
|
291
|
+
stop_summary_entry(),
|
|
292
|
+
meta_entry(
|
|
293
|
+
'Another Claude session sent a message: <agent-message from="a1">Done.</agent-message>'
|
|
294
|
+
),
|
|
295
|
+
],
|
|
296
|
+
[prompt_entry("<task-notification>\n<task-id>a1</task-id>\n</task-notification>")],
|
|
297
|
+
[
|
|
298
|
+
stop_summary_entry(),
|
|
299
|
+
prompt_entry("<task-notification>\n<task-id>a1</task-id>\n</task-notification>"),
|
|
300
|
+
],
|
|
301
|
+
[
|
|
302
|
+
stop_summary_entry(),
|
|
303
|
+
queued_entry("<task-notification>\n<task-id>a1</task-id>\n</task-notification>"),
|
|
304
|
+
],
|
|
305
|
+
],
|
|
306
|
+
)
|
|
307
|
+
def test_should_never_count_text_from_an_earlier_turn(
|
|
308
|
+
turn_start_entries: list[dict[str, object]],
|
|
309
|
+
) -> None:
|
|
310
|
+
all_entries = [
|
|
311
|
+
prompt_entry("Fix it."),
|
|
312
|
+
text_entry(words(MAXIMUM_BRIEF_WORD_COUNT * 2), stop_reason="end_turn"),
|
|
313
|
+
*turn_start_entries,
|
|
314
|
+
text_entry(words(10)),
|
|
315
|
+
question_entry(QUESTION_ID),
|
|
316
|
+
]
|
|
317
|
+
assert verdict(all_entries) is None
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
@pytest.mark.parametrize(
|
|
321
|
+
"held_stop_entries",
|
|
322
|
+
[
|
|
323
|
+
[stop_summary_entry()],
|
|
324
|
+
[
|
|
325
|
+
stop_summary_entry(),
|
|
326
|
+
prompt_entry("Stop hook feedback:\nThe reply ends with a question in plain text."),
|
|
327
|
+
],
|
|
328
|
+
[],
|
|
329
|
+
],
|
|
330
|
+
)
|
|
331
|
+
def test_should_keep_counting_through_a_held_stop(
|
|
332
|
+
held_stop_entries: list[dict[str, object]],
|
|
333
|
+
) -> None:
|
|
334
|
+
all_entries = [
|
|
335
|
+
prompt_entry("Fix it."),
|
|
336
|
+
text_entry(
|
|
337
|
+
bullets(MAXIMUM_BRIEF_LIST_ITEM_COUNT + 1) + "\n\nWhich one first?",
|
|
338
|
+
stop_reason="end_turn",
|
|
339
|
+
),
|
|
340
|
+
*held_stop_entries,
|
|
341
|
+
text_entry(words(10)),
|
|
342
|
+
question_entry(QUESTION_ID),
|
|
343
|
+
]
|
|
344
|
+
assert f"{MAXIMUM_BRIEF_LIST_ITEM_COUNT + 1} list or table items" in (
|
|
345
|
+
verdict(all_entries) or ""
|
|
346
|
+
)
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
def test_should_pass_a_retry_after_a_refusal() -> None:
|
|
350
|
+
all_entries = [
|
|
351
|
+
prompt_entry("Fix it."),
|
|
352
|
+
text_entry(words(MAXIMUM_BRIEF_WORD_COUNT * 2)),
|
|
353
|
+
question_entry(EARLIER_QUESTION_ID),
|
|
354
|
+
result_entry(EARLIER_QUESTION_ID, True),
|
|
355
|
+
text_entry(words(30)),
|
|
356
|
+
question_entry(QUESTION_ID),
|
|
357
|
+
]
|
|
358
|
+
assert verdict(all_entries) is None
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
def test_should_not_charge_a_second_question_for_the_first_brief() -> None:
|
|
362
|
+
all_entries = [
|
|
363
|
+
prompt_entry("Fix it."),
|
|
364
|
+
text_entry(words(MAXIMUM_BRIEF_WORD_COUNT - 10)),
|
|
365
|
+
question_entry(EARLIER_QUESTION_ID),
|
|
366
|
+
result_entry(EARLIER_QUESTION_ID, False),
|
|
367
|
+
text_entry(words(MAXIMUM_BRIEF_WORD_COUNT - 10)),
|
|
368
|
+
question_entry(QUESTION_ID),
|
|
369
|
+
]
|
|
370
|
+
assert verdict(all_entries) is None
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
@pytest.mark.parametrize(
|
|
374
|
+
"mid_turn_entry",
|
|
375
|
+
[
|
|
376
|
+
queued_entry('<agent-message from="a1">\nDone.\n</agent-message>'),
|
|
377
|
+
queued_entry("<task-notification>\n<task-id>a1</task-id>\n</task-notification>"),
|
|
378
|
+
{
|
|
379
|
+
"type": "user",
|
|
380
|
+
"isCompactSummary": True,
|
|
381
|
+
"message": {"role": "user", "content": "This session is being continued."},
|
|
382
|
+
},
|
|
383
|
+
],
|
|
384
|
+
)
|
|
385
|
+
def test_should_keep_counting_through_a_message_queued_mid_turn_that_the_user_did_not_type(
|
|
386
|
+
mid_turn_entry: dict[str, object],
|
|
387
|
+
) -> None:
|
|
388
|
+
all_entries = [
|
|
389
|
+
prompt_entry("Fix it."),
|
|
390
|
+
text_entry(words(MAXIMUM_BRIEF_WORD_COUNT)),
|
|
391
|
+
mid_turn_entry,
|
|
392
|
+
text_entry(words(10)),
|
|
393
|
+
question_entry(QUESTION_ID),
|
|
394
|
+
]
|
|
395
|
+
assert f"{MAXIMUM_BRIEF_WORD_COUNT + 10} words" in (verdict(all_entries) or "")
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
def test_should_reset_on_a_message_the_user_queued_mid_turn() -> None:
|
|
399
|
+
all_entries = [
|
|
400
|
+
prompt_entry("Fix it."),
|
|
401
|
+
text_entry(words(MAXIMUM_BRIEF_WORD_COUNT * 2)),
|
|
402
|
+
queued_entry("Okay, go on."),
|
|
403
|
+
text_entry(words(20)),
|
|
404
|
+
question_entry(QUESTION_ID),
|
|
405
|
+
]
|
|
406
|
+
assert verdict(all_entries) is None
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
def test_should_report_no_brief_while_the_question_is_not_written_yet() -> None:
|
|
410
|
+
assert (
|
|
411
|
+
question_brief_gate.brief_before_question(
|
|
412
|
+
[prompt_entry("Fix it."), text_entry(words(400))], QUESTION_ID
|
|
413
|
+
)
|
|
414
|
+
is None
|
|
415
|
+
)
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
def test_should_refuse_a_long_brief_through_the_entry_point(
|
|
419
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
|
420
|
+
) -> None:
|
|
421
|
+
transcript_path = write_transcript(tmp_path, long_brief_entries())
|
|
422
|
+
exit_code, error_text = run_main(
|
|
423
|
+
monkeypatch, capsys, json.dumps(question_payload(str(transcript_path))).encode()
|
|
424
|
+
)
|
|
425
|
+
assert exit_code == 2
|
|
426
|
+
assert f"{MAXIMUM_BRIEF_LIST_ITEM_COUNT + 15} list or table items" in error_text
|
|
427
|
+
|
|
428
|
+
|
|
429
|
+
def test_should_refuse_a_question_written_after_the_hook_starts(
|
|
430
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
|
431
|
+
) -> None:
|
|
432
|
+
all_entries = long_brief_entries()
|
|
433
|
+
transcript_path = write_transcript(tmp_path, all_entries[:-1])
|
|
434
|
+
late_line = json.dumps(all_entries[-1]) + "\n"
|
|
435
|
+
|
|
436
|
+
def append_the_call() -> None:
|
|
437
|
+
with transcript_path.open("a", encoding="utf-8") as transcript_file:
|
|
438
|
+
transcript_file.write(late_line)
|
|
439
|
+
|
|
440
|
+
late_writer = threading.Timer(LATE_WRITE_DELAY_SECONDS, append_the_call)
|
|
441
|
+
late_writer.start()
|
|
442
|
+
exit_code, error_text = run_main(
|
|
443
|
+
monkeypatch, capsys, json.dumps(question_payload(str(transcript_path))).encode()
|
|
444
|
+
)
|
|
445
|
+
late_writer.join()
|
|
446
|
+
assert exit_code == 2
|
|
447
|
+
assert "list or table items" in error_text
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
def test_should_pass_when_the_call_never_appears(
|
|
451
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
|
452
|
+
) -> None:
|
|
453
|
+
transcript_path = write_transcript(tmp_path, long_brief_entries()[:-1])
|
|
454
|
+
exit_code, error_text = run_main(
|
|
455
|
+
monkeypatch, capsys, json.dumps(question_payload(str(transcript_path))).encode()
|
|
456
|
+
)
|
|
457
|
+
assert (exit_code, error_text) == (0, "")
|
|
458
|
+
|
|
459
|
+
|
|
460
|
+
def test_should_keep_counting_through_a_user_entry_with_no_text(
|
|
461
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
|
462
|
+
) -> None:
|
|
463
|
+
all_entries = [
|
|
464
|
+
prompt_entry("Fix it."),
|
|
465
|
+
text_entry(table(REVIEW_ITEM_COUNT)),
|
|
466
|
+
prompt_entry(" "),
|
|
467
|
+
question_entry(QUESTION_ID),
|
|
468
|
+
]
|
|
469
|
+
exit_code, error_text = run_gate_on(tmp_path, monkeypatch, capsys, all_entries)
|
|
470
|
+
assert exit_code == 2
|
|
471
|
+
assert item_count_refusal(REVIEW_ITEM_COUNT) in error_text
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
def test_should_keep_counting_through_an_attachment_that_is_not_a_queued_message(
|
|
475
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
|
476
|
+
) -> None:
|
|
477
|
+
all_entries = [
|
|
478
|
+
prompt_entry("Fix it."),
|
|
479
|
+
text_entry(table(REVIEW_ITEM_COUNT)),
|
|
480
|
+
{"type": "attachment", "attachment": {"type": "hook_context", "prompt": "Fix"}},
|
|
481
|
+
question_entry(QUESTION_ID),
|
|
482
|
+
]
|
|
483
|
+
exit_code, error_text = run_gate_on(tmp_path, monkeypatch, capsys, all_entries)
|
|
484
|
+
assert exit_code == 2
|
|
485
|
+
assert item_count_refusal(REVIEW_ITEM_COUNT) in error_text
|
|
486
|
+
|
|
487
|
+
|
|
488
|
+
def test_should_end_a_turn_at_a_stop_hook_summary(
|
|
489
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
|
490
|
+
) -> None:
|
|
491
|
+
all_entries = [
|
|
492
|
+
prompt_entry("Fix it."),
|
|
493
|
+
text_entry(table(EARLIER_TURN_ITEM_COUNT)),
|
|
494
|
+
stop_summary_entry(),
|
|
495
|
+
task_notification_entry(),
|
|
496
|
+
text_entry(table(CURRENT_TURN_ITEM_COUNT)),
|
|
497
|
+
question_entry(QUESTION_ID),
|
|
498
|
+
]
|
|
499
|
+
exit_code, error_text = run_gate_on(tmp_path, monkeypatch, capsys, all_entries)
|
|
500
|
+
assert exit_code == 2
|
|
501
|
+
assert item_count_refusal(CURRENT_TURN_ITEM_COUNT) in error_text
|
|
502
|
+
|
|
503
|
+
|
|
504
|
+
def test_should_end_a_turn_only_at_an_assistant_message(
|
|
505
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
|
506
|
+
) -> None:
|
|
507
|
+
all_entries = [
|
|
508
|
+
prompt_entry("Fix it."),
|
|
509
|
+
text_entry(table(REVIEW_ITEM_COUNT)),
|
|
510
|
+
{"type": "progress", "message": {"stop_reason": "end_turn"}},
|
|
511
|
+
task_notification_entry(),
|
|
512
|
+
question_entry(QUESTION_ID),
|
|
513
|
+
]
|
|
514
|
+
exit_code, error_text = run_gate_on(tmp_path, monkeypatch, capsys, all_entries)
|
|
515
|
+
assert exit_code == 2
|
|
516
|
+
assert item_count_refusal(REVIEW_ITEM_COUNT) in error_text
|
|
517
|
+
|
|
518
|
+
|
|
519
|
+
def test_should_keep_counting_through_the_hook_attachment_of_a_held_stop(
|
|
520
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
|
521
|
+
) -> None:
|
|
522
|
+
all_entries = [
|
|
523
|
+
prompt_entry("Fix it."),
|
|
524
|
+
text_entry(table(REVIEW_ITEM_COUNT), stop_reason="end_turn"),
|
|
525
|
+
held_stop_feedback_entry(),
|
|
526
|
+
stop_hook_error_entry(),
|
|
527
|
+
stop_summary_entry(),
|
|
528
|
+
text_entry(words(10)),
|
|
529
|
+
question_entry(QUESTION_ID),
|
|
530
|
+
]
|
|
531
|
+
exit_code, error_text = run_gate_on(tmp_path, monkeypatch, capsys, all_entries)
|
|
532
|
+
assert exit_code == 2
|
|
533
|
+
assert item_count_refusal(REVIEW_ITEM_COUNT) in error_text
|
|
534
|
+
|
|
535
|
+
|
|
536
|
+
def test_should_keep_counting_across_a_tool_call_in_a_turn_a_task_notification_started(
|
|
537
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
|
538
|
+
) -> None:
|
|
539
|
+
all_entries = [
|
|
540
|
+
prompt_entry("Fix it."),
|
|
541
|
+
text_entry(words(10), stop_reason="end_turn"),
|
|
542
|
+
stop_summary_entry(),
|
|
543
|
+
task_notification_entry(),
|
|
544
|
+
text_entry(table(REVIEW_ITEM_COUNT)),
|
|
545
|
+
tool_call_entry(BASH_CALL_ID, "Bash"),
|
|
546
|
+
result_entry(BASH_CALL_ID, False),
|
|
547
|
+
question_entry(QUESTION_ID),
|
|
548
|
+
]
|
|
549
|
+
exit_code, error_text = run_gate_on(tmp_path, monkeypatch, capsys, all_entries)
|
|
550
|
+
assert exit_code == 2
|
|
551
|
+
assert item_count_refusal(REVIEW_ITEM_COUNT) in error_text
|
|
552
|
+
|
|
553
|
+
|
|
554
|
+
def test_should_keep_counting_across_a_tool_call_after_a_held_stop(
|
|
555
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
|
556
|
+
) -> None:
|
|
557
|
+
all_entries = [
|
|
558
|
+
prompt_entry("Fix it."),
|
|
559
|
+
text_entry(table(REVIEW_ITEM_COUNT), stop_reason="end_turn"),
|
|
560
|
+
held_stop_feedback_entry(),
|
|
561
|
+
stop_summary_entry(),
|
|
562
|
+
tool_call_entry(BASH_CALL_ID, "Bash"),
|
|
563
|
+
result_entry(BASH_CALL_ID, False),
|
|
564
|
+
question_entry(QUESTION_ID),
|
|
565
|
+
]
|
|
566
|
+
exit_code, error_text = run_gate_on(tmp_path, monkeypatch, capsys, all_entries)
|
|
567
|
+
assert exit_code == 2
|
|
568
|
+
assert item_count_refusal(REVIEW_ITEM_COUNT) in error_text
|
|
569
|
+
|
|
570
|
+
|
|
571
|
+
def test_should_count_only_a_tool_use_block_as_a_question(
|
|
572
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
|
573
|
+
) -> None:
|
|
574
|
+
all_entries = [
|
|
575
|
+
prompt_entry("Fix it."),
|
|
576
|
+
text_entry(table(REVIEW_ITEM_COUNT)),
|
|
577
|
+
{
|
|
578
|
+
"type": "assistant",
|
|
579
|
+
"message": {
|
|
580
|
+
"content": [
|
|
581
|
+
{"type": "server_tool_use", "id": "srvtoolu_1", "name": "AskUserQuestion"}
|
|
582
|
+
]
|
|
583
|
+
},
|
|
584
|
+
},
|
|
585
|
+
result_entry("srvtoolu_1", False),
|
|
586
|
+
question_entry(QUESTION_ID),
|
|
587
|
+
]
|
|
588
|
+
exit_code, error_text = run_gate_on(tmp_path, monkeypatch, capsys, all_entries)
|
|
589
|
+
assert exit_code == 2
|
|
590
|
+
assert item_count_refusal(REVIEW_ITEM_COUNT) in error_text
|
|
591
|
+
|
|
592
|
+
|
|
593
|
+
def test_should_keep_counting_across_a_tool_call_that_is_not_a_question(
|
|
594
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
|
595
|
+
) -> None:
|
|
596
|
+
all_entries = [
|
|
597
|
+
prompt_entry("Fix it."),
|
|
598
|
+
text_entry(table(REVIEW_ITEM_COUNT)),
|
|
599
|
+
tool_call_entry(BASH_CALL_ID, "Bash"),
|
|
600
|
+
result_entry(BASH_CALL_ID, False),
|
|
601
|
+
question_entry(QUESTION_ID),
|
|
602
|
+
]
|
|
603
|
+
exit_code, error_text = run_gate_on(tmp_path, monkeypatch, capsys, all_entries)
|
|
604
|
+
assert exit_code == 2
|
|
605
|
+
assert item_count_refusal(REVIEW_ITEM_COUNT) in error_text
|
|
606
|
+
|
|
607
|
+
|
|
608
|
+
def test_should_count_only_text_blocks_in_the_brief(
|
|
609
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
|
610
|
+
) -> None:
|
|
611
|
+
all_entries = [
|
|
612
|
+
prompt_entry("Fix it."),
|
|
613
|
+
{
|
|
614
|
+
"type": "assistant",
|
|
615
|
+
"message": {"content": [{"type": "thinking", "text": table(EARLIER_TURN_ITEM_COUNT)}]},
|
|
616
|
+
},
|
|
617
|
+
text_entry(table(CURRENT_TURN_ITEM_COUNT)),
|
|
618
|
+
question_entry(QUESTION_ID),
|
|
619
|
+
]
|
|
620
|
+
exit_code, error_text = run_gate_on(tmp_path, monkeypatch, capsys, all_entries)
|
|
621
|
+
assert exit_code == 2
|
|
622
|
+
assert item_count_refusal(CURRENT_TURN_ITEM_COUNT) in error_text
|
|
623
|
+
|
|
624
|
+
|
|
625
|
+
def test_should_start_a_new_count_at_a_prompt_the_user_types_after_an_interrupt(
|
|
626
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
|
627
|
+
) -> None:
|
|
628
|
+
all_entries = [
|
|
629
|
+
prompt_entry("Fix it."),
|
|
630
|
+
text_entry(table(EARLIER_TURN_ITEM_COUNT)),
|
|
631
|
+
{
|
|
632
|
+
"type": "user",
|
|
633
|
+
"message": {
|
|
634
|
+
"role": "user",
|
|
635
|
+
"content": [{"type": "text", "text": "[Request interrupted by user]"}],
|
|
636
|
+
},
|
|
637
|
+
},
|
|
638
|
+
prompt_entry("Use the other file."),
|
|
639
|
+
text_entry(table(CURRENT_TURN_ITEM_COUNT)),
|
|
640
|
+
question_entry(QUESTION_ID),
|
|
641
|
+
]
|
|
642
|
+
exit_code, error_text = run_gate_on(tmp_path, monkeypatch, capsys, all_entries)
|
|
643
|
+
assert exit_code == 2
|
|
644
|
+
assert item_count_refusal(CURRENT_TURN_ITEM_COUNT) in error_text
|
|
645
|
+
|
|
646
|
+
|
|
647
|
+
def test_should_start_a_new_count_at_a_question_result_that_shares_an_entry_with_another_result(
|
|
648
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
|
649
|
+
) -> None:
|
|
650
|
+
parallel_calls = {
|
|
651
|
+
"type": "assistant",
|
|
652
|
+
"message": {
|
|
653
|
+
"role": "assistant",
|
|
654
|
+
"stop_reason": "tool_use",
|
|
655
|
+
"content": [
|
|
656
|
+
{"type": "tool_use", "id": EARLIER_QUESTION_ID, "name": "AskUserQuestion"},
|
|
657
|
+
{"type": "tool_use", "id": BASH_CALL_ID, "name": "Bash"},
|
|
658
|
+
],
|
|
659
|
+
},
|
|
660
|
+
}
|
|
661
|
+
parallel_results = {
|
|
662
|
+
"type": "user",
|
|
663
|
+
"message": {
|
|
664
|
+
"role": "user",
|
|
665
|
+
"content": [
|
|
666
|
+
{"type": "tool_result", "tool_use_id": EARLIER_QUESTION_ID, "content": "answer"},
|
|
667
|
+
{"type": "tool_result", "tool_use_id": BASH_CALL_ID, "content": "ok"},
|
|
668
|
+
],
|
|
669
|
+
},
|
|
670
|
+
}
|
|
671
|
+
all_entries = [
|
|
672
|
+
prompt_entry("Fix it."),
|
|
673
|
+
text_entry(table(EARLIER_TURN_ITEM_COUNT)),
|
|
674
|
+
parallel_calls,
|
|
675
|
+
parallel_results,
|
|
676
|
+
text_entry(table(CURRENT_TURN_ITEM_COUNT)),
|
|
677
|
+
question_entry(QUESTION_ID),
|
|
678
|
+
]
|
|
679
|
+
exit_code, error_text = run_gate_on(tmp_path, monkeypatch, capsys, all_entries)
|
|
680
|
+
assert exit_code == 2
|
|
681
|
+
assert item_count_refusal(CURRENT_TURN_ITEM_COUNT) in error_text
|
|
682
|
+
|
|
683
|
+
|
|
684
|
+
def test_should_keep_counting_through_a_compaction_mid_turn(
|
|
685
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
|
686
|
+
) -> None:
|
|
687
|
+
all_entries = [
|
|
688
|
+
prompt_entry("Fix it."),
|
|
689
|
+
text_entry(table(REVIEW_ITEM_COUNT)),
|
|
690
|
+
{"type": "system", "subtype": "compact_boundary", "content": "Conversation compacted"},
|
|
691
|
+
{
|
|
692
|
+
"type": "user",
|
|
693
|
+
"isCompactSummary": True,
|
|
694
|
+
"message": {"role": "user", "content": "This session is being continued."},
|
|
695
|
+
},
|
|
696
|
+
text_entry(words(10)),
|
|
697
|
+
question_entry(QUESTION_ID),
|
|
698
|
+
]
|
|
699
|
+
exit_code, error_text = run_gate_on(tmp_path, monkeypatch, capsys, all_entries)
|
|
700
|
+
assert exit_code == 2
|
|
701
|
+
assert item_count_refusal(REVIEW_ITEM_COUNT) in error_text
|
|
702
|
+
|
|
703
|
+
|
|
704
|
+
def test_should_keep_a_turn_ended_through_the_queue_entries_written_after_a_stop(
|
|
705
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
|
706
|
+
) -> None:
|
|
707
|
+
all_entries = [
|
|
708
|
+
prompt_entry("Fix it."),
|
|
709
|
+
text_entry(table(EARLIER_TURN_ITEM_COUNT), stop_reason="end_turn"),
|
|
710
|
+
stop_summary_entry(),
|
|
711
|
+
{"type": "queue-operation", "operation": "enqueue", "content": "Done."},
|
|
712
|
+
meta_entry(
|
|
713
|
+
'Another Claude session sent a message: <agent-message from="a1">Done.</agent-message>'
|
|
714
|
+
),
|
|
715
|
+
text_entry(table(CURRENT_TURN_ITEM_COUNT)),
|
|
716
|
+
question_entry(QUESTION_ID),
|
|
717
|
+
]
|
|
718
|
+
exit_code, error_text = run_gate_on(tmp_path, monkeypatch, capsys, all_entries)
|
|
719
|
+
assert exit_code == 2
|
|
720
|
+
assert item_count_refusal(CURRENT_TURN_ITEM_COUNT) in error_text
|
|
721
|
+
|
|
722
|
+
|
|
723
|
+
def test_should_not_wait_when_the_question_is_already_written(
|
|
724
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
|
725
|
+
) -> None:
|
|
726
|
+
all_requested_waits: list[float] = []
|
|
727
|
+
monkeypatch.setattr(question_brief_gate.time, "sleep", all_requested_waits.append)
|
|
728
|
+
exit_code, error_text = run_gate_on(tmp_path, monkeypatch, capsys, long_brief_entries())
|
|
729
|
+
assert exit_code == 2
|
|
730
|
+
assert item_count_refusal(REVIEW_ITEM_COUNT) in error_text
|
|
731
|
+
assert all_requested_waits == []
|
|
732
|
+
|
|
733
|
+
|
|
734
|
+
@pytest.mark.parametrize(
|
|
735
|
+
"stdin_bytes",
|
|
736
|
+
[
|
|
737
|
+
b"",
|
|
738
|
+
b"{not json",
|
|
739
|
+
b"[1, 2]",
|
|
740
|
+
b"\xff\xfe\x00 not utf-8",
|
|
741
|
+
b"[" * 100_000,
|
|
742
|
+
b'{"tool_name": "AskUserQuestion", "tool_use_id": ' + b"[" * 100_000,
|
|
743
|
+
b'{"tool_name": "AskUserQuestion", "tool_use_id": ' + b"7" * 6000 + b"}",
|
|
744
|
+
],
|
|
745
|
+
)
|
|
746
|
+
def test_should_pass_on_unreadable_stdin(
|
|
747
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], stdin_bytes: bytes
|
|
748
|
+
) -> None:
|
|
749
|
+
assert run_main(monkeypatch, capsys, stdin_bytes) == (0, "")
|
|
750
|
+
|
|
751
|
+
|
|
752
|
+
@pytest.mark.parametrize(
|
|
753
|
+
"transcript_path",
|
|
754
|
+
[
|
|
755
|
+
7,
|
|
756
|
+
["a", "b"],
|
|
757
|
+
"has\x00nul.jsonl",
|
|
758
|
+
"n" * 5000,
|
|
759
|
+
"",
|
|
760
|
+
None,
|
|
761
|
+
],
|
|
762
|
+
)
|
|
763
|
+
def test_should_pass_a_transcript_path_that_is_not_a_usable_path(
|
|
764
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], transcript_path: object
|
|
765
|
+
) -> None:
|
|
766
|
+
payload_bytes = json.dumps(question_payload(transcript_path)).encode()
|
|
767
|
+
assert run_main(monkeypatch, capsys, payload_bytes) == (0, "")
|
|
768
|
+
|
|
769
|
+
|
|
770
|
+
@pytest.mark.parametrize(
|
|
771
|
+
"tool_use_id",
|
|
772
|
+
[7, ["a"], None, ""],
|
|
773
|
+
)
|
|
774
|
+
def test_should_pass_a_tool_use_id_that_is_not_text(
|
|
775
|
+
tmp_path: Path,
|
|
776
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
777
|
+
capsys: pytest.CaptureFixture[str],
|
|
778
|
+
tool_use_id: object,
|
|
779
|
+
) -> None:
|
|
780
|
+
transcript_path = write_transcript(tmp_path, long_brief_entries())
|
|
781
|
+
monkeypatch.setattr(question_brief_gate.time, "sleep", refuse_to_wait)
|
|
782
|
+
payload_bytes = json.dumps(
|
|
783
|
+
question_payload(str(transcript_path), tool_use_id=tool_use_id)
|
|
784
|
+
).encode()
|
|
785
|
+
assert run_main(monkeypatch, capsys, payload_bytes) == (0, "")
|
|
786
|
+
|
|
787
|
+
|
|
788
|
+
@pytest.mark.parametrize(
|
|
789
|
+
"odd_entry",
|
|
790
|
+
[
|
|
791
|
+
{"type": "assistant", "message": "a string"},
|
|
792
|
+
{"type": "assistant", "message": ["a", "list"]},
|
|
793
|
+
{"type": "user", "message": "a string"},
|
|
794
|
+
{"type": "user", "message": 7},
|
|
795
|
+
result_entry(["a", "list"], False),
|
|
796
|
+
result_entry({"an": "object"}, False),
|
|
797
|
+
{"type": "assistant", "message": {"content": [{"type": "text", "text": 7}]}},
|
|
798
|
+
{"type": "system", "subtype": ["stop_hook_summary"]},
|
|
799
|
+
{"type": "system", "subtype": {"a": 1}},
|
|
800
|
+
{"type": "attachment", "attachment": "queued_command"},
|
|
801
|
+
{"type": "attachment", "attachment": {"type": "queued_command", "prompt": ["Fix it."]}},
|
|
802
|
+
{
|
|
803
|
+
"type": "assistant",
|
|
804
|
+
"message": {
|
|
805
|
+
"content": [{"type": "tool_use", "id": ["a", "list"], "name": "AskUserQuestion"}]
|
|
806
|
+
},
|
|
807
|
+
},
|
|
808
|
+
],
|
|
809
|
+
)
|
|
810
|
+
def test_should_never_crash_on_an_odd_transcript_entry(
|
|
811
|
+
tmp_path: Path,
|
|
812
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
813
|
+
capsys: pytest.CaptureFixture[str],
|
|
814
|
+
odd_entry: dict[str, object],
|
|
815
|
+
) -> None:
|
|
816
|
+
all_entries = [
|
|
817
|
+
prompt_entry("Fix it."),
|
|
818
|
+
odd_entry,
|
|
819
|
+
text_entry(words(10)),
|
|
820
|
+
question_entry(QUESTION_ID),
|
|
821
|
+
]
|
|
822
|
+
transcript_path = write_transcript(tmp_path, all_entries)
|
|
823
|
+
payload_bytes = json.dumps(question_payload(str(transcript_path))).encode()
|
|
824
|
+
assert run_main(monkeypatch, capsys, payload_bytes) == (0, "")
|
|
825
|
+
|
|
826
|
+
|
|
827
|
+
def test_should_never_crash_on_a_deeply_nested_transcript_line(
|
|
828
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
|
829
|
+
) -> None:
|
|
830
|
+
transcript_path = write_transcript(tmp_path, long_brief_entries())
|
|
831
|
+
with transcript_path.open("a", encoding="utf-8") as transcript_file:
|
|
832
|
+
transcript_file.write("[" * 100_000 + "\n")
|
|
833
|
+
payload_bytes = json.dumps(question_payload(str(transcript_path))).encode()
|
|
834
|
+
assert run_main(monkeypatch, capsys, payload_bytes) == (0, "")
|
|
835
|
+
|
|
836
|
+
|
|
837
|
+
@pytest.mark.parametrize("missing_kind", ["absent", "directory"])
|
|
838
|
+
def test_should_return_at_once_for_a_transcript_it_cannot_read(
|
|
839
|
+
tmp_path: Path,
|
|
840
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
841
|
+
capsys: pytest.CaptureFixture[str],
|
|
842
|
+
missing_kind: str,
|
|
843
|
+
) -> None:
|
|
844
|
+
unreadable_path = tmp_path / "transcript.jsonl"
|
|
845
|
+
if missing_kind == "directory":
|
|
846
|
+
unreadable_path.mkdir()
|
|
847
|
+
monkeypatch.setattr(question_brief_gate.time, "sleep", refuse_to_wait)
|
|
848
|
+
payload_bytes = json.dumps(question_payload(str(unreadable_path))).encode()
|
|
849
|
+
assert run_main(monkeypatch, capsys, payload_bytes) == (0, "")
|
|
850
|
+
|
|
851
|
+
|
|
852
|
+
def test_should_return_at_once_for_a_transcript_it_may_not_open(
|
|
853
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
|
854
|
+
) -> None:
|
|
855
|
+
transcript_path = write_transcript(tmp_path, long_brief_entries())
|
|
856
|
+
|
|
857
|
+
def refuse_to_open(*_arguments: object, **_keywords: object) -> None:
|
|
858
|
+
raise PermissionError("permission denied")
|
|
859
|
+
|
|
860
|
+
monkeypatch.setattr(Path, "open", refuse_to_open)
|
|
861
|
+
monkeypatch.setattr(question_brief_gate.time, "sleep", refuse_to_wait)
|
|
862
|
+
payload_bytes = json.dumps(question_payload(str(transcript_path))).encode()
|
|
863
|
+
assert run_main(monkeypatch, capsys, payload_bytes) == (0, "")
|
|
864
|
+
|
|
865
|
+
|
|
866
|
+
def test_should_pass_at_once_when_the_transcript_path_is_a_named_pipe(tmp_path: Path) -> None:
|
|
867
|
+
pipe_path = tmp_path / "transcript.jsonl"
|
|
868
|
+
os.mkfifo(pipe_path)
|
|
869
|
+
completed = subprocess.run(
|
|
870
|
+
[sys.executable, str(GATE_SCRIPT)],
|
|
871
|
+
input=json.dumps(question_payload(str(pipe_path))),
|
|
872
|
+
capture_output=True,
|
|
873
|
+
text=True,
|
|
874
|
+
check=False,
|
|
875
|
+
timeout=FIFO_RUN_TIMEOUT_SECONDS,
|
|
876
|
+
)
|
|
877
|
+
assert (completed.returncode, completed.stderr) == (0, "")
|
|
878
|
+
|
|
879
|
+
|
|
880
|
+
def test_should_exit_two_from_the_script_for_a_long_brief(tmp_path: Path) -> None:
|
|
881
|
+
transcript_path = write_transcript(tmp_path, long_brief_entries())
|
|
882
|
+
completed = subprocess.run(
|
|
883
|
+
[sys.executable, str(GATE_SCRIPT)],
|
|
884
|
+
input=json.dumps(question_payload(str(transcript_path))),
|
|
885
|
+
capture_output=True,
|
|
886
|
+
text=True,
|
|
887
|
+
check=False,
|
|
888
|
+
)
|
|
889
|
+
assert completed.returncode == 2
|
|
890
|
+
assert "over the" in completed.stderr
|
|
891
|
+
|
|
892
|
+
|
|
893
|
+
def test_should_exit_zero_for_a_subagent_question(
|
|
894
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
|
895
|
+
) -> None:
|
|
896
|
+
transcript_path = write_transcript(tmp_path, long_brief_entries())
|
|
897
|
+
payload_bytes = json.dumps(question_payload(str(transcript_path), agent_id="a1")).encode()
|
|
898
|
+
assert run_main(monkeypatch, capsys, payload_bytes) == (0, "")
|
|
899
|
+
|
|
900
|
+
|
|
901
|
+
def test_should_exit_zero_for_another_tool(
|
|
902
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
|
903
|
+
) -> None:
|
|
904
|
+
transcript_path = write_transcript(tmp_path, long_brief_entries())
|
|
905
|
+
payload_bytes = json.dumps(question_payload(str(transcript_path), tool_name="Bash")).encode()
|
|
906
|
+
assert run_main(monkeypatch, capsys, payload_bytes) == (0, "")
|
|
907
|
+
|
|
908
|
+
|
|
909
|
+
@pytest.mark.parametrize(
|
|
910
|
+
"rule_path",
|
|
911
|
+
["rules/question-presentation.md", "docs/rule-guides/question-presentation.md"],
|
|
912
|
+
)
|
|
913
|
+
def test_should_state_the_limits_the_gate_enforces_in_each_rule_file(rule_path: str) -> None:
|
|
914
|
+
rule_text = (PACKAGE_ROOT / rule_path).read_text(encoding="utf-8")
|
|
915
|
+
assert f"{MAXIMUM_BRIEF_WORD_COUNT} words" in rule_text
|
|
916
|
+
assert f"{MAXIMUM_BRIEF_LIST_ITEM_COUNT} list" in rule_text
|