Files
magpie/internal/claudecode/keep.go
T
yetone 7d1f2ecba3 claudecode: magpie keeps the Claude Code it downloaded at the newest release
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.
2026-11-01 19:00:51 +08:00

82 lines
2.4 KiB
Go

package claudecode
import (
"context"
"fmt"
"log"
"os"
"time"
"github.com/yetone/magpie/internal/proc"
)
// When KeepCurrent first looks, and how often after that; vars for tests.
var (
keepFirst = time.Minute
keepEvery = 6 * time.Hour
)
// keep is what KeepCurrent reads of the machine; tests stand in for it.
var keep = struct {
own func() string
latest func(context.Context) (Release, error)
}{
own: func() string { return proc.FindTool("claude") },
latest: Latest,
}
// KeepCurrent keeps the Claude Code magpie downloaded at Claude Code's
// newest release while magpie serves: a minute after it starts and every six
// hours after. Every run magpie starts sets DISABLE_AUTOUPDATER, so nothing
// else updates it, and a container's download (in /config/cache, on the
// volume) would otherwise stay at the version first installed, image
// update after image update (alextjy on Discord). It does nothing when
// magpie downloaded none, when the machine has a Claude Code of its own
// (that one is run, and updates itself), or when DISABLE_AUTOUPDATER is set
// for magpie: Claude Code's own way of holding a version.
func KeepCurrent(ctx context.Context) {
t := time.NewTimer(keepFirst)
defer t.Stop()
for {
select {
case <-ctx.Done():
return
case <-t.C:
}
if said, err := keepOnce(ctx); err != nil {
log.Printf("Claude Code: keeping magpie's download current: %v", err)
} else if said != "" {
log.Print(said)
}
t.Reset(keepEvery)
}
}
// keepOnce updates magpie's download when a newer release is out, and says
// what it did ("" for nothing). The version an earlier look replaced goes
// first: no run has been started on it since then.
func keepOnce(ctx context.Context) (string, error) {
path, v := Downloaded()
if path == "" || keep.own() != "" || os.Getenv("DISABLE_AUTOUPDATER") != "" {
return "", nil
}
prune(v)
lctx, cancel := context.WithTimeout(ctx, 30*time.Second)
r, err := keep.latest(lctx)
cancel()
if err != nil {
return "", err
}
if !newer(r.Version, v) {
return "", nil
}
dctx, cancel := context.WithTimeout(ctx, 30*time.Minute)
defer cancel()
// the old version stays until the next look: a run being started now
// may have been handed its path
if _, err := install(dctx, r, nil); err != nil {
return "", fmt.Errorf("Claude Code %s: %w", r.Version, err)
}
return fmt.Sprintf("Claude Code: magpie's download updated from %s to %s", v, r.Version), nil
}