Files
magpie/internal/shortcut/shortcut_windows.go
T
yetone 92e8edd7fc windows: magpie is listed in Installed apps, and its Uninstall puts every agent back
Jevik_AI on X uninstalled magpie and rebooted, and Codex's config.toml
still had openai_base_url = "http://127.0.0.1:3425/backend-api/codex";
Rk1ngggg asked whether an update cleans it. magpie's own uninstall
(Settings › About › Uninstall magpie, `magpie uninstall`) already
disconnects every agent and takes Codex's leftovers (#3866), but on
Windows magpie wasn't in Settings › Apps › Installed apps, so it was
removed the way Windows offers for a program it doesn't list: by
deleting the exe and folders, which runs no code.

shortcut.Ensure now writes
HKCU\Software\Microsoft\Windows\CurrentVersion\Uninstall\magpie
(DisplayName, DisplayIcon, Publisher, InstallLocation, UninstallString,
URLInfoAbout, NoModify, NoRepair) at every installed start, beside App
Paths, and Remove (uninstall's System step) takes it out the same way:
only while it names this exe, or an exe that is gone. Its Uninstall
runs `magpie.exe uninstall --window` (shortcut.UninstallArgs). magpie.exe
has no console, where the terminal's `magpie uninstall` would refuse
for want of one to ask on, so those exact args open the app's window
(or forward to the running app, OnSecondInstanceLaunch) on Settings ›
About with Uninstall magpie's question asked (uninstallView,
?uninstall=1). It asks once: a redraw of Settings doesn't ask again,
and Cancel removes nothing. magpie web opened at that link asks
nothing. Deleting the app by hand still runs no code (macOS
drag-to-Trash included); agent-wiring.md says so.

Run on mote at the pushed head (rebased onto be9dbce02):
- gofmt -l clean; go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./...: ok. go build, GOOS=linux go build -tags
  nogui, GOOS=windows go build: ok.
- go test -tags nogui ./internal/gui ./internal/cli ./internal/shortcut
  ./internal/uninstall: ok (TestCoreScriptBudget included).
- uninstall-settings.test.cjs and i18n-keys.test.cjs: 40 of 40
  (chromium and webkit; the new Installed-apps cases in en and zh at
  1100 and 420).
At d3f40bedd, the same change on c7ebb6f8d, under suite.lock:
- go test -tags nogui -timeout 45m ./...: ok, every package.
- The whole GUI suite (--test-concurrency=4): 22518 of 22800; the 282
  reds are in 53 files the change doesn't reach (model-chip-steady 40,
  usage-agents-hide 22, zero-priced-free 20, provider-fetch-again 14,
  ...), the full-suite reds of #5475; the numbers are on that issue.
  Their run at c7ebb6f8d alone is queued behind other sessions' suites.
- The first full run found the core scripts 147 bytes over
  TestCoreScriptBudget; the page's part was cut to one line (the
  budget is not raised).
On xiaomi-gpu (win-run): go test ./internal/shortcut, incl.
TestInstalledAppsEntry on the real HKCU registry under a test key: ok.

Broken by hand, each red:
- registerAppPath not calling registerUninstall: TestInstalledAppsEntry
  (UninstallString = "") on Windows.
- main.go not taking UninstallArgs to the window:
  TestInstalledAppsUninstallOpensTheWindow (unknown "--window").
- app.js not asking on ?uninstall=1: 4 of 4 Installed-apps GUI cases.
- app.js asking without its once guard (params.delete): the same 4
  ("asked again").

Not tried: a real Settings › Apps › Uninstall click on Windows with an
installed magpie (the test box's install belongs to other sessions); the
second-instance forward (OnSecondInstanceLaunch) has no test of its own.
2026-11-09 22:33:31 +08:00

194 lines
5.8 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 shortcut
import (
"errors"
"os"
"path/filepath"
"runtime"
"strings"
ole "github.com/go-ole/go-ole"
"github.com/go-ole/go-ole/oleutil"
"golang.org/x/sys/windows"
"golang.org/x/sys/windows/registry"
"github.com/yetone/magpie/internal/appdir"
)
// appPath is where Win+R and `start magpie` look magpie.exe up: the user's
// own, no administrator needed. A var so the test writes a key of its own.
var appPath = `Software\Microsoft\Windows\CurrentVersion\App Paths\magpie.exe`
func registerAppPath(exe string) {
if k, _, err := registry.CreateKey(registry.CURRENT_USER, appPath, registry.SET_VALUE); err == nil {
_ = k.SetStringValue("", exe)
_ = k.SetStringValue("Path", filepath.Dir(exe))
k.Close()
}
registerUninstall(exe)
}
// uninstallKey is magpie's entry in Settings › Apps › Installed apps (and
// Control Panel's Programs and Features), the user's own. With none,
// Windows' own way to remove a program didn't list magpie, so it was
// deleted by hand, which runs no code: Codex kept openai_base_url on a
// gateway that was gone (Jevik_AI on X). Its Uninstall runs magpie's own
// (UninstallCommand), which puts every agent back first. A var so the
// test writes a key of its own.
var uninstallKey = `Software\Microsoft\Windows\CurrentVersion\Uninstall\magpie`
// registerUninstall writes the entry for exe, at every start, so it
// follows an exe that was moved.
func registerUninstall(exe string) {
k, _, err := registry.CreateKey(registry.CURRENT_USER, uninstallKey, registry.SET_VALUE)
if err != nil {
return
}
defer k.Close()
for name, v := range map[string]string{
"DisplayName": Name,
"DisplayIcon": exe + ",0",
"Publisher": Name,
"InstallLocation": filepath.Dir(exe),
"UninstallString": UninstallCommand(exe),
"URLInfoAbout": "https://usemagpie.ai",
} {
_ = k.SetStringValue(name, v)
}
// Uninstall is the one button: there is nothing to modify or repair
_ = k.SetDWordValue("NoModify", 1)
_ = k.SetDWordValue("NoRepair", 1)
}
// unregisterUninstall takes the entry out while it is exe's, or its exe
// is gone; another magpie's stays.
func unregisterUninstall(exe string) error {
k, err := registry.OpenKey(registry.CURRENT_USER, uninstallKey, registry.QUERY_VALUE)
if err != nil {
return nil // none
}
v, _, err := k.GetStringValue("DisplayIcon")
k.Close()
v = strings.TrimSuffix(v, ",0")
if err == nil && v != "" && !strings.EqualFold(filepath.Clean(v), filepath.Clean(exe)) {
if _, err := os.Stat(v); err == nil {
return nil // another magpie's
}
}
return registry.DeleteKey(registry.CURRENT_USER, uninstallKey)
}
func registeredAppPath() bool {
k, err := registry.OpenKey(registry.CURRENT_USER, appPath, registry.QUERY_VALUE)
if err != nil {
return false
}
k.Close()
return true
}
func startMenuLink() (string, error) {
dir, err := programs()
if err != nil {
return "", err
}
return filepath.Join(dir, Name+".lnk"), nil
}
func writeShortcut(lnk, exe string) (bool, error) { return write(lnk, exe) }
// programs is the user's Start menu Programs folder.
func programs() (string, error) {
if p, err := windows.KnownFolderPath(windows.FOLDERID_Programs, windows.KF_FLAG_CREATE); err == nil && p != "" {
return p, nil
}
a := appdir.Getenv("APPDATA")
if a == "" {
return "", errors.New("no Start menu folder")
}
p := filepath.Join(a, `Microsoft\Windows\Start Menu\Programs`)
return p, os.MkdirAll(p, 0o755)
}
// write has the shortcut at lnk open exe, in exe's folder, with its icon,
// and reports whether it had to: one already opening exe is left as it is.
// It is Windows' own shell link, made by the shell's WScript.Shell.
func write(lnk, exe string) (bool, error) {
// COM is set up per thread; this one keeps the goroutine
runtime.LockOSThread()
defer runtime.UnlockOSThread()
if err := ole.CoInitializeEx(0, ole.COINIT_APARTMENTTHREADED); err != nil {
var oe *ole.OleError
switch {
case !errors.As(err, &oe):
return false, err
case oe.Code() == 1: // S_FALSE: set up already, still ours to undo
defer ole.CoUninitialize()
case oe.Code() != 0x80010106: // RPC_E_CHANGED_MODE: set up otherwise, usable
return false, err
}
} else {
defer ole.CoUninitialize()
}
unk, err := oleutil.CreateObject("WScript.Shell")
if err != nil {
return false, err
}
defer unk.Release()
shell, err := unk.QueryInterface(ole.IID_IDispatch)
if err != nil {
return false, err
}
defer shell.Release()
// the shortcut at lnk, read in when there is one
v, err := oleutil.CallMethod(shell, "CreateShortcut", lnk)
if err != nil {
return false, err
}
sc := v.ToIDispatch()
defer sc.Release()
prop := func(name string) string {
p, err := oleutil.GetProperty(sc, name)
if err != nil {
return ""
}
defer p.Clear()
return p.ToString()
}
if !Stale(prop("TargetPath"), prop("WorkingDirectory"), exe) {
return false, nil
}
for name, val := range map[string]any{
"TargetPath": exe,
"WorkingDirectory": filepath.Dir(exe),
"IconLocation": exe + ",0",
"Description": Name,
} {
if _, err := oleutil.PutProperty(sc, name, val); err != nil {
return false, err
}
}
if _, err := oleutil.CallMethod(sc, "Save"); err != nil {
return false, err
}
return true, nil
}
// unregisterAppPath takes magpie's App Paths entry out while it opens exe,
// or an exe that is gone, and its Installed apps entry the same way.
func unregisterAppPath(exe string) error {
uerr := unregisterUninstall(exe)
k, err := registry.OpenKey(registry.CURRENT_USER, appPath, registry.QUERY_VALUE)
if err != nil {
return uerr // none
}
v, _, err := k.GetStringValue("")
k.Close()
if err == nil && v != "" && !strings.EqualFold(filepath.Clean(v), filepath.Clean(exe)) {
if _, err := os.Stat(v); err == nil {
return uerr // another magpie's
}
}
return errors.Join(uerr, registry.DeleteKey(registry.CURRENT_USER, appPath))
}