Files
magpie/internal/library/rtk_upgrade.go
T
wizzy-yang e0a3b8c60a library, plugins: the RTK page and the Installed plugin list open without waiting on the package manager or the plugins' vendors (#1178)
The Library's RTK page took ~3.9s to open and the Plugins page's
Installed list ~2.2s, every time.

- Installed named each plugin's subscriptions from plugin.Providers,
  the whole providers RPC: each plugin in turn fetching its vendor's
  model list. The page uses only the names, which plugin.Cached keeps
  without starting the host (refreshed in the background when a sign-in
  or the plugins change). It now reads those: 2.2s to 13ms with seven
  plugins. A plugin added a moment ago has no names yet, so its row no
  longer claims it "signs in to nothing magpie can use" until the
  plugins have answered (plugin.Answered, the row's named).
- ReadRTK waited up to 3s for GitHub and the package manager; winget
  show refreshes its sources and takes ~7.7s, so every open waited the
  full 3s and #1025's "waiting for winget" tag never came from it.
  GitHub stays on the first read; the package manager is asked in the
  background, its answer kept 6 hours as before. 3.9s to 1.6s.
- ReadRTK and SetRTK detected the agents twice per call; once now.

wizzy-yang's PR head 12342aa3, which conflicted with main: plugins.go
keeps main's clash rows (plugin.Clashes) with the names from Cached,
idName filled from Cached too; plugins.js keeps main's middleware/agent
and clash branches ahead of the PR's named one; providers.go keeps
main's Told and Settle beside the PR's Answered.

Run: see the next commit.
2026-10-13 14:29:52 +08:00

617 lines
21 KiB
Go

package library
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strings"
"sync"
"time"
"github.com/yetone/magpie/internal/proc"
"github.com/yetone/magpie/internal/source"
)
// rtk's version is kept up to date the way it was installed: Homebrew's
// with brew upgrade, winget's with winget upgrade, cargo's with cargo
// install again, and one its own script put in a folder with that script
// again, into the same folder. Its latest release is read from GitHub, as
// its script does.
// rtkReleases redirects to rtk's latest release (…/releases/tag/v0.50.0);
// rtkReleasesAPI says it in JSON, rate-limited, for when that doesn't.
var (
rtkReleases = "https://github.com/rtk-ai/rtk/releases/latest"
rtkReleasesAPI = "https://api.github.com/repos/rtk-ai/rtk/releases/latest"
)
// rtkLatest is the latest release as last read: good for six hours, a
// failure for fifteen minutes.
var rtkLatest struct {
sync.Mutex
v string
next time.Time
}
// CheckLatest fills in rtk's latest release (see RTKLatest), and, when it is
// newer than this rtk, what the package manager that installed it has — but
// only what that said before, and within its own age (Waiting): asking a
// package manager is slow (`winget show` refreshes its sources — seconds, on a
// slow or unreachable network far more), and a read that waits for it holds up
// the whole tab for a grey tag beside the version. CheckChannel asks in the
// background instead, and the next read draws with its answer.
func (v *RTKView) CheckLatest() {
if v.Path == "" {
return
}
v.Latest = RTKLatest()
if v.Latest == "" || v.Version == "" || !newer(v.Latest, v.Version) {
return
}
c := upgraderOf(v.Path)
ch := rtkChannelName(c)
if ch == "" {
return
}
// what it said last: not knowing leaves Upgrade offered, which is what a
// page drawn before that answer arrives shows
if has, ok := rtkChannelCached(c); ok && has != "" && !newer(has, v.Version) {
v.Waiting, v.WaitingHas = ch, has
}
}
// CheckChannel asks the package manager upgrade runs what rtk it has, and
// keeps the answer for six hours (see rtkChannelLatest), so that the next read
// of the tab draws with it without waiting. It is asked in the background,
// after the tab has gone out: it is a question for the package manager, which
// takes seconds, and it answers nothing anyone needs unless a release newer
// than the one installed is out (#1025).
func CheckChannel(v *RTKView) {
if v.Path == "" || v.Latest == "" || v.Version == "" || !newer(v.Latest, v.Version) {
return
}
if c := upgraderOf(v.Path); rtkChannelName(c) != "" {
rtkChannelLatest(c)
}
}
// upgraderOf is rtkUpgrader; a var so tests can give an rtk winget's
// upgrade on any platform, with a fake winget.
var upgraderOf = rtkUpgrader
// rtkChannelName is the package manager upgrade c runs, as the page names
// it, when it is one whose rtk can lag behind rtk's GitHub release; "" for
// cargo (built from rtk's own repository) and rtk's script (its releases).
func rtkChannelName(c []string) string {
if len(c) == 0 {
return ""
}
switch c[0] {
case "brew":
return "Homebrew"
case "winget":
return "winget"
}
return ""
}
// rtkChannels is the newest rtk each package manager has, as last asked or
// as an upgrade through it left: good for six hours, a failure (not known)
// for fifteen minutes. asking marks a package manager being asked right now:
// the ask runs outside the lock, which is held only to read and write this
// (its own subprocess takes seconds, and a reader that waited for it would
// hold up the tab: #1025, the tag beside the version).
var rtkChannels = struct {
sync.Mutex
m map[string]rtkChannel
asking map[string]bool
}{m: map[string]rtkChannel{}, asking: map[string]bool{}}
type rtkChannel struct {
v string
next time.Time
}
// rtkChannelCached is what the package manager said last, and whether it was
// said within its own age, without asking it again. It is what a read that must
// not wait draws with (see RTKView.CheckLatest).
func rtkChannelCached(c []string) (string, bool) {
rtkChannels.Lock()
defer rtkChannels.Unlock()
e, ok := rtkChannels.m[c[0]]
if !ok || !time.Now().Before(e.next) {
return "", false
}
return e.v, true
}
// rtkChannelLatest is the newest rtk the package manager upgrade c runs
// has, "" when it didn't say. It is a var so tests can count the asks (see
// TestCheckLatestNeverAsksThePackageManager).
//
// What the package manager said last is given back without asking it again,
// and the ask itself runs with the lock let go: `winget show` refreshes its
// sources and takes seconds, and a reader that only wants what is known
// (rtkChannelCached, which draws the card) must not queue behind it. An ask
// already in flight for the same manager is not started twice; it answers
// with what that one said last, or "" while nothing has been said.
var rtkChannelLatest = func(c []string) string {
rtkChannels.Lock()
if e, ok := rtkChannels.m[c[0]]; ok && time.Now().Before(e.next) {
v := e.v
rtkChannels.Unlock()
return v
}
if rtkChannels.asking[c[0]] {
v := rtkChannels.m[c[0]].v
rtkChannels.Unlock()
return v
}
rtkChannels.asking[c[0]] = true
rtkChannels.Unlock()
v, err := askRTKChannelNow(c)
rtkChannels.Lock()
delete(rtkChannels.asking, c[0])
if err != nil {
rtkChannels.m[c[0]] = rtkChannel{next: time.Now().Add(15 * time.Minute)}
rtkChannels.Unlock()
return ""
}
rtkChannels.m[c[0]] = rtkChannel{v: v, next: time.Now().Add(6 * time.Hour)}
rtkChannels.Unlock()
return v
}
// rtkChannelHas records what an upgrade through c found: the newest rtk it
// has is the one now installed.
func rtkChannelHas(c []string, v string) {
if rtkChannelName(c) == "" || v == "" {
return
}
rtkChannels.Lock()
rtkChannels.m[c[0]] = rtkChannel{v: v, next: time.Now().Add(6 * time.Hour)}
rtkChannels.Unlock()
}
// askRTKChannelNow is askRTKChannel as the ask runs: a var so a test can make
// it slow without a package manager to run, on any platform (see
// TestRTKReadWhileThePackageManagerIsBeingAsked).
var askRTKChannelNow = askRTKChannel
// askRTKChannel asks Homebrew (as it last updated itself, as the Agents
// page's update does) or winget (its source, as winget upgrade would) for
// the newest rtk it has. Nothing is installed.
func askRTKChannel(c []string) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
var args []string
switch c[0] {
case "brew":
args = []string{"info", "--json=v2", "rtk"}
case "winget":
args = []string{"show", "--id", "rtk-ai.rtk", "--exact", "--accept-source-agreements", "--disable-interactivity"}
default:
return "", errors.New("no package manager")
}
cmd := proc.CommandContext(ctx, c[0], args...)
cmd.Stdin = nil
cmd.Env = append(os.Environ(), "HOMEBREW_NO_AUTO_UPDATE=1", "HOMEBREW_NO_ENV_HINTS=1", "NONINTERACTIVE=1")
out, err := cmd.Output()
if err != nil {
return "", err
}
return rtkChannelVersion(c[0], string(out))
}
// rtkChannelVersion reads the version out of brew info --json=v2 or winget
// show. winget's labels are in the system's language ("Version:",
// "版本:"), so its version is the first one after the line naming the
// package, which comes right before it.
func rtkChannelVersion(tool, out string) (string, error) {
if tool == "brew" {
var r struct {
Formulae []struct {
Versions struct{ Stable string }
}
}
if err := json.Unmarshal([]byte(out), &r); err != nil {
return "", err
}
if len(r.Formulae) > 0 {
if m := semver.FindStringSubmatch(" " + r.Formulae[0].Versions.Stable); m != nil {
return m[1], nil
}
}
return "", errors.New("brew info says no version")
}
_, rest, ok := strings.Cut(out, "[rtk-ai.rtk]")
if !ok {
return "", errors.New("winget show doesn't name rtk-ai.rtk")
}
for _, l := range strings.FieldsFunc(rest, func(r rune) bool { return r == '\n' || r == '\r' }) {
if l = strings.TrimSpace(l); l == "" {
continue
}
if m := semver.FindStringSubmatch(l); m != nil {
return m[1], nil
}
break // the version is the line after the package's
}
return "", errors.New("winget show says no version")
}
// RTKLatest is rtk's latest release, from GitHub unless it was read lately;
// "" when GitHub can't be reached and never could.
func RTKLatest() string {
rtkLatest.Lock()
defer rtkLatest.Unlock()
if time.Now().Before(rtkLatest.next) {
return rtkLatest.v
}
ver, err := latestRTK()
if err != nil {
rtkLatest.next = time.Now().Add(15 * time.Minute)
return rtkLatest.v // an older answer is better than none
}
rtkLatest.v, rtkLatest.next = ver, time.Now().Add(6*time.Hour)
return ver
}
func latestRTK() (string, error) {
c := &http.Client{
Timeout: 8 * time.Second,
CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse },
}
req, _ := http.NewRequest("HEAD", rtkReleases, nil)
req.Header.Set("User-Agent", "magpie")
if resp, err := source.Do(c, req); err == nil {
resp.Body.Close()
if _, tag, ok := strings.Cut(resp.Header.Get("Location"), "/releases/tag/"); ok {
if m := semver.FindStringSubmatch(tag); m != nil {
return m[1], nil
}
}
}
req, _ = http.NewRequest("GET", rtkReleasesAPI, nil)
req.Header.Set("User-Agent", "magpie")
withGitHubToken(req)
req.Header.Set("Accept", "application/vnd.github+json")
resp, err := source.Do(c, req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return "", fmt.Errorf("GitHub answered %s", resp.Status)
}
var r struct {
Tag string `json:"tag_name"`
}
body, _ := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
if err := json.Unmarshal(body, &r); err != nil {
return "", err
}
if m := semver.FindStringSubmatch(r.Tag); m != nil {
return m[1], nil
}
return "", fmt.Errorf("no version in %q", r.Tag)
}
// RTKNewer says whether version a is after b, by their numbers.
func RTKNewer(a, b string) bool { return newer(a, b) }
// newer says whether version a is after b, by their numbers.
func newer(a, b string) bool {
var x, y [3]int
fmt.Sscanf(a, "%d.%d.%d", &x[0], &x[1], &x[2])
fmt.Sscanf(b, "%d.%d.%d", &y[0], &y[1], &y[2])
for i := range x {
if x[i] != y[i] {
return x[i] > y[i]
}
}
return false
}
// rtkUpgrader is how the rtk at bin is brought up to date, by where it was
// installed; nil when magpie can't tell.
func rtkUpgrader(bin string) []string {
real, err := filepath.EvalSymlinks(bin)
if err != nil {
real = bin
}
have := func(name string) bool { _, err := exec.LookPath(name); return err == nil }
slash := filepath.ToSlash(real)
dir := filepath.Dir(bin)
wingetUp := []string{"winget", "upgrade", "--id", "rtk-ai.rtk", "--exact", "--silent", "--accept-package-agreements", "--accept-source-agreements", "--disable-interactivity"}
switch {
case strings.Contains(slash, "/Cellar/rtk/") || strings.Contains(slash, "/.linuxbrew/"):
if have("brew") {
return []string{"brew", "upgrade", "rtk"}
}
// winget's own folders: its package, or the link it puts in
// WinGet\Links to one installed with --location (#1527)
case rtkOnWindows && (strings.Contains(strings.ToLower(slash), "/winget/") || strings.Contains(strings.ToLower(filepath.ToSlash(bin)), "/winget/")):
if have("winget") {
return wingetUp
}
case dir == filepath.Join(home(), ".cargo", "bin"):
if have("cargo") {
return []string{"cargo", "install", "--git", "https://github.com/rtk-ai/rtk", "--force"}
}
case !rtkOnWindows:
// its own script, told the folder it is in (RTK_INSTALL_DIR): the
// one a link to it points into (Put RTK on PATH's), not the link's
if st, err := os.Lstat(bin); err == nil && st.Mode()&os.ModeSymlink != 0 {
dir = filepath.Dir(real)
}
if have("curl") {
return []string{"sh", "-c", "curl -fsSL " + rtkScript + " | sh", dir}
}
// installed with winget --location into a folder of the user's own
// (D:\codes\tools\rtk): winget's records say so, not the folder's name
case have("winget") && wingetOwnsRTK(bin, real):
return wingetUp
}
return nil
}
// rtkOnWindows is whether rtk is upgraded as Windows has it; a var so a
// test can ask a fake winget on any platform.
var rtkOnWindows = runtime.GOOS == "windows"
// wingetRecords is where winget's records say it put rtk-ai.rtk: the
// InstallLocation of its entries among Windows' installed programs, which
// winget writes for a portable package like rtk's, wherever --location
// put it. ok is false when they can't be read (not Windows). A var for
// tests.
var wingetRecords = rtkWingetLocations
// rtkWingetOwned is whether winget installed the rtk at a path, as last
// asked: asking can run winget, which takes seconds, and the Library page
// reads rtk's card each time it is drawn.
var rtkWingetOwned = struct {
sync.Mutex
m map[string]rtkOwned
}{m: map[string]rtkOwned{}}
type rtkOwned struct {
yes bool
next time.Time
}
// wingetOwnsRTK says whether winget installed the rtk at bin (real, with
// links followed), remembered for ten minutes.
func wingetOwnsRTK(bin, real string) bool {
key := strings.ToLower(real)
rtkWingetOwned.Lock()
defer rtkWingetOwned.Unlock()
if o, ok := rtkWingetOwned.m[key]; ok && time.Now().Before(o.next) {
return o.yes
}
yes := askWingetOwns(bin, real)
rtkWingetOwned.m[key] = rtkOwned{yes: yes, next: time.Now().Add(10 * time.Minute)}
return yes
}
// askWingetOwns asks winget's records whether the rtk at bin is the one it
// installed: the folder its record names holds it, so an rtk.exe put
// somewhere else by hand isn't taken for winget's. When there is no record
// to read, winget list --id rtk-ai.rtk is asked, briefly, whether winget
// has rtk installed at all.
func askWingetOwns(bin, real string) bool {
if locs, ok := wingetRecords(); ok && len(locs) > 0 {
for _, l := range locs {
if inFolder(real, l) || inFolder(bin, l) {
return true
}
}
return false
}
ctx, cancel := context.WithTimeout(context.Background(), wingetListTimeout)
defer cancel()
cmd := proc.CommandContext(ctx, "winget", "list", "--id", "rtk-ai.rtk", "--exact", "--accept-source-agreements", "--disable-interactivity")
cmd.Stdin = nil
out, err := cmd.Output()
// it lists the package by its id in any language, and says it has none
// with exit code 0x8A150014
return err == nil && strings.Contains(string(out), "rtk-ai.rtk")
}
// wingetListTimeout is how long winget list is given; a var for tests.
var wingetListTimeout = 15 * time.Second
// inFolder says whether path is folder dir or inside it, case aside, as
// Windows has paths.
func inFolder(path, dir string) bool {
if dir == "" {
return false
}
p, d := strings.ToLower(filepath.Clean(path)), strings.ToLower(filepath.Clean(dir))
return p == d || strings.HasPrefix(p, strings.TrimRight(d, `\/`)+string(filepath.Separator))
}
// UpgradeRTK brings rtk up to its latest release the way it was installed.
func UpgradeRTK() (*RTKView, error) {
rtkMu.Lock()
defer rtkMu.Unlock()
bin := rtkPath()
if bin == "" {
return nil, fmt.Errorf("rtk isn't installed — install it first (%s)", RTKURL)
}
c := upgraderOf(bin)
if c == nil {
return nil, fmt.Errorf("magpie can't tell how the rtk at %s was installed — update it the way you installed it", bin)
}
before := rtkVersion()
var env []string
if c[0] == "sh" {
env = []string{"RTK_INSTALL_DIR=" + c[3]}
c = c[:3]
}
timeout := 10 * time.Minute
if c[0] == "cargo" {
timeout = 30 * time.Minute // it builds rtk
}
// brew upgrade updates Homebrew first, for its newest rtk
// winget having no newer rtk than this one yet is no failure
none := false
if _, err := runRTKInstaller(c, timeout, rtkAdminFolder(c, bin), env...); err != nil {
var ie *installError
if none = errors.As(err, &ie) && c[0] == "winget" && uint32(ie.code) == wingetNoUpgrade; !none {
return nil, err
}
}
v := ReadRTK()
// the package manager just installed the newest it has
rtkChannelHas(c, v.Version)
v.CheckLatest()
if v.Latest != "" && newer(v.Latest, v.Version) {
switch {
case c[0] == "brew":
v.Note = fmt.Sprintf("Homebrew's RTK is %s so far; RTK %s is out, and Homebrew usually has it within a few days", v.Version, v.Latest)
case c[0] == "winget" && v.Version == before:
v.Note = fmt.Sprintf("winget's RTK is %s so far; RTK %s is out, and winget usually has it within a few days", v.Version, v.Latest)
case v.Version == before:
v.Note = fmt.Sprintf("RTK is still %s after %s; RTK %s is out", v.Version, shown(c), v.Latest)
}
}
if none && v.Note == "" {
v.Note = fmt.Sprintf("winget has no newer RTK than %s yet", v.Version)
}
return v, nil
}
// runInstaller runs an installer with nothing to answer it. A failure says
// which tool failed, how it ended and what it said that matters — not its
// command line, which was all the page had room for (#741: a red
// "winget upgrade --id rtk-ai.rtk --exact --silent ..." and nothing else).
func runInstaller(c []string, timeout time.Duration, env ...string) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
cmd := proc.CommandContext(ctx, c[0], c[1:]...)
cmd.Stdin = nil
cmd.Env = append(append(os.Environ(), "NONINTERACTIVE=1"), env...)
out, err := cmd.CombinedOutput()
if err != nil {
code := -1
var ee *exec.ExitError
switch {
case errors.Is(ctx.Err(), context.DeadlineExceeded):
err = fmt.Errorf("didn't finish in %s", timeout)
case errors.As(err, &ee) && ee.ExitCode() != -1:
code = ee.ExitCode()
default:
err = fmt.Errorf("couldn't run: %w", err)
}
return string(out), installFailed(c, string(out), code, err)
}
return string(out), nil
}
// installError is an installer that failed: code is its exit code, -1 when
// it has none (not started, timed out).
type installError struct {
code int
msg string
}
func (e *installError) Error() string { return e.msg }
// winget's exit codes are HRESULTs (AppInstallerErrors.h); the ones an
// upgrade or install of rtk-ai.rtk meets, said plainly.
const wingetNoUpgrade uint32 = 0x8A15002B // APPINSTALLER_CLI_ERROR_UPDATE_NOT_APPLICABLE
var wingetSays = map[uint32]string{
wingetNoUpgrade: "winget has no newer RTK than this one yet",
0x8A150014: "winget finds no rtk-ai.rtk package",
wingetNeedsAdmin: "winget needs an administrator for this RTK, installed for every user of this computer",
0x8A150050: "winget can't tell which version of RTK is installed, so it won't upgrade it",
0x8A150101: "RTK is in use — close the agents running it and try again",
0x8A150111: "RTK is in use — close the agents running it and try again",
0x8A150052: "winget couldn't put the new rtk.exe in place — if an agent is running rtk, close it and try again",
0x8A150107: "winget couldn't reach the network",
0x8A150008: "winget couldn't download RTK",
0x8A150011: "the download didn't match winget's checksum",
0x8A150045: "winget couldn't open its package source",
0x8A150046: "winget's source agreements weren't accepted",
0x8A15003A: "a policy on this machine blocks winget",
0x8A15010F: "a policy on this machine blocks the install",
0x8A15010C: "the install was cancelled",
}
// exitCode is a process's exit code as its platform writes it: a Windows
// HRESULT (winget's) in hex, anything else in decimal.
func exitCode(code int) string {
if code < 0 || code > 0xFFFF {
return fmt.Sprintf("0x%08X", uint32(code))
}
return fmt.Sprint(code)
}
// installFailed is the error for installer c, which printed out and ended
// with exit code code, or -1 and err when it didn't end on its own.
func installFailed(c []string, out string, code int, err error) error {
tool := c[0]
if tool == "sh" {
tool = "RTK's install script"
}
ie := &installError{code: code}
if code == -1 {
ie.msg = fmt.Sprintf("%s %v", tool, err)
} else {
ie.msg = fmt.Sprintf("%s failed (exit code %s)", tool, exitCode(code))
if c[0] == "winget" {
if s := wingetSays[uint32(code)]; s != "" {
ie.msg += ": " + s
}
}
}
if said := installerSaid(out, 3); said != "" {
ie.msg += " — " + said
}
return ie
}
var (
ansiSeq = regexp.MustCompile(`\x1b\[[0-9;?]*[ -/]*[@-~]`)
// a line that is only a spinner frame or a progress bar (winget's
// ██████▒▒▒ 1.20 MB / 3.40 MB, Homebrew's ###, curl's %)
progressLine = regexp.MustCompile(`^(?:[-\\|/]|[█▓▒░#=>.\s]*\d*(?:\.\d+)?\s*(?:[KMG]i?B\s*/\s*\d+(?:\.\d+)?\s*[KMG]i?B|%)?)$`)
)
// installerSaid is the last lines an installer printed that say something:
// without the spinner frames and progress bars winget writes to a pipe,
// each redrawn after a carriage return, or its licence boilerplate.
func installerSaid(out string, n int) string {
out = strings.ToValidUTF8(ansiSeq.ReplaceAllString(out, ""), "")
var keep []string
for _, l := range strings.FieldsFunc(out, func(r rune) bool { return r == '\n' || r == '\r' }) {
l = strings.TrimSpace(l)
if l == "" || progressLine.MatchString(l) || strings.ContainsAny(l, "█▒") ||
strings.HasPrefix(l, "This application is licensed to you by its owner") ||
strings.HasPrefix(l, "Microsoft is not responsible for") {
continue
}
keep = append(keep, l)
}
if len(keep) > n {
keep = keep[len(keep)-n:]
}
s := strings.Join(keep, " ")
if r := []rune(s); len(r) > 400 {
s = "…" + string(r[len(r)-400:])
}
return s
}