alextjy on Discord runs magpie in Docker with Claude Code from `magpie claude-code install`, and asked whether a magpie update touches it and how it updates. The download lives in <cache>/claude-code (/config/cache, on the volume), so a new image keeps it. But nothing ever updated it: every run magpie starts sets DISABLE_AUTOUPDATER, so it stayed at the version first installed until the user ran install again. claudecode.KeepCurrent now runs while magpie serves (a WhileServing loop): a minute after start and every six hours it asks downloads.claude.ai for the latest release and downloads a newer one, checked against the manifest's SHA-256 and size as install does. The version it replaced is removed on the next look rather than at once, since a run being started may hold its path (Install, the CLI's, still removes it at once; a folder with a .part download in it is left alone). It does nothing when magpie downloaded none, when the machine has its own `claude` (that one is run and updates itself), or when DISABLE_AUTOUPDATER is set for magpie, Claude Code's own way to hold a version. A failed look keeps the download and is logged. It never downgrades. The status line of `magpie claude-code`, the Docker docs (en, zh, ja) and the bridge's subsystem page say so. Run: - wsl-run (Ubuntu 24.04, Go 1.26.8): go vet, then go test -race -v ./internal/claudecode/ -run 'Keep|Install|Find': all pass. - Broken by hand on WSL, each red: no newer() check (UpdatesMagpiesDownload, LeavesWhatIsntMagpiesToUpdate), no own() check (the_machine's_own), pruning at install (UpdatesMagpiesDownload), no prune on the next look (UpdatesMagpiesDownload). - Real use on WSL, sandbox HOME and XDG_CACHE_HOME: a throwaway program with a fake 2.1.1 download ran KeepCurrent against the real downloads.claude.ai. After 1m34s it logged "updated from 2.1.1 to 2.1.316", the kept binary's --version said "2.1.316 (Claude Code)", 2.1.1 was still there for the next look, and the loop ended with its context. - mote, at this tree: GOOS=windows and GOOS=linux go vet -tags nogui of both packages, go vet ./internal/claudecode/, go build ., then go test -tags nogui ./internal/claudecode/ ./internal/cli/ -timeout 45m: VET-OK; claudecode ok 0.680s, cli ok 56.673s. - gofmt -l clean. Not tried: the Docker image itself (no docker on the test boxes), and a Windows run whose replaced binary is still in use (prune leaves what it can't remove for the next look). Full suite not run: the change is the claudecode package, one WhileServing line and one CLI status line. A WSL run of ./internal/cli/ at the default 10m timeout timed out while the box ran other sessions' full suites; the 45m run above replaces it.
170 lines
5.2 KiB
Go
170 lines
5.2 KiB
Go
package claudecode
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// keepRelease serves 2.1.287 as downloads.claude.ai does and counts what is
|
|
// asked: the latest version, and the build's download.
|
|
func keepRelease(t *testing.T, latestV string) (asked, downloads *atomic.Int32) {
|
|
t.Helper()
|
|
const body = "#!/bin/sh\necho 2.1.287 '(Claude Code)'\n"
|
|
platform := thisPlatform(t)
|
|
asked, downloads = new(atomic.Int32), new(atomic.Int32)
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
switch r.URL.Path {
|
|
case "/latest":
|
|
asked.Add(1)
|
|
fmt.Fprint(w, latestV)
|
|
case "/2.1.287/manifest.json":
|
|
fmt.Fprint(w, manifest(platform, body))
|
|
case "/2.1.287/" + platform + "/" + exeName(runtime.GOOS):
|
|
downloads.Add(1)
|
|
fmt.Fprint(w, body)
|
|
default:
|
|
http.NotFound(w, r)
|
|
}
|
|
}))
|
|
t.Cleanup(srv.Close)
|
|
oldBase, oldKeep := Base, keep
|
|
Base = srv.URL
|
|
keep.own = func() string { return "" }
|
|
keep.latest = func(ctx context.Context) (Release, error) {
|
|
return latest(ctx, runtime.GOOS, runtime.GOARCH, false)
|
|
}
|
|
t.Cleanup(func() { Base, keep = oldBase, oldKeep })
|
|
t.Cleanup(func() { _ = Remove() })
|
|
t.Setenv("DISABLE_AUTOUPDATER", "")
|
|
return asked, downloads
|
|
}
|
|
|
|
// downloaded puts a Claude Code of version v where magpie keeps its own.
|
|
func downloaded(t *testing.T, v string) string {
|
|
t.Helper()
|
|
dir := filepath.Join(Root(), v)
|
|
if err := os.MkdirAll(dir, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p := filepath.Join(dir, exeName(runtime.GOOS))
|
|
if err := os.WriteFile(p, []byte("old"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return p
|
|
}
|
|
|
|
func TestKeepCurrentUpdatesMagpiesDownload(t *testing.T) {
|
|
asked, downloads := keepRelease(t, "2.1.287\n")
|
|
old := downloaded(t, "2.1.200")
|
|
|
|
said, err := keepOnce(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(said, "2.1.200 to 2.1.287") {
|
|
t.Errorf("said %q", said)
|
|
}
|
|
if p, v := Downloaded(); v != "2.1.287" || p == old {
|
|
t.Fatalf("Downloaded() = %q, %q; want 2.1.287", p, v)
|
|
}
|
|
// a run started a moment ago may hold the old path: it stays for now
|
|
if _, err := os.Stat(old); err != nil {
|
|
t.Errorf("the replaced version went at once: %v", err)
|
|
}
|
|
|
|
// the next look removes it, and downloads nothing again
|
|
if said, err := keepOnce(context.Background()); err != nil || said != "" {
|
|
t.Fatalf("second look: %q, %v", said, err)
|
|
}
|
|
if _, err := os.Stat(filepath.Dir(old)); !os.IsNotExist(err) {
|
|
t.Errorf("the replaced version is still there on the next look: %v", err)
|
|
}
|
|
if asked.Load() != 2 || downloads.Load() != 1 {
|
|
t.Errorf("asked %d times, downloaded %d; want 2 and 1", asked.Load(), downloads.Load())
|
|
}
|
|
}
|
|
|
|
func TestKeepCurrentLeavesWhatIsntMagpiesToUpdate(t *testing.T) {
|
|
for name, set := range map[string]func(t *testing.T){
|
|
"nothing downloaded": func(t *testing.T) {},
|
|
"the machine's own": func(t *testing.T) {
|
|
downloaded(t, "2.1.200")
|
|
keep.own = func() string { return "/usr/local/bin/claude" }
|
|
},
|
|
"held by the user": func(t *testing.T) { downloaded(t, "2.1.200"); t.Setenv("DISABLE_AUTOUPDATER", "1") },
|
|
"already the newest": func(t *testing.T) { downloaded(t, "2.1.287") },
|
|
"newer than the latest": func(t *testing.T) { downloaded(t, "2.1.300") },
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
_, downloads := keepRelease(t, "2.1.287")
|
|
set(t)
|
|
_, before := Downloaded()
|
|
if said, err := keepOnce(context.Background()); err != nil || said != "" {
|
|
t.Fatalf("keepOnce = %q, %v", said, err)
|
|
}
|
|
if downloads.Load() != 0 {
|
|
t.Errorf("downloaded %d times", downloads.Load())
|
|
}
|
|
if _, v := Downloaded(); v != before {
|
|
t.Errorf("Downloaded() is %q, was %q", v, before)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestKeepCurrentKeepsTheDownloadWhenTheReleaseCantBeRead(t *testing.T) {
|
|
keepRelease(t, "<!doctype html><title>Unavailable</title>")
|
|
old := downloaded(t, "2.1.200")
|
|
if _, err := keepOnce(context.Background()); err == nil {
|
|
t.Fatal("no error for a release that couldn't be read")
|
|
}
|
|
if p, v := Downloaded(); p != old || v != "2.1.200" {
|
|
t.Errorf("Downloaded() = %q, %q; want the old one kept", p, v)
|
|
}
|
|
|
|
keep.latest = func(context.Context) (Release, error) { return Release{}, errors.New("offline") }
|
|
if _, err := keepOnce(context.Background()); err == nil {
|
|
t.Fatal("no error when offline")
|
|
}
|
|
if p, _ := Downloaded(); p != old {
|
|
t.Errorf("Downloaded() = %q; want the old one kept", p)
|
|
}
|
|
}
|
|
|
|
func TestKeepCurrentLooksWhileServingAndEndsWithIt(t *testing.T) {
|
|
keepRelease(t, "2.1.287")
|
|
downloaded(t, "2.1.200")
|
|
oldFirst, oldEvery := keepFirst, keepEvery
|
|
keepFirst, keepEvery = 0, time.Hour
|
|
t.Cleanup(func() { keepFirst, keepEvery = oldFirst, oldEvery })
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
done := make(chan struct{})
|
|
go func() { KeepCurrent(ctx); close(done) }()
|
|
defer func() { cancel(); <-done }()
|
|
for deadline := time.Now().Add(20 * time.Second); ; time.Sleep(10 * time.Millisecond) {
|
|
if _, v := Downloaded(); v == "2.1.287" {
|
|
break
|
|
}
|
|
if time.Now().After(deadline) {
|
|
t.Fatal("KeepCurrent never updated the download")
|
|
}
|
|
}
|
|
cancel()
|
|
select {
|
|
case <-done:
|
|
case <-time.After(20 * time.Second):
|
|
t.Fatal("KeepCurrent didn't end with its context")
|
|
}
|
|
}
|