Commit Graph
1 Commits
Author SHA1 Message Date
Edward4226 b91bcf2606 library: a standalone Windows rtk gets rtk's own upgrade action (#2006, Edward4226; LoKi)
An rtk.exe that no package manager installed (rtk's own Windows release
unzipped into %USERPROFILE%\.local\bin, say) had no upgrader, so the
Library page showed RTK's update with no usable Upgrade. LoKi
(lokic7123-star) hit it on v0.1.1285 and again on 0.1.1320; Edward4226
reproduced it and wrote this patch (his bc2056804, author kept).

rtkUpgrader now gives such an rtk.exe the rtk-windows-release action
(shown as "rtk's own Windows release"): on amd64, when winget's folders,
cargo's folder and winget's own records/winget list didn't claim it.
UpgradeRTK then takes RTKLatest's version, downloads checksums.txt and
rtk-x86_64-pc-windows-msvc.zip from rtk-ai/rtk's release through
source.DoOfficial, checks the zip's SHA-256 against checksums.txt, takes
rtk.exe out of it, writes it beside the old one and swaps it in with
steady.Rename, putting the old one back if the swap fails.

Changed from Edward's patch in review:
- The action no longer names a version: it read rtkLatest under the lock
  RTKLatest holds while it asks GitHub (up to ~16s), so ReadRTK, a page
  draw, would wait on it. UpgradeRTK already falls back to the last known
  version through RTKLatest.
- A winget that couldn't say whether it owns the rtk (timed out, failed,
  couldn't run) is no answer, not "not winget's": askWingetOwns now
  returns (yes, known); only winget missing, exit 0 without rtk-ai.rtk,
  or 0x8A150014 (no package) count as known-no. The winget arm above is
  unchanged (yes only when winget listed it). Without this, a winget
  timeout handed a possibly winget-owned rtk to the standalone path.
- Only a zip member named rtk.exe is taken (no "any .exe" fallback, which
  could pick rtk-proxy's), and an oversized one is refused rather than
  cut short.
- An aside (rtk.exe.old-<pid>) an earlier upgrade couldn't remove, because
  an agent was running that rtk, is removed on the next upgrade.
- The arch is a var (rtkArch) so the upgrader test runs on arm64 hosts
  instead of skipping; the redundant winget/cargo folder checks are gone
  (the switch's earlier cases already return for those).
- Two of the patch's tests failed as written on macOS (ReplacesBinary
  counted VERSION as a leftover; UpgradeRunsTheInstall compared the
  script rtkPath finds there with rtk.exe's text); both fixed.
- New tests: TestRTKWindowsStandaloneAsksWinget (no winget / unlisted /
  listed / failed / hung), TestRTKWindowsReleaseZipNeedsRTKExe, the
  arm64 case, and a stale aside in TestRTKWindowsReleaseSwapsTheFile.

Run at the cherry-pick on origin/main ef6f3749d, with these changes:
- gofmt -l internal/library: nothing. go vet ./... (darwin),
  GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...: ok.
- go test -count=1 ./internal/library/ in a sandbox HOME: ok (207s).
  The full ./... suite was not run here; it runs in a combined suite.
  The last edit after that run was comments only (re-vetted).
- Red on the old code, each broken by hand and restored:
  - standalone arm returning nil (as origin/main has it):
    TestRTKWindowsStandaloneUpgrader "standalone: upgrader []; want
    rtk-windows-release" and TestRTKWindowsStandaloneAsksWinget
    "no winget: upgrader []".
  - winget's unknown taken as not-owned (Edward's semantics):
    TestRTKWindowsStandaloneAsksWinget "winget failed: upgrader
    [rtk-windows-release ...]; want none".
  - UpgradeRTK running the action as an installer:
    TestRTKWindowsReleaseReplacesBinary and ...UpgradeRunsTheInstall
    'exec: "rtk-windows-release": executable file not found'.

Real use on the Windows box (Windows amd64, sandbox USERPROFILE,
LOCALAPPDATA and APPDATA under C:\Users\yeton\magpie-2006-sb, PATH only
the sandbox .local\bin and System32; a throwaway driver calling ReadRTK
and UpgradeRTK, not committed):
- rtk.exe 1.0.0 from rtk's own release in .local\bin: ReadRTK showed
  latest 1.0.1, Upgrade "rtk's own Windows release"; UpgradeRTK fetched
  the real v1.0.1 release, `rtk --version` then said 1.0.1, the file's
  SHA-256 4f7ad08a...ed1e matches rtk.exe in the v1.0.1 zip, and the
  folder held rtk.exe alone.
- the same with the old rtk.exe running (rtk proxy ping -n 25): replaced
  to 1.0.1, rtk.exe.old-<pid> left while it ran; the next upgrade
  removed it.
- The library tests built for windows/amd64 ran on the box: the ones
  that don't need shell fakes pass (Upgrader, ZipNeedsRTKExe,
  ChecksumMismatch, Shown, UpgradeRunsTheInstall, SwapsTheFile).

Not tried:
- rtk's releases ship both rtk- and rtk-oss- editions; this always
  installs the default rtk- edition, as the sh installer path does, so an
  oss user upgraded this way gets the default edition.
- Windows on arm64: rtk has no arm64 Windows build; an arm64 magpie
  still offers no upgrade there. An amd64 magpie under emulation would
  take the amd64 path; not tried.
- A rtk.exe in a folder only an administrator can write (Program Files)
  fails with the write error; not tried.
- The GUI itself was not driven; the Upgrade text is ReadRTK's.

Co-reported-by: LoKi (lokic7123-star)

Full suite at this commit on 87d703c5c: set -o pipefail; go test -count=1
-timeout 45m ./... under the suite lock, every package ok (SUITE_EXIT 0).
2026-10-22 21:15:36 +08:00