claude-dev-env 8.121.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/bin/ever-registered-hooks.mjs +1 -0
- package/docs/rule-guides/question-presentation.md +4 -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/sent_file_question_detector.py +340 -0
- package/hooks/blocking/sent_file_question_gate.py +307 -0
- package/hooks/blocking/sent_file_scan.py +209 -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_sent_file_question_detector.py +68 -0
- package/hooks/blocking/test_sent_file_question_gate.py +688 -0
- package/hooks/blocking/test_sent_file_scan.py +71 -0
- package/hooks/blocking/test_user_step_wording.py +46 -0
- package/hooks/blocking/user_step_wording.py +113 -15
- package/hooks/features/conduct-gates.md +3 -0
- package/hooks/hooks.json +10 -0
- package/hooks/hooks_constants/closing_question_round_gate_constants.py +167 -0
- package/hooks/hooks_constants/sent_file_question_gate_constants.py +261 -0
- package/hooks/hooks_constants/test_closing_question_round_gate_constants.py +92 -0
- package/hooks/hooks_constants/user_step_wording_constants.py +4 -0
- package/package.json +1 -1
- package/scripts/policy_lint/adapter_configuration.py +5 -0
- package/scripts/policy_lint/config/constants.py +1 -0
- package/scripts/tests/test_adapter_configuration.py +8 -0
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import json
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
|
|
4
|
+
import pytest
|
|
5
|
+
|
|
6
|
+
from closing_question_round_lines import before_deadline, is_within_scan_cap, transcript_lines
|
|
7
|
+
from hooks_constants.closing_question_round_gate_constants import (
|
|
8
|
+
MAXIMUM_SCANNED_BYTES,
|
|
9
|
+
MAXIMUM_SCANNED_TEXT_BYTES,
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
NO_DEADLINE = float("inf")
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def test_should_pass_each_text_through_before_the_deadline_and_raise_after_it() -> None:
|
|
17
|
+
assert list(before_deadline(["one", "two"], NO_DEADLINE)) == ["one", "two"]
|
|
18
|
+
with pytest.raises(TimeoutError):
|
|
19
|
+
list(before_deadline(["one"], 0.0))
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def test_should_split_a_readable_transcript_into_lines_and_return_an_empty_list_otherwise(
|
|
23
|
+
tmp_path: Path,
|
|
24
|
+
) -> None:
|
|
25
|
+
transcript_path = tmp_path / "s.jsonl"
|
|
26
|
+
transcript_path.write_text('{"a": 1}\n{"b": 2}', encoding="utf-8")
|
|
27
|
+
assert transcript_lines(str(transcript_path), NO_DEADLINE) == ['{"a": 1}', '{"b": 2}']
|
|
28
|
+
assert transcript_lines(str(tmp_path / "missing.jsonl"), NO_DEADLINE) == []
|
|
29
|
+
assert transcript_lines(None, NO_DEADLINE) == []
|
|
30
|
+
with pytest.raises(TimeoutError):
|
|
31
|
+
transcript_lines(str(transcript_path), 0.0)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def test_transcript_lines_reads_a_line_over_sixteen_mebibytes_as_an_empty_line(
|
|
35
|
+
tmp_path: Path,
|
|
36
|
+
) -> None:
|
|
37
|
+
transcript_path = tmp_path / "s.jsonl"
|
|
38
|
+
oversized_line = "x" * (MAXIMUM_SCANNED_BYTES + 1)
|
|
39
|
+
transcript_path.write_text(f'{{"a": 1}}\n{oversized_line}\n{{"b": 2}}', encoding="utf-8")
|
|
40
|
+
assert transcript_lines(str(transcript_path), NO_DEADLINE) == ['{"a": 1}', "", '{"b": 2}']
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def test_is_within_scan_cap_counts_utf8_bytes() -> None:
|
|
44
|
+
assert is_within_scan_cap("x" * MAXIMUM_SCANNED_TEXT_BYTES)
|
|
45
|
+
assert not is_within_scan_cap("x" * (MAXIMUM_SCANNED_TEXT_BYTES + 1))
|
|
46
|
+
assert not is_within_scan_cap("é" * (MAXIMUM_SCANNED_TEXT_BYTES // 2 + 1))
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def test_transcript_lines_should_spell_out_json_escapes_and_drop_a_byte_order_mark(
|
|
50
|
+
tmp_path: Path,
|
|
51
|
+
) -> None:
|
|
52
|
+
transcript_path = tmp_path / "s.jsonl"
|
|
53
|
+
transcript_path.write_bytes(
|
|
54
|
+
b'\xef\xbb\xbf{"type": "user"}\n{"\\u0074urnOrigin": "sdk", "text": "caf\\u00e9"}\n{"a": "\\u0062"'
|
|
55
|
+
)
|
|
56
|
+
all_lines = transcript_lines(str(transcript_path), NO_DEADLINE)
|
|
57
|
+
assert all_lines[0] == '{"type": "user"}'
|
|
58
|
+
assert json.loads(all_lines[1]) == {"turnOrigin": "sdk", "text": "caf\u00e9"}
|
|
59
|
+
assert '"turnOrigin"' in all_lines[1]
|
|
60
|
+
assert all_lines[2] == '{"a": "\\u0062"'
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def test_transcript_lines_should_keep_a_line_whose_only_escapes_are_control_characters(
|
|
64
|
+
tmp_path: Path,
|
|
65
|
+
) -> None:
|
|
66
|
+
transcript_path = tmp_path / "s.jsonl"
|
|
67
|
+
transcript_path.write_bytes(b'{"content":"\\u001b[31mred\\u001b[0m"}')
|
|
68
|
+
assert transcript_lines(str(transcript_path), NO_DEADLINE) == [
|
|
69
|
+
'{"content":"\\u001b[31mred\\u001b[0m"}'
|
|
70
|
+
]
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
|
|
3
|
+
from closing_question_round_open_items import (
|
|
4
|
+
declares_no_open_items,
|
|
5
|
+
indented_code_masked,
|
|
6
|
+
marked_open_lines,
|
|
7
|
+
)
|
|
8
|
+
from hooks_constants.closing_question_round_gate_constants import (
|
|
9
|
+
MARKER_SCAN_WINDOW_CHARACTERS,
|
|
10
|
+
MAXIMUM_SCANNED_BYTES,
|
|
11
|
+
MAXIMUM_SCANNED_TEXT_BYTES,
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
NO_DEADLINE = float("inf")
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def test_marked_open_lines_returns_each_marked_line_once_in_order() -> None:
|
|
18
|
+
all_assistant_texts = [
|
|
19
|
+
"The flag is in place.\nThe retry path is untested.\n\n- The cleanup is parked.",
|
|
20
|
+
"The cleanup is out of\nscope for now.\nBoth tests pass.",
|
|
21
|
+
"The retry path is untested.",
|
|
22
|
+
]
|
|
23
|
+
assert marked_open_lines(all_assistant_texts, NO_DEADLINE) == [
|
|
24
|
+
"The retry path is untested.",
|
|
25
|
+
"- The cleanup is parked.",
|
|
26
|
+
"The cleanup is out of scope for now.",
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@pytest.mark.parametrize(
|
|
31
|
+
"negated_line",
|
|
32
|
+
[
|
|
33
|
+
"No follow-up is needed.",
|
|
34
|
+
"No open questions remain.",
|
|
35
|
+
"Nothing deferred, except the backoff setting.",
|
|
36
|
+
"Nothing is deferred.",
|
|
37
|
+
],
|
|
38
|
+
)
|
|
39
|
+
def test_marked_open_lines_should_keep_a_negated_marker(negated_line: str) -> None:
|
|
40
|
+
assert marked_open_lines([negated_line], NO_DEADLINE) == [negated_line]
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def test_marked_open_lines_should_find_a_marker_that_spans_a_scan_window_edge() -> None:
|
|
44
|
+
straddling_text = "w" * (MARKER_SCAN_WINDOW_CHARACTERS - 4) + " untested\nBoth tests pass."
|
|
45
|
+
assert marked_open_lines([straddling_text], NO_DEADLINE) == [straddling_text.split("\n")[0]]
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def test_marked_open_lines_should_skip_a_word_cut_at_a_scan_window_edge() -> None:
|
|
49
|
+
cut_text = "w" * (MARKER_SCAN_WINDOW_CHARACTERS + 128 - 6) + " deference is kept."
|
|
50
|
+
assert marked_open_lines([cut_text], NO_DEADLINE) == []
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def test_marked_open_lines_should_skip_a_text_over_sixteen_mebibytes() -> None:
|
|
54
|
+
oversized_text = "The retry path is untested. " + "x" * MAXIMUM_SCANNED_BYTES
|
|
55
|
+
assert marked_open_lines([oversized_text, "The cleanup is parked."], NO_DEADLINE) == [
|
|
56
|
+
"The cleanup is parked."
|
|
57
|
+
]
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def test_marked_open_lines_should_raise_once_the_deadline_passes() -> None:
|
|
61
|
+
with pytest.raises(TimeoutError):
|
|
62
|
+
marked_open_lines(["The cleanup is parked."], 0.0)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
@pytest.mark.parametrize(
|
|
66
|
+
("reply_text", "masked_text"),
|
|
67
|
+
[
|
|
68
|
+
("Run this:\n\n queue --deferred\n\nDone.", "Run this:\n\n\n\nDone."),
|
|
69
|
+
("\tqueue --deferred\nDone.", "\nDone."),
|
|
70
|
+
("- Item\n\n The retry is untested.", "- Item\n\n The retry is untested."),
|
|
71
|
+
("One line\n more deferred text", "One line\n more deferred text"),
|
|
72
|
+
],
|
|
73
|
+
)
|
|
74
|
+
def test_indented_code_masked_blanks_only_an_indented_code_block(
|
|
75
|
+
reply_text: str, masked_text: str
|
|
76
|
+
) -> None:
|
|
77
|
+
assert indented_code_masked(reply_text, NO_DEADLINE) == masked_text
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
@pytest.mark.parametrize(
|
|
81
|
+
("closing_reply", "is_declaration"),
|
|
82
|
+
[
|
|
83
|
+
("Both tests pass.\n\nOpen items: none.", True),
|
|
84
|
+
("Both tests pass.\n\n**Open items: none.**", True),
|
|
85
|
+
("Open items: none.\n\nBoth tests pass.", False),
|
|
86
|
+
("`Open items: none.`", False),
|
|
87
|
+
("- Open items: none.", False),
|
|
88
|
+
("Open items: none", True),
|
|
89
|
+
("Done.\n\n_Open items: none_", True),
|
|
90
|
+
("Open items: none yet", False),
|
|
91
|
+
("", False),
|
|
92
|
+
("Open items: none." + "x" * MAXIMUM_SCANNED_BYTES + "\nOpen items: none.", False),
|
|
93
|
+
],
|
|
94
|
+
)
|
|
95
|
+
def test_declares_no_open_items_reads_the_last_prose_line(
|
|
96
|
+
closing_reply: str, is_declaration: bool
|
|
97
|
+
) -> None:
|
|
98
|
+
assert declares_no_open_items(closing_reply, NO_DEADLINE) is is_declaration
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
@pytest.mark.parametrize("gap", [" " * 300, "\n" + " " * 300])
|
|
102
|
+
def test_marked_open_lines_should_find_a_marker_whose_long_gap_straddles_a_window_edge(
|
|
103
|
+
gap: str,
|
|
104
|
+
) -> None:
|
|
105
|
+
marker_text = f"out{gap}of scope here."
|
|
106
|
+
leading_text = "a" * (MARKER_SCAN_WINDOW_CHARACTERS - 150) + " "
|
|
107
|
+
all_lines = marked_open_lines([leading_text + marker_text], NO_DEADLINE)
|
|
108
|
+
assert len(all_lines) == 1
|
|
109
|
+
assert all_lines[0].endswith("of scope here.")
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def test_marked_open_lines_should_skip_a_text_over_the_text_cap() -> None:
|
|
113
|
+
oversized_text = "The retry path is untested.\n" + "a" * (MAXIMUM_SCANNED_TEXT_BYTES + 1)
|
|
114
|
+
assert marked_open_lines([oversized_text], NO_DEADLINE) == []
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
@pytest.mark.parametrize(
|
|
118
|
+
"emphasized_line",
|
|
119
|
+
["The retry is _untested_.", "The cleanup is __deferred__.", "The flag is _parked_ for now."],
|
|
120
|
+
)
|
|
121
|
+
def test_marked_open_lines_should_read_a_marker_inside_underscore_emphasis(
|
|
122
|
+
emphasized_line: str,
|
|
123
|
+
) -> None:
|
|
124
|
+
assert marked_open_lines([emphasized_line], NO_DEADLINE) == [emphasized_line]
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
@pytest.mark.parametrize(
|
|
128
|
+
"snake_case_line", ["The retry_untested_flag is set.", "The retry__untested__flag is set."]
|
|
129
|
+
)
|
|
130
|
+
def test_marked_open_lines_should_not_read_a_marker_inside_a_snake_case_name(
|
|
131
|
+
snake_case_line: str,
|
|
132
|
+
) -> None:
|
|
133
|
+
assert marked_open_lines([snake_case_line], NO_DEADLINE) == []
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
@pytest.mark.parametrize(
|
|
137
|
+
"path_line",
|
|
138
|
+
[
|
|
139
|
+
"I'll create todo.md and check the file.",
|
|
140
|
+
"The notes live in docs/pending/notes.md now.",
|
|
141
|
+
"Run hooks\\blocked\\list.py once.",
|
|
142
|
+
"The steps are in notes/todo.md, as before.",
|
|
143
|
+
"The list (see todo.md) is short.",
|
|
144
|
+
"Open https://example.com/docs/pending/ to read it.",
|
|
145
|
+
"Read **todo.md** first.",
|
|
146
|
+
"Read \u2039todo.md\u203a first.",
|
|
147
|
+
"The steps are in a/" + "b" * 90 + "/todo.md now.",
|
|
148
|
+
],
|
|
149
|
+
)
|
|
150
|
+
def test_marked_open_lines_should_not_read_a_marker_inside_a_file_name_or_path(
|
|
151
|
+
path_line: str,
|
|
152
|
+
) -> None:
|
|
153
|
+
assert marked_open_lines([path_line], NO_DEADLINE) == []
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
@pytest.mark.parametrize(
|
|
157
|
+
"marker_line",
|
|
158
|
+
[
|
|
159
|
+
"The retry is pending.",
|
|
160
|
+
"That stays a todo. Ship it now.",
|
|
161
|
+
"Left as a todo.",
|
|
162
|
+
"The tests are skipped/untested.",
|
|
163
|
+
"Status is pending/blocked on CI.",
|
|
164
|
+
"TODO/FIXME remain.",
|
|
165
|
+
"The check is untested\\unverified.",
|
|
166
|
+
"Ship it, a TODO.Ship it.",
|
|
167
|
+
"Step 1.untested",
|
|
168
|
+
"Use e.g.pending items.",
|
|
169
|
+
"Use e.g.todo items.",
|
|
170
|
+
"One item stays pending/ for now.",
|
|
171
|
+
"Linux passes. Windows/not tested/.",
|
|
172
|
+
"The parser/not covered/ cases stay as they are.",
|
|
173
|
+
"It is not covered/x.md yet.",
|
|
174
|
+
],
|
|
175
|
+
)
|
|
176
|
+
def test_marked_open_lines_should_read_a_marker_before_a_full_stop(marker_line: str) -> None:
|
|
177
|
+
assert marked_open_lines([marker_line], NO_DEADLINE) == [marker_line]
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
@pytest.mark.parametrize(
|
|
181
|
+
"long_path_line",
|
|
182
|
+
[
|
|
183
|
+
"Read " + "a" * 40 + "-" * 56 + "/todo.md now.",
|
|
184
|
+
"Read todo/" + "x" * 40 + "-" * 53 + ".md now.",
|
|
185
|
+
"Read docs/" + "-" * 200 + "/todo.md now.",
|
|
186
|
+
],
|
|
187
|
+
)
|
|
188
|
+
def test_marked_open_lines_should_count_each_hyphen_of_a_path_toward_the_reach(
|
|
189
|
+
long_path_line: str,
|
|
190
|
+
) -> None:
|
|
191
|
+
assert marked_open_lines([long_path_line], NO_DEADLINE) == [long_path_line]
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def test_marked_open_lines_should_not_read_a_snake_case_name_whose_underscore_ends_a_window() -> None:
|
|
195
|
+
window_end = MARKER_SCAN_WINDOW_CHARACTERS + 128
|
|
196
|
+
edge_text = "w" * (window_end - len(" deferred") - 1) + " deferred_flag is set."
|
|
197
|
+
assert edge_text.index("_") == window_end - 1
|
|
198
|
+
assert marked_open_lines([edge_text], NO_DEADLINE) == []
|
|
199
|
+
assert marked_open_lines([edge_text.replace("_flag", " flag")], NO_DEADLINE) != []
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def test_marked_open_lines_should_find_a_window_edge_marker_followed_only_by_hyphens() -> None:
|
|
203
|
+
leading_text = "x" * (MARKER_SCAN_WINDOW_CHARACTERS - 10)
|
|
204
|
+
all_lines = marked_open_lines([leading_text + " out of" + " " * 20 + "scope---"], NO_DEADLINE)
|
|
205
|
+
assert len(all_lines) == 1
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
@pytest.mark.parametrize("list_marker", ["1.", "-"])
|
|
209
|
+
def test_marked_open_lines_should_read_an_indented_paragraph_inside_a_wrapped_list_item(
|
|
210
|
+
list_marker: str,
|
|
211
|
+
) -> None:
|
|
212
|
+
reply_text = (
|
|
213
|
+
f"{list_marker} Retry path\n covers the timeout case.\n\n"
|
|
214
|
+
" I deferred the backoff setting.\n\nOpen items: none."
|
|
215
|
+
)
|
|
216
|
+
assert marked_open_lines([reply_text], NO_DEADLINE) == ["I deferred the backoff setting."]
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
def test_indented_code_masked_blanks_an_indented_block_after_a_list_ends() -> None:
|
|
220
|
+
reply_text = "- Retry path\n\nThe flag is set.\n\n deferred = True"
|
|
221
|
+
assert indented_code_masked(reply_text, NO_DEADLINE).split("\n")[-1] == ""
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
@pytest.mark.parametrize("tail", ["-", "------", "\n---", ""])
|
|
225
|
+
def test_marked_open_lines_should_find_a_marker_split_by_a_whitespace_run_past_a_window(
|
|
226
|
+
tail: str,
|
|
227
|
+
) -> None:
|
|
228
|
+
marker_text = "The cleanup is out" + " " * 1_100_000 + "of scope" + tail
|
|
229
|
+
all_lines = marked_open_lines([marker_text], NO_DEADLINE)
|
|
230
|
+
assert len(all_lines) == 1
|
|
231
|
+
assert all_lines[0].startswith("The cleanup is out")
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def test_marked_open_lines_should_quote_the_lines_of_a_marker_beside_long_space_runs() -> None:
|
|
235
|
+
reply_text = "The flag is set.\nThe cleanup is out of\nscope now."
|
|
236
|
+
assert marked_open_lines([reply_text], NO_DEADLINE) == [
|
|
237
|
+
"The cleanup is out of scope now."
|
|
238
|
+
]
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import json
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
|
|
4
|
+
import pytest
|
|
5
|
+
|
|
6
|
+
from closing_question_round_transcript import (
|
|
7
|
+
assistant_texts,
|
|
8
|
+
block_texts,
|
|
9
|
+
closing_reply_texts,
|
|
10
|
+
has_work_call,
|
|
11
|
+
last_typed_prompt_position,
|
|
12
|
+
stretch_start,
|
|
13
|
+
top_level_records,
|
|
14
|
+
)
|
|
15
|
+
from hooks_constants.closing_question_round_gate_constants import (
|
|
16
|
+
WORK_REASON_LEAD,
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
NO_DEADLINE = float("inf")
|
|
20
|
+
DEFERRAL_LINE = "I deferred the backoff setting to a later change."
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@pytest.fixture(autouse=True)
|
|
24
|
+
def interactive_session(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
25
|
+
monkeypatch.setenv("CLAUDE_CODE_SESSION_ATTENDED", "1")
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def typed_prompt_record(
|
|
29
|
+
prompt_text: str = "Add the retry flag to the export job.",
|
|
30
|
+
) -> dict[str, object]:
|
|
31
|
+
return {
|
|
32
|
+
"type": "user",
|
|
33
|
+
"turnOrigin": "human",
|
|
34
|
+
"promptSource": "typed",
|
|
35
|
+
"message": {"role": "user", "content": prompt_text},
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def machine_prompt_record(turn_origin: str) -> dict[str, object]:
|
|
40
|
+
return {
|
|
41
|
+
"type": "user",
|
|
42
|
+
"turnOrigin": turn_origin,
|
|
43
|
+
"promptSource": "system",
|
|
44
|
+
"message": {"role": "user", "content": "Add the retry flag."},
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def reply_record(reply_text: str) -> dict[str, object]:
|
|
49
|
+
return {
|
|
50
|
+
"type": "assistant",
|
|
51
|
+
"message": {"role": "assistant", "content": [{"type": "text", "text": reply_text}]},
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def tool_call_record(tool_name: object, tool_use_id: str | None = None) -> dict[str, object]:
|
|
56
|
+
tool_use = {
|
|
57
|
+
"type": "tool_use",
|
|
58
|
+
"id": tool_use_id or f"id-{tool_name}",
|
|
59
|
+
"name": tool_name,
|
|
60
|
+
"input": {},
|
|
61
|
+
}
|
|
62
|
+
return {"type": "assistant", "message": {"role": "assistant", "content": [tool_use]}}
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def tool_result_record(
|
|
66
|
+
tool_name: str, result_text: str, tool_use_id: str | None = None
|
|
67
|
+
) -> dict[str, object]:
|
|
68
|
+
tool_result = {
|
|
69
|
+
"type": "tool_result",
|
|
70
|
+
"tool_use_id": tool_use_id or f"id-{tool_name}",
|
|
71
|
+
"content": result_text,
|
|
72
|
+
"is_error": False,
|
|
73
|
+
}
|
|
74
|
+
return {"type": "user", "message": {"role": "user", "content": [tool_result]}}
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def json_lines(all_records: list[dict[str, object]]) -> list[str]:
|
|
78
|
+
return [json.dumps(each_record) for each_record in all_records]
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def test_last_typed_prompt_position_returns_the_last_prompt_with_a_human_turn_origin() -> None:
|
|
82
|
+
all_lines = json_lines(
|
|
83
|
+
[
|
|
84
|
+
machine_prompt_record("sdk"),
|
|
85
|
+
typed_prompt_record(),
|
|
86
|
+
reply_record("One."),
|
|
87
|
+
typed_prompt_record("Rename it."),
|
|
88
|
+
machine_prompt_record("task_notification"),
|
|
89
|
+
]
|
|
90
|
+
)
|
|
91
|
+
assert last_typed_prompt_position(all_lines, NO_DEADLINE) == 3
|
|
92
|
+
machine_lines = json_lines([machine_prompt_record("sdk")])
|
|
93
|
+
assert last_typed_prompt_position(machine_lines, NO_DEADLINE) is None
|
|
94
|
+
meta_lines = json_lines([{**typed_prompt_record(), "isMeta": True}])
|
|
95
|
+
assert last_typed_prompt_position(meta_lines, NO_DEADLINE) is None
|
|
96
|
+
human_tool_result = {**tool_result_record("Bash", "ok"), "turnOrigin": "human"}
|
|
97
|
+
assert last_typed_prompt_position(json_lines([human_tool_result]), NO_DEADLINE) is None
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def test_last_typed_prompt_position_falls_back_to_the_attended_variable_without_a_turn_origin(
|
|
101
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
102
|
+
) -> None:
|
|
103
|
+
untagged_prompt = {"type": "user", "message": {"role": "user", "content": "Add the flag."}}
|
|
104
|
+
all_lines = json_lines([untagged_prompt, reply_record(DEFERRAL_LINE), untagged_prompt])
|
|
105
|
+
assert last_typed_prompt_position(all_lines, NO_DEADLINE) == 2
|
|
106
|
+
reply_lines = json_lines([reply_record(DEFERRAL_LINE)])
|
|
107
|
+
assert last_typed_prompt_position(reply_lines, NO_DEADLINE) is None
|
|
108
|
+
machine_lines = json_lines([{**untagged_prompt, "turnOrigin": "sdk"}])
|
|
109
|
+
assert last_typed_prompt_position(machine_lines, NO_DEADLINE) is None
|
|
110
|
+
monkeypatch.delenv("CLAUDE_CODE_SESSION_ATTENDED")
|
|
111
|
+
assert last_typed_prompt_position(json_lines([untagged_prompt]), NO_DEADLINE) is None
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def test_top_level_records_keeps_assistant_records_and_this_gates_holds() -> None:
|
|
115
|
+
hold_record = {
|
|
116
|
+
"type": "user",
|
|
117
|
+
"isMeta": True,
|
|
118
|
+
"message": {"role": "user", "content": f"Stop hook feedback:\n{WORK_REASON_LEAD}"},
|
|
119
|
+
}
|
|
120
|
+
first_reply = reply_record("One.")
|
|
121
|
+
bash_call = tool_call_record("Bash")
|
|
122
|
+
sidechain_reply = {**reply_record("Three."), "isSidechain": True}
|
|
123
|
+
all_lines = json_lines(
|
|
124
|
+
[
|
|
125
|
+
typed_prompt_record(),
|
|
126
|
+
first_reply,
|
|
127
|
+
tool_result_record("Bash", "ok"),
|
|
128
|
+
bash_call,
|
|
129
|
+
hold_record,
|
|
130
|
+
sidechain_reply,
|
|
131
|
+
]
|
|
132
|
+
)
|
|
133
|
+
assert top_level_records([*all_lines, "not json"], NO_DEADLINE) == [
|
|
134
|
+
first_reply,
|
|
135
|
+
bash_call,
|
|
136
|
+
hold_record,
|
|
137
|
+
]
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def test_has_work_call_counts_any_tool_call_but_a_question_call() -> None:
|
|
141
|
+
assert has_work_call([tool_call_record("Edit")])
|
|
142
|
+
assert has_work_call([tool_call_record(["AskUserQuestion"])])
|
|
143
|
+
assert not has_work_call([tool_call_record("AskUserQuestion"), reply_record("One.")])
|
|
144
|
+
assert not has_work_call([tool_call_record("mcp__hearthbot__ask_decision")])
|
|
145
|
+
user_tool_use = {"type": "tool_use", "id": "id-user", "name": "Edit", "input": {}}
|
|
146
|
+
user_record = {"type": "user", "message": {"role": "user", "content": [user_tool_use]}}
|
|
147
|
+
assert not has_work_call([user_record])
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def test_stretch_start_follows_the_last_call_that_reached_the_user() -> None:
|
|
151
|
+
first_reply = reply_record("One.")
|
|
152
|
+
second_reply = reply_record("Two.")
|
|
153
|
+
sidechain_question = {
|
|
154
|
+
**tool_call_record("AskUserQuestion", tool_use_id="id-side"),
|
|
155
|
+
"isSidechain": True,
|
|
156
|
+
}
|
|
157
|
+
sidechain_answer = {
|
|
158
|
+
**tool_result_record("AskUserQuestion", "ok", tool_use_id="id-side"),
|
|
159
|
+
"isSidechain": True,
|
|
160
|
+
}
|
|
161
|
+
answered_call = tool_call_record("AskUserQuestion", tool_use_id="id-answered")
|
|
162
|
+
answer = tool_result_record("AskUserQuestion", "The user chose A.", tool_use_id="id-answered")
|
|
163
|
+
assert stretch_start(json_lines([first_reply, second_reply]), NO_DEADLINE) == 0
|
|
164
|
+
all_lines = json_lines(
|
|
165
|
+
[first_reply, answered_call, answer, second_reply, sidechain_question, sidechain_answer]
|
|
166
|
+
)
|
|
167
|
+
assert stretch_start(all_lines, NO_DEADLINE) == 2
|
|
168
|
+
decision_call = tool_call_record("mcp__hearthbot__ask_decision", tool_use_id="id-decision")
|
|
169
|
+
decision_answer = tool_result_record("Bash", "chose B", tool_use_id="id-decision")
|
|
170
|
+
decision_lines = json_lines([first_reply, decision_call, decision_answer])
|
|
171
|
+
assert stretch_start(decision_lines, NO_DEADLINE) == 2
|
|
172
|
+
unanswered_call = tool_call_record("AskUserQuestion")
|
|
173
|
+
assert stretch_start(json_lines([first_reply, unanswered_call]), NO_DEADLINE) == 0
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def test_block_texts_returns_the_string_text_of_each_text_block() -> None:
|
|
177
|
+
mixed_record = {
|
|
178
|
+
"type": "assistant",
|
|
179
|
+
"message": {
|
|
180
|
+
"role": "assistant",
|
|
181
|
+
"content": [
|
|
182
|
+
{"type": "text", "text": "One."},
|
|
183
|
+
{"type": "thinking", "thinking": "Hidden."},
|
|
184
|
+
{"type": "text", "text": 7},
|
|
185
|
+
{"type": "text", "text": "Two."},
|
|
186
|
+
],
|
|
187
|
+
},
|
|
188
|
+
}
|
|
189
|
+
assert block_texts(mixed_record) == ["One.", "Two."]
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def test_assistant_texts_reads_only_assistant_records() -> None:
|
|
193
|
+
all_stretch_records = [
|
|
194
|
+
typed_prompt_record("The prompt is parked."),
|
|
195
|
+
reply_record("One."),
|
|
196
|
+
tool_result_record("Bash", "Two."),
|
|
197
|
+
reply_record("Three."),
|
|
198
|
+
]
|
|
199
|
+
assert assistant_texts(all_stretch_records) == ["One.", "Three."]
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def test_closing_reply_texts_reads_a_closing_reply_the_transcript_does_not_end_with() -> None:
|
|
203
|
+
all_lines = json_lines([typed_prompt_record(), reply_record("Both tests pass.")])
|
|
204
|
+
assert closing_reply_texts(
|
|
205
|
+
{"last_assistant_message": DEFERRAL_LINE}, all_lines, NO_DEADLINE
|
|
206
|
+
) == [DEFERRAL_LINE]
|
|
207
|
+
assert (
|
|
208
|
+
closing_reply_texts(
|
|
209
|
+
{"last_assistant_message": " Both tests pass.\n"}, all_lines, NO_DEADLINE
|
|
210
|
+
)
|
|
211
|
+
== []
|
|
212
|
+
)
|
|
213
|
+
assert closing_reply_texts({"last_assistant_message": " "}, all_lines, NO_DEADLINE) == []
|
|
214
|
+
assert closing_reply_texts({}, all_lines, NO_DEADLINE) == []
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
def test_closing_reply_texts_sets_aside_the_question_calls_that_share_the_last_record() -> None:
|
|
218
|
+
question_line = "Which cache size should I use?"
|
|
219
|
+
question_call = {"type": "tool_use", "id": "id-card", "name": "AskUserQuestion", "input": {}}
|
|
220
|
+
edit_call = {"type": "tool_use", "id": "id-edit", "name": "Edit", "input": {}}
|
|
221
|
+
for all_blocks, all_expected_texts in [
|
|
222
|
+
([{"type": "text", "text": question_line}, question_call], []),
|
|
223
|
+
([{"type": "text", "text": question_line}, edit_call, question_call], [question_line]),
|
|
224
|
+
]:
|
|
225
|
+
card_record = {"type": "assistant", "message": {"role": "assistant", "content": all_blocks}}
|
|
226
|
+
all_lines = json_lines(
|
|
227
|
+
[
|
|
228
|
+
typed_prompt_record(),
|
|
229
|
+
card_record,
|
|
230
|
+
tool_result_record("AskUserQuestion", "The user chose 64.", tool_use_id="id-card"),
|
|
231
|
+
]
|
|
232
|
+
)
|
|
233
|
+
assert (
|
|
234
|
+
closing_reply_texts({"last_assistant_message": question_line}, all_lines, NO_DEADLINE)
|
|
235
|
+
== all_expected_texts
|
|
236
|
+
)
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import time
|
|
2
|
+
|
|
3
|
+
import pytest
|
|
4
|
+
|
|
5
|
+
import sent_file_question_detector
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@pytest.mark.parametrize(
|
|
9
|
+
("file_text", "expected_question"),
|
|
10
|
+
[
|
|
11
|
+
("Checks passed. Should I merge it?\n", "Should I merge it?"),
|
|
12
|
+
("Three dates fit. What release date works best?\n", "What release date works best?"),
|
|
13
|
+
("The run is done.\n\n- Did you run the tests?\n", "Did you run the tests?"),
|
|
14
|
+
("- [ ] Should I archive the old branches?\n", "Should I archive the old branches?"),
|
|
15
|
+
(
|
|
16
|
+
"- Did you run the tests?\n- Keep the cache or clear it?\n",
|
|
17
|
+
"Keep the cache or clear it?",
|
|
18
|
+
),
|
|
19
|
+
],
|
|
20
|
+
)
|
|
21
|
+
def test_question_for_user_should_return_the_question_the_text_asks(
|
|
22
|
+
file_text: str, expected_question: str
|
|
23
|
+
) -> None:
|
|
24
|
+
assert (
|
|
25
|
+
sent_file_question_detector.question_for_user(file_text, is_html=False) == expected_question
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@pytest.mark.parametrize(
|
|
30
|
+
"file_text",
|
|
31
|
+
[
|
|
32
|
+
"The build passed.\n",
|
|
33
|
+
"## Checklist\n\n- Did you run the tests?\n- Did you update the changelog?\n",
|
|
34
|
+
"- [ ] Did you run the tests?\n",
|
|
35
|
+
"Which Python version works best? Python 3.12.\n",
|
|
36
|
+
],
|
|
37
|
+
)
|
|
38
|
+
def test_question_for_user_should_return_none_for_text_that_asks_nothing(file_text: str) -> None:
|
|
39
|
+
assert sent_file_question_detector.question_for_user(file_text, is_html=False) is None
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
@pytest.mark.parametrize(
|
|
43
|
+
"file_text",
|
|
44
|
+
[
|
|
45
|
+
"# T" + " " * 3000 + "x",
|
|
46
|
+
"**a**" + " " * 50000,
|
|
47
|
+
"**a**" + " " * 50000 + "x",
|
|
48
|
+
"## Open" + " " * 50000 + "questions #" + " " * 50000 + "x",
|
|
49
|
+
],
|
|
50
|
+
)
|
|
51
|
+
def test_question_for_user_should_read_long_space_runs_in_linear_time(file_text: str) -> None:
|
|
52
|
+
started_at = time.perf_counter()
|
|
53
|
+
sent_file_question_detector.question_for_user(file_text, is_html=False)
|
|
54
|
+
assert time.perf_counter() - started_at < 1
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
@pytest.mark.parametrize(
|
|
58
|
+
"file_text",
|
|
59
|
+
[
|
|
60
|
+
"Q: one?\n" * 20000,
|
|
61
|
+
"word\n" * 100000,
|
|
62
|
+
("Should I " + "x " * 400 + "go? ") * 300,
|
|
63
|
+
],
|
|
64
|
+
)
|
|
65
|
+
def test_question_for_user_should_read_many_units_in_linear_time(file_text: str) -> None:
|
|
66
|
+
started_at = time.perf_counter()
|
|
67
|
+
sent_file_question_detector.question_for_user(file_text, is_html=False)
|
|
68
|
+
assert time.perf_counter() - started_at < 2
|