* update: say so when the `magpie` command is a copy the app left behind install.sh links the terminal's magpie into ~/.local/bin to the app's own binary, so it follows every GUI update for free. A command that arrived as a copied file — an older installer, a hand cp — keeps running its own build once magpie.app moves on, and a command behind the app is a stale build: one that predates the WebDAV short-write check (#531) truncated a remote backup on `magpie webdav now`, wedging the sync on every tick after. The gap is macOS-only: on Linux the app and the command are one file, and its update replaces it in place. magpie doesn't rewrite the user's PATH itself — that's the installer's job, and a background update has no business reaching there. What the command *is* says whether it can follow the app, so update.StaleCLI is a plain Lstat of the installer's own convention (MAGPIE_BIN_DIR, else ~/.local/bin): a regular file is behind, a link isn't, and nothing runs the old binary to ask its version. When it's behind, magpie says so where the user can see it: /api/state carries cliBehind and the window's status line says it once per run (the panel stays quiet, too small for advice), and `magpie update` adds the line after updating the app it found through the same copied command. TestStaleCLI pins the three file states over a temp bin dir; cli-behind.test.cjs (WebKit and Chromium, en and zh) fakes a state with cliBehind and checks the window says it, the panel doesn't, and a following command is never mentioned. go vet, and go test -tags nogui ./internal/update ./internal/gui under a temp HOME pass. * update: only say the command is behind when it is, and how to fix it Two things the review caught about the notice. A copy that isn't actually behind the app shouldn't nag. The standalone magpie-cli build is a legitimate regular file in ~/.local/bin beside the app, and the first cut warned about it on every window load however current it was. StaleCLI now takes the app's version and stays quiet unless the copy is a release that trails it — a copy at or ahead of the app, a build from source, and one that won't run are all left be. Reading the version runs the old binary, whose start-up migrates things, but that's the only way to tell a stale release from a current one, and it's reached only for a file already known by Lstat to be a copy, once per run. The advice named a step that didn't fix it. `magpie update` run from a copied command has no bundle to update (the copy isn't inside a .app), so it replaces the copy with another copy and the warning comes back. Both the window's line and `magpie update`'s now point at what actually works: re-run the installer, or link the command by hand — `ln -sf <app>/Contents/MacOS/magpie <cli>`, the link install.sh makes. TestStaleCLI covers the version decision (behind → say, current, ahead, source, unreadable → quiet) over a temp bin dir; the browser test's wording follows. Verified by mutation: dropping the version guard turns the stale case red, and Stat for Lstat turns the link case red. go vet, and go test -tags nogui ./internal/update ./internal/gui under a temp HOME pass. * gui: the window's copied-command advice is an Lstat with a per-version dismiss (yetone's review on #623) What: the window no longer runs the old binary's `version` to tell a stale copy from a current one. update.CopiedCLI is a bare Lstat, reached on every launch; the version read stays in update.StaleCLI, which only `magpie update` calls. The advice moved from a 9s status line to a small dialog with "Hide until the next version", kept in settings' cli-behind-quiet file as the version it was dismissed at, so the next app update asks once more. POST /api/cli-behind/quiet writes it. Why: `magpie version` isn't read-only — run() migrates settings before the command answers, so an old copy rewrote the user's real config with the old build's migrations (old MoveOffAccountIDs could drop fields), and the read on the first /api/state cost up to 2s on a cold start. An Lstat can't tell whether the copy trails, so what the window says is advice, not a warning that knows — dismissable, and worded as a copy rather than a stale one. Tests: TestCLIBehindIsLstatOnlyAndDismissible (a non-executable file is reported — nothing ran; dismiss quiets it and the file holds this version; the installer's symlink is quiet). cli-behind.test.cjs covers the dialog, its dismiss POST and the quiet panel, chromium+webkit, en+zh, 4/4. go vet, go build -tags nogui (darwin, GOOS=linux, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass (netproxy's TestBunThroughBridge fails the same way on the branch without this change). Co-Authored-By: Claude Code <noreply@anthropic.com> * gui: the dismiss survives a restart, and cliBehindVal is behind a lock (yetone's review on #623) What: the POST handler wrote cliBehindVal = "" while state() read it on other goroutines with no lock — a page dismissing while another view polled /api/state raced. cliBehindVal now sits behind cliBehindMu on both sides. The test also proves the promise the handler's one-line clear couldn't: after a simulated restart (fresh Once, empty value) the dismissed advice stays quiet, and the next version asks once more. Why: the restart case was untested — short-circuiting cliQuiet() left the suite green, because the handler's in-memory clear alone satisfied it. The quiet file is what actually carries the dismiss across runs. Tests: TestCLIBehindIsLstatOnlyAndDismissible extended with the restart and next-version legs; `if false && cliQuiet()` now fails it (mutation-checked). go vet, go test -tags nogui -race ./internal/gui ./internal/update pass; TestGatewayTakenOver fails the same way with -race on the unmodified branch (pre-existing). Co-Authored-By: Claude Code <noreply@anthropic.com> --------- Co-authored-by: Claude Code <noreply@anthropic.com>
89 lines
3.5 KiB
Go
89 lines
3.5 KiB
Go
package update
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/proc"
|
|
)
|
|
|
|
// StaleCLI is the path of a `magpie` command that is a copied file behind
|
|
// the app, or "" when there's nothing to say. install.sh links the command
|
|
// to magpie.app's binary so it follows every GUI update for free; a command
|
|
// that arrived as a copy — an older installer, a hand cp, or the standalone
|
|
// magpie-cli build — keeps running its own build once the app moves on, and
|
|
// a command behind the app is a stale build: one that predates the WebDAV
|
|
// short-write check (#531) truncated a remote backup on `magpie webdav now`,
|
|
// wedging the sync.
|
|
//
|
|
// A copy is only worth mentioning when it actually trails the app, so this
|
|
// reads the copy's version and stays quiet when it is at or ahead of
|
|
// appVersion, or isn't a release at all (a build from source, or one that
|
|
// won't run — none of those is a stale release to nag about). Reading the
|
|
// version runs the old binary, and that isn't read-only: magpie's start-up
|
|
// runs its migrations before `version` answers. So only `magpie update`,
|
|
// which the user started, asks this — the app's window uses CopiedCLI and
|
|
// its advice can be dismissed instead. magpie doesn't rewrite the user's
|
|
// PATH — the fix is theirs: re-run install.sh, or link it by hand, which is
|
|
// what the advice says. Only on the Mac, where the app and the command are
|
|
// two places; on Linux they are one file.
|
|
func StaleCLI(appVersion string) string {
|
|
cli := CopiedCLI()
|
|
if cli == "" {
|
|
return ""
|
|
}
|
|
old := cliVersion(cli)
|
|
if !Released(old) || !Newer(appVersion, old) {
|
|
return "" // a source build, an unreadable copy, or one at/above the app
|
|
}
|
|
return cli
|
|
}
|
|
|
|
// CopiedCLI is the command's path when it's a plain copied file rather than
|
|
// install.sh's link, or "" when it's absent, a link, or not a regular file.
|
|
// A bare Lstat — nothing is run, nothing is written — so the app's window
|
|
// can ask on every launch. It can't tell a stale copy from a current one
|
|
// (that needs the copy's version, which only `magpie update` may read), so
|
|
// what the window says off it is advice with a dismiss, not a warning that
|
|
// knows. Only on the Mac; on Linux the app and the command are one file.
|
|
func CopiedCLI() string {
|
|
if runtime.GOOS != "darwin" {
|
|
return ""
|
|
}
|
|
bin := os.Getenv("MAGPIE_BIN_DIR")
|
|
if bin == "" {
|
|
home := os.Getenv("HOME")
|
|
if home == "" {
|
|
return ""
|
|
}
|
|
bin = filepath.Join(home, ".local", "bin")
|
|
}
|
|
cli := filepath.Join(bin, "magpie")
|
|
st, err := os.Lstat(cli)
|
|
if err != nil || !st.Mode().IsRegular() {
|
|
return "" // absent, the installer's link, or nothing to point at
|
|
}
|
|
return cli
|
|
}
|
|
|
|
// cliVersion asks the binary at path what version it is, and is a var so
|
|
// tests need not build or run one. The command prints "magpie <version>".
|
|
// The wait is short: it runs while the app is starting to decide whether to
|
|
// say anything, so a copy that hangs must not stall it. Running the old
|
|
// binary isn't purely reading — magpie's start-up runs its migrations first
|
|
// — but that's the only way to tell a stale release from a current one, and
|
|
// it's reached only for a file already known to be a copy.
|
|
var cliVersion = func(path string) string {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
defer cancel()
|
|
out, err := proc.CommandContext(ctx, path, "version").Output()
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
return strings.TrimSpace(strings.TrimPrefix(strings.TrimSpace(string(out)), "magpie"))
|
|
}
|