5 Commits
Author SHA1 Message Date
yetone 60ebaea033 claude: the newest Claude Code on the machine runs, and an old one's sign-in says it is too old (zi_xiang5806 on X)
zi_xiang5806's in-magpie Claude sign-in ended "Sign-in didn't finish":
the claude magpie ran was an npm 2.1.38 first on PATH, which has no
`auth login --claudeai` ("error: unknown option '--claudeai'"), while
`claude update` had installed 2.1.325 into another Node after it.

- proc.FindTools lists every `claude` (each PATH folder, then where
  installers put it), deduped by real path, cached like FindTool.
- claudecode.Own (behind claudecode.Find, so the sign-in, the gateway's
  bridge, bili, keep and `magpie claude-code` all get it) runs the
  newest. The first on PATH is passed over only for one whose version
  is known and newer; an unreadable version is unknown, not old.
  Versions are read first where each is installed, running nothing
  (npm package.json via proc.PackageVersion, the native installer's
  versions/<v>, Homebrew's Caskroom/claude-code/<v>); only when one is
  known that way is another asked --version (VersionOf, kept per file
  until it changes, an unknown for a minute). With one claude, or none
  known, nothing is run.
- A sign-in whose claude says "unknown option"/"unknown command" now
  says Claude Code is too old, with its version and path, and to run
  `claude update` (or npm install -g @anthropic-ai/claude-code@latest).

Tests: TestClaudeSignInRunsTheNewestClaudeCode and
TestClaudeSignInSaysClaudeCodeIsTooOld put two fake npm-layout claudes
(2.1.38 first, 2.1.325 second) on PATH in a sandbox HOME; both were red
with Find reverted to the first on PATH (the first with the reporter's
exact "unknown option '--claudeai'"). TestOwn*, TestInstalledVersion,
TestFindTools.

Run at base 96d1b93b2 (the 5 commits rebased over touch none of these
files): full `go test -count=1 -timeout 45m ./...` on mote, 101 ok,
exit 0 (internal/gateway ok 1036s; an earlier version that ran
--version on every candidate turned ~20 gateway Claude bridge tests red
through their fake claude, which is why nothing is run when no version
is known where it is installed). claudecode, proc and provider
TestClaudeSignIn also ok on wsl-run (Linux) and claudecode on win-run;
GOOS=linux and GOOS=windows go vet of proc, claudecode, provider ok;
gofmt clean.

Not tried: a real sign-in with a real old CLI and account; WSL's
too-old message; the Agents page still shows the first on PATH.
2026-11-15 14:30:16 +08:00
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
yetone 7f827b3b0b claude: magpie claude-code runs lists the Claude Code processes and says what they are
@Candydidi3 on X: with Opus in Claude Code, "magpie里拉起很多子进程". On a
Claude account each conversation is answered by a real Claude Code
(`claude -p`) with magpie's MCP helper (`magpie claude-mcp-helper`) as its
child. Claude Code's parallel subagents are conversations of their own,
so a session that starts N of them has N pairs running. idleMost (6)
pairs are kept for an hour so the next turn reads the prompt cache, and
runs waiting on tool calls stay up to 30 minutes. On macOS and Windows
nothing bounds the total unless the user sets `magpie claude-code runs N`.
A task manager names them "2.1.311" (Claude Code's binary is named by its
version) and "magpie", and nothing told the user what they were.

No leak was found. With the bridge's fakes, 12 concurrent conversations
left 6 runs when they ended their turns, 12 when they were parked on
tool calls and never answered (bounded by the 30-minute timer), and 0
right after they were cancelled. Real Claude Code 2.1.311, run with the
bridge's args, exits within 0.1s of stdin EOF and when its parent is
killed, and its helper exits with it.

So nothing about which processes run changes. They become visible
instead:
- GET /v1/magpie/claude-runs (claude_runs.go) lists each run with its pid,
  model, account, state (answering, idle for its next turn, or waiting on
  tools), since when, when magpie lets it go, and its RSS, plus
  claudeRoom's live count. It is loopback only, refused like the heap
  profile, and served no-store.
- `magpie claude-code runs` prints that list under the bound and says
  what the processes are and how long they are kept.
- runs record when they started, when the current turn began, and the
  timer's deadline (endIn wraps every timer.Reset). The literal 30-minute
  timeouts become runTimeout.

Tests:
- TestClaudeRunsNamesEachProcess: idle, tools and answering runs are
  listed with the pids that are alive, and dropped once they end; every
  caller profileCallersRefused lists is refused. It went red with the
  route taken out (404), and with the guard off 3 callers got the list.
- TestClaudeCodeRunsListsTheProcesses covers the CLI's lines and the case
  with no magpie running. The existing CLI test now stubs the gateway, so
  it never asks the live magpie on :3425.
- oneport_test knows ClaudeRunsPath.

Real run (10-27): a sandbox HOME with a made-up Claude sign-in in its own
CLAUDE_CONFIG_DIR (no keychain item was written for it), `magpie serve`
on 127.0.0.1:47820, and a `claude` wrapper that exec'd real Claude Code
2.1.311 against a mock Anthropic API. 8 concurrent subagent-like
requests left 6 Claude Code, each with its helper. `magpie claude-code
runs` listed the same 6 pids ps showed, idle, ends in 59m, about 160 MB
each. SIGTERM to the sandbox serve ended all 12 within a second.

Run (go1.26.8, -tags nogui, private GOCACHE):
- At 184b4b8ba (this change on origin/main 5a3292354): gofmt clean; vet
  for darwin, linux and windows; `go test -count=1 -timeout 45m` of
  internal/cli ok (88s) and internal/gateway FAIL (1079s). Of the 118
  tests that failed, 117 failed on "no space left on device" (other
  sessions filled the disk to 0 bytes free mid-run), and
  TestHistoryKeepsDaysAndDropsOld failed because its big writes were cut
  short. All 118 run again with -count=1: ok (153s). The history test
  -count=3: ok.
- At this commit, rebased on origin/main 56de2d192 (Kiro's and Qoder's
  mins, plugin options, Codex app-server restarts and Hermes, none of it in these
  files): gofmt clean; vet for darwin, linux and windows; `go build
  -tags nogui .`; TestClaudeRunsNamesEachProcess, TestClientPathCoversHandler
  and TestClaudeCodeRuns* ok. The GUI build and a full gateway run there
  didn't fit on the disk (about 500 MB free).
Not run: the full ./... suite, Windows or Linux, a real Anthropic account,
and the GUI suite (no GUI change).
2026-10-27 18:22:01 +08:00
yetone 20c0688316 gateway: Claude Code processes are held to what the machine's memory holds; a full room is a 503 the next provider takes (#2956)
Cause: nothing bounded how many Claude Code processes magpie runs for
Claude subscriptions. Busy runs had no limit, idle ones were capped at 6
and parked ones at 16 per bridge, and one-off tests, warm-ups and /usage
reads were counted nowhere. The reporter's 512 MiB systemd service had 2
busy runs and 14 starting at once when the memory cgroup's OOM killer
ended them.

Measured (Docker arm64, real Claude Code 2.1.303):
- one idle run holds about 205 MiB RSS, 86-112 MiB of it anonymous;
- six runs under a 512 MiB memcg gave oom_kill 2 and a peak of 512 MiB.

Fix: claudeRoom (claude_room.go). Every Claude Code takes a place before
it starts and frees it when its process exits. That covers startRun and
runClaudeTree (tests, warm-up, /usage).

How many may run:
- the user's claudeRunsMost (`magpie claude-code runs N|auto`). It is
  machine-local (KeepOwn), and the Settings page's save keeps it.
- otherwise, on Linux, (limit - 256 MiB) / 128 MiB, at least 1. The limit
  is the least of MemTotal, cgroup v2 memory.max and memory.high up the
  tree, and cgroup v1 memory.limit_in_bytes. The reporter's 512 MiB
  cgroup gives 2, and 1 GiB gives 6.
- otherwise no bound: macOS, Windows, or limits that can't be read.

A full room first lets go of the oldest idle run. Its session is
shelved, so the next turn resumes it. Next it lets go of a run parked
past parkLongest. Busy runs are never let go. Otherwise it waits up to
2 minutes, then answers 503 with the reason and the command.

Routing treats that 503 as protected. No Claude account is rested or
asked next, since each would wait for the same room, and another
provider in the group answers.

Tests (each run red by hand on the broken code):
- TestClaudeRunsHeldToTheRoom: 6 conversations at room 2. With take
  forced to always admit: "6 Claude Code processes were alive at once,
  2 may be". Fixed: at most 2, and all 6 answer 200.
- TestClaudeRoomLetsAnIdleRunGoAndItResumes, TestClaudeRoomFullIs503 and
  TestClaudeOneOffsTakeRoom went red with take forced open.
- TestClaudeNoRoomPassesOnAndRestsNobody went red with the routing
  exemption off: both Claude accounts were tried and rested.
- TestClaudeMemRunsFromTheCgroup uses the reporter's literal meminfo,
  cgroup path and memory.max.
- TestClaudeCodeRunsSetsTheUsersOwn went red with validation loosened.
- TestClaudeRunsMostIsMachineLocal went red without the KeepOwn line.
- TestSettingsSaveKeepsWhatItDoesNotSend (internal/gui guard) caught a
  Settings page save clearing the value. It is kept now.

Run at this tree (macOS, go1.26.8):
- go test -timeout 45m -count=1 ./... with pipefail, under a load
  average of 650-800 from other sessions: 61 packages ok. Three were not:
  - internal/agent and internal/gateway hit the 45m timeout mid-test.
    The stacks show no hang. Re-run at -timeout 120m: agent ok (2779s).
  - In gateway, TestClaudeAccountsSpentFallBackToCodex hit its 30s wall
    bound. No room wait was logged before it, and it passed 5 of 5
    alone.
  - In the same run TestClaudeRoomLetsAnIdleRunGoAndItResumes was red
    because of its own check: a random request id in the fake's log
    spelled "a1". It now reads only the fake's "told" lines. Flipped to
    "a2", it goes red.
  - internal/gui: TestPanelJoinsFullScreenSpaces (a macOS window test)
    passes alone. TestSetAnswersWhileDevinAndProvidersHang has a 10s
    bound, and at the base commit under the same load it failed 2 of 3.
- The touched packages again, -count=1: settings, cli and gui ok.
  gateway: the room tests and TestClaudeAccountsSpentFallBackToCodex
  ok at -count=5.
- gofmt -l: clean.
- GOOS=linux and GOOS=windows go vet -tags nogui ./...: clean.

Not tried:
- the Docker 512 MiB reproduction re-run with this build against real
  Claude Code. The fix is shown with fake runs only.
- a real cgroup v1 host.
- the site's docker docs (en/zh/ja) don't mention `claude-code runs` yet.
2026-10-20 20:23:57 +08:00
yetone 23e764d0e0 cli: magpie's commands move from the root into internal/cli; the root keeps only main.go
The root held 89 .go files (30 commands, 59 tests). They move as they are
to internal/cli (package cli), with git mv so history follows them. The
only code change is main() becoming cli.Main(version): the root's new
main.go keeps `var version` in package main, so -ldflags "-X
main.version=…" in the Makefile, the Dockerfile and magpie-releases, and
every workflow, work unchanged. go-winres's rsrc_windows_*.syso still land
next to package main in the root.

Tests that read testdata by a root-relative path now go up one level
(../plugin/testdata/fake, ../sessions/testdata). Five subsystem doc links
point at internal/cli/.

Run:
- go build ./..., gofmt -l, go vet on darwin, linux and windows (nogui)
- builds with tags production, dev, "production nogui"; windows
  -H windowsgui; linux CGO_ENABLED=0
- make release-cli TARGETS="darwin/arm64 linux/amd64 windows/amd64
  android/arm64" VERSION=9.9.9-tidy: all four built, binary prints
  "magpie 9.9.9-tidy"
- docker build --build-arg VERSION=7.7.7-tidy: container prints
  "magpie 7.7.7-tidy"
- go test ./internal/cli (default and nogui): 152 tests, the same count
  as the root on main
- go test ./...: all packages pass (agent, gateway, library, provider
  timed out at 10m under other sessions' load and passed rerun alone
  with -timeout 40m; none of them changed)
- the built binary in a sandbox HOME: version, help, ls, providers,
  claude, and an unknown command's error
2026-10-12 20:40:22 +08:00