claude-dev-env 8.122.0 → 8.124.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/ever-registered-hooks.test.mjs +5 -0
- package/docs/templates/artifact-page/README.md +14 -1
- package/docs/templates/artifact-page/review-rounds.md +1 -1
- package/docs/templates/artifact-page/template.html +26 -18
- package/docs/templates/html-plan/README.md +4 -0
- package/hooks/blocking/artifact_current_page_fixtures/piled_review_page.html +399 -0
- package/hooks/blocking/artifact_current_page_fixtures/rebuilt_review_page.html +403 -0
- package/hooks/blocking/artifact_current_page_gate.py +418 -0
- package/hooks/blocking/artifact_repeated_item_render.cjs +112 -0
- 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/open_tasks_stop_gate.py +12 -1
- package/hooks/blocking/open_tasks_transcript.py +30 -0
- package/hooks/blocking/open_tasks_transcript_records.py +57 -1
- package/hooks/blocking/test_artifact_current_page_gate.py +415 -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/blocking/test_open_tasks_stop_gate.py +64 -0
- package/hooks/blocking/test_open_tasks_transcript.py +34 -0
- package/hooks/blocking/test_open_tasks_transcript_records.py +34 -0
- package/hooks/features/conduct-gates.md +6 -0
- package/hooks/hooks.json +15 -0
- package/hooks/hooks_constants/artifact_current_page_gate_constants.py +91 -0
- package/hooks/hooks_constants/artifact_dark_mode_gate_constants.py +3 -3
- package/hooks/hooks_constants/closing_question_round_gate_constants.py +167 -0
- package/hooks/hooks_constants/open_tasks_stop_gate_constants.py +8 -0
- package/hooks/hooks_constants/test_artifact_current_page_gate_constants.py +53 -0
- package/hooks/hooks_constants/test_artifact_dark_mode_gate_constants.py +6 -0
- package/hooks/hooks_constants/test_closing_question_round_gate_constants.py +92 -0
- package/hooks/session/artifact_link_record.py +109 -0
- package/hooks/session/test_artifact_link_record.py +101 -0
- package/hooks/test_context_budget_hooks.py +2 -0
- package/package.json +1 -1
- package/scripts/policy_lint/adapter_configuration.py +6 -0
- package/scripts/policy_lint/config/constants.py +1 -0
- package/scripts/tests/test_adapter_configuration.py +8 -0
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
import io
|
|
2
|
+
import json
|
|
3
|
+
import os
|
|
4
|
+
import subprocess
|
|
5
|
+
import sys
|
|
6
|
+
import threading
|
|
7
|
+
import time
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
import pytest
|
|
11
|
+
|
|
12
|
+
import closing_question_round_gate
|
|
13
|
+
|
|
14
|
+
from hooks_constants.closing_question_round_gate_constants import (
|
|
15
|
+
MAXIMUM_PAYLOAD_BYTES,
|
|
16
|
+
WORK_REASON_LEAD,
|
|
17
|
+
)
|
|
18
|
+
from test_closing_question_round_gate import (
|
|
19
|
+
held_reason,
|
|
20
|
+
reply_record,
|
|
21
|
+
stop_feedback_record,
|
|
22
|
+
tool_call_record,
|
|
23
|
+
tool_result_record,
|
|
24
|
+
typed_prompt_record,
|
|
25
|
+
write_records,
|
|
26
|
+
)
|
|
27
|
+
from test_closing_question_round_gate_work import (
|
|
28
|
+
DECLARATION,
|
|
29
|
+
WORK_FEEDBACK_TEXT,
|
|
30
|
+
edit_records,
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
GATE_PATH = Path(__file__).resolve().parent / "closing_question_round_gate.py"
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@pytest.fixture(autouse=True)
|
|
37
|
+
def scratch_home(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
|
|
38
|
+
monkeypatch.setenv("HOME", str(tmp_path))
|
|
39
|
+
monkeypatch.setenv("USERPROFILE", str(tmp_path))
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
@pytest.mark.parametrize(
|
|
43
|
+
"closing_reply",
|
|
44
|
+
[f"Done.\n\n* {DECLARATION}", f"Done.\n\n* **{DECLARATION}**", "Done.\n\nOpen_ items: none."],
|
|
45
|
+
)
|
|
46
|
+
def test_should_hold_a_declaration_in_a_star_list_item_or_with_a_stray_mark(
|
|
47
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
48
|
+
capsys: pytest.CaptureFixture[str],
|
|
49
|
+
tmp_path: Path,
|
|
50
|
+
closing_reply: str,
|
|
51
|
+
) -> None:
|
|
52
|
+
all_records = [typed_prompt_record(), *edit_records(), reply_record(closing_reply)]
|
|
53
|
+
assert str(held_reason(monkeypatch, capsys, tmp_path, all_records)).startswith(WORK_REASON_LEAD)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def test_should_not_count_a_hand_back_that_quotes_the_reason_as_a_hold(
|
|
57
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
58
|
+
) -> None:
|
|
59
|
+
hand_back = stop_feedback_record(f"A worker reports. It quotes this: {WORK_REASON_LEAD}")
|
|
60
|
+
all_records = [
|
|
61
|
+
typed_prompt_record(),
|
|
62
|
+
*edit_records(),
|
|
63
|
+
reply_record("Done."),
|
|
64
|
+
stop_feedback_record(WORK_FEEDBACK_TEXT),
|
|
65
|
+
hand_back,
|
|
66
|
+
reply_record("Done."),
|
|
67
|
+
]
|
|
68
|
+
assert str(held_reason(monkeypatch, capsys, tmp_path, all_records)).startswith(WORK_REASON_LEAD)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def test_should_not_count_a_typed_prompt_that_quotes_the_feedback_as_a_hold(
|
|
72
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
73
|
+
) -> None:
|
|
74
|
+
typed_quote = typed_prompt_record()
|
|
75
|
+
message_body = typed_quote["message"]
|
|
76
|
+
assert isinstance(message_body, dict)
|
|
77
|
+
message_body["content"] = WORK_FEEDBACK_TEXT
|
|
78
|
+
all_records = [
|
|
79
|
+
typed_prompt_record(),
|
|
80
|
+
typed_quote,
|
|
81
|
+
typed_quote,
|
|
82
|
+
typed_prompt_record(),
|
|
83
|
+
*edit_records(),
|
|
84
|
+
reply_record("Done."),
|
|
85
|
+
]
|
|
86
|
+
assert str(held_reason(monkeypatch, capsys, tmp_path, all_records)).startswith(WORK_REASON_LEAD)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
@pytest.mark.parametrize("closing_payload", [{}, {"last_assistant_message": ""}])
|
|
90
|
+
def test_should_hold_work_after_an_earlier_declaration_when_no_closing_text_follows(
|
|
91
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
92
|
+
capsys: pytest.CaptureFixture[str],
|
|
93
|
+
tmp_path: Path,
|
|
94
|
+
closing_payload: dict[str, object],
|
|
95
|
+
) -> None:
|
|
96
|
+
all_records = [
|
|
97
|
+
typed_prompt_record(),
|
|
98
|
+
*edit_records(),
|
|
99
|
+
reply_record(f"The retry flag is in place.\n\n{DECLARATION}"),
|
|
100
|
+
typed_prompt_record(),
|
|
101
|
+
*edit_records(),
|
|
102
|
+
]
|
|
103
|
+
reason = held_reason(monkeypatch, capsys, tmp_path, all_records, closing_payload)
|
|
104
|
+
assert str(reason).startswith(WORK_REASON_LEAD)
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def test_should_hold_a_tool_call_that_shares_a_record_with_the_declaration(
|
|
108
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
109
|
+
) -> None:
|
|
110
|
+
declared_call = tool_call_record("Edit", {"file_path": "jobs/export.py"}, tool_use_id="id-x")
|
|
111
|
+
message_body = declared_call["message"]
|
|
112
|
+
assert isinstance(message_body, dict)
|
|
113
|
+
message_body["content"] = [{"type": "text", "text": DECLARATION}, *message_body["content"]]
|
|
114
|
+
all_records = [
|
|
115
|
+
typed_prompt_record(),
|
|
116
|
+
declared_call,
|
|
117
|
+
tool_result_record("Edit", "The file was updated.", tool_use_id="id-x"),
|
|
118
|
+
]
|
|
119
|
+
reason = held_reason(monkeypatch, capsys, tmp_path, all_records, {"last_assistant_message": ""})
|
|
120
|
+
assert str(reason).startswith(WORK_REASON_LEAD)
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def test_should_allow_a_declaration_the_payload_carries_after_work(
|
|
124
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
125
|
+
) -> None:
|
|
126
|
+
all_records = [typed_prompt_record(), *edit_records()]
|
|
127
|
+
closing_payload: dict[str, object] = {"last_assistant_message": f"Done.\n\n{DECLARATION}"}
|
|
128
|
+
assert held_reason(monkeypatch, capsys, tmp_path, all_records, closing_payload) is None
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def test_should_allow_a_payload_declaration_that_repeats_a_text_from_before_the_work(
|
|
132
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
133
|
+
) -> None:
|
|
134
|
+
all_records = [typed_prompt_record(), reply_record(DECLARATION), *edit_records()]
|
|
135
|
+
closing_payload: dict[str, object] = {"last_assistant_message": DECLARATION}
|
|
136
|
+
assert held_reason(monkeypatch, capsys, tmp_path, all_records, closing_payload) is None
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
@pytest.mark.parametrize(
|
|
140
|
+
"closing_reply",
|
|
141
|
+
[f"**{DECLARATION}", f"{DECLARATION}__", f"**{DECLARATION}_"],
|
|
142
|
+
)
|
|
143
|
+
def test_should_hold_a_declaration_with_unmatched_marks(
|
|
144
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
145
|
+
capsys: pytest.CaptureFixture[str],
|
|
146
|
+
tmp_path: Path,
|
|
147
|
+
closing_reply: str,
|
|
148
|
+
) -> None:
|
|
149
|
+
all_records = [typed_prompt_record(), *edit_records(), reply_record(closing_reply)]
|
|
150
|
+
assert str(held_reason(monkeypatch, capsys, tmp_path, all_records)).startswith(WORK_REASON_LEAD)
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def test_should_allow_within_ten_seconds_when_a_scan_step_never_reads_the_clock(
|
|
154
|
+
tmp_path: Path,
|
|
155
|
+
) -> None:
|
|
156
|
+
transcript_path = tmp_path / "s.jsonl"
|
|
157
|
+
all_pending_calls = [
|
|
158
|
+
tool_call_record("AskUserQuestion", tool_use_id=f"id-{each_number}")
|
|
159
|
+
for each_number in range(20000)
|
|
160
|
+
]
|
|
161
|
+
oversized_result = tool_result_record("Read", "z" * (15 * 1024 * 1024), tool_use_id="id-none")
|
|
162
|
+
write_records(transcript_path, [typed_prompt_record(), *all_pending_calls, oversized_result])
|
|
163
|
+
hook_input = {"hook_event_name": "Stop", "transcript_path": str(transcript_path)}
|
|
164
|
+
started_at = time.monotonic()
|
|
165
|
+
completed_gate = subprocess.run(
|
|
166
|
+
[sys.executable, str(GATE_PATH)],
|
|
167
|
+
input=json.dumps(hook_input),
|
|
168
|
+
capture_output=True,
|
|
169
|
+
text=True,
|
|
170
|
+
check=False,
|
|
171
|
+
env={**os.environ, "HOME": str(tmp_path), "CLAUDE_CODE_SESSION_ATTENDED": "1"},
|
|
172
|
+
)
|
|
173
|
+
assert completed_gate.returncode in (0, 2)
|
|
174
|
+
assert time.monotonic() - started_at < 10
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def test_watchdog_should_not_exit_once_the_hold_output_has_started(
|
|
178
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
179
|
+
) -> None:
|
|
180
|
+
all_exit_codes: list[int] = []
|
|
181
|
+
monkeypatch.setattr(closing_question_round_gate.os, "_exit", all_exit_codes.append)
|
|
182
|
+
output_lock = threading.Lock()
|
|
183
|
+
output_lock.acquire()
|
|
184
|
+
closing_question_round_gate._allow_now(output_lock)
|
|
185
|
+
assert all_exit_codes == []
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def test_watchdog_should_exit_with_allow_before_any_hold_output(
|
|
189
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
190
|
+
) -> None:
|
|
191
|
+
all_exit_codes: list[int] = []
|
|
192
|
+
monkeypatch.setattr(closing_question_round_gate.os, "_exit", all_exit_codes.append)
|
|
193
|
+
closing_question_round_gate._allow_now(threading.Lock())
|
|
194
|
+
assert all_exit_codes == [0]
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
def test_should_allow_a_payload_over_the_payload_cap_without_parsing_it(
|
|
198
|
+
tmp_path: Path,
|
|
199
|
+
) -> None:
|
|
200
|
+
transcript_path = tmp_path / "s.jsonl"
|
|
201
|
+
write_records(transcript_path, [typed_prompt_record(), *edit_records(), reply_record("Done.")])
|
|
202
|
+
hook_input = {
|
|
203
|
+
"hook_event_name": "Stop",
|
|
204
|
+
"transcript_path": str(transcript_path),
|
|
205
|
+
"last_assistant_message": "Done.",
|
|
206
|
+
"padding": "z" * (MAXIMUM_PAYLOAD_BYTES + 1),
|
|
207
|
+
}
|
|
208
|
+
completed_gate = subprocess.run(
|
|
209
|
+
[sys.executable, str(GATE_PATH)],
|
|
210
|
+
input=json.dumps(hook_input),
|
|
211
|
+
capture_output=True,
|
|
212
|
+
text=True,
|
|
213
|
+
check=False,
|
|
214
|
+
env={**os.environ, "HOME": str(tmp_path), "CLAUDE_CODE_SESSION_ATTENDED": "1"},
|
|
215
|
+
)
|
|
216
|
+
assert (completed_gate.returncode, completed_gate.stdout) == (0, "")
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
def test_should_hold_a_payload_under_the_payload_cap(tmp_path: Path) -> None:
|
|
220
|
+
transcript_path = tmp_path / "s.jsonl"
|
|
221
|
+
write_records(transcript_path, [typed_prompt_record(), *edit_records(), reply_record("Done.")])
|
|
222
|
+
hook_input = {
|
|
223
|
+
"hook_event_name": "Stop",
|
|
224
|
+
"transcript_path": str(transcript_path),
|
|
225
|
+
"last_assistant_message": "Done.",
|
|
226
|
+
}
|
|
227
|
+
completed_gate = subprocess.run(
|
|
228
|
+
[sys.executable, str(GATE_PATH)],
|
|
229
|
+
input="\ufeff" + json.dumps(hook_input),
|
|
230
|
+
capture_output=True,
|
|
231
|
+
text=True,
|
|
232
|
+
check=False,
|
|
233
|
+
env={**os.environ, "HOME": str(tmp_path), "CLAUDE_CODE_SESSION_ATTENDED": "1"},
|
|
234
|
+
)
|
|
235
|
+
assert completed_gate.returncode == 2
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
def test_should_allow_within_ten_seconds_when_one_parse_holds_the_interpreter(
|
|
239
|
+
tmp_path: Path,
|
|
240
|
+
) -> None:
|
|
241
|
+
line_budget = 16 * 1024 * 1024 - 200
|
|
242
|
+
nested_unit = "[" * 500 + "0" + "]" * 500 + ","
|
|
243
|
+
nested_items = (nested_unit * (line_budget // len(nested_unit)))[:-1]
|
|
244
|
+
heavy_line = '{"type":"assistant","turnOrigin":"human","a":[' + nested_items + "]}\n"
|
|
245
|
+
prompt_line = (
|
|
246
|
+
json.dumps({"type": "user", "turnOrigin": "human", "message": {"role": "user", "content": "Go."}})
|
|
247
|
+
+ "\n"
|
|
248
|
+
)
|
|
249
|
+
transcript_path = tmp_path / "s.jsonl"
|
|
250
|
+
transcript_path.write_text(prompt_line + heavy_line * 3, encoding="utf-8")
|
|
251
|
+
hook_input = {"transcript_path": str(transcript_path), "last_assistant_message": "Done."}
|
|
252
|
+
unattended_environment = {**os.environ, "HOME": str(tmp_path)}
|
|
253
|
+
unattended_environment.pop("CLAUDE_CODE_SESSION_ATTENDED", None)
|
|
254
|
+
started_at = time.monotonic()
|
|
255
|
+
completed_gate = subprocess.run(
|
|
256
|
+
[sys.executable, str(GATE_PATH)],
|
|
257
|
+
input=json.dumps(hook_input),
|
|
258
|
+
capture_output=True,
|
|
259
|
+
text=True,
|
|
260
|
+
check=False,
|
|
261
|
+
env=unattended_environment,
|
|
262
|
+
)
|
|
263
|
+
assert completed_gate.returncode in (0, 2)
|
|
264
|
+
assert time.monotonic() - started_at < 10
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
def test_supervised_main_should_allow_with_no_output_when_the_decision_times_out(
|
|
268
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
|
269
|
+
) -> None:
|
|
270
|
+
def timed_out_run(*all_arguments: object, **all_options: object) -> object:
|
|
271
|
+
raise subprocess.TimeoutExpired(cmd="gate", timeout=1)
|
|
272
|
+
|
|
273
|
+
monkeypatch.setattr("sys.stdin", io.TextIOWrapper(io.BytesIO(b"{}")))
|
|
274
|
+
monkeypatch.setattr(closing_question_round_gate.subprocess, "run", timed_out_run)
|
|
275
|
+
assert closing_question_round_gate.supervised_main() == 0
|
|
276
|
+
assert capsys.readouterr().out == ""
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
def test_supervised_main_should_relay_the_decision_output_and_exit_code(
|
|
280
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
|
281
|
+
) -> None:
|
|
282
|
+
def held_run(*all_arguments: object, **all_options: object) -> object:
|
|
283
|
+
return subprocess.CompletedProcess(args=[], returncode=2, stdout=b'{"x": 1}', stderr=b"why")
|
|
284
|
+
|
|
285
|
+
monkeypatch.setattr("sys.stdin", io.TextIOWrapper(io.BytesIO(b"{}")))
|
|
286
|
+
monkeypatch.setattr(closing_question_round_gate.subprocess, "run", held_run)
|
|
287
|
+
assert closing_question_round_gate.supervised_main() == 2
|
|
288
|
+
captured = capsys.readouterr()
|
|
289
|
+
assert (captured.out, captured.err) == ('{"x": 1}', "why")
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
def test_supervised_main_should_allow_when_the_output_cannot_be_written(
|
|
293
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
294
|
+
) -> None:
|
|
295
|
+
def held_run(*all_arguments: object, **all_options: object) -> object:
|
|
296
|
+
return subprocess.CompletedProcess(args=[], returncode=2, stdout=b'{"x": 1}', stderr=b"why")
|
|
297
|
+
|
|
298
|
+
class ClosedStream:
|
|
299
|
+
buffer = io.BufferedWriter(io.BytesIO())
|
|
300
|
+
|
|
301
|
+
def __init__(self) -> None:
|
|
302
|
+
self.buffer.close()
|
|
303
|
+
|
|
304
|
+
monkeypatch.setattr("sys.stdin", io.TextIOWrapper(io.BytesIO(b"{}")))
|
|
305
|
+
monkeypatch.setattr("sys.stdout", ClosedStream())
|
|
306
|
+
monkeypatch.setattr(closing_question_round_gate.subprocess, "run", held_run)
|
|
307
|
+
assert closing_question_round_gate.supervised_main() == 0
|
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
|
|
3
|
+
import pytest
|
|
4
|
+
|
|
5
|
+
from hooks_constants.closing_question_round_gate_constants import (
|
|
6
|
+
CLOSING_ROUND_REASON_LEAD,
|
|
7
|
+
MAXIMUM_SCANNED_BYTES,
|
|
8
|
+
WORK_REASON_LEAD,
|
|
9
|
+
)
|
|
10
|
+
from test_closing_question_round_gate import (
|
|
11
|
+
held_reason,
|
|
12
|
+
machine_prompt_record,
|
|
13
|
+
reply_record,
|
|
14
|
+
run_gate,
|
|
15
|
+
stop_feedback_record,
|
|
16
|
+
tool_call_record,
|
|
17
|
+
tool_result_record,
|
|
18
|
+
typed_prompt_record,
|
|
19
|
+
write_records,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
DECLARATION = "Open items: none."
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@pytest.fixture(autouse=True)
|
|
26
|
+
def scratch_home(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
|
+
WORK_FEEDBACK_TEXT = f"Stop hook feedback:\n{WORK_REASON_LEAD}\nRead the task list."
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def edit_records() -> list[dict[str, object]]:
|
|
36
|
+
return [
|
|
37
|
+
tool_call_record("Edit", {"file_path": "jobs/export.py"}, tool_use_id="id-edit"),
|
|
38
|
+
tool_result_record("Edit", "The file was updated.", tool_use_id="id-edit"),
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def answered_card_records() -> list[dict[str, object]]:
|
|
43
|
+
return [
|
|
44
|
+
tool_call_record("AskUserQuestion", tool_use_id="id-card"),
|
|
45
|
+
tool_result_record("AskUserQuestion", "The user chose Leave it.", tool_use_id="id-card"),
|
|
46
|
+
]
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
@pytest.mark.parametrize("closing_reply", ["Done.", "finished", "Open items: none.\n\nDone."])
|
|
50
|
+
def test_should_hold_work_that_ends_with_no_card_and_no_declaration(
|
|
51
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
52
|
+
capsys: pytest.CaptureFixture[str],
|
|
53
|
+
tmp_path: Path,
|
|
54
|
+
closing_reply: str,
|
|
55
|
+
) -> None:
|
|
56
|
+
all_records = [typed_prompt_record(), *edit_records(), reply_record(closing_reply)]
|
|
57
|
+
reason = held_reason(monkeypatch, capsys, tmp_path, all_records)
|
|
58
|
+
assert str(reason).startswith(WORK_REASON_LEAD)
|
|
59
|
+
assert '"' + closing_reply + '"' not in str(reason)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def test_should_hold_an_agent_call_that_ends_with_finished(
|
|
63
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
64
|
+
) -> None:
|
|
65
|
+
all_records = [
|
|
66
|
+
typed_prompt_record(),
|
|
67
|
+
tool_call_record("Agent", {"prompt": "Check the export job."}),
|
|
68
|
+
tool_result_record("Agent", "The export job is fine."),
|
|
69
|
+
reply_record("finished"),
|
|
70
|
+
]
|
|
71
|
+
assert str(held_reason(monkeypatch, capsys, tmp_path, all_records)).startswith(WORK_REASON_LEAD)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
@pytest.mark.parametrize(
|
|
75
|
+
"closing_reply",
|
|
76
|
+
[
|
|
77
|
+
f"The retry flag is in place.\n\n{DECLARATION}",
|
|
78
|
+
f"The retry flag is in place.\n{DECLARATION}",
|
|
79
|
+
f"The retry flag is in place.\n\n**{DECLARATION}**",
|
|
80
|
+
f"The retry flag is in place.\n\n_{DECLARATION}_\n\n",
|
|
81
|
+
],
|
|
82
|
+
)
|
|
83
|
+
def test_should_allow_work_that_ends_with_the_declaration(
|
|
84
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
85
|
+
capsys: pytest.CaptureFixture[str],
|
|
86
|
+
tmp_path: Path,
|
|
87
|
+
closing_reply: str,
|
|
88
|
+
) -> None:
|
|
89
|
+
all_records = [typed_prompt_record(), *edit_records(), reply_record(closing_reply)]
|
|
90
|
+
assert held_reason(monkeypatch, capsys, tmp_path, all_records) is None
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
@pytest.mark.parametrize(
|
|
94
|
+
"closing_reply",
|
|
95
|
+
[
|
|
96
|
+
f"`{DECLARATION}`",
|
|
97
|
+
f"- {DECLARATION}",
|
|
98
|
+
"Open items: none yet",
|
|
99
|
+
f"{DECLARATION} Both tests pass.",
|
|
100
|
+
f"> {DECLARATION}",
|
|
101
|
+
f"```\n{DECLARATION}\n```",
|
|
102
|
+
f"Done.\n\n {DECLARATION}",
|
|
103
|
+
],
|
|
104
|
+
)
|
|
105
|
+
def test_should_hold_work_whose_last_line_is_no_plain_declaration(
|
|
106
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
107
|
+
capsys: pytest.CaptureFixture[str],
|
|
108
|
+
tmp_path: Path,
|
|
109
|
+
closing_reply: str,
|
|
110
|
+
) -> None:
|
|
111
|
+
all_records = [typed_prompt_record(), *edit_records(), reply_record(closing_reply)]
|
|
112
|
+
assert str(held_reason(monkeypatch, capsys, tmp_path, all_records)).startswith(WORK_REASON_LEAD)
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def test_should_hold_a_marker_line_beside_the_declaration(
|
|
116
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
117
|
+
) -> None:
|
|
118
|
+
closing_reply = f"The retry path is untested.\n\n{DECLARATION}"
|
|
119
|
+
all_records = [typed_prompt_record(), *edit_records(), reply_record(closing_reply)]
|
|
120
|
+
reason = str(held_reason(monkeypatch, capsys, tmp_path, all_records))
|
|
121
|
+
assert reason.startswith(CLOSING_ROUND_REASON_LEAD)
|
|
122
|
+
assert '"The retry path is untested."' in reason
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def test_should_read_the_declaration_from_the_payload_closing_reply(
|
|
126
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
127
|
+
) -> None:
|
|
128
|
+
all_records = [typed_prompt_record(), *edit_records(), reply_record("The flag is in place.")]
|
|
129
|
+
closing_reply = f"The flag is in place.\n\n{DECLARATION}"
|
|
130
|
+
assert (
|
|
131
|
+
held_reason(
|
|
132
|
+
monkeypatch, capsys, tmp_path, all_records, {"last_assistant_message": closing_reply}
|
|
133
|
+
)
|
|
134
|
+
is None
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
@pytest.mark.parametrize(
|
|
139
|
+
"closing_reply", ["I made no changes for either answer.", "The backoff setting stays as it is."]
|
|
140
|
+
)
|
|
141
|
+
def test_should_allow_an_answered_card_followed_only_by_prose(
|
|
142
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
143
|
+
capsys: pytest.CaptureFixture[str],
|
|
144
|
+
tmp_path: Path,
|
|
145
|
+
closing_reply: str,
|
|
146
|
+
) -> None:
|
|
147
|
+
all_records = [
|
|
148
|
+
typed_prompt_record(),
|
|
149
|
+
*edit_records(),
|
|
150
|
+
reply_record("Should the backoff setting change?"),
|
|
151
|
+
*answered_card_records(),
|
|
152
|
+
reply_record(closing_reply),
|
|
153
|
+
]
|
|
154
|
+
assert held_reason(monkeypatch, capsys, tmp_path, all_records) is None
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def test_should_hold_work_after_an_answered_card_until_the_declaration(
|
|
158
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
159
|
+
) -> None:
|
|
160
|
+
all_records = [
|
|
161
|
+
typed_prompt_record(),
|
|
162
|
+
reply_record(DECLARATION),
|
|
163
|
+
*answered_card_records(),
|
|
164
|
+
*edit_records(),
|
|
165
|
+
reply_record("The backoff setting is in place."),
|
|
166
|
+
]
|
|
167
|
+
assert str(held_reason(monkeypatch, capsys, tmp_path, all_records)).startswith(WORK_REASON_LEAD)
|
|
168
|
+
all_records[-1] = reply_record(f"The backoff setting is in place.\n\n{DECLARATION}")
|
|
169
|
+
assert held_reason(monkeypatch, capsys, tmp_path, all_records) is None
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def test_should_hold_a_chat_only_turn_after_an_earlier_turn_did_work(
|
|
173
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
174
|
+
) -> None:
|
|
175
|
+
all_records = [
|
|
176
|
+
typed_prompt_record(),
|
|
177
|
+
*edit_records(),
|
|
178
|
+
reply_record("The flag is in place."),
|
|
179
|
+
typed_prompt_record("Thanks."),
|
|
180
|
+
reply_record("You're welcome."),
|
|
181
|
+
]
|
|
182
|
+
assert str(held_reason(monkeypatch, capsys, tmp_path, all_records)).startswith(WORK_REASON_LEAD)
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
@pytest.mark.parametrize(
|
|
186
|
+
"all_extra_fields",
|
|
187
|
+
[
|
|
188
|
+
{"agent_id": "agent-1"},
|
|
189
|
+
{"conversation_id": "conversation-1"},
|
|
190
|
+
{"background_tasks": [{"id": "task-1"}]},
|
|
191
|
+
],
|
|
192
|
+
)
|
|
193
|
+
def test_should_allow_work_in_an_exempt_stop(
|
|
194
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
195
|
+
capsys: pytest.CaptureFixture[str],
|
|
196
|
+
tmp_path: Path,
|
|
197
|
+
all_extra_fields: dict[str, object],
|
|
198
|
+
) -> None:
|
|
199
|
+
transcript_path = write_records(
|
|
200
|
+
tmp_path / "s.jsonl", [typed_prompt_record(), *edit_records(), reply_record("Done.")]
|
|
201
|
+
)
|
|
202
|
+
assert run_gate(monkeypatch, capsys, transcript_path, all_extra_fields) == {}
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def test_should_allow_work_in_a_headless_run_or_a_session_no_person_typed_into(
|
|
206
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
207
|
+
) -> None:
|
|
208
|
+
all_machine_records = [machine_prompt_record("sdk"), *edit_records(), reply_record("Done.")]
|
|
209
|
+
assert held_reason(monkeypatch, capsys, tmp_path, all_machine_records) is None
|
|
210
|
+
monkeypatch.setenv("CLAUDE_CODE_SESSION_ATTENDED", "0")
|
|
211
|
+
all_typed_records = [typed_prompt_record(), *edit_records(), reply_record("Done.")]
|
|
212
|
+
assert held_reason(monkeypatch, capsys, tmp_path, all_typed_records) is None
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
def test_should_count_both_reasons_toward_the_cap_of_two(
|
|
216
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
217
|
+
) -> None:
|
|
218
|
+
all_records = [
|
|
219
|
+
typed_prompt_record(),
|
|
220
|
+
*edit_records(),
|
|
221
|
+
reply_record("Done."),
|
|
222
|
+
stop_feedback_record(WORK_FEEDBACK_TEXT),
|
|
223
|
+
reply_record("The jitter option is parked."),
|
|
224
|
+
]
|
|
225
|
+
assert held_reason(monkeypatch, capsys, tmp_path, all_records) is not None
|
|
226
|
+
all_records += [stop_feedback_record(), reply_record("Done again.")]
|
|
227
|
+
assert held_reason(monkeypatch, capsys, tmp_path, all_records) is None
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
def test_should_reset_the_cap_on_a_new_typed_prompt(
|
|
231
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
232
|
+
) -> None:
|
|
233
|
+
all_records = [
|
|
234
|
+
typed_prompt_record(),
|
|
235
|
+
*edit_records(),
|
|
236
|
+
reply_record("Done."),
|
|
237
|
+
stop_feedback_record(WORK_FEEDBACK_TEXT),
|
|
238
|
+
stop_feedback_record(WORK_FEEDBACK_TEXT),
|
|
239
|
+
reply_record("Done again."),
|
|
240
|
+
]
|
|
241
|
+
assert held_reason(monkeypatch, capsys, tmp_path, all_records) is None
|
|
242
|
+
all_records += [typed_prompt_record("Rename the flag."), reply_record("Renamed.")]
|
|
243
|
+
assert str(held_reason(monkeypatch, capsys, tmp_path, all_records)).startswith(WORK_REASON_LEAD)
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
def test_should_not_reset_the_cap_on_a_notification_prompt(
|
|
247
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
248
|
+
) -> None:
|
|
249
|
+
all_records = [
|
|
250
|
+
typed_prompt_record(),
|
|
251
|
+
*edit_records(),
|
|
252
|
+
reply_record("Done."),
|
|
253
|
+
stop_feedback_record(WORK_FEEDBACK_TEXT),
|
|
254
|
+
machine_prompt_record("task_notification", "<task-notification>done</task-notification>"),
|
|
255
|
+
stop_feedback_record(WORK_FEEDBACK_TEXT),
|
|
256
|
+
reply_record("Done again."),
|
|
257
|
+
]
|
|
258
|
+
assert held_reason(monkeypatch, capsys, tmp_path, all_records) is None
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
def test_should_reset_the_cap_on_a_prompt_of_a_host_with_no_turn_origin(
|
|
262
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
263
|
+
) -> None:
|
|
264
|
+
untagged_prompt = {"type": "user", "message": {"role": "user", "content": "Add the flag."}}
|
|
265
|
+
all_records = [
|
|
266
|
+
untagged_prompt,
|
|
267
|
+
*edit_records(),
|
|
268
|
+
stop_feedback_record(WORK_FEEDBACK_TEXT),
|
|
269
|
+
stop_feedback_record(WORK_FEEDBACK_TEXT),
|
|
270
|
+
reply_record("Done."),
|
|
271
|
+
]
|
|
272
|
+
assert held_reason(monkeypatch, capsys, tmp_path, all_records) is None
|
|
273
|
+
all_records += [untagged_prompt, reply_record("Done again.")]
|
|
274
|
+
assert str(held_reason(monkeypatch, capsys, tmp_path, all_records)).startswith(WORK_REASON_LEAD)
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
def test_should_hold_a_marker_in_an_indented_list_continuation(
|
|
278
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
279
|
+
) -> None:
|
|
280
|
+
closing_reply = "- The flag is in place.\n\n - The retry path is untested."
|
|
281
|
+
reason = held_reason(
|
|
282
|
+
monkeypatch, capsys, tmp_path, [typed_prompt_record(), reply_record(closing_reply)]
|
|
283
|
+
)
|
|
284
|
+
assert '"- The retry path is untested."' in str(reason)
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
def test_should_skip_a_transcript_line_and_a_closing_reply_over_sixteen_mebibytes(
|
|
288
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
289
|
+
) -> None:
|
|
290
|
+
oversized_text = "The retry path is untested. " + "x" * MAXIMUM_SCANNED_BYTES
|
|
291
|
+
all_records = [typed_prompt_record(), reply_record(oversized_text)]
|
|
292
|
+
assert held_reason(monkeypatch, capsys, tmp_path, all_records) is None
|
|
293
|
+
all_records = [typed_prompt_record(), reply_record("The flag is in place.")]
|
|
294
|
+
assert (
|
|
295
|
+
held_reason(
|
|
296
|
+
monkeypatch, capsys, tmp_path, all_records, {"last_assistant_message": oversized_text}
|
|
297
|
+
)
|
|
298
|
+
is None
|
|
299
|
+
)
|
|
300
|
+
all_records = [
|
|
301
|
+
typed_prompt_record(),
|
|
302
|
+
reply_record(oversized_text),
|
|
303
|
+
reply_record("The retry path is untested."),
|
|
304
|
+
]
|
|
305
|
+
assert '"The retry path is untested."' in str(
|
|
306
|
+
held_reason(monkeypatch, capsys, tmp_path, all_records)
|
|
307
|
+
)
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
def test_should_allow_a_chat_only_turn_after_a_work_turn_that_declared(
|
|
311
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
312
|
+
) -> None:
|
|
313
|
+
all_records = [
|
|
314
|
+
typed_prompt_record(),
|
|
315
|
+
*edit_records(),
|
|
316
|
+
reply_record(f"The retry flag is in place.\n\n{DECLARATION}"),
|
|
317
|
+
typed_prompt_record(),
|
|
318
|
+
reply_record("Yes, the flag defaults to three tries."),
|
|
319
|
+
]
|
|
320
|
+
assert held_reason(monkeypatch, capsys, tmp_path, all_records) is None
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
def test_should_hold_new_work_after_a_declared_turn(
|
|
324
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
325
|
+
) -> None:
|
|
326
|
+
all_records = [
|
|
327
|
+
typed_prompt_record(),
|
|
328
|
+
*edit_records(),
|
|
329
|
+
reply_record(f"The retry flag is in place.\n\n{DECLARATION}"),
|
|
330
|
+
typed_prompt_record(),
|
|
331
|
+
*edit_records(),
|
|
332
|
+
reply_record("The default is now five tries."),
|
|
333
|
+
]
|
|
334
|
+
assert str(held_reason(monkeypatch, capsys, tmp_path, all_records)).startswith(WORK_REASON_LEAD)
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
def test_should_keep_a_marker_line_written_before_a_declaration(
|
|
338
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
339
|
+
) -> None:
|
|
340
|
+
all_records = [
|
|
341
|
+
typed_prompt_record(),
|
|
342
|
+
*edit_records(),
|
|
343
|
+
reply_record(f"The retry path is untested.\n\n{DECLARATION}"),
|
|
344
|
+
typed_prompt_record(),
|
|
345
|
+
reply_record("Yes."),
|
|
346
|
+
]
|
|
347
|
+
assert '"The retry path is untested."' in str(
|
|
348
|
+
held_reason(monkeypatch, capsys, tmp_path, all_records)
|
|
349
|
+
)
|