claude-dev-env 8.4.2 → 8.5.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/commands/fix-followups.md +66 -0
- package/hooks/followup_ledger.py +143 -0
- package/hooks/hooks_constants/followup_ledger_constants.py +27 -0
- package/hooks/test_followup_ledger.py +83 -0
- package/package.json +1 -1
- package/rules/flag-non-breaking-findings.md +72 -0
- package/scripts/dev_env_scripts_constants/followup_constants.py +72 -0
- package/scripts/followup_cli.py +273 -0
- package/scripts/test_followup_cli.py +156 -0
- package/scripts/test_validate_instruction_pairs.py +144 -1
- package/scripts/validate_instruction_pairs.py +277 -47
- package/scripts/windows/pstack/pstack-command-shim.ps1 +9 -2
- package/scripts/audit-hooks-cli.mjs +0 -59
- package/scripts/audit-hooks.mjs +0 -452
- package/scripts/tests/test-audit-hooks-cli.mjs +0 -150
- package/scripts/tests/test-audit-hooks.mjs +0 -497
package/scripts/audit-hooks.mjs
DELETED
|
@@ -1,452 +0,0 @@
|
|
|
1
|
-
import { spawnSync } from "node:child_process";
|
|
2
|
-
import { existsSync, readFileSync } from "node:fs";
|
|
3
|
-
import { homedir } from "node:os";
|
|
4
|
-
import path from "node:path";
|
|
5
|
-
|
|
6
|
-
import { KNOWN_GIT_HOOK_NAMES } from "../bin/git_hooks_installer.mjs";
|
|
7
|
-
|
|
8
|
-
const dispatcherSpecs = Object.freeze([
|
|
9
|
-
["script:blocking/pre_tool_use_dispatcher.py", "hooks_constants/pre_tool_use_dispatcher_constants.py", "ALL_HOSTED_HOOK_ENTRIES"],
|
|
10
|
-
["script:blocking/bash_pre_tool_use_dispatcher.py", "hooks_constants/bash_pre_tool_use_dispatcher_constants.py", "ALL_BASH_HOSTED_HOOK_ENTRIES"],
|
|
11
|
-
["script:validation/post_tool_use_dispatcher.py", "hooks_constants/post_tool_use_dispatcher_constants.py", "ALL_POST_HOSTED_HOOK_ENTRIES"],
|
|
12
|
-
["script:blocking/bash_post_call_dispatcher.py", "hooks_constants/bash_post_call_dispatcher_constants.py", "ALL_BASH_POST_TOOL_USE_HOSTED_HOOK_ENTRIES"],
|
|
13
|
-
]);
|
|
14
|
-
const lifecycleNames = new Set([
|
|
15
|
-
"delete",
|
|
16
|
-
"keep_boundary_check",
|
|
17
|
-
"keep_nonblocking_automation",
|
|
18
|
-
"move_to_ci",
|
|
19
|
-
"move_to_explicit_command",
|
|
20
|
-
"move_to_linter",
|
|
21
|
-
]);
|
|
22
|
-
const nonblockingLifecycle = "keep_nonblocking_automation";
|
|
23
|
-
const blockingGitEvents = new Set(["pre-commit", "pre-push"]);
|
|
24
|
-
|
|
25
|
-
function normalizeMatcher(rawMatcher) {
|
|
26
|
-
return typeof rawMatcher === "string" && rawMatcher
|
|
27
|
-
? rawMatcher.split("|").sort().join("|")
|
|
28
|
-
: "*";
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function targetFromCommand(command, scope, event, ordinal) {
|
|
32
|
-
const normalized = command.replaceAll("\\", "/");
|
|
33
|
-
const hookMarker = "/hooks/";
|
|
34
|
-
if (normalized.includes(hookMarker)) {
|
|
35
|
-
const relative = normalized.split(hookMarker).at(-1).split(/[\s"']/u, 1)[0];
|
|
36
|
-
if (relative.endsWith(".py")) return `script:${relative}`;
|
|
37
|
-
}
|
|
38
|
-
const importMatch = /\bfrom\s+([A-Za-z_][\w.]*)\s+import\s+main\b/u.exec(command);
|
|
39
|
-
if (importMatch) return `script:${importMatch[1].replaceAll(".", "/")}.py`;
|
|
40
|
-
const scriptMatches = [...normalized.matchAll(/(?:^|\s)["']?([^"'/\s]+\.(?:exe|js|mjs|ps1|py))\b/gu)];
|
|
41
|
-
if (scriptMatches.length) return `external:${scriptMatches.at(-1)[1]}`;
|
|
42
|
-
return `external:${scope}:${event}:${ordinal}`;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function readDirect(configPath, scope) {
|
|
46
|
-
const parsed = JSON.parse(readFileSync(configPath, "utf8"));
|
|
47
|
-
const records = [];
|
|
48
|
-
for (const [event, groups] of Object.entries(parsed.hooks ?? {})) {
|
|
49
|
-
for (const group of Array.isArray(groups) ? groups : []) {
|
|
50
|
-
appendGroupRecords(records, scope, event, group);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
return records;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function appendGroupRecords(records, scope, event, group) {
|
|
57
|
-
const matcher = normalizeMatcher(group.matcher);
|
|
58
|
-
for (const hook of Array.isArray(group.hooks) ? group.hooks : []) {
|
|
59
|
-
if (hook?.type !== "command" || typeof hook.command !== "string") continue;
|
|
60
|
-
const ordinal = records.length;
|
|
61
|
-
const target = targetFromCommand(hook.command, scope, event, ordinal);
|
|
62
|
-
const isDispatcher = dispatcherSpecs.some(([knownTarget]) => knownTarget === target);
|
|
63
|
-
records.push({
|
|
64
|
-
event,
|
|
65
|
-
matcher,
|
|
66
|
-
ordinal,
|
|
67
|
-
role: isDispatcher ? "dispatcher" : "hook",
|
|
68
|
-
scope,
|
|
69
|
-
target,
|
|
70
|
-
timeoutSeconds: Number.isInteger(hook.timeout) ? hook.timeout : null,
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
function assignmentBody(source, rosterName) {
|
|
76
|
-
const rosterOffset = source.lastIndexOf(rosterName);
|
|
77
|
-
if (rosterOffset < 0) throw new Error(`missing roster ${rosterName}`);
|
|
78
|
-
const equalsOffset = source.indexOf("=", rosterOffset);
|
|
79
|
-
const openOffset = source.indexOf("(", equalsOffset);
|
|
80
|
-
if (equalsOffset < 0 || openOffset < 0) throw new Error(`invalid roster ${rosterName}`);
|
|
81
|
-
const closeOffset = matchingParenthesis(source, openOffset);
|
|
82
|
-
return source.slice(openOffset + 1, closeOffset);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
function matchingParenthesis(source, openOffset) {
|
|
86
|
-
let depth = 0;
|
|
87
|
-
let quote = "";
|
|
88
|
-
for (let offset = openOffset; offset < source.length; offset += 1) {
|
|
89
|
-
const character = source[offset];
|
|
90
|
-
const previous = source[offset - 1];
|
|
91
|
-
if (quote && character === quote && previous !== "\\") quote = "";
|
|
92
|
-
else if (!quote && (character === '"' || character === "'")) quote = character;
|
|
93
|
-
else if (!quote && character === "(") depth += 1;
|
|
94
|
-
else if (!quote && character === ")") depth -= 1;
|
|
95
|
-
if (depth === 0) return offset;
|
|
96
|
-
}
|
|
97
|
-
throw new Error("unclosed roster");
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
function topLevelEntries(source) {
|
|
101
|
-
const entries = [];
|
|
102
|
-
let depth = 0;
|
|
103
|
-
let quote = "";
|
|
104
|
-
let start = 0;
|
|
105
|
-
for (let offset = 0; offset < source.length; offset += 1) {
|
|
106
|
-
const character = source[offset];
|
|
107
|
-
const previous = source[offset - 1];
|
|
108
|
-
if (quote && character === quote && previous !== "\\") quote = "";
|
|
109
|
-
else if (!quote && (character === '"' || character === "'")) quote = character;
|
|
110
|
-
else if (!quote && /[([{]/u.test(character)) depth += 1;
|
|
111
|
-
else if (!quote && /[\])}]/u.test(character)) depth -= 1;
|
|
112
|
-
else if (!quote && character === "," && depth === 0) {
|
|
113
|
-
entries.push(source.slice(start, offset));
|
|
114
|
-
start = offset + 1;
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
entries.push(source.slice(start));
|
|
118
|
-
return entries.filter((entry) => entry.trim());
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
function toolMatchers(source) {
|
|
122
|
-
const matchers = new Map();
|
|
123
|
-
for (const match of source.matchAll(/\b([A-Z][A-Z0-9_]*_TOOL_NAME)\s*=\s*["']([^"']+)["']/gu)) {
|
|
124
|
-
matchers.set(match[1], new Set([match[2]]));
|
|
125
|
-
}
|
|
126
|
-
const collections = [...source.matchAll(/\b([A-Z][A-Z0-9_]*_TOOL_NAMES)\s*(?::[^=]+)?=\s*frozenset\s*\(/gu)];
|
|
127
|
-
for (const collection of collections) {
|
|
128
|
-
const openOffset = source.indexOf("(", collection.index + collection[0].indexOf("frozenset"));
|
|
129
|
-
const body = source.slice(openOffset + 1, matchingParenthesis(source, openOffset));
|
|
130
|
-
const names = new Set();
|
|
131
|
-
for (const token of body.match(/\b[A-Z][A-Z0-9_]+\b/gu) ?? []) {
|
|
132
|
-
for (const toolName of matchers.get(token) ?? []) names.add(toolName);
|
|
133
|
-
}
|
|
134
|
-
matchers.set(collection[1], names);
|
|
135
|
-
}
|
|
136
|
-
return matchers;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
function rosterEntries(hooksRoot, constantsPath, rosterName) {
|
|
140
|
-
const source = readFileSync(path.join(hooksRoot, constantsPath), "utf8");
|
|
141
|
-
const body = assignmentBody(source, rosterName);
|
|
142
|
-
const matchers = toolMatchers(source);
|
|
143
|
-
return topLevelEntries(body).map((expression) => {
|
|
144
|
-
const targetMatch = /["']([^"']+\.py)["']/u.exec(expression);
|
|
145
|
-
if (!targetMatch) throw new Error(`invalid roster entry ${rosterName}`);
|
|
146
|
-
const names = new Set();
|
|
147
|
-
for (const token of expression.match(/\b[A-Z][A-Z0-9_]+\b/gu) ?? []) {
|
|
148
|
-
for (const toolName of matchers.get(token) ?? []) names.add(toolName);
|
|
149
|
-
}
|
|
150
|
-
return {
|
|
151
|
-
matcher: names.size ? [...names].sort().join("|") : null,
|
|
152
|
-
target: `script:${targetMatch[1].replaceAll("\\", "/")}`,
|
|
153
|
-
};
|
|
154
|
-
});
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
function expandHosted(directRecords, hooksRoot) {
|
|
158
|
-
const parents = new Map(
|
|
159
|
-
directRecords.filter(({ role }) => role === "dispatcher").map((record) => [record.target, record]),
|
|
160
|
-
);
|
|
161
|
-
const hosted = [];
|
|
162
|
-
for (const [dispatcherTarget, constantsPath, rosterName] of dispatcherSpecs) {
|
|
163
|
-
const parent = parents.get(dispatcherTarget);
|
|
164
|
-
if (!parent) continue;
|
|
165
|
-
const entries = rosterEntries(hooksRoot, constantsPath, rosterName);
|
|
166
|
-
for (const entry of entries) {
|
|
167
|
-
hosted.push({
|
|
168
|
-
event: parent.event,
|
|
169
|
-
matcher: entry.matcher ?? parent.matcher,
|
|
170
|
-
ordinal: hosted.length,
|
|
171
|
-
parentTarget: parent.target,
|
|
172
|
-
role: "hosted",
|
|
173
|
-
scope: parent.scope,
|
|
174
|
-
target: entry.target,
|
|
175
|
-
timeoutSeconds: parent.timeoutSeconds,
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
return hosted;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
function logicalRecords(directRecords, hostedRecords) {
|
|
183
|
-
return [...directRecords.filter(({ role }) => role !== "dispatcher"), ...hostedRecords].sort(recordSort);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
function recordSort(left, right) {
|
|
187
|
-
const leftKey = [left.scope, left.event, left.matcher, left.target, left.role, left.ordinal].join("\0");
|
|
188
|
-
const rightKey = [right.scope, right.event, right.matcher, right.target, right.role, right.ordinal].join("\0");
|
|
189
|
-
return leftKey.localeCompare(rightKey);
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
function duplicateFindings(records, scope) {
|
|
193
|
-
const counts = new Map();
|
|
194
|
-
for (const record of records) {
|
|
195
|
-
const key = [record.event, record.matcher, record.target].join("\0");
|
|
196
|
-
counts.set(key, (counts.get(key) ?? 0) + 1);
|
|
197
|
-
}
|
|
198
|
-
return [...counts.entries()]
|
|
199
|
-
.filter(([, count]) => count > 1)
|
|
200
|
-
.map(([key, count]) => ({
|
|
201
|
-
code: "DUPLICATE_EFFECTIVE_EXECUTION",
|
|
202
|
-
message: `The same event, matcher, and target run ${count} times.`,
|
|
203
|
-
scope,
|
|
204
|
-
severity: "error",
|
|
205
|
-
target: key.split("\0").at(-1),
|
|
206
|
-
}));
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
function missingTargetFindings(records, hooksRoot, scope) {
|
|
210
|
-
return records
|
|
211
|
-
.filter(({ target }) => target.startsWith("script:"))
|
|
212
|
-
.filter(({ target }) => !existsSync(path.join(hooksRoot, target.slice("script:".length))))
|
|
213
|
-
.map(({ target }) => ({
|
|
214
|
-
code: "MISSING_HOOK_TARGET",
|
|
215
|
-
message: "The registered script is missing from the installed hook tree.",
|
|
216
|
-
scope,
|
|
217
|
-
severity: "error",
|
|
218
|
-
target,
|
|
219
|
-
}));
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
function missingManagedFindings(expectedRecords, installedRecords, scope) {
|
|
223
|
-
const installedIdentities = new Set(installedRecords.map(recordIdentity));
|
|
224
|
-
return expectedRecords
|
|
225
|
-
.filter((record) => !installedIdentities.has(recordIdentity(record)))
|
|
226
|
-
.map(({ target }) => ({
|
|
227
|
-
code: "MISSING_MANAGED_REGISTRATION",
|
|
228
|
-
message: "The installed configuration is missing a canonical registration.",
|
|
229
|
-
scope,
|
|
230
|
-
severity: "error",
|
|
231
|
-
target,
|
|
232
|
-
}));
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
function recordIdentity({ event, matcher, target }) {
|
|
236
|
-
return [event, matcher, target].join("\0");
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
function readLifecycle(catalogPath) {
|
|
240
|
-
if (!catalogPath) return [];
|
|
241
|
-
const parsed = JSON.parse(readFileSync(catalogPath, "utf8"));
|
|
242
|
-
return Object.entries(parsed.hooks ?? {}).map(([target, decision]) => {
|
|
243
|
-
const safeTarget = /^(?:external:[A-Za-z0-9_.:-]+|git:(?:post-commit|pre-commit|pre-push)|script:[A-Za-z0-9_./-]+)$/u;
|
|
244
|
-
if (!safeTarget.test(target) || !lifecycleNames.has(decision?.lifecycle) || !decision.reason) {
|
|
245
|
-
throw new Error("invalid lifecycle entry");
|
|
246
|
-
}
|
|
247
|
-
return { lifecycle: decision.lifecycle, target };
|
|
248
|
-
});
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
function lifecycleFindings(records, lifecycle) {
|
|
252
|
-
const targets = new Set(records.map(({ target }) => target));
|
|
253
|
-
const classified = new Set(lifecycle.map(({ target }) => target));
|
|
254
|
-
const missing = [...targets].filter((target) => !classified.has(target));
|
|
255
|
-
const stale = [...classified].filter((target) => !targets.has(target));
|
|
256
|
-
return [
|
|
257
|
-
...missing.map((target) => ({
|
|
258
|
-
code: "UNCLASSIFIED_HOOK",
|
|
259
|
-
message: "The effective hook has no lifecycle decision.",
|
|
260
|
-
scope: "catalog",
|
|
261
|
-
severity: "error",
|
|
262
|
-
target,
|
|
263
|
-
})),
|
|
264
|
-
...stale.map((target) => ({
|
|
265
|
-
code: "STALE_LIFECYCLE_ENTRY",
|
|
266
|
-
message: "The lifecycle target is absent from the canonical graph.",
|
|
267
|
-
scope: "catalog",
|
|
268
|
-
severity: "warning",
|
|
269
|
-
target,
|
|
270
|
-
})),
|
|
271
|
-
];
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
function nonblockingFindings(records, lifecycle) {
|
|
275
|
-
const lifecycleByTarget = new Map(
|
|
276
|
-
lifecycle.map(({ target, lifecycle: lifecycleName }) => [target, lifecycleName]),
|
|
277
|
-
);
|
|
278
|
-
const uniqueTargets = [...new Set(records.map(({ target }) => target))];
|
|
279
|
-
const lifecycleMismatches = uniqueTargets
|
|
280
|
-
.filter((target) => !target.startsWith("git:") && lifecycleByTarget.has(target))
|
|
281
|
-
.filter((target) => lifecycleByTarget.get(target) !== nonblockingLifecycle)
|
|
282
|
-
.map((target) => ({
|
|
283
|
-
code: "NONBLOCKING_LIFECYCLE_MISMATCH",
|
|
284
|
-
message: "The registration is not eligible for nonblocking automation.",
|
|
285
|
-
scope: "catalog",
|
|
286
|
-
severity: "error",
|
|
287
|
-
target,
|
|
288
|
-
}));
|
|
289
|
-
const blockingGitFindings = records
|
|
290
|
-
.filter(({ event, scope }) => scope === "git" && blockingGitEvents.has(event))
|
|
291
|
-
.map(({ target }) => ({
|
|
292
|
-
code: "NATIVE_GIT_BLOCKING_HOOK",
|
|
293
|
-
message: "A native Git blocking hook remains active.",
|
|
294
|
-
scope: "git",
|
|
295
|
-
severity: "error",
|
|
296
|
-
target,
|
|
297
|
-
}));
|
|
298
|
-
return [...lifecycleMismatches, ...blockingGitFindings];
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
function registrationEligibility(records, lifecycle, isRequired) {
|
|
302
|
-
if (!isRequired) {
|
|
303
|
-
return { status: "not_checked", runtimeProof: "not_assessed" };
|
|
304
|
-
}
|
|
305
|
-
const lifecycleByTarget = new Map(
|
|
306
|
-
lifecycle.map(({ target, lifecycle: lifecycleName }) => [target, lifecycleName]),
|
|
307
|
-
);
|
|
308
|
-
const uniqueTargets = [...new Set(records.map(({ target }) => target))];
|
|
309
|
-
const hasUnclassifiedTarget = uniqueTargets.some((target) => !lifecycleByTarget.has(target));
|
|
310
|
-
const hasIneligibleTarget = uniqueTargets.some(
|
|
311
|
-
(target) => lifecycleByTarget.get(target) && lifecycleByTarget.get(target) !== nonblockingLifecycle,
|
|
312
|
-
);
|
|
313
|
-
const hasBlockingGitHook = records.some(
|
|
314
|
-
({ event, scope }) => scope === "git" && blockingGitEvents.has(event),
|
|
315
|
-
);
|
|
316
|
-
const isEligible = !hasUnclassifiedTarget && !hasIneligibleTarget && !hasBlockingGitHook;
|
|
317
|
-
return {
|
|
318
|
-
status: isEligible ? "eligible" : "ineligible",
|
|
319
|
-
runtimeProof: "not_assessed",
|
|
320
|
-
};
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
function readInstalled(configPath, hooksRoot, scope) {
|
|
324
|
-
if (!existsSync(configPath)) return { findings: [], logical: [], records: [] };
|
|
325
|
-
const records = readDirect(configPath, scope);
|
|
326
|
-
const hosted = expandHosted(records, hooksRoot);
|
|
327
|
-
const logical = logicalRecords(records, hosted);
|
|
328
|
-
return {
|
|
329
|
-
findings: [
|
|
330
|
-
...duplicateFindings(logical, scope),
|
|
331
|
-
...missingTargetFindings([...records, ...hosted], hooksRoot, scope),
|
|
332
|
-
],
|
|
333
|
-
logical,
|
|
334
|
-
records,
|
|
335
|
-
};
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
function activeGitHooks(repositoryRoot) {
|
|
339
|
-
const gitQuery = spawnSync("git", ["rev-parse", "--git-path", "hooks"], {
|
|
340
|
-
cwd: repositoryRoot,
|
|
341
|
-
encoding: "utf8",
|
|
342
|
-
shell: false,
|
|
343
|
-
timeout: 10_000,
|
|
344
|
-
});
|
|
345
|
-
if (gitQuery.status !== 0) return [];
|
|
346
|
-
const hooksPath = gitQuery.stdout?.trim();
|
|
347
|
-
if (!hooksPath) return [];
|
|
348
|
-
const hooksRoot = path.isAbsolute(hooksPath) ? hooksPath : path.resolve(repositoryRoot, hooksPath);
|
|
349
|
-
return KNOWN_GIT_HOOK_NAMES
|
|
350
|
-
.filter((name) => existsSync(path.join(hooksRoot, name)))
|
|
351
|
-
.map((name, ordinal) => ({
|
|
352
|
-
event: name,
|
|
353
|
-
matcher: "*",
|
|
354
|
-
ordinal,
|
|
355
|
-
role: "native",
|
|
356
|
-
scope: "git",
|
|
357
|
-
target: `git:${name}`,
|
|
358
|
-
timeoutSeconds: null,
|
|
359
|
-
}));
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
function knownGitHooks() {
|
|
363
|
-
return KNOWN_GIT_HOOK_NAMES.map((event, ordinal) => ({
|
|
364
|
-
event,
|
|
365
|
-
matcher: "*",
|
|
366
|
-
ordinal,
|
|
367
|
-
role: "native",
|
|
368
|
-
scope: "git",
|
|
369
|
-
target: `git:${event}`,
|
|
370
|
-
timeoutSeconds: null,
|
|
371
|
-
}));
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
export function auditHooks(options) {
|
|
375
|
-
const repositoryRoot = path.resolve(options.repositoryRoot);
|
|
376
|
-
const hooksRoot = path.join(repositoryRoot, "packages", "claude-dev-env", "hooks");
|
|
377
|
-
const direct = readDirect(path.join(hooksRoot, "hooks.json"), "canonical");
|
|
378
|
-
const hosted = expandHosted(direct, hooksRoot);
|
|
379
|
-
const logical = logicalRecords(direct, hosted);
|
|
380
|
-
const knownGit = knownGitHooks();
|
|
381
|
-
const lifecycle = readLifecycle(options.catalogPath);
|
|
382
|
-
const dispatcherCount = direct.filter(({ role }) => role === "dispatcher").length;
|
|
383
|
-
const installed = options.includeInstalled
|
|
384
|
-
? installedAudit(options.homeDirectory ?? homedir(), repositoryRoot, logical)
|
|
385
|
-
: { claudeCount: 0, codexCount: 0, findings: [], git: [], logical: [], records: [] };
|
|
386
|
-
const allLogical = [...logical, ...installed.logical, ...installed.git];
|
|
387
|
-
const isNonblockingRequired = options.requireNonblocking === true;
|
|
388
|
-
const findings = [
|
|
389
|
-
...duplicateFindings(logical, "canonical"),
|
|
390
|
-
...(options.catalogPath || isNonblockingRequired ? lifecycleFindings(allLogical, lifecycle) : []),
|
|
391
|
-
...(isNonblockingRequired ? nonblockingFindings(allLogical, lifecycle) : []),
|
|
392
|
-
...installed.findings,
|
|
393
|
-
].sort((left, right) => JSON.stringify(left).localeCompare(JSON.stringify(right)));
|
|
394
|
-
const targets = new Set(allLogical.map(({ target }) => target));
|
|
395
|
-
const classified = new Set(lifecycle.map(({ target }) => target));
|
|
396
|
-
return {
|
|
397
|
-
directRegistrations: direct.sort(recordSort),
|
|
398
|
-
findings,
|
|
399
|
-
hostedRegistrations: hosted.sort(recordSort),
|
|
400
|
-
installedRegistrations: installed.records.sort(recordSort),
|
|
401
|
-
installedLogicalRegistrations: installed.logical.sort(recordSort),
|
|
402
|
-
gitRegistrations: installed.git,
|
|
403
|
-
knownGitRegistrations: knownGit,
|
|
404
|
-
lifecycle,
|
|
405
|
-
logicalRegistrations: logical,
|
|
406
|
-
schemaVersion: 1,
|
|
407
|
-
summary: {
|
|
408
|
-
claudeDirectCount: installed.claudeCount,
|
|
409
|
-
codexDirectCount: installed.codexCount,
|
|
410
|
-
directCount: direct.length,
|
|
411
|
-
dispatcherCount,
|
|
412
|
-
effectiveCount: hosted.length,
|
|
413
|
-
findingCount: findings.length,
|
|
414
|
-
gitHookCount: installed.git.length,
|
|
415
|
-
knownGitHookCount: knownGit.length,
|
|
416
|
-
lifecycleCount: lifecycle.length,
|
|
417
|
-
logicalAssociationCount: logical.length,
|
|
418
|
-
unclassifiedCount: options.catalogPath || isNonblockingRequired
|
|
419
|
-
? [...targets].filter((target) => !classified.has(target)).length
|
|
420
|
-
: 0,
|
|
421
|
-
uniqueTargetCount: targets.size,
|
|
422
|
-
},
|
|
423
|
-
registrationEligibility: registrationEligibility(allLogical, lifecycle, isNonblockingRequired),
|
|
424
|
-
};
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
function installedAudit(homeDirectory, repositoryRoot, canonicalLogical) {
|
|
428
|
-
const claude = readInstalled(
|
|
429
|
-
path.join(homeDirectory, ".claude", "settings.json"),
|
|
430
|
-
path.join(homeDirectory, ".claude", "hooks"),
|
|
431
|
-
"claude",
|
|
432
|
-
);
|
|
433
|
-
const codex = readInstalled(
|
|
434
|
-
path.join(homeDirectory, ".codex", "hooks.json"),
|
|
435
|
-
path.join(homeDirectory, ".codex", "hooks"),
|
|
436
|
-
"codex",
|
|
437
|
-
);
|
|
438
|
-
const git = activeGitHooks(repositoryRoot);
|
|
439
|
-
return {
|
|
440
|
-
claudeCount: claude.records.length,
|
|
441
|
-
codexCount: codex.records.length,
|
|
442
|
-
findings: [
|
|
443
|
-
...claude.findings,
|
|
444
|
-
...codex.findings,
|
|
445
|
-
...missingManagedFindings(canonicalLogical, claude.logical, "claude"),
|
|
446
|
-
...missingManagedFindings(canonicalLogical, codex.logical, "codex"),
|
|
447
|
-
],
|
|
448
|
-
git,
|
|
449
|
-
logical: [...claude.logical, ...codex.logical],
|
|
450
|
-
records: [...claude.records, ...codex.records],
|
|
451
|
-
};
|
|
452
|
-
}
|
|
@@ -1,150 +0,0 @@
|
|
|
1
|
-
import assert from "node:assert/strict";
|
|
2
|
-
import { spawnSync } from "node:child_process";
|
|
3
|
-
import {
|
|
4
|
-
mkdirSync,
|
|
5
|
-
mkdtempSync,
|
|
6
|
-
readFileSync,
|
|
7
|
-
rmSync,
|
|
8
|
-
writeFileSync,
|
|
9
|
-
} from "node:fs";
|
|
10
|
-
import { tmpdir } from "node:os";
|
|
11
|
-
import path from "node:path";
|
|
12
|
-
import test from "node:test";
|
|
13
|
-
import { fileURLToPath } from "node:url";
|
|
14
|
-
|
|
15
|
-
const packageRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
|
|
16
|
-
const auditCliPath = path.join(packageRoot, "audit-hooks-cli.mjs");
|
|
17
|
-
|
|
18
|
-
function writeJson(filePath, jsonDocument) {
|
|
19
|
-
writeFileSync(filePath, JSON.stringify(jsonDocument), "utf8");
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function createAuditFixture({
|
|
23
|
-
canonicalHooks = {},
|
|
24
|
-
claudeHooks = { hooks: {} },
|
|
25
|
-
codexHooks = { hooks: {} },
|
|
26
|
-
} = {}) {
|
|
27
|
-
const fixtureRoot = mkdtempSync(path.join(tmpdir(), "hook-audit-cli-"));
|
|
28
|
-
const canonicalHooksRoot = path.join(fixtureRoot, "packages", "claude-dev-env", "hooks");
|
|
29
|
-
const homeDirectory = path.join(fixtureRoot, "home");
|
|
30
|
-
mkdirSync(canonicalHooksRoot, { recursive: true });
|
|
31
|
-
mkdirSync(path.join(homeDirectory, ".claude", "hooks"), { recursive: true });
|
|
32
|
-
mkdirSync(path.join(homeDirectory, ".codex", "hooks"), { recursive: true });
|
|
33
|
-
writeJson(path.join(canonicalHooksRoot, "hooks.json"), { hooks: canonicalHooks });
|
|
34
|
-
writeJson(path.join(homeDirectory, ".claude", "settings.json"), claudeHooks);
|
|
35
|
-
writeJson(path.join(homeDirectory, ".codex", "hooks.json"), codexHooks);
|
|
36
|
-
return { fixtureRoot, homeDirectory };
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function runAuditCli(allArguments) {
|
|
40
|
-
const childProcess = spawnSync(process.execPath, [auditCliPath, ...allArguments], {
|
|
41
|
-
cwd: packageRoot,
|
|
42
|
-
encoding: "utf8",
|
|
43
|
-
env: { ...process.env, HOME: path.join(tmpdir(), "unused-audit-cli-home") },
|
|
44
|
-
shell: false,
|
|
45
|
-
});
|
|
46
|
-
assert.equal(childProcess.error, undefined);
|
|
47
|
-
return childProcess;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
test("writes a JSON audit report to stdout through the CLI", () => {
|
|
51
|
-
const fixture = createAuditFixture({
|
|
52
|
-
canonicalHooks: {
|
|
53
|
-
PreToolUse: [{
|
|
54
|
-
matcher: "Write",
|
|
55
|
-
hooks: [{
|
|
56
|
-
type: "command",
|
|
57
|
-
command: "python3 " + "$" + "{CLAUDE_PLUGIN_ROOT}/hooks/blocking/write_existing_file_blocker.py",
|
|
58
|
-
}],
|
|
59
|
-
}],
|
|
60
|
-
},
|
|
61
|
-
});
|
|
62
|
-
try {
|
|
63
|
-
const childProcess = runAuditCli([
|
|
64
|
-
"--format",
|
|
65
|
-
"json",
|
|
66
|
-
"--repository-root",
|
|
67
|
-
fixture.fixtureRoot,
|
|
68
|
-
]);
|
|
69
|
-
|
|
70
|
-
assert.equal(childProcess.status, 0);
|
|
71
|
-
assert.equal(childProcess.stderr, "");
|
|
72
|
-
const report = JSON.parse(childProcess.stdout);
|
|
73
|
-
assert.equal(report.summary.directCount, 1);
|
|
74
|
-
assert.equal(report.registrationEligibility.status, "not_checked");
|
|
75
|
-
} finally {
|
|
76
|
-
rmSync(fixture.fixtureRoot, { force: true, recursive: true });
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
test("writes the JSON audit report to the requested file through the CLI", () => {
|
|
81
|
-
const fixture = createAuditFixture();
|
|
82
|
-
const reportPath = path.join(fixture.fixtureRoot, "audit-report.json");
|
|
83
|
-
try {
|
|
84
|
-
const childProcess = runAuditCli([
|
|
85
|
-
"--format",
|
|
86
|
-
"json",
|
|
87
|
-
"--output",
|
|
88
|
-
reportPath,
|
|
89
|
-
"--repository-root",
|
|
90
|
-
fixture.fixtureRoot,
|
|
91
|
-
]);
|
|
92
|
-
|
|
93
|
-
assert.equal(childProcess.status, 0);
|
|
94
|
-
assert.equal(childProcess.stdout, "");
|
|
95
|
-
const report = JSON.parse(readFileSync(reportPath, "utf8"));
|
|
96
|
-
assert.equal(report.summary.directCount, 0);
|
|
97
|
-
assert.equal(report.summary.findingCount, 0);
|
|
98
|
-
} finally {
|
|
99
|
-
rmSync(fixture.fixtureRoot, { force: true, recursive: true });
|
|
100
|
-
}
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
test("returns a strict nonblocking failure through the CLI", () => {
|
|
104
|
-
const fixture = createAuditFixture({
|
|
105
|
-
claudeHooks: {
|
|
106
|
-
hooks: {
|
|
107
|
-
SessionStart: [{
|
|
108
|
-
hooks: [{ command: "node cleanup.mjs", type: "command" }],
|
|
109
|
-
}],
|
|
110
|
-
},
|
|
111
|
-
},
|
|
112
|
-
});
|
|
113
|
-
const catalogPath = path.join(fixture.fixtureRoot, "lifecycle.json");
|
|
114
|
-
writeJson(catalogPath, {
|
|
115
|
-
hooks: {
|
|
116
|
-
"external:cleanup.mjs": {
|
|
117
|
-
lifecycle: "delete",
|
|
118
|
-
reason: "The installed entry is retired.",
|
|
119
|
-
replacement: null,
|
|
120
|
-
},
|
|
121
|
-
},
|
|
122
|
-
});
|
|
123
|
-
try {
|
|
124
|
-
const childProcess = runAuditCli([
|
|
125
|
-
"--format",
|
|
126
|
-
"json",
|
|
127
|
-
"--catalog",
|
|
128
|
-
catalogPath,
|
|
129
|
-
"--home",
|
|
130
|
-
fixture.homeDirectory,
|
|
131
|
-
"--installed",
|
|
132
|
-
"--require-nonblocking",
|
|
133
|
-
"--repository-root",
|
|
134
|
-
fixture.fixtureRoot,
|
|
135
|
-
]);
|
|
136
|
-
|
|
137
|
-
assert.equal(childProcess.status, 1);
|
|
138
|
-
assert.equal(childProcess.stderr, "");
|
|
139
|
-
const report = JSON.parse(childProcess.stdout);
|
|
140
|
-
assert.equal(report.registrationEligibility.status, "ineligible");
|
|
141
|
-
assert.equal(
|
|
142
|
-
report.findings.some(({ code, target }) =>
|
|
143
|
-
code === "NONBLOCKING_LIFECYCLE_MISMATCH" && target === "external:cleanup.mjs",
|
|
144
|
-
),
|
|
145
|
-
true,
|
|
146
|
-
);
|
|
147
|
-
} finally {
|
|
148
|
-
rmSync(fixture.fixtureRoot, { force: true, recursive: true });
|
|
149
|
-
}
|
|
150
|
-
});
|