Jevik_AI on X (Windows, portable magpie) deleted magpie's folders by hand. ~/.codex/config.toml kept openai_base_url on 127.0.0.1:3425, and every Codex turn then failed with a 502, with nothing left that knew why. Deleting magpie's files never puts an agent back, so there is now an uninstall that does. - internal/uninstall: Run goes in this order. System removes the background service, opening at login, the Start menu shortcut (shortcut.Remove, with its App Paths key), the magpie:// links and the privacy model's helper. Agents ends Codex's account failover, then runs each connected agent's own Disconnect, or its offline disconnect for an app that is closed. That is the same code as the Agents page, so a value the user changed by hand since Connect stays theirs (e7f21dba5for Codex). It then ports Codex's rollouts and restarts the Codex app's app-server (provider.RestartCodexAppWhenIdle, which says whether one was left on the old config). Data deletes magpie's folders, but keeps the Library's folder while it holds skills, servers or hooks the user's agents use. The executable is left where the user put it. - `magpie uninstall [--yes]`: refuses while another magpie runs, and says which one. On a terminal it lists what goes and asks [y/N]; with no terminal, only --yes does it. - Settings › About › Uninstall magpie: POST /api/uninstall runs System and Agents. If anything failed, nothing else is removed and the sheet offers Try again. Otherwise POST /api/uninstall/finish starts a detached `magpie uninstall --data-after <pid>` (Setsid on unix, DETACHED_PROCESS on Windows) and the app quits; the files go once it has ended, since WebView2 holds its folder open until then. A Codex app still on the old config is named before quitting. magpie web refuses both routes. Strings go through t() in all five languages. - --data-after refuses a pid that is no process (0, negative, its own): kill(0) counts as alive, and a run given 0 waited out its 2 minutes. Not done, and why: - No restore on quit. Quitting keeps agents on magpie by design: the service or the next start serves them. - No Apps & Features entry or Windows uninstall hook. A portable copy registers nothing with Windows, and an uninstaller that ran without magpie's own disconnect would leave the configs as they are. The `# magpie:` note above each line magpie writes (previous commit) is the recovery path for a copy deleted by hand. Run at this head (db75663a0's tree, on5d24bc6cc), on mote: - gofmt -l clean; go vet ./... on darwin (-tags nogui), GOOS=windows and GOOS=linux -tags nogui: ok. - go test -tags nogui -count=1 -timeout 45m on ., internal/uninstall, cli, gui (TestCoreScriptBudget included), provider, shortcut, edit, agent and proc: all ok. - GUI at this head: mote was at load 100-120 with other sessions' GUI runs. Chromium crashed (SIGBUS) mid-file in settings-search and settings-sections after their en cases passed, WebKit failed to launch within 180s in s3-sync, settings-search and settings-sections, and fonts and title-bar-tools went red on unstable elements (#2984 has fonts under load). uninstall-settings had not reported when this was pushed. - At the earlier base b3580c72c, on this machine: the full suite (-timeout 45m), red only on #3884, #3632 (both since fixed on main) and the core budget, which main's later move of Web search out (#4133) has room for now. GUI: 104 Settings and i18n files in chromium and webkit, uninstall-settings and i18n-keys green in both; the reds were #3797, #3876 and the load reds above. Tried for real, in a sandbox HOME: `magpie uninstall` end to end with Codex connected and a sandbox Codex app running (config.toml back without magpie's lines, magpie's data gone, the exe kept, the app's app-server restarted onto the restored config); codex-cli 0.162.1 reading the noted config.toml. Other agents were covered by tests only. Not tried: the GUI's Uninstall in a real app window, a real --data-after run waiting on a live app's pid, and the Windows box. Broken by hand: --data-after's pid guard removed -> TestUninstallDataAfterNeedsAProcess red (0, -1 and its own pid accepted); restored, green. At the pushed head (rebased on 51f66fe8b; the uninstall call adapted to EndCodexFailover's (bool, error) on the way), on mote: - gofmt clean; go vet darwin, GOOS=linux -tags nogui, GOOS=windows ok; go build ./... ok. - go test -timeout 45m: internal/uninstall, edit, shortcut, cli, agent, provider ok; internal/gui red only on TestMainWindowCorners/macOS_26_SDK (#3949, macOS 27). - GUI: uninstall-settings and i18n-keys, 30 pass 0 fail. - WSL (magpie-desktop, CGO_ENABLED=0): uninstall, edit, shortcut, provider ok; agent red on TestGrokInstalled, TestOmpDetectedWithPiDir, TestZedAppPath, the same at origin/main51f66fe8b(#4033, #3548).
231 lines
7.7 KiB
Go
231 lines
7.7 KiB
Go
package uninstall
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"runtime"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/agent"
|
|
"github.com/yetone/magpie/internal/appdir"
|
|
"github.com/yetone/magpie/internal/library"
|
|
)
|
|
|
|
// fakes puts a fake in for everything Run asks of the system, and records
|
|
// what it was asked, in order.
|
|
func fakes(t *testing.T, list []*agent.Agent, isWired map[string]bool, failing map[string]error) *[]string {
|
|
t.Helper()
|
|
var did []string
|
|
wasAgents, wasWired, wasDisconnect := agents, wired, disconnectOne
|
|
wasFailover, wasPort, wasRestart := endFailover, portRollouts, restartCodexApp
|
|
wasService, wasAutostart, wasShortcut, wasScheme, wasPII, wasExe := removeService, autostartOff, removeShortcut, removeScheme, endPII, executable
|
|
t.Cleanup(func() {
|
|
agents, wired, disconnectOne = wasAgents, wasWired, wasDisconnect
|
|
endFailover, portRollouts, restartCodexApp = wasFailover, wasPort, wasRestart
|
|
removeService, autostartOff, removeShortcut, removeScheme, endPII, executable = wasService, wasAutostart, wasShortcut, wasScheme, wasPII, wasExe
|
|
})
|
|
agents = func() []*agent.Agent { return list }
|
|
wired = func(a *agent.Agent) bool { return isWired[a.ID] }
|
|
disconnectOne = func(a *agent.Agent) error {
|
|
did = append(did, "disconnect "+a.ID)
|
|
if err := failing[a.ID]; err != nil {
|
|
return err
|
|
}
|
|
isWired[a.ID] = false
|
|
return nil
|
|
}
|
|
endFailover = func() (bool, error) { did = append(did, "failover"); return true, nil }
|
|
portRollouts = func(time.Duration) { did = append(did, "rollouts") }
|
|
restartCodexApp = func(context.Context, time.Duration) (int, bool, error) {
|
|
did = append(did, "codex app")
|
|
return 1, false, nil
|
|
}
|
|
step := func(name string) func() error {
|
|
return func() error { did = append(did, name); return nil }
|
|
}
|
|
removeService = step("service")
|
|
autostartOff = step("autostart")
|
|
removeShortcut = func(string) error { did = append(did, "shortcut"); return nil }
|
|
removeScheme = func(string) error { did = append(did, "scheme"); return nil }
|
|
endPII = step("pii")
|
|
executable = func() (string, error) { return "/opt/magpie/magpie", nil }
|
|
return &did
|
|
}
|
|
|
|
func write(t *testing.T, p, s string) {
|
|
t.Helper()
|
|
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(p, []byte(s), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func gone(t *testing.T, p string) bool {
|
|
t.Helper()
|
|
_, err := os.Stat(p)
|
|
return errors.Is(err, os.ErrNotExist)
|
|
}
|
|
|
|
// Uninstalling stops the service before anything else (one still serving
|
|
// would wire agents again), disconnects only the agents magpie is in,
|
|
// restarts the Codex app's app-server when Codex was one of them, and
|
|
// removes magpie's files last.
|
|
func TestRunOrder(t *testing.T) {
|
|
codex := &agent.Agent{ID: "codex", Name: "Codex"}
|
|
claude := &agent.Agent{ID: "claude", Name: "Claude Code"}
|
|
gemini := &agent.Agent{ID: "gemini", Name: "Gemini CLI"}
|
|
did := fakes(t, []*agent.Agent{codex, claude, gemini}, map[string]bool{"codex": true, "claude": true}, nil)
|
|
settings := filepath.Join(appdir.Config(), "settings.json")
|
|
write(t, settings, "{}")
|
|
write(t, filepath.Join(appdir.Cache(), "models.json"), "{}")
|
|
|
|
r := Run(context.Background())
|
|
|
|
want := []string{"service", "autostart", "shortcut", "scheme", "pii", "failover", "disconnect codex", "disconnect claude", "rollouts", "codex app"}
|
|
if !slices.Equal(*did, want) {
|
|
t.Fatalf("did %q, want %q", *did, want)
|
|
}
|
|
if r.Failed() || r.CodexApp {
|
|
t.Fatalf("report: %+v", r)
|
|
}
|
|
if !gone(t, appdir.Config()) || !gone(t, appdir.Cache()) {
|
|
t.Fatal("magpie's folders left")
|
|
}
|
|
if r.Exe != "/opt/magpie/magpie" {
|
|
t.Fatalf("exe %q", r.Exe)
|
|
}
|
|
if len(r.Agents) != 2 || r.Agents[0].What != "Codex" || r.Agents[1].What != "Claude Code" {
|
|
t.Fatalf("agents %+v", r.Agents)
|
|
}
|
|
}
|
|
|
|
// An agent that can't be disconnected is said, and the rest are still
|
|
// done; Codex not among them leaves the Codex app alone.
|
|
func TestRunSaysWhatFailed(t *testing.T) {
|
|
claude := &agent.Agent{ID: "claude", Name: "Claude Code"}
|
|
goose := &agent.Agent{ID: "goose", Name: "Goose"}
|
|
did := fakes(t, []*agent.Agent{claude, goose}, map[string]bool{"claude": true, "goose": true}, map[string]error{"claude": errors.New("settings.json: permission denied")})
|
|
r := Run(context.Background())
|
|
if slices.Contains(*did, "codex app") {
|
|
t.Fatalf("the Codex app restarted with no Codex connected: %q", *did)
|
|
}
|
|
if !slices.Contains(*did, "disconnect goose") {
|
|
t.Fatalf("goose skipped after claude failed: %q", *did)
|
|
}
|
|
if !r.Failed() {
|
|
t.Fatal("not failed")
|
|
}
|
|
says := strings.Join(r.Says(), "\n")
|
|
if !strings.Contains(says, "! disconnected Claude Code: settings.json: permission denied") || !strings.Contains(says, "✓ disconnected Goose") {
|
|
t.Fatalf("says:\n%s", says)
|
|
}
|
|
}
|
|
|
|
// A Codex turn still running past the wait leaves the app-server alone,
|
|
// and the report tells the user to reopen the app.
|
|
func TestRunCodexAppLeft(t *testing.T) {
|
|
codex := &agent.Agent{ID: "codex", Name: "Codex"}
|
|
fakes(t, []*agent.Agent{codex}, map[string]bool{"codex": true}, nil)
|
|
restartCodexApp = func(context.Context, time.Duration) (int, bool, error) { return 0, true, nil }
|
|
if r := Run(context.Background()); !r.CodexApp {
|
|
t.Fatal("CodexApp not said")
|
|
}
|
|
}
|
|
|
|
// Skills and servers the user's agents use from the Library stay; the
|
|
// rest of magpie's folder goes.
|
|
func TestDataKeepsLibraryInUse(t *testing.T) {
|
|
fakes(t, nil, map[string]bool{}, nil)
|
|
skill := filepath.Join(library.Dir(), "skills", "docx", "SKILL.md")
|
|
write(t, skill, "# docx")
|
|
settings := filepath.Join(appdir.Config(), "settings.json")
|
|
write(t, settings, "{}")
|
|
write(t, filepath.Join(appdir.Config(), "usage", "2026-10.db"), "x")
|
|
|
|
r := &Report{}
|
|
Data(r)
|
|
if !gone(t, settings) || !gone(t, filepath.Join(appdir.Config(), "usage")) {
|
|
t.Fatal("magpie's own files left")
|
|
}
|
|
if gone(t, skill) {
|
|
t.Fatal("a skill in use removed")
|
|
}
|
|
if len(r.Kept) != 1 || r.Kept[0].What != library.Dir() {
|
|
t.Fatalf("kept %+v", r.Kept)
|
|
}
|
|
|
|
// an empty Library goes with the rest
|
|
os.RemoveAll(filepath.Join(library.Dir(), "skills", "docx"))
|
|
r = &Report{}
|
|
Data(r)
|
|
if !gone(t, appdir.Config()) || len(r.Kept) != 0 {
|
|
t.Fatalf("empty library kept: %+v", r)
|
|
}
|
|
}
|
|
|
|
// Never the home, a root or a relative path, from a variable pointing at
|
|
// one; a folder inside another is listed once.
|
|
func TestFoldersNeverHome(t *testing.T) {
|
|
home, _ := os.UserHomeDir()
|
|
remove, _ := Folders()
|
|
if len(remove) == 0 {
|
|
t.Fatal("nothing to remove")
|
|
}
|
|
for _, p := range remove {
|
|
if !filepath.IsAbs(p) || within(home, p) {
|
|
t.Fatalf("would remove %s (home %s)", p, home)
|
|
}
|
|
for _, q := range remove {
|
|
if p != q && within(p, q) {
|
|
t.Fatalf("%s listed inside %s", p, q)
|
|
}
|
|
}
|
|
}
|
|
root := "/"
|
|
if runtime.GOOS == "windows" {
|
|
root = `C:\`
|
|
}
|
|
h := filepath.Join(root, "Users", "me")
|
|
for p, ok := range map[string]bool{
|
|
"": false,
|
|
"magpie": false,
|
|
root: false,
|
|
h: false,
|
|
filepath.Join(root, "Users"): false,
|
|
filepath.Join(h, ".config", "magpie"): true,
|
|
filepath.Join(root, "Users", "me2"): true,
|
|
} {
|
|
if removable(p, h) != ok {
|
|
t.Errorf("removable(%q) = %v", p, !ok)
|
|
}
|
|
}
|
|
}
|
|
|
|
// What the app starts as it quits removes magpie's files once the app is
|
|
// gone.
|
|
func TestAfterExit(t *testing.T) {
|
|
fakes(t, nil, map[string]bool{}, nil)
|
|
settings := filepath.Join(appdir.Config(), "settings.json")
|
|
write(t, settings, "{}")
|
|
cmd := exec.Command(os.Args[0], "-test.run=^$")
|
|
if err := cmd.Run(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
start := time.Now()
|
|
r := AfterExit(cmd.Process.Pid, time.Minute)
|
|
if time.Since(start) > 30*time.Second {
|
|
t.Fatal("waited on a process that had ended")
|
|
}
|
|
if r.Failed() || !gone(t, settings) {
|
|
t.Fatalf("report %+v", r)
|
|
}
|
|
}
|