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).
358 lines
9.9 KiB
Go
358 lines
9.9 KiB
Go
// Package uninstall takes magpie off this computer: `magpie uninstall` and
|
||
// Settings › Uninstall magpie.
|
||
//
|
||
// Jevik_AI on X deleted magpie's folders by hand, and Codex's config.toml
|
||
// kept openai_base_url on 127.0.0.1:3425: every Codex turn failed with a
|
||
// 502, with nothing left that knew why. Deleting magpie's files never puts
|
||
// an agent back, so uninstalling does that first, the way each agent's
|
||
// Disconnect does (what magpie changed goes back, what the user changed
|
||
// by hand since stays theirs), then removes what magpie registered with
|
||
// the system, then its own files.
|
||
//
|
||
// What it leaves: the magpie executable itself (a running program can't
|
||
// delete itself everywhere, and the user put it there), and the Library's
|
||
// folder while it holds skills or servers the user's agents use.
|
||
package uninstall
|
||
|
||
import (
|
||
"context"
|
||
"errors"
|
||
"fmt"
|
||
"os"
|
||
"os/exec"
|
||
"path/filepath"
|
||
"slices"
|
||
"strconv"
|
||
"strings"
|
||
"time"
|
||
|
||
"github.com/yetone/magpie/internal/agent"
|
||
"github.com/yetone/magpie/internal/appdir"
|
||
"github.com/yetone/magpie/internal/autostart"
|
||
"github.com/yetone/magpie/internal/claudecode"
|
||
"github.com/yetone/magpie/internal/daemon"
|
||
"github.com/yetone/magpie/internal/library"
|
||
"github.com/yetone/magpie/internal/pii"
|
||
"github.com/yetone/magpie/internal/proc"
|
||
"github.com/yetone/magpie/internal/provider"
|
||
"github.com/yetone/magpie/internal/shortcut"
|
||
"github.com/yetone/magpie/internal/update"
|
||
)
|
||
|
||
// Done is one thing uninstalling did, or couldn't.
|
||
type Done struct {
|
||
What string `json:"what"`
|
||
Err string `json:"error,omitempty"`
|
||
}
|
||
|
||
// Report is what Run did.
|
||
type Report struct {
|
||
Agents []Done `json:"agents"`
|
||
System []Done `json:"system"`
|
||
Data []Done `json:"data"`
|
||
// Kept are folders left on purpose, each with why
|
||
Kept []Done `json:"kept,omitempty"`
|
||
// CodexApp: a Codex app still on the config it read before, which
|
||
// the user quits and opens
|
||
CodexApp bool `json:"codexApp,omitempty"`
|
||
// Exe is the magpie executable, left where it is
|
||
Exe string `json:"exe"`
|
||
}
|
||
|
||
// Failed says whether anything couldn't be done.
|
||
func (r *Report) Failed() bool {
|
||
for _, l := range [][]Done{r.Agents, r.System, r.Data} {
|
||
for _, d := range l {
|
||
if d.Err != "" {
|
||
return true
|
||
}
|
||
}
|
||
}
|
||
return false
|
||
}
|
||
|
||
// what Run asks of the system; the tests put fakes in
|
||
var (
|
||
agents = agent.All
|
||
endFailover = agent.EndCodexFailover
|
||
portRollouts = agent.PortCodexRollouts
|
||
restartCodexApp = provider.RestartCodexAppWhenIdle
|
||
removeService = func() error {
|
||
err := daemon.Uninstall()
|
||
if errors.Is(err, daemon.ErrUnsupported) {
|
||
return nil
|
||
}
|
||
return err
|
||
}
|
||
autostartOff = func() error { return autostart.Set(false) }
|
||
removeShortcut = shortcut.Remove
|
||
removeScheme = unregisterScheme
|
||
endPII = pii.Remove
|
||
executable = os.Executable
|
||
wired = (*agent.Agent).Wired
|
||
disconnectOne = disconnect
|
||
)
|
||
|
||
// codexAppWait is how long Run waits for a running Codex turn to end
|
||
// before it restarts the Codex app's app-server.
|
||
var codexAppWait = 30 * time.Second
|
||
|
||
// Connected are the agents magpie is in now, which Run disconnects.
|
||
func Connected() []*agent.Agent {
|
||
var out []*agent.Agent
|
||
for _, a := range agents() {
|
||
if wired(a) {
|
||
out = append(out, a)
|
||
}
|
||
}
|
||
return out
|
||
}
|
||
|
||
// Agents disconnects every agent magpie is in: the first half of
|
||
// uninstalling, which the app does while it still runs.
|
||
func Agents(ctx context.Context, r *Report) {
|
||
// failover first: it is a provider in Codex's config too
|
||
if _, err := endFailover(); err != nil {
|
||
r.Agents = append(r.Agents, Done{What: "Codex account failover", Err: err.Error()})
|
||
}
|
||
codex := false
|
||
for _, a := range Connected() {
|
||
err := disconnectOne(a)
|
||
d := Done{What: a.Name}
|
||
if err != nil {
|
||
d.Err = err.Error()
|
||
}
|
||
r.Agents = append(r.Agents, d)
|
||
codex = codex || a.ID == "codex"
|
||
}
|
||
// the threads Codex ran on magpie's models open without magpie
|
||
portRollouts(10 * time.Second)
|
||
if codex {
|
||
// the Codex app read the old config as its app-server started
|
||
if _, left, err := restartCodexApp(ctx, codexAppWait); left || err != nil {
|
||
r.CodexApp = true
|
||
}
|
||
}
|
||
}
|
||
|
||
// disconnect is the Agents page's Disconnect, and for an app whose
|
||
// settings can't be read while it is closed, its offline disconnect.
|
||
func disconnect(a *agent.Agent) error {
|
||
err := a.Disconnect()
|
||
var off *agent.RuntimeUnavailableError
|
||
if errors.As(err, &off) && off.Offline == agent.OfflineDisconnect && a.Native != nil && a.Native.ExecuteOffline != nil {
|
||
plan, perr := a.Native.Disconnect()
|
||
if perr != nil {
|
||
return perr
|
||
}
|
||
return a.Native.ExecuteOffline(plan)
|
||
}
|
||
return err
|
||
}
|
||
|
||
// System removes what magpie registered with the system: its background
|
||
// service, opening at login, its Start menu shortcut and links.
|
||
func System(r *Report) {
|
||
exe, _ := executable()
|
||
for _, s := range []struct {
|
||
what string
|
||
do func() error
|
||
}{
|
||
{"background service", removeService},
|
||
{"open at login", autostartOff},
|
||
{"Start menu shortcut", func() error { return removeShortcut(exe) }},
|
||
{"magpie:// links", func() error { return removeScheme(exe) }},
|
||
{"privacy model's helper", endPII},
|
||
} {
|
||
d := Done{What: s.what}
|
||
if err := s.do(); err != nil {
|
||
d.Err = err.Error()
|
||
}
|
||
r.System = append(r.System, d)
|
||
}
|
||
}
|
||
|
||
// Folders are magpie's own folders Data removes, and the Library's folder
|
||
// when it keeps that.
|
||
func Folders() (remove []string, keep string) {
|
||
home, _ := os.UserHomeDir()
|
||
add := func(p string) {
|
||
if removable(p, home) && !slices.Contains(remove, p) {
|
||
remove = append(remove, p)
|
||
}
|
||
}
|
||
cfg := appdir.Config()
|
||
if libraryInUse() {
|
||
keep = library.Dir()
|
||
}
|
||
add(cfg)
|
||
add(appdir.Cache())
|
||
if c, err := appdir.SystemCache(); err == nil {
|
||
add(c)
|
||
}
|
||
add(appdir.WebView())
|
||
add(claudecode.Root())
|
||
for _, p := range webData() {
|
||
add(p)
|
||
}
|
||
// a folder inside another is removed with it
|
||
var out []string
|
||
for _, p := range remove {
|
||
if !slices.ContainsFunc(remove, func(q string) bool { return q != p && within(p, q) }) {
|
||
out = append(out, p)
|
||
}
|
||
}
|
||
return out, keep
|
||
}
|
||
|
||
// libraryInUse says whether the Library holds skills, servers or hooks:
|
||
// an agent given one reads it from there, or has it from there, and it
|
||
// is the user's, so it stays.
|
||
func libraryInUse() bool {
|
||
es, err := os.ReadDir(library.Dir())
|
||
if err != nil {
|
||
return false
|
||
}
|
||
for _, e := range es {
|
||
if e.IsDir() {
|
||
if sub, _ := os.ReadDir(filepath.Join(library.Dir(), e.Name())); len(sub) > 0 {
|
||
return true
|
||
}
|
||
}
|
||
}
|
||
return false
|
||
}
|
||
|
||
// Data removes magpie's own files: settings, providers, sign-ins kept by
|
||
// magpie, usage, caches. With the Library in use, everything in magpie's
|
||
// folder but it goes.
|
||
func Data(r *Report) {
|
||
remove, keep := Folders()
|
||
for _, p := range remove {
|
||
d := Done{What: p}
|
||
var err error
|
||
if keep != "" && within(keep, p) {
|
||
err = removeAllBut(p, keep)
|
||
} else {
|
||
err = os.RemoveAll(p)
|
||
}
|
||
if err != nil {
|
||
d.Err = err.Error()
|
||
}
|
||
r.Data = append(r.Data, d)
|
||
}
|
||
if keep != "" {
|
||
r.Kept = append(r.Kept, Done{What: keep, Err: "skills and servers your agents use from magpie's Library"})
|
||
}
|
||
}
|
||
|
||
// removeAllBut removes dir but keep, which is inside it.
|
||
func removeAllBut(dir, keep string) error {
|
||
es, err := os.ReadDir(dir)
|
||
if errors.Is(err, os.ErrNotExist) {
|
||
return nil
|
||
}
|
||
if err != nil {
|
||
return err
|
||
}
|
||
var errs []error
|
||
for _, e := range es {
|
||
p := filepath.Join(dir, e.Name())
|
||
switch {
|
||
case samePath(p, keep):
|
||
case within(keep, p):
|
||
errs = append(errs, removeAllBut(p, keep))
|
||
default:
|
||
errs = append(errs, os.RemoveAll(p))
|
||
}
|
||
}
|
||
return errors.Join(errs...)
|
||
}
|
||
|
||
// Run uninstalls magpie: agents, then the system, then its files. The
|
||
// app runs Data itself once it has quit (WebView2 holds its folder open
|
||
// while it runs), so it calls Agents and System only.
|
||
func Run(ctx context.Context) *Report {
|
||
r := &Report{}
|
||
// the service first: one still serving would put magpie back
|
||
System(r)
|
||
Agents(ctx, r)
|
||
Data(r)
|
||
r.Exe, _ = executable()
|
||
return r
|
||
}
|
||
|
||
// AfterExit waits for process pid to end, for up to wait, then removes
|
||
// magpie's files: what the app starts as it quits.
|
||
func AfterExit(pid int, wait time.Duration) *Report {
|
||
until := time.Now().Add(wait)
|
||
for update.Alive(pid) && time.Now().Before(until) {
|
||
time.Sleep(200 * time.Millisecond)
|
||
}
|
||
r := &Report{}
|
||
Data(r)
|
||
return r
|
||
}
|
||
|
||
// startAfter starts cmd outside this process, which it outlives; the
|
||
// tests put a fake in
|
||
var startAfter = func(cmd *exec.Cmd) error {
|
||
cmd.Stdin, cmd.Stdout, cmd.Stderr = nil, nil, nil
|
||
detach(cmd)
|
||
if err := cmd.Start(); err != nil {
|
||
return err
|
||
}
|
||
return cmd.Process.Release()
|
||
}
|
||
|
||
// DataAfterExit starts `magpie uninstall --data-after <this pid>`, which
|
||
// removes magpie's files once this process, the app, has quit: the app's
|
||
// half of Run after Agents and System.
|
||
func DataAfterExit() error {
|
||
exe, err := executable()
|
||
if err != nil {
|
||
return err
|
||
}
|
||
return startAfter(proc.Command(exe, "uninstall", "--data-after", strconv.Itoa(os.Getpid())))
|
||
}
|
||
|
||
// removable says whether p can be one of magpie's folders: never one
|
||
// that holds more than magpie's, as the home, a root or a relative path
|
||
// would, from a variable set to one.
|
||
func removable(p, home string) bool {
|
||
return p != "" && filepath.IsAbs(p) && filepath.Dir(p) != p && (home == "" || !within(home, p))
|
||
}
|
||
|
||
// within says whether p is dir or inside it.
|
||
func within(p, dir string) bool {
|
||
rel, err := filepath.Rel(dir, p)
|
||
return err == nil && rel != ".." && !strings.HasPrefix(rel, ".."+string(filepath.Separator)) && !filepath.IsAbs(rel)
|
||
}
|
||
|
||
func samePath(a, b string) bool { return filepath.Clean(a) == filepath.Clean(b) }
|
||
|
||
// Says is the report as lines, for the terminal and the app's log.
|
||
func (r *Report) Says() []string {
|
||
var out []string
|
||
line := func(kind string, d Done) {
|
||
if d.Err != "" {
|
||
out = append(out, fmt.Sprintf("! %s %s: %s", kind, d.What, d.Err))
|
||
} else {
|
||
out = append(out, fmt.Sprintf("✓ %s %s", kind, d.What))
|
||
}
|
||
}
|
||
for _, d := range r.Agents {
|
||
line("disconnected", d)
|
||
}
|
||
for _, d := range r.System {
|
||
line("removed", d)
|
||
}
|
||
for _, d := range r.Data {
|
||
line("deleted", d)
|
||
}
|
||
for _, d := range r.Kept {
|
||
out = append(out, "· kept "+d.What+": "+d.Err)
|
||
}
|
||
return out
|
||
}
|