diff --git a/internal/agent/agentenv_test.go b/internal/agent/agentenv_test.go index 12f13d271..07bc86537 100644 --- a/internal/agent/agentenv_test.go +++ b/internal/agent/agentenv_test.go @@ -27,6 +27,7 @@ var notAnAgent = map[string]string{ "USER": "the process' own", "XDG_CONFIG_HOME": "magpie's own folder's, which appdir decides", "XDG_DATA_HOME": "the same", + "XDG_CACHE_HOME": "Go's build cache's too, which a sandbox keeps shared; Air's ACP OpenCode under it is found by path in a test's own home (#2423)", "MAGPIE_ADDR": "magpie's own", "MAGPIE_DEBUG": "magpie's own debug flag", "MAGPIE_PUBLIC_URL": "magpie's own public URL", diff --git a/internal/agent/air.go b/internal/agent/air.go index 980e9440a..4c4e4c594 100644 --- a/internal/agent/air.go +++ b/internal/agent/air.go @@ -36,6 +36,7 @@ import ( "path/filepath" "runtime" "strings" + "time" "github.com/yetone/magpie/internal/appdir" "github.com/yetone/magpie/internal/edit" @@ -78,7 +79,59 @@ var airOpenCode = func(home string) string { if p := filepath.Join(home, ".opencode", "bin", name); isFile(p) { return p } - return "" + return acpOpenCode(home, name, runtime.GOOS) +} + +// acpOpenCode is an OpenCode a JetBrains IDE or Zed installed from the ACP +// registry, unpacked into the editor's own folder and on no PATH (#2423: +// IDEA's at ~/Library/Caches/JetBrains/IntelliJIdea2026.2/acp-agents/ +// opencode/1.19.0/opencode); the newest when there are several, "" when +// none. +func acpOpenCode(home, name, goos string) string { + var globs []string + switch goos { + case "darwin": + globs = []string{ + filepath.Join(home, "Library", "Caches", "JetBrains", "*", "acp-agents", "opencode", "*", name), + filepath.Join(home, "Library", "Application Support", "Zed", "external_agents", "registry", "opencode", "*", name), + } + case "windows": + local := appdir.Getenv("LOCALAPPDATA") + if local == "" { + local = filepath.Join(home, "AppData", "Local") + } + globs = []string{ + filepath.Join(local, "JetBrains", "*", "acp-agents", "opencode", "*", name), + filepath.Join(local, "Zed", "external_agents", "registry", "opencode", "*", name), + } + default: + cache := appdir.Getenv("XDG_CACHE_HOME") + if cache == "" { + cache = filepath.Join(home, ".cache") + } + data := appdir.Getenv("XDG_DATA_HOME") + if data == "" { + data = filepath.Join(home, ".local", "share") + } + globs = []string{ + filepath.Join(cache, "JetBrains", "*", "acp-agents", "opencode", "*", name), + filepath.Join(data, "zed", "external_agents", "registry", "opencode", "*", name), + } + } + best, at := "", time.Time{} + for _, g := range globs { + ps, _ := filepath.Glob(g) + for _, p := range ps { + fi, err := os.Stat(p) + if err != nil || !fi.Mode().IsRegular() { + continue + } + if best == "" || fi.ModTime().After(at) { + best, at = p, fi.ModTime() + } + } + } + return best } func air(home, cfg string) *Agent { diff --git a/internal/agent/air_test.go b/internal/agent/air_test.go index bbde832c4..ccedd4f9a 100644 --- a/internal/agent/air_test.go +++ b/internal/agent/air_test.go @@ -277,3 +277,44 @@ func TestAirProviderWritesKeepCustomFields(t *testing.T) { }) } } + +// An OpenCode a JetBrains IDE or Zed installed from the ACP registry, in +// the editor's own folder and on no PATH, is the one Air starts (#2423, +// whiteS18: IDEA 2026.2's on macOS); the newest of several. +func TestAirFindsACPRegistryOpenCode(t *testing.T) { + home := t.TempDir() + t.Setenv("LOCALAPPDATA", filepath.Join(home, "AppData", "Local")) + t.Setenv("XDG_CACHE_HOME", "") + t.Setenv("XDG_DATA_HOME", "") + put := func(p string, age time.Duration) string { + writeFile(t, p, "#!/bin/sh\n") + when := time.Now().Add(-age) + if err := os.Chtimes(p, when, when); err != nil { + t.Fatal(err) + } + return p + } + for _, c := range []struct { + goos, name string + older, want string + }{ + {"darwin", "opencode", + filepath.Join(home, "Library", "Application Support", "Zed", "external_agents", "registry", "opencode", "v_1.18.11_c8581a12e4e7afe7_fd7ca24743b4ae6b", "opencode"), + filepath.Join(home, "Library", "Caches", "JetBrains", "IntelliJIdea2026.2", "acp-agents", "opencode", "1.19.0", "opencode")}, + {"linux", "opencode", + filepath.Join(home, ".local", "share", "zed", "external_agents", "registry", "opencode", "v_1.18.11", "opencode"), + filepath.Join(home, ".cache", "JetBrains", "IntelliJIdea2026.2", "acp-agents", "opencode", "1.19.0", "opencode")}, + {"windows", "opencode.exe", + filepath.Join(home, "AppData", "Local", "JetBrains", "IntelliJIdea2026.2", "acp-agents", "opencode", "1.18.0", "opencode.exe"), + filepath.Join(home, "AppData", "Local", "Zed", "external_agents", "registry", "opencode", "v_1.19.0", "opencode.exe")}, + } { + if got := acpOpenCode(home, c.name, c.goos); got != "" { + t.Fatalf("%s: none installed, got %q", c.goos, got) + } + put(c.older, time.Hour) + put(c.want, time.Minute) + if got := acpOpenCode(home, c.name, c.goos); got != c.want { + t.Fatalf("%s: got %q, want %q", c.goos, got, c.want) + } + } +}