Skip to content
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
`"<Git bash.exe>" "<script.sh>"` 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
Expand Down
52 changes: 47 additions & 5 deletions internal/memory/setup/kimi.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package setup
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"

"github.com/mnemon-dev/mnemon/internal/memory/setup/assets"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -114,19 +116,59 @@ 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"
}
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 {
// <root>/cmd/git.exe or <root>/bin/git.exe -> <root>/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]]",
Expand Down
33 changes: 33 additions & 0 deletions internal/memory/setup/kimi_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package setup

import (
"fmt"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -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)
}
}
Loading