Repository navigation
fix(claude): shell-portable SessionStart hook + PATHEXT-aware CLI resolution (#1902) - #1905
Conversation
…olution (#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.
📦 Built Package ArtifactBranch: Option A — Install this exact build (recommended)npm install -g billion-context@0.0.1896-pr.1905.b129983This exact version is immutable and reproducible. The rolling Option B — Download artifact
tar xzf billion-context-pr1905.tgz
npm install -g packageInstall instructions are refreshed on each push. |
[bot] 🏷 Review 完成(PR #1905,Fixes #1902)。结论:可合并——代码、回归、测试三关均过;CI 尚余 2 个 Windows test job(win22/win24)在跑,绿了即可合。无小问题需要补 commit,也未发现需立项的新问题。 ① 是什么问题:Windows 上 ② 什么原因导致的:根因不是「 ③ 怎么修复的:hook 改为 bare ④ 是否有回归问题:审过既有行为,无意回归为零:
⑤ 是否破坏缓存:不涉及。未触碰 prefix cache、会话持久化、更新节流。 ⑥ 是否改了设计方向:无转向,反而是收敛——hook 生成回到最保守的可解析形态(bare token),与 kimi lane 统一,未来客户端再换 shell 也不会翻车。 ⑦ 是否引入了新的配置:无。 测试有效性:新断言有牙——resolveClaudeCli 五个形态全钉(#1902 复现/同目录 PATHEXT 优先/PATH 序胜扩展名/无匹配回退/PATHEXT 注入);installer round-trip 断言发出的 hook 以 本地验证(rebased head d55756d):typecheck clean;定向套件(hook-command/claude-native/kimi-native)78 tests,69 pass,0 fail,9 skip(win32/live-e2e gated)——与 PR pre-flight 完全一致。CI:11 check-runs 已绿(build-artifact、version-guard、codex/opencode/pi/dsh real-lane e2e、hermetic registry e2e ubuntu+win、ubuntu 22/24 test),余 2 个 windows-latest test job 运行中——恰是本 fix 针对的 Windows shell 集成矩阵,等其绿完。 合并判定:可合。等最后两个 Windows job 转绿后由人工合并即可。 |
What
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 never started under cmd.exe.
portableHookCommand()emitted the install-time node path with a leading PowerShell-only&for spaced paths (& "C:/Program Files/nodejs/node.exe" <script>). Claude Code 2.1.284 runs hooks through non-interactive cmd.exe, which dies on the leading&(& was unexpected at this time.) — silently, so the lane just never came up. #1376 had probe-verified PowerShell on Claude Code 2.1.282, while #1902 measured cmd.exe on 2.1.284: the client's hook shell is not stable across versions, and no single spelling of a spaced command path survives both shells ("quoted" head argsis a parse error in PowerShell;& "quoted" head argsis an error in cmd).Root fix: stop emitting a spaced command token at all — the claude hook now carries a bare
nodehead (node C:/.../dist/claude-native-bootstrap.js), PATH-resolved by every client shell, exactly like the kimi hook already does. Soundness invariant: claude cannot run withoutnoderesolvable on PATH (its npm shim invokes barenode), and the hook inherits claude's environment. Bonus: the hook no longer pins an absolute node binary, so a node moved/upgraded after install can no longer strand it.Self-heal for existing installs:
refreshOurHookCommands()rewrites our owned hook commands (matched on the bootstrap script suffix, not the exe) on the next session-start repin or reinstall — no manual settings edit needed.2. MCP registration ENOENTed at install time.
resolveClaudeCli()took the raw firstwhere.exehit, which for npm global installs is the extensionless POSIX shim (...\npm\claude) — unspawnable by Node. Hits are now ranked the way cmd.exe itself resolves them: PATH order first, PATHEXT order within each directory (envPATHEXT, default.COM;.EXE;.BAT;.CMD); when nothing carries a PATHEXT extension the old first-hit behavior stands (the downstream ENOENT stays truthful). ACLAUDE=override still passes through untouched (the reporter's working bypass remains valid).Behavior change (intended, disclosed)
& "<abs node path>" <script>/<abs node path> <script>→node <forward-slashed script>. Old value → new value as shown; cause: the old form was unparsable by whichever of cmd/PowerShell the client picks per version (Windows: plugin install claude 的 SessionStart hook 带 PowerShell "&" 前缀,cmd.exe 下静默失败,claude lane 无法自举(附修复建议) #1902 vs [bug] Windows: SessionStart hook 命令不跨 shell —— 反斜杠路径与带引号的命令 token 都静默失败(claude + kimi lane) #1376 evidence above).resolveClaudeClifirst-hit → PATHEXT-ranked selection (extensionless shims are skipped in favor of.cmd/.exesiblings in the same directory; earlier PATH directories still win over later ones regardless of extension).pathextis a test-injection parameter only.Coordination
claudeInstall()in the same file but disjoint lines (no hunk overlap with either edit here); semantically complementary — itsmcp remove/mcp adddance runs through the sameresolveClaudeClicall and inherits this fix. Merges cleanly in either order.Pre-flight (rebased onto current master 562d0e6)
npm run typecheck— cleantests/hook-command.test.ts tests/claude-native.test.ts tests/kimi-native.test.ts— 78 tests, 69 pass, 0 fail, 9 skip (win32-only + live-e2e gated)npm run build— success.cmdwins), same-dir PATHEXT precedence, PATH-order-beats-extension, no-match fallback, injectable PATHEXT; installer round-trip asserts the emitted hook carries a barenodehead. Full suite + Windows shell integration left to CI.Fixes #1902