From 23ce84166cf4c53227b429bd139a82a0835616df Mon Sep 17 00:00:00 2001 From: Mohammed Alkindi Date: Fri, 13 Nov 2026 22:43:42 -0800 Subject: [PATCH] fix(setup): run Kimi hooks through bash.exe on Windows Kimi Code runs hook commands natively on Windows, so a bare .sh path fails under cmd or opens the .sh file association, a Git Bash window on every hook event. On Windows, register each command as "" "", resolving Git for Windows' bash and skipping the System32 WSL launcher. Other platforms keep the bare path. Fixes #178 --- CHANGELOG.md | 4 +++ internal/memory/setup/kimi.go | 52 +++++++++++++++++++++++++++--- internal/memory/setup/kimi_test.go | 33 +++++++++++++++++++ 3 files changed, 84 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 96661716e..673d778bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -69,6 +69,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/). ### Fixed +- `mnemon setup --target kimi` on Windows registers each hook as + `"" ""` instead of a bare `.sh` path. Kimi Code runs + hook commands natively on Windows, so the bare path failed under `cmd` or + opened a Git Bash window on every hook event. Other platforms are unchanged. - The generated Homebrew cask no longer emits the deprecated `postflight` stanza. `brew` commands that load the cask printed "Calling `postflight` is deprecated! Use `postflight_steps` instead." on every invocation. The diff --git a/internal/memory/setup/kimi.go b/internal/memory/setup/kimi.go index 1559dd1e5..db96446d8 100644 --- a/internal/memory/setup/kimi.go +++ b/internal/memory/setup/kimi.go @@ -3,7 +3,9 @@ package setup import ( "fmt" "os" + "os/exec" "path/filepath" + "runtime" "strings" "github.com/mnemon-dev/mnemon/internal/memory/setup/assets" @@ -48,7 +50,7 @@ func KimiRegisterHooks(configDir string) (string, error) { return "", err } - updated := addKimiHooks(string(data), absHooksDir) + updated := addKimiHooks(string(data), absHooksDir, runtime.GOOS, kimiWindowsBash) if err := os.MkdirAll(configDir, 0755); err != nil { return "", err } @@ -114,12 +116,16 @@ func KimiEject(configDir string) []error { return errs } -func addKimiHooks(config, hooksDir string) string { +func addKimiHooks(config, hooksDir, goos string, findBash func() string) string { cleaned := strings.TrimRight(removeKimiHooks(config), "\n") + bash := "" + if goos == "windows" { + bash = findBash() + } blocks := []string{ - kimiHookBlock("SessionStart", "", filepath.Join(hooksDir, "prime.sh")), - kimiHookBlock("UserPromptSubmit", "", filepath.Join(hooksDir, "user_prompt.sh")), - kimiHookBlock("Stop", "", filepath.Join(hooksDir, "stop.sh")), + kimiHookBlock("SessionStart", "", kimiHookCommand(hooksDir, "prime.sh", goos, bash)), + kimiHookBlock("UserPromptSubmit", "", kimiHookCommand(hooksDir, "user_prompt.sh", goos, bash)), + kimiHookBlock("Stop", "", kimiHookCommand(hooksDir, "stop.sh", goos, bash)), } if cleaned == "" { return strings.Join(blocks, "\n\n") + "\n" @@ -127,6 +133,42 @@ func addKimiHooks(config, hooksDir string) string { return cleaned + "\n\n" + strings.Join(blocks, "\n\n") + "\n" } +// kimiHookCommand returns the config.toml command for a hook script. Kimi Code +// on Windows runs the command natively, so a bare .sh path either fails under +// cmd or opens the .sh file association (a Git Bash window). Run it through an +// explicit bash.exe instead. +func kimiHookCommand(hooksDir, filename, goos, bash string) string { + if goos != "windows" { + return filepath.Join(hooksDir, filename) + } + script := strings.TrimRight(hooksDir, `\/`) + `\` + filename + return `"` + bash + `" "` + script + `"` +} + +// kimiWindowsBash locates the Git for Windows bash.exe. It deliberately skips +// the System32 bash.exe, which is the WSL launcher and cannot run these scripts. +func kimiWindowsBash() string { + var candidates []string + for _, env := range []string{"ProgramFiles", "ProgramFiles(x86)", "ProgramW6432"} { + if root := os.Getenv(env); root != "" { + candidates = append(candidates, filepath.Join(root, "Git", "bin", "bash.exe")) + } + } + if local := os.Getenv("LocalAppData"); local != "" { + candidates = append(candidates, filepath.Join(local, "Programs", "Git", "bin", "bash.exe")) + } + if git, err := exec.LookPath("git"); err == nil { + // /cmd/git.exe or /bin/git.exe -> /bin/bash.exe + candidates = append(candidates, filepath.Join(filepath.Dir(filepath.Dir(git)), "bin", "bash.exe")) + } + for _, c := range candidates { + if info, err := os.Stat(c); err == nil && !info.IsDir() { + return c + } + } + return "bash.exe" +} + func kimiHookBlock(event, matcher, command string) string { lines := []string{ "[[hooks]]", diff --git a/internal/memory/setup/kimi_test.go b/internal/memory/setup/kimi_test.go index 15ed4cc6d..9ba0135d5 100644 --- a/internal/memory/setup/kimi_test.go +++ b/internal/memory/setup/kimi_test.go @@ -1,6 +1,7 @@ package setup import ( + "fmt" "os" "path/filepath" "strings" @@ -112,3 +113,35 @@ timeout = 3 t.Fatalf("custom hook should be preserved: %s", text) } } + +func TestAddKimiHooksWindowsInvokesScriptsThroughBash(t *testing.T) { + const bash = `C:\Program Files\Git\bin\bash.exe` + hooksDir := `C:\Users\u\.kimi-code\hooks\mnemon` + + text := addKimiHooks("", hooksDir, "windows", func() string { return bash }) + + for _, script := range []string{"prime.sh", "user_prompt.sh", "stop.sh"} { + want := fmt.Sprintf("command = %q", `"`+bash+`" "`+hooksDir+`\`+script+`"`) + if !strings.Contains(text, want) { + t.Fatalf("%s should be run through bash.exe, want %s in:\n%s", script, want, text) + } + } +} + +func TestAddKimiHooksNonWindowsRegistersBareScripts(t *testing.T) { + text := addKimiHooks("", "/home/u/.kimi-code/hooks/mnemon", "linux", func() string { + t.Fatal("bash lookup should not run off Windows") + return "" + }) + if !strings.Contains(text, fmt.Sprintf("command = %q", filepath.Join("/home/u/.kimi-code/hooks/mnemon", "prime.sh"))) { + t.Fatalf("non-Windows hooks should stay bare scripts: %s", text) + } +} + +func TestRemoveKimiHooksRemovesWindowsBashCommands(t *testing.T) { + text := addKimiHooks("model = \"kimi\"\n", `C:\Users\u\.kimi-code\hooks\mnemon`, "windows", + func() string { return `C:\Program Files\Git\bin\bash.exe` }) + if got := removeKimiHooks(text); strings.Contains(got, "mnemon") || !strings.Contains(got, `model = "kimi"`) { + t.Fatalf("windows bash hooks should be removed cleanly: %s", got) + } +}