Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down
3 changes: 2 additions & 1 deletion internal/memory/setup/claude.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"runtime"

"github.com/mnemon-dev/mnemon/internal/memory/setup/assets"
)
Expand Down Expand Up @@ -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
}
Expand Down
45 changes: 44 additions & 1 deletion internal/memory/setup/claude_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
}
23 changes: 18 additions & 5 deletions internal/memory/setup/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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),
},
},
}
Expand All @@ -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),
},
},
}
Expand All @@ -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),
},
},
}
Expand All @@ -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),
},
},
}
Expand Down
2 changes: 1 addition & 1 deletion internal/memory/setup/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading