Files
magpie/internal/claudecode/keep.go
T
yetone e484b4ff26 update: a slow download still moving is never ended by a total time; Bun, Claude Code and pf-serve too
#5572: since 80a47abba a silent download is broken off after 30s and
tried again, but the update's download still had a 10-minute total
(the update client's Timeout, the GUI run's ctx, `magpie update`'s ctx).
A download under ~140 KB/s, still receiving bytes, failed the 83 MB
Windows exe at 10 minutes part way. The stall timer is what ends a dead
one now; nothing ends one that is still moving.

- internal/update: fetch downloads through fileClient, the client without
  its Timeout. The feed and notes keep the 10-minute client. fetch's stall
  timer runs from the ask to the last byte, so a server that never answers
  still ends at stallAfter (30s), and download's retries stay bounded
  (len(retryWaits)+1 = 6 tries), so a connection that keeps going silent
  ends in about 4 minutes.
- internal/gui (Mac Stage, Windows/Linux StageBinary): run's 10 minutes
  (checkWait, a var for tests) bound only LatestIn; the download runs
  under a ctx with no deadline.
- internal/cli `magpie update` (Mac Stage, Windows/Linux ReplaceBinary):
  the same split.
- Siblings with the same fixed cap:
  - new internal/stall: Start(ctx, after) gives a ctx that ends with
    ErrStalled once `after` goes by with no byte read through w.Reader.
  - Bun (internal/plugin getURLOnce): watched with a 60s stall;
    KeepBunUpdated's 10-minute ctx around CheckBun is gone.
  - Claude Code (internal/claudecode downloadOnce, ~200 MB): watched with
    a 60s stall; keepOnce's 30-minute ctx around install is gone.
  - pf-serve (internal/pii upgrade): its 10-minute ctx is gone; get
    already had a 60s stall and the client a 60s header timeout, as the
    model's download (no total) does.
- Left as they are: plugin installs and npm updates (KeepUpdated's 10
  minutes) run `bun install`, whose network is Bun's own, small tarballs;
  rtk (5 min client), skills (2 min client), skilltree and market reads
  are small files; Grok CLI install has no cap of its own (its caller's
  ctx). None of them carries a large download behind a wall-clock total.

Run on mote (mote-run), at 49f207c78 + this change; the targeted
set again after rebasing on d4a221f79 (ok):
- go test ./internal/stall ./internal/update ./internal/claudecode
  ./internal/pii -run 'Stall|Slow|Silen|TestDownload|TestInstall|TestKeep';
  go test ./internal/plugin -run Bun; go test ./internal/gui -run
  TestUpdate: ok
- new: TestSlowButMovingIsReadWhole, TestSilenceIsAStall (no answer, part
  way), TestDownloadSlowButMovingOutlastsTheClientTimeout (client Timeout
  300ms, body ~1.2s: one try, no retry), TestUpdateSlowDownloadOutlastsTheCheckBound
  (checkWait 200ms, body ~600ms: ready), TestDownloadThatGoesSilentIsAStall
  (claudecode), TestBunDownloadThatGoesSilentIsAStall. The silent server
  after its retries stays covered by TestDownloadStallEveryTimeSaysSo
  (6 tries, errStalled).
- go test -race -count=3 ./internal/stall ./internal/update -run
  'Stall|Slow|Silen': ok
- win-run (Windows 11): ./internal/stall ./internal/update
  ./internal/claudecode ./internal/plugin with the same -run, and
  ./internal/gui -run 'TestUpdateSlowDownload|TestUpdateDownloadSaysStepAndSpeed': ok
- wsl-run (Ubuntu 24.04), -tags nogui: the same packages and ./internal/gui
  -run TestUpdate: ok
- gofmt -l; go vet ./...; GOOS=linux go vet -tags nogui ./...;
  GOOS=windows go vet ./...; go build; GOOS=linux go build -tags nogui;
  GOOS=windows go build: clean
- Full suite: run after the push under the suite lock.

Broken by hand: fetch back on `client` fails
TestDownloadSlowButMovingOutlastsTheClientTimeout ("broke off ...
Client.Timeout or context cancellation while reading body"); the GUI's
download back under the check's ctx fails
TestUpdateSlowDownloadOutlastsTheCheckBound (state error, "context
deadline exceeded"); stall's reader not putting the timer off fails
TestSlowButMovingIsReadWhole (50 of 120 bytes); Bun's and Claude Code's
downloads unwatched fail their silent tests at the 20s bound ("context
deadline exceeded").

Not tried: a real update over a slow or throttled link, on any OS; a real
Bun or Claude Code download over one. The Mac's unpack and signature
check now run with no deadline (they had the 10 minutes before).

Fixes #5572
2026-11-07 06:09:42 +08:00

82 lines
2.5 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
}
// no bound on the whole: a slow download still moving finishes, and
// one that stops sending is ended by downloadStall (#5572). The old
// version stays until the next look: a run being started now may have
// been handed its path
if _, err := install(ctx, 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
}