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
|
@@ -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
|
|
@@ -3,8 +3,9 @@
|
|
|
3
3
|
A Bash tool call starts one interpreter, the dispatcher's own. Each roster
|
|
4
4
|
entry then runs in that process through runpy, so the roster's exact content is
|
|
5
5
|
the chain's cost after that single start. The roster holds the MSYS rewriter,
|
|
6
|
-
the headless Claude broker gate,
|
|
7
|
-
|
|
6
|
+
the headless Claude broker gate, the gh global account gate, the gh API budget
|
|
7
|
+
gate, the cloud GraphQL gate, the Python Windows invocation gate, and the
|
|
8
|
+
transcript root search gate; any other hook added here fails this test.
|
|
8
9
|
"""
|
|
9
10
|
|
|
10
11
|
import sys
|
|
@@ -22,7 +23,7 @@ from hooks_constants.bash_pre_tool_use_dispatcher_constants import (
|
|
|
22
23
|
)
|
|
23
24
|
|
|
24
25
|
|
|
25
|
-
def
|
|
26
|
+
def test_bash_roster_starts_only_the_msys_rewriter_and_the_six_gates() -> None:
|
|
26
27
|
assert ALL_BASH_HOSTED_HOOK_ENTRIES == (
|
|
27
28
|
BashHostedHookEntry(
|
|
28
29
|
script_relative_path="blocking/msys_rev_path_rewriter.py",
|
|
@@ -38,6 +39,10 @@ def test_bash_roster_starts_only_the_msys_rewriter_and_the_five_gates() -> None:
|
|
|
38
39
|
applicable_tool_names=ALL_BASH_AND_POWERSHELL_TOOL_NAMES,
|
|
39
40
|
replaced_by_plugin_name="shell-guards",
|
|
40
41
|
),
|
|
42
|
+
BashHostedHookEntry(
|
|
43
|
+
script_relative_path="blocking/gh_api_budget_gate.py",
|
|
44
|
+
applicable_tool_names=ALL_BASH_AND_POWERSHELL_TOOL_NAMES,
|
|
45
|
+
),
|
|
41
46
|
BashHostedHookEntry(
|
|
42
47
|
script_relative_path="blocking/cloud_graphql_gate.py",
|
|
43
48
|
applicable_tool_names=ALL_BASH_AND_POWERSHELL_TOOL_NAMES,
|
|
@@ -274,6 +274,7 @@ def test_should_drop_the_gates_the_shell_guards_mod_holds_while_it_is_on(
|
|
|
274
274
|
all_script_paths = [each_entry.script_relative_path for each_entry in select_applicable_entries(BASH_TOOL_NAME)]
|
|
275
275
|
assert all_script_paths == [
|
|
276
276
|
"blocking/msys_rev_path_rewriter.py",
|
|
277
|
+
"blocking/gh_api_budget_gate.py",
|
|
277
278
|
"blocking/cloud_graphql_gate.py",
|
|
278
279
|
"blocking/python_windows_invocation_gate.py",
|
|
279
280
|
"blocking/transcript_root_search_gate.py",
|