howtofixcli 0.7.13 → 0.7.15
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/dist/chunk-27TRKD65.js +2 -0
- package/dist/chunk-3FYCFLNK.js +1 -0
- package/dist/chunk-56SEAGAQ.js +1 -0
- package/dist/chunk-6FUWMLWO.js +1 -0
- package/dist/chunk-7CHDNTH6.js +1 -0
- package/dist/chunk-7DTQKDO4.js +2 -0
- package/dist/chunk-AGGIYXN7.js +2 -0
- package/dist/chunk-AUDMRGWF.js +189 -0
- package/dist/chunk-CPJKKHGH.js +1 -0
- package/dist/chunk-DAPXGSGD.js +12 -0
- package/dist/chunk-DOD7MUGX.js +1 -0
- package/dist/chunk-DV2BBBBJ.js +2 -0
- package/dist/chunk-EIBOS5KO.js +5 -0
- package/dist/chunk-EYWOVCSS.js +6 -0
- package/dist/chunk-FPXVDP32.js +8 -0
- package/dist/chunk-GXB7RZHH.js +1 -0
- package/dist/chunk-HT3MFLAV.js +34 -0
- package/dist/chunk-ILDDDLRC.js +1 -0
- package/dist/chunk-JFYVRWJI.js +2 -0
- package/dist/chunk-JMKK26AT.js +2 -0
- package/dist/chunk-LAHVHRCS.js +2 -0
- package/dist/chunk-LOOFOBTS.js +1 -0
- package/dist/chunk-LSJC73DG.js +1 -0
- package/dist/chunk-MOKUBKE7.js +1 -0
- package/dist/chunk-N345KWWK.js +1 -0
- package/dist/chunk-ONZLNFQ2.js +3 -0
- package/dist/chunk-ORJ2QZBG.js +3 -0
- package/dist/chunk-PCALBZOO.js +2 -0
- package/dist/chunk-SX5VE6XA.js +110 -0
- package/dist/chunk-TSEKPGK4.js +1 -0
- package/dist/chunk-UNK5HRQR.js +1 -0
- package/dist/chunk-UTYFLLSM.js +1 -0
- package/dist/chunk-V2RPZMLK.js +1 -0
- package/dist/chunk-V3R4FYTJ.js +1 -0
- package/dist/chunk-V656XVXV.js +8 -0
- package/dist/chunk-VLMQSD25.js +1 -0
- package/dist/chunk-WKUJ7CFO.js +1 -0
- package/dist/chunk-WOT6VMZA.js +1 -0
- package/dist/chunk-WUPU6LTD.js +4 -0
- package/dist/chunk-WX3V5P7U.js +5 -0
- package/dist/chunk-Y7YM5R75.js +5 -0
- package/dist/chunk-YW7NORH3.js +2 -0
- package/dist/main.js +99 -540
- package/package.json +17 -64
- package/dist/account-credential.js +0 -136
- package/dist/account-credential.test.js +0 -38
- package/dist/activity.js +0 -124
- package/dist/agent-defs.js +0 -237
- package/dist/agent-limiter.js +0 -68
- package/dist/agents.js +0 -554
- package/dist/anim.js +0 -361
- package/dist/args.js +0 -228
- package/dist/at-file.js +0 -28
- package/dist/brand-extension.js +0 -87
- package/dist/brand.js +0 -76
- package/dist/browser.js +0 -58
- package/dist/cache-report.js +0 -139
- package/dist/checkpoint.js +0 -437
- package/dist/claims.js +0 -167
- package/dist/cli-auth.js +0 -142
- package/dist/cli-checkin.js +0 -120
- package/dist/cli-credentials.js +0 -254
- package/dist/clipboard.js +0 -149
- package/dist/collapse.js +0 -129
- package/dist/commands.js +0 -294
- package/dist/compaction-guard.js +0 -81
- package/dist/context-extension.js +0 -17
- package/dist/dcp/commands.js +0 -172
- package/dist/dcp/compress-state.js +0 -139
- package/dist/dcp/compress.js +0 -329
- package/dist/dcp/dcp-extension.js +0 -295
- package/dist/dcp/dedup.js +0 -93
- package/dist/dcp/ids.js +0 -189
- package/dist/dcp/index.js +0 -25
- package/dist/dcp/middle-prune.js +0 -76
- package/dist/dcp/prompts.js +0 -68
- package/dist/dcp/protected-content.js +0 -221
- package/dist/dcp/protected.js +0 -161
- package/dist/dcp/prune.js +0 -418
- package/dist/dcp/state.js +0 -176
- package/dist/device-id.js +0 -90
- package/dist/device-id.test.js +0 -23
- package/dist/errors.js +0 -63
- package/dist/extensions.js +0 -48
- package/dist/fatal.js +0 -35
- package/dist/feed.js +0 -498
- package/dist/fs-atomic.js +0 -54
- package/dist/gateway-errors.js +0 -90
- package/dist/git.js +0 -230
- package/dist/goal-extension.js +0 -88
- package/dist/goal.js +0 -222
- package/dist/hooks-extension.js +0 -176
- package/dist/hooks.js +0 -443
- package/dist/howtofix-account.js +0 -735
- package/dist/hud-line.js +0 -153
- package/dist/hwid.js +0 -130
- package/dist/interrupt.js +0 -60
- package/dist/json-events.js +0 -50
- package/dist/level-style.js +0 -112
- package/dist/loop-guard.js +0 -95
- package/dist/lsp-tools.js +0 -32
- package/dist/lsp.js +0 -429
- package/dist/mcp-config.js +0 -85
- package/dist/mcp-lifecycle.js +0 -147
- package/dist/model-catalog.js +0 -217
- package/dist/model-facts.js +0 -35
- package/dist/model-resolve.js +0 -65
- package/dist/notes.js +0 -78
- package/dist/oauth-login.js +0 -568
- package/dist/paths.js +0 -61
- package/dist/permissions.js +0 -298
- package/dist/plan-mode.js +0 -363
- package/dist/preset-command.js +0 -227
- package/dist/preset-extension.js +0 -35
- package/dist/preset-mcp.js +0 -292
- package/dist/presets.js +0 -727
- package/dist/prompt-files.js +0 -103
- package/dist/prompt-text.js +0 -41
- package/dist/proxy.js +0 -137
- package/dist/quality/ast.js +0 -196
- package/dist/quality/cognitive.js +0 -83
- package/dist/quality/cyclomatic.js +0 -30
- package/dist/quality/halstead.js +0 -83
- package/dist/quality/index.js +0 -18
- package/dist/quality/length.js +0 -3
- package/dist/quality/maintainability.js +0 -15
- package/dist/quality/nesting.js +0 -57
- package/dist/quality/report.js +0 -803
- package/dist/quality/smells.js +0 -67
- package/dist/quality-tool.js +0 -63
- package/dist/read-only-commands.js +0 -597
- package/dist/repo-map.js +0 -292
- package/dist/rtk.js +0 -193
- package/dist/sandbox.js +0 -105
- package/dist/schema.js +0 -214
- package/dist/session-lifecycle.js +0 -19
- package/dist/session-retention.js +0 -111
- package/dist/session.js +0 -447
- package/dist/sessions.js +0 -100
- package/dist/settings.js +0 -148
- package/dist/skills.js +0 -35
- package/dist/slop-tool.js +0 -96
- package/dist/spend-cap.js +0 -43
- package/dist/subagent.js +0 -171
- package/dist/tar.js +0 -146
- package/dist/theme.js +0 -79
- package/dist/thinking.js +0 -45
- package/dist/todo.js +0 -114
- package/dist/tool-loadout.js +0 -97
- package/dist/tool-render.js +0 -769
- package/dist/tools.js +0 -176
- package/dist/ui.js +0 -4778
- package/dist/update.js +0 -551
- package/dist/usage-segment.js +0 -69
- package/dist/web-tools.js +0 -46
- package/dist/web.js +0 -265
- package/dist/welcome.js +0 -179
- package/dist/workflow-engine.js +0 -246
- package/dist/workflow-progress.js +0 -446
- package/dist/workflow-view.js +0 -195
- package/dist/workflow.js +0 -349
package/dist/hooks.js
DELETED
|
@@ -1,443 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* User-configured hooks: shell commands that run at lifecycle points.
|
|
3
|
-
*
|
|
4
|
-
* Why this exists: every extension in this CLI is compiled into the bundle. A
|
|
5
|
-
* user who wants "run prettier after every edit" or "refuse any `rm -rf`" or
|
|
6
|
-
* "add our house rules to every prompt" has to edit the source and rebuild — or
|
|
7
|
-
* repeat themselves every session. Claude Code answers this with **hooks**: a
|
|
8
|
-
* JSON config naming a shell command per lifecycle point, and a stdin/stdout
|
|
9
|
-
* contract the command speaks. That is the right shape for this because the
|
|
10
|
-
* command can be anything the user already has — a script, `jq`, a linter — with
|
|
11
|
-
* no build step and no TypeScript.
|
|
12
|
-
*
|
|
13
|
-
* The SDK's extension API is the substrate (`hxf.on(...)` exposes 48 events), but
|
|
14
|
-
* it requires code in the bundle. This module is the declarative layer on top: a
|
|
15
|
-
* config file, mapped onto the SDK's own events.
|
|
16
|
-
*
|
|
17
|
-
* <AGENT_DIR>/hooks.json
|
|
18
|
-
*
|
|
19
|
-
* ## The contract (deliberately Claude Code's, so it is already familiar)
|
|
20
|
-
*
|
|
21
|
-
* The command receives a JSON payload on **stdin** and answers on **stdout**:
|
|
22
|
-
*
|
|
23
|
-
* - **exit 0** — allowed. `stdout` may carry JSON with a `hookSpecificOutput`
|
|
24
|
-
* block (see below). Plain text on stdout is added as context where the event
|
|
25
|
-
* has a place for it.
|
|
26
|
-
* - **exit 2** — blocked. `stderr` is the reason the model and the user are
|
|
27
|
-
* shown. This is Claude Code's convention and it is the useful one: a hook
|
|
28
|
-
* needs a way to say no that is not the same as "the hook crashed".
|
|
29
|
-
* - **any other exit** — the hook failed. It is reported and the action is
|
|
30
|
-
* allowed, because a broken linter must not become a broken session.
|
|
31
|
-
*
|
|
32
|
-
* `hookSpecificOutput` fields, matching Claude Code:
|
|
33
|
-
*
|
|
34
|
-
* ```json
|
|
35
|
-
* { "hookSpecificOutput": {
|
|
36
|
-
* "hookEventName": "PreToolUse",
|
|
37
|
-
* "permissionDecision": "deny",
|
|
38
|
-
* "permissionDecisionReason": "no rm -rf in this repo",
|
|
39
|
-
* "additionalContext": "…" } }
|
|
40
|
-
* ```
|
|
41
|
-
*
|
|
42
|
-
* ## What is supported, and what is not
|
|
43
|
-
*
|
|
44
|
-
* Supported: `PreToolUse` (block a call, or rewrite its arguments),
|
|
45
|
-
* `PostToolUse` (add context after one), `UserPromptSubmit` (add context to a
|
|
46
|
-
* prompt, or block it), `SessionStart` (add context once per session), `Stop`
|
|
47
|
-
* (notification when a run settles), `PreCompact` (observe a compaction),
|
|
48
|
-
* `SessionEnd` (observe a shutdown).
|
|
49
|
-
*
|
|
50
|
-
* Not supported, and named rather than silently ignored: `SubagentStop`,
|
|
51
|
-
* `Notification`, `PermissionRequest`, `PostToolUseFailure` do not map onto an
|
|
52
|
-
* event this CLI can act on yet. An unknown name is reported at load, never
|
|
53
|
-
* dropped quietly — a hook the user wrote that never fires is worse than a
|
|
54
|
-
* config error.
|
|
55
|
-
*
|
|
56
|
-
* ## Rules that are load-bearing
|
|
57
|
-
*
|
|
58
|
-
* - **A rewritten tool input is validated before it is used.** `updatedInput` is
|
|
59
|
-
* the most dangerous field in the contract: it replaces what the tool will
|
|
60
|
-
* actually do. Claude Code rejects a rewrite that fails the tool's schema, and
|
|
61
|
-
* so do we — an unvalidated rewrite turns a guard into a weapon. The schema
|
|
62
|
-
* comes from the SDK's own `getAllTools()`, so it is the same one the provider
|
|
63
|
-
* was given.
|
|
64
|
-
* - **A rewrite is applied only when nothing blocks.** A hook that denies wins
|
|
65
|
-
* over one that rewrites: refusing is the stronger statement.
|
|
66
|
-
* - **A hook is a subprocess on an interactive path, so it must be async and
|
|
67
|
-
* bounded.** Every spawn is `spawn` (never `execFileSync`) with a per-hook
|
|
68
|
-
* timeout, because a hung hook would otherwise freeze the TUI for as long as
|
|
69
|
-
* the process lives. On timeout the hook is killed and the action is allowed.
|
|
70
|
-
* - **A hook that cannot run is not a reason to refuse to work.** A missing
|
|
71
|
-
* config, unreadable file, bad JSON, or a command that does not exist all
|
|
72
|
-
* degrade to "no hooks" with a diagnostic.
|
|
73
|
-
* - **The config is read once per session**, not per tool call. Re-reading a file
|
|
74
|
-
* on every `tool_call` is a synchronous disk hit on the hot path.
|
|
75
|
-
*/
|
|
76
|
-
import { spawn } from "node:child_process";
|
|
77
|
-
import { existsSync, readFileSync } from "node:fs";
|
|
78
|
-
import { join } from "node:path";
|
|
79
|
-
import { Value } from "typebox/value";
|
|
80
|
-
import { AGENT_DIR } from "./paths.js";
|
|
81
|
-
/** The user's hook config. Named here so the docs and the check script agree. */
|
|
82
|
-
export const HOOKS_FILE = join(AGENT_DIR, "hooks.json");
|
|
83
|
-
/**
|
|
84
|
-
* A hook config is a payload, not a program. Past this size it is skipped rather
|
|
85
|
-
* than truncated, for the same reason `APPEND_SYSTEM.md` is: half a config is a
|
|
86
|
-
* different config.
|
|
87
|
-
*/
|
|
88
|
-
const MAX_BYTES = 64 * 1024;
|
|
89
|
-
/** Claude Code's default. Long enough for a real linter, short enough to not wedge a turn. */
|
|
90
|
-
export const DEFAULT_TIMEOUT_S = 30;
|
|
91
|
-
/** The events this CLI can actually act on. Anything else is reported. */
|
|
92
|
-
export const SUPPORTED_EVENTS = ["PreToolUse", "PostToolUse", "UserPromptSubmit", "SessionStart", "Stop", "PreCompact", "SessionEnd"];
|
|
93
|
-
/**
|
|
94
|
-
* Validate a rewritten tool input against the tool's own schema.
|
|
95
|
-
*
|
|
96
|
-
* The reference is the SDK's own extension runtime: it exposes `typebox/value`
|
|
97
|
-
* as a **virtual module** for extensions (`core/extensions/virtual-modules.js`),
|
|
98
|
-
* and uses it to validate the same tool inputs. Writing a hand-rolled checker
|
|
99
|
-
* instead would be a second, weaker opinion on a question the toolchain already
|
|
100
|
-
* answers — and the failure mode is the worst kind: a rewrite that passes our
|
|
101
|
-
* check and then breaks the tool.
|
|
102
|
-
*
|
|
103
|
-
* `typebox` is a direct dependency of the SDK, and the schema it gives us
|
|
104
|
-
* (`ToolInfo.parameters`) is the very one the provider was handed, so this is
|
|
105
|
-
* the same validation the model's own call goes through.
|
|
106
|
-
*
|
|
107
|
-
* Returns `undefined` when the value is acceptable, or a reason it is not.
|
|
108
|
-
*/
|
|
109
|
-
export function checkToolInput(schema, value) {
|
|
110
|
-
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
|
111
|
-
return "the rewrite is not a JSON object";
|
|
112
|
-
}
|
|
113
|
-
if (schema === null || typeof schema !== "object")
|
|
114
|
-
return undefined;
|
|
115
|
-
try {
|
|
116
|
-
if (Value.Check(schema, value))
|
|
117
|
-
return undefined;
|
|
118
|
-
const first = [...Value.Errors(schema, value)][0];
|
|
119
|
-
if (!first)
|
|
120
|
-
return "the rewrite does not match the tool's schema";
|
|
121
|
-
const at = "instancePath" in first ? String(first.instancePath) : "";
|
|
122
|
-
const where = at && at !== "/" ? ` at \`${at}\`` : "";
|
|
123
|
-
return `the rewrite does not match the tool's schema${where}: ${first.message}`;
|
|
124
|
-
}
|
|
125
|
-
catch (error) {
|
|
126
|
-
// A schema TypeBox cannot walk (an exotic tool, a future shape) must not
|
|
127
|
-
// become a reason to refuse the call: the rewrite is dropped and the tool
|
|
128
|
-
// runs with its original arguments.
|
|
129
|
-
return `the tool's schema could not be checked: ${error instanceof Error ? error.message : String(error)}`;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* Read and validate the config. Never throws: a broken config must leave the
|
|
134
|
-
* session working, with a diagnostic saying what was wrong.
|
|
135
|
-
*/
|
|
136
|
-
export function loadHooks(path = HOOKS_FILE) {
|
|
137
|
-
const diagnostics = [];
|
|
138
|
-
const empty = { hooks: {} };
|
|
139
|
-
try {
|
|
140
|
-
if (!existsSync(path))
|
|
141
|
-
return { config: empty, diagnostics };
|
|
142
|
-
const raw = readFileSync(path, "utf8");
|
|
143
|
-
if (Buffer.byteLength(raw, "utf8") > MAX_BYTES) {
|
|
144
|
-
diagnostics.push({ source: path, message: `skipped: larger than ${MAX_BYTES} bytes` });
|
|
145
|
-
return { config: empty, diagnostics };
|
|
146
|
-
}
|
|
147
|
-
const parsed = JSON.parse(raw);
|
|
148
|
-
const result = validateHooks(parsed);
|
|
149
|
-
for (const message of result.diagnostics)
|
|
150
|
-
diagnostics.push({ source: path, message });
|
|
151
|
-
return { config: result.config, diagnostics };
|
|
152
|
-
}
|
|
153
|
-
catch (error) {
|
|
154
|
-
diagnostics.push({
|
|
155
|
-
source: path,
|
|
156
|
-
message: `unreadable: ${error instanceof Error ? error.message : String(error)}`,
|
|
157
|
-
});
|
|
158
|
-
return { config: empty, diagnostics };
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
/**
|
|
162
|
-
* Validate a parsed config. Pure, so the shape rules are testable without a
|
|
163
|
-
* session or a file — including the two that matter most: an unsupported event
|
|
164
|
-
* name is *reported*, and a malformed entry is dropped rather than taking the
|
|
165
|
-
* whole config with it.
|
|
166
|
-
*/
|
|
167
|
-
export function validateHooks(parsed) {
|
|
168
|
-
const diagnostics = [];
|
|
169
|
-
const config = { hooks: {} };
|
|
170
|
-
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
171
|
-
return { config, diagnostics: ["expected a JSON object with a `hooks` key"] };
|
|
172
|
-
}
|
|
173
|
-
const hooks = parsed.hooks;
|
|
174
|
-
if (hooks === undefined)
|
|
175
|
-
return { config, diagnostics: ["no `hooks` key"] };
|
|
176
|
-
if (hooks === null || typeof hooks !== "object" || Array.isArray(hooks)) {
|
|
177
|
-
return { config, diagnostics: ["`hooks` must be an object keyed by event name"] };
|
|
178
|
-
}
|
|
179
|
-
for (const [name, value] of Object.entries(hooks)) {
|
|
180
|
-
if (!SUPPORTED_EVENTS.includes(name)) {
|
|
181
|
-
diagnostics.push(`unknown event \`${name}\` — supported: ${SUPPORTED_EVENTS.join(", ")}`);
|
|
182
|
-
continue;
|
|
183
|
-
}
|
|
184
|
-
if (!Array.isArray(value)) {
|
|
185
|
-
diagnostics.push(`\`${name}\` must be an array of matchers`);
|
|
186
|
-
continue;
|
|
187
|
-
}
|
|
188
|
-
const matchers = [];
|
|
189
|
-
for (const entry of value) {
|
|
190
|
-
const matcher = validateMatcher(entry, name, diagnostics);
|
|
191
|
-
if (matcher)
|
|
192
|
-
matchers.push(matcher);
|
|
193
|
-
}
|
|
194
|
-
if (matchers.length)
|
|
195
|
-
config.hooks[name] = matchers;
|
|
196
|
-
}
|
|
197
|
-
return { config, diagnostics };
|
|
198
|
-
}
|
|
199
|
-
function validateMatcher(entry, event, diagnostics) {
|
|
200
|
-
if (entry === null || typeof entry !== "object" || Array.isArray(entry)) {
|
|
201
|
-
diagnostics.push(`\`${event}\`: a matcher must be an object`);
|
|
202
|
-
return undefined;
|
|
203
|
-
}
|
|
204
|
-
const record = entry;
|
|
205
|
-
const commands = Array.isArray(record.hooks) ? record.hooks : undefined;
|
|
206
|
-
if (!commands) {
|
|
207
|
-
diagnostics.push(`\`${event}\`: a matcher needs a \`hooks\` array`);
|
|
208
|
-
return undefined;
|
|
209
|
-
}
|
|
210
|
-
const hooks = [];
|
|
211
|
-
for (const command of commands) {
|
|
212
|
-
if (command === null || typeof command !== "object")
|
|
213
|
-
continue;
|
|
214
|
-
const c = command;
|
|
215
|
-
const type = typeof c.type === "string" ? c.type : "command";
|
|
216
|
-
if (type !== "command") {
|
|
217
|
-
diagnostics.push(`\`${event}\`: only \`type: "command"\` is supported, got \`${type}\``);
|
|
218
|
-
continue;
|
|
219
|
-
}
|
|
220
|
-
if (typeof c.command !== "string" || !c.command.trim()) {
|
|
221
|
-
diagnostics.push(`\`${event}\`: a hook needs a non-empty \`command\``);
|
|
222
|
-
continue;
|
|
223
|
-
}
|
|
224
|
-
const timeout = typeof c.timeout === "number" && Number.isFinite(c.timeout) && c.timeout > 0 ? c.timeout : undefined;
|
|
225
|
-
hooks.push({
|
|
226
|
-
type: "command",
|
|
227
|
-
command: c.command,
|
|
228
|
-
timeout,
|
|
229
|
-
...(c.async === true ? { async: true } : {}),
|
|
230
|
-
...(c.once === true ? { once: true } : {}),
|
|
231
|
-
});
|
|
232
|
-
}
|
|
233
|
-
if (!hooks.length)
|
|
234
|
-
return undefined;
|
|
235
|
-
const matcher = typeof record.matcher === "string" ? record.matcher : undefined;
|
|
236
|
-
return { matcher, hooks };
|
|
237
|
-
}
|
|
238
|
-
/**
|
|
239
|
-
* Which matchers fire for a tool. `undefined`, `""`, `*` and a `|`-separated
|
|
240
|
-
* list all work, because that is how Claude Code's `matcher` reads and a user
|
|
241
|
-
* arriving from there will write it that way. Case-insensitive: tool names are
|
|
242
|
-
* written `bash` here and `Bash` there.
|
|
243
|
-
*/
|
|
244
|
-
export function matchersFor(config, event, toolName) {
|
|
245
|
-
const matchers = config.hooks[event];
|
|
246
|
-
if (!matchers)
|
|
247
|
-
return [];
|
|
248
|
-
return matchers.filter((matcher) => {
|
|
249
|
-
const pattern = matcher.matcher?.trim();
|
|
250
|
-
if (!pattern || pattern === "*")
|
|
251
|
-
return true;
|
|
252
|
-
if (toolName === undefined)
|
|
253
|
-
return false;
|
|
254
|
-
const name = toolName.toLowerCase();
|
|
255
|
-
return pattern
|
|
256
|
-
.split("|")
|
|
257
|
-
.map((part) => part.trim().toLowerCase())
|
|
258
|
-
.filter(Boolean)
|
|
259
|
-
.includes(name);
|
|
260
|
-
});
|
|
261
|
-
}
|
|
262
|
-
/**
|
|
263
|
-
* Run one hook. Async and bounded: a hook that hangs is killed and reported, so
|
|
264
|
-
* a bad command cannot freeze the session.
|
|
265
|
-
*/
|
|
266
|
-
export function runHook(command, payload, options = { cwd: process.cwd() }) {
|
|
267
|
-
const timeoutS = options.timeoutS ?? DEFAULT_TIMEOUT_S;
|
|
268
|
-
return new Promise((resolve) => {
|
|
269
|
-
let stdout = "";
|
|
270
|
-
let stderr = "";
|
|
271
|
-
let timedOut = false;
|
|
272
|
-
let settled = false;
|
|
273
|
-
const child = spawn(command, {
|
|
274
|
-
cwd: options.cwd,
|
|
275
|
-
shell: true,
|
|
276
|
-
stdio: ["pipe", "pipe", "pipe"],
|
|
277
|
-
});
|
|
278
|
-
// A hook that exits without reading stdin closes the pipe under us, and the
|
|
279
|
-
// write then fails **asynchronously** — an unhandled `error` event on the
|
|
280
|
-
// socket, which takes the whole process down. `exit 0` and `exit 2` are both
|
|
281
|
-
// normal hooks and both do this, so the handler is not optional. (The
|
|
282
|
-
// try/catch around the `end()` call below does not catch it: by then the
|
|
283
|
-
// write has been queued.)
|
|
284
|
-
child.stdin?.on("error", () => { });
|
|
285
|
-
child.stdout?.on("error", () => { });
|
|
286
|
-
child.stderr?.on("error", () => { });
|
|
287
|
-
const timer = setTimeout(() => {
|
|
288
|
-
timedOut = true;
|
|
289
|
-
child.kill("SIGKILL");
|
|
290
|
-
}, timeoutS * 1000);
|
|
291
|
-
child.stdout?.on("data", (chunk) => {
|
|
292
|
-
stdout += chunk.toString("utf8");
|
|
293
|
-
});
|
|
294
|
-
child.stderr?.on("data", (chunk) => {
|
|
295
|
-
stderr += chunk.toString("utf8");
|
|
296
|
-
});
|
|
297
|
-
const finish = (code) => {
|
|
298
|
-
if (settled)
|
|
299
|
-
return;
|
|
300
|
-
settled = true;
|
|
301
|
-
clearTimeout(timer);
|
|
302
|
-
resolve({ code, stdout, stderr, timedOut });
|
|
303
|
-
};
|
|
304
|
-
child.on("error", (error) => {
|
|
305
|
-
stderr += error.message;
|
|
306
|
-
finish(null);
|
|
307
|
-
});
|
|
308
|
-
child.on("close", (code) => finish(code));
|
|
309
|
-
try {
|
|
310
|
-
child.stdin?.end(JSON.stringify(payload));
|
|
311
|
-
}
|
|
312
|
-
catch {
|
|
313
|
-
// A hook that closes stdin early is not an error; the exit code decides.
|
|
314
|
-
}
|
|
315
|
-
});
|
|
316
|
-
}
|
|
317
|
-
/**
|
|
318
|
-
* Read a hook's answer. Pure, so every branch is testable without spawning.
|
|
319
|
-
*
|
|
320
|
-
* The precedence is the point: an explicit `permissionDecision` beats the exit
|
|
321
|
-
* code, and `exit 2` beats stdout, because a hook that wrote a reason to stderr
|
|
322
|
-
* and exited 2 means it — that is the convention.
|
|
323
|
-
*/
|
|
324
|
-
export function interpretHookResult(result) {
|
|
325
|
-
const { code, stdout, stderr, timedOut } = result;
|
|
326
|
-
const trimmedErr = stderr.trim();
|
|
327
|
-
if (timedOut)
|
|
328
|
-
return { block: false, error: `timed out after ${DEFAULT_TIMEOUT_S}s and was killed` };
|
|
329
|
-
const parsed = parseJsonObject(stdout);
|
|
330
|
-
// `additionalContext` is the field Claude Code uses for "here is more context";
|
|
331
|
-
// it is honoured whatever the exit code, because a hook can both add context
|
|
332
|
-
// and refuse.
|
|
333
|
-
const specific = parsed?.hookSpecificOutput;
|
|
334
|
-
const context = typeof specific?.additionalContext === "string" ? specific.additionalContext : typeof parsed?.additionalContext === "string" ? parsed.additionalContext : undefined;
|
|
335
|
-
const decision = typeof specific?.permissionDecision === "string" ? specific.permissionDecision : undefined;
|
|
336
|
-
const decisionReason = typeof specific?.permissionDecisionReason === "string" ? specific.permissionDecisionReason : undefined;
|
|
337
|
-
// `updatedInput` replaces what the tool will actually do. It is read here and
|
|
338
|
-
// validated by the caller, which is the only place that has the tool's schema.
|
|
339
|
-
const rewritten = specific?.updatedInput;
|
|
340
|
-
const updatedInput = rewritten !== null && typeof rewritten === "object" && !Array.isArray(rewritten) ? rewritten : undefined;
|
|
341
|
-
if (decision === "deny" || decision === "block") {
|
|
342
|
-
return { block: true, reason: decisionReason || trimmedErr || "denied by a hook", context };
|
|
343
|
-
}
|
|
344
|
-
if (decision === "allow")
|
|
345
|
-
return { block: false, context, updatedInput };
|
|
346
|
-
if (code === 2)
|
|
347
|
-
return { block: true, reason: trimmedErr || "blocked by a hook", context };
|
|
348
|
-
if (code !== 0) {
|
|
349
|
-
return {
|
|
350
|
-
block: false,
|
|
351
|
-
context,
|
|
352
|
-
updatedInput,
|
|
353
|
-
error: `exited ${code === null ? "without a status" : code}${trimmedErr ? `: ${trimmedErr}` : ""}`,
|
|
354
|
-
};
|
|
355
|
-
}
|
|
356
|
-
// Exit 0 with plain text is the simple form: the text is the context.
|
|
357
|
-
if (!parsed && stdout.trim())
|
|
358
|
-
return { block: false, context: stdout.trim() };
|
|
359
|
-
return { block: false, context, updatedInput };
|
|
360
|
-
}
|
|
361
|
-
function parseJsonObject(text) {
|
|
362
|
-
const trimmed = text.trim();
|
|
363
|
-
if (!trimmed.startsWith("{"))
|
|
364
|
-
return undefined;
|
|
365
|
-
try {
|
|
366
|
-
const value = JSON.parse(trimmed);
|
|
367
|
-
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
368
|
-
return value;
|
|
369
|
-
}
|
|
370
|
-
return undefined;
|
|
371
|
-
}
|
|
372
|
-
catch {
|
|
373
|
-
return undefined;
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
/** Commands already run, for `once`. Session-scoped by design. */
|
|
377
|
-
const firedOnce = new Set();
|
|
378
|
-
/** Reset the `once` ledger. Called when a session starts, so a resumed session re-arms. */
|
|
379
|
-
export function resetOnceHooks() {
|
|
380
|
-
firedOnce.clear();
|
|
381
|
-
}
|
|
382
|
-
/**
|
|
383
|
-
* Run every matcher for an event and combine the outcomes. A block wins over
|
|
384
|
-
* context and over a rewrite: refusing is the stronger statement, and a hook that
|
|
385
|
-
* said no meant it. Contexts from every hook are kept, in order, so two hooks can
|
|
386
|
-
* each add a piece. A rewrite is last-writer-wins, because two hooks rewriting the
|
|
387
|
-
* same call cannot both be right.
|
|
388
|
-
*
|
|
389
|
-
* `async: true` and `once: true` are handled here rather than at each call site,
|
|
390
|
-
* so no event can forget them. An async hook is started and **not awaited**: it
|
|
391
|
-
* cannot contribute a decision, only a side effect.
|
|
392
|
-
*/
|
|
393
|
-
export async function runHooksFor(config, event, payload, options) {
|
|
394
|
-
const matchers = matchersFor(config, event, payload.tool_name);
|
|
395
|
-
if (!matchers.length)
|
|
396
|
-
return { outcome: { block: false }, ran: 0 };
|
|
397
|
-
const contexts = [];
|
|
398
|
-
let ran = 0;
|
|
399
|
-
let block;
|
|
400
|
-
let error;
|
|
401
|
-
let updatedInput;
|
|
402
|
-
for (const matcher of matchers) {
|
|
403
|
-
for (const hook of matcher.hooks) {
|
|
404
|
-
if (hook.once) {
|
|
405
|
-
const key = `${event}\u0000${hook.command}`;
|
|
406
|
-
if (firedOnce.has(key))
|
|
407
|
-
continue;
|
|
408
|
-
firedOnce.add(key);
|
|
409
|
-
}
|
|
410
|
-
ran += 1;
|
|
411
|
-
if (hook.async) {
|
|
412
|
-
// Started, never awaited. A rejection is swallowed on purpose: an async
|
|
413
|
-
// hook has no channel to report on, and an unhandled rejection would take
|
|
414
|
-
// the process down for a log line.
|
|
415
|
-
void runHook(hook.command, payload, { cwd: options.cwd, timeoutS: hook.timeout }).catch(() => { });
|
|
416
|
-
continue;
|
|
417
|
-
}
|
|
418
|
-
const result = await runHook(hook.command, payload, {
|
|
419
|
-
cwd: options.cwd,
|
|
420
|
-
timeoutS: hook.timeout,
|
|
421
|
-
});
|
|
422
|
-
const outcome = interpretHookResult(result);
|
|
423
|
-
if (outcome.context)
|
|
424
|
-
contexts.push(outcome.context);
|
|
425
|
-
if (outcome.error)
|
|
426
|
-
error = outcome.error;
|
|
427
|
-
if (outcome.updatedInput)
|
|
428
|
-
updatedInput = outcome.updatedInput;
|
|
429
|
-
if (outcome.block && !block)
|
|
430
|
-
block = outcome;
|
|
431
|
-
}
|
|
432
|
-
}
|
|
433
|
-
if (block) {
|
|
434
|
-
return {
|
|
435
|
-
outcome: { block: true, reason: block.reason, context: contexts.join("\n\n") || undefined },
|
|
436
|
-
ran,
|
|
437
|
-
};
|
|
438
|
-
}
|
|
439
|
-
return {
|
|
440
|
-
outcome: { block: false, context: contexts.join("\n\n") || undefined, error, updatedInput },
|
|
441
|
-
ran,
|
|
442
|
-
};
|
|
443
|
-
}
|