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.
298 lines
8.5 KiB
Go
298 lines
8.5 KiB
Go
// Package claudecode finds the Claude Code magpie's Claude subscription
|
|
// runs, and downloads Anthropic's own build of it when the user asks, for a
|
|
// machine with none: a server or a container (Jorben on Discord).
|
|
//
|
|
// The build comes from where Anthropic's installer (claude.ai/install.sh)
|
|
// takes it: the release's manifest.json names each platform's SHA-256 and
|
|
// size, and the binary is checked against both before it is kept. It is
|
|
// kept in magpie's cache folder, not on PATH, so it stands in for no agent:
|
|
// it is only what the bridge and the sign-in run.
|
|
package claudecode
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"runtime"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/yetone/magpie/internal/appdir"
|
|
"github.com/yetone/magpie/internal/libc"
|
|
"github.com/yetone/magpie/internal/proc"
|
|
)
|
|
|
|
// Base is where Anthropic publishes Claude Code's builds, as its installer
|
|
// downloads them; a var for tests.
|
|
var Base = "https://downloads.claude.ai/claude-code-releases"
|
|
|
|
// Root is the folder magpie keeps the Claude Code it downloaded in, one
|
|
// folder per version.
|
|
func Root() string { return filepath.Join(appdir.Cache(), "claude-code") }
|
|
|
|
func exeName(goos string) string {
|
|
if goos == "windows" {
|
|
return "claude.exe"
|
|
}
|
|
return "claude"
|
|
}
|
|
|
|
var versionRe = regexp.MustCompile(`^\d+\.\d+\.\d+(-[0-9A-Za-z.-]+)?$`)
|
|
|
|
// Downloaded is the Claude Code magpie downloaded and its version, or ""
|
|
// when there is none: the newest of Root's versions that has the binary.
|
|
func Downloaded() (path, version string) {
|
|
ents, err := os.ReadDir(Root())
|
|
if err != nil {
|
|
return "", ""
|
|
}
|
|
for _, e := range ents {
|
|
v := e.Name()
|
|
if !e.IsDir() || !versionRe.MatchString(v) {
|
|
continue
|
|
}
|
|
p := filepath.Join(Root(), v, exeName(runtime.GOOS))
|
|
if st, err := os.Stat(p); err != nil || st.IsDir() {
|
|
continue
|
|
}
|
|
if version == "" || newer(v, version) {
|
|
path, version = p, v
|
|
}
|
|
}
|
|
return path, version
|
|
}
|
|
|
|
// newer is whether version a is after b (numbers compared one by one).
|
|
func newer(a, b string) bool {
|
|
pa, pb := strings.SplitN(strings.SplitN(a, "-", 2)[0], ".", 3), strings.SplitN(strings.SplitN(b, "-", 2)[0], ".", 3)
|
|
for i := range 3 {
|
|
x, _ := strconv.Atoi(pa[i])
|
|
y, _ := strconv.Atoi(pb[i])
|
|
if x != y {
|
|
return x > y
|
|
}
|
|
}
|
|
return a > b
|
|
}
|
|
|
|
// Find is the Claude Code to run on this machine: the user's own (on PATH
|
|
// or where its installers put it, proc.FindTool), else the one magpie
|
|
// downloaded; "" when there is neither.
|
|
func Find() string {
|
|
if p := proc.FindTool("claude"); p != "" {
|
|
return p
|
|
}
|
|
p, _ := Downloaded()
|
|
return p
|
|
}
|
|
|
|
// InstallHint is what an error about a missing Claude Code adds: how to
|
|
// have magpie download it.
|
|
const InstallHint = "or run `magpie claude-code install` to download Anthropic's own build for magpie"
|
|
|
|
// Platform is the name Anthropic's manifest gives the build for goos/goarch
|
|
// (musl: a Linux whose C library is musl, as Alpine's).
|
|
func Platform(goos, goarch string, musl bool) (string, error) {
|
|
arch := map[string]string{"amd64": "x64", "arm64": "arm64"}[goarch]
|
|
if arch == "" {
|
|
return "", fmt.Errorf("Claude Code has no build for %s/%s", goos, goarch)
|
|
}
|
|
switch goos {
|
|
case "darwin":
|
|
return "darwin-" + arch, nil
|
|
case "linux":
|
|
if musl {
|
|
return "linux-" + arch + "-musl", nil
|
|
}
|
|
return "linux-" + arch, nil
|
|
case "windows":
|
|
return "win32-" + arch, nil
|
|
}
|
|
return "", fmt.Errorf("Claude Code has no build for %s", goos)
|
|
}
|
|
|
|
// Release is a build of Claude Code for this machine, as the manifest
|
|
// names it.
|
|
type Release struct {
|
|
Version string
|
|
Platform string
|
|
URL string
|
|
Checksum string // SHA-256, hex
|
|
Size int64
|
|
}
|
|
|
|
// Latest asks for the newest release (the "latest" channel, Claude Code's
|
|
// own default) and its build for this machine.
|
|
func Latest(ctx context.Context) (Release, error) {
|
|
return latest(ctx, runtime.GOOS, runtime.GOARCH, libc.Musl())
|
|
}
|
|
|
|
func latest(ctx context.Context, goos, goarch string, musl bool) (Release, error) {
|
|
platform, err := Platform(goos, goarch, musl)
|
|
if err != nil {
|
|
return Release{}, err
|
|
}
|
|
b, err := get(ctx, Base+"/latest", 256)
|
|
if err != nil {
|
|
return Release{}, err
|
|
}
|
|
v := strings.TrimSpace(string(b))
|
|
if !versionRe.MatchString(v) {
|
|
// an HTML page in its place: a region downloads.claude.ai doesn't serve
|
|
return Release{}, fmt.Errorf("%s/latest didn't name a version (got %q): downloads.claude.ai may not be reachable from here", Base, short(v))
|
|
}
|
|
b, err = get(ctx, Base+"/"+v+"/manifest.json", 1<<20)
|
|
if err != nil {
|
|
return Release{}, err
|
|
}
|
|
var m struct {
|
|
Platforms map[string]struct {
|
|
Binary string `json:"binary"`
|
|
Checksum string `json:"checksum"`
|
|
Size int64 `json:"size"`
|
|
} `json:"platforms"`
|
|
}
|
|
if err := json.Unmarshal(b, &m); err != nil {
|
|
return Release{}, fmt.Errorf("Claude Code %s's manifest: %w", v, err)
|
|
}
|
|
p, ok := m.Platforms[platform]
|
|
if !ok {
|
|
return Release{}, fmt.Errorf("Claude Code %s has no %s build", v, platform)
|
|
}
|
|
if ok, _ := regexp.MatchString(`^[0-9a-f]{64}$`, p.Checksum); !ok {
|
|
return Release{}, fmt.Errorf("Claude Code %s's manifest has no checksum for %s", v, platform)
|
|
}
|
|
if p.Size <= 0 {
|
|
return Release{}, fmt.Errorf("Claude Code %s's manifest has no size for %s", v, platform)
|
|
}
|
|
bin := p.Binary
|
|
if bin == "" || strings.ContainsAny(bin, `/\`) {
|
|
bin = exeName(goos)
|
|
}
|
|
return Release{Version: v, Platform: platform, URL: Base + "/" + v + "/" + platform + "/" + bin, Checksum: p.Checksum, Size: p.Size}, nil
|
|
}
|
|
|
|
func short(s string) string {
|
|
if len(s) > 60 {
|
|
return s[:60] + "…"
|
|
}
|
|
return s
|
|
}
|
|
|
|
func get(ctx context.Context, url string, limit int64) ([]byte, error) {
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
res, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer res.Body.Close()
|
|
if res.StatusCode != http.StatusOK {
|
|
return nil, fmt.Errorf("%s: %s", url, res.Status)
|
|
}
|
|
b, err := io.ReadAll(io.LimitReader(res.Body, limit+1))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if int64(len(b)) > limit {
|
|
return nil, fmt.Errorf("%s: larger than expected", url)
|
|
}
|
|
return b, nil
|
|
}
|
|
|
|
// Install downloads r into Root, checks its size and SHA-256 against the
|
|
// manifest's, and keeps it as the Claude Code magpie runs when the machine
|
|
// has none of its own; the versions downloaded before are removed. progress,
|
|
// when set, is told the bytes so far. It returns the binary's path.
|
|
func Install(ctx context.Context, r Release, progress func(done int64)) (string, error) {
|
|
dir := filepath.Join(Root(), r.Version)
|
|
if !versionRe.MatchString(r.Version) {
|
|
return "", fmt.Errorf("not a version: %q", r.Version)
|
|
}
|
|
if err := os.MkdirAll(dir, 0o755); err != nil {
|
|
return "", err
|
|
}
|
|
exe := filepath.Join(dir, exeName(runtime.GOOS))
|
|
tmp := exe + ".part"
|
|
if err := download(ctx, r, tmp, progress); err != nil {
|
|
os.Remove(tmp)
|
|
return "", err
|
|
}
|
|
if err := os.Rename(tmp, exe); err != nil {
|
|
os.Remove(tmp)
|
|
return "", err
|
|
}
|
|
// one version kept: what a run takes is the newest, and each is ~240 MB
|
|
ents, _ := os.ReadDir(Root())
|
|
for _, e := range ents {
|
|
if e.IsDir() && e.Name() != r.Version && versionRe.MatchString(e.Name()) {
|
|
_ = os.RemoveAll(filepath.Join(Root(), e.Name()))
|
|
}
|
|
}
|
|
return exe, nil
|
|
}
|
|
|
|
func download(ctx context.Context, r Release, to string, progress func(int64)) error {
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, r.URL, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
res, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer res.Body.Close()
|
|
if res.StatusCode != http.StatusOK {
|
|
return fmt.Errorf("%s: %s", r.URL, res.Status)
|
|
}
|
|
out, err := os.OpenFile(to, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o755)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
h := sha256.New()
|
|
n, err := io.Copy(io.MultiWriter(out, h, progressWriter(progress)), io.LimitReader(res.Body, r.Size+1))
|
|
if cerr := out.Close(); err == nil {
|
|
err = cerr
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if n != r.Size {
|
|
return fmt.Errorf("the download is %d bytes, the manifest says %d", n, r.Size)
|
|
}
|
|
if got := hex.EncodeToString(h.Sum(nil)); got != r.Checksum {
|
|
return errors.New("the download's SHA-256 doesn't match the one in Anthropic's manifest")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type progressFunc func(int64)
|
|
|
|
func progressWriter(f func(int64)) io.Writer {
|
|
var done int64
|
|
return progressFunc(func(n int64) {
|
|
done += n
|
|
if f != nil {
|
|
f(done)
|
|
}
|
|
})
|
|
}
|
|
|
|
func (f progressFunc) Write(p []byte) (int, error) {
|
|
f(int64(len(p)))
|
|
return len(p), nil
|
|
}
|
|
|
|
// Remove deletes every Claude Code magpie downloaded.
|
|
func Remove() error { return os.RemoveAll(Root()) }
|