claude-dev-env 8.115.0 → 8.116.1
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/install.prompt-context.test.mjs +5 -6
- package/docs/rule-guides/verified-work.md +1 -1
- package/hooks/advisory/commit_and_pull_request_words.py +346 -0
- package/hooks/advisory/piece_clauses.py +248 -0
- package/hooks/advisory/pieces_per_level_analysis.py +192 -0
- package/hooks/advisory/pieces_per_level_note.py +265 -0
- package/hooks/advisory/test_commit_and_pull_request_words.py +126 -0
- package/hooks/advisory/test_piece_clauses.py +183 -0
- package/hooks/advisory/test_pieces_per_level_note.py +389 -0
- package/hooks/advisory/test_pieces_per_level_note_bounds.py +215 -0
- package/hooks/advisory/test_pieces_per_level_note_deadline.py +282 -0
- package/hooks/blocking/final_reply_gate.py +7 -29
- package/hooks/blocking/test_final_reply_gate.py +4 -196
- package/hooks/features/README.md +1 -1
- package/hooks/features/conduct-gates.md +5 -12
- package/hooks/features/session-context.md +4 -1
- package/hooks/hooks.json +10 -41
- package/hooks/hooks_constants/background_silence_prompt_constants.py +2 -0
- package/hooks/hooks_constants/pieces_per_level_note_constants.py +255 -0
- package/hooks/hooks_constants/resolve_thread_reply_gate_constants.py +0 -8
- package/hooks/hooks_constants/shell_command_wrappers.py +20 -8
- package/hooks/hooks_constants/test_shell_command_wrappers.py +7 -0
- package/hooks/session/background_silence_prompt.py +1 -1
- package/hooks/test_context_budget_hooks.py +0 -1
- package/package.json +1 -1
- package/scripts/durable_post_lint.py +0 -12
- package/scripts/durable_post_lint_config/config/constants.py +1 -2
- package/scripts/policy_lint/adapter_configuration.py +0 -2
- package/scripts/policy_lint/config/constants.py +0 -2
- package/scripts/test_durable_post_lint.py +0 -53
- package/scripts/tests/test_adapter_configuration.py +0 -16
- package/scripts/tests/test_verified_work_rule.py +13 -0
- package/hooks/blocking/chat_reply_gate.py +0 -260
- package/hooks/blocking/reply_length_gate.py +0 -804
- package/hooks/blocking/test_chat_reply_gate.py +0 -302
- package/hooks/blocking/test_reply_length_gate.py +0 -1324
- package/hooks/hooks_constants/chat_reply_gate_constants.py +0 -60
- package/hooks/hooks_constants/reply_length_gate_constants.py +0 -203
- package/hooks/hooks_constants/test_reply_length_gate_constants.py +0 -81
- package/hooks/hooks_constants/test_visual_reply_rules_constants.py +0 -50
- package/hooks/hooks_constants/visual_reply_rules_constants.py +0 -106
- package/hooks/test_visual_reply_rules.py +0 -362
- package/hooks/visual_reply_rules.py +0 -478
- package/rules/visual-reply-rules.json +0 -56
|
@@ -1,804 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env python3
|
|
2
|
-
"""PreToolUse hook that caps the length of a chat reply before it posts.
|
|
3
|
-
|
|
4
|
-
The gate reads the ``text`` field of the chat tools that post to the user.
|
|
5
|
-
It denies the call when the text holds more than MAXIMUM_SENTENCE_COUNT
|
|
6
|
-
sentences, or a sentence longer than MAXIMUM_WORDS_PER_SENTENCE words.
|
|
7
|
-
It also denies a pull request number the reader cannot open::
|
|
8
|
-
|
|
9
|
-
flag: Both land in PR 5256.
|
|
10
|
-
flag: It merged in #4347.
|
|
11
|
-
ok: Both land in [PR 5256](https://github.com/owner/repo/pull/5256).
|
|
12
|
-
|
|
13
|
-
It denies a colon that joins two clauses on one line, and an em dash::
|
|
14
|
-
|
|
15
|
-
flag: That second reading matters: one message added 6.7k.
|
|
16
|
-
ok: That second reading matters. One message added 6.7k.
|
|
17
|
-
ok: Two checks failed at 9:47, then the list follows:
|
|
18
|
-
(each item on its own line below the colon)
|
|
19
|
-
|
|
20
|
-
It denies a causal sentence ("so", "because", "caused", "due to",
|
|
21
|
-
"therefore", "means") that carries no inline code, URL, or markdown link
|
|
22
|
-
of its own. A command block in the same reply is no evidence::
|
|
23
|
-
|
|
24
|
-
flag: That window was Windows PowerShell without Administrator, so the write did not land.
|
|
25
|
-
ok: The write did not land, because the next read printed `Right after write: 1`.
|
|
26
|
-
|
|
27
|
-
It denies a banned word or phrase, matched whole and case-insensitive::
|
|
28
|
-
|
|
29
|
-
flag: The cause is likely the cache.
|
|
30
|
-
ok: The cache log shows the miss at 12:04.
|
|
31
|
-
|
|
32
|
-
The gate uses the ``banned_words`` list in ``~/.claude/reply-banned-words.json``,
|
|
33
|
-
or in the file that CLAUDE_REPLY_BANNED_WORDS_PATH names. Without a valid
|
|
34
|
-
list there, it uses ALL_DEFAULT_BANNED_WORDS.
|
|
35
|
-
|
|
36
|
-
It denies a sentence that guesses, estimates, hedges a duration, or admits
|
|
37
|
-
a check did not run::
|
|
38
|
-
|
|
39
|
-
flag: My guess is one to two hours.
|
|
40
|
-
flag: I didn't confirm this one.
|
|
41
|
-
ok: The last build took 39 minutes.
|
|
42
|
-
|
|
43
|
-
It denies an Actions run, a pull request link, or a run, job, or check id
|
|
44
|
-
that no successful tool result in the transcript shows. A subagent's report
|
|
45
|
-
is the result of its Agent call. With no readable transcript, this check
|
|
46
|
-
is skipped. The closing-reply gate uses
|
|
47
|
-
``unseen_citation_violation_with_hook_text``, where a pull request link also
|
|
48
|
-
counts as shown when hook or harness text shows it.
|
|
49
|
-
|
|
50
|
-
The banned-word, unvalidated claim, and citation checks also read the prose of a
|
|
51
|
-
decision card: its question, its context, and each option's label and
|
|
52
|
-
consequence. They read an AskUserQuestion card the same way: each
|
|
53
|
-
question, its header, and each option's label and description.
|
|
54
|
-
A decision card gets a length check on its question, context, reason, and
|
|
55
|
-
each option's consequence. No sentence there runs longer than
|
|
56
|
-
MAXIMUM_WORDS_PER_SENTENCE words, and the context holds at most
|
|
57
|
-
MAXIMUM_CARD_CONTEXT_SENTENCE_COUNT sentences. A context may hold no list
|
|
58
|
-
item line, such as "- Cedar" or "1. Cedar". A card refused for length or for
|
|
59
|
-
a list names the page flow to use instead: an Artifact page with a tap button
|
|
60
|
-
per item and a Send button, then a short reply that links the page.
|
|
61
|
-
|
|
62
|
-
Each non-empty line counts as its own sentence, so a list counts one
|
|
63
|
-
sentence per item. URLs, markdown link targets, inline code spans, and
|
|
64
|
-
fenced blocks carry no words.
|
|
65
|
-
|
|
66
|
-
With visual reply mode on, the gate also runs the checks in
|
|
67
|
-
``visual_reply_rules.py``. The mode is off until
|
|
68
|
-
``~/.claude/visual-reply-mode.json`` holds ``{"enabled": true}``. A reply
|
|
69
|
-
or a decision card may hold no abbreviation and no tracker number outside
|
|
70
|
-
a link. A reply of more than one sentence needs a widget or a page earlier
|
|
71
|
-
in the turn. A turn sends one reply, so a reply after a delivered reply in
|
|
72
|
-
the same turn is denied. A widget may hold no anchor link, since a widget
|
|
73
|
-
link does not open in the Claude app.
|
|
74
|
-
|
|
75
|
-
A reply that reports results, a markdown table or three or more figures outside
|
|
76
|
-
links and code, needs one picture: an image file in ``attached_outputs``, or a
|
|
77
|
-
markdown image. This check runs whether or not the visual mode is on.
|
|
78
|
-
|
|
79
|
-
A length limit is a smell elsewhere in this package, recorded and fixed in a
|
|
80
|
-
later pass. A posted reply reaches the user the moment it sends and has no
|
|
81
|
-
later pass, so this gate denies it and the model resends a shorter one.
|
|
82
|
-
"""
|
|
83
|
-
|
|
84
|
-
from __future__ import annotations
|
|
85
|
-
|
|
86
|
-
import itertools
|
|
87
|
-
import json
|
|
88
|
-
import os
|
|
89
|
-
import re
|
|
90
|
-
import shlex
|
|
91
|
-
import sys
|
|
92
|
-
from collections.abc import Callable, Iterable
|
|
93
|
-
from pathlib import Path
|
|
94
|
-
|
|
95
|
-
hooks_root_directory = str(Path(__file__).resolve().parent.parent)
|
|
96
|
-
if hooks_root_directory not in sys.path:
|
|
97
|
-
sys.path.insert(0, hooks_root_directory)
|
|
98
|
-
|
|
99
|
-
from gh_post_body_texts import extract_mcp_body_texts
|
|
100
|
-
from json_file_reader import read_json_object
|
|
101
|
-
from visual_reply_rules import (
|
|
102
|
-
VisualReplyRules,
|
|
103
|
-
abbreviation_violation,
|
|
104
|
-
hook_supplied_text,
|
|
105
|
-
load_rules,
|
|
106
|
-
mode_enabled,
|
|
107
|
-
results_picture_violation,
|
|
108
|
-
second_reply_violation,
|
|
109
|
-
successful_result_text,
|
|
110
|
-
visual_violation,
|
|
111
|
-
widget_anchor_violation,
|
|
112
|
-
widget_flowchart_advice,
|
|
113
|
-
)
|
|
114
|
-
from hooks_constants.hook_block_logger import log_hook_block
|
|
115
|
-
from hooks_constants.pre_tool_use_stdin import read_hook_input_dictionary_from_stdin
|
|
116
|
-
from hooks_constants.hook_specific_output_keys import (
|
|
117
|
-
HOOK_EVENT_NAME_KEY,
|
|
118
|
-
HOOK_SPECIFIC_OUTPUT_KEY,
|
|
119
|
-
)
|
|
120
|
-
from hooks_constants.reply_length_gate_constants import (
|
|
121
|
-
ALL_CHECKED_MCP_TOOL_NAMES,
|
|
122
|
-
BASH_TOOL_NAME,
|
|
123
|
-
COMMAND_KEY,
|
|
124
|
-
COMMENT_RETRY_INSTRUCTION,
|
|
125
|
-
DETAILS_BLOCK_PATTERN,
|
|
126
|
-
ALL_GH_BODY_FLAGS,
|
|
127
|
-
ALL_GH_COMMENT_ACTIONS,
|
|
128
|
-
ALL_GH_COMMENT_GROUPS,
|
|
129
|
-
GH_BODY_FLAG_PREFIX,
|
|
130
|
-
GH_PROGRAM_NAME,
|
|
131
|
-
SHELL_OPERATOR_CHARACTERS,
|
|
132
|
-
ALL_CHECKED_TOOL_NAMES,
|
|
133
|
-
ALL_DECISION_CARD_LENGTH_KEYS,
|
|
134
|
-
ALL_DECISION_CARD_PROSE_KEYS,
|
|
135
|
-
ALL_DECISION_OPTION_LENGTH_KEYS,
|
|
136
|
-
ALL_DECISION_OPTION_PROSE_KEYS,
|
|
137
|
-
ALL_DEFAULT_BANNED_WORDS,
|
|
138
|
-
ALL_EDIT_TOOL_NAMES,
|
|
139
|
-
ALL_EVIDENCE_PATTERNS,
|
|
140
|
-
ALL_QUESTION_CARD_PROSE_KEYS,
|
|
141
|
-
ALL_QUESTION_OPTION_PROSE_KEYS,
|
|
142
|
-
ALLOW_EXIT_CODE,
|
|
143
|
-
BANNED_WORD_MESSAGE,
|
|
144
|
-
BANNED_WORD_PART_SEPARATOR,
|
|
145
|
-
BANNED_WORD_PATTERN_TEMPLATE,
|
|
146
|
-
BANNED_WORDS_FILE_NAME,
|
|
147
|
-
BANNED_WORDS_JSON_KEY,
|
|
148
|
-
BANNED_WORDS_PATH_ENV_VAR,
|
|
149
|
-
BLOCK_EXIT_CODE,
|
|
150
|
-
CARD_CONTEXT_LIST_MESSAGE,
|
|
151
|
-
CARD_CONTEXT_TOO_LONG_MESSAGE,
|
|
152
|
-
CARD_FORMAT_FLOW_INSTRUCTION,
|
|
153
|
-
CARD_LIST_ITEM_PATTERN,
|
|
154
|
-
CAUSAL_CLAIM_PATTERN,
|
|
155
|
-
CARD_TEXT_SEPARATOR,
|
|
156
|
-
CITED_NUMBER_IN_RESULT_TEMPLATE,
|
|
157
|
-
CITED_NUMBER_PATTERN,
|
|
158
|
-
CITED_PULL_NUMBER_GROUP,
|
|
159
|
-
CLAUDE_HOME_DIRECTORY_NAME,
|
|
160
|
-
CONFIG_FILE_ENCODING,
|
|
161
|
-
CURLY_APOSTROPHE,
|
|
162
|
-
DECISION_CARD_CONTEXT_KEY,
|
|
163
|
-
DECISION_CARD_OPTIONS_KEY,
|
|
164
|
-
DECISION_CARD_TOOL_NAME,
|
|
165
|
-
DURATION_CLAIM_PATTERN,
|
|
166
|
-
EM_DASH_MESSAGE,
|
|
167
|
-
EM_DASH_PATTERN,
|
|
168
|
-
FENCED_BLOCK_PATTERN,
|
|
169
|
-
FINDING_SEPARATOR,
|
|
170
|
-
FIX_EVERY_ITEM_INSTRUCTION,
|
|
171
|
-
ADDITIONAL_CONTEXT_KEY,
|
|
172
|
-
HOOK_EVENT_NAME,
|
|
173
|
-
INLINE_CODE_PATTERN,
|
|
174
|
-
LINE_BREAK_PATTERN,
|
|
175
|
-
LINK_TARGET_PATTERN,
|
|
176
|
-
LONG_SENTENCE_MESSAGE,
|
|
177
|
-
MARKDOWN_LINK_PATTERN,
|
|
178
|
-
MAXIMUM_CARD_CONTEXT_SENTENCE_COUNT,
|
|
179
|
-
MAXIMUM_SENTENCE_COUNT,
|
|
180
|
-
MAXIMUM_WORDS_PER_SENTENCE,
|
|
181
|
-
MID_SENTENCE_COLON_MESSAGE,
|
|
182
|
-
MID_SENTENCE_COLON_PATTERN,
|
|
183
|
-
PULL_LINK_IN_HOOK_TEXT_TEMPLATE,
|
|
184
|
-
QUESTION_CARD_QUESTIONS_KEY,
|
|
185
|
-
QUESTION_CARD_TOOL_NAME,
|
|
186
|
-
REPLY_ECHO_PATTERN,
|
|
187
|
-
RETRY_INSTRUCTION,
|
|
188
|
-
SENTENCE_END_PATTERN,
|
|
189
|
-
SENTENCE_PREVIEW_SUFFIX,
|
|
190
|
-
SENTENCE_PREVIEW_WORD_COUNT,
|
|
191
|
-
STRAIGHT_APOSTROPHE,
|
|
192
|
-
TEXT_KEY,
|
|
193
|
-
TOO_MANY_SENTENCES_MESSAGE,
|
|
194
|
-
TOOL_INPUT_KEY,
|
|
195
|
-
TOOL_NAME_KEY,
|
|
196
|
-
TRANSCRIPT_PATH_KEY,
|
|
197
|
-
UNLINKED_PULL_REQUEST_MESSAGE,
|
|
198
|
-
UNLINKED_PULL_REQUEST_PATTERN,
|
|
199
|
-
UNSEEN_CITATION_MESSAGE,
|
|
200
|
-
UNSOURCED_CAUSE_MESSAGE,
|
|
201
|
-
UNVALIDATED_CLAIM_MESSAGE,
|
|
202
|
-
UNVALIDATED_CLAIM_PATTERN,
|
|
203
|
-
URL_PATTERN,
|
|
204
|
-
WORD_PATTERN,
|
|
205
|
-
WORD_SEPARATOR,
|
|
206
|
-
)
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
def countable_text(reply_text: str) -> str:
|
|
210
|
-
"""Remove the spans that carry no words: fenced blocks, code, link targets, URLs."""
|
|
211
|
-
without_fences = FENCED_BLOCK_PATTERN.sub(" ", reply_text)
|
|
212
|
-
without_code = INLINE_CODE_PATTERN.sub(" ", without_fences)
|
|
213
|
-
without_link_targets = LINK_TARGET_PATTERN.sub("]", without_code)
|
|
214
|
-
return URL_PATTERN.sub(" ", without_link_targets)
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
def sentence_word_lists(reply_text: str) -> list[list[str]]:
|
|
218
|
-
"""Split the text into sentences, one per line or terminal mark, as word lists."""
|
|
219
|
-
all_word_lists = [
|
|
220
|
-
WORD_PATTERN.findall(each_sentence) for each_sentence in prose_sentences(countable_text(reply_text))
|
|
221
|
-
]
|
|
222
|
-
return [each_word_list for each_word_list in all_word_lists if each_word_list]
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
def joined_violations(all_violations: Iterable[str | None]) -> str | None:
|
|
226
|
-
"""Join every violation found into one deny reason, one violation per line.
|
|
227
|
-
|
|
228
|
-
::
|
|
229
|
-
|
|
230
|
-
[] -> None
|
|
231
|
-
["Too long."] -> "Too long."
|
|
232
|
-
["Too long.", "Em dash."] -> "Too long." and "Em dash." on two lines
|
|
233
|
-
|
|
234
|
-
None entries are skipped. A joined reason can join again at an outer
|
|
235
|
-
level and stays one violation per line, so ``numbered_violations``
|
|
236
|
-
numbers the whole reason once.
|
|
237
|
-
"""
|
|
238
|
-
all_found = [each_violation for each_violation in all_violations if each_violation]
|
|
239
|
-
return FINDING_SEPARATOR.join(all_found) if all_found else None
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
def numbered_violations(violation: str) -> str:
|
|
243
|
-
"""Number each line of a joined deny reason that holds several violations.
|
|
244
|
-
|
|
245
|
-
::
|
|
246
|
-
|
|
247
|
-
"Too long." -> "Too long."
|
|
248
|
-
"Too long.\nEm dash." -> "1. Too long." and "2. Em dash." on two lines
|
|
249
|
-
"""
|
|
250
|
-
all_lines = violation.split(FINDING_SEPARATOR)
|
|
251
|
-
if len(all_lines) == 1:
|
|
252
|
-
return violation
|
|
253
|
-
return FINDING_SEPARATOR.join(
|
|
254
|
-
f"{each_position}. {each_line}" for each_position, each_line in enumerate(all_lines, start=1)
|
|
255
|
-
)
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
def collected_violations(all_checks: Iterable[Callable[[], str | None]]) -> str | None:
|
|
259
|
-
"""Run every check in order and join each violation it reports.
|
|
260
|
-
|
|
261
|
-
A check that raises ends the run. With nothing found yet the error
|
|
262
|
-
propagates, which lets the call through as an early-return gate did.
|
|
263
|
-
After a finding, the findings so far stand, as the first one did when
|
|
264
|
-
the gate never ran the later checks.
|
|
265
|
-
"""
|
|
266
|
-
all_violations: list[str | None] = []
|
|
267
|
-
for each_check in all_checks:
|
|
268
|
-
is_stopped, violation = _checked_violation(each_check, any(all_violations))
|
|
269
|
-
if is_stopped:
|
|
270
|
-
break
|
|
271
|
-
all_violations.append(violation)
|
|
272
|
-
return joined_violations(all_violations)
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
def _checked_violation(check: Callable[[], str | None], has_finding: bool) -> tuple[bool, str | None]:
|
|
276
|
-
try:
|
|
277
|
-
return False, check()
|
|
278
|
-
except Exception:
|
|
279
|
-
if has_finding:
|
|
280
|
-
return True, None
|
|
281
|
-
raise
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
def length_violation(reply_text: str) -> str | None:
|
|
285
|
-
"""Return the deny reason for an over-long reply and each over-long sentence, or None when it fits."""
|
|
286
|
-
all_sentences = sentence_word_lists(reply_text)
|
|
287
|
-
return joined_violations(
|
|
288
|
-
[
|
|
289
|
-
TOO_MANY_SENTENCES_MESSAGE.format(
|
|
290
|
-
sentence_count=len(all_sentences), sentence_limit=MAXIMUM_SENTENCE_COUNT
|
|
291
|
-
)
|
|
292
|
-
if len(all_sentences) > MAXIMUM_SENTENCE_COUNT
|
|
293
|
-
else None,
|
|
294
|
-
long_sentence_violation(reply_text),
|
|
295
|
-
]
|
|
296
|
-
)
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
def long_sentence_violation(prose_text: str) -> str | None:
|
|
300
|
-
"""Return the deny reason for each sentence over the word limit, or None."""
|
|
301
|
-
return joined_violations(
|
|
302
|
-
LONG_SENTENCE_MESSAGE.format(
|
|
303
|
-
word_count=len(each_sentence),
|
|
304
|
-
word_limit=MAXIMUM_WORDS_PER_SENTENCE,
|
|
305
|
-
sentence_preview=WORD_SEPARATOR.join(each_sentence[:SENTENCE_PREVIEW_WORD_COUNT])
|
|
306
|
-
+ SENTENCE_PREVIEW_SUFFIX,
|
|
307
|
-
)
|
|
308
|
-
for each_sentence in sentence_word_lists(prose_text)
|
|
309
|
-
if len(each_sentence) > MAXIMUM_WORDS_PER_SENTENCE
|
|
310
|
-
)
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
def mid_sentence_colon_violation(reply_text: str) -> str | None:
|
|
314
|
-
"""Return the deny reason for a colon followed by prose on its line, or None."""
|
|
315
|
-
if MID_SENTENCE_COLON_PATTERN.search(countable_text(reply_text)) is None:
|
|
316
|
-
return None
|
|
317
|
-
return MID_SENTENCE_COLON_MESSAGE
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
def em_dash_violation(reply_text: str) -> str | None:
|
|
321
|
-
"""Return the deny reason for an em dash in the prose, or None."""
|
|
322
|
-
if EM_DASH_PATTERN.search(countable_text(reply_text)) is None:
|
|
323
|
-
return None
|
|
324
|
-
return EM_DASH_MESSAGE
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
def prose_sentences(reply_text: str) -> list[str]:
|
|
328
|
-
"""Split the text outside fenced blocks into sentences, one per line or terminal mark."""
|
|
329
|
-
return [
|
|
330
|
-
each_sentence.strip()
|
|
331
|
-
for each_line in LINE_BREAK_PATTERN.split(FENCED_BLOCK_PATTERN.sub(" ", reply_text))
|
|
332
|
-
for each_sentence in SENTENCE_END_PATTERN.split(each_line)
|
|
333
|
-
]
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
def unsourced_cause_violation(reply_text: str) -> str | None:
|
|
337
|
-
"""Return the deny reason for each causal sentence that cites no evidence itself, or None."""
|
|
338
|
-
return joined_violations(
|
|
339
|
-
UNSOURCED_CAUSE_MESSAGE.format(sentence=each_sentence.strip())
|
|
340
|
-
for each_sentence in prose_sentences(reply_text)
|
|
341
|
-
if CAUSAL_CLAIM_PATTERN.search(INLINE_CODE_PATTERN.sub(" ", each_sentence))
|
|
342
|
-
and not any(each_pattern.search(each_sentence) for each_pattern in ALL_EVIDENCE_PATTERNS)
|
|
343
|
-
)
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
def prose_outside_links(reply_text: str) -> str:
|
|
347
|
-
"""Remove fenced blocks, code spans, whole markdown links and URLs."""
|
|
348
|
-
without_fences = FENCED_BLOCK_PATTERN.sub(" ", reply_text)
|
|
349
|
-
without_code = INLINE_CODE_PATTERN.sub(" ", without_fences)
|
|
350
|
-
without_links = MARKDOWN_LINK_PATTERN.sub(" ", without_code)
|
|
351
|
-
return URL_PATTERN.sub(" ", without_links)
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
def unlinked_pull_request_violation(reply_text: str) -> str | None:
|
|
355
|
-
"""Return the deny reason for each pull request number outside a link, or None."""
|
|
356
|
-
return joined_violations(
|
|
357
|
-
UNLINKED_PULL_REQUEST_MESSAGE.format(reference=each_match.group(0))
|
|
358
|
-
for each_match in UNLINKED_PULL_REQUEST_PATTERN.finditer(prose_outside_links(reply_text))
|
|
359
|
-
)
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
def banned_words_config_path() -> Path:
|
|
363
|
-
"""Return the banned-words file named by the environment, or the one in the Claude home."""
|
|
364
|
-
path_override = os.environ.get(BANNED_WORDS_PATH_ENV_VAR)
|
|
365
|
-
if path_override:
|
|
366
|
-
return Path(path_override)
|
|
367
|
-
return Path.home() / CLAUDE_HOME_DIRECTORY_NAME / BANNED_WORDS_FILE_NAME
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
def configured_banned_words() -> tuple[str, ...]:
|
|
371
|
-
"""Return the configured banned words, or the defaults when no valid list is configured."""
|
|
372
|
-
config_document = read_json_object(banned_words_config_path(), CONFIG_FILE_ENCODING)
|
|
373
|
-
if config_document is None:
|
|
374
|
-
return ALL_DEFAULT_BANNED_WORDS
|
|
375
|
-
all_configured_words = config_document.get(BANNED_WORDS_JSON_KEY)
|
|
376
|
-
if not isinstance(all_configured_words, list):
|
|
377
|
-
return ALL_DEFAULT_BANNED_WORDS
|
|
378
|
-
return tuple(
|
|
379
|
-
each_word.strip()
|
|
380
|
-
for each_word in all_configured_words
|
|
381
|
-
if isinstance(each_word, str) and each_word.strip()
|
|
382
|
-
)
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
def banned_word_violation(reply_text: str, all_banned_words: tuple[str, ...]) -> str | None:
|
|
386
|
-
"""Return the deny reason for each banned word in the prose, or None."""
|
|
387
|
-
prose_text = countable_text(reply_text)
|
|
388
|
-
return joined_violations(
|
|
389
|
-
BANNED_WORD_MESSAGE.format(banned_word=each_banned_word)
|
|
390
|
-
for each_banned_word in all_banned_words
|
|
391
|
-
if re.search(
|
|
392
|
-
BANNED_WORD_PATTERN_TEMPLATE.format(
|
|
393
|
-
word_pattern=BANNED_WORD_PART_SEPARATOR.join(
|
|
394
|
-
re.escape(each_part) for each_part in each_banned_word.split()
|
|
395
|
-
)
|
|
396
|
-
),
|
|
397
|
-
prose_text,
|
|
398
|
-
re.IGNORECASE,
|
|
399
|
-
)
|
|
400
|
-
)
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
def unvalidated_claim_violation(reply_text: str) -> str | None:
|
|
404
|
-
"""Return the deny reason quoting each sentence that guesses or admits a skipped check, or None.
|
|
405
|
-
|
|
406
|
-
A duration counts as a guess unless its sentence cites the run, job, check,
|
|
407
|
-
or pull request that measured it::
|
|
408
|
-
|
|
409
|
-
flag: It should take about 40 minutes.
|
|
410
|
-
ok: The last queue build took 39 minutes, run 18239174012.
|
|
411
|
-
"""
|
|
412
|
-
all_claims: list[str] = []
|
|
413
|
-
for each_sentence in prose_sentences(reply_text):
|
|
414
|
-
prose_text = countable_text(each_sentence).replace(CURLY_APOSTROPHE, STRAIGHT_APOSTROPHE)
|
|
415
|
-
is_guess = UNVALIDATED_CLAIM_PATTERN.search(prose_text) is not None
|
|
416
|
-
is_uncited_duration = (
|
|
417
|
-
DURATION_CLAIM_PATTERN.search(prose_text) is not None
|
|
418
|
-
and CITED_NUMBER_PATTERN.search(each_sentence) is None
|
|
419
|
-
)
|
|
420
|
-
if is_guess or is_uncited_duration:
|
|
421
|
-
all_claims.append(UNVALIDATED_CLAIM_MESSAGE.format(claim=each_sentence))
|
|
422
|
-
return joined_violations(all_claims)
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
def _citations_no_tool_result_shows(reply_text: str, transcript_path: object) -> list[re.Match[str]]:
|
|
426
|
-
all_citations = list(CITED_NUMBER_PATTERN.finditer(FENCED_BLOCK_PATTERN.sub(" ", reply_text)))
|
|
427
|
-
if not all_citations:
|
|
428
|
-
return []
|
|
429
|
-
result_text = successful_result_text(transcript_path)
|
|
430
|
-
if result_text is None:
|
|
431
|
-
return []
|
|
432
|
-
return [
|
|
433
|
-
each_match
|
|
434
|
-
for each_match in all_citations
|
|
435
|
-
if re.search(
|
|
436
|
-
CITED_NUMBER_IN_RESULT_TEMPLATE.format(
|
|
437
|
-
number=next(each_group for each_group in each_match.groups() if each_group)
|
|
438
|
-
),
|
|
439
|
-
result_text,
|
|
440
|
-
)
|
|
441
|
-
is None
|
|
442
|
-
]
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
def unseen_citation_violation(reply_text: str, transcript_path: object) -> str | None:
|
|
446
|
-
"""Return the deny reason for each cited run, job, check, or pull request no tool result shows.
|
|
447
|
-
|
|
448
|
-
Returns None when the text cites nothing or the transcript cannot be read.
|
|
449
|
-
"""
|
|
450
|
-
return joined_violations(
|
|
451
|
-
UNSEEN_CITATION_MESSAGE.format(citation=each_match.group(0))
|
|
452
|
-
for each_match in _citations_no_tool_result_shows(reply_text, transcript_path)
|
|
453
|
-
)
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
def unseen_citation_violation_with_hook_text(reply_text: str, transcript_path: object) -> str | None:
|
|
457
|
-
"""Return the deny reason for each cited run, job, check, or pull request the session never showed.
|
|
458
|
-
|
|
459
|
-
A tool result that shows the number counts for every kind. A ``/pull/N``
|
|
460
|
-
link also counts when hook or harness text shows the same link, such as
|
|
461
|
-
stop-hook feedback or a background agent's task notification. This gate's
|
|
462
|
-
own feedback, which quotes the citation or sentence it blocked, does not count.
|
|
463
|
-
|
|
464
|
-
Returns None when the text cites nothing or the transcript cannot be read.
|
|
465
|
-
"""
|
|
466
|
-
all_unseen = _citations_no_tool_result_shows(reply_text, transcript_path)
|
|
467
|
-
hook_text = (
|
|
468
|
-
REPLY_ECHO_PATTERN.sub(" ", hook_supplied_text(transcript_path) or "")
|
|
469
|
-
if any(each_match[CITED_PULL_NUMBER_GROUP] for each_match in all_unseen)
|
|
470
|
-
else ""
|
|
471
|
-
)
|
|
472
|
-
return joined_violations(
|
|
473
|
-
UNSEEN_CITATION_MESSAGE.format(citation=each_match.group(0))
|
|
474
|
-
for each_match in all_unseen
|
|
475
|
-
if not (
|
|
476
|
-
each_match[CITED_PULL_NUMBER_GROUP]
|
|
477
|
-
and re.search(
|
|
478
|
-
PULL_LINK_IN_HOOK_TEXT_TEMPLATE.format(number=each_match[CITED_PULL_NUMBER_GROUP]),
|
|
479
|
-
hook_text,
|
|
480
|
-
)
|
|
481
|
-
)
|
|
482
|
-
)
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
def all_card_texts(
|
|
486
|
-
all_card_input: dict[str, object],
|
|
487
|
-
all_card_keys: tuple[str, ...],
|
|
488
|
-
all_option_keys: tuple[str, ...],
|
|
489
|
-
) -> list[str]:
|
|
490
|
-
"""Collect the named string fields of a decision card and of each of its options."""
|
|
491
|
-
all_options = all_card_input.get(DECISION_CARD_OPTIONS_KEY)
|
|
492
|
-
all_option_fields = [
|
|
493
|
-
each_option.get(each_key)
|
|
494
|
-
for each_option in (all_options if isinstance(all_options, list) else [])
|
|
495
|
-
if isinstance(each_option, dict)
|
|
496
|
-
for each_key in all_option_keys
|
|
497
|
-
]
|
|
498
|
-
all_card_fields = [all_card_input.get(each_key) for each_key in all_card_keys]
|
|
499
|
-
return [
|
|
500
|
-
each_field
|
|
501
|
-
for each_field in all_card_fields + all_option_fields
|
|
502
|
-
if isinstance(each_field, str)
|
|
503
|
-
]
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
def card_length_violation(all_card_input: dict[str, object]) -> str | None:
|
|
507
|
-
"""Return the deny reason, with the page flow to use, for an over-long or listed card."""
|
|
508
|
-
violation = card_shape_violation(all_card_input)
|
|
509
|
-
return None if violation is None else violation + CARD_FORMAT_FLOW_INSTRUCTION
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
def card_shape_violation(all_card_input: dict[str, object]) -> str | None:
|
|
513
|
-
"""Return the deny reason for each listed or over-long card context and card sentence, or None."""
|
|
514
|
-
context_text = all_card_input.get(DECISION_CARD_CONTEXT_KEY)
|
|
515
|
-
all_violations: list[str | None] = []
|
|
516
|
-
if isinstance(context_text, str):
|
|
517
|
-
if CARD_LIST_ITEM_PATTERN.search(FENCED_BLOCK_PATTERN.sub(" ", context_text)) is not None:
|
|
518
|
-
all_violations.append(CARD_CONTEXT_LIST_MESSAGE)
|
|
519
|
-
context_sentence_count = len(sentence_word_lists(context_text))
|
|
520
|
-
if context_sentence_count > MAXIMUM_CARD_CONTEXT_SENTENCE_COUNT:
|
|
521
|
-
all_violations.append(
|
|
522
|
-
CARD_CONTEXT_TOO_LONG_MESSAGE.format(
|
|
523
|
-
sentence_count=context_sentence_count,
|
|
524
|
-
sentence_limit=MAXIMUM_CARD_CONTEXT_SENTENCE_COUNT,
|
|
525
|
-
)
|
|
526
|
-
)
|
|
527
|
-
all_length_checked_texts = all_card_texts(
|
|
528
|
-
all_card_input, ALL_DECISION_CARD_LENGTH_KEYS, ALL_DECISION_OPTION_LENGTH_KEYS
|
|
529
|
-
)
|
|
530
|
-
all_violations.extend(long_sentence_violation(each_text) for each_text in all_length_checked_texts)
|
|
531
|
-
return joined_violations(all_violations)
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
def all_question_card_texts(all_card_input: dict[str, object]) -> list[str]:
|
|
535
|
-
"""Collect the prose fields of a question card: each question, its header, and each option's text."""
|
|
536
|
-
all_questions = all_card_input.get(QUESTION_CARD_QUESTIONS_KEY)
|
|
537
|
-
all_question_fields: list[object] = []
|
|
538
|
-
for each_question in all_questions if isinstance(all_questions, list) else []:
|
|
539
|
-
if not isinstance(each_question, dict):
|
|
540
|
-
continue
|
|
541
|
-
all_question_fields.extend(each_question.get(each_key) for each_key in ALL_QUESTION_CARD_PROSE_KEYS)
|
|
542
|
-
all_options = each_question.get(DECISION_CARD_OPTIONS_KEY)
|
|
543
|
-
all_question_fields.extend(
|
|
544
|
-
each_option.get(each_key)
|
|
545
|
-
for each_option in (all_options if isinstance(all_options, list) else [])
|
|
546
|
-
if isinstance(each_option, dict)
|
|
547
|
-
for each_key in ALL_QUESTION_OPTION_PROSE_KEYS
|
|
548
|
-
)
|
|
549
|
-
return [each_field for each_field in all_question_fields if isinstance(each_field, str)]
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
def active_mode_rules() -> VisualReplyRules | None:
|
|
553
|
-
"""Return the visual reply rules when the mode is on and the rule file parses."""
|
|
554
|
-
if not mode_enabled(Path.home() / CLAUDE_HOME_DIRECTORY_NAME):
|
|
555
|
-
return None
|
|
556
|
-
return load_rules()
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
def card_violation(card_text: str, mode_rules: VisualReplyRules | None) -> str | None:
|
|
560
|
-
"""Return the deny reason for each problem in a decision card's prose, or None."""
|
|
561
|
-
return collected_violations(
|
|
562
|
-
[
|
|
563
|
-
lambda: banned_word_violation(card_text, configured_banned_words()),
|
|
564
|
-
lambda: unvalidated_claim_violation(card_text),
|
|
565
|
-
lambda: None
|
|
566
|
-
if mode_rules is None
|
|
567
|
-
else abbreviation_violation(prose_outside_links(card_text), mode_rules),
|
|
568
|
-
]
|
|
569
|
-
)
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
def reply_violation(
|
|
573
|
-
reply_text: str, transcript_path: object, mode_rules: VisualReplyRules | None
|
|
574
|
-
) -> str | None:
|
|
575
|
-
"""Return the deny reason for each problem in a reply's text, or None."""
|
|
576
|
-
all_checks: list[Callable[[], str | None]] = [
|
|
577
|
-
lambda: length_violation(reply_text),
|
|
578
|
-
lambda: mid_sentence_colon_violation(reply_text),
|
|
579
|
-
lambda: em_dash_violation(reply_text),
|
|
580
|
-
lambda: unlinked_pull_request_violation(reply_text),
|
|
581
|
-
lambda: unsourced_cause_violation(reply_text),
|
|
582
|
-
lambda: banned_word_violation(reply_text, configured_banned_words()),
|
|
583
|
-
lambda: unvalidated_claim_violation(reply_text),
|
|
584
|
-
lambda: unseen_citation_violation(reply_text, transcript_path),
|
|
585
|
-
]
|
|
586
|
-
if mode_rules is not None:
|
|
587
|
-
all_checks.extend(
|
|
588
|
-
[
|
|
589
|
-
lambda: abbreviation_violation(prose_outside_links(reply_text), mode_rules),
|
|
590
|
-
lambda: second_reply_violation(transcript_path, mode_rules),
|
|
591
|
-
lambda: visual_violation(len(sentence_word_lists(reply_text)), transcript_path, mode_rules),
|
|
592
|
-
]
|
|
593
|
-
)
|
|
594
|
-
return collected_violations(all_checks)
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
def tool_violation(
|
|
598
|
-
tool_name: object, all_tool_input: dict[str, object], transcript_path: object = None
|
|
599
|
-
) -> tuple[str, str] | None:
|
|
600
|
-
"""Return the deny reason and the checked text for one call, or None when it passes."""
|
|
601
|
-
mode_rules = active_mode_rules()
|
|
602
|
-
anchor_violation = (
|
|
603
|
-
widget_anchor_violation(tool_name, all_tool_input)
|
|
604
|
-
if isinstance(tool_name, str) and mode_rules is not None
|
|
605
|
-
else None
|
|
606
|
-
)
|
|
607
|
-
if anchor_violation is None:
|
|
608
|
-
return _text_violation(tool_name, all_tool_input, transcript_path, mode_rules)
|
|
609
|
-
try:
|
|
610
|
-
checked = _text_violation(tool_name, all_tool_input, transcript_path, mode_rules)
|
|
611
|
-
except Exception:
|
|
612
|
-
checked = None
|
|
613
|
-
if checked is None:
|
|
614
|
-
return anchor_violation, str(all_tool_input)
|
|
615
|
-
return FINDING_SEPARATOR.join([anchor_violation, checked[0]]), checked[1]
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
def _text_violation(
|
|
619
|
-
tool_name: object,
|
|
620
|
-
all_tool_input: dict[str, object],
|
|
621
|
-
transcript_path: object,
|
|
622
|
-
mode_rules: VisualReplyRules | None,
|
|
623
|
-
) -> tuple[str, str] | None:
|
|
624
|
-
if tool_name in (DECISION_CARD_TOOL_NAME, QUESTION_CARD_TOOL_NAME):
|
|
625
|
-
return _card_text_violation(tool_name, all_tool_input, transcript_path, mode_rules)
|
|
626
|
-
if tool_name not in ALL_CHECKED_TOOL_NAMES:
|
|
627
|
-
return None
|
|
628
|
-
reply_text = all_tool_input.get(TEXT_KEY)
|
|
629
|
-
if not isinstance(reply_text, str):
|
|
630
|
-
return None
|
|
631
|
-
all_checks = [lambda: reply_violation(reply_text, transcript_path, mode_rules)]
|
|
632
|
-
if tool_name not in ALL_EDIT_TOOL_NAMES:
|
|
633
|
-
all_checks.append(
|
|
634
|
-
lambda: results_picture_violation(
|
|
635
|
-
prose_outside_links(reply_text),
|
|
636
|
-
reply_text,
|
|
637
|
-
all_tool_input,
|
|
638
|
-
mode_rules if mode_rules is not None else load_rules(),
|
|
639
|
-
)
|
|
640
|
-
)
|
|
641
|
-
violation = collected_violations(all_checks)
|
|
642
|
-
return None if violation is None else (violation, reply_text)
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
def _card_text_violation(
|
|
646
|
-
tool_name: object,
|
|
647
|
-
all_tool_input: dict[str, object],
|
|
648
|
-
transcript_path: object,
|
|
649
|
-
mode_rules: VisualReplyRules | None,
|
|
650
|
-
) -> tuple[str, str] | None:
|
|
651
|
-
if tool_name == DECISION_CARD_TOOL_NAME:
|
|
652
|
-
all_texts = all_card_texts(all_tool_input, ALL_DECISION_CARD_PROSE_KEYS, ALL_DECISION_OPTION_PROSE_KEYS)
|
|
653
|
-
shape_violation = card_length_violation(all_tool_input)
|
|
654
|
-
else:
|
|
655
|
-
all_texts = all_question_card_texts(all_tool_input)
|
|
656
|
-
shape_violation = None
|
|
657
|
-
card_text = CARD_TEXT_SEPARATOR.join(all_texts)
|
|
658
|
-
violation = collected_violations(
|
|
659
|
-
[
|
|
660
|
-
lambda: shape_violation,
|
|
661
|
-
lambda: card_violation(card_text, mode_rules),
|
|
662
|
-
lambda: unseen_citation_violation(card_text, transcript_path),
|
|
663
|
-
]
|
|
664
|
-
)
|
|
665
|
-
return None if violation is None else (violation, card_text)
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
def github_comment_texts(tool_name: object, all_tool_input: dict[str, object]) -> list[str]:
|
|
669
|
-
"""Return the GitHub comment texts one call would post, or an empty list.
|
|
670
|
-
|
|
671
|
-
::
|
|
672
|
-
|
|
673
|
-
mcp__github__add_issue_comment {"body": "Done."} -> ["Done."]
|
|
674
|
-
Bash 'gh pr comment 12 --body "Done."' -> ["Done."]
|
|
675
|
-
Bash 'git push && gh pr comment 12 --body "Done."' -> ["Done."]
|
|
676
|
-
Bash 'gh pr create --body "Long body"' -> []
|
|
677
|
-
"""
|
|
678
|
-
if tool_name in ALL_CHECKED_MCP_TOOL_NAMES:
|
|
679
|
-
return extract_mcp_body_texts(all_tool_input)
|
|
680
|
-
command = all_tool_input.get(COMMAND_KEY)
|
|
681
|
-
if tool_name != BASH_TOOL_NAME or not isinstance(command, str):
|
|
682
|
-
return []
|
|
683
|
-
try:
|
|
684
|
-
command_lexer = shlex.shlex(command, posix=True, punctuation_chars=True)
|
|
685
|
-
command_lexer.whitespace_split = True
|
|
686
|
-
all_command_tokens = list(command_lexer)
|
|
687
|
-
except ValueError:
|
|
688
|
-
return []
|
|
689
|
-
return gh_comment_bodies(all_command_tokens)
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
def gh_comment_bodies(all_command_tokens: list[str]) -> list[str]:
|
|
693
|
-
"""Return the ``--body`` values of each ``gh pr|issue comment|review`` call in the tokens.
|
|
694
|
-
|
|
695
|
-
::
|
|
696
|
-
|
|
697
|
-
git push && gh -R o/r pr comment 12 --body "Done." -> ["Done."]
|
|
698
|
-
gh pr create --body "Long body" -> []
|
|
699
|
-
"""
|
|
700
|
-
all_bodies: list[str] = []
|
|
701
|
-
for each_command_tokens in gh_command_token_runs(all_command_tokens):
|
|
702
|
-
all_pairs = list(itertools.pairwise(each_command_tokens))
|
|
703
|
-
if not any(
|
|
704
|
-
each_group in ALL_GH_COMMENT_GROUPS and each_action in ALL_GH_COMMENT_ACTIONS
|
|
705
|
-
for each_group, each_action in all_pairs
|
|
706
|
-
):
|
|
707
|
-
continue
|
|
708
|
-
all_bodies.extend(
|
|
709
|
-
each_token[len(GH_BODY_FLAG_PREFIX):]
|
|
710
|
-
for each_token in each_command_tokens
|
|
711
|
-
if each_token.startswith(GH_BODY_FLAG_PREFIX)
|
|
712
|
-
)
|
|
713
|
-
all_bodies.extend(each_value for each_flag, each_value in all_pairs if each_flag in ALL_GH_BODY_FLAGS)
|
|
714
|
-
return all_bodies
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
def is_command_token(token: str) -> bool:
|
|
718
|
-
"""Return True when the token is part of a command, False for ``&&``, ``||``, ``;`` or ``|``."""
|
|
719
|
-
return not set(token) <= SHELL_OPERATOR_CHARACTERS
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
def gh_command_token_runs(all_command_tokens: list[str]) -> list[list[str]]:
|
|
723
|
-
"""Return the tokens after each ``gh`` token, up to the next shell operator."""
|
|
724
|
-
all_runs: list[list[str]] = []
|
|
725
|
-
for each_index, each_token in enumerate(all_command_tokens):
|
|
726
|
-
if each_token == GH_PROGRAM_NAME:
|
|
727
|
-
all_runs.append(list(itertools.takewhile(is_command_token, all_command_tokens[each_index + 1:])))
|
|
728
|
-
return all_runs
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
def comment_length_violation(comment_text: str) -> str | None:
|
|
732
|
-
"""Return the deny reason for one over-long GitHub comment text, or None when it fits.
|
|
733
|
-
|
|
734
|
-
Fenced code blocks and collapsed ``<details>`` blocks carry no words.
|
|
735
|
-
"""
|
|
736
|
-
violation = length_violation(DETAILS_BLOCK_PATTERN.sub(" ", comment_text))
|
|
737
|
-
return None if violation is None else violation + COMMENT_RETRY_INSTRUCTION
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
def github_comment_violation(tool_name: object, all_tool_input: dict[str, object]) -> tuple[str, str] | None:
|
|
741
|
-
"""Return the deny reason and text for every over-long GitHub comment, or None.
|
|
742
|
-
|
|
743
|
-
Fenced code blocks and collapsed ``<details>`` blocks carry no words.
|
|
744
|
-
"""
|
|
745
|
-
all_over_long = [
|
|
746
|
-
(violation, each_comment_text)
|
|
747
|
-
for each_comment_text in github_comment_texts(tool_name, all_tool_input)
|
|
748
|
-
if (violation := comment_length_violation(each_comment_text)) is not None
|
|
749
|
-
]
|
|
750
|
-
if not all_over_long:
|
|
751
|
-
return None
|
|
752
|
-
return (
|
|
753
|
-
FINDING_SEPARATOR.join(each_violation for each_violation, _ in all_over_long),
|
|
754
|
-
CARD_TEXT_SEPARATOR.join(each_text for _, each_text in all_over_long),
|
|
755
|
-
)
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
def write_flowchart_advice(tool_name: object, all_tool_input: dict[str, object]) -> None:
|
|
759
|
-
"""Print the flowchart advice for an allowed widget call that is not a flowchart."""
|
|
760
|
-
mode_rules = active_mode_rules()
|
|
761
|
-
if not isinstance(tool_name, str) or mode_rules is None:
|
|
762
|
-
return
|
|
763
|
-
advice = widget_flowchart_advice(tool_name, all_tool_input, mode_rules)
|
|
764
|
-
if advice is None:
|
|
765
|
-
return
|
|
766
|
-
hook_event_output = {HOOK_EVENT_NAME_KEY: HOOK_EVENT_NAME, ADDITIONAL_CONTEXT_KEY: advice}
|
|
767
|
-
sys.stdout.write(json.dumps({HOOK_SPECIFIC_OUTPUT_KEY: hook_event_output}))
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
def main() -> int:
|
|
771
|
-
hook_input = read_hook_input_dictionary_from_stdin()
|
|
772
|
-
if hook_input is None:
|
|
773
|
-
return ALLOW_EXIT_CODE
|
|
774
|
-
tool_name = hook_input.get(TOOL_NAME_KEY)
|
|
775
|
-
tool_input = hook_input.get(TOOL_INPUT_KEY)
|
|
776
|
-
if not isinstance(tool_input, dict):
|
|
777
|
-
return ALLOW_EXIT_CODE
|
|
778
|
-
checked = github_comment_violation(tool_name, tool_input) or tool_violation(
|
|
779
|
-
tool_name, tool_input, hook_input.get(TRANSCRIPT_PATH_KEY)
|
|
780
|
-
)
|
|
781
|
-
if checked is None:
|
|
782
|
-
write_flowchart_advice(tool_name, tool_input)
|
|
783
|
-
return ALLOW_EXIT_CODE
|
|
784
|
-
violation, reply_text = checked
|
|
785
|
-
numbered_violation = numbered_violations(violation)
|
|
786
|
-
instruction = RETRY_INSTRUCTION + FIX_EVERY_ITEM_INSTRUCTION
|
|
787
|
-
block_reason = (
|
|
788
|
-
numbered_violation + FINDING_SEPARATOR + instruction.lstrip()
|
|
789
|
-
if FINDING_SEPARATOR in numbered_violation
|
|
790
|
-
else numbered_violation + instruction
|
|
791
|
-
)
|
|
792
|
-
log_hook_block(
|
|
793
|
-
Path(__file__).name,
|
|
794
|
-
HOOK_EVENT_NAME,
|
|
795
|
-
block_reason,
|
|
796
|
-
tool_name=str(tool_name),
|
|
797
|
-
offending_input_preview=reply_text,
|
|
798
|
-
)
|
|
799
|
-
sys.stderr.write(block_reason)
|
|
800
|
-
return BLOCK_EXIT_CODE
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
if __name__ == "__main__":
|
|
804
|
-
sys.exit(main())
|