claude-dev-env 8.119.2 → 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/gh_api_budget_gate.py +286 -0
- package/hooks/blocking/test_auto_mode_refusal_stop_gate.py +484 -0
- package/hooks/blocking/test_bash_dispatcher_interpreter_starts.py +8 -3
- package/hooks/blocking/test_bash_pre_tool_use_dispatcher.py +1 -0
- package/hooks/blocking/test_gh_api_budget_gate.py +602 -0
- package/hooks/features/README.md +1 -1
- package/hooks/features/bash-dispatchers.md +3 -0
- 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/hooks_constants/bash_pre_tool_use_dispatcher_constants.py +4 -0
- package/hooks/hooks_constants/gh_api_budget.py +186 -0
- package/hooks/hooks_constants/gh_api_budget_gate_constants.py +215 -0
- package/hooks/hooks_constants/gh_argument_lists.py +169 -0
- package/hooks/hooks_constants/gh_shell_loop_scan.py +386 -0
- package/hooks/hooks_constants/test_bash_pre_tool_use_dispatcher_constants.py +5 -1
- package/hooks/hooks_constants/test_gh_api_budget.py +125 -0
- package/hooks/hooks_constants/test_gh_argument_lists.py +49 -0
- package/hooks/hooks_constants/test_gh_shell_loop_scan.py +50 -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
|
@@ -16,26 +16,268 @@ if advisory_directory not in sys.path:
|
|
|
16
16
|
import auto_mode_denial_quick_fix
|
|
17
17
|
|
|
18
18
|
|
|
19
|
+
@pytest.fixture(autouse=True)
|
|
20
|
+
def scratch_config_directory(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
|
|
21
|
+
config_directory = tmp_path / "claude-config"
|
|
22
|
+
config_directory.mkdir()
|
|
23
|
+
monkeypatch.setenv("CLAUDE_CONFIG_DIR", str(config_directory))
|
|
24
|
+
monkeypatch.setenv("HOME", str(config_directory))
|
|
25
|
+
return config_directory
|
|
26
|
+
|
|
27
|
+
|
|
19
28
|
def _denial_payload(**overrides: object) -> dict[str, object]:
|
|
20
29
|
payload: dict[str, object] = {
|
|
30
|
+
"session_id": "session-1",
|
|
31
|
+
"transcript_path": "/scratch/transcript.jsonl",
|
|
32
|
+
"cwd": "/scratch",
|
|
33
|
+
"permission_mode": "auto",
|
|
21
34
|
"hook_event_name": "PermissionDenied",
|
|
22
35
|
"tool_name": "mcp__github__resolve_review_thread",
|
|
23
36
|
"tool_input": {"threadId": "PRRT_abc"},
|
|
24
|
-
"
|
|
25
|
-
"
|
|
37
|
+
"tool_use_id": "toolu_1",
|
|
38
|
+
"reason": "[Security Weaken] resolving review threads bypasses review",
|
|
26
39
|
}
|
|
27
40
|
payload.update(overrides)
|
|
28
41
|
return payload
|
|
29
42
|
|
|
30
43
|
|
|
31
|
-
def
|
|
32
|
-
|
|
33
|
-
|
|
44
|
+
def _hook_output_from_stdin(
|
|
45
|
+
payload: dict[str, object],
|
|
46
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
47
|
+
capsys: pytest.CaptureFixture[str],
|
|
48
|
+
) -> dict[str, object]:
|
|
49
|
+
monkeypatch.setattr(sys, "stdin", io.StringIO(json.dumps(payload)))
|
|
50
|
+
assert auto_mode_denial_quick_fix.main() == 0
|
|
51
|
+
return json.loads(capsys.readouterr().out)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
@pytest.mark.parametrize(
|
|
55
|
+
("reason", "expected_rule_name", "expects_allow_block"),
|
|
56
|
+
[
|
|
57
|
+
("[Auto-Mode Bypass]", "Auto-Mode Bypass", True),
|
|
58
|
+
("[Git Destructive] force push to a shared branch", "Git Destructive", True),
|
|
59
|
+
(
|
|
60
|
+
"Unauthorized Persistence, because the script adds a cron line.",
|
|
61
|
+
"Unauthorized Persistence",
|
|
62
|
+
True,
|
|
63
|
+
),
|
|
64
|
+
(
|
|
65
|
+
"the command force-cancels workflow runs in bulk.",
|
|
66
|
+
"the command force-cancels workflow runs in bulk",
|
|
67
|
+
True,
|
|
68
|
+
),
|
|
69
|
+
(
|
|
70
|
+
"Force Push to a shared branch is destructive.",
|
|
71
|
+
"Force Push to a shared branch is destructive",
|
|
72
|
+
True,
|
|
73
|
+
),
|
|
74
|
+
(
|
|
75
|
+
"the script's [0] entry rewrites every workflow file in the repository"
|
|
76
|
+
" and then deletes the backup copies. It runs nightly.",
|
|
77
|
+
"the scripts 0 entry rewrites every workflow file in the repository and then d...",
|
|
78
|
+
True,
|
|
79
|
+
),
|
|
80
|
+
(
|
|
81
|
+
"Auto mode could not evaluate this action because the transcript was too long.",
|
|
82
|
+
"unnamed rule",
|
|
83
|
+
False,
|
|
84
|
+
),
|
|
85
|
+
("Classifier unavailable", "unnamed rule", False),
|
|
86
|
+
],
|
|
87
|
+
)
|
|
88
|
+
def test_should_name_the_rule_and_branch_for_each_recorded_reason(
|
|
89
|
+
reason: str,
|
|
90
|
+
expected_rule_name: str,
|
|
91
|
+
expects_allow_block: bool,
|
|
92
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
93
|
+
capsys: pytest.CaptureFixture[str],
|
|
94
|
+
) -> None:
|
|
95
|
+
hook_output = _hook_output_from_stdin(
|
|
96
|
+
_denial_payload(tool_name="Bash", tool_input={"command": "gh run cancel 1"}, reason=reason),
|
|
97
|
+
monkeypatch,
|
|
98
|
+
capsys,
|
|
99
|
+
)
|
|
100
|
+
context_text = hook_output["hookSpecificOutput"]["additionalContext"]
|
|
101
|
+
assert hook_output["systemMessage"] == (
|
|
102
|
+
f"Auto mode blocked Bash [{expected_rule_name}]. Claude will propose a quick fix."
|
|
103
|
+
)
|
|
104
|
+
assert f"Denial reason: {reason}" in context_text
|
|
105
|
+
assert ("```powershell" in context_text) is expects_allow_block
|
|
106
|
+
if expects_allow_block:
|
|
107
|
+
assert f"rule [{expected_rule_name}]" in context_text
|
|
108
|
+
assert f"{expected_rule_name} exception: Bash calls like `gh run cancel 1`" in context_text
|
|
109
|
+
else:
|
|
110
|
+
assert "no classifier verdict" in context_text
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def test_should_parse_a_bare_label_with_its_prose() -> None:
|
|
114
|
+
assert auto_mode_denial_quick_fix.parse_denial_reason(
|
|
115
|
+
"Unauthorized Persistence, because the script adds a cron line."
|
|
116
|
+
) == auto_mode_denial_quick_fix.DenialReason(
|
|
117
|
+
rule_label="Unauthorized Persistence",
|
|
118
|
+
has_verdict=True,
|
|
119
|
+
prose="because the script adds a cron line.",
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
@pytest.mark.parametrize(
|
|
124
|
+
"reason",
|
|
125
|
+
[
|
|
126
|
+
"Reason: [Git Destructive] force push",
|
|
127
|
+
"Permission for this action was denied by the Claude Code auto mode classifier. "
|
|
128
|
+
"Reason: [Git Destructive] force push",
|
|
129
|
+
],
|
|
130
|
+
)
|
|
131
|
+
def test_should_read_the_same_label_after_a_leading_reason_marker(reason: str) -> None:
|
|
132
|
+
assert auto_mode_denial_quick_fix.parse_denial_reason(reason) == (
|
|
133
|
+
auto_mode_denial_quick_fix.DenialReason(
|
|
134
|
+
rule_label="Git Destructive", has_verdict=True, prose="force push"
|
|
135
|
+
)
|
|
34
136
|
)
|
|
35
137
|
|
|
36
138
|
|
|
37
|
-
def
|
|
38
|
-
|
|
139
|
+
def _flow_on(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
|
|
140
|
+
playbook_path = tmp_path / "agent-channels" / "docs" / "auto-mode-rule.md"
|
|
141
|
+
playbook_path.parent.mkdir(parents=True)
|
|
142
|
+
playbook_path.write_text("# Playbook\n", encoding="utf-8")
|
|
143
|
+
repository_root = playbook_path.parent.parent
|
|
144
|
+
(repository_root / ".git").mkdir()
|
|
145
|
+
(repository_root / "setup").mkdir()
|
|
146
|
+
(repository_root / "setup" / "add_auto_mode_rule.py").write_text("", encoding="utf-8")
|
|
147
|
+
monkeypatch.setenv("CLAUDE_AUTO_MODE_RULE_PLAYBOOK", str(playbook_path))
|
|
148
|
+
return playbook_path
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def _ledger_payload(tmp_path: Path, **overrides: object) -> dict[str, object]:
|
|
152
|
+
all_fields: dict[str, object] = {
|
|
153
|
+
"tool_name": "Bash",
|
|
154
|
+
"tool_input": {"command": "git push -f origin claude/x"},
|
|
155
|
+
"reason": "[Git Destructive] force push",
|
|
156
|
+
"transcript_path": str(tmp_path / "transcript.jsonl"),
|
|
157
|
+
}
|
|
158
|
+
all_fields.update(overrides)
|
|
159
|
+
return _denial_payload(**all_fields)
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def test_should_record_one_entry_and_return_its_question(
|
|
163
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, scratch_config_directory: Path
|
|
164
|
+
) -> None:
|
|
165
|
+
_flow_on(tmp_path, monkeypatch)
|
|
166
|
+
expected_question = (
|
|
167
|
+
"Auto mode refused 1 Bash step under the Git Destructive rule. "
|
|
168
|
+
"Should auto mode have refused it?"
|
|
169
|
+
)
|
|
170
|
+
for _ in range(2):
|
|
171
|
+
context_text = auto_mode_denial_quick_fix.hook_output_with_ledger(
|
|
172
|
+
_ledger_payload(tmp_path)
|
|
173
|
+
)["hookSpecificOutput"]["additionalContext"]
|
|
174
|
+
assert expected_question in context_text
|
|
175
|
+
ledger_directory = scratch_config_directory / "state" / "auto-mode-refusals" / "session-1"
|
|
176
|
+
all_entry_files = [
|
|
177
|
+
each_path
|
|
178
|
+
for each_path in ledger_directory.glob("*.json")
|
|
179
|
+
if not each_path.name.startswith("question-")
|
|
180
|
+
]
|
|
181
|
+
assert len(all_entry_files) == 1
|
|
182
|
+
assert json.loads(all_entry_files[0].read_text(encoding="utf-8"))["answer"] is None
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
@pytest.mark.parametrize(
|
|
186
|
+
"overrides",
|
|
187
|
+
[
|
|
188
|
+
{"reason": "the command cancels runs."},
|
|
189
|
+
{"tool_input": {"command": "python3 setup/add_auto_mode_rule.py add --rule-file r.txt"}},
|
|
190
|
+
],
|
|
191
|
+
)
|
|
192
|
+
def test_should_record_nothing_for_prose_or_the_rule_tool(
|
|
193
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, overrides: dict[str, object]
|
|
194
|
+
) -> None:
|
|
195
|
+
playbook_path = _flow_on(tmp_path, monkeypatch)
|
|
196
|
+
payload = _ledger_payload(tmp_path, cwd=str(playbook_path.parent.parent), **overrides)
|
|
197
|
+
assert auto_mode_denial_quick_fix.hook_output_with_ledger(
|
|
198
|
+
payload
|
|
199
|
+
) == auto_mode_denial_quick_fix.build_hook_output(payload)
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def test_should_append_the_question_only_when_the_flow_is_on(
|
|
203
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
204
|
+
) -> None:
|
|
205
|
+
payload = _ledger_payload(tmp_path)
|
|
206
|
+
assert auto_mode_denial_quick_fix.hook_output_with_ledger(
|
|
207
|
+
payload
|
|
208
|
+
) == auto_mode_denial_quick_fix.build_hook_output(payload)
|
|
209
|
+
_flow_on(tmp_path, monkeypatch)
|
|
210
|
+
context_text = auto_mode_denial_quick_fix.hook_output_with_ledger(payload)[
|
|
211
|
+
"hookSpecificOutput"
|
|
212
|
+
]["additionalContext"]
|
|
213
|
+
assert context_text.startswith("=== AUTO MODE DENIAL QUICK FIX ===")
|
|
214
|
+
assert "```powershell" in context_text
|
|
215
|
+
assert "=== AUTO MODE QUESTION ===" in context_text
|
|
216
|
+
assert "Auto mode refused 1 Bash step under the Git Destructive rule." in context_text
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
UNEXPLAINED_DANGER_REASON = (
|
|
220
|
+
"The server-side auto mode classifier judged this action dangerous (it gave no explanation)"
|
|
221
|
+
)
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def test_should_parse_an_unexplained_danger_reason_as_the_unexplained_rule() -> None:
|
|
225
|
+
assert auto_mode_denial_quick_fix.parse_denial_reason(UNEXPLAINED_DANGER_REASON) == (
|
|
226
|
+
auto_mode_denial_quick_fix.DenialReason(
|
|
227
|
+
rule_label="Unexplained", has_verdict=True, prose=UNEXPLAINED_DANGER_REASON
|
|
228
|
+
)
|
|
229
|
+
)
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
@pytest.mark.parametrize(
|
|
233
|
+
"no_verdict_reason",
|
|
234
|
+
[
|
|
235
|
+
"The server-side auto mode classifier gave no verdict (timed out)",
|
|
236
|
+
"a hook changed this call's input after auto mode reviewed it",
|
|
237
|
+
],
|
|
238
|
+
)
|
|
239
|
+
def test_should_parse_a_reason_with_no_classifier_verdict_as_no_verdict(
|
|
240
|
+
no_verdict_reason: str,
|
|
241
|
+
) -> None:
|
|
242
|
+
assert auto_mode_denial_quick_fix.parse_denial_reason(no_verdict_reason) == (
|
|
243
|
+
auto_mode_denial_quick_fix.DenialReason(
|
|
244
|
+
rule_label=None, has_verdict=False, prose=no_verdict_reason
|
|
245
|
+
)
|
|
246
|
+
)
|
|
247
|
+
context_text = auto_mode_denial_quick_fix.build_hook_output(
|
|
248
|
+
{"tool_name": "Bash", "tool_input": {"command": "ls"}, "reason": no_verdict_reason}
|
|
249
|
+
)["hookSpecificOutput"]["additionalContext"]
|
|
250
|
+
assert "```powershell" not in context_text
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
def test_should_record_an_unexplained_refusal_and_ask_its_question(
|
|
254
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, scratch_config_directory: Path
|
|
255
|
+
) -> None:
|
|
256
|
+
_flow_on(tmp_path, monkeypatch)
|
|
257
|
+
context_text = auto_mode_denial_quick_fix.hook_output_with_ledger(
|
|
258
|
+
_ledger_payload(tmp_path, reason=UNEXPLAINED_DANGER_REASON)
|
|
259
|
+
)["hookSpecificOutput"]["additionalContext"]
|
|
260
|
+
assert (
|
|
261
|
+
"Auto mode refused 1 Bash step under the Unexplained rule. "
|
|
262
|
+
"Should auto mode have refused it?"
|
|
263
|
+
) in context_text
|
|
264
|
+
assert "Unexplained exception: Bash calls like `git push -f origin claude/x`" in context_text
|
|
265
|
+
assert "Find the matching rule with: claude auto-mode defaults" in context_text
|
|
266
|
+
assert "--label" not in context_text
|
|
267
|
+
assert "Ask this question in place of the two fixes above" in context_text
|
|
268
|
+
ledger_directory = scratch_config_directory / "state" / "auto-mode-refusals" / "session-1"
|
|
269
|
+
all_entry_files = [
|
|
270
|
+
each_path
|
|
271
|
+
for each_path in ledger_directory.glob("*.json")
|
|
272
|
+
if not each_path.name.startswith("question-")
|
|
273
|
+
]
|
|
274
|
+
assert len(all_entry_files) == 1
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
def test_should_parse_a_missing_reason_as_no_verdict() -> None:
|
|
278
|
+
assert auto_mode_denial_quick_fix.parse_denial_reason("") == (
|
|
279
|
+
auto_mode_denial_quick_fix.DenialReason(rule_label=None, has_verdict=False, prose="")
|
|
280
|
+
)
|
|
39
281
|
|
|
40
282
|
|
|
41
283
|
def test_should_summarize_a_bash_call_by_its_command() -> None:
|
|
@@ -72,8 +314,7 @@ def test_should_propose_rule_entry_and_block_for_a_classifier_denial() -> None:
|
|
|
72
314
|
|
|
73
315
|
|
|
74
316
|
def test_should_omit_the_block_when_no_verdict_exists() -> None:
|
|
75
|
-
payload = _denial_payload(
|
|
76
|
-
del payload["classifier_verdict"]
|
|
317
|
+
payload = _denial_payload(reason="classifier unavailable")
|
|
77
318
|
context_text = auto_mode_denial_quick_fix.build_hook_output(payload)["hookSpecificOutput"][
|
|
78
319
|
"additionalContext"
|
|
79
320
|
]
|
|
@@ -100,6 +341,117 @@ def test_should_stay_quiet_on_malformed_input(
|
|
|
100
341
|
assert capsys.readouterr().out == ""
|
|
101
342
|
|
|
102
343
|
|
|
344
|
+
@pytest.mark.parametrize(
|
|
345
|
+
("reason", "expected_rule_label"),
|
|
346
|
+
[
|
|
347
|
+
("[Don't Touch] edits a guarded file", "Dont Touch"),
|
|
348
|
+
(
|
|
349
|
+
"[Label'; Remove-Item -Recurse C:\\ #] wipes the drive",
|
|
350
|
+
"Label Remove-Item -Recurse C",
|
|
351
|
+
),
|
|
352
|
+
("[Line One\nLine Two] splits the command", "Line One Line Two"),
|
|
353
|
+
("[\x1b[31mRed `$(whoami)` \"q\"] colours the terminal", "31mRed whoami q"),
|
|
354
|
+
("Data\nExfiltration, because it posts the file.", "Data Exfiltration"),
|
|
355
|
+
],
|
|
356
|
+
)
|
|
357
|
+
def test_should_keep_only_safe_characters_in_a_parsed_label(
|
|
358
|
+
reason: str, expected_rule_label: str
|
|
359
|
+
) -> None:
|
|
360
|
+
assert auto_mode_denial_quick_fix.parse_denial_reason(reason).rule_label == expected_rule_label
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
def test_should_parse_a_label_with_no_safe_characters_as_prose() -> None:
|
|
364
|
+
assert auto_mode_denial_quick_fix.parse_denial_reason("[';#] force push") == (
|
|
365
|
+
auto_mode_denial_quick_fix.DenialReason(
|
|
366
|
+
rule_label=None, has_verdict=True, prose="[';#] force push"
|
|
367
|
+
)
|
|
368
|
+
)
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
def test_should_double_every_quote_in_the_rule_wording_command() -> None:
|
|
372
|
+
assert auto_mode_denial_quick_fix.rule_wording_command_for("Don't \u2019Touch") == (
|
|
373
|
+
"claude auto-mode defaults --label 'Don''t \u2019\u2019Touch'"
|
|
374
|
+
)
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
def test_should_draft_the_rule_wording_command_without_a_label_for_prose() -> None:
|
|
378
|
+
assert auto_mode_denial_quick_fix.rule_wording_command_for(None) == (
|
|
379
|
+
"claude auto-mode defaults"
|
|
380
|
+
)
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
def test_should_pass_a_hostile_label_to_claude_as_one_argument(tmp_path: Path) -> None:
|
|
384
|
+
context_text = auto_mode_denial_quick_fix.build_hook_output(
|
|
385
|
+
_denial_payload(reason="[Label'; Remove-Item -Recurse C:\\ #\n$(whoami)] wipes the drive")
|
|
386
|
+
)["hookSpecificOutput"]["additionalContext"]
|
|
387
|
+
drafted_line = context_text.splitlines()[-1].removeprefix("Read the rule wording with: ")
|
|
388
|
+
script_path = tmp_path / "drafted_line.ps1"
|
|
389
|
+
script_path.write_text(
|
|
390
|
+
"function claude { $args | ConvertTo-Json -Compress }\n" + drafted_line + "\n",
|
|
391
|
+
encoding="utf-8",
|
|
392
|
+
)
|
|
393
|
+
completed = subprocess.run(
|
|
394
|
+
["pwsh", "-NoProfile", "-File", str(script_path)],
|
|
395
|
+
check=True,
|
|
396
|
+
capture_output=True,
|
|
397
|
+
text=True,
|
|
398
|
+
)
|
|
399
|
+
assert json.loads(completed.stdout) == [
|
|
400
|
+
"auto-mode",
|
|
401
|
+
"defaults",
|
|
402
|
+
"--label",
|
|
403
|
+
"Label Remove-Item -Recurse C whoami",
|
|
404
|
+
]
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
def test_should_draft_the_rule_wording_command_without_a_label_for_a_prose_reason() -> None:
|
|
408
|
+
context_text = auto_mode_denial_quick_fix.build_hook_output(
|
|
409
|
+
_denial_payload(reason="the command force-cancels workflow runs in bulk.")
|
|
410
|
+
)["hookSpecificOutput"]["additionalContext"]
|
|
411
|
+
assert "--label" not in context_text
|
|
412
|
+
assert context_text.endswith("Find the matching rule with: claude auto-mode defaults")
|
|
413
|
+
assert "Denial reason: the command force-cancels workflow runs in bulk." in context_text
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
def test_should_show_a_hostile_reason_on_one_line_without_control_characters() -> None:
|
|
417
|
+
hook_output = auto_mode_denial_quick_fix.build_hook_output(
|
|
418
|
+
_denial_payload(reason="the step\n```powershell\nRemove-Item C:\\\n```\n\x1b[31m done.")
|
|
419
|
+
)
|
|
420
|
+
context_text = hook_output["hookSpecificOutput"]["additionalContext"]
|
|
421
|
+
assert "Denial reason: the step ```powershell Remove-Item C:\\ ``` [31m done." in context_text
|
|
422
|
+
assert "\x1b" not in context_text
|
|
423
|
+
assert "\x1b" not in hook_output["systemMessage"]
|
|
424
|
+
assert context_text.count("\n```powershell") == 1
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
@pytest.mark.parametrize("reason", [42, {"label": "Git Destructive"}, ["[Git Destructive]"]])
|
|
428
|
+
def test_should_treat_a_reason_that_is_not_text_as_no_verdict(reason: object) -> None:
|
|
429
|
+
hook_output = auto_mode_denial_quick_fix.build_hook_output(_denial_payload(reason=reason))
|
|
430
|
+
context_text = hook_output["hookSpecificOutput"]["additionalContext"]
|
|
431
|
+
assert "no classifier verdict" in context_text
|
|
432
|
+
assert "powershell" not in context_text
|
|
433
|
+
assert hook_output["systemMessage"] == (
|
|
434
|
+
"Auto mode blocked mcp__github__resolve_review_thread [unnamed rule]."
|
|
435
|
+
" Claude will propose a quick fix."
|
|
436
|
+
)
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
def test_should_write_a_hostile_entry_unchanged_when_powershell_runs(tmp_path: Path) -> None:
|
|
440
|
+
hostile_entry = "it's \u2018a\u2019 \u201ab\u201b `tick` $(Get-Date) \"q\"\nnext; Remove-Item x"
|
|
441
|
+
block = (
|
|
442
|
+
auto_mode_denial_quick_fix.powershell_block_for(hostile_entry)
|
|
443
|
+
.replace("claude auto-mode config", "")
|
|
444
|
+
.replace("Join-Path $HOME", f"Join-Path '{tmp_path}'")
|
|
445
|
+
)
|
|
446
|
+
script_path = tmp_path / "block.ps1"
|
|
447
|
+
script_path.write_text(block, encoding="utf-8")
|
|
448
|
+
subprocess.run(["pwsh", "-NoProfile", "-File", str(script_path)], check=True)
|
|
449
|
+
written_settings = json.loads(
|
|
450
|
+
(tmp_path / ".claude" / "settings.json").read_text(encoding="utf-8")
|
|
451
|
+
)
|
|
452
|
+
assert written_settings["autoMode"]["allow"] == ["$defaults", hostile_entry]
|
|
453
|
+
|
|
454
|
+
|
|
103
455
|
def test_should_write_the_entry_with_defaults_when_powershell_runs(tmp_path: Path) -> None:
|
|
104
456
|
block = (
|
|
105
457
|
auto_mode_denial_quick_fix.powershell_block_for("it's allowed")
|
|
@@ -111,3 +463,175 @@ def test_should_write_the_entry_with_defaults_when_powershell_runs(tmp_path: Pat
|
|
|
111
463
|
(tmp_path / ".claude" / "settings.json").read_text(encoding="utf-8")
|
|
112
464
|
)
|
|
113
465
|
assert written_settings["autoMode"]["allow"] == ["$defaults", "it's allowed"]
|
|
466
|
+
|
|
467
|
+
|
|
468
|
+
def _allowed_ledger_entry(scratch_config_directory: Path) -> Path:
|
|
469
|
+
ledger_directory = scratch_config_directory / "state" / "auto-mode-refusals" / "session-1"
|
|
470
|
+
ledger_directory.mkdir(parents=True, exist_ok=True)
|
|
471
|
+
allowed_input = {"command": "git push -f origin claude/allowed"}
|
|
472
|
+
(ledger_directory / "allowed.json").write_text(
|
|
473
|
+
json.dumps(
|
|
474
|
+
{
|
|
475
|
+
"id": "allowed",
|
|
476
|
+
"tool_name": "Bash",
|
|
477
|
+
"tool_input": allowed_input,
|
|
478
|
+
"rule_label": "Git Destructive",
|
|
479
|
+
"action_summary": allowed_input["command"],
|
|
480
|
+
"sequence": 1,
|
|
481
|
+
"answer": "allowed",
|
|
482
|
+
}
|
|
483
|
+
),
|
|
484
|
+
encoding="utf-8",
|
|
485
|
+
)
|
|
486
|
+
return ledger_directory
|
|
487
|
+
|
|
488
|
+
|
|
489
|
+
def _entry_file_count(ledger_directory: Path) -> int:
|
|
490
|
+
return sum(
|
|
491
|
+
1
|
|
492
|
+
for each_path in ledger_directory.glob("*.json")
|
|
493
|
+
if not each_path.name.startswith("question-")
|
|
494
|
+
)
|
|
495
|
+
|
|
496
|
+
|
|
497
|
+
@pytest.mark.parametrize(
|
|
498
|
+
("file_name", "file_fields"),
|
|
499
|
+
[
|
|
500
|
+
("no-id.json", {"answer": None, "tool_name": "Bash", "rule_label": "Git Destructive"}),
|
|
501
|
+
(
|
|
502
|
+
"bad-sequence.json",
|
|
503
|
+
{
|
|
504
|
+
"id": "bad-sequence",
|
|
505
|
+
"tool_name": "Bash",
|
|
506
|
+
"rule_label": "Git Destructive",
|
|
507
|
+
"sequence": "first",
|
|
508
|
+
"answer": None,
|
|
509
|
+
},
|
|
510
|
+
),
|
|
511
|
+
(
|
|
512
|
+
"question-bad-offset.json",
|
|
513
|
+
{
|
|
514
|
+
"record": "question",
|
|
515
|
+
"question": "Q?",
|
|
516
|
+
"entry_ids": ["x"],
|
|
517
|
+
"transcript_offset": "late",
|
|
518
|
+
},
|
|
519
|
+
),
|
|
520
|
+
],
|
|
521
|
+
)
|
|
522
|
+
def test_should_skip_a_ledger_file_that_does_not_parse_and_still_ask(
|
|
523
|
+
tmp_path: Path,
|
|
524
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
525
|
+
capsys: pytest.CaptureFixture[str],
|
|
526
|
+
scratch_config_directory: Path,
|
|
527
|
+
file_name: str,
|
|
528
|
+
file_fields: dict[str, object],
|
|
529
|
+
) -> None:
|
|
530
|
+
_flow_on(tmp_path, monkeypatch)
|
|
531
|
+
ledger_directory = scratch_config_directory / "state" / "auto-mode-refusals" / "session-1"
|
|
532
|
+
ledger_directory.mkdir(parents=True)
|
|
533
|
+
(ledger_directory / file_name).write_text(json.dumps(file_fields), encoding="utf-8")
|
|
534
|
+
payload = _ledger_payload(tmp_path)
|
|
535
|
+
context_text = _hook_output_from_stdin(payload, monkeypatch, capsys)["hookSpecificOutput"][
|
|
536
|
+
"additionalContext"
|
|
537
|
+
]
|
|
538
|
+
today_context = auto_mode_denial_quick_fix.build_hook_output(payload)["hookSpecificOutput"][
|
|
539
|
+
"additionalContext"
|
|
540
|
+
]
|
|
541
|
+
assert context_text.startswith(today_context)
|
|
542
|
+
assert "Auto mode refused 1 Bash step under the Git Destructive rule." in context_text
|
|
543
|
+
|
|
544
|
+
|
|
545
|
+
def test_should_fall_back_to_todays_output_on_any_ledger_error(
|
|
546
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
|
547
|
+
) -> None:
|
|
548
|
+
_flow_on(tmp_path, monkeypatch)
|
|
549
|
+
|
|
550
|
+
def raise_lookup_error(*_: object) -> None:
|
|
551
|
+
raise LookupError("ledger broke")
|
|
552
|
+
|
|
553
|
+
monkeypatch.setattr(
|
|
554
|
+
auto_mode_denial_quick_fix.auto_mode_refusal_ledger,
|
|
555
|
+
"apply_transcript_answers",
|
|
556
|
+
raise_lookup_error,
|
|
557
|
+
)
|
|
558
|
+
payload = _ledger_payload(tmp_path)
|
|
559
|
+
assert _hook_output_from_stdin(
|
|
560
|
+
payload, monkeypatch, capsys
|
|
561
|
+
) == auto_mode_denial_quick_fix.build_hook_output(payload)
|
|
562
|
+
|
|
563
|
+
|
|
564
|
+
def test_should_fall_back_to_todays_output_for_a_playbook_at_the_filesystem_root(
|
|
565
|
+
tmp_path: Path,
|
|
566
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
567
|
+
capsys: pytest.CaptureFixture[str],
|
|
568
|
+
scratch_config_directory: Path,
|
|
569
|
+
) -> None:
|
|
570
|
+
ledger_directory = _allowed_ledger_entry(scratch_config_directory)
|
|
571
|
+
monkeypatch.setattr(
|
|
572
|
+
auto_mode_denial_quick_fix.auto_mode_refusal_ledger,
|
|
573
|
+
"playbook_path_from_environment",
|
|
574
|
+
lambda: Path("/auto-mode-rule.md"),
|
|
575
|
+
)
|
|
576
|
+
payload = _ledger_payload(tmp_path)
|
|
577
|
+
assert _hook_output_from_stdin(
|
|
578
|
+
payload, monkeypatch, capsys
|
|
579
|
+
) == auto_mode_denial_quick_fix.build_hook_output(payload)
|
|
580
|
+
assert _entry_file_count(ledger_directory) == 1
|
|
581
|
+
|
|
582
|
+
|
|
583
|
+
def _rule_worktree(tmp_path: Path, branch_name: str) -> Path:
|
|
584
|
+
repository_git_directory = tmp_path / "agent-channels" / ".git"
|
|
585
|
+
worktree_git_directory = repository_git_directory / "worktrees" / "agent-channels-rule"
|
|
586
|
+
worktree_git_directory.mkdir(parents=True)
|
|
587
|
+
(worktree_git_directory / "HEAD").write_text(
|
|
588
|
+
f"ref: refs/heads/{branch_name}\n", encoding="utf-8"
|
|
589
|
+
)
|
|
590
|
+
(worktree_git_directory / "commondir").write_text("../..\n", encoding="utf-8")
|
|
591
|
+
worktree_directory = tmp_path / "scratch" / "agent-channels-rule"
|
|
592
|
+
worktree_directory.mkdir(parents=True)
|
|
593
|
+
(worktree_directory / ".git").write_text(
|
|
594
|
+
f"gitdir: {worktree_git_directory}\n", encoding="utf-8"
|
|
595
|
+
)
|
|
596
|
+
(worktree_git_directory / "gitdir").write_text(
|
|
597
|
+
f"{worktree_directory / '.git'}\n", encoding="utf-8"
|
|
598
|
+
)
|
|
599
|
+
return worktree_directory
|
|
600
|
+
|
|
601
|
+
|
|
602
|
+
@pytest.mark.parametrize(
|
|
603
|
+
("branch_name", "command", "is_entry_expected"),
|
|
604
|
+
[
|
|
605
|
+
(
|
|
606
|
+
"claude/auto-mode-rule-own-push",
|
|
607
|
+
'git commit -m "Add the Own push auto mode rule"',
|
|
608
|
+
False,
|
|
609
|
+
),
|
|
610
|
+
(
|
|
611
|
+
"claude/auto-mode-rule-own-push",
|
|
612
|
+
"git push -u origin claude/auto-mode-rule-own-push",
|
|
613
|
+
False,
|
|
614
|
+
),
|
|
615
|
+
("claude/project-thread-rule", 'git commit -m "Add a rule"', True),
|
|
616
|
+
("claude/auto-mode-rule-own-push", "git push -u origin claude/project-thread-rule", True),
|
|
617
|
+
],
|
|
618
|
+
)
|
|
619
|
+
def test_should_skip_only_a_rule_branch_commit_or_push_in_a_rule_worktree(
|
|
620
|
+
tmp_path: Path,
|
|
621
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
622
|
+
scratch_config_directory: Path,
|
|
623
|
+
branch_name: str,
|
|
624
|
+
command: str,
|
|
625
|
+
is_entry_expected: bool,
|
|
626
|
+
) -> None:
|
|
627
|
+
_flow_on(tmp_path, monkeypatch)
|
|
628
|
+
ledger_directory = _allowed_ledger_entry(scratch_config_directory)
|
|
629
|
+
worktree_directory = _rule_worktree(tmp_path, branch_name)
|
|
630
|
+
payload = _ledger_payload(
|
|
631
|
+
tmp_path,
|
|
632
|
+
tool_input={"command": command},
|
|
633
|
+
reason="[Destructive or Unwanted Actions] writes a branch",
|
|
634
|
+
cwd=str(worktree_directory),
|
|
635
|
+
)
|
|
636
|
+
auto_mode_denial_quick_fix.hook_output_with_ledger(payload)
|
|
637
|
+
assert _entry_file_count(ledger_directory) == (2 if is_entry_expected else 1)
|