Files
magpie/internal/agent/install_missing_test.go
T
yetone d0fcb8bcdf agent: an agent uninstalled with its folder left behind shows "CLI not found" with no live settings, Hermes and the shared-folder agents too (#2177, mengyianx; #2173, SkyTodInfi)
Detection goes by an agent's folder, and an uninstall leaves the folder
behind. Before this commit, Hermes with only its 37-byte
~/.hermes/config.yaml (custom_providers: [], mcp_servers: {}) was listed as
installed in `magpie ls`, the GUI and the TUI, with model and effort pickers
that no program reads. CLIMissing (#843) covered only agents that have an
install command and their own folder. Hermes and the agents whose folder
something else keeps too (claude, codex, cline, opencode, goose) were never
gone.

What changes:
- internal/agent/gone.go adds cliHeld, which asks whether anything here still
  reads the folder:
  - the CLI on PATH or in proc.FindTool's folders (for goose, a binary that
    isn't pressly's Go goose);
  - its desktop app (HasApp) or sign;
  - the vendor installer's own place: ~/.claude/local, ~/.opencode/bin,
    ~/.codex/packages/standalone, Hermes' <home>/hermes-agent checkout;
  - for the shared folders, an editor extension under
    ~/.<editor>/extensions (anthropic.claude-code, openai.chatgpt,
    saoudrizwan.claude-dev), Claude.app or Claude Desktop, OpenCode.app or
    Goose.app on a Mac, a JetBrains IDE's settings (its agents bring their
    own program) or Zed's external_agents.
  When magpie can't tell, the agent is never gone. That covers OpenCode and
  Goose off a Mac, Gemini (its folder is Antigravity's and Code Assist's
  too), agents with their own detect, and WSL and plugin agents. It also
  covers Bin agents with no install command, except Hermes (cliOnly), since
  their installers' paths aren't verified.
- GUI: a gone row shows "CLI not found" and none of its settings, and the
  tag's tip says why. It offers no Apply again. While it is still wired it
  keeps its switch and Disconnect. Once it isn't wired it folds under Not
  set up, and Get started doesn't offer it (connectKind is "no"; the sandbox
  run showed Hermes offered there before). The panel shows the words only.
  The new tip is translated into zh, zh-TW, ja and de.
- The TUI leaves the agent out. `magpie ls` says "CLI not found" beside its
  path.
- docs/subsystems/agent-wiring.md describes all of the above.
- aside-native.test.cjs drops cliMissing from an Aside fixture. That state
  can't happen (Aside has no install command and isn't in cliOnly), and
  that test is about runtime_unavailable.

What was run:
- Sandbox HOME holding only the reporter's ~/.hermes/config.yaml (37 bytes)
  and a bare ~/.codex, with env -i and PATH=/usr/bin:/bin:
  - `magpie ls` at base 331c27703 lists "Hermes Agent — · effort —".
  - The fix lists "Hermes Agent  CLI not found  ~/.hermes/config.yaml".
  - Codex stays listed, because this Mac has ChatGPT.app's Codex (HasApp),
    so a really installed agent is kept.
- `magpie web` from the same sandbox, screenshot in Chromium: the Hermes row
  shows "CLI not found" and no pickers. That run found Get started still
  offering Hermes, which this commit fixes.
- go test ./internal/agent (2255s under load ~250 on 14 cores) and
  ./internal/cli and ./internal/tui pass. ./internal/gui fails only
  TestMainWindowCorners/macOS_26_SDK, killed at its 40s deadline under the
  same load. That is #1936; it has nothing from this change in its path, and
  it passed on base once and failed on it twice.
- GUI in Chromium and WebKit:
  - agent-cli-missing.test.cjs covers 5 languages, widths 1100 and 420, and
    the panel.
  - getting-started.test.cjs adds a gone Hermes to the fixture; the chips
    stay codex, claude and opencode.
  - Also run: i18n-keys, aside-native, agent-*, panel-* and click-scroll.
- gofmt -l is clean. go vet passes on darwin, linux (-tags nogui) and
  windows for the touched packages.

Broken by hand, and the tests went red each time:
- Dropping the openai.chatgpt check made TestCLIMissingSharedFolders' codex
  case red.
- Dropping the hermes-agent case made TestCLIMissingHermes red.
- Dropping the GUI gone branch, or the agentUsed change ("the one not
  connected stayed up"), made agent-cli-missing red.
- Dropping connectKind's cliMissing line made getting-started red
  (30 of 60 failed: chips gained hermes).

Not covered:
- library.Targets (Skills/MCP) still lists a gone agent as a place to give
  skills to.
2026-10-14 22:50:46 +08:00

244 lines
8.9 KiB
Go

package agent
import (
"os"
"path/filepath"
"runtime"
"testing"
"github.com/yetone/magpie/internal/testenv"
)
// An agent whose settings are left but whose CLI is gone (#843: dsh
// uninstalled, ~/.dsh kept) is still listed, and Install another agent
// offers it again, marked; one found where users' tools go though not on
// PATH (#839) isn't missing. Codex's folder with neither its CLI nor
// anything else that reads it is a leftover too (#2177).
func TestCLIMissing(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("PATH", t.TempDir())
// no desktop app either, whatever this machine has installed
was := appFolders
appFolders = func() []string { return nil }
t.Cleanup(func() { appFolders = was })
bin := "magpie-test-gone-dsh"
dir := filepath.Join(home, ".dsh")
if err := os.MkdirAll(dir, 0o700); err != nil {
t.Fatal(err)
}
dsh := &Agent{ID: "dsh", Name: "DeepSeek Harness", Icon: "deepseek-color", Bin: bin, Dir: dir, Path: filepath.Join(dir, "config.yaml")}
codex := &Agent{ID: "codex", Name: "Codex", Bin: "magpie-test-gone-codex", Dir: dir, Path: filepath.Join(dir, "config.toml")}
never := &Agent{ID: "crush", Name: "Crush", Bin: "magpie-test-gone-crush", Dir: filepath.Join(home, "nope"), Path: filepath.Join(home, "nope", "crush.json")}
wsl := &Agent{ID: "dsh@wsl:Ubuntu", Name: "DeepSeek Harness", Bin: bin, WSL: "Ubuntu", Dir: dir, Path: filepath.Join(dir, "config.yaml")}
if !dsh.Detected() || !dsh.CLIMissing() {
t.Fatalf("dsh with ~/.dsh and no dsh: detected %v, missing %v", dsh.Detected(), dsh.CLIMissing())
}
if !codex.CLIMissing() {
t.Fatal("codex's folder alone, nothing reading it, not taken for missing")
}
if never.CLIMissing() || wsl.CLIMissing() {
t.Fatalf("missing: crush not here %v, wsl %v", never.CLIMissing(), wsl.CLIMissing())
}
got := installsOf([]*Agent{dsh, codex, never, wsl}, "darwin", true)
ids := map[string]Install{}
for _, x := range got {
ids[x.ID] = x
}
if x, ok := ids["dsh"]; !ok || !x.Missing || len(x.Commands) != 1 || x.Commands[0].Command != "npm install -g @deepseek-ai/dsh" {
t.Fatalf("dsh offered as %+v (all: %+v)", x, got)
}
if x, ok := ids["crush"]; !ok || x.Missing {
t.Fatalf("crush, not here at all, offered as %+v", x)
}
if x, ok := ids["codex"]; !ok || !x.Missing {
t.Fatalf("codex, its folder left, offered as %+v", x)
}
// installed in ~/.local/bin, which magpie's PATH hasn't: found
local := filepath.Join(home, ".local", "bin")
if err := os.MkdirAll(local, 0o755); err != nil {
t.Fatal(err)
}
name := bin
if runtime.GOOS == "windows" {
name += ".exe"
}
testenv.Program(t, filepath.Join(local, name), "#!/bin/sh\n")
if dsh.CLIMissing() {
t.Fatal("dsh in ~/.local/bin taken for missing")
}
for _, x := range installsOf([]*Agent{dsh}, "darwin", true) {
t.Fatalf("installed dsh offered: %+v", x)
}
}
// noApps points the app folders at an empty folder of the test's, so
// whatever this machine has installed counts for nothing.
func noApps(t *testing.T) string {
t.Helper()
apps := t.TempDir()
was := appFolders
appFolders = func() []string { return []string{apps} }
t.Cleanup(func() { appFolders = was })
return apps
}
func mkdirT(t *testing.T, p string) {
t.Helper()
if err := os.MkdirAll(p, 0o755); err != nil {
t.Fatal(err)
}
}
func writeT(t *testing.T, p, body string) {
t.Helper()
mkdirT(t, filepath.Dir(p))
if err := os.WriteFile(p, []byte(body), 0o600); err != nil {
t.Fatal(err)
}
}
// #2173 (SkyTodInfi): Hermes uninstalled, ~/.hermes/config.yaml left with
// 37 bytes, no hermes anywhere: listed, as gone. Its own install.sh's
// checkout, or hermes in ~/.local/bin, is Hermes installed.
func TestCLIMissingHermes(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("PATH", t.TempDir())
noApps(t)
dir := filepath.Join(home, ".hermes")
writeT(t, filepath.Join(dir, "config.yaml"), "custom_providers: []\nmcp_servers: {}\n")
h := &Agent{ID: "hermes", Name: "Hermes Agent", Bin: "magpie-test-gone-hermes", Dir: dir, Path: filepath.Join(dir, "config.yaml")}
if !h.Detected() || !h.CLIMissing() {
t.Fatalf("hermes by its config.yaml alone: detected %v, missing %v", h.Detected(), h.CLIMissing())
}
// no install command magpie knows: not offered, though gone
for _, x := range installsOf([]*Agent{h}, runtime.GOOS, true) {
t.Fatalf("hermes offered: %+v", x)
}
mkdirT(t, filepath.Join(dir, "hermes-agent"))
if h.CLIMissing() {
t.Fatal("hermes with its checkout taken for missing")
}
if err := os.RemoveAll(filepath.Join(dir, "hermes-agent")); err != nil {
t.Fatal(err)
}
name := h.Bin
if runtime.GOOS == "windows" {
name += ".exe"
}
local := filepath.Join(home, ".local", "bin")
mkdirT(t, local)
testenv.Program(t, filepath.Join(local, name), "#!/bin/sh\n")
if h.CLIMissing() {
t.Fatal("hermes in ~/.local/bin taken for missing")
}
}
// An agent whose folder isn't only its CLI's is gone only when nothing
// else that reads it is here: an editor's extension, a desktop app, an
// IDE whose agents bring their own program. Never one magpie can't tell
// about (OpenCode off a Mac, Gemini, detected its own way).
type goneCase struct {
id string
folder string
with func(t *testing.T, home, apps string)
missing bool
}
func TestCLIMissingSharedFolders(t *testing.T) {
cases := []goneCase{
{"claude", ".claude", nil, true},
{"claude", ".claude", func(t *testing.T, home, _ string) {
mkdirT(t, filepath.Join(home, ".vscode", "extensions", "anthropic.claude-code-2.1.3-darwin-arm64"))
}, false},
{"claude", ".claude", func(t *testing.T, home, _ string) {
mkdirT(t, filepath.Join(home, ".cursor", "extensions", "anthropic.claude-code-2.1.3"))
}, false},
{"claude", ".claude", func(t *testing.T, home, _ string) {
writeT(t, filepath.Join(home, ".claude", "local", exe("claude")), "")
}, false},
{"claude", ".claude", func(t *testing.T, home, _ string) {
mkdirT(t, jetBrainsDirFor(home))
}, false},
{"codex", ".codex", nil, true},
{"codex", ".codex", func(t *testing.T, home, _ string) {
mkdirT(t, filepath.Join(home, ".vscode-server", "extensions", "openai.chatgpt-26.5.1"))
}, false},
{"codex", ".codex", func(t *testing.T, home, _ string) {
mkdirT(t, filepath.Join(home, ".codex", "packages", "standalone"))
}, false},
{"codex", ".codex", func(t *testing.T, home, _ string) {
mkdirT(t, zedAgentsDirFor(home))
}, false},
// another extension's folder of the same prefix is not Codex's
{"codex", ".codex", func(t *testing.T, home, _ string) {
mkdirT(t, filepath.Join(home, ".vscode", "extensions", "openai.chatgpt.other"))
}, true},
{"cline", ".cline", nil, true},
{"cline", ".cline", func(t *testing.T, home, _ string) {
mkdirT(t, filepath.Join(home, ".windsurf", "extensions", "saoudrizwan.claude-dev-3.40.0"))
}, false},
}
if runtime.GOOS == "darwin" {
cases = append(cases,
goneCase{"claude", ".claude", func(t *testing.T, _, apps string) { mkdirT(t, filepath.Join(apps, "Claude.app")) }, false},
goneCase{"opencode", ".opencode-config", nil, true},
goneCase{"opencode", ".opencode-config", func(t *testing.T, _, apps string) { mkdirT(t, filepath.Join(apps, "OpenCode.app")) }, false},
)
} else {
cases = append(cases, goneCase{"opencode", ".opencode-config", nil, false})
}
for i, c := range cases {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("APPDATA", filepath.Join(home, "AppData", "Roaming"))
t.Setenv("LOCALAPPDATA", filepath.Join(home, "AppData", "Local"))
t.Setenv("PATH", t.TempDir())
apps := noApps(t)
dir := filepath.Join(home, c.folder)
writeT(t, filepath.Join(dir, "settings.json"), "{}")
a := &Agent{ID: c.id, Name: c.id, Bin: "magpie-test-gone-" + c.id, Dir: dir, Path: filepath.Join(dir, "settings.json")}
if c.with != nil {
c.with(t, home, apps)
}
if got := a.CLIMissing(); got != c.missing {
t.Errorf("case %d (%s): missing %v, want %v", i, c.id, got, c.missing)
}
}
// detected its own way: never taken for gone
home := t.TempDir()
t.Setenv("HOME", home)
g := &Agent{ID: "gemini", Bin: "magpie-test-gone-gemini", Dir: home, Path: filepath.Join(home, "x"), detect: func() bool { return true }}
if g.CLIMissing() {
t.Fatal("gemini, detected its own way, taken for missing")
}
}
// jetBrainsDirFor and zedAgentsDirFor are where jetBrainsHere and
// zedAgentsHere look on this system.
func jetBrainsDirFor(home string) string {
switch runtime.GOOS {
case "darwin":
return filepath.Join(home, "Library", "Application Support", "JetBrains")
case "windows":
return filepath.Join(home, "AppData", "Roaming", "JetBrains")
}
return filepath.Join(home, ".config", "JetBrains")
}
func zedAgentsDirFor(home string) string {
switch runtime.GOOS {
case "darwin":
return filepath.Join(home, "Library", "Application Support", "Zed", "external_agents")
case "windows":
return filepath.Join(home, "AppData", "Local", "Zed", "external_agents")
}
return filepath.Join(home, ".local", "share", "zed", "external_agents")
}