Skip to content

A self-organizing Obsidian vault that gives AI coding agents persistent memory.

fix: give test hook spawns a 30 s timeout and report a kill as a kill (#350)

Brenno Ferraridf40ec389021
diff --git a/.claude/scripts/tests/_helpers.test.ts b/.claude/scripts/tests/_helpers.test.ts
index f9cdba1..6354a04 100644
--- a/.claude/scripts/tests/_helpers.test.ts
+++ b/.claude/scripts/tests/_helpers.test.ts
@@ -15,7 +15,7 @@ import { chmodSync, existsSync, mkdirSync, mkdtempSync, writeFileSync } from "no
import { tmpdir } from "node:os";
import { join } from "node:path";
-import { leakedTempDirs, rmTemp, waitFor } from "./_helpers.ts";
+import { leakedTempDirs, rmTemp, runScript, waitFor } from "./_helpers.ts";
describe("waitFor", () => {
test("returns as soon as the condition holds", async () => {
@@ -132,3 +132,33 @@ describe("rmTemp", () => {
},
);
});
+
+describe("runScript", () => {
+ // A spawn killed by the timeout used to come back as empty stdout and exit
+ // -1, so the test failed on whatever it parsed next: "hook stdout is not one
+ // JSON object" on Windows CI, where nothing said a timeout had fired (#348).
+ /** Run `fn` on a script named `name` holding `body`, in a temp folder removed after. */
+ const withScript = (name: string, body: string, fn: (script: string) => void) => {
+ const dir = mkdtempSync(join(tmpdir(), "run-script-"));
+ try {
+ const script = join(dir, name);
+ writeFileSync(script, body);
+ fn(script);
+ } finally {
+ rmTemp(dir);
+ }
+ };
+
+ test("a script that outlives the timeout is reported as killed, not as empty output", () => {
+ // It would sleep a minute; the kill makes that free, and no slow runner finishes it in time.
+ withScript("sleeps.ts", "setTimeout(() => {}, 60_000);\n", (script) =>
+ assert.throws(() => runScript(script, null, {}, { timeoutMs: 300 }), /sleeps\.ts was killed after 0\.3 s/),
+ );
+ });
+
+ test("a script that finishes in time returns its output (the control)", () => {
+ withScript("prints.ts", 'process.stdout.write("done");\n', (script) =>
+ assert.equal(runScript(script, null, {}, { timeoutMs: 10_000 }).stdout, "done"),
+ );
+ });
+});
diff --git a/.claude/scripts/tests/_helpers.ts b/.claude/scripts/tests/_helpers.ts
index c7179bd..f4f57fa 100644
--- a/.claude/scripts/tests/_helpers.ts
+++ b/.claude/scripts/tests/_helpers.ts
@@ -32,7 +32,7 @@
import { spawnSync } from "node:child_process";
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
-import { join } from "node:path";
+import { basename, join } from "node:path";
import { parseHookContext } from "../lib/hook-io.ts";
import { REFRESH_SWITCH } from "../lib/qmd-refresh.ts";
@@ -59,11 +59,37 @@ export type RunResult = {
*/
export const hookContext = (stdout: string): string => parseHookContext(stdout, "SessionStart");
+/**
+ * How long a hook spawn may take before it is killed. One session-start spawn
+ * takes 5-12 s on Windows CI (#349), so the old 10 s left under a second of
+ * margin and failed a run on runner variance alone (#348). It also bounds what
+ * a hung hook costs: each spawn that hangs takes this long, one at a time.
+ */
+export const SCRIPT_TIMEOUT_MS = 30_000;
+
+/**
+ * Throw if `proc` was killed by its timeout. A killed run has no output to
+ * judge: say so, rather than hand the caller empty stdout to fail on as
+ * something else (#348).
+ */
+export function assertNotKilled(
+ proc: { readonly signal: NodeJS.Signals | null; readonly stderr?: string | null },
+ label: string,
+ timeoutMs: number,
+): void {
+ if (proc.signal === null) return;
+ const tail = (proc.stderr ?? "").trimEnd().split("\n").slice(-5).join("\n");
+ const detail = tail ? `: ${tail}` : "";
+ throw new Error(`${label} was killed after ${timeoutMs / 1000} s (${proc.signal})${detail}`);
+}
+
export function runScript(
scriptPath: string,
stdin: string | object | null,
envOverrides?: Readonly<Record<string, string>>,
+ opts: { readonly timeoutMs?: number } = {},
): RunResult {
+ const timeoutMs = opts.timeoutMs ?? SCRIPT_TIMEOUT_MS;
const input =
stdin === null
? ""
@@ -84,10 +110,11 @@ export function runScript(
{
input,
encoding: "utf-8",
- timeout: 10_000,
+ timeout: timeoutMs,
env,
},
);
+ assertNotKilled(proc, basename(scriptPath), timeoutMs);
return {
stdout: proc.stdout ?? "",
stderr: proc.stderr ?? "",
diff --git a/.claude/scripts/tests/refresh-switch.test.ts b/.claude/scripts/tests/refresh-switch.test.ts
index 73a37e3..73a5bcb 100644
--- a/.claude/scripts/tests/refresh-switch.test.ts
+++ b/.claude/scripts/tests/refresh-switch.test.ts
@@ -18,7 +18,7 @@ import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
import { REFRESH_SWITCH } from "../lib/qmd-refresh.ts";
-import { rmTemp, runScript } from "./_helpers.ts";
+import { assertNotKilled, rmTemp, runScript, SCRIPT_TIMEOUT_MS } from "./_helpers.ts";
const testsDir = dirname(fileURLToPath(import.meta.url));
@@ -46,8 +46,9 @@ test("the preload forces the switch off, even over an exported 'on' or ''", () =
"-e",
`process.stdout.write(String(process.env.${REFRESH_SWITCH}))`,
],
- { encoding: "utf8", timeout: 30_000, env: { ...process.env, [REFRESH_SWITCH]: exported } },
+ { encoding: "utf8", timeout: SCRIPT_TIMEOUT_MS, env: { ...process.env, [REFRESH_SWITCH]: exported } },
);
+ assertNotKilled(r, "the preload probe", SCRIPT_TIMEOUT_MS);
assert.equal(r.stdout, "off", `with ${REFRESH_SWITCH}=${JSON.stringify(exported)} exported`);
}
});
diff --git a/.claude/scripts/tests/tidy-fix.test.ts b/.claude/scripts/tests/tidy-fix.test.ts
index f074a92..07a2b62 100644
--- a/.claude/scripts/tests/tidy-fix.test.ts
+++ b/.claude/scripts/tests/tidy-fix.test.ts
@@ -20,7 +20,7 @@ import { join, dirname, resolve } from "node:path";
import { tmpdir } from "node:os";
import { fileURLToPath } from "node:url";
-import { rmTemp } from "./_helpers.ts";
+import { assertNotKilled, rmTemp, SCRIPT_TIMEOUT_MS } from "./_helpers.ts";
import { projectSlug } from "../tidy-fix.ts";
const SCRIPT = resolve(
@@ -42,6 +42,7 @@ function run(
["--disable-warning=ExperimentalWarning", "--experimental-strip-types", SCRIPT, ...args],
{
encoding: "utf-8",
+ timeout: SCRIPT_TIMEOUT_MS,
env: {
...process.env,
CLAUDE_PROJECT_DIR: root,
@@ -50,6 +51,7 @@ function run(
},
},
);
+ assertNotKilled(r, "tidy-fix", SCRIPT_TIMEOUT_MS);
return { stdout: r.stdout ?? "", code: r.status ?? -1 };
}