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.
Files changed (30) hide show
  1. package/bin/ever-registered-hooks.mjs +1 -0
  2. package/docs/rule-guides/question-presentation.md +4 -0
  3. package/hooks/blocking/closing_question_round_gate.py +387 -0
  4. package/hooks/blocking/closing_question_round_lines.py +109 -0
  5. package/hooks/blocking/closing_question_round_open_items.py +348 -0
  6. package/hooks/blocking/closing_question_round_transcript.py +344 -0
  7. package/hooks/blocking/sent_file_question_detector.py +340 -0
  8. package/hooks/blocking/sent_file_question_gate.py +307 -0
  9. package/hooks/blocking/sent_file_scan.py +209 -0
  10. package/hooks/blocking/test_closing_question_round_gate.py +983 -0
  11. package/hooks/blocking/test_closing_question_round_gate_declaration.py +307 -0
  12. package/hooks/blocking/test_closing_question_round_gate_work.py +349 -0
  13. package/hooks/blocking/test_closing_question_round_lines.py +70 -0
  14. package/hooks/blocking/test_closing_question_round_open_items.py +238 -0
  15. package/hooks/blocking/test_closing_question_round_transcript.py +236 -0
  16. package/hooks/blocking/test_sent_file_question_detector.py +68 -0
  17. package/hooks/blocking/test_sent_file_question_gate.py +688 -0
  18. package/hooks/blocking/test_sent_file_scan.py +71 -0
  19. package/hooks/blocking/test_user_step_wording.py +46 -0
  20. package/hooks/blocking/user_step_wording.py +113 -15
  21. package/hooks/features/conduct-gates.md +3 -0
  22. package/hooks/hooks.json +10 -0
  23. package/hooks/hooks_constants/closing_question_round_gate_constants.py +167 -0
  24. package/hooks/hooks_constants/sent_file_question_gate_constants.py +261 -0
  25. package/hooks/hooks_constants/test_closing_question_round_gate_constants.py +92 -0
  26. package/hooks/hooks_constants/user_step_wording_constants.py +4 -0
  27. package/package.json +1 -1
  28. package/scripts/policy_lint/adapter_configuration.py +5 -0
  29. package/scripts/policy_lint/config/constants.py +1 -0
  30. 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))