hookset 1.1.0 → 1.1.1
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/verify-before-stop.mjs +85 -14
- package/package.json +1 -1
|
@@ -29,12 +29,15 @@ import {
|
|
|
29
29
|
import { tmpdir } from "node:os";
|
|
30
30
|
import { join } from "node:path";
|
|
31
31
|
|
|
32
|
-
import { globToRegExp } from "./guard-bash.mjs";
|
|
32
|
+
import { commands, globToRegExp } from "./guard-bash.mjs";
|
|
33
33
|
import { isTestCommand } from "./stop-after-3.mjs";
|
|
34
34
|
|
|
35
35
|
const DOCS = /\.(md|mdx|txt)$/i;
|
|
36
36
|
|
|
37
|
-
/**
|
|
37
|
+
/**
|
|
38
|
+
* The uncommitted paths `git status` lists, staged, unstaged and untracked, relative to the repo
|
|
39
|
+
* root. A rename lists both sides, so code renamed to a doc still counts; a copy only its new path.
|
|
40
|
+
*/
|
|
38
41
|
export function changedPaths(dir) {
|
|
39
42
|
const r = spawnSync(
|
|
40
43
|
"git",
|
|
@@ -47,7 +50,8 @@ export function changedPaths(dir) {
|
|
|
47
50
|
for (let i = 0; i < parts.length; i++) {
|
|
48
51
|
const entry = parts[i];
|
|
49
52
|
paths.push(entry.slice(3));
|
|
50
|
-
// A rename or copy is followed by its old path
|
|
53
|
+
// A rename or copy is followed by its old path. A rename removed it; a copy left it as it was.
|
|
54
|
+
if (entry[0] === "R" && parts[i + 1]) paths.push(parts[i + 1]);
|
|
51
55
|
if (entry[0] === "R" || entry[0] === "C") i++;
|
|
52
56
|
}
|
|
53
57
|
return paths;
|
|
@@ -59,8 +63,18 @@ export function counts(path, ignore = []) {
|
|
|
59
63
|
return !DOCS.test(rel) && !ignore.some((g) => globToRegExp(g).test(rel));
|
|
60
64
|
}
|
|
61
65
|
|
|
66
|
+
/** The work tree's root, so a hook whose cwd is a package still reads root-relative paths. */
|
|
67
|
+
function rootOf(dir) {
|
|
68
|
+
const r = spawnSync("git", ["rev-parse", "--show-toplevel"], {
|
|
69
|
+
cwd: dir,
|
|
70
|
+
encoding: "utf8",
|
|
71
|
+
});
|
|
72
|
+
return r.status === 0 && r.stdout.trim() ? r.stdout.trim() : dir;
|
|
73
|
+
}
|
|
74
|
+
|
|
62
75
|
/** One hash over every uncommitted code path and its content, or null when there is none. */
|
|
63
|
-
export function fingerprint(
|
|
76
|
+
export function fingerprint(cwd, ignore = []) {
|
|
77
|
+
const dir = rootOf(cwd);
|
|
64
78
|
const paths = changedPaths(dir)
|
|
65
79
|
.filter((p) => counts(p, ignore))
|
|
66
80
|
.sort();
|
|
@@ -93,9 +107,33 @@ function passedHash(session) {
|
|
|
93
107
|
}
|
|
94
108
|
}
|
|
95
109
|
|
|
96
|
-
/**
|
|
97
|
-
|
|
98
|
-
|
|
110
|
+
/**
|
|
111
|
+
* True when the shell's exit status is the test run's own: the test is the last command and no
|
|
112
|
+
* `|`, `||`, `;` or `&` can hide its failure or run an edit after it. `cd pkg && npx vitest run`
|
|
113
|
+
* proves a pass; `npm test || true`, `npm test | tail` and `npm test && sed -i … a.ts` do not.
|
|
114
|
+
*/
|
|
115
|
+
export function provesPass(command) {
|
|
116
|
+
// `2>&1` merges streams; its `&` is not a background launch.
|
|
117
|
+
const merged = String(command).replace(/\d*>&\d+/g, " ");
|
|
118
|
+
const bare = merged
|
|
119
|
+
.replace(/'[^']*'|"(?:\\.|[^"\\])*"/g, '""')
|
|
120
|
+
.replaceAll("&&", " ");
|
|
121
|
+
if (/[|;&\n]/.test(bare)) return false;
|
|
122
|
+
const last = commands(merged).at(-1);
|
|
123
|
+
return Boolean(last) && isTestCommand(last.join(" "));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* PostToolUse: a passing foreground test run (`{ command, failed, background }`) remembers what
|
|
128
|
+
* the code looked like. A background launch returns before its tests finish. Prints nothing.
|
|
129
|
+
*/
|
|
130
|
+
export function recordPass(
|
|
131
|
+
session,
|
|
132
|
+
dir,
|
|
133
|
+
{ command, failed, background = false },
|
|
134
|
+
ignore = [],
|
|
135
|
+
) {
|
|
136
|
+
if (failed || background || !provesPass(command)) return;
|
|
99
137
|
const fp = fingerprint(dir, ignore);
|
|
100
138
|
// A pass with nothing uncommitted proves the committed tree; forget any older fingerprint.
|
|
101
139
|
if (fp) writeFileSync(stateFile(session), JSON.stringify({ hash: fp.hash }));
|
|
@@ -114,7 +152,7 @@ export function decideStop(session, dir, active, ignore = []) {
|
|
|
114
152
|
decision: "block",
|
|
115
153
|
reason:
|
|
116
154
|
`[verify-before-stop] Code changed since the last passing test run (${shown}). ` +
|
|
117
|
-
"Run the tests that cover it, or the repo's verify skill, and say what passed. " +
|
|
155
|
+
"Run the tests that cover it, or the repo's verify skill, in the foreground as the last command with no pipe, `||`, `;` or `&` after it, and say what passed. " +
|
|
118
156
|
"If they cannot run yet (you need an answer first), say that in your reply and end the turn; this hook will not stop you twice.",
|
|
119
157
|
};
|
|
120
158
|
}
|
|
@@ -176,22 +214,52 @@ function selfTest() {
|
|
|
176
214
|
bad.push("the second stop of a turn should always pass");
|
|
177
215
|
if (decideStop(session, dir, false, ["*.ts"]))
|
|
178
216
|
bad.push("a verifyIgnore glob should not count");
|
|
179
|
-
recordPass(session, dir, "npm run build", false);
|
|
217
|
+
recordPass(session, dir, { command: "npm run build", failed: false });
|
|
180
218
|
if (!decideStop(session, dir, false)) bad.push("a build is not a test run");
|
|
181
|
-
recordPass(session, dir, "npx vitest run", true);
|
|
219
|
+
recordPass(session, dir, { command: "npx vitest run", failed: true });
|
|
182
220
|
if (!decideStop(session, dir, false))
|
|
183
221
|
bad.push("a failing test run is not a pass");
|
|
184
|
-
recordPass(session, dir, "npx vitest run", false);
|
|
222
|
+
recordPass(session, dir, { command: "npx vitest run", failed: false });
|
|
185
223
|
if (decideStop(session, dir, false))
|
|
186
224
|
bad.push("a passing run over this code should end the turn");
|
|
187
225
|
writeFileSync(join(dir, "a.ts"), "export const a = 2;\n");
|
|
188
226
|
if (!decideStop(session, dir, false))
|
|
189
227
|
bad.push("an edit after the pass should send the turn back");
|
|
228
|
+
for (const command of [
|
|
229
|
+
"npm test || true",
|
|
230
|
+
"npx vitest run 2>&1 | tail -5",
|
|
231
|
+
"npm test; echo done",
|
|
232
|
+
"npm test && sed -i s/2/3/ a.ts",
|
|
233
|
+
]) {
|
|
234
|
+
recordPass(session, dir, { command, failed: false });
|
|
235
|
+
if (!decideStop(session, dir, false))
|
|
236
|
+
bad.push(`"${command}" cannot prove a pass`);
|
|
237
|
+
}
|
|
238
|
+
recordPass(session, dir, { command: "npm test", failed: false, background: true });
|
|
239
|
+
if (!decideStop(session, dir, false))
|
|
240
|
+
bad.push("a background launch is not a finished run");
|
|
241
|
+
mkdirSync(join(dir, "pkg"), { recursive: true });
|
|
242
|
+
writeFileSync(join(dir, "pkg", "b.ts"), "export const b = 1;\n");
|
|
243
|
+
recordPass(session, join(dir, "pkg"), {
|
|
244
|
+
command: "npx vitest run 2>&1",
|
|
245
|
+
failed: false,
|
|
246
|
+
});
|
|
247
|
+
if (decideStop(session, join(dir, "pkg"), false))
|
|
248
|
+
bad.push("a pass from a package directory should cover the same tree");
|
|
249
|
+
writeFileSync(join(dir, "pkg", "b.ts"), "export const b = 2;\n");
|
|
250
|
+
if (!decideStop(session, join(dir, "pkg"), false))
|
|
251
|
+
bad.push("an edit seen from a package directory should send the turn back");
|
|
252
|
+
git("add", "-A");
|
|
253
|
+
git("-c", "user.email=t@t", "-c", "user.name=t", "commit", "-q", "-m", "code");
|
|
254
|
+
git("mv", "a.ts", "a.md");
|
|
255
|
+
rmSync(stateFile(session), { force: true });
|
|
256
|
+
if (!decideStop(session, dir, false))
|
|
257
|
+
bad.push("renaming code to a doc removes code and should send the turn back");
|
|
190
258
|
|
|
191
259
|
rmSync(stateFile(session), { force: true });
|
|
192
260
|
rmSync(dir, { recursive: true, force: true });
|
|
193
261
|
process.stdout.write(
|
|
194
|
-
bad.length ? `${bad.join("\n")}\n${bad.length} failing\n` : "ok:
|
|
262
|
+
bad.length ? `${bad.join("\n")}\n${bad.length} failing\n` : "ok: 17 cases\n",
|
|
195
263
|
);
|
|
196
264
|
process.exit(bad.length ? 1 : 0);
|
|
197
265
|
}
|
|
@@ -227,8 +295,11 @@ else if (isMain) {
|
|
|
227
295
|
recordPass(
|
|
228
296
|
payload.session_id,
|
|
229
297
|
dir,
|
|
230
|
-
|
|
231
|
-
|
|
298
|
+
{
|
|
299
|
+
command,
|
|
300
|
+
failed: failedResponse(payload),
|
|
301
|
+
background: payload.tool_input?.run_in_background === true,
|
|
302
|
+
},
|
|
232
303
|
ignore,
|
|
233
304
|
);
|
|
234
305
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hookset",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Git hooks presets for AI coding agents: lefthook presets pulled by a pinned tag, a Claude Code hook pack that denies skipped hooks, and `hookset init` and `check`. No dependency outside the burgee family. A husky migration path.",
|
|
6
6
|
"keywords": [
|