claude-dev-env 8.121.0 → 8.122.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/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_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/sent_file_question_gate_constants.py +261 -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,209 @@
|
|
|
1
|
+
"""Read the text of a file a session sends to the user, for the sent file question gate.
|
|
2
|
+
|
|
3
|
+
A small file is read whole. A large file is read through its first and last
|
|
4
|
+
128 KB, so a question at either end is still found and the read stays
|
|
5
|
+
fast::
|
|
6
|
+
|
|
7
|
+
a 3 KB notes.md -> its whole text
|
|
8
|
+
a 2 MB report.md -> its first 128 KB, a blank line, its last 128 KB
|
|
9
|
+
a .md file holding binary data -> None
|
|
10
|
+
|
|
11
|
+
``plain_page_text`` turns an HTML page into its shown text in one pass over
|
|
12
|
+
its tags, with no parser, for the gate's second read.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
import codecs
|
|
18
|
+
import html
|
|
19
|
+
import os
|
|
20
|
+
import sys
|
|
21
|
+
from pathlib import Path
|
|
22
|
+
|
|
23
|
+
hooks_root_directory = str(Path(__file__).resolve().parent.parent)
|
|
24
|
+
if hooks_root_directory not in sys.path:
|
|
25
|
+
sys.path.insert(0, hooks_root_directory)
|
|
26
|
+
blocking_directory = str(Path(__file__).resolve().parent)
|
|
27
|
+
if blocking_directory not in sys.path:
|
|
28
|
+
sys.path.insert(0, blocking_directory)
|
|
29
|
+
|
|
30
|
+
from hooks_constants.sent_file_question_gate_constants import (
|
|
31
|
+
BINARY_READ_MODE,
|
|
32
|
+
BLOCK_BREAK,
|
|
33
|
+
CLOSING_TAG_MARK,
|
|
34
|
+
LINE_BREAK,
|
|
35
|
+
PLAIN_HEADING_BREAK,
|
|
36
|
+
SPACE,
|
|
37
|
+
SPACE_RUN_PATTERN,
|
|
38
|
+
TAG_CLOSE,
|
|
39
|
+
TAG_OPEN,
|
|
40
|
+
TASK_BOX_MARK,
|
|
41
|
+
FILE_PART_BYTES,
|
|
42
|
+
FILE_PARTS_SEPARATOR,
|
|
43
|
+
MAXIMUM_UTF8_CONTINUATION_BYTES,
|
|
44
|
+
MAXIMUM_WHOLE_FILE_BYTES,
|
|
45
|
+
NUL_BYTE,
|
|
46
|
+
UTF8_CONTINUATION_BYTES,
|
|
47
|
+
UTF8_ENCODING,
|
|
48
|
+
)
|
|
49
|
+
from hooks_constants.user_step_wording_constants import (
|
|
50
|
+
ALL_HTML_HEADING_TAGS,
|
|
51
|
+
ALL_HTML_HIDDEN_TAGS,
|
|
52
|
+
ALL_HTML_INLINE_TAGS,
|
|
53
|
+
HTML_CHECKBOX_INPUT_TYPE,
|
|
54
|
+
HTML_INPUT_TAG,
|
|
55
|
+
HTML_LINE_BREAK_TAG,
|
|
56
|
+
)
|
|
57
|
+
from user_step_wording import sent_file_location
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _decoded_part(part_bytes: bytes, is_cut_at_end: bool) -> str:
|
|
61
|
+
decoder = codecs.getincrementaldecoder(UTF8_ENCODING)()
|
|
62
|
+
return decoder.decode(part_bytes, final=not is_cut_at_end)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def _tail_without_split_character(tail_bytes: bytes) -> bytes:
|
|
66
|
+
cut_length = 0
|
|
67
|
+
while (
|
|
68
|
+
cut_length < MAXIMUM_UTF8_CONTINUATION_BYTES
|
|
69
|
+
and cut_length < len(tail_bytes)
|
|
70
|
+
and tail_bytes[cut_length] in UTF8_CONTINUATION_BYTES
|
|
71
|
+
):
|
|
72
|
+
cut_length += 1
|
|
73
|
+
return tail_bytes[cut_length:]
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def _head_and_tail_bytes(file_path: Path) -> tuple[bytes, bytes]:
|
|
77
|
+
with file_path.open(BINARY_READ_MODE) as sent_file:
|
|
78
|
+
head_bytes = sent_file.read(MAXIMUM_WHOLE_FILE_BYTES + 1)
|
|
79
|
+
if len(head_bytes) <= MAXIMUM_WHOLE_FILE_BYTES:
|
|
80
|
+
return head_bytes, b""
|
|
81
|
+
sent_file.seek(-FILE_PART_BYTES, os.SEEK_END)
|
|
82
|
+
return head_bytes[:FILE_PART_BYTES], _tail_without_split_character(
|
|
83
|
+
sent_file.read(FILE_PART_BYTES)
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def sent_file_scan_text(file_path_text: str, working_directory: object) -> str | None:
|
|
88
|
+
"""Return the text the gate reads from a sent file, or None when it reads none.
|
|
89
|
+
|
|
90
|
+
::
|
|
91
|
+
|
|
92
|
+
a 3 KB notes.md -> its whole text
|
|
93
|
+
a 2 MB report.md -> its first 128 KB, a blank line, its last 128 KB
|
|
94
|
+
a .md file holding binary data -> None
|
|
95
|
+
shot.png, a folder, a lost path -> None
|
|
96
|
+
|
|
97
|
+
A file that holds a NUL byte or is not valid UTF-8 gives None, so the
|
|
98
|
+
gate never quotes garbled bytes.
|
|
99
|
+
|
|
100
|
+
Args:
|
|
101
|
+
file_path_text: The path the tool call names.
|
|
102
|
+
working_directory: The session's ``cwd`` from the hook payload.
|
|
103
|
+
|
|
104
|
+
Returns:
|
|
105
|
+
The decoded text, or None.
|
|
106
|
+
"""
|
|
107
|
+
file_path = sent_file_location(file_path_text, working_directory)
|
|
108
|
+
if file_path is None:
|
|
109
|
+
return None
|
|
110
|
+
try:
|
|
111
|
+
head_bytes, tail_bytes = _head_and_tail_bytes(file_path)
|
|
112
|
+
except (OSError, ValueError):
|
|
113
|
+
return None
|
|
114
|
+
if NUL_BYTE in head_bytes or NUL_BYTE in tail_bytes:
|
|
115
|
+
return None
|
|
116
|
+
try:
|
|
117
|
+
head_text = _decoded_part(head_bytes, is_cut_at_end=bool(tail_bytes))
|
|
118
|
+
tail_text = _decoded_part(tail_bytes, is_cut_at_end=False)
|
|
119
|
+
except UnicodeDecodeError:
|
|
120
|
+
return None
|
|
121
|
+
return head_text + FILE_PARTS_SEPARATOR + tail_text if tail_text else head_text
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def sent_file_is_partly_read(file_path_text: str, working_directory: object) -> bool:
|
|
125
|
+
"""Return True when ``sent_file_scan_text`` reads only the start and end of the file.
|
|
126
|
+
|
|
127
|
+
::
|
|
128
|
+
|
|
129
|
+
a 3 KB notes.md -> False
|
|
130
|
+
a 2 MB report.md -> True
|
|
131
|
+
shot.png, lost path -> False
|
|
132
|
+
|
|
133
|
+
Args:
|
|
134
|
+
file_path_text: The path the tool call names.
|
|
135
|
+
working_directory: The session's ``cwd`` from the hook payload.
|
|
136
|
+
|
|
137
|
+
Returns:
|
|
138
|
+
True for a sent text file over 256 KB.
|
|
139
|
+
"""
|
|
140
|
+
file_path = sent_file_location(file_path_text, working_directory)
|
|
141
|
+
if file_path is None:
|
|
142
|
+
return False
|
|
143
|
+
try:
|
|
144
|
+
return file_path.stat().st_size > MAXIMUM_WHOLE_FILE_BYTES
|
|
145
|
+
except OSError:
|
|
146
|
+
return False
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
def _tag_name(tag_body: str) -> str:
|
|
150
|
+
all_name_words = tag_body.lstrip(CLOSING_TAG_MARK).split(None, 1)
|
|
151
|
+
return all_name_words[0].rstrip(CLOSING_TAG_MARK).lower() if all_name_words else ""
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def _after_tag(tag_body: str, hidden_tag_name: str) -> tuple[str, str]:
|
|
155
|
+
is_closing = tag_body.startswith(CLOSING_TAG_MARK)
|
|
156
|
+
tag_name = _tag_name(tag_body)
|
|
157
|
+
if hidden_tag_name:
|
|
158
|
+
return ("" if is_closing and tag_name == hidden_tag_name else hidden_tag_name), ""
|
|
159
|
+
if not is_closing and tag_name in ALL_HTML_HIDDEN_TAGS:
|
|
160
|
+
return tag_name, ""
|
|
161
|
+
if tag_name in ALL_HTML_HEADING_TAGS and not is_closing:
|
|
162
|
+
return "", PLAIN_HEADING_BREAK
|
|
163
|
+
if tag_name == HTML_INPUT_TAG and HTML_CHECKBOX_INPUT_TYPE in tag_body.lower():
|
|
164
|
+
return "", BLOCK_BREAK + TASK_BOX_MARK
|
|
165
|
+
if tag_name == HTML_LINE_BREAK_TAG:
|
|
166
|
+
return "", LINE_BREAK
|
|
167
|
+
if tag_name in ALL_HTML_INLINE_TAGS:
|
|
168
|
+
return "", ""
|
|
169
|
+
return "", BLOCK_BREAK
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def plain_page_text(page_markup: str) -> str:
|
|
173
|
+
"""Return the shown text of an HTML page through one pass over its tags, with no parser.
|
|
174
|
+
|
|
175
|
+
::
|
|
176
|
+
|
|
177
|
+
<h2>Plan</h2><p>Should I merge it?</p> -> "\\n\\n# Plan\\n\\n\\n\\nShould I merge it?\\n\\n"
|
|
178
|
+
<script>var x = 'Should I?'</script> -> ""
|
|
179
|
+
<script <script <script ... -> "" (an unclosed tag ends the text)
|
|
180
|
+
|
|
181
|
+
The gate falls back to this read when the HTML parser does not finish in
|
|
182
|
+
time. Each character is visited once. A heading or ``summary`` starts a
|
|
183
|
+
"# " line, another block tag starts a paragraph, and the contents of
|
|
184
|
+
script, style, code, pre, blockquote, and the other hidden tags are left
|
|
185
|
+
out. Character references are decoded.
|
|
186
|
+
|
|
187
|
+
Args:
|
|
188
|
+
page_markup: The HTML page source.
|
|
189
|
+
|
|
190
|
+
Returns:
|
|
191
|
+
The shown text as Markdown-like paragraphs.
|
|
192
|
+
"""
|
|
193
|
+
all_parts: list[str] = []
|
|
194
|
+
hidden_tag_name = ""
|
|
195
|
+
position = 0
|
|
196
|
+
while position < len(page_markup):
|
|
197
|
+
tag_start = page_markup.find(TAG_OPEN, position)
|
|
198
|
+
text_end = len(page_markup) if tag_start == -1 else tag_start
|
|
199
|
+
if not hidden_tag_name:
|
|
200
|
+
all_parts.append(SPACE_RUN_PATTERN.sub(SPACE, page_markup[position:text_end]))
|
|
201
|
+
tag_end = -1 if tag_start == -1 else page_markup.find(TAG_CLOSE, tag_start)
|
|
202
|
+
if tag_end == -1:
|
|
203
|
+
break
|
|
204
|
+
hidden_tag_name, tag_break = _after_tag(
|
|
205
|
+
page_markup[tag_start + 1 : tag_end].strip(), hidden_tag_name
|
|
206
|
+
)
|
|
207
|
+
all_parts.append(tag_break)
|
|
208
|
+
position = tag_end + 1
|
|
209
|
+
return html.unescape("".join(all_parts))
|
|
@@ -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
|