// src/hook-command.ts // The hook commands node9 writes into an agent's config, read back: is this // entry node9's, which subcommand does it run, and does it still point at a // node9 that exists. Kept apart from setup.ts so the hook itself (check.ts) // can ask without loading setup and its prompt and TOML dependencies. import fs from 'fs'; import { isShimTargetStale } from './node9-shim'; /** The hook shim's path inside a command: `/.node9/bin/hook`. */ export const SHIM_COMMAND_PATH = /[/\\]\.node9[/\\]bin[/\\]hook$/; /** * A previously-installed Node9 hook command can outlive the install that * wrote it — `npm uninstall -g node9-ai` deletes the cli.js but leaves the * stored command in `~/.claude/settings.json` pointing at a vanished path. * Claude Code then crashes every tool call with `node:internal/modules/cjs/loader`. * * Returns true when the command references at least one absolute path that * no longer exists on disk. Bare commands like `node9 check` (no `/`) * resolve via $PATH at runtime and are never considered stale here. */ export function isStaleHookCommand(command: string): boolean { if (!command) return false; // Pre-fix hook commands were unquoted (`/path/to/node /path/to/cli.js // check`) so a whitespace split produced clean tokens. The post-fix // form is quoted (`"/path/.../node" "/path/.../cli.js" check`) where // a whitespace-split would chop a single quoted token in two if the // path itself contained a space. Match both forms by walking quoted // and bare runs in one pass. const tokens: string[] = []; const re = /"([^"]*)"|(\S+)/g; let m: RegExpExecArray | null; while ((m = re.exec(command)) !== null) tokens.push(m[1] ?? m[2] ?? ''); for (const tok of tokens) { // Absolute path on POSIX (`/...`) or Windows (`C:/...` after the // forward-slash normalisation done by fullPathCommand). Other tokens // (subcommand names like `check`, `log`) are skipped. if (!tok.startsWith('/') && !/^[A-Za-z]:\//.test(tok)) continue; if (!fs.existsSync(tok)) return true; if (SHIM_COMMAND_PATH.test(tok) && isShimTargetStale(tok)) return true; } return false; } // Matches hook commands written by node9 in any of these forms: // node9 check (global install, NODE9_TESTING) // /path/to/node9 check (global install, unquoted) // "/path/to/node9" check (global install, post-#185 quoted) // /path/to/node /path/to/cli.js check (npm link / local, pre-fix) // "/path/to/node" "/path/to/cli.js" check (npm link / local, quoted) // The optional `"?` after the binary/script name is the key: fullPathCommand // quotes paths (since #185, to survive spaces in $HOME), so a global binary // install emits `"/path/node9" check`. Without matching that, a freshly // wired install reads back as "not wired" — and setup, whose dedup check // uses this same predicate, appends duplicate hooks on every re-run. // The word-boundary prefix (?:^|[\s/\\"]) still blocks false matches on // binaries that merely contain "node9" (e.g. mynode9). export function isNode9Hook(cmd: string | undefined): boolean { if (!cmd) return false; return ( /(?:^|[\s/\\"])node9"? (?:check|log)/.test(cmd) || /(?:^|[\s/\\])cli\.js"? (?:check|log)/.test(cmd) || // The shim form: "/.node9/bin/hook" check. Without this alternative a // freshly wired install reads back as "not wired" and setup appends // duplicate hooks on every re-run — the same trap the quoted-binary form // fell into after #185. /[/\\]\.node9[/\\]bin[/\\]hook"? (?:check|log)/.test(cmd) ); } /** The subcommand of a node9 hook command ('check' | 'log'), or null when the command is not node9's. */ export function node9HookSubcommand(cmd: string | undefined): 'check' | 'log' | null { if (!cmd || !isNode9Hook(cmd)) return null; const m = /(?:node9|cli\.js|[/\\]hook)"? (check|log)\b/.exec(cmd); return m ? (m[1] as 'check' | 'log') : null; }