@descent-vtt/spec-harness 0.18.0 → 0.19.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/dist/hooks.d.ts CHANGED
@@ -40,21 +40,87 @@ export interface HookRequest {
40
40
  readonly cwd: string | null;
41
41
  /** Files the tool writes, as the tool named them - absolute, usually. */
42
42
  readonly paths: readonly string[];
43
+ /** The session and the tool call the question is about, when it says: `session_id` and `tool_use_id`. */
44
+ readonly session: string | null;
45
+ readonly call: string | null;
43
46
  }
47
+ /**
48
+ * The event Claude Code runs as a session starts, resumes, is cleared or
49
+ * compacted, or is forked: no tool is named, and the hook checks what every
50
+ * write of the session will need (ADR-0005).
51
+ */
52
+ export declare const SESSION_START = "SessionStart";
44
53
  /** Reads a Claude Code hook's stdin, or says why it cannot. */
45
54
  export declare function parseClaudeHook(raw: string): HookRequest | {
46
55
  readonly error: string;
47
56
  };
57
+ /**
58
+ * The answer as a session starts: what would hold every write of it, in
59
+ * one line, for the person as `systemMessage` and for Claude as
60
+ * `additionalContext`; or nothing, where all is well. Always exit 0: the
61
+ * event cannot block, and a check that fails must not keep a session from
62
+ * starting.
63
+ */
64
+ export declare function sessionResponse(problem: string | null): HookResponse;
48
65
  export interface HookResponse {
49
66
  /** What to print on stdout: JSON, or nothing. */
50
67
  readonly stdout: string;
51
68
  readonly exitCode: number;
52
69
  }
70
+ /** What a warning after a write says: a decision's message and its next step. */
71
+ export type Warning = Pick<Decision, 'message' | 'hint'>;
72
+ /** The answer after a write: the warnings, as additional context for the model, or nothing. */
73
+ export declare function afterResponse(warnings: readonly Warning[]): HookResponse;
53
74
  /**
54
75
  * The answer to a Claude Code hook: a refusal or a question before a write,
55
76
  * a warning after one, or nothing.
56
77
  */
57
78
  export declare function claudeResponse(event: string, decisions: readonly Decision[]): HookResponse;
79
+ /** The decisions a run after the write would say: those that warn, each by its message and next step. */
80
+ export declare function warningsOf(decisions: readonly Decision[]): Warning[];
81
+ /**
82
+ * What the hook reads besides its question and its line, as it decides: the
83
+ * project Claude Code names, the brief the environment names, and the
84
+ * branch a forge's CI names on a detached head (ADR-0004).
85
+ */
86
+ export declare const CALL_VARIABLES: readonly string[];
87
+ /**
88
+ * A tool call, as both of its hooks see it: the release, the session and
89
+ * the call, the tool and its paths, the session's directory, what the
90
+ * hook's line gives, the directory the hook was started in, and the
91
+ * variables it reads (ADR-0002). What a run before the write keeps is
92
+ * answered after it only to a run for which all of it is the same.
93
+ */
94
+ export interface CallRecord {
95
+ readonly release: string;
96
+ readonly session: string;
97
+ readonly call: string;
98
+ readonly tool: string;
99
+ readonly paths: readonly string[];
100
+ readonly cwd: string | null;
101
+ readonly line: readonly (string | null)[];
102
+ readonly started: string;
103
+ readonly env: readonly (string | null)[];
104
+ }
105
+ /**
106
+ * The call a question is about, or `null` where it does not say which
107
+ * session and call it is, or says it in a form no file is named by.
108
+ */
109
+ export declare function callRecord(request: HookRequest, line: Readonly<{
110
+ root: string | undefined;
111
+ brief: string | undefined;
112
+ base: string | undefined;
113
+ }>, started: string, env: Readonly<Record<string, string | undefined>>, release: string): CallRecord | null;
114
+ /** The name of the file a call's decisions are kept under. */
115
+ export declare function callFile(record: CallRecord): string;
116
+ /** What a run before the write keeps for the run after it: the call, and the warnings it decided. */
117
+ export declare function keptCall(record: CallRecord, warnings: readonly Warning[]): string;
118
+ /**
119
+ * The warnings kept for this call, or `null` where what was kept is not
120
+ * JSON, is another call's, or holds anything but warnings: such a call is
121
+ * decided as it was before anything was kept (ADR-0002).
122
+ */
123
+ export declare function warningsKept(text: string, record: CallRecord): Warning[] | null;
58
124
  /**
59
125
  * The answer to git's pre-commit hook: every file the commit would change
60
126
  * that the round may not, and exit 1 if any is refused. A warning prints and
package/dist/hooks.js CHANGED
@@ -36,6 +36,12 @@ const PATH_FIELDS = ['file_path', 'path', 'notebook_path'];
36
36
  function isObject(value) {
37
37
  return typeof value === 'object' && value !== null && !Array.isArray(value);
38
38
  }
39
+ /**
40
+ * The event Claude Code runs as a session starts, resumes, is cleared or
41
+ * compacted, or is forked: no tool is named, and the hook checks what every
42
+ * write of the session will need (ADR-0005).
43
+ */
44
+ export const SESSION_START = 'SessionStart';
39
45
  /** Reads a Claude Code hook's stdin, or says why it cannot. */
40
46
  export function parseClaudeHook(raw) {
41
47
  let value;
@@ -49,24 +55,53 @@ export function parseClaudeHook(raw) {
49
55
  return { error: 'the hook input is not a JSON object' };
50
56
  const event = value['hook_event_name'];
51
57
  const tool = value['tool_name'];
52
- if (typeof event !== 'string' || typeof tool !== 'string')
58
+ if (typeof event !== 'string' || (typeof tool !== 'string' && event !== SESSION_START))
53
59
  return { error: 'the hook input names no event or tool' };
60
+ // A session's start names no tool, and is read as naming none.
61
+ const named = typeof tool === 'string' ? tool : '';
54
62
  const input = isObject(value['tool_input']) ? value['tool_input'] : {};
55
63
  const paths = [];
56
- if (WRITING_TOOLS.includes(tool)) {
64
+ if (WRITING_TOOLS.includes(named)) {
57
65
  for (const field of PATH_FIELDS) {
58
66
  const path = input[field];
59
67
  if (typeof path === 'string' && path !== '' && !paths.includes(path))
60
68
  paths.push(path);
61
69
  }
62
70
  }
63
- const cwd = value['cwd'];
64
- return { event, tool, cwd: typeof cwd === 'string' ? cwd : null, paths };
71
+ const text = (field) => {
72
+ const found = value[field];
73
+ return typeof found === 'string' ? found : null;
74
+ };
75
+ return { event, tool: named, cwd: text('cwd'), paths, session: text('session_id'), call: text('tool_use_id') };
76
+ }
77
+ /**
78
+ * The answer as a session starts: what would hold every write of it, in
79
+ * one line, for the person as `systemMessage` and for Claude as
80
+ * `additionalContext`; or nothing, where all is well. Always exit 0: the
81
+ * event cannot block, and a check that fails must not keep a session from
82
+ * starting.
83
+ */
84
+ export function sessionResponse(problem) {
85
+ if (problem === null)
86
+ return NONE;
87
+ const said = `spec-harness: ${problem}`;
88
+ return { stdout: `${JSON.stringify({ systemMessage: said, hookSpecificOutput: { hookEventName: SESSION_START, additionalContext: said } })}\n`, exitCode: 0 };
65
89
  }
66
90
  const NONE = { stdout: '', exitCode: 0 };
67
91
  function reasonOf(decisions) {
68
92
  return decisions.map((decision) => `spec-harness: ${decision.message}. Next: ${decision.hint}.`).join('\n');
69
93
  }
94
+ /** The answer after a write: the warnings, as additional context for the model, or nothing. */
95
+ export function afterResponse(warnings) {
96
+ if (warnings.length === 0)
97
+ return NONE;
98
+ return {
99
+ stdout: `${JSON.stringify({
100
+ hookSpecificOutput: { hookEventName: 'PostToolUse', additionalContext: reasonOf(warnings) },
101
+ })}\n`,
102
+ exitCode: 0,
103
+ };
104
+ }
70
105
  /**
71
106
  * The answer to a Claude Code hook: a refusal or a question before a write,
72
107
  * a warning after one, or nothing.
@@ -89,19 +124,81 @@ export function claudeResponse(event, decisions) {
89
124
  exitCode: 0,
90
125
  };
91
126
  }
92
- if (event === 'PostToolUse') {
93
- const warned = decisions.filter((decision) => decision.verdict === 'warn');
94
- if (warned.length === 0)
95
- return NONE;
96
- return {
97
- stdout: `${JSON.stringify({
98
- hookSpecificOutput: { hookEventName: 'PostToolUse', additionalContext: reasonOf(warned) },
99
- })}\n`,
100
- exitCode: 0,
101
- };
102
- }
127
+ if (event === 'PostToolUse')
128
+ return afterResponse(warningsOf(decisions));
103
129
  return NONE;
104
130
  }
131
+ /** The decisions a run after the write would say: those that warn, each by its message and next step. */
132
+ export function warningsOf(decisions) {
133
+ return decisions.filter((decision) => decision.verdict === 'warn').map(({ message, hint }) => ({ message, hint }));
134
+ }
135
+ /**
136
+ * What the hook reads besides its question and its line, as it decides: the
137
+ * project Claude Code names, the brief the environment names, and the
138
+ * branch a forge's CI names on a detached head (ADR-0004).
139
+ */
140
+ export const CALL_VARIABLES = [
141
+ 'CLAUDE_PROJECT_DIR',
142
+ 'SPEC_BRIEF',
143
+ 'GITHUB_HEAD_REF',
144
+ 'GITHUB_REF_TYPE',
145
+ 'GITHUB_REF_NAME',
146
+ 'CI_MERGE_REQUEST_SOURCE_BRANCH_NAME',
147
+ 'CI_COMMIT_BRANCH',
148
+ ];
149
+ /** An id the hook names a file by: what Claude Code's ids are made of, and nothing a path could climb out by. */
150
+ const ID = /^[\w-]{1,256}$/;
151
+ /**
152
+ * The call a question is about, or `null` where it does not say which
153
+ * session and call it is, or says it in a form no file is named by.
154
+ */
155
+ export function callRecord(request, line, started, env, release) {
156
+ const { session, call } = request;
157
+ if (session === null || call === null || !ID.test(session) || !ID.test(call))
158
+ return null;
159
+ return {
160
+ release,
161
+ session,
162
+ call,
163
+ tool: request.tool,
164
+ paths: request.paths,
165
+ cwd: request.cwd,
166
+ line: [line.root ?? null, line.brief ?? null, line.base ?? null],
167
+ started,
168
+ env: CALL_VARIABLES.map((name) => env[name] ?? null),
169
+ };
170
+ }
171
+ /** The name of the file a call's decisions are kept under. */
172
+ export function callFile(record) {
173
+ return `${record.session}.${record.call}.json`;
174
+ }
175
+ /** What a run before the write keeps for the run after it: the call, and the warnings it decided. */
176
+ export function keptCall(record, warnings) {
177
+ return `${JSON.stringify({ record, warnings })}\n`;
178
+ }
179
+ function isWarning(value) {
180
+ return isObject(value) && typeof value['message'] === 'string' && typeof value['hint'] === 'string';
181
+ }
182
+ /**
183
+ * The warnings kept for this call, or `null` where what was kept is not
184
+ * JSON, is another call's, or holds anything but warnings: such a call is
185
+ * decided as it was before anything was kept (ADR-0002).
186
+ */
187
+ export function warningsKept(text, record) {
188
+ let value;
189
+ try {
190
+ value = JSON.parse(text);
191
+ }
192
+ catch {
193
+ // Read as an empty object, a text that is not JSON holds no record
194
+ // either: that mutant is equivalent.
195
+ return null;
196
+ }
197
+ if (!isObject(value) || JSON.stringify(value['record']) !== JSON.stringify(record))
198
+ return null;
199
+ const warnings = value['warnings'];
200
+ return Array.isArray(warnings) && warnings.every(isWarning) ? warnings.map(({ message, hint }) => ({ message, hint })) : null;
201
+ }
105
202
  /**
106
203
  * The answer to git's pre-commit hook: every file the commit would change
107
204
  * that the round may not, and exit 1 if any is refused. A warning prints and
package/dist/hooks.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"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,IAAI,OAAO,GAAG,CAAC,CAAC;IAChB,KAAK,MAAM,QAAQ,IAAI,SAAS,EAAE,CAAC;QACjC,IAAI,QAAQ,CAAC,OAAO,KAAK,OAAO;YAAE,SAAS;QAC3C,MAAM,QAAQ,GAAG,QAAQ,CAAC,OAAO,KAAK,MAAM,IAAI,QAAQ,CAAC,OAAO,KAAK,KAAK,CAAC;QAC3E,IAAI,QAAQ;YAAE,OAAO,IAAI,CAAC,CAAC;QAC3B,KAAK,CAAC,IAAI,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,SAAS,KAAK,QAAQ,CAAC,OAAO,EAAE,EAAE,YAAY,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC;IACtG,CAAC;IACD,IAAI,OAAO,GAAG,CAAC;QAAE,KAAK,CAAC,IAAI,CAAC,iBAAiB,OAAO,4DAA4D,CAAC,CAAC;IAClH,OAAO,EAAE,IAAI,EAAE,KAAK,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,QAAQ,EAAE,OAAO,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;AACpG,CAAC","sourcesContent":["/**\n * The guard, spoken in the languages that ask it: an agent's pre- and\n * post-tool hooks, and git's pre-commit.\n *\n * Claude Code sends a hook one JSON object on stdin naming the tool and its\n * input. A file-editing tool names its file under `file_path`, `path` or\n * `notebook_path` depending on the tool and the release; all three are read,\n * because a guard that missed a renamed field would allow everything.\n *\n * What the answer may not do matters as much as what it does. Returning\n * `allow` from a pre-tool hook skips the person's own permission prompt, so\n * the guard never says `allow`: it says `deny`, or `ask`, or nothing. A\n * warning - a write outside the scope when the repository asked only to be\n * warned - is told to the model after the write, through the post-tool hook's\n * additional context, where it cannot unlock anything.\n */\n\nimport type { Decision } from './guard.js';\n\n/**\n * The tools whose input names a file they write, by every name a release of\n * Claude Code has had for one. The hook reads a path from each: a name more\n * costs nothing, and a Claude Code that still has the tool may send it.\n */\nexport const WRITING_TOOLS: readonly string[] = ['Edit', 'Write', 'MultiEdit', 'NotebookEdit'];\n\n/**\n * Those of them a Claude Code the harness runs under can call\n * (`CLAUDE_CODE_MINIMUM`, versions.ts). Its tools reference\n * (https://code.claude.com/docs/en/tools-reference, read 2026-10-12) has\n * `Edit`, `Write` and `NotebookEdit` in its table of tools, and no\n * `MultiEdit`, which its permissions page calls a legacy tool; and the tool\n * inputs each release declares hold a `MultiEdit` up to 1.0.128 and none\n * from 2.0.0 on (ADR-0005). So what doctor and init say of the writes a\n * line is started for names these alone: a tool nobody can call leaves no\n * write unchecked.\n */\nexport const CALLABLE_TOOLS: readonly string[] = ['Edit', 'Write', 'NotebookEdit'];\n\nconst PATH_FIELDS: readonly string[] = ['file_path', 'path', 'notebook_path'];\n\nexport interface HookRequest {\n readonly event: string;\n readonly tool: string;\n /** The session's working directory, when the hook says. */\n readonly cwd: string | null;\n /** Files the tool writes, as the tool named them - absolute, usually. */\n readonly paths: readonly string[];\n}\n\ntype Json = Record<string, unknown>;\n\nfunction isObject(value: unknown): value is Json {\n return typeof value === 'object' && value !== null && !Array.isArray(value);\n}\n\n/** Reads a Claude Code hook's stdin, or says why it cannot. */\nexport function parseClaudeHook(raw: string): HookRequest | { readonly error: string } {\n let value: unknown;\n try {\n value = JSON.parse(raw);\n } catch {\n return { error: 'the hook input is not JSON' };\n }\n if (!isObject(value)) return { error: 'the hook input is not a JSON object' };\n const event = value['hook_event_name'];\n const tool = value['tool_name'];\n if (typeof event !== 'string' || typeof tool !== 'string') return { error: 'the hook input names no event or tool' };\n const input = isObject(value['tool_input']) ? value['tool_input'] : {};\n const paths: string[] = [];\n if (WRITING_TOOLS.includes(tool)) {\n for (const field of PATH_FIELDS) {\n const path = input[field];\n if (typeof path === 'string' && path !== '' && !paths.includes(path)) paths.push(path);\n }\n }\n const cwd = value['cwd'];\n return { event, tool, cwd: typeof cwd === 'string' ? cwd : null, paths };\n}\n\nexport interface HookResponse {\n /** What to print on stdout: JSON, or nothing. */\n readonly stdout: string;\n readonly exitCode: number;\n}\n\nconst NONE: HookResponse = { stdout: '', exitCode: 0 };\n\nfunction reasonOf(decisions: readonly Decision[]): string {\n return decisions.map((decision) => `spec-harness: ${decision.message}. Next: ${decision.hint}.`).join('\\n');\n}\n\n/**\n * The answer to a Claude Code hook: a refusal or a question before a write,\n * a warning after one, or nothing.\n */\nexport function claudeResponse(event: string, decisions: readonly Decision[]): HookResponse {\n if (event === 'PreToolUse') {\n const denied = decisions.filter((decision) => decision.verdict === 'deny');\n const asked = decisions.filter((decision) => decision.verdict === 'ask');\n const decided = denied.length > 0 ? { verdict: 'deny', list: denied } : asked.length > 0 ? { verdict: 'ask', list: asked } : null;\n if (decided === null) return NONE;\n return {\n stdout: `${JSON.stringify({\n hookSpecificOutput: {\n hookEventName: 'PreToolUse',\n permissionDecision: decided.verdict,\n permissionDecisionReason: reasonOf(decided.list),\n },\n })}\\n`,\n exitCode: 0,\n };\n }\n if (event === 'PostToolUse') {\n const warned = decisions.filter((decision) => decision.verdict === 'warn');\n if (warned.length === 0) return NONE;\n return {\n stdout: `${JSON.stringify({\n hookSpecificOutput: { hookEventName: 'PostToolUse', additionalContext: reasonOf(warned) },\n })}\\n`,\n exitCode: 0,\n };\n }\n return NONE;\n}\n\n/**\n * The answer to git's pre-commit hook: every file the commit would change\n * that the round may not, and exit 1 if any is refused. A warning prints and\n * lets the commit through; a question has no one to ask in git, so it refuses.\n */\nexport function gitResponse(decisions: readonly Decision[]): { readonly text: string; readonly exitCode: number } {\n const lines: string[] = [];\n let refused = 0;\n for (const decision of decisions) {\n if (decision.verdict === 'allow') continue;\n const blocking = decision.verdict === 'deny' || decision.verdict === 'ask';\n if (blocking) refused += 1;\n lines.push(`${blocking ? 'refused' : 'warning'} ${decision.message}`, ` ${decision.hint}`);\n }\n if (refused > 0) lines.push(`spec-harness: ${refused} file(s) this round may not change; the commit was stopped`);\n return { text: lines.length === 0 ? '' : `${lines.join('\\n')}\\n`, exitCode: refused > 0 ? 1 : 0 };\n}\n"]}
1
+ {"version":3,"file":"hooks.js","sourceRoot":"","sources":["../src/hooks.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAIH;;;;GAIG;AACH,MAAM,CAAC,MAAM,aAAa,GAAsB,CAAC,MAAM,EAAE,OAAO,EAAE,WAAW,EAAE,cAAc,CAAC,CAAC;AAE/F;;;;;;;;;;GAUG;AACH,MAAM,CAAC,MAAM,cAAc,GAAsB,CAAC,MAAM,EAAE,OAAO,EAAE,cAAc,CAAC,CAAC;AAEnF,MAAM,WAAW,GAAsB,CAAC,WAAW,EAAE,MAAM,EAAE,eAAe,CAAC,CAAC;AAgB9E,SAAS,QAAQ,CAAC,KAAc;IAC9B,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC9E,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,cAAc,CAAC;AAE5C,+DAA+D;AAC/D,MAAM,UAAU,eAAe,CAAC,GAAW;IACzC,IAAI,KAAc,CAAC;IACnB,IAAI,CAAC;QACH,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAC1B,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,EAAE,KAAK,EAAE,4BAA4B,EAAE,CAAC;IACjD,CAAC;IACD,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC;QAAE,OAAO,EAAE,KAAK,EAAE,qCAAqC,EAAE,CAAC;IAC9E,MAAM,KAAK,GAAG,KAAK,CAAC,iBAAiB,CAAC,CAAC;IACvC,MAAM,IAAI,GAAG,KAAK,CAAC,WAAW,CAAC,CAAC;IAChC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CA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;QAC3E,IAAI,QAAQ;YAAE,OAAO,IAAI,CAAC,CAAC;QAC3B,KAAK,CAAC,IAAI,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,SAAS,KAAK,QAAQ,CAAC,OAAO,EAAE,EAAE,YAAY,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC;IACtG,CAAC;IACD,IAAI,OAAO,GAAG,CAAC;QAAE,KAAK,CAAC,IAAI,CAAC,iBAAiB,OAAO,4DAA4D,CAAC,CAAC;IAClH,OAAO,EAAE,IAAI,EAAE,KAAK,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,QAAQ,EAAE,OAAO,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;AACpG,CAAC","sourcesContent":["/**\n * The guard, spoken in the languages that ask it: an agent's pre- and\n * post-tool hooks, and git's pre-commit.\n *\n * Claude Code sends a hook one JSON object on stdin naming the tool and its\n * input. A file-editing tool names its file under `file_path`, `path` or\n * `notebook_path` depending on the tool and the release; all three are read,\n * because a guard that missed a renamed field would allow everything.\n *\n * What the answer may not do matters as much as what it does. Returning\n * `allow` from a pre-tool hook skips the person's own permission prompt, so\n * the guard never says `allow`: it says `deny`, or `ask`, or nothing. A\n * warning - a write outside the scope when the repository asked only to be\n * warned - is told to the model after the write, through the post-tool hook's\n * additional context, where it cannot unlock anything.\n */\n\nimport type { Decision } from './guard.js';\n\n/**\n * The tools whose input names a file they write, by every name a release of\n * Claude Code has had for one. The hook reads a path from each: a name more\n * costs nothing, and a Claude Code that still has the tool may send it.\n */\nexport const WRITING_TOOLS: readonly string[] = ['Edit', 'Write', 'MultiEdit', 'NotebookEdit'];\n\n/**\n * Those of them a Claude Code the harness runs under can call\n * (`CLAUDE_CODE_MINIMUM`, versions.ts). Its tools reference\n * (https://code.claude.com/docs/en/tools-reference, read 2026-10-12) has\n * `Edit`, `Write` and `NotebookEdit` in its table of tools, and no\n * `MultiEdit`, which its permissions page calls a legacy tool; and the tool\n * inputs each release declares hold a `MultiEdit` up to 1.0.128 and none\n * from 2.0.0 on (ADR-0005). So what doctor and init say of the writes a\n * line is started for names these alone: a tool nobody can call leaves no\n * write unchecked.\n */\nexport const CALLABLE_TOOLS: readonly string[] = ['Edit', 'Write', 'NotebookEdit'];\n\nconst PATH_FIELDS: readonly string[] = ['file_path', 'path', 'notebook_path'];\n\nexport interface HookRequest {\n readonly event: string;\n readonly tool: string;\n /** The session's working directory, when the hook says. */\n readonly cwd: string | null;\n /** Files the tool writes, as the tool named them - absolute, usually. */\n readonly paths: readonly string[];\n /** The session and the tool call the question is about, when it says: `session_id` and `tool_use_id`. */\n readonly session: string | null;\n readonly call: string | null;\n}\n\ntype Json = Record<string, unknown>;\n\nfunction isObject(value: unknown): value is Json {\n return typeof value === 'object' && value !== null && !Array.isArray(value);\n}\n\n/**\n * The event Claude Code runs as a session starts, resumes, is cleared or\n * compacted, or is forked: no tool is named, and the hook checks what every\n * write of the session will need (ADR-0005).\n */\nexport const SESSION_START = 'SessionStart';\n\n/** Reads a Claude Code hook's stdin, or says why it cannot. */\nexport function parseClaudeHook(raw: string): HookRequest | { readonly error: string } {\n let value: unknown;\n try {\n value = JSON.parse(raw);\n } catch {\n return { error: 'the hook input is not JSON' };\n }\n if (!isObject(value)) return { error: 'the hook input is not a JSON object' };\n const event = value['hook_event_name'];\n const tool = value['tool_name'];\n if (typeof event !== 'string' || (typeof tool !== 'string' && event !== SESSION_START)) return { error: 'the hook input names no event or tool' };\n // A session's start names no tool, and is read as naming none.\n const named = typeof tool === 'string' ? tool : '';\n const input = isObject(value['tool_input']) ? value['tool_input'] : {};\n const paths: string[] = [];\n if (WRITING_TOOLS.includes(named)) {\n for (const field of PATH_FIELDS) {\n const path = input[field];\n if (typeof path === 'string' && path !== '' && !paths.includes(path)) paths.push(path);\n }\n }\n const text = (field: string): string | null => {\n const found = value[field];\n return typeof found === 'string' ? found : null;\n };\n return { event, tool: named, cwd: text('cwd'), paths, session: text('session_id'), call: text('tool_use_id') };\n}\n\n/**\n * The answer as a session starts: what would hold every write of it, in\n * one line, for the person as `systemMessage` and for Claude as\n * `additionalContext`; or nothing, where all is well. Always exit 0: the\n * event cannot block, and a check that fails must not keep a session from\n * starting.\n */\nexport function sessionResponse(problem: string | null): HookResponse {\n if (problem === null) return NONE;\n const said = `spec-harness: ${problem}`;\n return { stdout: `${JSON.stringify({ systemMessage: said, hookSpecificOutput: { hookEventName: SESSION_START, additionalContext: said } })}\\n`, exitCode: 0 };\n}\n\nexport interface HookResponse {\n /** What to print on stdout: JSON, or nothing. */\n readonly stdout: string;\n readonly exitCode: number;\n}\n\nconst NONE: HookResponse = { stdout: '', exitCode: 0 };\n\nfunction reasonOf(decisions: readonly Warning[]): string {\n return decisions.map((decision) => `spec-harness: ${decision.message}. Next: ${decision.hint}.`).join('\\n');\n}\n\n/** What a warning after a write says: a decision's message and its next step. */\nexport type Warning = Pick<Decision, 'message' | 'hint'>;\n\n/** The answer after a write: the warnings, as additional context for the model, or nothing. */\nexport function afterResponse(warnings: readonly Warning[]): HookResponse {\n if (warnings.length === 0) return NONE;\n return {\n stdout: `${JSON.stringify({\n hookSpecificOutput: { hookEventName: 'PostToolUse', additionalContext: reasonOf(warnings) },\n })}\\n`,\n exitCode: 0,\n };\n}\n\n/**\n * The answer to a Claude Code hook: a refusal or a question before a write,\n * a warning after one, or nothing.\n */\nexport function claudeResponse(event: string, decisions: readonly Decision[]): HookResponse {\n if (event === 'PreToolUse') {\n const denied = decisions.filter((decision) => decision.verdict === 'deny');\n const asked = decisions.filter((decision) => decision.verdict === 'ask');\n const decided = denied.length > 0 ? { verdict: 'deny', list: denied } : asked.length > 0 ? { verdict: 'ask', list: asked } : null;\n if (decided === null) return NONE;\n return {\n stdout: `${JSON.stringify({\n hookSpecificOutput: {\n hookEventName: 'PreToolUse',\n permissionDecision: decided.verdict,\n permissionDecisionReason: reasonOf(decided.list),\n },\n })}\\n`,\n exitCode: 0,\n };\n }\n if (event === 'PostToolUse') return afterResponse(warningsOf(decisions));\n return NONE;\n}\n\n/** The decisions a run after the write would say: those that warn, each by its message and next step. */\nexport function warningsOf(decisions: readonly Decision[]): Warning[] {\n return decisions.filter((decision) => decision.verdict === 'warn').map(({ message, hint }) => ({ message, hint }));\n}\n\n/**\n * What the hook reads besides its question and its line, as it decides: the\n * project Claude Code names, the brief the environment names, and the\n * branch a forge's CI names on a detached head (ADR-0004).\n */\nexport const CALL_VARIABLES: readonly string[] = [\n 'CLAUDE_PROJECT_DIR',\n 'SPEC_BRIEF',\n 'GITHUB_HEAD_REF',\n 'GITHUB_REF_TYPE',\n 'GITHUB_REF_NAME',\n 'CI_MERGE_REQUEST_SOURCE_BRANCH_NAME',\n 'CI_COMMIT_BRANCH',\n];\n\n/**\n * A tool call, as both of its hooks see it: the release, the session and\n * the call, the tool and its paths, the session's directory, what the\n * hook's line gives, the directory the hook was started in, and the\n * variables it reads (ADR-0002). What a run before the write keeps is\n * answered after it only to a run for which all of it is the same.\n */\nexport interface CallRecord {\n readonly release: string;\n readonly session: string;\n readonly call: string;\n readonly tool: string;\n readonly paths: readonly string[];\n readonly cwd: string | null;\n readonly line: readonly (string | null)[];\n readonly started: string;\n readonly env: readonly (string | null)[];\n}\n\n/** An id the hook names a file by: what Claude Code's ids are made of, and nothing a path could climb out by. */\nconst ID = /^[\\w-]{1,256}$/;\n\n/**\n * The call a question is about, or `null` where it does not say which\n * session and call it is, or says it in a form no file is named by.\n */\nexport function callRecord(\n request: HookRequest,\n line: Readonly<{ root: string | undefined; brief: string | undefined; base: string | undefined }>,\n started: string,\n env: Readonly<Record<string, string | undefined>>,\n release: string,\n): CallRecord | null {\n const { session, call } = request;\n if (session === null || call === null || !ID.test(session) || !ID.test(call)) return null;\n return {\n release,\n session,\n call,\n tool: request.tool,\n paths: request.paths,\n cwd: request.cwd,\n line: [line.root ?? null, line.brief ?? null, line.base ?? null],\n started,\n env: CALL_VARIABLES.map((name) => env[name] ?? null),\n };\n}\n\n/** The name of the file a call's decisions are kept under. */\nexport function callFile(record: CallRecord): string {\n return `${record.session}.${record.call}.json`;\n}\n\n/** What a run before the write keeps for the run after it: the call, and the warnings it decided. */\nexport function keptCall(record: CallRecord, warnings: readonly Warning[]): string {\n return `${JSON.stringify({ record, warnings })}\\n`;\n}\n\nfunction isWarning(value: unknown): value is Warning {\n return isObject(value) && typeof value['message'] === 'string' && typeof value['hint'] === 'string';\n}\n\n/**\n * The warnings kept for this call, or `null` where what was kept is not\n * JSON, is another call's, or holds anything but warnings: such a call is\n * decided as it was before anything was kept (ADR-0002).\n */\nexport function warningsKept(text: string, record: CallRecord): Warning[] | null {\n let value: unknown;\n try {\n value = JSON.parse(text);\n } catch {\n // Read as an empty object, a text that is not JSON holds no record\n // either: that mutant is equivalent.\n return null;\n }\n if (!isObject(value) || JSON.stringify(value['record']) !== JSON.stringify(record)) return null;\n const warnings = value['warnings'];\n return Array.isArray(warnings) && warnings.every(isWarning) ? warnings.map(({ message, hint }) => ({ message, hint })) : null;\n}\n\n/**\n * The answer to git's pre-commit hook: every file the commit would change\n * that the round may not, and exit 1 if any is refused. A warning prints and\n * lets the commit through; a question has no one to ask in git, so it refuses.\n */\nexport function gitResponse(decisions: readonly Decision[]): { readonly text: string; readonly exitCode: number } {\n const lines: string[] = [];\n let refused = 0;\n for (const decision of decisions) {\n if (decision.verdict === 'allow') continue;\n const blocking = decision.verdict === 'deny' || decision.verdict === 'ask';\n if (blocking) refused += 1;\n lines.push(`${blocking ? 'refused' : 'warning'} ${decision.message}`, ` ${decision.hint}`);\n }\n if (refused > 0) lines.push(`spec-harness: ${refused} file(s) this round may not change; the commit was stopped`);\n return { text: lines.length === 0 ? '' : `${lines.join('\\n')}\\n`, exitCode: refused > 0 ? 1 : 0 };\n}\n"]}
package/dist/round.js CHANGED
@@ -17,7 +17,7 @@ import { fileURLToPath } from 'node:url';
17
17
  import { readArchivePlan, readGuardRun } from './answers.js';
18
18
  import { SiblingOutputError } from './briefs.js';
19
19
  import { sameId } from './branch.js';
20
- import { commasUnder, enabledPlugin, exportedFile, handedLines, hooksDisabled, HOSTED_REGISTRY, isStarted, loadsPlugin, MARKETPLACE_REGISTRY, patternReader, pluginMarketplace, pluginPresence, PROJECT_SETTINGS, registersServer, reportHooks, SPEC_BRIEF_CONFIGS, wiringState, } from './configure.js';
20
+ import { arrayMatcher, enabledPlugin, exportedFile, handedLines, hooksDisabled, HOSTED_REGISTRY, isStarted, loadsPlugin, MARKETPLACE_REGISTRY, matchingUnder, patternReader, pluginMarketplace, pluginPresence, PROJECT_SETTINGS, registersServer, reportHooks, SPEC_BRIEF_CONFIGS, startsSessions, wiringState, } from './configure.js';
21
21
  import { readRules, renderContext } from './context.js';
22
22
  import { isDirectory, readJsonObject, readText, repositoryPath, stateDirectory, writeAtomic } from './fs.js';
23
23
  import { blameLine, changes, commonDirectory, isUncommitted, mergeBase, remoteDefault, revision, show, verifyCommit, workTreeRoot } from './git.js';
@@ -447,13 +447,21 @@ export async function claudeCodeWiring(root, io, pattern = patternReader(), repo
447
447
  const roots = marketplace === null ? [] : await Promise.all(pluginDirectories(io.env).map((directory) => pluginsRoot(directory, marketplace)));
448
448
  const presence = plugin === null ? null : pluginPresence(plugin, sources, roots);
449
449
  const server = registersServer(await readJsonObject(join(root, '.mcp.json')));
450
+ // A line under SessionStart is the hook's by the hook's own reading of it, as any line is.
451
+ // A root on it that is no directory is a line of the hook that is off, and
452
+ // one all the same, so the mutant of the test for a directory is
453
+ // equivalent.
454
+ const isHook = (words) => claudeHookLine(words, parseOptions, root, (path) => isDirectory(resolve(root, path))) !== null;
455
+ const sessionStart = sources.some(({ settings }) => startsSessions(settings, isHook));
456
+ // init writes its hooks into the project's file alone, and none where an array unloads it.
457
+ const unloaded = arrayMatcher(sources.find(({ file }) => file === PROJECT_SETTINGS).settings);
450
458
  const under = (release) => {
451
- const lines = reportHooks(sources, root, (words) => read.get(JSON.stringify(words)), pattern, commasUnder(release));
459
+ const lines = reportHooks(sources, root, (words) => read.get(JSON.stringify(words)), pattern, matchingUnder(release));
452
460
  // A line Claude Code starts for no write, before one or after, is in no
453
461
  // one's way: it is said, and the file is not counted for it.
454
462
  const hooks = sources.map(({ file }) => file).filter((file) => lines.some((line) => line.file === file && (isStarted(line.before) || isStarted(line.after))));
455
463
  // In the order a document has them, which a script may compare by the byte.
456
- return { plugin, presence, hooksOff: hooksDisabled(sources), hooks, lines, server, release };
464
+ return { plugin, presence, hooksOff: hooksDisabled(sources), hooks, lines, server, sessionStart, release, unloaded };
457
465
  };
458
466
  const untold = under(null);
459
467
  const asked = reported ? wiringState(untold) !== 'none' : JSON.stringify(untold.lines) !== JSON.stringify(reportHooks(sources, root, (words) => read.get(JSON.stringify(words)), pattern));