From c412893ddb59958e8a04e188b4fabe7483c00673 Mon Sep 17 00:00:00 2001 From: Mohammed Alkindi Date: Fri, 13 Nov 2026 23:15:47 -0800 Subject: [PATCH] fix(setup): quote forward-slash hook paths for Claude Code on Windows Claude Code runs hook commands through Git Bash on Windows, which strips the backslashes from a native path (C:\Users\x\...\prime.sh becomes C:Usersx...prime.sh, exit 127). Claude hooks fail open, so memory was silently never primed or recalled. Emit a quoted forward-slash path on Windows; other platforms are unchanged. --- CHANGELOG.md | 6 ++++ internal/memory/setup/claude.go | 3 +- internal/memory/setup/claude_test.go | 45 +++++++++++++++++++++++++- internal/memory/setup/settings.go | 23 ++++++++++--- internal/memory/setup/settings_test.go | 2 +- 5 files changed, 71 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 96661716e..2838389ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -73,6 +73,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/). stanza. `brew` commands that load the cask printed "Calling `postflight` is deprecated! Use `postflight_steps` instead." on every invocation. The quarantine strip now runs through the declarative `postflight_steps` DSL. +- `mnemon setup --target claude-code` on Windows registers hook commands as + quoted forward-slash paths (`"C:/Users/x/.claude/hooks/mnemon/prime.sh"`). + Claude Code runs hook commands through Git Bash, which stripped the + backslashes from the old `C:\Users\x\...\prime.sh` form, so the hooks failed + with "command not found" and memory was never primed or recalled. Other + platforms are unchanged. - Entity extraction no longer treats numbers, versions, and addresses with no letter (`0.52`, `127.0.0.1`) or dotted abbreviations (`e.g`, `i.e`) as file paths, and no longer admits ordinary words written in capitals (`NEVER`, diff --git a/internal/memory/setup/claude.go b/internal/memory/setup/claude.go index f71858218..7d1f6da77 100644 --- a/internal/memory/setup/claude.go +++ b/internal/memory/setup/claude.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "path/filepath" + "runtime" "github.com/mnemon-dev/mnemon/internal/memory/setup/assets" ) @@ -147,7 +148,7 @@ func ClaudeRegisterHooks(configDir string, sel HookSelection) (string, error) { if err != nil { return "", err } - addClaudeHooksSelective(data, hooksDir, sel) + addClaudeHooksSelective(data, hooksDir, sel, runtime.GOOS) if err := WriteJSONFile(settingsPath, data); err != nil { return "", err } diff --git a/internal/memory/setup/claude_test.go b/internal/memory/setup/claude_test.go index 11871b363..c4117ab5f 100644 --- a/internal/memory/setup/claude_test.go +++ b/internal/memory/setup/claude_test.go @@ -180,7 +180,7 @@ func TestClaudeRegisterHooksCollisionWritesAbsoluteCommands(t *testing.T) { hooks := data["hooks"].(map[string]any) for _, ev := range []string{"SessionStart", "UserPromptSubmit", "Stop"} { entry := hooks[ev].([]any)[0].(map[string]any)["hooks"].([]any)[0].(map[string]any) - cmd := entry["command"].(string) + cmd := strings.Trim(entry["command"].(string), `"`) // Windows commands are quoted if !filepath.IsAbs(cmd) { t.Fatalf("%s command must be absolute in the user-global file, got %q", ev, cmd) } @@ -207,3 +207,46 @@ func TestClaudeRegisterHooksProjectLocalStaysRelative(t *testing.T) { t.Fatalf("genuine project-local install must keep existing relative form, got %q", cmd) } } + +func TestAddClaudeHooksSelectiveWindowsQuotesForwardSlashPaths(t *testing.T) { + for _, tc := range []struct{ name, dir, prefix string }{ + {"global", `C:\Users\John Smith\.claude\hooks\mnemon`, "C:/Users/John Smith/.claude/hooks/mnemon"}, + {"project-local", `.claude\hooks\mnemon`, ".claude/hooks/mnemon"}, + {"trailing separator", `C:\u\.claude\hooks\mnemon\`, "C:/u/.claude/hooks/mnemon"}, + } { + t.Run(tc.name, func(t *testing.T) { + data := map[string]any{} + addClaudeHooksSelective(data, tc.dir, HookSelection{Remind: true, Nudge: true, Compact: true}, "windows") + hooks := data["hooks"].(map[string]any) + for ev, script := range map[string]string{ + "SessionStart": "prime.sh", "UserPromptSubmit": "user_prompt.sh", + "Stop": "stop.sh", "PreCompact": "compact.sh", + } { + entry := hooks[ev].([]any)[0].(map[string]any)["hooks"].([]any)[0].(map[string]any) + want := `"` + tc.prefix + "/" + script + `"` + if got := entry["command"].(string); got != want { + t.Fatalf("%s command = %q, want %q", ev, got, want) + } + } + }) + } +} + +func TestAddClaudeHooksSelectiveNonWindowsKeepsBarePaths(t *testing.T) { + data := map[string]any{} + addClaudeHooksSelective(data, "/home/u/.claude/hooks/mnemon", HookSelection{Remind: true}, "linux") + hooks := data["hooks"].(map[string]any) + entry := hooks["SessionStart"].([]any)[0].(map[string]any)["hooks"].([]any)[0].(map[string]any) + if got, want := entry["command"].(string), filepath.Join("/home/u/.claude/hooks/mnemon", "prime.sh"); got != want { + t.Fatalf("non-Windows command = %q, want %q", got, want) + } +} + +func TestAddClaudeHooksSelectiveWindowsReplacesQuotedMnemonHooks(t *testing.T) { + data := map[string]any{} + addClaudeHooksSelective(data, `C:\u\.claude\hooks\mnemon`, HookSelection{}, "windows") + addClaudeHooksSelective(data, `C:\u\.claude\hooks\mnemon`, HookSelection{}, "windows") + if n := len(data["hooks"].(map[string]any)["SessionStart"].([]any)); n != 1 { + t.Fatalf("re-registering must replace the quoted hook, got %d entries", n) + } +} diff --git a/internal/memory/setup/settings.go b/internal/memory/setup/settings.go index 658d04b6c..5e627853e 100644 --- a/internal/memory/setup/settings.go +++ b/internal/memory/setup/settings.go @@ -195,9 +195,22 @@ func removeIfEmpty(dir string) { } } +// claudeHookCommand returns the settings.json command for a hook script. Claude +// Code runs the command through Git Bash on Windows, where bash treats the +// backslashes of a native path as escapes (C:\Users\x\prime.sh becomes +// C:Usersxprime.sh). On Windows emit forward slashes and quote the path, which +// also survives spaces in the profile directory. Other platforms are unchanged. +func claudeHookCommand(hooksDir, filename, goos string) string { + if goos != "windows" { + return filepath.Join(hooksDir, filename) + } + dir := strings.TrimRight(strings.ReplaceAll(hooksDir, `\`, "/"), "/") + return `"` + dir + "/" + filename + `"` +} + // addClaudeHooksSelective idempotently sets mnemon hooks in Claude Code settings. // Prime (SessionStart) is always registered; Remind and Nudge are conditional. -func addClaudeHooksSelective(data map[string]interface{}, hooksDir string, sel HookSelection) { +func addClaudeHooksSelective(data map[string]interface{}, hooksDir string, sel HookSelection, goos string) { RemoveClaudeHooks(data) hooks := ensureHooksMap(data) @@ -206,7 +219,7 @@ func addClaudeHooksSelective(data map[string]interface{}, hooksDir string, sel H "hooks": []interface{}{ map[string]interface{}{ "type": "command", - "command": filepath.Join(hooksDir, "prime.sh"), + "command": claudeHookCommand(hooksDir, "prime.sh", goos), }, }, } @@ -219,7 +232,7 @@ func addClaudeHooksSelective(data map[string]interface{}, hooksDir string, sel H "hooks": []interface{}{ map[string]interface{}{ "type": "command", - "command": filepath.Join(hooksDir, "user_prompt.sh"), + "command": claudeHookCommand(hooksDir, "user_prompt.sh", goos), }, }, } @@ -233,7 +246,7 @@ func addClaudeHooksSelective(data map[string]interface{}, hooksDir string, sel H "hooks": []interface{}{ map[string]interface{}{ "type": "command", - "command": filepath.Join(hooksDir, "stop.sh"), + "command": claudeHookCommand(hooksDir, "stop.sh", goos), }, }, } @@ -247,7 +260,7 @@ func addClaudeHooksSelective(data map[string]interface{}, hooksDir string, sel H "hooks": []interface{}{ map[string]interface{}{ "type": "command", - "command": filepath.Join(hooksDir, "compact.sh"), + "command": claudeHookCommand(hooksDir, "compact.sh", goos), }, }, } diff --git a/internal/memory/setup/settings_test.go b/internal/memory/setup/settings_test.go index 6229dad45..a94ff7dbd 100644 --- a/internal/memory/setup/settings_test.go +++ b/internal/memory/setup/settings_test.go @@ -46,7 +46,7 @@ func TestAddClaudeHooksSelectiveReplacesExistingMnemonHooks(t *testing.T) { }, } - addClaudeHooksSelective(data, "/new/hooks", HookSelection{Remind: true, Nudge: false, Compact: true}) + addClaudeHooksSelective(data, "/new/hooks", HookSelection{Remind: true, Nudge: false, Compact: true}, "linux") hooks := data["hooks"].(map[string]any) if len(hooks["SessionStart"].([]any)) != 2 {