claude-dev-env 8.122.0 → 8.123.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/hooks/blocking/closing_question_round_gate.py +387 -0
- package/hooks/blocking/closing_question_round_lines.py +109 -0
- package/hooks/blocking/closing_question_round_open_items.py +348 -0
- package/hooks/blocking/closing_question_round_transcript.py +344 -0
- package/hooks/blocking/test_closing_question_round_gate.py +983 -0
- package/hooks/blocking/test_closing_question_round_gate_declaration.py +307 -0
- package/hooks/blocking/test_closing_question_round_gate_work.py +349 -0
- package/hooks/blocking/test_closing_question_round_lines.py +70 -0
- package/hooks/blocking/test_closing_question_round_open_items.py +238 -0
- package/hooks/blocking/test_closing_question_round_transcript.py +236 -0
- package/hooks/hooks_constants/closing_question_round_gate_constants.py +167 -0
- package/hooks/hooks_constants/test_closing_question_round_gate_constants.py +92 -0
- package/package.json +1 -1
|
@@ -0,0 +1,983 @@
|
|
|
1
|
+
import io
|
|
2
|
+
import json
|
|
3
|
+
import os
|
|
4
|
+
import subprocess
|
|
5
|
+
import sys
|
|
6
|
+
import time
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
import pytest
|
|
10
|
+
|
|
11
|
+
import closing_question_round_gate
|
|
12
|
+
from hooks_constants.closing_question_round_gate_constants import (
|
|
13
|
+
CLOSING_ROUND_REASON_LEAD,
|
|
14
|
+
MAXIMUM_QUOTED_LINE_COUNT,
|
|
15
|
+
MAXIMUM_QUOTED_LINE_LENGTH,
|
|
16
|
+
WORK_REASON_LEAD,
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
GATE_PATH = Path(closing_question_round_gate.__file__)
|
|
20
|
+
SESSION_ID = "s-123"
|
|
21
|
+
DEFERRAL_LINE = "I deferred the backoff setting to a later change."
|
|
22
|
+
FEEDBACK_TEXT = f'Stop hook feedback:\n{CLOSING_ROUND_REASON_LEAD}\nThe lines:\n"{DEFERRAL_LINE}"'
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@pytest.fixture(autouse=True)
|
|
26
|
+
def interactive_session(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
|
|
27
|
+
monkeypatch.setenv("HOME", str(tmp_path))
|
|
28
|
+
monkeypatch.setenv("USERPROFILE", str(tmp_path))
|
|
29
|
+
monkeypatch.setenv("CLAUDE_CODE_SESSION_ATTENDED", "1")
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def typed_prompt_record(
|
|
33
|
+
prompt_text: str = "Add the retry flag to the export job.",
|
|
34
|
+
) -> dict[str, object]:
|
|
35
|
+
return {
|
|
36
|
+
"type": "user",
|
|
37
|
+
"turnOrigin": "human",
|
|
38
|
+
"promptSource": "typed",
|
|
39
|
+
"message": {"role": "user", "content": prompt_text},
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def machine_prompt_record(
|
|
44
|
+
turn_origin: str, prompt_text: str = "Add the retry flag."
|
|
45
|
+
) -> dict[str, object]:
|
|
46
|
+
return {
|
|
47
|
+
"type": "user",
|
|
48
|
+
"turnOrigin": turn_origin,
|
|
49
|
+
"promptSource": "system",
|
|
50
|
+
"message": {"role": "user", "content": prompt_text},
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def reply_record(reply_text: str) -> dict[str, object]:
|
|
55
|
+
return {
|
|
56
|
+
"type": "assistant",
|
|
57
|
+
"message": {"role": "assistant", "content": [{"type": "text", "text": reply_text}]},
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def tool_call_record(
|
|
62
|
+
tool_name: object,
|
|
63
|
+
all_input_fields: dict[str, object] | None = None,
|
|
64
|
+
tool_use_id: str | None = None,
|
|
65
|
+
) -> dict[str, object]:
|
|
66
|
+
tool_use = {
|
|
67
|
+
"type": "tool_use",
|
|
68
|
+
"id": tool_use_id or f"id-{tool_name}",
|
|
69
|
+
"name": tool_name,
|
|
70
|
+
"input": all_input_fields or {},
|
|
71
|
+
}
|
|
72
|
+
return {"type": "assistant", "message": {"role": "assistant", "content": [tool_use]}}
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def tool_result_record(
|
|
76
|
+
tool_name: str, result_text: str, is_error: bool = False, tool_use_id: str | None = None
|
|
77
|
+
) -> dict[str, object]:
|
|
78
|
+
tool_result = {
|
|
79
|
+
"type": "tool_result",
|
|
80
|
+
"tool_use_id": tool_use_id or f"id-{tool_name}",
|
|
81
|
+
"content": result_text,
|
|
82
|
+
"is_error": is_error,
|
|
83
|
+
}
|
|
84
|
+
return {"type": "user", "message": {"role": "user", "content": [tool_result]}}
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def stop_feedback_record(feedback_text: str = FEEDBACK_TEXT) -> dict[str, object]:
|
|
88
|
+
return {"type": "user", "isMeta": True, "message": {"role": "user", "content": feedback_text}}
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def write_records(transcript_path: Path, all_records: list[dict[str, object]]) -> Path:
|
|
92
|
+
transcript_path.write_text(
|
|
93
|
+
"\n".join(json.dumps(each_record) for each_record in all_records), encoding="utf-8"
|
|
94
|
+
)
|
|
95
|
+
return transcript_path
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def run_gate(
|
|
99
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
100
|
+
capsys: pytest.CaptureFixture[str],
|
|
101
|
+
transcript_path: Path | None,
|
|
102
|
+
all_extra_fields: dict[str, object] | None = None,
|
|
103
|
+
) -> dict[str, str]:
|
|
104
|
+
hook_input: dict[str, object] = {
|
|
105
|
+
"hook_event_name": "Stop",
|
|
106
|
+
"session_id": SESSION_ID,
|
|
107
|
+
"stop_hook_active": False,
|
|
108
|
+
**(all_extra_fields or {}),
|
|
109
|
+
}
|
|
110
|
+
if transcript_path is not None:
|
|
111
|
+
hook_input["transcript_path"] = str(transcript_path)
|
|
112
|
+
monkeypatch.setattr(
|
|
113
|
+
"sys.stdin", io.TextIOWrapper(io.BytesIO(json.dumps(hook_input).encode("utf-8")))
|
|
114
|
+
)
|
|
115
|
+
exit_code = closing_question_round_gate.main()
|
|
116
|
+
captured = capsys.readouterr()
|
|
117
|
+
block_output = json.loads(captured.out) if captured.out else {}
|
|
118
|
+
assert exit_code == (2 if block_output else 0)
|
|
119
|
+
assert captured.err == block_output.get("reason", "")
|
|
120
|
+
return block_output
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def held_reason(
|
|
124
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
125
|
+
capsys: pytest.CaptureFixture[str],
|
|
126
|
+
tmp_path: Path,
|
|
127
|
+
all_records: list[dict[str, object]],
|
|
128
|
+
all_extra_fields: dict[str, object] | None = None,
|
|
129
|
+
) -> str | None:
|
|
130
|
+
block_output = run_gate(
|
|
131
|
+
monkeypatch, capsys, write_records(tmp_path / "s.jsonl", all_records), all_extra_fields
|
|
132
|
+
)
|
|
133
|
+
return block_output.get("reason")
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def test_should_hold_a_typed_session_that_wrote_a_deferral_and_sent_no_card(
|
|
137
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
138
|
+
) -> None:
|
|
139
|
+
transcript_path = write_records(
|
|
140
|
+
tmp_path / "s.jsonl",
|
|
141
|
+
[typed_prompt_record(), reply_record(f"The flag is in place.\n\n{DEFERRAL_LINE}")],
|
|
142
|
+
)
|
|
143
|
+
block_output = run_gate(monkeypatch, capsys, transcript_path)
|
|
144
|
+
assert block_output["decision"] == "block"
|
|
145
|
+
assert block_output["reason"].startswith(CLOSING_ROUND_REASON_LEAD)
|
|
146
|
+
assert f'"{DEFERRAL_LINE}"' in block_output["reason"]
|
|
147
|
+
assert "The flag is in place." not in block_output["reason"]
|
|
148
|
+
assert "AskUserQuestion" in block_output["reason"]
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def test_should_log_a_hold_and_no_allow(
|
|
152
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
153
|
+
) -> None:
|
|
154
|
+
log_path = tmp_path / ".claude" / "logs" / "hook-blocks.log"
|
|
155
|
+
assert (
|
|
156
|
+
held_reason(
|
|
157
|
+
monkeypatch, capsys, tmp_path, [typed_prompt_record(), reply_record("Both tests pass.")]
|
|
158
|
+
)
|
|
159
|
+
is None
|
|
160
|
+
)
|
|
161
|
+
assert not log_path.exists()
|
|
162
|
+
assert held_reason(
|
|
163
|
+
monkeypatch, capsys, tmp_path, [typed_prompt_record(), reply_record(DEFERRAL_LINE)]
|
|
164
|
+
)
|
|
165
|
+
assert (
|
|
166
|
+
json.loads(log_path.read_text(encoding="utf-8"))["hook"] == "closing_question_round_gate.py"
|
|
167
|
+
)
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def test_should_allow_once_a_question_card_follows_the_open_item_line(
|
|
171
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
172
|
+
) -> None:
|
|
173
|
+
all_records = [
|
|
174
|
+
typed_prompt_record(),
|
|
175
|
+
reply_record(DEFERRAL_LINE),
|
|
176
|
+
tool_call_record("AskUserQuestion"),
|
|
177
|
+
tool_result_record("AskUserQuestion", "The user chose Leave it."),
|
|
178
|
+
reply_record("The backoff setting stays as it is."),
|
|
179
|
+
]
|
|
180
|
+
assert held_reason(monkeypatch, capsys, tmp_path, all_records) is None
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def test_should_hold_for_an_open_item_line_written_after_the_cards(
|
|
184
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
185
|
+
) -> None:
|
|
186
|
+
all_records = [
|
|
187
|
+
typed_prompt_record(),
|
|
188
|
+
reply_record(DEFERRAL_LINE),
|
|
189
|
+
tool_call_record("AskUserQuestion"),
|
|
190
|
+
tool_result_record("AskUserQuestion", "The user chose Add it now."),
|
|
191
|
+
reply_record("The jitter option is out of scope for this change."),
|
|
192
|
+
]
|
|
193
|
+
reason = held_reason(monkeypatch, capsys, tmp_path, all_records)
|
|
194
|
+
assert reason is not None
|
|
195
|
+
assert "jitter option" in reason
|
|
196
|
+
assert DEFERRAL_LINE not in reason
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
@pytest.mark.parametrize(
|
|
200
|
+
"all_question_records",
|
|
201
|
+
[
|
|
202
|
+
[
|
|
203
|
+
tool_call_record("AskUserQuestion", tool_use_id="id-refused"),
|
|
204
|
+
tool_result_record(
|
|
205
|
+
"AskUserQuestion",
|
|
206
|
+
"Question refused: the chat text runs 600 words.",
|
|
207
|
+
is_error=True,
|
|
208
|
+
tool_use_id="id-refused",
|
|
209
|
+
),
|
|
210
|
+
],
|
|
211
|
+
[
|
|
212
|
+
tool_call_record("AskUserQuestion", tool_use_id="id-errored"),
|
|
213
|
+
tool_result_record(
|
|
214
|
+
"AskUserQuestion",
|
|
215
|
+
"InputValidationError: questions is required.",
|
|
216
|
+
is_error=True,
|
|
217
|
+
tool_use_id="id-errored",
|
|
218
|
+
),
|
|
219
|
+
],
|
|
220
|
+
[tool_call_record("AskUserQuestion", tool_use_id="id-unanswered")],
|
|
221
|
+
[
|
|
222
|
+
tool_call_record("AskUserQuestion", tool_use_id="id-asked"),
|
|
223
|
+
tool_result_record("Bash", "ok", tool_use_id="id-other"),
|
|
224
|
+
],
|
|
225
|
+
],
|
|
226
|
+
)
|
|
227
|
+
def test_should_hold_when_the_question_call_did_not_reach_the_user(
|
|
228
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
229
|
+
capsys: pytest.CaptureFixture[str],
|
|
230
|
+
tmp_path: Path,
|
|
231
|
+
all_question_records: list[dict[str, object]],
|
|
232
|
+
) -> None:
|
|
233
|
+
all_records = [typed_prompt_record(), reply_record(DEFERRAL_LINE), *all_question_records]
|
|
234
|
+
assert f'"{DEFERRAL_LINE}"' in str(held_reason(monkeypatch, capsys, tmp_path, all_records))
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
def test_should_start_the_stretch_after_the_last_call_that_reached_the_user(
|
|
238
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
239
|
+
) -> None:
|
|
240
|
+
all_records = [
|
|
241
|
+
typed_prompt_record(),
|
|
242
|
+
reply_record(DEFERRAL_LINE),
|
|
243
|
+
tool_call_record("AskUserQuestion", tool_use_id="id-first"),
|
|
244
|
+
tool_result_record("AskUserQuestion", "The user chose Leave it.", tool_use_id="id-first"),
|
|
245
|
+
reply_record("The jitter option is parked."),
|
|
246
|
+
tool_call_record("AskUserQuestion", tool_use_id="id-second"),
|
|
247
|
+
tool_result_record(
|
|
248
|
+
"AskUserQuestion", "Question refused.", is_error=True, tool_use_id="id-second"
|
|
249
|
+
),
|
|
250
|
+
]
|
|
251
|
+
reason = held_reason(monkeypatch, capsys, tmp_path, all_records)
|
|
252
|
+
assert '"The jitter option is parked."' in str(reason)
|
|
253
|
+
assert DEFERRAL_LINE not in str(reason)
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
@pytest.mark.parametrize(
|
|
257
|
+
"open_line",
|
|
258
|
+
[
|
|
259
|
+
"This defers the cleanup.",
|
|
260
|
+
"I will send the cleanup as a follow-up.",
|
|
261
|
+
"The cleanup goes in a follow up PR.",
|
|
262
|
+
"The cleanup is parked.",
|
|
263
|
+
"The cleanup is not covered here.",
|
|
264
|
+
"The cleanup is left for later.",
|
|
265
|
+
"The cleanup goes in a later pull request.",
|
|
266
|
+
"One known gap is the cleanup.",
|
|
267
|
+
"That is a known limitation.",
|
|
268
|
+
"One open question is the cache size.",
|
|
269
|
+
"The cache size is unresolved.",
|
|
270
|
+
"The cache size is uncertain.",
|
|
271
|
+
"I am not sure the cache is warm.",
|
|
272
|
+
"I am unsure about the cache.",
|
|
273
|
+
"It is unclear which cache wins.",
|
|
274
|
+
"I assumed the cache holds 64 entries.",
|
|
275
|
+
"I worked on, assuming the cache holds 64 entries.",
|
|
276
|
+
"The cache size is an assumption.",
|
|
277
|
+
"The cache size is unverified.",
|
|
278
|
+
"I could not verify the cache size.",
|
|
279
|
+
"I couldn't verify the cache size.",
|
|
280
|
+
"I couldn\u2019t verify the cache size.",
|
|
281
|
+
"I did not verify the cache size.",
|
|
282
|
+
"The cache path is untested.",
|
|
283
|
+
"The cache path is not tested.",
|
|
284
|
+
"The cache size was a judgment call.",
|
|
285
|
+
"The cache size is your call.",
|
|
286
|
+
"The cache size is up to you.",
|
|
287
|
+
"The cache size needs your decision.",
|
|
288
|
+
"THE CACHE SIZE IS DEFERRED.",
|
|
289
|
+
"No follow-up is needed except the retry fix.",
|
|
290
|
+
"Nothing deferred except the backoff setting.",
|
|
291
|
+
"No follow-ups except the retry fix.",
|
|
292
|
+
"Nothing is deferred but the cache size.",
|
|
293
|
+
"Nothing deferred other than the cache size.",
|
|
294
|
+
"Nothing deferred apart from the cache size.",
|
|
295
|
+
"Nothing deferred besides the cache size.",
|
|
296
|
+
"Nothing deferred aside from the cache size.",
|
|
297
|
+
"The cache warm-up is pending.",
|
|
298
|
+
"TODO: warm the cache.",
|
|
299
|
+
"The cache size is TBD.",
|
|
300
|
+
"I skipped the cache warm-up.",
|
|
301
|
+
"I am skipping the cache warm-up.",
|
|
302
|
+
"The cache warm-up is not done.",
|
|
303
|
+
"The cache warm-up is blocked.",
|
|
304
|
+
"The cache suite did not run.",
|
|
305
|
+
"The cache suite didn't run.",
|
|
306
|
+
"The cache suite was not run.",
|
|
307
|
+
"I could not reproduce the cache miss.",
|
|
308
|
+
"I couldn't reproduce the cache miss.",
|
|
309
|
+
"One deferral is the cache size.",
|
|
310
|
+
"Two deferrals remain.",
|
|
311
|
+
"One uncertainty is the cache size.",
|
|
312
|
+
"The cache size needs your input.",
|
|
313
|
+
"The cache size and the TTL need your input.",
|
|
314
|
+
"I need you to pick the cache size.",
|
|
315
|
+
"We assumed the cache holds 64 entries.",
|
|
316
|
+
"I assume the cache holds 64 entries.",
|
|
317
|
+
"We assume the cache holds 64 entries.",
|
|
318
|
+
"I left it for you.",
|
|
319
|
+
"I left these for you.",
|
|
320
|
+
"The cache size is for you to decide.",
|
|
321
|
+
"The cache size is waiting on you.",
|
|
322
|
+
"The cache size waits on you.",
|
|
323
|
+
"Should the cache hold 64 entries?",
|
|
324
|
+
"- Should the cache hold 64 entries?**",
|
|
325
|
+
"You may want to decide on the timeout.",
|
|
326
|
+
"I didn't test the retry path.",
|
|
327
|
+
"I did not test the retry path.",
|
|
328
|
+
"I'll do the docs in a separate PR.",
|
|
329
|
+
"The docs go in a separate pull request.",
|
|
330
|
+
"I could not confirm the cache size.",
|
|
331
|
+
"I couldn't confirm the cache size.",
|
|
332
|
+
"I was unable to confirm the cache size.",
|
|
333
|
+
"The cache warm-up is not yet implemented.",
|
|
334
|
+
"The cache size rests on your judgment.",
|
|
335
|
+
"Nothing deferred, except the backoff setting.",
|
|
336
|
+
"Nothing is deferred.",
|
|
337
|
+
"No follow-up is needed.",
|
|
338
|
+
"No open questions remain.",
|
|
339
|
+
"The cleanup is not deferred.",
|
|
340
|
+
"Nothing is out of scope.",
|
|
341
|
+
],
|
|
342
|
+
)
|
|
343
|
+
def test_should_hold_for_each_marker_of_an_open_item(
|
|
344
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
345
|
+
capsys: pytest.CaptureFixture[str],
|
|
346
|
+
tmp_path: Path,
|
|
347
|
+
open_line: str,
|
|
348
|
+
) -> None:
|
|
349
|
+
reason = held_reason(
|
|
350
|
+
monkeypatch, capsys, tmp_path, [typed_prompt_record(), reply_record(open_line)]
|
|
351
|
+
)
|
|
352
|
+
assert reason is not None
|
|
353
|
+
assert f'"{open_line}"' in reason
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
@pytest.mark.parametrize(
|
|
357
|
+
"closed_line",
|
|
358
|
+
[
|
|
359
|
+
"Both tests pass, and the job is renamed.",
|
|
360
|
+
"The flag is `--fast?`",
|
|
361
|
+
"I will follow up with the build once it lands.",
|
|
362
|
+
"The undeferred jobs ran first.",
|
|
363
|
+
"Open items: none.",
|
|
364
|
+
],
|
|
365
|
+
)
|
|
366
|
+
def test_should_allow_a_line_with_no_marker_and_no_work(
|
|
367
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
368
|
+
capsys: pytest.CaptureFixture[str],
|
|
369
|
+
tmp_path: Path,
|
|
370
|
+
closed_line: str,
|
|
371
|
+
) -> None:
|
|
372
|
+
assert (
|
|
373
|
+
held_reason(
|
|
374
|
+
monkeypatch, capsys, tmp_path, [typed_prompt_record(), reply_record(closed_line)]
|
|
375
|
+
)
|
|
376
|
+
is None
|
|
377
|
+
)
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
def test_should_hold_for_a_marker_that_sits_beside_a_negated_one(
|
|
381
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
382
|
+
) -> None:
|
|
383
|
+
open_line = "Nothing is deferred, but the retry path is untested."
|
|
384
|
+
reason = held_reason(
|
|
385
|
+
monkeypatch, capsys, tmp_path, [typed_prompt_record(), reply_record(open_line)]
|
|
386
|
+
)
|
|
387
|
+
assert reason is not None
|
|
388
|
+
assert f'"{open_line}"' in reason
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
@pytest.mark.parametrize(
|
|
392
|
+
"masked_reply",
|
|
393
|
+
[
|
|
394
|
+
'The job holds three status values.\n\n```python\nSTATUS_NAMES = ("queued", "deferred")\n```\n\nThat is all.',
|
|
395
|
+
"The changelog has one entry.\n\n> The backoff setting is deferred.\n\nThat entry is a month old.",
|
|
396
|
+
"A paused job holds the status `deferred` until the queue drains.",
|
|
397
|
+
"The page has one block.\n\n<div>\nThe backoff setting is deferred.\n</div>\n\nThat is all.",
|
|
398
|
+
"The flag is not `--fast` sure enough.",
|
|
399
|
+
"Run this to see the queue:\n\n queue list --status deferred\n\nThat is all.",
|
|
400
|
+
" queue list --status deferred\n\nThat is all.",
|
|
401
|
+
],
|
|
402
|
+
)
|
|
403
|
+
def test_should_leave_out_fences_quotes_code_spans_html_and_indented_code_blocks(
|
|
404
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
405
|
+
capsys: pytest.CaptureFixture[str],
|
|
406
|
+
tmp_path: Path,
|
|
407
|
+
masked_reply: str,
|
|
408
|
+
) -> None:
|
|
409
|
+
assert (
|
|
410
|
+
held_reason(
|
|
411
|
+
monkeypatch, capsys, tmp_path, [typed_prompt_record(), reply_record(masked_reply)]
|
|
412
|
+
)
|
|
413
|
+
is None
|
|
414
|
+
)
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
def test_should_read_assistant_text_blocks_only(
|
|
418
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
419
|
+
) -> None:
|
|
420
|
+
thinking_record = {
|
|
421
|
+
"type": "assistant",
|
|
422
|
+
"message": {
|
|
423
|
+
"role": "assistant",
|
|
424
|
+
"content": [{"type": "thinking", "thinking": "I am not sure which file it is."}],
|
|
425
|
+
},
|
|
426
|
+
}
|
|
427
|
+
sidechain_record = {**reply_record("The second caller is unverified."), "isSidechain": True}
|
|
428
|
+
all_records = [
|
|
429
|
+
typed_prompt_record("Note that the backoff setting is deferred."),
|
|
430
|
+
thinking_record,
|
|
431
|
+
sidechain_record,
|
|
432
|
+
tool_call_record(
|
|
433
|
+
"Write", {"file_path": "notes.md", "content": "The backoff setting is deferred."}
|
|
434
|
+
),
|
|
435
|
+
tool_result_record("Write", "The backoff setting is deferred and untested."),
|
|
436
|
+
reply_record("The note is written.\n\nOpen items: none."),
|
|
437
|
+
]
|
|
438
|
+
assert held_reason(monkeypatch, capsys, tmp_path, all_records) is None
|
|
439
|
+
|
|
440
|
+
|
|
441
|
+
def test_should_hold_for_a_deferral_from_an_earlier_turn_after_a_notification_turn(
|
|
442
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
443
|
+
) -> None:
|
|
444
|
+
all_records = [
|
|
445
|
+
typed_prompt_record(),
|
|
446
|
+
reply_record(DEFERRAL_LINE),
|
|
447
|
+
machine_prompt_record("task_notification", "<task-notification>done</task-notification>"),
|
|
448
|
+
reply_record("The test run finished, and all tests pass."),
|
|
449
|
+
]
|
|
450
|
+
reason = held_reason(monkeypatch, capsys, tmp_path, all_records)
|
|
451
|
+
assert reason is not None
|
|
452
|
+
assert f'"{DEFERRAL_LINE}"' in reason
|
|
453
|
+
|
|
454
|
+
|
|
455
|
+
@pytest.mark.parametrize(
|
|
456
|
+
"all_extra_fields",
|
|
457
|
+
[
|
|
458
|
+
{"agent_id": "agent-1"},
|
|
459
|
+
{"conversation_id": "conversation-1"},
|
|
460
|
+
{"background_tasks": [{"id": "task-1"}]},
|
|
461
|
+
],
|
|
462
|
+
)
|
|
463
|
+
def test_should_allow_a_subagent_a_cursor_stop_and_a_stop_with_background_work(
|
|
464
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
465
|
+
capsys: pytest.CaptureFixture[str],
|
|
466
|
+
tmp_path: Path,
|
|
467
|
+
all_extra_fields: dict[str, object],
|
|
468
|
+
) -> None:
|
|
469
|
+
transcript_path = write_records(
|
|
470
|
+
tmp_path / "s.jsonl", [typed_prompt_record(), reply_record(DEFERRAL_LINE)]
|
|
471
|
+
)
|
|
472
|
+
assert run_gate(monkeypatch, capsys, transcript_path, all_extra_fields) == {}
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+
@pytest.mark.parametrize(
|
|
476
|
+
"all_extra_fields", [{"agent_id": ""}, {"background_tasks": []}, {"stop_hook_active": True}]
|
|
477
|
+
)
|
|
478
|
+
def test_should_hold_when_a_field_that_exempts_a_stop_is_empty_or_marks_a_held_stop(
|
|
479
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
480
|
+
capsys: pytest.CaptureFixture[str],
|
|
481
|
+
tmp_path: Path,
|
|
482
|
+
all_extra_fields: dict[str, object],
|
|
483
|
+
) -> None:
|
|
484
|
+
transcript_path = write_records(
|
|
485
|
+
tmp_path / "s.jsonl", [typed_prompt_record(), reply_record(DEFERRAL_LINE)]
|
|
486
|
+
)
|
|
487
|
+
assert run_gate(monkeypatch, capsys, transcript_path, all_extra_fields)["decision"] == "block"
|
|
488
|
+
|
|
489
|
+
|
|
490
|
+
def test_should_allow_a_headless_run_and_hold_when_the_variable_is_unset(
|
|
491
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
492
|
+
) -> None:
|
|
493
|
+
transcript_path = write_records(
|
|
494
|
+
tmp_path / "s.jsonl", [typed_prompt_record(), reply_record(DEFERRAL_LINE)]
|
|
495
|
+
)
|
|
496
|
+
monkeypatch.setenv("CLAUDE_CODE_SESSION_ATTENDED", "0")
|
|
497
|
+
assert run_gate(monkeypatch, capsys, transcript_path) == {}
|
|
498
|
+
monkeypatch.delenv("CLAUDE_CODE_SESSION_ATTENDED")
|
|
499
|
+
assert run_gate(monkeypatch, capsys, transcript_path)["decision"] == "block"
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
@pytest.mark.parametrize("turn_origin", ["sdk", "task_notification"])
|
|
503
|
+
def test_should_allow_a_session_no_person_typed_into(
|
|
504
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
505
|
+
capsys: pytest.CaptureFixture[str],
|
|
506
|
+
tmp_path: Path,
|
|
507
|
+
turn_origin: str,
|
|
508
|
+
) -> None:
|
|
509
|
+
all_records = [machine_prompt_record(turn_origin), reply_record(DEFERRAL_LINE)]
|
|
510
|
+
assert held_reason(monkeypatch, capsys, tmp_path, all_records) is None
|
|
511
|
+
|
|
512
|
+
|
|
513
|
+
def test_should_hold_an_agent_seeded_session_a_person_later_typed_into(
|
|
514
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
515
|
+
) -> None:
|
|
516
|
+
all_records = [
|
|
517
|
+
machine_prompt_record("task_notification"),
|
|
518
|
+
reply_record("The retry flag is in place."),
|
|
519
|
+
typed_prompt_record("Add the backoff setting too."),
|
|
520
|
+
reply_record("The jitter option is parked."),
|
|
521
|
+
]
|
|
522
|
+
assert '"The jitter option is parked."' in str(
|
|
523
|
+
held_reason(monkeypatch, capsys, tmp_path, all_records)
|
|
524
|
+
)
|
|
525
|
+
|
|
526
|
+
|
|
527
|
+
def test_should_hold_a_second_time_and_allow_the_third_stop(
|
|
528
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
529
|
+
) -> None:
|
|
530
|
+
all_held_once_records = [
|
|
531
|
+
typed_prompt_record(),
|
|
532
|
+
reply_record(DEFERRAL_LINE),
|
|
533
|
+
stop_feedback_record(),
|
|
534
|
+
reply_record("The backoff setting waits for the next change."),
|
|
535
|
+
]
|
|
536
|
+
assert f'"{DEFERRAL_LINE}"' in str(
|
|
537
|
+
held_reason(monkeypatch, capsys, tmp_path, all_held_once_records)
|
|
538
|
+
)
|
|
539
|
+
all_held_twice_records = [
|
|
540
|
+
*all_held_once_records,
|
|
541
|
+
stop_feedback_record(),
|
|
542
|
+
reply_record("No question tool here."),
|
|
543
|
+
]
|
|
544
|
+
assert held_reason(monkeypatch, capsys, tmp_path, all_held_twice_records) is None
|
|
545
|
+
|
|
546
|
+
|
|
547
|
+
def test_should_start_a_new_hold_count_after_a_question_card(
|
|
548
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
549
|
+
) -> None:
|
|
550
|
+
all_records = [
|
|
551
|
+
typed_prompt_record(),
|
|
552
|
+
reply_record(DEFERRAL_LINE),
|
|
553
|
+
stop_feedback_record(),
|
|
554
|
+
stop_feedback_record(),
|
|
555
|
+
tool_call_record("AskUserQuestion"),
|
|
556
|
+
tool_result_record("AskUserQuestion", "The user chose Add it now."),
|
|
557
|
+
reply_record("The jitter option is parked."),
|
|
558
|
+
]
|
|
559
|
+
assert '"The jitter option is parked."' in str(
|
|
560
|
+
held_reason(monkeypatch, capsys, tmp_path, all_records)
|
|
561
|
+
)
|
|
562
|
+
|
|
563
|
+
|
|
564
|
+
def test_should_not_count_feedback_from_another_gate_or_an_assistant_quote_as_a_hold(
|
|
565
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
566
|
+
) -> None:
|
|
567
|
+
all_records = [
|
|
568
|
+
typed_prompt_record(f"What does this mean? {CLOSING_ROUND_REASON_LEAD}"),
|
|
569
|
+
reply_record(DEFERRAL_LINE),
|
|
570
|
+
stop_feedback_record(
|
|
571
|
+
"Stop hook feedback:\nThe task list shows completed work beside open items."
|
|
572
|
+
),
|
|
573
|
+
reply_record(f"The gate said: {CLOSING_ROUND_REASON_LEAD}"),
|
|
574
|
+
stop_feedback_record("Stop hook feedback:\nThis turn worked on the user's ask."),
|
|
575
|
+
reply_record(f"The gate said again: {CLOSING_ROUND_REASON_LEAD}"),
|
|
576
|
+
]
|
|
577
|
+
assert f'"{DEFERRAL_LINE}"' in str(held_reason(monkeypatch, capsys, tmp_path, all_records))
|
|
578
|
+
|
|
579
|
+
|
|
580
|
+
def test_should_quote_at_most_eight_trimmed_lines_and_count_the_rest(
|
|
581
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
582
|
+
) -> None:
|
|
583
|
+
long_line = "The cache path is untested " + "and slow " * 40
|
|
584
|
+
all_open_item_lines = [f"Step {each_number} is deferred." for each_number in range(1, 11)]
|
|
585
|
+
reason = held_reason(
|
|
586
|
+
monkeypatch,
|
|
587
|
+
capsys,
|
|
588
|
+
tmp_path,
|
|
589
|
+
[
|
|
590
|
+
typed_prompt_record(),
|
|
591
|
+
reply_record("\n".join(all_open_item_lines)),
|
|
592
|
+
reply_record(long_line),
|
|
593
|
+
],
|
|
594
|
+
)
|
|
595
|
+
assert reason is not None
|
|
596
|
+
all_quoted_lines = [each_line for each_line in reason.split("\n") if each_line.startswith('"')]
|
|
597
|
+
assert len(all_quoted_lines) == MAXIMUM_QUOTED_LINE_COUNT
|
|
598
|
+
assert all_quoted_lines[0] == '"Step 4 is deferred."'
|
|
599
|
+
assert len(all_quoted_lines[-1]) == MAXIMUM_QUOTED_LINE_LENGTH + len('""')
|
|
600
|
+
assert "Step 3 is deferred." not in reason
|
|
601
|
+
assert "Earlier lines left out here: 3." in reason
|
|
602
|
+
|
|
603
|
+
|
|
604
|
+
def test_should_quote_a_repeated_line_once(
|
|
605
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
606
|
+
) -> None:
|
|
607
|
+
reason = held_reason(
|
|
608
|
+
monkeypatch,
|
|
609
|
+
capsys,
|
|
610
|
+
tmp_path,
|
|
611
|
+
[typed_prompt_record(), reply_record(DEFERRAL_LINE), reply_record(DEFERRAL_LINE)],
|
|
612
|
+
)
|
|
613
|
+
assert reason is not None
|
|
614
|
+
assert reason.count(DEFERRAL_LINE) == 1
|
|
615
|
+
|
|
616
|
+
|
|
617
|
+
def test_should_allow_malformed_stdin(
|
|
618
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
|
619
|
+
) -> None:
|
|
620
|
+
monkeypatch.setattr("sys.stdin", io.TextIOWrapper(io.BytesIO(b"not json")))
|
|
621
|
+
assert closing_question_round_gate.main() == 0
|
|
622
|
+
assert capsys.readouterr().out == ""
|
|
623
|
+
|
|
624
|
+
|
|
625
|
+
def test_should_allow_a_session_with_no_readable_transcript(
|
|
626
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
627
|
+
) -> None:
|
|
628
|
+
assert run_gate(monkeypatch, capsys, None) == {}
|
|
629
|
+
assert run_gate(monkeypatch, capsys, tmp_path / "missing.jsonl") == {}
|
|
630
|
+
|
|
631
|
+
|
|
632
|
+
def test_should_hold_through_a_subprocess_with_the_documented_contract(tmp_path: Path) -> None:
|
|
633
|
+
transcript_path = write_records(
|
|
634
|
+
tmp_path / "s.jsonl", [typed_prompt_record(), reply_record(DEFERRAL_LINE)]
|
|
635
|
+
)
|
|
636
|
+
hook_input = {
|
|
637
|
+
"hook_event_name": "Stop",
|
|
638
|
+
"session_id": SESSION_ID,
|
|
639
|
+
"transcript_path": str(transcript_path),
|
|
640
|
+
}
|
|
641
|
+
completed_process = subprocess.run(
|
|
642
|
+
[sys.executable, str(GATE_PATH)],
|
|
643
|
+
input=json.dumps(hook_input),
|
|
644
|
+
capture_output=True,
|
|
645
|
+
text=True,
|
|
646
|
+
env={
|
|
647
|
+
"HOME": str(tmp_path),
|
|
648
|
+
"USERPROFILE": str(tmp_path),
|
|
649
|
+
"CLAUDE_CODE_SESSION_ATTENDED": "1",
|
|
650
|
+
},
|
|
651
|
+
check=False,
|
|
652
|
+
)
|
|
653
|
+
assert completed_process.returncode == 2
|
|
654
|
+
assert json.loads(completed_process.stdout)["decision"] == "block"
|
|
655
|
+
assert completed_process.stderr.startswith(CLOSING_ROUND_REASON_LEAD)
|
|
656
|
+
|
|
657
|
+
|
|
658
|
+
def test_is_exempt_stop_reads_the_payload_and_the_environment(
|
|
659
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
660
|
+
) -> None:
|
|
661
|
+
assert closing_question_round_gate.is_exempt_stop({"agent_id": "agent-1"})
|
|
662
|
+
assert closing_question_round_gate.is_exempt_stop({"conversation_id": "conversation-1"})
|
|
663
|
+
assert closing_question_round_gate.is_exempt_stop({"background_tasks": [{"id": "task-1"}]})
|
|
664
|
+
assert closing_question_round_gate.is_exempt_stop({"background_tasks": {"task-1": {}}})
|
|
665
|
+
assert closing_question_round_gate.is_exempt_stop({"background_tasks": "task-1"})
|
|
666
|
+
assert not closing_question_round_gate.is_exempt_stop({"background_tasks": {}})
|
|
667
|
+
assert not closing_question_round_gate.is_exempt_stop({"agent_id": "", "background_tasks": []})
|
|
668
|
+
monkeypatch.setenv("CLAUDE_CODE_SESSION_ATTENDED", "0")
|
|
669
|
+
assert closing_question_round_gate.is_exempt_stop({})
|
|
670
|
+
|
|
671
|
+
|
|
672
|
+
def test_prior_hold_count_counts_feedback_records_that_carry_the_lead() -> None:
|
|
673
|
+
all_stretch_records = [
|
|
674
|
+
stop_feedback_record(),
|
|
675
|
+
stop_feedback_record("Stop hook feedback:\nThe task list shows completed work."),
|
|
676
|
+
reply_record(CLOSING_ROUND_REASON_LEAD),
|
|
677
|
+
{
|
|
678
|
+
"type": "user",
|
|
679
|
+
"message": {
|
|
680
|
+
"role": "user",
|
|
681
|
+
"content": f"Stop hook feedback:\n{CLOSING_ROUND_REASON_LEAD}",
|
|
682
|
+
},
|
|
683
|
+
},
|
|
684
|
+
typed_prompt_record(CLOSING_ROUND_REASON_LEAD),
|
|
685
|
+
]
|
|
686
|
+
assert closing_question_round_gate.prior_hold_count(all_stretch_records) == 2
|
|
687
|
+
|
|
688
|
+
|
|
689
|
+
def test_closing_round_reason_leads_with_the_fixed_sentence_and_ends_with_the_instruction() -> None:
|
|
690
|
+
reason = closing_question_round_gate.closing_round_reason(
|
|
691
|
+
["The cleanup is parked.", "The cache is unverified."]
|
|
692
|
+
)
|
|
693
|
+
all_reason_lines = reason.split("\n")
|
|
694
|
+
assert all_reason_lines[0] == CLOSING_ROUND_REASON_LEAD
|
|
695
|
+
assert all_reason_lines[2:4] == ['"The cleanup is parked."', '"The cache is unverified."']
|
|
696
|
+
assert "lines left out here" not in reason
|
|
697
|
+
assert "at most four questions in one call" in all_reason_lines[-1]
|
|
698
|
+
assert "refuses a question past 450 words or 15 items" in all_reason_lines[-1]
|
|
699
|
+
assert "a refused question starts a new count" in all_reason_lines[-1]
|
|
700
|
+
assert "write a link in a card as a bare URL" in all_reason_lines[-1]
|
|
701
|
+
assert "end the reply with the line Open items: none." in all_reason_lines[-1]
|
|
702
|
+
assert all_reason_lines[-1].endswith(
|
|
703
|
+
"send one question that says so and lets the user add an item."
|
|
704
|
+
)
|
|
705
|
+
|
|
706
|
+
|
|
707
|
+
def test_work_reason_leads_with_its_sentence_and_ends_with_the_declaration_line() -> None:
|
|
708
|
+
reason = closing_question_round_gate.work_reason()
|
|
709
|
+
all_reason_lines = reason.split("\n")
|
|
710
|
+
assert all_reason_lines[0] == WORK_REASON_LEAD
|
|
711
|
+
assert len(all_reason_lines) == 2
|
|
712
|
+
assert "Read the task list, your notes, and the live state of each item." in reason
|
|
713
|
+
assert "needs or can use the user's judgment" in reason
|
|
714
|
+
assert "refuses a question past 450 words or 15 items" in reason
|
|
715
|
+
assert reason.endswith("in plain text with no backticks or list marker: Open items: none.")
|
|
716
|
+
|
|
717
|
+
|
|
718
|
+
def test_should_read_the_closing_reply_from_the_payload(
|
|
719
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
720
|
+
) -> None:
|
|
721
|
+
all_records = [
|
|
722
|
+
typed_prompt_record(),
|
|
723
|
+
reply_record(DEFERRAL_LINE),
|
|
724
|
+
tool_call_record("AskUserQuestion"),
|
|
725
|
+
tool_result_record("AskUserQuestion", "The user chose Leave it."),
|
|
726
|
+
]
|
|
727
|
+
closing_reply = "The backoff setting stays.\n\nThe jitter option is parked."
|
|
728
|
+
reason = held_reason(
|
|
729
|
+
monkeypatch, capsys, tmp_path, all_records, {"last_assistant_message": closing_reply}
|
|
730
|
+
)
|
|
731
|
+
assert '"The jitter option is parked."' in str(reason)
|
|
732
|
+
assert DEFERRAL_LINE not in str(reason)
|
|
733
|
+
assert (
|
|
734
|
+
held_reason(monkeypatch, capsys, tmp_path, all_records, {"last_assistant_message": 7})
|
|
735
|
+
is None
|
|
736
|
+
)
|
|
737
|
+
|
|
738
|
+
|
|
739
|
+
def test_should_not_count_a_closing_reply_the_transcript_already_holds_twice(
|
|
740
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
741
|
+
) -> None:
|
|
742
|
+
closing_reply = "\n".join(f"Step {each_number} is deferred." for each_number in range(1, 9))
|
|
743
|
+
reason = held_reason(
|
|
744
|
+
monkeypatch,
|
|
745
|
+
capsys,
|
|
746
|
+
tmp_path,
|
|
747
|
+
[typed_prompt_record(), reply_record("Step 0 is deferred."), reply_record(closing_reply)],
|
|
748
|
+
{"last_assistant_message": closing_reply},
|
|
749
|
+
)
|
|
750
|
+
assert "Earlier lines left out here: 1." in str(reason)
|
|
751
|
+
assert '"Step 1 is deferred."' in str(reason)
|
|
752
|
+
|
|
753
|
+
|
|
754
|
+
def test_should_not_reread_a_closing_reply_written_before_the_cards(
|
|
755
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
756
|
+
) -> None:
|
|
757
|
+
all_records = [
|
|
758
|
+
typed_prompt_record(),
|
|
759
|
+
reply_record(DEFERRAL_LINE),
|
|
760
|
+
tool_call_record("AskUserQuestion"),
|
|
761
|
+
tool_result_record("AskUserQuestion", "The user chose Leave it."),
|
|
762
|
+
]
|
|
763
|
+
assert (
|
|
764
|
+
held_reason(
|
|
765
|
+
monkeypatch, capsys, tmp_path, all_records, {"last_assistant_message": DEFERRAL_LINE}
|
|
766
|
+
)
|
|
767
|
+
is None
|
|
768
|
+
)
|
|
769
|
+
|
|
770
|
+
|
|
771
|
+
@pytest.mark.parametrize(
|
|
772
|
+
("wrapped_reply", "quoted_text"),
|
|
773
|
+
[
|
|
774
|
+
(
|
|
775
|
+
"The cleanup is out of\nscope for this change.",
|
|
776
|
+
"The cleanup is out of scope for this change.",
|
|
777
|
+
),
|
|
778
|
+
("The retry path is not\ntested yet.", "The retry path is not tested yet."),
|
|
779
|
+
],
|
|
780
|
+
)
|
|
781
|
+
def test_should_find_a_marker_split_across_a_soft_line_break(
|
|
782
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
783
|
+
capsys: pytest.CaptureFixture[str],
|
|
784
|
+
tmp_path: Path,
|
|
785
|
+
wrapped_reply: str,
|
|
786
|
+
quoted_text: str,
|
|
787
|
+
) -> None:
|
|
788
|
+
reason = held_reason(
|
|
789
|
+
monkeypatch, capsys, tmp_path, [typed_prompt_record(), reply_record(wrapped_reply)]
|
|
790
|
+
)
|
|
791
|
+
assert f'"{quoted_text}"' in str(reason)
|
|
792
|
+
|
|
793
|
+
|
|
794
|
+
def test_should_hold_on_a_host_whose_prompt_records_carry_no_turn_origin(
|
|
795
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
796
|
+
) -> None:
|
|
797
|
+
untagged_prompt = {"type": "user", "message": {"role": "user", "content": "Add the flag."}}
|
|
798
|
+
all_records = [untagged_prompt, reply_record(DEFERRAL_LINE)]
|
|
799
|
+
assert f'"{DEFERRAL_LINE}"' in str(held_reason(monkeypatch, capsys, tmp_path, all_records))
|
|
800
|
+
monkeypatch.delenv("CLAUDE_CODE_SESSION_ATTENDED")
|
|
801
|
+
assert held_reason(monkeypatch, capsys, tmp_path, all_records) is None
|
|
802
|
+
|
|
803
|
+
|
|
804
|
+
def test_should_hold_a_session_a_custom_slash_command_started(
|
|
805
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
806
|
+
) -> None:
|
|
807
|
+
command_record = {
|
|
808
|
+
"type": "user",
|
|
809
|
+
"turnOrigin": "human",
|
|
810
|
+
"message": {
|
|
811
|
+
"role": "user",
|
|
812
|
+
"content": "<command-message>hello</command-message>\n"
|
|
813
|
+
"<command-name>/hello</command-name>",
|
|
814
|
+
},
|
|
815
|
+
}
|
|
816
|
+
command_text_record = {
|
|
817
|
+
"type": "user",
|
|
818
|
+
"isMeta": True,
|
|
819
|
+
"message": {"role": "user", "content": "Say hello and list what is left."},
|
|
820
|
+
}
|
|
821
|
+
all_records = [command_record, command_text_record, reply_record(DEFERRAL_LINE)]
|
|
822
|
+
assert f'"{DEFERRAL_LINE}"' in str(held_reason(monkeypatch, capsys, tmp_path, all_records))
|
|
823
|
+
|
|
824
|
+
|
|
825
|
+
@pytest.mark.parametrize("malformed_name", [["AskUserQuestion"], {"name": "AskUserQuestion"}])
|
|
826
|
+
def test_should_read_past_a_tool_call_whose_name_is_no_string(
|
|
827
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
828
|
+
capsys: pytest.CaptureFixture[str],
|
|
829
|
+
tmp_path: Path,
|
|
830
|
+
malformed_name: object,
|
|
831
|
+
) -> None:
|
|
832
|
+
all_records = [
|
|
833
|
+
typed_prompt_record(),
|
|
834
|
+
reply_record(DEFERRAL_LINE),
|
|
835
|
+
tool_call_record(malformed_name, tool_use_id="id-malformed"),
|
|
836
|
+
tool_result_record("AskUserQuestion", "ok", tool_use_id="id-malformed"),
|
|
837
|
+
]
|
|
838
|
+
assert f'"{DEFERRAL_LINE}"' in str(held_reason(monkeypatch, capsys, tmp_path, all_records))
|
|
839
|
+
|
|
840
|
+
|
|
841
|
+
def test_should_allow_with_no_output_when_an_internal_step_raises(
|
|
842
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
843
|
+
) -> None:
|
|
844
|
+
def raise_type_error(*_: object) -> list[str]:
|
|
845
|
+
raise TypeError("unhashable type: 'list'")
|
|
846
|
+
|
|
847
|
+
transcript_path = write_records(
|
|
848
|
+
tmp_path / "s.jsonl", [typed_prompt_record(), reply_record(DEFERRAL_LINE)]
|
|
849
|
+
)
|
|
850
|
+
assert run_gate(monkeypatch, capsys, transcript_path)["decision"] == "block"
|
|
851
|
+
monkeypatch.setattr(closing_question_round_gate, "marked_open_lines", raise_type_error)
|
|
852
|
+
assert run_gate(monkeypatch, capsys, transcript_path) == {}
|
|
853
|
+
|
|
854
|
+
|
|
855
|
+
def test_should_allow_with_no_output_once_the_deadline_passes(
|
|
856
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
857
|
+
) -> None:
|
|
858
|
+
transcript_path = write_records(
|
|
859
|
+
tmp_path / "s.jsonl", [typed_prompt_record(), reply_record(DEFERRAL_LINE)]
|
|
860
|
+
)
|
|
861
|
+
assert run_gate(monkeypatch, capsys, transcript_path)["decision"] == "block"
|
|
862
|
+
monkeypatch.setattr(closing_question_round_gate, "STOP_DEADLINE_SECONDS", 0.0)
|
|
863
|
+
assert run_gate(monkeypatch, capsys, transcript_path) == {}
|
|
864
|
+
|
|
865
|
+
|
|
866
|
+
@pytest.mark.parametrize("closed_stream_name", ["stdin", "stdout", "stderr"])
|
|
867
|
+
def test_supervised_main_should_allow_with_no_output_when_a_standard_stream_is_closed(
|
|
868
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], closed_stream_name: str
|
|
869
|
+
) -> None:
|
|
870
|
+
def held_run(*all_arguments: object, **all_options: object) -> object:
|
|
871
|
+
return subprocess.CompletedProcess(args=[], returncode=2, stdout=b'{"x": 1}', stderr=b"why")
|
|
872
|
+
|
|
873
|
+
monkeypatch.setattr("sys.stdin", io.TextIOWrapper(io.BytesIO(b"{}")))
|
|
874
|
+
monkeypatch.setattr(closing_question_round_gate.subprocess, "run", held_run)
|
|
875
|
+
monkeypatch.setattr(f"sys.{closed_stream_name}", None)
|
|
876
|
+
assert closing_question_round_gate.supervised_main() == 0
|
|
877
|
+
captured = capsys.readouterr()
|
|
878
|
+
assert (captured.out, captured.err) == ("", "")
|
|
879
|
+
|
|
880
|
+
|
|
881
|
+
def test_should_hold_work_whose_record_spells_its_type_with_a_json_escape(
|
|
882
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
883
|
+
) -> None:
|
|
884
|
+
escaped_work_line = json.dumps(tool_call_record("Edit")).replace(
|
|
885
|
+
'"assistant"', '"\\u0061ssistant"'
|
|
886
|
+
)
|
|
887
|
+
transcript_path = tmp_path / "s.jsonl"
|
|
888
|
+
transcript_path.write_text(
|
|
889
|
+
"\n".join(
|
|
890
|
+
[
|
|
891
|
+
json.dumps(typed_prompt_record()),
|
|
892
|
+
escaped_work_line,
|
|
893
|
+
json.dumps(tool_result_record("Edit", "The file was updated.")),
|
|
894
|
+
]
|
|
895
|
+
),
|
|
896
|
+
encoding="utf-8",
|
|
897
|
+
)
|
|
898
|
+
block_output = run_gate(
|
|
899
|
+
monkeypatch, capsys, transcript_path, {"last_assistant_message": "Done."}
|
|
900
|
+
)
|
|
901
|
+
assert block_output["reason"].startswith(WORK_REASON_LEAD)
|
|
902
|
+
|
|
903
|
+
|
|
904
|
+
def _held_gate_run(
|
|
905
|
+
tmp_path: Path, all_stream_targets: dict[str, int]
|
|
906
|
+
) -> tuple[subprocess.CompletedProcess[bytes], float]:
|
|
907
|
+
transcript_path = write_records(
|
|
908
|
+
tmp_path / "s.jsonl", [typed_prompt_record(), reply_record(DEFERRAL_LINE)]
|
|
909
|
+
)
|
|
910
|
+
hook_input = json.dumps({"hook_event_name": "Stop", "transcript_path": str(transcript_path)})
|
|
911
|
+
buffered_environment = {
|
|
912
|
+
**{
|
|
913
|
+
each_name: each_value
|
|
914
|
+
for each_name, each_value in os.environ.items()
|
|
915
|
+
if each_name != "PYTHONUNBUFFERED"
|
|
916
|
+
},
|
|
917
|
+
"HOME": str(tmp_path),
|
|
918
|
+
"CLAUDE_CODE_SESSION_ATTENDED": "1",
|
|
919
|
+
}
|
|
920
|
+
started_at = time.monotonic()
|
|
921
|
+
completed_gate = subprocess.run(
|
|
922
|
+
[sys.executable, str(GATE_PATH)],
|
|
923
|
+
input=hook_input.encode("utf-8"),
|
|
924
|
+
env=buffered_environment,
|
|
925
|
+
timeout=20,
|
|
926
|
+
check=False,
|
|
927
|
+
**{"stdout": subprocess.DEVNULL, "stderr": subprocess.DEVNULL, **all_stream_targets},
|
|
928
|
+
)
|
|
929
|
+
return completed_gate, time.monotonic() - started_at
|
|
930
|
+
|
|
931
|
+
|
|
932
|
+
@pytest.mark.parametrize("stream_name", ["stdout", "stderr"])
|
|
933
|
+
def test_should_allow_the_stop_soon_after_the_write_starts_when_the_output_pipe_is_full(
|
|
934
|
+
tmp_path: Path, stream_name: str
|
|
935
|
+
) -> None:
|
|
936
|
+
read_end, write_end = os.pipe()
|
|
937
|
+
os.set_blocking(write_end, False)
|
|
938
|
+
try:
|
|
939
|
+
while True:
|
|
940
|
+
os.write(write_end, b"x" * 4096)
|
|
941
|
+
except BlockingIOError:
|
|
942
|
+
os.set_blocking(write_end, True)
|
|
943
|
+
try:
|
|
944
|
+
completed_gate, elapsed_seconds = _held_gate_run(tmp_path, {stream_name: write_end})
|
|
945
|
+
finally:
|
|
946
|
+
os.close(write_end)
|
|
947
|
+
os.close(read_end)
|
|
948
|
+
assert completed_gate.returncode == 0
|
|
949
|
+
assert elapsed_seconds < 5
|
|
950
|
+
|
|
951
|
+
|
|
952
|
+
@pytest.mark.parametrize("stream_name", ["stdout", "stderr"])
|
|
953
|
+
def test_should_allow_the_stop_when_the_output_pipe_has_no_reader(
|
|
954
|
+
tmp_path: Path, stream_name: str
|
|
955
|
+
) -> None:
|
|
956
|
+
read_end, write_end = os.pipe()
|
|
957
|
+
os.close(read_end)
|
|
958
|
+
try:
|
|
959
|
+
completed_gate, _ = _held_gate_run(tmp_path, {stream_name: write_end})
|
|
960
|
+
finally:
|
|
961
|
+
os.close(write_end)
|
|
962
|
+
assert completed_gate.returncode == 0
|
|
963
|
+
|
|
964
|
+
|
|
965
|
+
@pytest.mark.parametrize("unwritable_stderr", ["closed_reader", "full_device"])
|
|
966
|
+
def test_should_leave_no_hold_on_stdout_when_stderr_cannot_be_written(
|
|
967
|
+
tmp_path: Path, unwritable_stderr: str
|
|
968
|
+
) -> None:
|
|
969
|
+
if unwritable_stderr == "full_device":
|
|
970
|
+
if not Path("/dev/full").exists():
|
|
971
|
+
pytest.skip("this host has no /dev/full")
|
|
972
|
+
stderr_target = os.open("/dev/full", os.O_WRONLY)
|
|
973
|
+
else:
|
|
974
|
+
read_end, stderr_target = os.pipe()
|
|
975
|
+
os.close(read_end)
|
|
976
|
+
try:
|
|
977
|
+
completed_gate, _ = _held_gate_run(
|
|
978
|
+
tmp_path, {"stderr": stderr_target, "stdout": subprocess.PIPE}
|
|
979
|
+
)
|
|
980
|
+
finally:
|
|
981
|
+
os.close(stderr_target)
|
|
982
|
+
assert completed_gate.returncode == 0
|
|
983
|
+
assert b'"block"' not in completed_gate.stdout
|