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).
152 lines
5.0 KiB
Go
152 lines
5.0 KiB
Go
// Package shortcut puts magpie in the Start menu on Windows. There it is
|
|
// one exe with no installer, so nothing else would: the first start of the
|
|
// desktop app makes a Magpie shortcut in the user's Start menu opening the
|
|
// exe it runs as, and Windows search finds it by that. It is made once:
|
|
// magpie remembers it did, and a shortcut the user deleted stays deleted
|
|
// (#508); one still there is pointed at the exe again when that was moved.
|
|
// A portable magpie (a data folder beside it) makes none and registers
|
|
// nothing, leaving no trace on the machine. Elsewhere it does nothing —
|
|
// the Mac has the app in Applications, Linux its own ways.
|
|
package shortcut
|
|
|
|
import (
|
|
"errors"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/yetone/magpie/internal/appdir"
|
|
)
|
|
|
|
// Name is what the shortcut is called, and what Windows search finds.
|
|
const Name = "Magpie"
|
|
|
|
// StateName is the file in magpie's folder recording that the shortcut was
|
|
// made, and for which exe.
|
|
const StateName = "start-menu-shortcut"
|
|
|
|
// what Ensure asks of the system; the tests put fakes in
|
|
var (
|
|
// startMenu is where the shortcut goes (…\Start Menu\Programs\Magpie.lnk)
|
|
startMenu = startMenuLink
|
|
// writeLink has the shortcut at lnk open exe, and reports whether it had to
|
|
writeLink = writeShortcut
|
|
// register has Win+R and `start magpie` find exe
|
|
register = registerAppPath
|
|
// registered reports whether a magpie registered itself there before
|
|
registered = registeredAppPath
|
|
)
|
|
|
|
// Ensure makes the shortcut on the first start, keeps it pointed at this
|
|
// magpie after that while it is there, and never makes it again once the
|
|
// user deleted it. A portable magpie makes none. It fails quietly — magpie
|
|
// runs the same without one — and is skipped with MAGPIE_NO_SHORTCUT=1
|
|
// (tests, sandboxes).
|
|
func Ensure() {
|
|
if os.Getenv("MAGPIE_NO_SHORTCUT") == "1" {
|
|
return
|
|
}
|
|
exe, err := os.Executable()
|
|
if err != nil {
|
|
return
|
|
}
|
|
if p, err := filepath.EvalSymlinks(exe); err == nil {
|
|
exe = p
|
|
}
|
|
if err := ensure(Target(exe), appdir.Portable(), filepath.Join(appdir.Config(), StateName)); err != nil {
|
|
log.Println("start menu shortcut:", err)
|
|
}
|
|
}
|
|
|
|
// errNoStartMenu is startMenu's answer where there is none (off Windows).
|
|
var errNoStartMenu = errors.New("no Start menu here")
|
|
|
|
// ensure is Ensure for exe, portable being the data folder of a portable
|
|
// magpie ("" installed) and state the file remembering the shortcut was
|
|
// made.
|
|
func ensure(exe, portable, state string) error {
|
|
if portable != "" {
|
|
return nil
|
|
}
|
|
lnk, err := startMenu()
|
|
if errors.Is(err, errNoStartMenu) {
|
|
return nil
|
|
}
|
|
// a magpie from before the state file registered itself at every start
|
|
before := registered()
|
|
register(exe)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, gone := os.Stat(lnk)
|
|
if gone != nil {
|
|
if _, err := os.Stat(state); err == nil {
|
|
// made once already: the user deleted it, and it stays deleted
|
|
return nil
|
|
}
|
|
if before {
|
|
// an older magpie made it at its every start, and it is gone:
|
|
// the user deleted it, so it isn't made again now either
|
|
return remember(state, exe)
|
|
}
|
|
}
|
|
if _, err := writeLink(lnk, exe); err != nil {
|
|
return err
|
|
}
|
|
return remember(state, exe)
|
|
}
|
|
|
|
// remember records in state that the shortcut was made, for exe.
|
|
func remember(state, exe string) error {
|
|
if err := os.MkdirAll(filepath.Dir(state), 0o755); err != nil {
|
|
return err
|
|
}
|
|
return os.WriteFile(state, []byte(exe+"\n"), 0o644)
|
|
}
|
|
|
|
// aside is what an update moves the exe it replaces to, or stages the new
|
|
// one as: magpie.exe.old, magpie.exe.old-2…, magpie.exe.new
|
|
var aside = regexp.MustCompile(`(?i)(\.exe)\.(old(-\d+)?|new)$`)
|
|
|
|
// Target is what the shortcut opens for exe: exe itself, but for a magpie
|
|
// left running from where an update moved it aside, the exe in its place,
|
|
// which is what the next start runs. An update replaces the exe where it
|
|
// is, so the shortcut stays good through it.
|
|
func Target(exe string) string {
|
|
return aside.ReplaceAllString(exe, "$1")
|
|
}
|
|
|
|
// Stale reports whether a shortcut opening target in dir is not one for
|
|
// exe: there is none yet, or the exe was moved. Windows paths are the same
|
|
// in any case.
|
|
func Stale(target, dir, exe string) bool {
|
|
same := func(a, b string) bool {
|
|
return a != "" && strings.EqualFold(filepath.Clean(a), filepath.Clean(b))
|
|
}
|
|
return !same(target, exe) || !same(dir, filepath.Dir(exe))
|
|
}
|
|
|
|
// what Remove asks of the system besides startMenu; the tests put fakes in
|
|
var unregister = unregisterAppPath
|
|
|
|
// Remove takes out what Ensure made, for magpie uninstall: the Start menu
|
|
// shortcut, the App Paths entry while it names exe (or an exe gone), and
|
|
// the record of having made them. A portable magpie made none, and an
|
|
// entry another copy of magpie registered stays that copy's.
|
|
func Remove(exe string) error {
|
|
var errs []error
|
|
if lnk, err := startMenu(); err == nil {
|
|
if err := os.Remove(lnk); err != nil && !os.IsNotExist(err) {
|
|
errs = append(errs, err)
|
|
}
|
|
} else if !errors.Is(err, errNoStartMenu) {
|
|
errs = append(errs, err)
|
|
}
|
|
if err := unregister(Target(exe)); err != nil {
|
|
errs = append(errs, err)
|
|
}
|
|
return errors.Join(errs...)
|
|
}
|