claude-dev-env 8.120.0 → 8.122.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 +2 -0
- package/docs/rule-guides/question-presentation.md +4 -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/sent_file_question_detector.py +340 -0
- package/hooks/blocking/sent_file_question_gate.py +307 -0
- package/hooks/blocking/sent_file_scan.py +209 -0
- package/hooks/blocking/test_auto_mode_refusal_stop_gate.py +484 -0
- package/hooks/blocking/test_sent_file_question_detector.py +68 -0
- package/hooks/blocking/test_sent_file_question_gate.py +688 -0
- package/hooks/blocking/test_sent_file_scan.py +71 -0
- package/hooks/blocking/test_user_step_wording.py +46 -0
- package/hooks/blocking/user_step_wording.py +113 -15
- package/hooks/features/README.md +1 -1
- package/hooks/features/conduct-gates.md +3 -0
- package/hooks/features/session-context.md +5 -2
- package/hooks/hooks.json +15 -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/sent_file_question_gate_constants.py +261 -0
- package/hooks/hooks_constants/user_step_wording_constants.py +4 -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 +10 -0
- package/scripts/policy_lint/config/constants.py +2 -0
- package/scripts/tests/test_adapter_configuration.py +16 -0
|
@@ -0,0 +1,398 @@
|
|
|
1
|
+
"""Behavior tests for the auto mode refusal ledger."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import subprocess
|
|
7
|
+
import sys
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
import pytest
|
|
11
|
+
|
|
12
|
+
import auto_mode_refusal_ledger as ledger
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@pytest.fixture(autouse=True)
|
|
16
|
+
def scratch_config_directory(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
|
|
17
|
+
config_directory = tmp_path / "config"
|
|
18
|
+
config_directory.mkdir()
|
|
19
|
+
monkeypatch.setenv("CLAUDE_CONFIG_DIR", str(config_directory))
|
|
20
|
+
monkeypatch.setenv("HOME", str(tmp_path))
|
|
21
|
+
monkeypatch.delenv("CLAUDE_AUTO_MODE_RULE_PLAYBOOK", raising=False)
|
|
22
|
+
return config_directory
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def _add(ledger_directory: Path, command: str, rule_label: str = "Git Destructive") -> str:
|
|
26
|
+
tool_input = {"command": command}
|
|
27
|
+
entry_id = ledger.entry_id_for("Bash", tool_input)
|
|
28
|
+
ledger.add_entry(
|
|
29
|
+
ledger_directory,
|
|
30
|
+
{
|
|
31
|
+
"id": entry_id,
|
|
32
|
+
"tool_name": "Bash",
|
|
33
|
+
"tool_input": tool_input,
|
|
34
|
+
"rule_label": rule_label,
|
|
35
|
+
"action_summary": command,
|
|
36
|
+
},
|
|
37
|
+
)
|
|
38
|
+
return entry_id
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _append_ask(transcript_path: Path, question: str, answer: str, tool_use_id: str) -> None:
|
|
42
|
+
call_record = {
|
|
43
|
+
"type": "assistant",
|
|
44
|
+
"message": {
|
|
45
|
+
"content": [
|
|
46
|
+
{
|
|
47
|
+
"type": "tool_use",
|
|
48
|
+
"id": tool_use_id,
|
|
49
|
+
"name": "AskUserQuestion",
|
|
50
|
+
"input": {"questions": [{"question": question}]},
|
|
51
|
+
}
|
|
52
|
+
]
|
|
53
|
+
},
|
|
54
|
+
}
|
|
55
|
+
result_record = {
|
|
56
|
+
"type": "user",
|
|
57
|
+
"toolUseResult": {"questions": [{"question": question}], "answers": {question: answer}},
|
|
58
|
+
"message": {"content": [{"type": "tool_result", "tool_use_id": tool_use_id}]},
|
|
59
|
+
}
|
|
60
|
+
with transcript_path.open("a", encoding="utf-8") as transcript_file:
|
|
61
|
+
transcript_file.write(json.dumps(call_record) + "\n" + json.dumps(result_record) + "\n")
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _answers(ledger_directory: Path) -> list[object]:
|
|
65
|
+
return [each_entry.answer for each_entry in ledger.load_entries(ledger_directory)]
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def test_should_turn_the_flow_on_only_for_an_existing_playbook(
|
|
69
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
70
|
+
) -> None:
|
|
71
|
+
assert ledger.playbook_path_from_environment() is None
|
|
72
|
+
monkeypatch.setenv("CLAUDE_AUTO_MODE_RULE_PLAYBOOK", str(tmp_path / "missing.md"))
|
|
73
|
+
assert ledger.playbook_path_from_environment() is None
|
|
74
|
+
playbook_path = tmp_path / "auto-mode-rule.md"
|
|
75
|
+
playbook_path.write_text("# Playbook\n", encoding="utf-8")
|
|
76
|
+
monkeypatch.setenv("CLAUDE_AUTO_MODE_RULE_PLAYBOOK", str(playbook_path))
|
|
77
|
+
assert ledger.playbook_path_from_environment() == playbook_path
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def _rule_repository(tmp_path: Path) -> Path:
|
|
81
|
+
repository_root = tmp_path / "agent-channels"
|
|
82
|
+
(repository_root / ".git").mkdir(parents=True)
|
|
83
|
+
(repository_root / "setup").mkdir()
|
|
84
|
+
(repository_root / "setup" / "add_auto_mode_rule.py").write_text("", encoding="utf-8")
|
|
85
|
+
return repository_root
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def test_should_find_the_rule_repository_two_folders_above_the_playbook(tmp_path: Path) -> None:
|
|
89
|
+
_rule_repository(tmp_path)
|
|
90
|
+
playbook_path = tmp_path / "agent-channels" / "docs" / "auto-mode-rule.md"
|
|
91
|
+
assert ledger.rule_repository_root(playbook_path) == tmp_path.resolve() / "agent-channels"
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
@pytest.mark.parametrize("missing_marker", [".git", "setup/add_auto_mode_rule.py"])
|
|
95
|
+
def test_should_find_no_rule_repository_without_git_and_the_rule_tool(
|
|
96
|
+
tmp_path: Path, missing_marker: str
|
|
97
|
+
) -> None:
|
|
98
|
+
repository_root = _rule_repository(tmp_path)
|
|
99
|
+
marker_path = repository_root / missing_marker
|
|
100
|
+
if marker_path.is_dir():
|
|
101
|
+
marker_path.rmdir()
|
|
102
|
+
else:
|
|
103
|
+
marker_path.unlink()
|
|
104
|
+
playbook_path = repository_root / "docs" / "auto-mode-rule.md"
|
|
105
|
+
assert ledger.rule_repository_root(playbook_path) is None
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
@pytest.mark.parametrize("playbook_text", ["/auto-mode-rule.md", "/docs/auto-mode-rule.md"])
|
|
109
|
+
def test_should_find_no_rule_repository_for_a_playbook_at_or_one_below_the_filesystem_root(
|
|
110
|
+
playbook_text: str,
|
|
111
|
+
) -> None:
|
|
112
|
+
assert ledger.rule_repository_root(Path(playbook_text)) is None
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def _refused_step(command: str, rule_label: str | None, **overrides: object) -> ledger.RefusedStep:
|
|
116
|
+
all_fields: dict[str, object] = {
|
|
117
|
+
"session_id": "s1",
|
|
118
|
+
"transcript_path": None,
|
|
119
|
+
"tool_name": "Bash",
|
|
120
|
+
"tool_input": {"command": command},
|
|
121
|
+
"working_folder": None,
|
|
122
|
+
"rule_label": rule_label,
|
|
123
|
+
"reason": f"[{rule_label}] refused",
|
|
124
|
+
"action_summary": command,
|
|
125
|
+
}
|
|
126
|
+
all_fields.update(overrides)
|
|
127
|
+
return ledger.RefusedStep(**all_fields)
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def test_should_record_a_labelled_refusal_once_and_return_its_question(tmp_path: Path) -> None:
|
|
131
|
+
repository_root = tmp_path / "agent-channels"
|
|
132
|
+
expected_question = ledger.question_text_for("Bash", "Git Destructive", 1)
|
|
133
|
+
for _ in range(2):
|
|
134
|
+
question = ledger.record_refusal(
|
|
135
|
+
_refused_step("git push -f", "Git Destructive"), repository_root
|
|
136
|
+
)
|
|
137
|
+
assert question == expected_question
|
|
138
|
+
assert _answers(ledger.ledger_directory_for("s1")) == [None]
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
@pytest.mark.parametrize(
|
|
142
|
+
("command", "rule_label"),
|
|
143
|
+
[
|
|
144
|
+
("git push -f", None),
|
|
145
|
+
("python3 setup/add_auto_mode_rule.py add --rule-file r.txt", "Destructive"),
|
|
146
|
+
],
|
|
147
|
+
)
|
|
148
|
+
def test_should_record_nothing_for_prose_or_a_rule_flow_step(
|
|
149
|
+
tmp_path: Path, command: str, rule_label: str | None
|
|
150
|
+
) -> None:
|
|
151
|
+
repository_root = _rule_repository(tmp_path)
|
|
152
|
+
refused_step = _refused_step(command, rule_label, working_folder=str(repository_root))
|
|
153
|
+
assert ledger.record_refusal(refused_step, repository_root) is None
|
|
154
|
+
assert ledger.load_entries(ledger.ledger_directory_for("s1")) == []
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def test_should_record_the_rule_tool_run_from_an_unknown_folder(tmp_path: Path) -> None:
|
|
158
|
+
repository_root = _rule_repository(tmp_path)
|
|
159
|
+
refused_step = _refused_step(
|
|
160
|
+
"python3 setup/add_auto_mode_rule.py add --rule-file r.txt", "Destructive"
|
|
161
|
+
)
|
|
162
|
+
assert ledger.record_refusal(refused_step, repository_root) is not None
|
|
163
|
+
assert len(ledger.load_entries(ledger.ledger_directory_for("s1"))) == 1
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
def test_should_word_the_question_for_one_step_and_for_several() -> None:
|
|
167
|
+
assert ledger.question_text_for("Bash", "Git Destructive", 1) == (
|
|
168
|
+
"Auto mode refused 1 Bash step under the Git Destructive rule. "
|
|
169
|
+
"Should auto mode have refused it?"
|
|
170
|
+
)
|
|
171
|
+
assert ledger.question_text_for("Bash", "Git Destructive", 3) == (
|
|
172
|
+
"Auto mode refused 3 Bash steps under the Git Destructive rule. "
|
|
173
|
+
"Should auto mode have refused them?"
|
|
174
|
+
)
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def test_should_add_one_entry_per_id_in_refusal_order(tmp_path: Path) -> None:
|
|
178
|
+
ledger_directory = tmp_path / "ledger"
|
|
179
|
+
first_id = _add(ledger_directory, "git push -f")
|
|
180
|
+
assert _add(ledger_directory, "git push -f") == first_id
|
|
181
|
+
second_id = _add(ledger_directory, "git reset --hard")
|
|
182
|
+
all_entries = ledger.load_entries(ledger_directory)
|
|
183
|
+
assert [each.entry_id for each in all_entries] == [first_id, second_id]
|
|
184
|
+
assert [each.sequence for each in all_entries] == [1, 2]
|
|
185
|
+
assert _answers(ledger_directory) == [None, None]
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def test_should_report_whether_add_entry_wrote_a_file(tmp_path: Path) -> None:
|
|
189
|
+
fields = {"id": "abc", "tool_name": "Bash", "rule_label": "Git"}
|
|
190
|
+
assert ledger.add_entry(tmp_path, fields) is True
|
|
191
|
+
assert ledger.add_entry(tmp_path, fields) is False
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def test_should_group_by_tool_and_exact_label_and_cap_at_five(tmp_path: Path) -> None:
|
|
195
|
+
for each_index in range(7):
|
|
196
|
+
_add(tmp_path, f"git push -f origin b{each_index}")
|
|
197
|
+
_add(tmp_path, "git commit --amend", rule_label="Git")
|
|
198
|
+
all_groups = ledger.question_groups(ledger.load_entries(tmp_path))
|
|
199
|
+
assert [each.question for each in all_groups] == [
|
|
200
|
+
ledger.question_text_for("Bash", "Git Destructive", 5),
|
|
201
|
+
ledger.question_text_for("Bash", "Git", 1),
|
|
202
|
+
]
|
|
203
|
+
assert all_groups[0].all_action_summaries[0] == "git push -f origin b0"
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def test_should_keep_a_record_offset_until_its_ids_change(tmp_path: Path) -> None:
|
|
207
|
+
ledger_directory = tmp_path / "ledger"
|
|
208
|
+
transcript_path = tmp_path / "transcript.jsonl"
|
|
209
|
+
transcript_path.write_text("{}\n", encoding="utf-8")
|
|
210
|
+
_add(ledger_directory, "git push -f")
|
|
211
|
+
ledger.write_question_records(
|
|
212
|
+
ledger_directory,
|
|
213
|
+
ledger.question_groups(ledger.load_entries(ledger_directory)),
|
|
214
|
+
transcript_path,
|
|
215
|
+
)
|
|
216
|
+
transcript_path.write_text("{}\n{}\n{}\n", encoding="utf-8")
|
|
217
|
+
ledger.write_question_records(
|
|
218
|
+
ledger_directory,
|
|
219
|
+
ledger.question_groups(ledger.load_entries(ledger_directory)),
|
|
220
|
+
transcript_path,
|
|
221
|
+
)
|
|
222
|
+
all_records = [
|
|
223
|
+
json.loads(each_path.read_text(encoding="utf-8"))
|
|
224
|
+
for each_path in ledger_directory.glob("question-*.json")
|
|
225
|
+
]
|
|
226
|
+
assert len(all_records) == 1
|
|
227
|
+
assert "answer" not in all_records[0]
|
|
228
|
+
assert all_records[0]["transcript_offset"] == 3
|
|
229
|
+
assert all_records[0]["question"] == ledger.question_text_for("Bash", "Git Destructive", 1)
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
def test_should_keep_an_old_answer_off_a_step_that_reuses_its_question(tmp_path: Path) -> None:
|
|
233
|
+
ledger_directory = tmp_path / "ledger"
|
|
234
|
+
transcript_path = tmp_path / "transcript.jsonl"
|
|
235
|
+
question = ledger.question_text_for("Bash", "Git Destructive", 1)
|
|
236
|
+
|
|
237
|
+
def refuse_and_record(command: str) -> None:
|
|
238
|
+
_add(ledger_directory, command)
|
|
239
|
+
ledger.apply_transcript_answers(ledger_directory, transcript_path)
|
|
240
|
+
ledger.write_question_records(
|
|
241
|
+
ledger_directory,
|
|
242
|
+
ledger.question_groups(ledger.load_entries(ledger_directory)),
|
|
243
|
+
transcript_path,
|
|
244
|
+
)
|
|
245
|
+
|
|
246
|
+
refuse_and_record("git push -f")
|
|
247
|
+
_append_ask(transcript_path, question, "Keep refusing", "toolu_first")
|
|
248
|
+
ledger.apply_transcript_answers(ledger_directory, transcript_path)
|
|
249
|
+
refuse_and_record("git reset --hard")
|
|
250
|
+
ledger.apply_transcript_answers(ledger_directory, transcript_path)
|
|
251
|
+
assert _answers(ledger_directory) == ["kept", None]
|
|
252
|
+
_append_ask(transcript_path, question, "Allow it", "toolu_second")
|
|
253
|
+
ledger.apply_transcript_answers(ledger_directory, transcript_path)
|
|
254
|
+
assert _answers(ledger_directory) == ["kept", "allowed"]
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
def test_should_set_answers_only_from_asks_after_the_record(tmp_path: Path) -> None:
|
|
258
|
+
ledger_directory = tmp_path / "ledger"
|
|
259
|
+
transcript_path = tmp_path / "transcript.jsonl"
|
|
260
|
+
question = ledger.question_text_for("Bash", "Git Destructive", 1)
|
|
261
|
+
_append_ask(transcript_path, question, "Keep refusing", "toolu_early")
|
|
262
|
+
_add(ledger_directory, "git push -f")
|
|
263
|
+
ledger.write_question_records(
|
|
264
|
+
ledger_directory,
|
|
265
|
+
ledger.question_groups(ledger.load_entries(ledger_directory)),
|
|
266
|
+
transcript_path,
|
|
267
|
+
)
|
|
268
|
+
ledger.apply_transcript_answers(ledger_directory, transcript_path)
|
|
269
|
+
assert _answers(ledger_directory) == [None]
|
|
270
|
+
_append_ask(transcript_path, question.lower(), "Allow it", "toolu_other_text")
|
|
271
|
+
_append_ask(transcript_path, question, "allow it", "toolu_other_answer")
|
|
272
|
+
ledger.apply_transcript_answers(ledger_directory, transcript_path)
|
|
273
|
+
assert _answers(ledger_directory) == [None]
|
|
274
|
+
_append_ask(transcript_path, question, "Allow it (Recommended)", "toolu_late")
|
|
275
|
+
ledger.apply_transcript_answers(ledger_directory, transcript_path)
|
|
276
|
+
assert _answers(ledger_directory) == ["allowed"]
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
def _write_records(ledger_directory: Path, transcript_path: Path) -> None:
|
|
280
|
+
ledger.write_question_records(
|
|
281
|
+
ledger_directory,
|
|
282
|
+
ledger.question_groups(ledger.load_entries(ledger_directory)),
|
|
283
|
+
transcript_path,
|
|
284
|
+
)
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
def test_should_take_a_card_text_answer_only_when_one_record_of_the_tool_could_own_it(
|
|
288
|
+
tmp_path: Path,
|
|
289
|
+
) -> None:
|
|
290
|
+
ledger_directory = tmp_path / "ledger"
|
|
291
|
+
transcript_path = tmp_path / "transcript.jsonl"
|
|
292
|
+
card_question = "Should auto mode have refused this Bash step?"
|
|
293
|
+
_add(ledger_directory, "git push -f")
|
|
294
|
+
_write_records(ledger_directory, transcript_path)
|
|
295
|
+
_append_ask(transcript_path, card_question, "Allow it", "toolu_card_one")
|
|
296
|
+
ledger.apply_transcript_answers(ledger_directory, transcript_path)
|
|
297
|
+
assert _answers(ledger_directory) == ["allowed"]
|
|
298
|
+
other_directory = tmp_path / "other-ledger"
|
|
299
|
+
other_transcript_path = tmp_path / "other-transcript.jsonl"
|
|
300
|
+
_add(other_directory, "git push -f")
|
|
301
|
+
_add(other_directory, "rm -rf build", rule_label="Irreversible Local Destruction")
|
|
302
|
+
_write_records(other_directory, other_transcript_path)
|
|
303
|
+
_append_ask(other_transcript_path, card_question, "Allow it", "toolu_card_two")
|
|
304
|
+
ledger.apply_transcript_answers(other_directory, other_transcript_path)
|
|
305
|
+
assert _answers(other_directory) == [None, None]
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
def test_should_apply_answers_from_asks_already_read(tmp_path: Path) -> None:
|
|
309
|
+
ledger_directory = tmp_path / "ledger"
|
|
310
|
+
transcript_path = tmp_path / "transcript.jsonl"
|
|
311
|
+
question = ledger.question_text_for("Bash", "Git Destructive", 1)
|
|
312
|
+
_add(ledger_directory, "git push -f")
|
|
313
|
+
_write_records(ledger_directory, transcript_path)
|
|
314
|
+
_append_ask(transcript_path, question, "Keep refusing", "toolu_kept")
|
|
315
|
+
ledger.apply_asked_answers(ledger_directory, ledger.read_asked_questions(transcript_path))
|
|
316
|
+
assert _answers(ledger_directory) == ["kept"]
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
def test_should_merge_fields_into_entries_and_keep_their_other_fields(tmp_path: Path) -> None:
|
|
320
|
+
first_id = _add(tmp_path, "git push -f")
|
|
321
|
+
second_id = _add(tmp_path, "git reset --hard")
|
|
322
|
+
ledger.merge_entry_fields(tmp_path, {first_id: {"question_holds": 1}, "missing": {"x": 1}})
|
|
323
|
+
first_entry, second_entry = ledger.load_entries(tmp_path)
|
|
324
|
+
assert (first_entry.question_holds, first_entry.action_summary) == (1, "git push -f")
|
|
325
|
+
assert second_entry.question_holds == 0
|
|
326
|
+
assert second_entry.entry_id == second_id
|
|
327
|
+
assert not (tmp_path / "missing.json").exists()
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
_PARALLEL_REFUSAL_SCRIPT = """
|
|
331
|
+
import sys
|
|
332
|
+
from pathlib import Path
|
|
333
|
+
sys.path.insert(0, sys.argv[1])
|
|
334
|
+
import auto_mode_refusal_ledger as ledger
|
|
335
|
+
ledger.record_refusal(
|
|
336
|
+
ledger.RefusedStep(
|
|
337
|
+
session_id="s1",
|
|
338
|
+
transcript_path=None,
|
|
339
|
+
tool_name="Bash",
|
|
340
|
+
tool_input={"command": sys.argv[2]},
|
|
341
|
+
working_folder=None,
|
|
342
|
+
rule_label="Git Destructive",
|
|
343
|
+
reason="[Git Destructive] force push",
|
|
344
|
+
action_summary=sys.argv[2],
|
|
345
|
+
),
|
|
346
|
+
Path(sys.argv[3]),
|
|
347
|
+
)
|
|
348
|
+
"""
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
def test_should_give_parallel_refusals_distinct_sequences_and_one_record_with_every_id(
|
|
352
|
+
tmp_path: Path, scratch_config_directory: Path
|
|
353
|
+
) -> None:
|
|
354
|
+
repository_root = _rule_repository(tmp_path)
|
|
355
|
+
hooks_folder = str(Path(ledger.__file__).resolve().parent)
|
|
356
|
+
all_commands = [f"git push -f origin claude/x{each_index}" for each_index in range(8)]
|
|
357
|
+
all_processes = [
|
|
358
|
+
subprocess.Popen(
|
|
359
|
+
[
|
|
360
|
+
sys.executable,
|
|
361
|
+
"-c",
|
|
362
|
+
_PARALLEL_REFUSAL_SCRIPT,
|
|
363
|
+
hooks_folder,
|
|
364
|
+
each_command,
|
|
365
|
+
str(repository_root),
|
|
366
|
+
]
|
|
367
|
+
)
|
|
368
|
+
for each_command in all_commands
|
|
369
|
+
]
|
|
370
|
+
assert [each.wait() for each in all_processes] == [0] * 8
|
|
371
|
+
ledger_directory = ledger.ledger_directory_for("s1")
|
|
372
|
+
all_entries = ledger.load_entries(ledger_directory)
|
|
373
|
+
assert sorted(each.sequence for each in all_entries) == list(range(1, 9))
|
|
374
|
+
all_records = ledger.load_question_records(ledger_directory)
|
|
375
|
+
five_oldest_ids = tuple(
|
|
376
|
+
each.entry_id for each in sorted(all_entries, key=lambda each: each.sequence)[:5]
|
|
377
|
+
)
|
|
378
|
+
five_step_question = ledger.question_text_for("Bash", "Git Destructive", 5)
|
|
379
|
+
assert [
|
|
380
|
+
each.all_entry_ids for each in all_records if each.question == five_step_question
|
|
381
|
+
] == [five_oldest_ids]
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
def test_should_clear_a_stale_ledger_lock_after_the_wait(
|
|
385
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
386
|
+
) -> None:
|
|
387
|
+
monkeypatch.setattr(ledger, "LEDGER_LOCK_WAIT_SECONDS", 0.05)
|
|
388
|
+
ledger_directory = tmp_path / "ledger"
|
|
389
|
+
ledger_directory.mkdir()
|
|
390
|
+
stale_lock_path = ledger_directory / ".ledger.lock"
|
|
391
|
+
stale_lock_path.write_text("", encoding="utf-8")
|
|
392
|
+
with ledger.ledger_lock(ledger_directory):
|
|
393
|
+
_add(ledger_directory, "git push -f")
|
|
394
|
+
assert not stale_lock_path.exists()
|
|
395
|
+
assert _answers(ledger_directory) == [None]
|
|
396
|
+
with ledger.ledger_lock(ledger_directory):
|
|
397
|
+
assert stale_lock_path.exists()
|
|
398
|
+
assert not stale_lock_path.exists()
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
"""Behavior tests for reading and writing the refusal ledger's files."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
import pytest
|
|
9
|
+
|
|
10
|
+
import auto_mode_refusal_ledger_files as ledger_files
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@pytest.fixture(autouse=True)
|
|
14
|
+
def scratch_config_directory(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
|
|
15
|
+
config_directory = tmp_path / "config"
|
|
16
|
+
config_directory.mkdir()
|
|
17
|
+
monkeypatch.setenv("CLAUDE_CONFIG_DIR", str(config_directory))
|
|
18
|
+
monkeypatch.setenv("HOME", str(tmp_path))
|
|
19
|
+
return config_directory
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _entry_fields(**overrides: object) -> dict[str, object]:
|
|
23
|
+
all_fields: dict[str, object] = {
|
|
24
|
+
"id": "3f9a",
|
|
25
|
+
"tool_name": "Bash",
|
|
26
|
+
"rule_label": "Git Destructive",
|
|
27
|
+
"action_summary": "git push -f",
|
|
28
|
+
"sequence": 2,
|
|
29
|
+
"answer": "kept",
|
|
30
|
+
}
|
|
31
|
+
all_fields.update(overrides)
|
|
32
|
+
return all_fields
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def test_should_place_the_ledger_under_the_config_directory(scratch_config_directory: Path) -> None:
|
|
36
|
+
assert ledger_files.ledger_directory_for("s-1") == (
|
|
37
|
+
scratch_config_directory / "state" / "auto-mode-refusals" / "s-1"
|
|
38
|
+
)
|
|
39
|
+
assert ledger_files.ledger_directory_for("../x").name == "___x"
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def test_should_give_two_edits_of_one_file_two_ids() -> None:
|
|
43
|
+
first_id = ledger_files.entry_id_for("Edit", {"file_path": "a.py", "old_string": "x"})
|
|
44
|
+
second_id = ledger_files.entry_id_for("Edit", {"file_path": "a.py", "old_string": "y"})
|
|
45
|
+
assert first_id != second_id
|
|
46
|
+
assert first_id == ledger_files.entry_id_for("Edit", {"old_string": "x", "file_path": "a.py"})
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def test_should_give_a_retry_with_a_new_description_the_same_id() -> None:
|
|
50
|
+
first_id = ledger_files.entry_id_for(
|
|
51
|
+
"Bash", {"command": "python3 -c 1", "description": "Write marker file"}
|
|
52
|
+
)
|
|
53
|
+
retry_id = ledger_files.entry_id_for(
|
|
54
|
+
"Bash", {"command": "python3 -c 1", "description": "Retry writing marker file"}
|
|
55
|
+
)
|
|
56
|
+
other_command_id = ledger_files.entry_id_for(
|
|
57
|
+
"Bash", {"command": "python3 -c 2", "description": "Write marker file"}
|
|
58
|
+
)
|
|
59
|
+
assert first_id == retry_id
|
|
60
|
+
assert first_id != other_command_id
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def test_should_parse_an_entry_with_every_field_typed() -> None:
|
|
64
|
+
assert ledger_files.parse_entry(_entry_fields()) == ledger_files.LedgerEntry(
|
|
65
|
+
"3f9a", "Bash", "Git Destructive", "git push -f", 2, "kept"
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def test_should_parse_the_stop_gate_counts_and_asked_mark() -> None:
|
|
70
|
+
parsed_entry = ledger_files.parse_entry(
|
|
71
|
+
_entry_fields(question_holds=2, rule_holds=1, question_asked=True)
|
|
72
|
+
)
|
|
73
|
+
assert parsed_entry is not None
|
|
74
|
+
assert (parsed_entry.question_holds, parsed_entry.rule_holds) == (2, 1)
|
|
75
|
+
assert parsed_entry.is_question_asked
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
@pytest.mark.parametrize(
|
|
79
|
+
"overrides",
|
|
80
|
+
[
|
|
81
|
+
{"id": 7},
|
|
82
|
+
{"sequence": "first"},
|
|
83
|
+
{"sequence": True},
|
|
84
|
+
{"answer": "maybe"},
|
|
85
|
+
{"question_holds": "2"},
|
|
86
|
+
{"rule_holds": None},
|
|
87
|
+
{"question_asked": 1},
|
|
88
|
+
],
|
|
89
|
+
)
|
|
90
|
+
def test_should_reject_an_entry_with_a_field_of_the_wrong_type(
|
|
91
|
+
overrides: dict[str, object],
|
|
92
|
+
) -> None:
|
|
93
|
+
assert ledger_files.parse_entry(_entry_fields(**overrides)) is None
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def test_should_reject_an_entry_without_an_id_or_an_answer_key() -> None:
|
|
97
|
+
entry_fields = _entry_fields()
|
|
98
|
+
del entry_fields["id"]
|
|
99
|
+
assert ledger_files.parse_entry(entry_fields) is None
|
|
100
|
+
assert ledger_files.parse_entry({"question": "Q?"}) is None
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def test_should_parse_a_question_record_and_reject_a_bad_offset() -> None:
|
|
104
|
+
record_fields = {
|
|
105
|
+
"record": "question",
|
|
106
|
+
"question": "Q?",
|
|
107
|
+
"entry_ids": ["a", "b"],
|
|
108
|
+
"transcript_offset": 900,
|
|
109
|
+
}
|
|
110
|
+
assert ledger_files.parse_question_record(record_fields) == ledger_files.QuestionRecord(
|
|
111
|
+
"Q?", ("a", "b"), 900
|
|
112
|
+
)
|
|
113
|
+
assert (
|
|
114
|
+
ledger_files.parse_question_record({**record_fields, "transcript_offset": "late"}) is None
|
|
115
|
+
)
|
|
116
|
+
assert ledger_files.parse_question_record({**record_fields, "entry_ids": [1]}) is None
|
|
117
|
+
assert ledger_files.parse_question_record(
|
|
118
|
+
{**record_fields, "tool_name": "Bash"}
|
|
119
|
+
) == ledger_files.QuestionRecord("Q?", ("a", "b"), 900, "Bash")
|
|
120
|
+
assert ledger_files.parse_question_record({**record_fields, "tool_name": 7}) == (
|
|
121
|
+
ledger_files.QuestionRecord("Q?", ("a", "b"), 900, "")
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def test_should_write_and_read_back_one_object(tmp_path: Path) -> None:
|
|
126
|
+
entry_path = ledger_files.entry_file_path(tmp_path / "ledger", "3f9a")
|
|
127
|
+
ledger_files.write_json_object(entry_path, _entry_fields())
|
|
128
|
+
assert entry_path.name == "3f9a.json"
|
|
129
|
+
assert ledger_files.read_json_object(entry_path) == _entry_fields()
|
|
130
|
+
entry_path.write_text("[1, 2]", encoding="utf-8")
|
|
131
|
+
assert ledger_files.read_json_object(entry_path) is None
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def test_should_load_only_the_files_that_parse_oldest_first(tmp_path: Path) -> None:
|
|
135
|
+
ledger_files.write_json_object(tmp_path / "late.json", _entry_fields(id="late", sequence=5))
|
|
136
|
+
ledger_files.write_json_object(tmp_path / "early.json", _entry_fields(id="early", sequence=1))
|
|
137
|
+
(tmp_path / "no-id.json").write_text(json.dumps({"answer": None}), encoding="utf-8")
|
|
138
|
+
(tmp_path / "broken.json").write_text("{", encoding="utf-8")
|
|
139
|
+
assert [each.entry_id for each in ledger_files.load_entries(tmp_path)] == ["early", "late"]
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def test_should_load_only_the_question_records_that_parse(tmp_path: Path) -> None:
|
|
143
|
+
question_path = ledger_files.question_file_path(tmp_path, "Q?")
|
|
144
|
+
ledger_files.write_json_object(
|
|
145
|
+
question_path,
|
|
146
|
+
{"record": "question", "question": "Q?", "entry_ids": ["a"], "transcript_offset": 3},
|
|
147
|
+
)
|
|
148
|
+
ledger_files.write_json_object(
|
|
149
|
+
ledger_files.question_file_path(tmp_path, "Other?"),
|
|
150
|
+
{"record": "question", "question": "Other?", "entry_ids": "a"},
|
|
151
|
+
)
|
|
152
|
+
assert question_path.name.startswith("question-")
|
|
153
|
+
assert ledger_files.load_question_records(tmp_path) == [
|
|
154
|
+
ledger_files.QuestionRecord("Q?", ("a",), 3)
|
|
155
|
+
]
|