Files
billion-context/tests/hook-command.test.ts
T
ework-agent d55756d4f0 fix(claude): shell-portable SessionStart hook + PATHEXT-aware CLI resolution (#1902)
Two Windows defects in the claude install lane, both reported with repros
in #1902 (bili 0.1.179, Claude Code 2.1.284):

1. The SessionStart hook command carried the install-time node path with a
   PowerShell-only leading '&' for spaced paths. Claude Code 2.1.284 runs
   hooks through cmd.exe, which dies on the '&' ('& was unexpected at this
   time.') — the lane silently never started. #1376 had probe-verified
   PowerShell on 2.1.282, so no single spelling of a spaced command path
   survives both client shells across versions. Root fix: emit a bare
   'node' head (PATH-resolved by every client shell — claude's own npm shim
   requires node on PATH), like the kimi hook already does. Existing
   installs self-heal: refreshOurHookCommands rewrites our owned hook
   commands on the next session-start repin or reinstall.

2. resolveClaudeCli took the raw first where.exe hit, which for npm global
   installs is the extensionless POSIX shim — unspawnable by Node (ENOENT),
   so MCP registration failed at install time. Hits are now ranked the way
   cmd.exe resolves them: PATH order first, PATHEXT order within each
   directory; no-match keeps the old first-hit behavior. A CLAUDE override
   (the reporter's working bypass) still passes through untouched.

Behavior change (intended, disclosed): the emitted claude hook command goes
from '& "<abs node path>" <script>' / '<abs node path> <script>' to
'node <forward-slashed script>'. The absolute-path form also went away, so
a node binary moved after install can no longer strand the hook.

Tests: resolveClaudeCli gains the #1902 repro (extensionless-first npm dir)
plus PATH-vs-PATHEXT precedence pins; the installer round-trip now asserts
the emitted hook carries a bare 'node' head; hook-command.test.ts comments
updated to record both observed client shells. Typecheck + targeted suites
(hook-command, claude-native, kimi-native) green locally; Windows CI runs
the shell integration tests.
2026-10-02 15:08:12 +08:00

177 lines
8.4 KiB
TypeScript

import { test } from "node:test";
import assert from "node:assert/strict";
import { spawnSync } from "node:child_process";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { isOursSessionStartEntry, portableHookCommand } from "../src/plugin-install.ts";
import { rmrf } from "./tmp-rm.ts";
// The SessionStart hook is the one place bili hands a client a SHELL STRING
// rather than an argv array, so the client's shell re-parses our path. The unit
// tests pin the exact spelling; the integration tests below run the produced
// command through every shell the platform actually has, so a "fix" that only
// looks right on paper fails here. See portableHookCommand for the traps.
const EXE = "D:\\Dev\\node\\node.exe";
const JS = "C:\\Users\\u\\AppData\\Local\\Temp\\bili\\claude-native-bootstrap.js";
test("portableHookCommand: plain paths emit a bare token and an unquoted argument", () => {
assert.equal(
portableHookCommand(EXE, [JS]),
"D:/Dev/node/node.exe C:/Users/u/AppData/Local/Temp/bili/claude-native-bootstrap.js",
);
});
test("portableHookCommand: no backslash survives into the command line", () => {
// The escape-eating is the bug; assert on the whole emitted string rather
// than on the pieces, so a future refactor cannot reintroduce one.
for (const cmd of [
portableHookCommand(EXE, [JS]),
portableHookCommand("C:\\Program Files\\nodejs\\node.exe", [JS]),
portableHookCommand(EXE, ["C:\\a b\\c\\bootstrap.js"]),
portableHookCommand("C:\\Program Files\\nodejs\\node.exe", ["C:\\a b\\c\\bootstrap.js"]),
]) {
assert.ok(!cmd.includes("\\"), cmd);
}
});
test("portableHookCommand: an argument with whitespace is quoted (safe in bash, PowerShell and cmd)", () => {
assert.equal(
portableHookCommand(EXE, ["C:\\Program Files\\bili\\bootstrap.js"]),
'D:/Dev/node/node.exe "C:/Program Files/bili/bootstrap.js"',
);
});
test("portableHookCommand: a command path with whitespace falls back to the & call operator", () => {
// No spelling covers a spaced command path in all three shells, and the
// client's shell varies by Claude Code version (cmd on 2.1.284, #1902;
// PowerShell probe-verified on 2.1.282, #1376) — so no SHIPPED hook emits
// this form anymore: claude and kimi both pass a bare `node` head. The
// branch stays pinned for hypothetical spaced-head callers.
assert.equal(
portableHookCommand("C:\\Program Files\\nodejs\\node.exe", [JS]),
'& "C:/Program Files/nodejs/node.exe" C:/Users/u/AppData/Local/Temp/bili/claude-native-bootstrap.js',
);
assert.equal(
portableHookCommand("C:\\Program Files\\nodejs\\node.exe", ["C:\\a b\\c.js"]),
'& "C:/Program Files/nodejs/node.exe" "C:/a b/c.js"',
);
});
test("portableHookCommand: no arguments, and the bare `node` the kimi hook uses", () => {
assert.equal(portableHookCommand("node"), "node");
// The kimi hook resolves `node` through PATH, so it can never need `&` —
// the one shell the fallback would break (cmd) never sees a spaced command.
const kimi = portableHookCommand("node", ["C:\\Users\\u\\.kimi\\plugins\\managed\\billion-context\\dist\\kimi\\bootstrap-hook.js"]);
assert.equal(kimi, "node C:/Users/u/.kimi/plugins/managed/billion-context/dist/kimi/bootstrap-hook.js");
assert.ok(!kimi.startsWith("& "), kimi);
});
test("isOursSessionStartEntry still matches every form the installer can emit", () => {
// Uninstall/reinstall detection keys off this regex; if it drifts from the
// emitted spelling, bili stops recognizing its own hook and leaves orphans.
const entry = (cmd: string) => ({ hooks: [{ command: cmd }] });
for (const cmd of [
portableHookCommand(EXE, [JS]),
portableHookCommand(EXE, ["C:\\a b\\c\\claude-native-bootstrap.js"]),
portableHookCommand("C:\\Program Files\\nodejs\\node.exe", ["C:\\a b\\c\\claude-native-bootstrap.js"]),
]) {
assert.equal(isOursSessionStartEntry(entry(cmd)), true, cmd);
}
assert.equal(isOursSessionStartEntry({ hooks: [{ command: "echo hi" }] }), false);
});
interface Shell {
name: string;
exe: string;
wrap: (command: string) => string[];
/** cmd.exe must receive the hook line VERBATIM: Node re-quotes any argv
* element containing spaces with `\"`, which cmd does not unescape, so the
* default path measures this harness instead of cmd. PowerShell takes the
* line as a single `-Command` argument and needs the normal quoting. */
verbatim?: boolean;
}
/** Every shell that can plausibly run a hook on this platform. Order is
* irrelevant; each is skipped individually when its binary is absent. */
function platformShells(): Shell[] {
if (process.platform === "win32") {
const sysRoot = process.env.SystemRoot ?? "C:\\Windows";
const ps = path.join(sysRoot, "System32", "WindowsPowerShell", "v1.0", "powershell.exe");
return [
{ name: "powershell", exe: fs.existsSync(ps) ? ps : "powershell.exe", wrap: (c) => ["-NoProfile", "-NonInteractive", "-Command", c] },
{ name: "cmd", exe: process.env.ComSpec ?? "cmd.exe", wrap: (c) => ["/d", "/c", c], verbatim: true },
];
}
return [
{ name: "sh", exe: "sh", wrap: (c) => ["-c", c] },
{ name: "bash", exe: "bash", wrap: (c) => ["-c", c] },
];
}
function runInShell(shell: Shell, command: string): { status: number | null; out: string; missing: boolean } {
const r = spawnSync(shell.exe, shell.wrap(command), {
encoding: "utf8",
windowsHide: true,
windowsVerbatimArguments: shell.verbatim === true,
});
return {
status: r.status,
out: `${r.stdout ?? ""}${r.stderr ?? ""}`,
missing: r.error !== undefined && (r.error as NodeJS.ErrnoException).code === "ENOENT",
};
}
test("the emitted command really runs: bare token + spaced argument, through every shell here", () => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), "bili-hookcmd-"));
// A space in the directory name forces the argument to be quoted — the
// common real case, since Windows temp paths and usernames both carry them.
const dir = path.join(root, "probe dir");
fs.mkdirSync(dir, { recursive: true });
const probe = path.join(dir, "hook-probe.js");
fs.writeFileSync(probe, "process.stdout.write('HOOKOK');");
try {
const command = portableHookCommand(process.execPath, [probe]);
assert.ok(!command.includes("\\"), command);
const ran: string[] = [];
for (const shell of platformShells()) {
// `& "spaced head"` is the PowerShell call-operator form. cmd never
// sees it in production (hooks resolve a bare `node`; see the design
// note in plugin-install.ts) and cannot parse `&` — so don't demand
// cmd compatibility for it. The dedicated PowerShell test below
// covers that form where it is actually used.
if (shell.name === "cmd" && command.startsWith("& ")) continue;
const r = runInShell(shell, command);
if (r.missing) continue;
assert.equal(r.status, 0, `${shell.name} exited ${r.status} for: ${command}\n${r.out}`);
assert.match(r.out, /HOOKOK/, `${shell.name} did not run the probe: ${command}\n${r.out}`);
ran.push(shell.name);
}
assert.ok(ran.length > 0, "no shell available to verify against");
} finally {
rmrf(root);
}
});
test("the & fallback really runs under PowerShell", { skip: process.platform !== "win32" }, () => {
const ps = platformShells().find((s) => s.name === "powershell");
assert.ok(ps, "powershell entry missing");
const root = fs.mkdtempSync(path.join(os.tmpdir(), "bili-hookamp-"));
try {
// A spaced directory holding a runnable stands in for a node.exe
// installed under `C:\Program Files` — same shape, no 100MB copy.
const dir = path.join(root, "Program Files dir");
fs.mkdirSync(dir, { recursive: true });
const shim = path.join(dir, "hook-probe.cmd");
fs.writeFileSync(shim, "@echo off\r\necho HOOKOK\r\n");
const command = portableHookCommand(shim, []);
assert.ok(command.startsWith('& "'), command);
const r = runInShell(ps, command);
assert.equal(r.status, 0, `${r.status} for: ${command}\n${r.out}`);
assert.match(r.out, /HOOKOK/, `${command}\n${r.out}`);
} finally {
rmrf(root);
}
});