@descent-vtt/spec-harness 0.18.0 → 0.20.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/.claude-plugin/marketplace.json +1 -1
- package/.claude-plugin/plugin.json +1 -1
- package/CHANGELOG.md +372 -0
- package/README.md +78 -11
- package/dist/briefs.d.ts +45 -0
- package/dist/briefs.js +73 -0
- package/dist/briefs.js.map +1 -1
- package/dist/cli.d.ts +1 -1
- package/dist/cli.js +384 -84
- package/dist/cli.js.map +1 -1
- package/dist/commands.js +7 -2
- package/dist/commands.js.map +1 -1
- package/dist/config.d.ts +15 -1
- package/dist/config.js +16 -3
- package/dist/config.js.map +1 -1
- package/dist/configure.d.ts +142 -32
- package/dist/configure.js +316 -89
- package/dist/configure.js.map +1 -1
- package/dist/fs.d.ts +150 -3
- package/dist/fs.js +459 -17
- package/dist/fs.js.map +1 -1
- package/dist/git.d.ts +85 -9
- package/dist/git.js +140 -16
- package/dist/git.js.map +1 -1
- package/dist/hooks.d.ts +111 -0
- package/dist/hooks.js +172 -16
- package/dist/hooks.js.map +1 -1
- package/dist/round.d.ts +8 -0
- package/dist/round.js +32 -9
- package/dist/round.js.map +1 -1
- package/dist/sandbox.js +4 -3
- package/dist/sandbox.js.map +1 -1
- package/dist/server.d.ts +25 -1
- package/dist/server.js +194 -27
- package/dist/server.js.map +1 -1
- package/dist/setup.d.ts +6 -3
- package/dist/setup.js +74 -23
- package/dist/setup.js.map +1 -1
- package/dist/siblings.d.ts +42 -7
- package/dist/siblings.js +135 -30
- package/dist/siblings.js.map +1 -1
- package/dist/types.d.ts +5 -0
- package/dist/types.js.map +1 -1
- package/dist/usage.d.ts +66 -12
- package/dist/usage.js +134 -21
- package/dist/usage.js.map +1 -1
- package/dist/workspace.d.ts +80 -1
- package/dist/workspace.js +188 -8
- package/dist/workspace.js.map +1 -1
- package/hooks/hooks.json +16 -0
- package/package.json +2 -2
package/dist/hooks.js
CHANGED
|
@@ -36,6 +36,20 @@ 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';
|
|
45
|
+
/**
|
|
46
|
+
* The event Claude Code runs as its main agent finishes a turn, under which
|
|
47
|
+
* the hook reports what the work tree holds, where the configuration asks
|
|
48
|
+
* for it (ADR-0005). No tool is named.
|
|
49
|
+
*/
|
|
50
|
+
export const TURN_END = 'Stop';
|
|
51
|
+
/** The events that name no tool, which the hook answers of the session and not of a write. */
|
|
52
|
+
const NO_TOOL = [SESSION_START, TURN_END];
|
|
39
53
|
/** Reads a Claude Code hook's stdin, or says why it cannot. */
|
|
40
54
|
export function parseClaudeHook(raw) {
|
|
41
55
|
let value;
|
|
@@ -49,24 +63,94 @@ export function parseClaudeHook(raw) {
|
|
|
49
63
|
return { error: 'the hook input is not a JSON object' };
|
|
50
64
|
const event = value['hook_event_name'];
|
|
51
65
|
const tool = value['tool_name'];
|
|
52
|
-
if (typeof event !== 'string' || typeof tool !== 'string')
|
|
66
|
+
if (typeof event !== 'string' || (typeof tool !== 'string' && !NO_TOOL.includes(event)))
|
|
53
67
|
return { error: 'the hook input names no event or tool' };
|
|
68
|
+
// A session's start and a turn's end name no tool, and are read as naming none.
|
|
69
|
+
const named = typeof tool === 'string' ? tool : '';
|
|
54
70
|
const input = isObject(value['tool_input']) ? value['tool_input'] : {};
|
|
55
71
|
const paths = [];
|
|
56
|
-
if (WRITING_TOOLS.includes(
|
|
72
|
+
if (WRITING_TOOLS.includes(named)) {
|
|
57
73
|
for (const field of PATH_FIELDS) {
|
|
58
74
|
const path = input[field];
|
|
59
75
|
if (typeof path === 'string' && path !== '' && !paths.includes(path))
|
|
60
76
|
paths.push(path);
|
|
61
77
|
}
|
|
62
78
|
}
|
|
63
|
-
const
|
|
64
|
-
|
|
79
|
+
const text = (field) => {
|
|
80
|
+
const found = value[field];
|
|
81
|
+
return typeof found === 'string' ? found : null;
|
|
82
|
+
};
|
|
83
|
+
return { event, tool: named, cwd: text('cwd'), paths, session: text('session_id'), call: text('tool_use_id') };
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* The answer as a session starts: what would hold every write of it, in
|
|
87
|
+
* one line, for the person as `systemMessage` and for Claude as
|
|
88
|
+
* `additionalContext`; or nothing, where all is well. Always exit 0: the
|
|
89
|
+
* event cannot block, and a check that fails must not keep a session from
|
|
90
|
+
* starting.
|
|
91
|
+
*/
|
|
92
|
+
export function sessionResponse(problem) {
|
|
93
|
+
if (problem === null)
|
|
94
|
+
return NONE;
|
|
95
|
+
const said = `spec-harness: ${problem}`;
|
|
96
|
+
return { stdout: `${JSON.stringify({ systemMessage: said, hookSpecificOutput: { hookEventName: SESSION_START, additionalContext: said } })}\n`, exitCode: 0 };
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* The answer as a turn ends: the report, in one `systemMessage`, or
|
|
100
|
+
* nothing, where there is nothing to report. Always exit 0, and never a
|
|
101
|
+
* field that has Claude go on: `decision`, `additionalContext` and exit 2
|
|
102
|
+
* each continue the turn under Stop, and `continue: false` would stop one
|
|
103
|
+
* another hook continues. Claude Code shows a `systemMessage` to the person
|
|
104
|
+
* from an entry it waits on, and hands it to Claude on the next turn from
|
|
105
|
+
* one it runs in the background, as `init` writes it (ADR-0005, ADR-0012).
|
|
106
|
+
*/
|
|
107
|
+
export function turnResponse(report) {
|
|
108
|
+
if (report === null)
|
|
109
|
+
return NONE;
|
|
110
|
+
return { stdout: `${JSON.stringify({ systemMessage: report })}\n`, exitCode: 0 };
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* The answer before the one write a configuration that does not load lets
|
|
114
|
+
* through, a write to the configuration file itself: no decision, so that
|
|
115
|
+
* the person's own permission prompt stands as for any write the guard
|
|
116
|
+
* passes, and the line that says why every other write is held, for the
|
|
117
|
+
* person as `systemMessage` and for Claude as `additionalContext`, which
|
|
118
|
+
* Claude Code's hooks reference has a PreToolUse hook's answer carry
|
|
119
|
+
* (ADR-0005). Exit 0.
|
|
120
|
+
*/
|
|
121
|
+
export function mendResponse(said) {
|
|
122
|
+
const line = `spec-harness: ${said}`;
|
|
123
|
+
return { stdout: `${JSON.stringify({ systemMessage: line, hookSpecificOutput: { hookEventName: 'PreToolUse', additionalContext: line } })}\n`, exitCode: 0 };
|
|
124
|
+
}
|
|
125
|
+
/** The answer that says nothing and decides nothing: the write goes on as the person's own permissions have it. */
|
|
126
|
+
export const NONE = { stdout: '', exitCode: 0 };
|
|
127
|
+
/**
|
|
128
|
+
* The answer where the hook checks nothing and says so, a question it cannot
|
|
129
|
+
* read or a line of its own that turns the guard off: one line, as
|
|
130
|
+
* `systemMessage`, which Claude Code's hooks reference has shown to the
|
|
131
|
+
* person on every event the hook answers, and exit 0. It decides nothing,
|
|
132
|
+
* so the call goes on as it would without the hook. It was exit 1, which
|
|
133
|
+
* the reference counts as a hook that failed: under `onFailure: "block"`,
|
|
134
|
+
* which holds the call of a hook that cannot start, it held every write of
|
|
135
|
+
* a session for a line only a write can mend (ADR-0005).
|
|
136
|
+
*/
|
|
137
|
+
export function warningResponse(said) {
|
|
138
|
+
return { stdout: `${JSON.stringify({ systemMessage: `spec-harness: ${said}` })}\n`, exitCode: 0 };
|
|
65
139
|
}
|
|
66
|
-
const NONE = { stdout: '', exitCode: 0 };
|
|
67
140
|
function reasonOf(decisions) {
|
|
68
141
|
return decisions.map((decision) => `spec-harness: ${decision.message}. Next: ${decision.hint}.`).join('\n');
|
|
69
142
|
}
|
|
143
|
+
/** The answer after a write: the warnings, as additional context for the model, or nothing. */
|
|
144
|
+
export function afterResponse(warnings) {
|
|
145
|
+
if (warnings.length === 0)
|
|
146
|
+
return NONE;
|
|
147
|
+
return {
|
|
148
|
+
stdout: `${JSON.stringify({
|
|
149
|
+
hookSpecificOutput: { hookEventName: 'PostToolUse', additionalContext: reasonOf(warnings) },
|
|
150
|
+
})}\n`,
|
|
151
|
+
exitCode: 0,
|
|
152
|
+
};
|
|
153
|
+
}
|
|
70
154
|
/**
|
|
71
155
|
* The answer to a Claude Code hook: a refusal or a question before a write,
|
|
72
156
|
* a warning after one, or nothing.
|
|
@@ -89,19 +173,91 @@ export function claudeResponse(event, decisions) {
|
|
|
89
173
|
exitCode: 0,
|
|
90
174
|
};
|
|
91
175
|
}
|
|
92
|
-
if (event === 'PostToolUse')
|
|
93
|
-
|
|
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
|
-
}
|
|
176
|
+
if (event === 'PostToolUse')
|
|
177
|
+
return afterResponse(warningsOf(decisions));
|
|
103
178
|
return NONE;
|
|
104
179
|
}
|
|
180
|
+
/** The decisions a run after the write would say: those that warn, each by its message and next step. */
|
|
181
|
+
export function warningsOf(decisions) {
|
|
182
|
+
return decisions.filter((decision) => decision.verdict === 'warn').map(({ message, hint }) => ({ message, hint }));
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* What the hook reads besides its question and its line, as it decides: the
|
|
186
|
+
* project Claude Code names, the brief the environment names, and the
|
|
187
|
+
* branch a forge's CI names on a detached head (ADR-0004).
|
|
188
|
+
*/
|
|
189
|
+
export const CALL_VARIABLES = [
|
|
190
|
+
'CLAUDE_PROJECT_DIR',
|
|
191
|
+
'SPEC_BRIEF',
|
|
192
|
+
'GITHUB_HEAD_REF',
|
|
193
|
+
'GITHUB_REF_TYPE',
|
|
194
|
+
'GITHUB_REF_NAME',
|
|
195
|
+
'CI_MERGE_REQUEST_SOURCE_BRANCH_NAME',
|
|
196
|
+
'CI_COMMIT_BRANCH',
|
|
197
|
+
];
|
|
198
|
+
/** An id the hook names a file by: what Claude Code's ids are made of, and nothing a path could climb out by. */
|
|
199
|
+
const ID = /^[\w-]{1,256}$/;
|
|
200
|
+
/**
|
|
201
|
+
* The session a question is in, which spec-brief's answers are kept for,
|
|
202
|
+
* or `null` where it names none in the form Claude Code's ids have: such a
|
|
203
|
+
* question has every answer asked for afresh (ADR-0002).
|
|
204
|
+
*/
|
|
205
|
+
export function sessionOf(request) {
|
|
206
|
+
// The pattern reads null as "null", and what is answered is the session,
|
|
207
|
+
// null: the test of null is the compiler's, and its mutant equivalent.
|
|
208
|
+
return request.session !== null && ID.test(request.session) ? request.session : null;
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* The call a question is about, or `null` where it does not say which
|
|
212
|
+
* session and call it is, or says it in a form no file is named by.
|
|
213
|
+
*/
|
|
214
|
+
export function callRecord(request, line, started, env, release) {
|
|
215
|
+
const { session, call } = request;
|
|
216
|
+
if (session === null || call === null || !ID.test(session) || !ID.test(call))
|
|
217
|
+
return null;
|
|
218
|
+
return {
|
|
219
|
+
release,
|
|
220
|
+
session,
|
|
221
|
+
call,
|
|
222
|
+
tool: request.tool,
|
|
223
|
+
paths: request.paths,
|
|
224
|
+
cwd: request.cwd,
|
|
225
|
+
line: [line.root ?? null, line.brief ?? null, line.base ?? null],
|
|
226
|
+
started,
|
|
227
|
+
env: CALL_VARIABLES.map((name) => env[name] ?? null),
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
/** The name of the file a call's decisions are kept under. */
|
|
231
|
+
export function callFile(record) {
|
|
232
|
+
return `${record.session}.${record.call}.json`;
|
|
233
|
+
}
|
|
234
|
+
/** What a run before the write keeps for the run after it: the call, and the warnings it decided. */
|
|
235
|
+
export function keptCall(record, warnings) {
|
|
236
|
+
return `${JSON.stringify({ record, warnings })}\n`;
|
|
237
|
+
}
|
|
238
|
+
function isWarning(value) {
|
|
239
|
+
return isObject(value) && typeof value['message'] === 'string' && typeof value['hint'] === 'string';
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* The warnings kept for this call, or `null` where what was kept is not
|
|
243
|
+
* JSON, is another call's, or holds anything but warnings: such a call is
|
|
244
|
+
* decided as it was before anything was kept (ADR-0002).
|
|
245
|
+
*/
|
|
246
|
+
export function warningsKept(text, record) {
|
|
247
|
+
let value;
|
|
248
|
+
try {
|
|
249
|
+
value = JSON.parse(text);
|
|
250
|
+
}
|
|
251
|
+
catch {
|
|
252
|
+
// Read as an empty object, a text that is not JSON holds no record
|
|
253
|
+
// either: that mutant is equivalent.
|
|
254
|
+
return null;
|
|
255
|
+
}
|
|
256
|
+
if (!isObject(value) || JSON.stringify(value['record']) !== JSON.stringify(record))
|
|
257
|
+
return null;
|
|
258
|
+
const warnings = value['warnings'];
|
|
259
|
+
return Array.isArray(warnings) && warnings.every(isWarning) ? warnings.map(({ message, hint }) => ({ message, hint })) : null;
|
|
260
|
+
}
|
|
105
261
|
/**
|
|
106
262
|
* The answer to git's pre-commit hook: every file the commit would change
|
|
107
263
|
* 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,"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;AAa9E,S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,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,cAA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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/**\n * The event Claude Code runs as its main agent finishes a turn, under which\n * the hook reports what the work tree holds, where the configuration asks\n * for it (ADR-0005). No tool is named.\n */\nexport const TURN_END = 'Stop';\n\n/** The events that name no tool, which the hook answers of the session and not of a write. */\nconst NO_TOOL: readonly string[] = [SESSION_START, TURN_END];\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' && !NO_TOOL.includes(event))) return { error: 'the hook input names no event or tool' };\n // A session's start and a turn's end name no tool, and are 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\n/**\n * The answer as a turn ends: the report, in one `systemMessage`, or\n * nothing, where there is nothing to report. Always exit 0, and never a\n * field that has Claude go on: `decision`, `additionalContext` and exit 2\n * each continue the turn under Stop, and `continue: false` would stop one\n * another hook continues. Claude Code shows a `systemMessage` to the person\n * from an entry it waits on, and hands it to Claude on the next turn from\n * one it runs in the background, as `init` writes it (ADR-0005, ADR-0012).\n */\nexport function turnResponse(report: string | null): HookResponse {\n if (report === null) return NONE;\n return { stdout: `${JSON.stringify({ systemMessage: report })}\\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\n/**\n * The answer before the one write a configuration that does not load lets\n * through, a write to the configuration file itself: no decision, so that\n * the person's own permission prompt stands as for any write the guard\n * passes, and the line that says why every other write is held, for the\n * person as `systemMessage` and for Claude as `additionalContext`, which\n * Claude Code's hooks reference has a PreToolUse hook's answer carry\n * (ADR-0005). Exit 0.\n */\nexport function mendResponse(said: string): HookResponse {\n const line = `spec-harness: ${said}`;\n return { stdout: `${JSON.stringify({ systemMessage: line, hookSpecificOutput: { hookEventName: 'PreToolUse', additionalContext: line } })}\\n`, exitCode: 0 };\n}\n\n/** The answer that says nothing and decides nothing: the write goes on as the person's own permissions have it. */\nexport const NONE: HookResponse = { stdout: '', exitCode: 0 };\n\n/**\n * The answer where the hook checks nothing and says so, a question it cannot\n * read or a line of its own that turns the guard off: one line, as\n * `systemMessage`, which Claude Code's hooks reference has shown to the\n * person on every event the hook answers, and exit 0. It decides nothing,\n * so the call goes on as it would without the hook. It was exit 1, which\n * the reference counts as a hook that failed: under `onFailure: \"block\"`,\n * which holds the call of a hook that cannot start, it held every write of\n * a session for a line only a write can mend (ADR-0005).\n */\nexport function warningResponse(said: string): HookResponse {\n return { stdout: `${JSON.stringify({ systemMessage: `spec-harness: ${said}` })}\\n`, exitCode: 0 };\n}\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 session a question is in, which spec-brief's answers are kept for,\n * or `null` where it names none in the form Claude Code's ids have: such a\n * question has every answer asked for afresh (ADR-0002).\n */\nexport function sessionOf(request: HookRequest): string | null {\n // The pattern reads null as \"null\", and what is answered is the session,\n // null: the test of null is the compiler's, and its mutant equivalent.\n return request.session !== null && ID.test(request.session) ? request.session : null;\n}\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.d.ts
CHANGED
|
@@ -127,10 +127,18 @@ export declare function claudeCodeWiring(root: string, io: CliIO, pattern?: Patt
|
|
|
127
127
|
* wrong with the name when no round can work under it. Every other command
|
|
128
128
|
* stops there. A diagnosis says it, being what a person runs when the guard
|
|
129
129
|
* holds a write they expected it to pass.
|
|
130
|
+
*
|
|
131
|
+
* `branch` is whether the name no round can work under is the branch's: a
|
|
132
|
+
* brief it names that spec-brief does not know, or has archived. The hook
|
|
133
|
+
* reads the same branch and holds every write under such a name, which is a
|
|
134
|
+
* setup that holds every write, as a hook's line naming such a brief is
|
|
135
|
+
* (ADR-0004). A name the flag gives is the question doctor was asked, and
|
|
136
|
+
* holds no write of anyone's.
|
|
130
137
|
*/
|
|
131
138
|
export declare function diagnoseBrief(workspace: Workspace, options: Pick<Options, 'brief'>, env: CliIO['env']): Promise<{
|
|
132
139
|
readonly id: string | null;
|
|
133
140
|
readonly problem: string | null;
|
|
141
|
+
readonly branch: boolean;
|
|
134
142
|
}>;
|
|
135
143
|
export interface AuditResult {
|
|
136
144
|
readonly brief: BriefRow;
|
package/dist/round.js
CHANGED
|
@@ -17,9 +17,9 @@ 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 {
|
|
20
|
+
import { arrayMatcher, enabledPlugin, exportedFile, handedLines, hooksDisabled, HOSTED_REGISTRY, isStarted, loadsPlugin, MARKETPLACE_REGISTRY, matchingUnder, patternReader, pluginMarketplace, pluginPresence, PROJECT_SETTINGS, readsOnFailure, registersServer, reportHooks, SPEC_BRIEF_CONFIGS, startsSessions, wiringState, } from './configure.js';
|
|
21
21
|
import { readRules, renderContext } from './context.js';
|
|
22
|
-
import { isDirectory, readJsonObject, readText, repositoryPath, stateDirectory, writeAtomic } from './fs.js';
|
|
22
|
+
import { isDirectory, isFile, readJsonObject, readText, repositoryPath, stateDirectory, writeAtomic } from './fs.js';
|
|
23
23
|
import { blameLine, changes, commonDirectory, isUncommitted, mergeBase, remoteDefault, revision, show, verifyCommit, workTreeRoot } from './git.js';
|
|
24
24
|
import { decide } from './guard.js';
|
|
25
25
|
import { diffManifest, ecosystemOf, readManifestNames } from './manifests.js';
|
|
@@ -310,7 +310,10 @@ export function pluginFile(root) {
|
|
|
310
310
|
// or on the missing key inside this block, which the catch reads as no
|
|
311
311
|
// installed copy: `exports?.` and `target !== null` are equivalent to
|
|
312
312
|
// their mutants here. So is the encoding: a Buffer parses as its text.
|
|
313
|
-
|
|
313
|
+
// Only a file is read: what stands there and is none is no installed
|
|
314
|
+
// copy, as a manifest with no exports is (ADR-0002).
|
|
315
|
+
const file = join(installed, 'package.json');
|
|
316
|
+
const manifest = isFile(file) ? JSON.parse(readFileSync(file, 'utf8')) : {};
|
|
314
317
|
const target = exportedFile(manifest.exports?.['./spec-brief-plugin']);
|
|
315
318
|
if (target !== null)
|
|
316
319
|
candidates.push(join(installed, target));
|
|
@@ -447,13 +450,21 @@ export async function claudeCodeWiring(root, io, pattern = patternReader(), repo
|
|
|
447
450
|
const roots = marketplace === null ? [] : await Promise.all(pluginDirectories(io.env).map((directory) => pluginsRoot(directory, marketplace)));
|
|
448
451
|
const presence = plugin === null ? null : pluginPresence(plugin, sources, roots);
|
|
449
452
|
const server = registersServer(await readJsonObject(join(root, '.mcp.json')));
|
|
453
|
+
// A line under SessionStart is the hook's by the hook's own reading of it, as any line is.
|
|
454
|
+
// A root on it that is no directory is a line of the hook that is off, and
|
|
455
|
+
// one all the same, so the mutant of the test for a directory is
|
|
456
|
+
// equivalent.
|
|
457
|
+
const isHook = (words) => claudeHookLine(words, parseOptions, root, (path) => isDirectory(resolve(root, path))) !== null;
|
|
458
|
+
const sessionStart = sources.some(({ settings }) => startsSessions(settings, isHook));
|
|
459
|
+
// init writes its hooks into the project's file alone, and none where an array unloads it.
|
|
460
|
+
const unloaded = arrayMatcher(sources.find(({ file }) => file === PROJECT_SETTINGS).settings);
|
|
450
461
|
const under = (release) => {
|
|
451
|
-
const lines = reportHooks(sources, root, (words) => read.get(JSON.stringify(words)), pattern,
|
|
462
|
+
const lines = reportHooks(sources, root, (words) => read.get(JSON.stringify(words)), pattern, matchingUnder(release), readsOnFailure(release));
|
|
452
463
|
// A line Claude Code starts for no write, before one or after, is in no
|
|
453
464
|
// one's way: it is said, and the file is not counted for it.
|
|
454
465
|
const hooks = sources.map(({ file }) => file).filter((file) => lines.some((line) => line.file === file && (isStarted(line.before) || isStarted(line.after))));
|
|
455
466
|
// 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 };
|
|
467
|
+
return { plugin, presence, hooksOff: hooksDisabled(sources), hooks, lines, server, sessionStart, release, unloaded };
|
|
457
468
|
};
|
|
458
469
|
const untold = under(null);
|
|
459
470
|
const asked = reported ? wiringState(untold) !== 'none' : JSON.stringify(untold.lines) !== JSON.stringify(reportHooks(sources, root, (words) => read.get(JSON.stringify(words)), pattern));
|
|
@@ -516,25 +527,34 @@ async function hookLineHere(words, root, io) {
|
|
|
516
527
|
* wrong with the name when no round can work under it. Every other command
|
|
517
528
|
* stops there. A diagnosis says it, being what a person runs when the guard
|
|
518
529
|
* holds a write they expected it to pass.
|
|
530
|
+
*
|
|
531
|
+
* `branch` is whether the name no round can work under is the branch's: a
|
|
532
|
+
* brief it names that spec-brief does not know, or has archived. The hook
|
|
533
|
+
* reads the same branch and holds every write under such a name, which is a
|
|
534
|
+
* setup that holds every write, as a hook's line naming such a brief is
|
|
535
|
+
* (ADR-0004). A name the flag gives is the question doctor was asked, and
|
|
536
|
+
* holds no write of anyone's.
|
|
519
537
|
*/
|
|
520
538
|
export async function diagnoseBrief(workspace, options, env) {
|
|
521
539
|
if (options.brief !== undefined && isBlank(options.brief))
|
|
522
|
-
return { id: null, problem: emptyValue('--brief', 'brief', options.brief) };
|
|
540
|
+
return { id: null, problem: emptyValue('--brief', 'brief', options.brief), branch: false };
|
|
523
541
|
const id = namedId(workspace, options, env);
|
|
524
542
|
// A spec-brief that cannot be run is a row of the report, with the reason.
|
|
525
543
|
if (workspace.siblings.locate('spec-brief').kind !== 'found')
|
|
526
|
-
return { id, problem: null };
|
|
544
|
+
return { id, problem: null, branch: false };
|
|
527
545
|
try {
|
|
528
546
|
// Where nothing names a brief, spec-brief is not asked (ADR-0004), and
|
|
529
547
|
// there is no problem to state.
|
|
530
|
-
|
|
548
|
+
const active = await activeBrief(workspace, options, env);
|
|
549
|
+
const branch = (active.kind === 'unknown' || active.kind === 'archived') && active.source === 'branch';
|
|
550
|
+
return { id, problem: describeActive(active).problem, branch };
|
|
531
551
|
}
|
|
532
552
|
catch (error) {
|
|
533
553
|
// Asking spec-brief throws nothing but this, so the check is equivalent
|
|
534
554
|
// to its mutant; it keeps any other error the stack it needs.
|
|
535
555
|
if (!(error instanceof SiblingError))
|
|
536
556
|
throw error;
|
|
537
|
-
return { id, problem: `spec-brief could not say whether it knows brief ${id}: ${error.message}
|
|
557
|
+
return { id, problem: `spec-brief could not say whether it knows brief ${id}: ${error.message}`, branch: false };
|
|
538
558
|
}
|
|
539
559
|
}
|
|
540
560
|
async function archiveReasons(workspace, brief, base) {
|
|
@@ -611,6 +631,9 @@ export async function listEscalations(workspace) {
|
|
|
611
631
|
const directory = await escalationDirectory(workspace);
|
|
612
632
|
const out = [];
|
|
613
633
|
for (const name of (await readdir(directory)).filter((n) => n.endsWith('.json')).sort()) {
|
|
634
|
+
// What is no file is no request, and is passed over as one that cannot be read is.
|
|
635
|
+
if (!isFile(join(directory, name)))
|
|
636
|
+
continue;
|
|
614
637
|
try {
|
|
615
638
|
// A Buffer parses as its text, so the encoding is equivalent to its mutant.
|
|
616
639
|
const request = JSON.parse(await readFile(join(directory, name), 'utf8'));
|