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.
197 lines
7.2 KiB
Go
197 lines
7.2 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/claudecode"
|
|
"github.com/yetone/magpie/internal/gateway"
|
|
"github.com/yetone/magpie/internal/proc"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
const claudeCodeUsage = "usage: magpie claude-code which Claude Code the Claude subscription runs\n" +
|
|
" magpie claude-code install [--yes] download Anthropic's own build of Claude Code for magpie (a server or container without one)\n" +
|
|
" magpie claude-code remove delete the Claude Code magpie downloaded\n" +
|
|
" magpie claude-code runs [N|auto] how many Claude Code processes may run at once (auto: what this machine's memory holds)"
|
|
|
|
// claudeCodeCmd: magpie claude-code [install [--yes]|remove]. A Claude
|
|
// subscription is answered by Claude Code, and signed in to through it; a
|
|
// server or a container has none (Jorben on Discord), so magpie downloads
|
|
// Anthropic's build when asked, as Anthropic's installer does.
|
|
func claudeCodeCmd(args []string) error {
|
|
if len(args) == 0 {
|
|
return claudeCodeStatus()
|
|
}
|
|
switch args[0] {
|
|
case "install":
|
|
yes := false
|
|
for _, a := range args[1:] {
|
|
switch a {
|
|
case "--yes", "-y":
|
|
yes = true
|
|
default:
|
|
return fmt.Errorf("magpie claude-code install: unknown %q\n%s", a, claudeCodeUsage)
|
|
}
|
|
}
|
|
return installClaudeCode(yes)
|
|
case "remove", "rm":
|
|
path, v := claudeCode.downloaded()
|
|
if path == "" {
|
|
fmt.Println(muted.Render("magpie hasn't downloaded Claude Code"))
|
|
return nil
|
|
}
|
|
if err := claudecode.Remove(); err != nil {
|
|
return err
|
|
}
|
|
fmt.Println(green.Render("✓"), "removed Claude Code", v, muted.Render("("+claudecode.Root()+")"))
|
|
return nil
|
|
case "runs":
|
|
return claudeCodeRuns(args[1:])
|
|
case "-h", "--help", "help":
|
|
fmt.Println(claudeCodeUsage)
|
|
return nil
|
|
}
|
|
return fmt.Errorf("magpie claude-code: unknown %q\n%s", args[0], claudeCodeUsage)
|
|
}
|
|
|
|
// claudeCodeRuns: magpie claude-code runs [N|auto]. Each Claude
|
|
// subscription conversation, test and /usage read is a Claude Code process
|
|
// of about 100 MiB; a 512 MiB service ran 16 and the kernel killed them
|
|
// (#2956). magpie holds them to what the machine's memory holds unless the
|
|
// user says how many (claudeRunsMost).
|
|
func claudeCodeRuns(args []string) error {
|
|
s := settings.Load()
|
|
switch {
|
|
case len(args) > 1:
|
|
return fmt.Errorf("magpie claude-code runs takes one value\n%s", claudeCodeUsage)
|
|
case len(args) == 1 && (args[0] == "-h" || args[0] == "--help"):
|
|
fmt.Println(claudeCodeUsage)
|
|
return nil
|
|
case len(args) == 1:
|
|
n := 0
|
|
if args[0] != "auto" {
|
|
v, err := strconv.Atoi(args[0])
|
|
if err != nil || v < 1 {
|
|
return fmt.Errorf("magpie claude-code runs takes a number from 1, or auto, not %q", args[0])
|
|
}
|
|
n = v
|
|
}
|
|
s.ClaudeRunsMost = n
|
|
if err := settings.Save(s); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
auto, why := gateway.ClaudeMemRuns()
|
|
autoSaid := "no bound: this machine's memory isn't known to magpie"
|
|
if auto > 0 {
|
|
autoSaid = why
|
|
}
|
|
if s.ClaudeRunsMost > 0 {
|
|
fmt.Println(green.Render("●"), s.ClaudeRunsMost, "Claude Code processes may run at once", muted.Render("· your setting"))
|
|
fmt.Println(muted.Render(" auto would be " + autoSaid + " · magpie claude-code runs auto"))
|
|
return nil
|
|
}
|
|
if auto > 0 {
|
|
fmt.Println(green.Render("●"), auto, "Claude Code processes may run at once", muted.Render("· "+why))
|
|
} else {
|
|
fmt.Println(green.Render("●"), "any number of Claude Code processes may run", muted.Render("· "+autoSaid))
|
|
}
|
|
fmt.Println(muted.Render(" magpie claude-code runs N sets how many"))
|
|
return nil
|
|
}
|
|
|
|
// claudeCode is what the command reads of the machine; tests stand in for it.
|
|
var claudeCode = struct {
|
|
own func() string
|
|
downloaded func() (string, string)
|
|
latest func(context.Context) (claudecode.Release, error)
|
|
install func(context.Context, claudecode.Release, func(int64)) (string, error)
|
|
}{
|
|
own: func() string { return proc.FindTool("claude") },
|
|
downloaded: claudecode.Downloaded,
|
|
latest: claudecode.Latest,
|
|
install: claudecode.Install,
|
|
}
|
|
|
|
func claudeCodeStatus() error {
|
|
own := claudeCode.own()
|
|
path, v := claudeCode.downloaded()
|
|
switch {
|
|
case own != "":
|
|
fmt.Println(green.Render("●"), "Claude Code", muted.Render(own), muted.Render("· this machine's own, which the Claude subscription runs"))
|
|
if path != "" {
|
|
fmt.Println(muted.Render(" magpie's download (" + v + ", " + path + ") isn't used while it is here · magpie claude-code remove deletes it"))
|
|
}
|
|
case path != "":
|
|
fmt.Println(green.Render("●"), "Claude Code", v, muted.Render(path), muted.Render("· downloaded by magpie"))
|
|
fmt.Println(muted.Render(" magpie claude-code install gets the newest · magpie claude-code remove deletes it"))
|
|
default:
|
|
fmt.Println(amber.Render("○"), "no Claude Code here: a Claude subscription can't be signed in to or answered")
|
|
fmt.Println(muted.Render(" install it (https://code.claude.com/docs), or magpie claude-code install downloads Anthropic's own build for magpie"))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// installClaudeCode downloads the newest Claude Code after saying what,
|
|
// from where and to where, and asking (unless yes).
|
|
func installClaudeCode(yes bool) error {
|
|
ctx, stop := interruptContext()
|
|
defer stop()
|
|
lctx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
|
r, err := claudeCode.latest(lctx)
|
|
cancel()
|
|
if err != nil {
|
|
return fmt.Errorf("asking for Claude Code's newest release: %w", err)
|
|
}
|
|
if path, v := claudeCode.downloaded(); v == r.Version {
|
|
fmt.Println(green.Render("✓"), "Claude Code", v, "is downloaded already", muted.Render("("+path+")"))
|
|
return nil
|
|
}
|
|
fmt.Println("Claude Code", bold.Render(r.Version), muted.Render("("+r.Platform+", "+mb(r.Size)+")"))
|
|
fmt.Println(muted.Render(" from " + r.URL))
|
|
fmt.Println(muted.Render(" to " + claudecode.Root()))
|
|
fmt.Println(muted.Render(" Anthropic's own build, as its installer downloads it; its SHA-256 is checked against Anthropic's manifest"))
|
|
fmt.Println(muted.Render(" it is Anthropic's software, under Anthropic's terms; magpie runs it for the Claude subscription only"))
|
|
if own := claudeCode.own(); own != "" {
|
|
fmt.Println(amber.Render("!"), "this machine has a Claude Code of its own,", own+": it is the one run while it is there")
|
|
}
|
|
if !yes {
|
|
fmt.Print("Download it? [y/N] ")
|
|
var answer string
|
|
fmt.Scanln(&answer)
|
|
if a := strings.ToLower(strings.TrimSpace(answer)); a != "y" && a != "yes" {
|
|
return fmt.Errorf("download canceled")
|
|
}
|
|
}
|
|
last := time.Time{}
|
|
tty := isTerminal(os.Stdout)
|
|
path, err := claudeCode.install(ctx, r, func(done int64) {
|
|
if !tty || time.Since(last) < 200*time.Millisecond && done < r.Size {
|
|
return
|
|
}
|
|
last = time.Now()
|
|
fmt.Printf("\r %s of %s", mb(done), mb(r.Size))
|
|
})
|
|
if tty {
|
|
fmt.Println()
|
|
}
|
|
if err != nil {
|
|
return fmt.Errorf("downloading Claude Code %s: %w", r.Version, err)
|
|
}
|
|
fmt.Println(green.Render("✓"), "Claude Code", r.Version, "downloaded and checked", muted.Render("("+path+")"))
|
|
fmt.Println(muted.Render(" add a Claude account: magpie accounts add claude"))
|
|
return nil
|
|
}
|
|
|
|
func mb(n int64) string { return fmt.Sprintf("%.0f MB", float64(n)/(1<<20)) }
|
|
|
|
func isTerminal(f *os.File) bool {
|
|
st, err := f.Stat()
|
|
return err == nil && st.Mode()&os.ModeCharDevice != 0
|
|
}
|