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,260 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env python3
|
|
2
|
-
"""Hook that shows the user one short chat reply per message.
|
|
3
|
-
|
|
4
|
-
``reply_length_gate.py`` checks only the chat tools that post to Slack.
|
|
5
|
-
A reply typed straight into the chat is no tool call, so this hook runs
|
|
6
|
-
the same checks on it::
|
|
7
|
-
|
|
8
|
-
UserPromptSubmit, first of the session -> additionalContext states the rules
|
|
9
|
-
UserPromptSubmit, later -> only the draft the user saw, if any
|
|
10
|
-
MessageDisplay, batch before the last -> hold the text, show nothing yet
|
|
11
|
-
MessageDisplay, last batch, passes -> show the whole message
|
|
12
|
-
MessageDisplay, last batch, fails -> show a draft from a small model that passes
|
|
13
|
-
|
|
14
|
-
A Stop hook would see the reply only after it is on screen, so each block
|
|
15
|
-
would add a second reply under the first. MessageDisplay changes the text
|
|
16
|
-
before it renders. The transcript keeps the original, so the next
|
|
17
|
-
UserPromptSubmit tells the agent the text the user saw.
|
|
18
|
-
|
|
19
|
-
The draft comes from ``claude -p`` on DEFAULT_DRAFTER_MODEL with no setting
|
|
20
|
-
sources and no tools. The draft must pass ``reply_violation``; after
|
|
21
|
-
MAXIMUM_DRAFT_ATTEMPTS failed drafts, the original shows.
|
|
22
|
-
|
|
23
|
-
The checks come from ``reply_length_gate.reply_violation``: at most
|
|
24
|
-
MAXIMUM_SENTENCE_COUNT sentences of at most MAXIMUM_WORDS_PER_SENTENCE
|
|
25
|
-
words, no mid-sentence colon, no em dash, linked pull requests, a source
|
|
26
|
-
for each causal claim, and no banned word.
|
|
27
|
-
"""
|
|
28
|
-
|
|
29
|
-
from __future__ import annotations
|
|
30
|
-
|
|
31
|
-
import json
|
|
32
|
-
import os
|
|
33
|
-
import shutil
|
|
34
|
-
import subprocess
|
|
35
|
-
import sys
|
|
36
|
-
import tempfile
|
|
37
|
-
import uuid
|
|
38
|
-
from pathlib import Path
|
|
39
|
-
|
|
40
|
-
hooks_root_directory = str(Path(__file__).resolve().parent.parent)
|
|
41
|
-
if hooks_root_directory not in sys.path:
|
|
42
|
-
sys.path.insert(0, hooks_root_directory)
|
|
43
|
-
blocking_directory = str(Path(__file__).resolve().parent)
|
|
44
|
-
if blocking_directory not in sys.path:
|
|
45
|
-
sys.path.insert(0, blocking_directory)
|
|
46
|
-
|
|
47
|
-
from once_per_session import claim_first_showing, clear_showings
|
|
48
|
-
from reply_length_gate import reply_violation
|
|
49
|
-
from hooks_constants.chat_reply_gate_constants import (
|
|
50
|
-
CLAUDE_SESSION_ID_ENV_VAR,
|
|
51
|
-
DEFAULT_DRAFTER_MODEL,
|
|
52
|
-
DELTA_KEY,
|
|
53
|
-
DISPLAY_CONTENT_KEY,
|
|
54
|
-
DRAFTER_GUARD_ENV_VAR,
|
|
55
|
-
DRAFTER_MODEL_ENV_VAR,
|
|
56
|
-
DRAFTER_REQUEST_TEMPLATE,
|
|
57
|
-
DRAFTER_ERROR_KEY,
|
|
58
|
-
DRAFTER_EXECUTABLE_NAME,
|
|
59
|
-
DRAFTER_RESULT_KEY,
|
|
60
|
-
DRAFTER_RULES,
|
|
61
|
-
DRAFTER_TIMEOUT_SECONDS,
|
|
62
|
-
FINAL_KEY,
|
|
63
|
-
HELD_TEXT_FILE_PREFIX,
|
|
64
|
-
HOOK_EVENT_NAME_INPUT_KEY,
|
|
65
|
-
MAXIMUM_DRAFT_ATTEMPTS,
|
|
66
|
-
MESSAGE_DISPLAY_EVENT_NAME,
|
|
67
|
-
MESSAGE_ID_KEY,
|
|
68
|
-
REPLY_LIMIT_ONCE_MARKER_NAME,
|
|
69
|
-
REPLY_LIMIT_REMINDER,
|
|
70
|
-
SESSION_END_EVENT_NAME,
|
|
71
|
-
SESSION_ID_KEY,
|
|
72
|
-
SHOWN_DRAFT_FILE_PREFIX,
|
|
73
|
-
SHOWN_DRAFT_HEADING,
|
|
74
|
-
SHOWN_DRAFT_PREFIX,
|
|
75
|
-
STATE_FILE_ENCODING,
|
|
76
|
-
STATE_FILE_SUFFIX,
|
|
77
|
-
USER_PROMPT_SUBMIT_EVENT_NAME,
|
|
78
|
-
)
|
|
79
|
-
from hooks_constants.hook_block_logger import log_hook_block
|
|
80
|
-
from hooks_constants.hook_specific_output_keys import (
|
|
81
|
-
HOOK_EVENT_NAME_KEY,
|
|
82
|
-
HOOK_SPECIFIC_OUTPUT_KEY,
|
|
83
|
-
)
|
|
84
|
-
from hooks_constants.pre_tool_use_stdin import read_hook_input_dictionary_from_stdin
|
|
85
|
-
from hooks_constants.reply_length_gate_constants import (
|
|
86
|
-
ADDITIONAL_CONTEXT_KEY,
|
|
87
|
-
ALLOW_EXIT_CODE,
|
|
88
|
-
)
|
|
89
|
-
from hooks_constants.session_file_id import session_file_id
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
def state_file_path(file_prefix: str, state_key: object) -> Path:
|
|
93
|
-
"""Return the temp file that holds one message's held text or one session's shown draft."""
|
|
94
|
-
file_name = f"{file_prefix}{session_file_id(state_key)}{STATE_FILE_SUFFIX}"
|
|
95
|
-
return Path(tempfile.gettempdir()) / file_name
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
def read_state_text(state_path: Path) -> str:
|
|
99
|
-
"""Return the stored text, or an empty string when the file is absent or unreadable."""
|
|
100
|
-
try:
|
|
101
|
-
return state_path.read_text(encoding=STATE_FILE_ENCODING)
|
|
102
|
-
except OSError:
|
|
103
|
-
return ""
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
def write_state_text(state_path: Path, state_text: str) -> None:
|
|
107
|
-
"""Store the text; a failed write leaves the shown text unchanged."""
|
|
108
|
-
try:
|
|
109
|
-
state_path.write_text(state_text, encoding=STATE_FILE_ENCODING)
|
|
110
|
-
except OSError:
|
|
111
|
-
return
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
def drafter_command() -> list[str]:
|
|
115
|
-
"""Return the headless claude command that drafts with no settings and no tools."""
|
|
116
|
-
return [
|
|
117
|
-
shutil.which(DRAFTER_EXECUTABLE_NAME) or DRAFTER_EXECUTABLE_NAME,
|
|
118
|
-
"-p",
|
|
119
|
-
"--model",
|
|
120
|
-
os.environ.get(DRAFTER_MODEL_ENV_VAR, DEFAULT_DRAFTER_MODEL),
|
|
121
|
-
"--setting-sources",
|
|
122
|
-
"",
|
|
123
|
-
"--tools",
|
|
124
|
-
"",
|
|
125
|
-
"--no-session-persistence",
|
|
126
|
-
"--session-id",
|
|
127
|
-
str(uuid.uuid4()),
|
|
128
|
-
"--output-format",
|
|
129
|
-
"json",
|
|
130
|
-
"--system-prompt",
|
|
131
|
-
DRAFTER_RULES,
|
|
132
|
-
]
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
def drafter_environment() -> dict[str, str]:
|
|
136
|
-
"""Return the drafter's environment, marked so this hook exits inside it."""
|
|
137
|
-
environment = dict(os.environ)
|
|
138
|
-
environment[DRAFTER_GUARD_ENV_VAR] = "1"
|
|
139
|
-
environment.pop(CLAUDE_SESSION_ID_ENV_VAR, None)
|
|
140
|
-
return environment
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
def ask_drafter(reply_text: str, violation: str) -> str | None:
|
|
144
|
-
"""Return one draft from the small model, or None when the call fails."""
|
|
145
|
-
try:
|
|
146
|
-
completed = subprocess.run(
|
|
147
|
-
drafter_command(),
|
|
148
|
-
input=DRAFTER_REQUEST_TEMPLATE.format(violation=violation, reply_text=reply_text),
|
|
149
|
-
capture_output=True,
|
|
150
|
-
text=True,
|
|
151
|
-
timeout=DRAFTER_TIMEOUT_SECONDS,
|
|
152
|
-
env=drafter_environment(),
|
|
153
|
-
check=False,
|
|
154
|
-
)
|
|
155
|
-
drafter_output = json.loads(completed.stdout)
|
|
156
|
-
draft_text = drafter_output.get(DRAFTER_RESULT_KEY)
|
|
157
|
-
except (OSError, subprocess.TimeoutExpired, json.JSONDecodeError, AttributeError):
|
|
158
|
-
return None
|
|
159
|
-
if drafter_output.get(DRAFTER_ERROR_KEY) is True:
|
|
160
|
-
return None
|
|
161
|
-
if not isinstance(draft_text, str) or not draft_text.strip():
|
|
162
|
-
return None
|
|
163
|
-
return draft_text.strip()
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
def compliant_draft(reply_text: str, violation: str) -> str | None:
|
|
167
|
-
"""Return a draft that passes ``reply_violation``, or None after MAXIMUM_DRAFT_ATTEMPTS."""
|
|
168
|
-
current_violation = violation
|
|
169
|
-
for _ in range(MAXIMUM_DRAFT_ATTEMPTS):
|
|
170
|
-
draft_text = ask_drafter(reply_text, current_violation)
|
|
171
|
-
if draft_text is None:
|
|
172
|
-
continue
|
|
173
|
-
draft_violation = reply_violation(draft_text, None, None)
|
|
174
|
-
if draft_violation is None:
|
|
175
|
-
return draft_text
|
|
176
|
-
current_violation = draft_violation
|
|
177
|
-
return None
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
def shown_text(all_hook_input: dict[str, object]) -> str | None:
|
|
181
|
-
"""Return the text to show for this batch, or None to show the batch as it is."""
|
|
182
|
-
message_id = all_hook_input.get(MESSAGE_ID_KEY)
|
|
183
|
-
if not isinstance(message_id, str) or not message_id:
|
|
184
|
-
return None
|
|
185
|
-
held_path = state_file_path(HELD_TEXT_FILE_PREFIX, message_id)
|
|
186
|
-
held_text = read_state_text(held_path)
|
|
187
|
-
message_text = held_text + str(all_hook_input.get(DELTA_KEY) or "")
|
|
188
|
-
if not all_hook_input.get(FINAL_KEY):
|
|
189
|
-
write_state_text(held_path, message_text)
|
|
190
|
-
return ""
|
|
191
|
-
held_path.unlink(missing_ok=True)
|
|
192
|
-
shown_draft_path = state_file_path(SHOWN_DRAFT_FILE_PREFIX, all_hook_input.get(SESSION_ID_KEY))
|
|
193
|
-
violation = reply_violation(message_text, None, None)
|
|
194
|
-
draft_text = None if violation is None else compliant_draft(message_text, violation)
|
|
195
|
-
if violation is None or draft_text is None:
|
|
196
|
-
shown_draft_path.unlink(missing_ok=True)
|
|
197
|
-
return message_text if held_text else None
|
|
198
|
-
write_state_text(shown_draft_path, draft_text)
|
|
199
|
-
log_hook_block(
|
|
200
|
-
Path(__file__).name,
|
|
201
|
-
MESSAGE_DISPLAY_EVENT_NAME,
|
|
202
|
-
violation,
|
|
203
|
-
offending_input_preview=message_text,
|
|
204
|
-
)
|
|
205
|
-
return draft_text
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
def write_hook_output(event_name: str, output_key: str, output_text: str) -> None:
|
|
209
|
-
"""Print one hookSpecificOutput field for the event."""
|
|
210
|
-
hook_event_output = {HOOK_EVENT_NAME_KEY: event_name, output_key: output_text}
|
|
211
|
-
sys.stdout.write(json.dumps({HOOK_SPECIFIC_OUTPUT_KEY: hook_event_output}))
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
def reply_limit_reminder(all_hook_fields: dict[str, object]) -> str:
|
|
215
|
-
"""Return the reply rules on a session's first prompt, plus the draft the user saw in place of the last reply.
|
|
216
|
-
|
|
217
|
-
The rules show on the first prompt of a session and on the first prompt
|
|
218
|
-
after each compaction; other prompts carry only a shown draft, or nothing.
|
|
219
|
-
"""
|
|
220
|
-
reminder = (
|
|
221
|
-
REPLY_LIMIT_REMINDER
|
|
222
|
-
if claim_first_showing(REPLY_LIMIT_ONCE_MARKER_NAME, all_hook_fields)
|
|
223
|
-
else ""
|
|
224
|
-
)
|
|
225
|
-
shown_draft_path = state_file_path(SHOWN_DRAFT_FILE_PREFIX, all_hook_fields.get(SESSION_ID_KEY))
|
|
226
|
-
shown_draft = read_state_text(shown_draft_path)
|
|
227
|
-
if not shown_draft:
|
|
228
|
-
return reminder
|
|
229
|
-
shown_draft_path.unlink(missing_ok=True)
|
|
230
|
-
if not reminder:
|
|
231
|
-
return SHOWN_DRAFT_HEADING + shown_draft
|
|
232
|
-
return reminder + SHOWN_DRAFT_PREFIX + shown_draft
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
def main() -> int:
|
|
236
|
-
if os.environ.get(DRAFTER_GUARD_ENV_VAR):
|
|
237
|
-
return ALLOW_EXIT_CODE
|
|
238
|
-
hook_input = read_hook_input_dictionary_from_stdin()
|
|
239
|
-
if hook_input is None:
|
|
240
|
-
return ALLOW_EXIT_CODE
|
|
241
|
-
event_name = hook_input.get(HOOK_EVENT_NAME_INPUT_KEY)
|
|
242
|
-
if event_name == USER_PROMPT_SUBMIT_EVENT_NAME:
|
|
243
|
-
prompt_context = reply_limit_reminder(hook_input)
|
|
244
|
-
if prompt_context:
|
|
245
|
-
write_hook_output(USER_PROMPT_SUBMIT_EVENT_NAME, ADDITIONAL_CONTEXT_KEY, prompt_context)
|
|
246
|
-
return ALLOW_EXIT_CODE
|
|
247
|
-
if event_name == SESSION_END_EVENT_NAME:
|
|
248
|
-
state_file_path(SHOWN_DRAFT_FILE_PREFIX, hook_input.get(SESSION_ID_KEY)).unlink(missing_ok=True)
|
|
249
|
-
clear_showings(hook_input.get(SESSION_ID_KEY))
|
|
250
|
-
return ALLOW_EXIT_CODE
|
|
251
|
-
if event_name != MESSAGE_DISPLAY_EVENT_NAME:
|
|
252
|
-
return ALLOW_EXIT_CODE
|
|
253
|
-
display_text = shown_text(hook_input)
|
|
254
|
-
if display_text is not None:
|
|
255
|
-
write_hook_output(MESSAGE_DISPLAY_EVENT_NAME, DISPLAY_CONTENT_KEY, display_text)
|
|
256
|
-
return ALLOW_EXIT_CODE
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
if __name__ == "__main__":
|
|
260
|
-
sys.exit(main())
|