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
|
@@ -10,6 +10,7 @@ export const EVER_REGISTERED_HOOK_RELATIVE_PATHS = new Set([
|
|
|
10
10
|
'advisory/refactor_guard.py',
|
|
11
11
|
'blocking/agent-execution-intent-gate.py',
|
|
12
12
|
'blocking/artifact_dark_mode_gate.py',
|
|
13
|
+
'blocking/auto_mode_refusal_stop_gate.py',
|
|
13
14
|
'blocking/artifact_look_gate.py',
|
|
14
15
|
'blocking/ask_user_question_shape_blocker.py',
|
|
15
16
|
'blocking/ask_user_question_style_blocker.py',
|
|
@@ -1,15 +1,42 @@
|
|
|
1
1
|
#!/usr/bin/env python3
|
|
2
2
|
"""PermissionDenied advisor: a proposed quick fix for each auto mode denial.
|
|
3
3
|
|
|
4
|
-
This hook never retries the denied call. It reads the denial
|
|
5
|
-
classifier rule
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
A
|
|
12
|
-
|
|
4
|
+
This hook never retries the denied call. It reads the denial's ``reason``,
|
|
5
|
+
names the classifier rule, drafts one ``autoMode.allow`` entry for the denied
|
|
6
|
+
action, and adds a PowerShell 7 block that writes the entry to
|
|
7
|
+
``~/.claude/settings.json`` with ``"$defaults"`` kept. The agent receives the
|
|
8
|
+
proposal as ``additionalContext``; the user receives a one-line
|
|
9
|
+
``systemMessage``.
|
|
10
|
+
|
|
11
|
+
A ``reason`` that says auto mode could not evaluate the action, that the
|
|
12
|
+
classifier was unavailable or gave no verdict, that a hook changed the input
|
|
13
|
+
after review, or that is not text has no verdict. It gets a short note and no
|
|
14
|
+
block, because an allow entry cannot clear it.
|
|
15
|
+
|
|
16
|
+
The ``reason`` can echo text from a web page or a file. A rule label keeps
|
|
17
|
+
only ASCII letters, digits, spaces, ``-``, ``_`` and ``/``, and every quote
|
|
18
|
+
form PowerShell reads as a single quote is doubled where text enters a
|
|
19
|
+
single-quoted literal.
|
|
20
|
+
|
|
21
|
+
When ``CLAUDE_AUTO_MODE_RULE_PLAYBOOK`` names an existing file, the hook also
|
|
22
|
+
keeps the refusal ledger::
|
|
23
|
+
|
|
24
|
+
labelled refusal -> one ledger entry per distinct step, the group's
|
|
25
|
+
question record, and the exact question appended
|
|
26
|
+
to the agent's context
|
|
27
|
+
unexplained danger -> the same, under the rule label ``Unexplained``
|
|
28
|
+
anything else -> today's output and no ledger file
|
|
29
|
+
|
|
30
|
+
An unexplained danger reason starts ``The server-side auto mode classifier
|
|
31
|
+
judged this action dangerous``. It first reads the user's answers from
|
|
32
|
+
``transcript_path``. It writes no entry for any other reason with no rule
|
|
33
|
+
label, or for one of the rule flow's own
|
|
34
|
+
steps, which ``auto_mode_rule_steps.py`` names: a Bash command that runs
|
|
35
|
+
``setup/add_auto_mode_rule.py``, or, once the session holds an allowed entry,
|
|
36
|
+
a Bash ``git commit`` or ``git push`` of a ``claude/auto-mode-rule-`` branch
|
|
37
|
+
in the playbook's repository or one of its worktrees. Any ledger or
|
|
38
|
+
transcript error, and a playbook with no repository folder above it, gives
|
|
39
|
+
today's output.
|
|
13
40
|
"""
|
|
14
41
|
|
|
15
42
|
from __future__ import annotations
|
|
@@ -17,40 +44,145 @@ from __future__ import annotations
|
|
|
17
44
|
import json
|
|
18
45
|
import re
|
|
19
46
|
import sys
|
|
47
|
+
import unicodedata
|
|
48
|
+
from dataclasses import dataclass
|
|
20
49
|
from pathlib import Path
|
|
21
50
|
|
|
22
51
|
_hooks_root_directory = str(Path(__file__).resolve().parent.parent)
|
|
23
52
|
if _hooks_root_directory not in sys.path:
|
|
24
53
|
sys.path.insert(0, _hooks_root_directory)
|
|
25
54
|
|
|
55
|
+
import auto_mode_refusal_ledger
|
|
26
56
|
from hooks_constants.auto_mode_denial_quick_fix_constants import (
|
|
27
57
|
ALL_COMMAND_INPUT_KEYS,
|
|
58
|
+
ALL_NO_VERDICT_REASON_PREFIXES,
|
|
59
|
+
ALL_POWERSHELL_SINGLE_QUOTE_CHARACTERS,
|
|
28
60
|
ALLOW_ENTRY_TEMPLATE,
|
|
61
|
+
BARE_LABEL_PATTERN,
|
|
62
|
+
BRACKETED_LABEL_PATTERN,
|
|
63
|
+
CLASSIFIER_REASON_PREFIX_PATTERN,
|
|
64
|
+
DEFAULT_TOOL_NAME,
|
|
29
65
|
HOOK_EVENT_NAME,
|
|
66
|
+
LABELLED_RULE_WORDING_COMMAND_TEMPLATE,
|
|
67
|
+
LABELLED_RULE_WORDING_LINE_TEMPLATE,
|
|
30
68
|
MAXIMUM_ACTION_SUMMARY_CHARACTERS,
|
|
69
|
+
MAXIMUM_PROSE_RULE_NAME_CHARACTERS,
|
|
31
70
|
NO_VERDICT_CONTEXT_TEMPLATE,
|
|
32
71
|
POWERSHELL_BLOCK_TEMPLATE,
|
|
33
|
-
|
|
72
|
+
QUESTION_CONTEXT_TEMPLATE,
|
|
73
|
+
RULE_LABEL_DISALLOWED_CHARACTER_PATTERN,
|
|
74
|
+
SENTENCE_END_PATTERN,
|
|
34
75
|
SUMMARY_WORD_SEPARATOR,
|
|
35
76
|
TRUNCATION_MARKER,
|
|
77
|
+
UNEXPLAINED_RULE_LABEL,
|
|
78
|
+
UNEXPLAINED_VERDICT_REASON_PREFIX,
|
|
79
|
+
UNLABELLED_RULE_WORDING_COMMAND,
|
|
80
|
+
UNLABELLED_RULE_WORDING_LINE_TEMPLATE,
|
|
36
81
|
UNNAMED_RULE_LABEL,
|
|
37
82
|
USER_MESSAGE_TEMPLATE,
|
|
38
83
|
VERDICT_CONTEXT_TEMPLATE,
|
|
39
84
|
)
|
|
40
85
|
|
|
41
86
|
|
|
42
|
-
|
|
43
|
-
|
|
87
|
+
@dataclass(frozen=True)
|
|
88
|
+
class DenialReason:
|
|
89
|
+
"""One parsed PermissionDenied ``reason``.
|
|
90
|
+
|
|
91
|
+
Attributes:
|
|
92
|
+
rule_label: The classifier rule label, or None when the text has none.
|
|
93
|
+
has_verdict: False when auto mode reached no verdict on the action.
|
|
94
|
+
prose: The reason text after the label, or the whole text.
|
|
95
|
+
"""
|
|
96
|
+
|
|
97
|
+
rule_label: str | None
|
|
98
|
+
has_verdict: bool
|
|
99
|
+
prose: str
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def _one_display_line(text: str) -> str:
|
|
103
|
+
single_line_text = SUMMARY_WORD_SEPARATOR.join(text.split())
|
|
104
|
+
return "".join(
|
|
105
|
+
each_character
|
|
106
|
+
for each_character in single_line_text
|
|
107
|
+
if not unicodedata.category(each_character).startswith("C")
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def _safe_rule_label(text: str) -> str:
|
|
112
|
+
kept_characters = re.sub(RULE_LABEL_DISALLOWED_CHARACTER_PATTERN, "", _one_display_line(text))
|
|
113
|
+
return SUMMARY_WORD_SEPARATOR.join(kept_characters.split())
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def parse_denial_reason(reason: str) -> DenialReason:
|
|
117
|
+
"""Split a PermissionDenied ``reason`` into its rule label and prose.
|
|
118
|
+
|
|
119
|
+
::
|
|
120
|
+
|
|
121
|
+
"[Git Destructive] force push" -> label "Git Destructive", verdict
|
|
122
|
+
"[Don't Touch] edits a file" -> label "Dont Touch", verdict
|
|
123
|
+
"Unauthorized Persistence, because" -> label "Unauthorized Persistence", verdict
|
|
124
|
+
"the command cancels runs." -> no label, verdict
|
|
125
|
+
"Classifier unavailable" -> no label, no verdict
|
|
126
|
+
"The server-side auto mode classifier
|
|
127
|
+
gave no verdict (timed out)" -> no label, no verdict
|
|
128
|
+
"Reason: [Git Destructive] force" -> label "Git Destructive", verdict
|
|
129
|
+
"The server-side auto mode classifier judged this action dangerous
|
|
130
|
+
(it gave no explanation)" -> label "Unexplained", verdict
|
|
131
|
+
|
|
132
|
+
A leading ``Reason:``, and the classifier sentence ``Permission for this
|
|
133
|
+
action was denied by the Claude Code auto mode classifier.`` before it,
|
|
134
|
+
are dropped first. A reason that starts ``The server-side auto mode
|
|
135
|
+
classifier judged this action dangerous`` gets the label ``Unexplained``,
|
|
136
|
+
and the whole text is the prose. A bracketed label counts only at the
|
|
137
|
+
start of the remaining text. A bare label is two or more capitalised words that end at a comma, colon, semicolon,
|
|
138
|
+
period, spaced dash, or the end of the text. A label keeps only ASCII
|
|
139
|
+
letters, digits, spaces, ``-``, ``_`` and ``/``, with whitespace
|
|
140
|
+
collapsed. A label left with none of them is no label, and the whole
|
|
141
|
+
text is the prose. An empty reason has no verdict, and so has a reason
|
|
142
|
+
that starts ``Auto mode could not evaluate this action``, ``Classifier
|
|
143
|
+
unavailable``, ``The server-side auto mode classifier gave no verdict``
|
|
144
|
+
or ``a hook changed this call's input after auto mode reviewed it``.
|
|
44
145
|
|
|
45
146
|
Args:
|
|
46
|
-
|
|
47
|
-
``"[Security Weaken] lowers a guard"``.
|
|
147
|
+
reason: The ``reason`` text from the hook input.
|
|
48
148
|
|
|
49
149
|
Returns:
|
|
50
|
-
The
|
|
150
|
+
The label or None, whether a verdict exists, and the prose.
|
|
51
151
|
"""
|
|
52
|
-
|
|
53
|
-
|
|
152
|
+
stripped_reason = re.sub(CLASSIFIER_REASON_PREFIX_PATTERN, "", reason, count=1).strip()
|
|
153
|
+
if not stripped_reason or stripped_reason.lower().startswith(ALL_NO_VERDICT_REASON_PREFIXES):
|
|
154
|
+
return DenialReason(rule_label=None, has_verdict=False, prose=stripped_reason)
|
|
155
|
+
if stripped_reason.lower().startswith(UNEXPLAINED_VERDICT_REASON_PREFIX):
|
|
156
|
+
return DenialReason(
|
|
157
|
+
rule_label=UNEXPLAINED_RULE_LABEL, has_verdict=True, prose=stripped_reason
|
|
158
|
+
)
|
|
159
|
+
for each_label_pattern in (BRACKETED_LABEL_PATTERN, BARE_LABEL_PATTERN):
|
|
160
|
+
label_match = re.match(each_label_pattern, stripped_reason, re.DOTALL)
|
|
161
|
+
safe_label = _safe_rule_label(label_match.group("label")) if label_match else ""
|
|
162
|
+
if safe_label:
|
|
163
|
+
return DenialReason(
|
|
164
|
+
rule_label=safe_label,
|
|
165
|
+
has_verdict=True,
|
|
166
|
+
prose=label_match.group("prose").strip(),
|
|
167
|
+
)
|
|
168
|
+
return DenialReason(rule_label=None, has_verdict=True, prose=stripped_reason)
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def _prose_rule_name(prose: str) -> str:
|
|
172
|
+
first_sentence = re.split(SENTENCE_END_PATTERN, prose, maxsplit=1)[0]
|
|
173
|
+
cleaned_name = _safe_rule_label(first_sentence)
|
|
174
|
+
if len(cleaned_name) <= MAXIMUM_PROSE_RULE_NAME_CHARACTERS:
|
|
175
|
+
return cleaned_name or UNNAMED_RULE_LABEL
|
|
176
|
+
kept_length = MAXIMUM_PROSE_RULE_NAME_CHARACTERS - len(TRUNCATION_MARKER)
|
|
177
|
+
return cleaned_name[:kept_length] + TRUNCATION_MARKER
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def _rule_name_for(denial: DenialReason) -> str:
|
|
181
|
+
if denial.rule_label:
|
|
182
|
+
return denial.rule_label
|
|
183
|
+
if denial.has_verdict:
|
|
184
|
+
return _prose_rule_name(denial.prose)
|
|
185
|
+
return UNNAMED_RULE_LABEL
|
|
54
186
|
|
|
55
187
|
|
|
56
188
|
def _summary_source_text(tool_input: object) -> str:
|
|
@@ -72,29 +204,71 @@ def action_summary_from(tool_input: object) -> str:
|
|
|
72
204
|
The command, URL or path when the input has one, else the compact
|
|
73
205
|
JSON input, cut to the summary length limit.
|
|
74
206
|
"""
|
|
75
|
-
single_line_text =
|
|
207
|
+
single_line_text = _one_display_line(_summary_source_text(tool_input))
|
|
76
208
|
if len(single_line_text) <= MAXIMUM_ACTION_SUMMARY_CHARACTERS:
|
|
77
209
|
return single_line_text
|
|
78
210
|
kept_length = MAXIMUM_ACTION_SUMMARY_CHARACTERS - len(TRUNCATION_MARKER)
|
|
79
211
|
return single_line_text[:kept_length] + TRUNCATION_MARKER
|
|
80
212
|
|
|
81
213
|
|
|
214
|
+
def _powershell_literal_text(text: str) -> str:
|
|
215
|
+
for each_quote_character in ALL_POWERSHELL_SINGLE_QUOTE_CHARACTERS:
|
|
216
|
+
text = text.replace(each_quote_character, each_quote_character + each_quote_character)
|
|
217
|
+
return text
|
|
218
|
+
|
|
219
|
+
|
|
82
220
|
def powershell_block_for(allow_entry: str) -> str:
|
|
83
221
|
"""Return the PowerShell 7 block that adds one allow entry.
|
|
84
222
|
|
|
223
|
+
PowerShell ends a single-quoted literal at ``'`` and at the curly quotes
|
|
224
|
+
U+2018 to U+201B, so each of them is doubled.
|
|
225
|
+
|
|
85
226
|
Args:
|
|
86
227
|
allow_entry: The ``autoMode.allow`` entry text.
|
|
87
228
|
|
|
88
229
|
Returns:
|
|
89
230
|
The block, with the entry in a single-quoted PowerShell literal.
|
|
90
231
|
"""
|
|
91
|
-
escaped_entry = allow_entry
|
|
232
|
+
escaped_entry = _powershell_literal_text(allow_entry)
|
|
92
233
|
return POWERSHELL_BLOCK_TEMPLATE.replace("{escaped_entry}", escaped_entry)
|
|
93
234
|
|
|
94
235
|
|
|
236
|
+
def rule_wording_command_for(rule_label: str | None) -> str:
|
|
237
|
+
"""Return the command that prints the classifier rule wording.
|
|
238
|
+
|
|
239
|
+
::
|
|
240
|
+
|
|
241
|
+
"Git Destructive" -> claude auto-mode defaults --label 'Git Destructive'
|
|
242
|
+
None -> claude auto-mode defaults
|
|
243
|
+
|
|
244
|
+
Args:
|
|
245
|
+
rule_label: The parsed rule label, or None for a prose-named reason.
|
|
246
|
+
|
|
247
|
+
Returns:
|
|
248
|
+
The command, with the label in a single-quoted literal whose quotes
|
|
249
|
+
are doubled.
|
|
250
|
+
"""
|
|
251
|
+
if rule_label is None:
|
|
252
|
+
return UNLABELLED_RULE_WORDING_COMMAND
|
|
253
|
+
return LABELLED_RULE_WORDING_COMMAND_TEMPLATE.format(
|
|
254
|
+
escaped_label=_powershell_literal_text(rule_label)
|
|
255
|
+
)
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
def _rule_wording_line_for(rule_label: str | None) -> str:
|
|
259
|
+
named_rule_label = None if rule_label == UNEXPLAINED_RULE_LABEL else rule_label
|
|
260
|
+
line_template = (
|
|
261
|
+
UNLABELLED_RULE_WORDING_LINE_TEMPLATE
|
|
262
|
+
if named_rule_label is None
|
|
263
|
+
else LABELLED_RULE_WORDING_LINE_TEMPLATE
|
|
264
|
+
)
|
|
265
|
+
return line_template.format(command=rule_wording_command_for(named_rule_label))
|
|
266
|
+
|
|
267
|
+
|
|
95
268
|
def _verdict_context_text(
|
|
96
|
-
tool_name: str, denial_reason: str, action_summary: str,
|
|
269
|
+
tool_name: str, denial_reason: str, action_summary: str, parsed_denial: DenialReason
|
|
97
270
|
) -> str:
|
|
271
|
+
rule_label = _rule_name_for(parsed_denial)
|
|
98
272
|
allow_entry = ALLOW_ENTRY_TEMPLATE.format(
|
|
99
273
|
rule_label=rule_label,
|
|
100
274
|
tool_name=tool_name,
|
|
@@ -106,6 +280,7 @@ def _verdict_context_text(
|
|
|
106
280
|
denial_reason=denial_reason,
|
|
107
281
|
action_summary=action_summary,
|
|
108
282
|
powershell_block=powershell_block_for(allow_entry),
|
|
283
|
+
rule_wording_line=_rule_wording_line_for(parsed_denial.rule_label),
|
|
109
284
|
)
|
|
110
285
|
|
|
111
286
|
|
|
@@ -117,14 +292,20 @@ def build_hook_output(all_denial_fields: dict[str, object]) -> dict[str, object]
|
|
|
117
292
|
|
|
118
293
|
Returns:
|
|
119
294
|
A ``hookSpecificOutput`` with ``additionalContext`` for the agent and
|
|
120
|
-
a top-level ``systemMessage`` for the user.
|
|
295
|
+
a top-level ``systemMessage`` for the user. A ``reason`` that is not
|
|
296
|
+
text has no verdict, and the reason is shown on one line with
|
|
297
|
+
control characters removed.
|
|
121
298
|
"""
|
|
122
|
-
tool_name = str(all_denial_fields.get("tool_name") or
|
|
123
|
-
|
|
299
|
+
tool_name = str(all_denial_fields.get("tool_name") or DEFAULT_TOOL_NAME)
|
|
300
|
+
raw_reason = all_denial_fields.get("reason")
|
|
301
|
+
denial_reason = _one_display_line(raw_reason) if isinstance(raw_reason, str) else ""
|
|
124
302
|
action_summary = action_summary_from(all_denial_fields.get("tool_input"))
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
303
|
+
parsed_denial = parse_denial_reason(denial_reason)
|
|
304
|
+
rule_label = _rule_name_for(parsed_denial)
|
|
305
|
+
if parsed_denial.has_verdict:
|
|
306
|
+
context_text = _verdict_context_text(
|
|
307
|
+
tool_name, denial_reason, action_summary, parsed_denial
|
|
308
|
+
)
|
|
128
309
|
else:
|
|
129
310
|
context_text = NO_VERDICT_CONTEXT_TEMPLATE.format(
|
|
130
311
|
tool_name=tool_name,
|
|
@@ -140,6 +321,73 @@ def build_hook_output(all_denial_fields: dict[str, object]) -> dict[str, object]
|
|
|
140
321
|
}
|
|
141
322
|
|
|
142
323
|
|
|
324
|
+
def _refused_step_from(
|
|
325
|
+
all_denial_fields: dict[str, object],
|
|
326
|
+
) -> auto_mode_refusal_ledger.RefusedStep | None:
|
|
327
|
+
session_id = all_denial_fields.get("session_id")
|
|
328
|
+
if not isinstance(session_id, str) or not session_id:
|
|
329
|
+
return None
|
|
330
|
+
raw_transcript_path = all_denial_fields.get("transcript_path")
|
|
331
|
+
raw_working_folder = all_denial_fields.get("cwd")
|
|
332
|
+
raw_reason = all_denial_fields.get("reason")
|
|
333
|
+
denial_reason = _one_display_line(raw_reason) if isinstance(raw_reason, str) else ""
|
|
334
|
+
tool_input = all_denial_fields.get("tool_input")
|
|
335
|
+
return auto_mode_refusal_ledger.RefusedStep(
|
|
336
|
+
session_id=session_id,
|
|
337
|
+
transcript_path=Path(raw_transcript_path) if isinstance(raw_transcript_path, str) else None,
|
|
338
|
+
tool_name=str(all_denial_fields.get("tool_name") or DEFAULT_TOOL_NAME),
|
|
339
|
+
tool_input=tool_input,
|
|
340
|
+
working_folder=raw_working_folder if isinstance(raw_working_folder, str) else None,
|
|
341
|
+
rule_label=parse_denial_reason(denial_reason).rule_label,
|
|
342
|
+
reason=denial_reason,
|
|
343
|
+
action_summary=action_summary_from(tool_input),
|
|
344
|
+
)
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
def _question_context_for(all_denial_fields: dict[str, object]) -> str | None:
|
|
348
|
+
playbook_path = auto_mode_refusal_ledger.playbook_path_from_environment()
|
|
349
|
+
if playbook_path is None:
|
|
350
|
+
return None
|
|
351
|
+
repository_root = auto_mode_refusal_ledger.rule_repository_root(playbook_path)
|
|
352
|
+
refused_step = _refused_step_from(all_denial_fields)
|
|
353
|
+
if repository_root is None or refused_step is None:
|
|
354
|
+
return None
|
|
355
|
+
question = auto_mode_refusal_ledger.record_refusal(refused_step, repository_root)
|
|
356
|
+
if question is None:
|
|
357
|
+
return None
|
|
358
|
+
return QUESTION_CONTEXT_TEMPLATE.format(question=question, playbook_path=playbook_path)
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
def hook_output_with_ledger(all_denial_fields: dict[str, object]) -> dict[str, object]:
|
|
362
|
+
"""Return the hook output, with the ledger kept when the flow is on.
|
|
363
|
+
|
|
364
|
+
::
|
|
365
|
+
|
|
366
|
+
flow off or playbook missing -> build_hook_output, byte for byte
|
|
367
|
+
playbook with no repository above -> build_hook_output
|
|
368
|
+
flow on, question returned -> same, plus the AUTO MODE QUESTION block
|
|
369
|
+
any ledger or transcript error -> build_hook_output
|
|
370
|
+
|
|
371
|
+
Args:
|
|
372
|
+
all_denial_fields: The hook input JSON.
|
|
373
|
+
|
|
374
|
+
Returns:
|
|
375
|
+
The PermissionDenied hook output.
|
|
376
|
+
"""
|
|
377
|
+
hook_output = build_hook_output(all_denial_fields)
|
|
378
|
+
try:
|
|
379
|
+
question_context = _question_context_for(all_denial_fields)
|
|
380
|
+
except Exception:
|
|
381
|
+
return hook_output
|
|
382
|
+
if question_context is None:
|
|
383
|
+
return hook_output
|
|
384
|
+
hook_specific_output = dict(hook_output["hookSpecificOutput"])
|
|
385
|
+
hook_specific_output["additionalContext"] = (
|
|
386
|
+
str(hook_specific_output["additionalContext"]) + question_context
|
|
387
|
+
)
|
|
388
|
+
return {**hook_output, "hookSpecificOutput": hook_specific_output}
|
|
389
|
+
|
|
390
|
+
|
|
143
391
|
def main() -> int:
|
|
144
392
|
try:
|
|
145
393
|
payload = json.load(sys.stdin)
|
|
@@ -147,7 +395,7 @@ def main() -> int:
|
|
|
147
395
|
return 0
|
|
148
396
|
if not isinstance(payload, dict):
|
|
149
397
|
return 0
|
|
150
|
-
sys.stdout.write(json.dumps(
|
|
398
|
+
sys.stdout.write(json.dumps(hook_output_with_ledger(payload)))
|
|
151
399
|
sys.stdout.flush()
|
|
152
400
|
return 0
|
|
153
401
|
|