Files
magpie/internal/plugin/bunupdate.go
T
yetone 23c6e865a7 plugin: a musl Linux (Alpine) runs plugins on Bun's musl build, kept apart from the glibc one (#1877)
emptyteeth found it: bunTarget picked Bun's build from GOOS/GOARCH alone,
so Alpine got bun-linux-x64-baseline, a glibc build whose interpreter
(/lib64/ld-linux-x86-64.so.2) Alpine lacks. exec then said "fork/exec
…/bun/1.4.2/magpie-bun: no such file or directory", which reads as a
missing file, and no plugin could be added or run.

- bunTarget: a musl Linux gets bun-linux-x64-musl-baseline or
  bun-linux-aarch64-musl. The musl check moves out of internal/claudecode
  into internal/libc (libc.Musl, unchanged rule: Anthropic's installer's
  /lib/libc.musl-*.so.1), which claudecode, the plugin host and the CLI
  test that skipped on musl now share.
- bunSums: the two musl builds' checksums, checked against Bun v1.4.2's
  own SHASUMS256.txt (downloaded from the GitHub release; they match the
  issue's numbers).
- Cache: a musl Linux keeps its Buns and state.json under bun/musl, so a
  machine whose C library changed never runs the other build, nor takes
  its state, whose Bad may list versions only the glibc build failed
  (CheckBun on Alpine set aside every newer Bun). glibc, macOS and Windows
  stay in bun/<v>, so a Bun downloaded before is used as it is, with no
  new download.
- A bun on disk that exec can't start with ENOENT says it is a build for
  another C library and names MAGPIE_BUN (cantStart), for the host's start
  and for bun add/bun update. The same for a $MAGPIE_BUN.
- Bun's musl build needs libstdc++ and libgcc, which a bare Alpine lacks:
  the loader then prints "Error loading shared library libstdc++.so.6"
  above ~40 "Error relocating" lines, and bun add cut it off. bun add,
  bun update and a host that died starting now say which library is
  missing and `apk add libstdc++ libgcc` (missingLibs, whyDied).

Decided: a Bun that can't be started at all is not counted as crashed.
The fallback goes to the version before, which is the same build for
this system and fails the same way, and setting the current one aside
would mark a good version bad for good. The libc check happens before the
download instead (the target), so Alpine never fetches the glibc build.

Run:
- docker alpine:3.20 (arm64, and amd64 under emulation), sandbox HOME in
  the container, linux nogui builds. origin/main's binary reproduces the
  report: "bun add …: fork/exec /sbx/.cache/magpie/bun/1.4.2/magpie-bun:
  no such file or directory". This one on bare Alpine downloads
  bun/musl/1.4.2 and says "this system lacks libstdc++.so.6 … apk add
  libstdc++ libgcc"; after apk add, `magpie plugin add
  @magpie-community/opencode-cursor-auth` adds it and `plugin list` starts
  the host and lists cursor (arm64 and amd64), with the old glibc
  bun/1.4.2 left beside it unused. With libstdc++ removed again, plugin
  list says the library; MAGPIE_BUN pointed at the glibc build gives the
  C library message.
- New tests (bunmusl_test.go, libc_test.go), each broken by hand and seen
  red: no musl branch (TestBunTargetForMusl, TestMuslBunKeptApartFromGlibc),
  no bun/musl root (TestMuslBunKeptApartFromGlibc), cantStart returning
  the error as is (TestCantStartSaysCLibrary,
  TestHostOnUnstartableBunSaysCLibrary, which fails with the reporter's
  exact message), host.go without it, no musl sum (TestBunDefaultSums),
  no hint in bun add (TestMissingLibsNamesThem) or in the host's start
  (TestHostOnBunLackingLibsSaysWhich), muslAt always false (TestMuslAt).
  -race -count=20 on them: green.
- gofmt -l; go vet ./... on darwin, GOOS=linux -tags nogui, GOOS=windows;
  go build for darwin, linux nogui, windows; go test -tags nogui for
  internal/plugin, libc, claudecode, cli in a sandbox HOME, and
  go test -tags nogui ./... (all green). One earlier run of
  internal/plugin with the other three packages went red, and its output
  was lost to a tail; four reruns of it, alone and together, were green,
  and the new tests were green under -race -count=20, so which test it
  was isn't known.

Not tried: a glibc Linux end to end after the change (the unit test
covers that its Bun stays at bun/<v>); a musl Linux other than Alpine
(the detection is unchanged and looks for Alpine's libc.musl-*.so.1).
The old glibc bun/<v> on an Alpine machine stays on disk (~90 MB); it
isn't removed, as a volume shared with a glibc container could be using
it.
2026-10-13 07:25:22 +08:00

322 lines
9.4 KiB
Go

package plugin
// Bun kept up to date, as the plugins are: every six hours magpie asks
// GitHub for Bun's newest release and, once it has been out two days (a
// release with a bad bug is usually followed by a fix within them),
// downloads it, checks its checksum, tries it and switches the plugin
// host to it, a reply streaming through the old host finishing first.
// BunVersion is the floor: magpie never runs an older Bun. The Bun it ran
// before stays on disk, and a new one the host won't start on is set
// aside for the one before, never tried again.
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"slices"
"strings"
"time"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/steady"
"github.com/yetone/magpie/internal/update"
)
// bunSettle is how long a Bun release is out before magpie takes it.
const bunSettle = 48 * time.Hour
// bunState is what magpie keeps about the Bun it runs, in bun/state.json.
type bunState struct {
// Current is the Bun the host runs on, "" for BunVersion.
Current string `json:"current,omitempty"`
// Previous is the one it ran on before, kept to fall back on.
Previous string `json:"previous,omitempty"`
// Bad are versions that failed their try or the host's start.
Bad []string `json:"bad,omitempty"`
Checked time.Time `json:"checked,omitzero"`
}
// bunRoot is where magpie keeps its Buns and their state.json. A musl
// Linux keeps them under bun/musl: one version is a different build there,
// so a machine whose C library changed under it (a container's image
// moved from Debian to Alpine, or back) never runs the other one's build,
// nor takes its state, whose Bad may list versions only the wrong build
// failed (#1877). Everywhere else they stay in bun itself, where a Bun
// downloaded before is.
func bunRoot() string {
root := filepath.Join(filepath.Dir(catalog.CachePath()), "bun")
if t, err := bunTarget(); err == nil && strings.Contains(t, "-musl") {
root = filepath.Join(root, "musl")
}
return root
}
func bunDirOf(v string) string { return filepath.Join(bunRoot(), v) }
// bunExeOf is where magpie keeps Bun v: named magpie-bun, so a proxy app's
// PROCESS-NAME rule can tell the requests plugins make (and their installs)
// from any other Bun's (#1048).
func bunExeOf(v string) string { return filepath.Join(bunDirOf(v), magpieBun()) }
func magpieBun() string { return "magpie-" + bunExe() }
func bunStatePath() string { return filepath.Join(bunRoot(), "state.json") }
func readBunState() bunState {
var s bunState
if b, err := steady.ReadFile(bunStatePath()); err == nil {
_ = json.Unmarshal(b, &s)
}
return s
}
func writeBunState(s bunState) error {
b, err := json.MarshalIndent(s, "", " ")
if err != nil {
return err
}
if err := os.MkdirAll(bunRoot(), 0o755); err != nil {
return err
}
return writeWhole(bunStatePath(), b)
}
func haveBun(v string) bool {
exe := bunExeOf(v)
if _, err := os.Stat(exe); err == nil {
return true
}
// a Bun downloaded before it was named magpie-bun takes the name
_ = os.Rename(filepath.Join(bunDirOf(v), bunExe()), exe)
_, err := os.Stat(exe)
return err == nil
}
// usable is whether v may be run: no older than the floor, not set aside.
func (s bunState) usable(v string) bool {
return v != "" && !update.Newer(BunVersion, v) && !slices.Contains(s.Bad, v)
}
// BunInUse is the version of the Bun plugins run on (or will, once it is
// downloaded): the newest magpie took, else BunVersion.
func BunInUse() string {
if b := os.Getenv("MAGPIE_BUN"); b != "" {
bunMu.Lock()
defer bunMu.Unlock()
if _, ok := envBunVersion[b]; !ok {
envBunVersion[b] = bunReported(b)
}
return envBunVersion[b]
}
bunMu.Lock()
defer bunMu.Unlock()
return inUseLocked()
}
func inUseLocked() string {
s := readBunState()
if s.usable(s.Current) && haveBun(s.Current) {
return s.Current
}
return BunVersion
}
// envBunVersion is what each $MAGPIE_BUN said it is, asked once.
var envBunVersion = map[string]string{}
var bunVersionRe = regexp.MustCompile(`^\d+\.\d+\.\d+$`)
// bunReported is the version a bun says it is, "" when it won't say.
func bunReported(exe string) string {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
out, err := command(ctx, exe, "--version").Output()
if err != nil {
return ""
}
return strings.TrimSpace(string(out))
}
// bunLatest is Bun's newest release and when it was published (tests stub it).
var bunLatest = func(ctx context.Context) (string, time.Time, error) {
b, err := getURL(ctx, "https://api.github.com/repos/oven-sh/bun/releases/latest", 4<<20)
if err != nil {
return "", time.Time{}, err
}
var r struct {
Tag string `json:"tag_name"`
Published time.Time `json:"published_at"`
Draft bool `json:"draft"`
Pre bool `json:"prerelease"`
}
if err := json.Unmarshal(b, &r); err != nil {
return "", time.Time{}, err
}
v := strings.TrimPrefix(r.Tag, "bun-v")
if r.Draft || r.Pre || !bunVersionRe.MatchString(v) {
return "", time.Time{}, fmt.Errorf("Bun's latest release is %q", r.Tag)
}
return v, r.Published, nil
}
// tryBun runs a downloaded Bun on what the host needs of it: it says its
// version, and runs a script with fetch, AsyncLocalStorage and the file
// system (tests stub it).
var tryBun = func(ctx context.Context, exe, v string) error {
if got := bunReported(exe); got != v {
return fmt.Errorf("it says it is %q", got)
}
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
script := `const { AsyncLocalStorage } = require("node:async_hooks"); const fs = require("node:fs"); const s = new AsyncLocalStorage();` +
`s.run(1, async () => { await Promise.resolve(); if (s.getStore() !== 1 || typeof fetch !== "function" || !fs.existsSync(process.execPath)) process.exit(3); console.log("ok") })`
out, err := command(ctx, exe, "-e", script).Output()
if err != nil {
var ee *exec.ExitError
if errors.As(err, &ee) && len(ee.Stderr) > 0 {
return fmt.Errorf("%w: %s", err, strings.TrimSpace(string(ee.Stderr)))
}
return err
}
if strings.TrimSpace(string(out)) != "ok" {
return fmt.Errorf("it printed %q", strings.TrimSpace(string(out)))
}
return nil
}
// CheckBun takes Bun's newest release when it is newer than the one in
// use, out two days and not set aside: downloaded, checked, tried, then
// switched to, the host restarted gracefully. It says the version taken,
// "" when none was.
func CheckBun(ctx context.Context) (string, error) {
if os.Getenv("MAGPIE_BUN") != "" {
return "", nil
}
latest, published, err := bunLatest(ctx)
if err != nil {
return "", err
}
bunMu.Lock()
s := readBunState()
s.Checked = time.Now().UTC().Truncate(time.Second)
use := inUseLocked()
if !update.Newer(latest, use) || !s.usable(latest) || time.Since(published) < bunSettle {
err := writeBunState(s)
bunMu.Unlock()
return "", err
}
bunMu.Unlock()
exe := bunExeOf(latest)
if !haveBun(latest) {
if err := downloadBun(ctx, latest, exe); err != nil {
return "", fmt.Errorf("downloading Bun %s: %w", latest, err)
}
}
if err := tryBun(ctx, exe, latest); err != nil {
if ctx.Err() != nil {
return "", err
}
setAside(latest, fmt.Sprintf("its try failed: %s", err))
return "", fmt.Errorf("Bun %s: %w", latest, err)
}
bunMu.Lock()
s = readBunState()
if use != BunVersion || haveBun(BunVersion) {
s.Previous = use
}
s.Current = latest
s.Checked = time.Now().UTC().Truncate(time.Second)
err = writeBunState(s)
if err == nil {
pruneBuns(s)
}
bunMu.Unlock()
if err != nil {
return "", err
}
log.Printf("plugins: Bun %s → %s", use, latest)
if Running() {
Restart()
}
return latest, nil
}
// setAside marks v as not to be run again and goes back to the Bun before
// it, removing it from disk.
func setAside(v, why string) {
log.Printf("plugins: Bun %s set aside, %s", v, why)
bunMu.Lock()
defer bunMu.Unlock()
s := readBunState()
if !slices.Contains(s.Bad, v) {
s.Bad = append(s.Bad, v)
}
if s.Current == v {
s.Current, s.Previous = s.Previous, ""
}
_ = writeBunState(s)
_ = os.RemoveAll(bunDirOf(v))
}
// fallBack is the Bun to start the host on when it wouldn't start on the
// one in use: the one before, when there is one to go back to.
func fallBack() (string, bool) {
bunMu.Lock()
defer bunMu.Unlock()
s := readBunState()
if s.Current == "" || !s.usable(s.Current) {
return "", false
}
prev := s.Previous
if prev == "" {
prev = BunVersion
}
if prev == s.Current || !s.usable(prev) || !haveBun(prev) {
return "", false
}
return bunExeOf(prev), true
}
// pruneBuns removes the Buns neither in use nor kept to fall back on.
func pruneBuns(s bunState) {
keep := map[string]bool{s.Current: true, s.Previous: true}
if s.Previous == "" {
keep[BunVersion] = true
}
es, _ := os.ReadDir(bunRoot())
for _, e := range es {
if e.IsDir() && bunVersionRe.MatchString(e.Name()) && !keep[e.Name()] {
_ = os.RemoveAll(filepath.Join(bunRoot(), e.Name()))
}
}
}
// KeepBunUpdated looks for a newer Bun a little after magpie starts, then
// every bunEvery, while there are plugins to run on it.
func KeepBunUpdated(ctx context.Context) {
t := time.NewTimer(2 * time.Minute)
defer t.Stop()
for {
select {
case <-ctx.Done():
return
case <-t.C:
}
if len(Load().Plugins) > 0 && HasBun() {
cctx, cancel := context.WithTimeout(ctx, 10*time.Minute)
if _, err := CheckBun(cctx); err != nil {
log.Printf("plugins: looking for a newer Bun: %s", err)
}
cancel()
}
t.Reset(bunEvery)
}
}