iai-personal-memory-engine icon indicating copy to clipboard operation
iai-personal-memory-engine copied to clipboard

Windows: Claude Code CLI rewrites hook command to unquoted "C:\Program Files\Git\bin\bash.exe", breaking every hook

Open oscampo opened this issue 3 months ago • 0 comments

Summary

capture-hooks install on Windows writes hook commands as f"bash {path}", e.g. bash C:\Users\me\.claude\hooks\iai-mcp-turn-capture.sh. That's correct as written. But Claude Code CLI itself resolves the bare bash token to its absolute path and persists the resolved string back into settings.json — unquoted. Git for Windows' default install path is C:\Program Files\Git\bin\bash.exe, which has a space, so the unquoted rewrite corrupts the command:

SessionStart:startup hook error
Failed with non-blocking status code: /usr/bin/bash: line 1: C:Program: command not found
UserPromptSubmit hook error
Failed with non-blocking status code: /usr/bin/bash: line 1: C:Program: command not found

Confirmed live on Windows 11, Claude Code CLI v2.1.234. Every hook (Stop, UserPromptSubmit, SessionStart, per-turn recall) failed on every single turn, with zero ambient captures reaching the store, on an install that capture-hooks status reported as fully WIRED — the hooks were correctly registered, just broken at execution.

How it was diagnosed

Confirmed iai-mcp itself never writes an absolute bash path — grepped the whole source tree, zero matches for "Program Files" or "bash.exe" — so the broken string in settings.json had to come from Claude Code CLI's own hook-command resolution, not from this project. Manually rewrote the four hook commands in settings.json to "C:\Program Files\Git\bin\bash.exe" "C:\...\iai-mcp-turn-capture.sh" (quoted, both segments), opened a fresh Claude Code CLI session, and all hook error messages disappeared. Verified real encrypted content landed in ~/.iai-mcp/.deferred-captures/ tagged with that exact session's session_id.

A second, compounding bug

cmd_capture_hooks_install's "already wired" checks only look for the hook script's filename as a substring of the existing command — which a broken command still contains. So re-running capture-hooks install on an already-broken install does nothing: it prints "already has X hook — no change" and leaves the corrupted command in place forever. There's no self-healing path short of manual JSON surgery (or full uninstall/reinstall, which I didn't test but likely hits the same bug on reinstall).

Fix applied (fork, commit 9a0ea77b, main)

  1. _hook_shell_command(): on Windows, resolves bash via shutil.which and writes the command pre-quoted ('"{bash_path}" "{script}"'), so the string iai-mcp writes is already safe and doesn't depend on Claude Code CLI's own resolution/rewrite behavior. POSIX behavior unchanged.
  2. _upsert_hook_entry(): replaces the four "already wired" boolean checks with one helper that compares the full command (not just the marker substring) and self-heals a stale/broken entry in place, so a future breaking change in Claude Code CLI's resolution, or any other cause of a corrupted command, is fixed by simply re-running capture-hooks install instead of requiring manual settings.json edits.

Unit-tested standalone (fresh registration, no-op when already correct, self-heal when marker present but command differs) and verified end-to-end on the real machine as described above.

Happy to open the equivalent PR against CodeAbra/main if useful (same as #113/#114).

oscampo avatar Aug 18 '26 16:08 oscampo