Files
magpie/internal/cli/uninstall_cli.go
T
yetone 96a703a591 uninstall: magpie uninstall and Settings › Uninstall magpie put every agent back before magpie's files go
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 (e7f21dba5 for
  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, on 5d24bc6cc), 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/main 51f66fe8b (#4033, #3548).
2026-10-29 17:47:30 +08:00

132 lines
4.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package cli
import (
"context"
"fmt"
"os"
"strconv"
"strings"
"time"
"golang.org/x/term"
"github.com/yetone/magpie/internal/daemon"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/uninstall"
)
const uninstallUsage = "usage: magpie uninstall [--yes]"
// what uninstallCmd asks of the system; the tests put fakes in
var (
servedBy = gateway.ServedBy
serviceStatus = daemon.Get
runUninstall = uninstall.Run
)
// magpie uninstall [--yes]: every agent disconnected and put back as it
// was, magpie's service, login item and shortcuts removed, then its own
// files (uninstall.Run). Deleting magpie's folders by hand left Codex
// pointed at a gateway that was gone (Jevik_AI on X). It asks first on a
// terminal; with no terminal to ask on, only --yes does it.
func uninstallCmd(args []string) error {
yes := false
for _, a := range args {
switch a {
case "--yes", "-y":
yes = true
default:
return fmt.Errorf("unknown %q: nothing was changed\n%s", a, uninstallUsage)
}
}
// a magpie still running would serve, and keep writing its files,
// after its folders are gone: the service is stopped here, anything
// else is quit first
if s := servedBy(); s.Running && s.Pid != os.Getpid() {
if st, err := serviceStatus(); err != nil || !st.Installed || st.PID != 0 && st.PID != s.Pid {
how := "magpie"
switch s.Serves {
case "app", "tray":
how = "magpie's app"
case "serve", "tui", "web":
how = "magpie " + s.Serves
}
pid := ""
if s.Pid != 0 {
pid = " (pid " + strconv.Itoa(s.Pid) + ")"
}
return fmt.Errorf("%s is running%s: quit it first, or uninstall from the app's Settings › Uninstall magpie. Nothing was changed", how, pid)
}
}
connected := uninstall.Connected()
remove, keep := uninstall.Folders()
if !yes {
if !term.IsTerminal(int(os.Stdin.Fd())) {
return fmt.Errorf("this disconnects every agent and deletes magpie's settings, providers and usage; run it with --yes to do so without being asked")
}
fmt.Println("This takes magpie off this computer:")
if len(connected) > 0 {
var names []string
for _, a := range connected {
names = append(names, a.Name)
}
fmt.Println(" · disconnects", strings.Join(names, ", "), "and puts back what each had (what you changed by hand since stays)")
}
fmt.Println(" · removes magpie's background service, opening at login and shortcuts")
for _, p := range remove {
fmt.Println(" · deletes", p)
}
if keep != "" {
fmt.Println(" · keeps", keep, "(skills and servers your agents use)")
}
fmt.Print("Uninstall magpie? Your providers, keys and usage in magpie can't be brought back. [y/N] ")
line, _ := stdin().ReadString('\n')
if a := strings.ToLower(strings.TrimSpace(line)); a != "y" && a != "yes" {
return fmt.Errorf("nothing was changed")
}
}
r := runUninstall(context.Background())
for _, l := range r.Says() {
switch {
case strings.HasPrefix(l, "✓"):
fmt.Println(green.Render("✓") + l[len("✓"):])
case strings.HasPrefix(l, "!"):
fmt.Println(amber.Render("!") + l[1:])
default:
fmt.Println(muted.Render(l))
}
}
if r.CodexApp {
fmt.Println(amber.Render("!"), "the Codex app is still on what it read before: quit it and open it again")
}
if r.Exe != "" {
fmt.Println(muted.Render(" magpie itself is left at " + r.Exe + ": delete it to finish"))
}
if r.Failed() {
return fmt.Errorf("some of magpie couldn't be taken off; what is marked ! above is left")
}
fmt.Println(green.Render("✓"), "magpie is uninstalled")
return nil
}
// uninstallData is `magpie uninstall --data-after <pid>`, which the app
// starts as it quits from Settings › Uninstall magpie: once the app has
// ended, its files are deleted (WebView2 holds its folder open while the
// app runs).
func uninstallData(args []string) error {
if len(args) != 1 {
return fmt.Errorf("usage: magpie uninstall --data-after <pid>")
}
// 0 or less is no process: kill(0) asks this process group, which
// is alive as long as this runs, and the files went only at the wait's end
pid, err := strconv.Atoi(args[0])
if err != nil || pid <= 0 || pid == os.Getpid() {
return fmt.Errorf("--data-after takes the app's process id, not %q: nothing was changed", args[0])
}
r := uninstall.AfterExit(pid, 2*time.Minute)
for _, l := range r.Says() {
fmt.Println(l)
}
return nil
}