Files
magpie/internal/gateway/claude_room.go
T
yinwu01 fd85c0a1e9 gateway: a test's memory root reads nothing of the machine it runs on (#5118)
TestCodexBodyLimitFromMemory's "unknown" case is red on every Linux
machine with more than 8 GiB: ubuntu CI ("unknown: 998 MiB" in the Test
job at 0a5dc40ab), the Linux box under WSL ("unknown: 1994 MiB") and
this one ("unknown: 2048 MiB"). The case points memLimit at an empty
root with memGOOS "linux". codexBodyLimit, bodyRoomSize and
claudeRunBudget fall back to proc.MemTotal() whenever memGOOS is this
system's, which held for a test's root too, so on Linux the fixture
read the test machine's own memory. Elsewhere the GOOS differs: macOS
CI at 0a5dc40ab doesn't fail this test (its job is red on others).

The three now fall back only when memOwn: memGOOS is this system's and
memRoot is "/", the only root magpie itself sets. The magpie binary
behaves as before; a test's root no longer reaches the machine's
memory.

What was run (tree 039a3044, on origin/main 43849d51, go1.26.8,
linux/amd64, Linux 5.15), outside graftcp, umask 077:
- TestAFixtureRootIsNotThisComputer (new): an empty root naming this
  computer's GOOS leaves bodyRoomSize, claudeRunBudget and
  codexBodyLimit at their unknown values. With memOwn put back to the
  GOOS check alone: red on all three ("body room 16111 MiB (a
  thirty-second of 515578 MiB (the computer's memory)), want none",
  "run budget 64447 MiB (...), want the unknown 4096 MiB", "Codex turn
  limit 2048 MiB, want 512 MiB"), and TestCodexBodyLimitFromMemory
  "unknown: 2048 MiB". With claude_rss.go's fallback alone put back as
  it was, only the run budget. With the change, with
  TestCodexBodyLimitFromMemory, TestBodyRoomSize,
  TestClaudeRunBudgetScalesWithMemory, TestClaudeMemRuns* and
  TestClaudeRunsMost*: -count=3 21 of 21 ok, and -race -count=3 21 of
  21 ok.
- gofmt -l clean. go vet -tags nogui for linux and windows, go vet for
  windows, go build -tags nogui for linux and darwin and go build for
  windows: exit 0 each. GOOS=darwin go vet -tags nogui: exit 1 at
  internal/gui/traywindows_darwin_test.go (trayImagePNG is darwin &&
  cgo, and a cross vet has no cgo), the same at origin/main 43849d51.
  go vet and go build without the tag on linux: exit 1 (no libsoup-3.0
  or gio-unix-2.0 pkg-config here).
- go test -tags nogui -count=1 -timeout 45m ./... (Bun 1.4.2): 84
  packages ok, gateway among them; red in three, each red the same run
  alone at origin/main 43849d51: sessions' TestCCListingKeepsItsNames
  (#4925), agent's TestDshACPSettingsGetTheRoute (this box's umask 077,
  #5238), provider's TestOpenCodeProjectIs225s and
  TestOpenCodeEnvironmentIs225s (an empty /tmp/.git on this box,
  #5239).

Not tried: a Mac or Windows. There the new test should be red on the
old code too, as the fallback took that machine's memory the same way;
it was run only here.

Reviewed and landed by yetone at origin/main d17a0b3ca (patch hash
639baff3... checked against the one posted on #5118). CI went red on
this at 4a9cfb52e (run 41865226839, ubuntu). Run at this tree:
- Linux (wsl-run, magpie-desktop, go1.26.8, -tags nogui): with memOwn
  put back to `memGOOS == runtime.GOOS` alone, TestCodexBodyLimitFromMemory
  "unknown: 1994 MiB" and TestAFixtureRootIsNotThisComputer red on all
  three readers (body room 997 MiB, run budget 3989 MiB, Codex turn
  limit 1994 MiB). With the change, the memory tests (TestCodexBodyLimit-
  FromMemory, TestAFixtureRootIsNotThisComputer, TestBodyRoomSize,
  TestClaudeRunBudgetScalesWithMemory, TestClaudeMemRuns*,
  TestClaudeRunsMost*): ok.
- macOS (mote): with memOwn put back, TestAFixtureRootIsNotThisComputer
  red on all three (2048/8192/2048 MiB from mote's 64 GiB);
  TestCodexBodyLimitFromMemory passes there either way (memGOOS
  "linux" differs). With the change, the same set -race: ok.
- gofmt -l, go vet, GOOS=linux go vet -tags nogui, GOOS=windows go vet
  (internal/gateway), go build, GOOS=linux go build -tags nogui,
  GOOS=windows go build: clean on mote.
Not tried: Windows; the full suite (run once after this batch).
2026-11-05 00:57:55 +08:00

365 lines
11 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package gateway
import (
"bytes"
"context"
"fmt"
"log"
"os"
"path"
"path/filepath"
"runtime"
"slices"
"strconv"
"strings"
"sync"
"time"
"github.com/yetone/magpie/internal/settings"
)
// claudeRoom counts the Claude Code processes magpie has running for
// Claude subscriptions: each conversation's run, busy, waiting for its next
// turn or on its caller's tool results, and each one-off test, warm-up and
// /usage read. Each holds about 100 MiB of its own (Claude Code 2.1.303 on
// Linux arm64: 86–112 MiB anonymous, the 285 MB binary's pages shared), and
// nothing bounded how many ran: a 512 MiB service had 2 busy runs and 14
// starting at once, and the kernel killed them (#2956). A new one waits for
// room (take): an idle run is let go for it first, its session shelved for
// its next turn, then one waiting parkLongest on tool results; with neither,
// it waits for one to end, claudeRoomWait at most, and is answered 503,
// which routing passes to the next account or provider.
type claudeRoomT struct {
mu sync.Mutex
// live is how many Claude Code processes run; leaving how many of them
// magpie has asked to end, not ended yet; waiting how many asks wait
// for room
live, leaving, waiting int
// freed is closed, and made anew, each time one ends, or a run comes
// to wait for its next turn (nudge), which may be let go for room
freed chan struct{}
}
var claudeRoom = &claudeRoomT{freed: make(chan struct{})}
// claudeRoomWait is how long a Claude Code waits for room before its
// request is answered 503.
var claudeRoomWait = 2 * time.Minute
// claudeFull is the error of an ask that found no room in claudeRoomWait.
type claudeFull struct {
live, most int
why string
waited time.Duration
}
// claudeNoRoomSaid is in every claudeFull's words, as routing finds it
// (claudeNoRoom).
const claudeNoRoomSaid = "Claude Code processes are running, as many as may run at once"
// claudeNoRoom says whether a failed reply is the bridge's 503 for a full
// claudeRoom.
func claudeNoRoom(status int, body []byte) bool {
return status == 503 && bytes.Contains(body, []byte(claudeNoRoomSaid))
}
func (e *claudeFull) Error() string {
return fmt.Sprintf("%d "+claudeNoRoomSaid+" (%s); none ended within %s. "+
"Set how many may run with `magpie claude-code runs N` (claudeRunsMost in settings.json)",
e.live, e.why, e.waited)
}
// claudeRunsMost is how many Claude Code processes may run at once, and
// what says so: the user's claudeRunsMost, else what this machine's memory
// holds (claudeMemRuns), else 0, no bound.
func claudeRunsMost() (int, string) {
if n := settings.Load().ClaudeRunsMost; n > 0 {
return n, "claudeRunsMost in settings"
}
return claudeMemRuns()
}
// ClaudeMemRuns is how many Claude Code processes this machine's memory
// holds, and why; 0 when not bounded by it (`magpie claude-code runs`).
func ClaudeMemRuns() (int, string) { return claudeMemRuns() }
// The memory a Claude Code process is given, and what is kept for magpie
// and the rest of the machine, as the bound is sized from memory: 512 MiB
// holds 2, 1 GiB 6.
const (
claudeRunMiB = 128
claudeReserveMiB = 256
)
var memRuns struct {
sync.Mutex
known bool
n int
why string
}
// claudeMemRuns is how many Claude Code processes this machine's memory
// holds: (limit - claudeReserveMiB) / claudeRunMiB, 1 at least, the
// limit the least of the cgroup's memory.max and memory.high (v1: its
// memory.limit_in_bytes) and MemTotal. A machine whose memory can't be
// read, or that isn't Linux, has no bound from it (0): a limit not known
// is not a small one. It is read once.
func claudeMemRuns() (int, string) {
memRuns.Lock()
defer memRuns.Unlock()
if !memRuns.known {
memRuns.known = true
if limit, why := memLimit(memGOOS, memRoot); limit > 0 {
n := runsFor(limit)
memRuns.n = n
memRuns.why = fmt.Sprintf("%d MiB of memory (%s) holds %d", limit>>20, why, n)
}
}
return memRuns.n, memRuns.why
}
// runsFor is how many Claude Code processes limit bytes of memory hold.
func runsFor(limit int64) int {
return max(int((limit>>20-claudeReserveMiB)/claudeRunMiB), 1)
}
// memGOOS and memRoot are the system memLimit reads, and the root its
// files are under, for tests.
var (
memGOOS = runtime.GOOS
memRoot = "/"
)
// memOwn says memLimit reads this computer, so where it reads nothing
// the computer's memory may stand in. A test's root is not this computer
// even when it names this computer's system: such a root, holding
// nothing, read the test machine's own memory as its own (#5118, on Linux
// CI and WSL).
func memOwn() bool { return memGOOS == runtime.GOOS && memRoot == "/" }
// memLimit is the memory magpie's processes may use, in bytes, and where
// that is said; 0 when not known.
func memLimit(goos, root string) (int64, string) {
if goos != "linux" {
return 0, ""
}
var limit int64
var why string
least := func(n int64, from string) {
if n > 0 && (limit == 0 || n < limit) {
limit, why = n, from
}
}
if b, err := os.ReadFile(filepath.Join(root, "proc/meminfo")); err == nil {
for _, line := range strings.Split(string(b), "\n") {
if f := strings.Fields(line); len(f) >= 2 && f[0] == "MemTotal:" {
if kb, err := strconv.ParseInt(f[1], 10, 64); err == nil {
least(kb<<10, "MemTotal")
}
}
}
}
b, err := os.ReadFile(filepath.Join(root, "proc/self/cgroup"))
if err != nil {
return limit, why
}
cgroups := filepath.Join(root, "sys/fs/cgroup")
for _, line := range strings.Split(strings.TrimSpace(string(b)), "\n") {
parts := strings.SplitN(line, ":", 3)
if len(parts) != 3 {
continue
}
switch {
case parts[0] == "0" && parts[1] == "":
// v2: this cgroup's limits and every one above it. The
// cgroup path is a "/" path whatever the host reads it on,
// so the walk is path's: filepath's root is "\" on Windows,
// where dir never read "/" and the loop never ended (#4113).
for dir := path.Clean("/" + parts[2]); ; dir = path.Dir(dir) {
for _, name := range []string{"memory.max", "memory.high"} {
if n, ok := readLimit(filepath.Join(cgroups, dir, name)); ok {
least(n, "cgroup "+path.Join(dir, name))
}
}
if dir == "/" {
break
}
}
case slices.Contains(strings.Split(parts[1], ","), "memory"):
// v1: the memory controller's group, or the container's root
// where the host's path isn't mounted
for _, dir := range []string{filepath.Join(cgroups, "memory", parts[2]), filepath.Join(cgroups, "memory")} {
if n, ok := readLimit(filepath.Join(dir, "memory.limit_in_bytes")); ok {
least(n, "cgroup memory.limit_in_bytes")
break
}
}
}
}
return limit, why
}
// readLimit is a cgroup memory limit file's bytes; "max", a file not
// there or not read, is none.
func readLimit(path string) (int64, bool) {
b, err := os.ReadFile(path)
if err != nil {
return 0, false
}
n, err := strconv.ParseInt(strings.TrimSpace(string(b)), 10, 64)
if err != nil || n <= 0 {
return 0, false
}
return n, true
}
// take waits for room for one Claude Code process, what naming it in the
// log, and gives the func that frees it once the process has ended; left
// says magpie ended it (leave). An ask that finds the room full lets go of
// an idle or long-parked run for it, unless enough are ending already, and
// waits for one to end: claudeRoomWait at most, or as long as ctx lasts.
func (c *claudeRoomT) take(ctx context.Context, what string) (func(left bool), error) {
var deadline <-chan time.Time
var timer *time.Timer
began := time.Now()
waiting := false
defer func() {
if timer != nil {
timer.Stop()
}
if waiting {
c.mu.Lock()
c.waiting--
c.mu.Unlock()
}
}()
for {
most, why := claudeRunsMost()
c.mu.Lock()
if most <= 0 || c.live < most {
c.live++
c.mu.Unlock()
if waiting {
log.Printf("claude: %s had room after %s", what, time.Since(began).Round(time.Millisecond))
}
return c.release, nil
}
if !waiting {
waiting = true
c.waiting++
log.Printf("claude: %s waits for room: %d Claude Code processes run, %d may (%s)", what, c.live, most, why)
}
freed, live := c.freed, c.live
evict := c.live-c.leaving >= most && c.leaving < c.waiting
c.mu.Unlock()
if evict && letGoForRoom(most) {
continue // what it let go ends soon: waited for below
}
if timer == nil {
timer = time.NewTimer(claudeRoomWait)
deadline = timer.C
}
select {
case <-freed:
case <-time.After(roomLook):
// a run parked since before comes to have waited parkLongest
case <-deadline:
log.Printf("claude: %s found no room in %s", what, claudeRoomWait)
return nil, &claudeFull{live: live, most: most, why: why, waited: claudeRoomWait}
case <-ctx.Done():
return nil, ctx.Err()
}
}
}
// leave counts a process magpie has asked to end as ending, so asks
// waiting for room don't let go of another for it.
func (c *claudeRoomT) leave() {
c.mu.Lock()
c.leaving++
c.mu.Unlock()
}
// roomLook is how often an ask waiting for room looks again for a run to
// let go, unasked.
var roomLook = 5 * time.Second
// nudge has the asks waiting for room look again: a run has come to wait
// for its next turn.
func (c *claudeRoomT) nudge() {
c.mu.Lock()
if c.waiting > 0 {
close(c.freed)
c.freed = make(chan struct{})
}
c.mu.Unlock()
}
// release frees a process's room once it has ended.
func (c *claudeRoomT) release(left bool) {
c.mu.Lock()
c.live--
if left {
c.leaving--
}
close(c.freed)
c.freed = make(chan struct{})
c.mu.Unlock()
}
// letGoForRoom lets go of one run for room, of any bridge: the one idle
// longest, its session shelved for its conversation's next turn as when
// more than idleMost wait, else the one waiting longest on tool results
// past parkLongest. It says whether it found one. Busy runs, and those
// waiting on tool calls made recently, are kept.
func letGoForRoom(most int) bool {
var idle, parked *subscriptionRun
var parkedAt time.Time
var idleBridge *subscriptionBridge
var idleKey string
now := time.Now()
bridges.Range(func(k, _ any) bool {
b := k.(*subscriptionBridge)
b.mu.Lock()
for key, run := range b.idle {
if idle == nil || run.idleAt.Before(idle.idleAt) {
idle, idleBridge, idleKey = run, b, key
}
}
for _, run := range b.runs {
run.mu.Lock()
at, closed, killed := run.parkedAt, run.closed, run.killed
run.mu.Unlock()
if !closed && !killed && !at.IsZero() && now.Sub(at) >= parkLongest && (parked == nil || at.Before(parkedAt)) {
parked, parkedAt = run, at
}
}
b.mu.Unlock()
return true
})
why := "magpie let it go to make room for another Claude Code (" + strconv.Itoa(most) + " may run at once)"
if idle != nil {
b := idleBridge
b.mu.Lock()
if b.idle[idleKey] != idle {
b.mu.Unlock()
return true // taken meanwhile: the room is looked at again
}
b.shelve(idle)
delete(b.idle, idleKey)
if idle.idleKey == idleKey {
idle.idleKey = ""
}
b.mu.Unlock()
log.Printf("claude: an idle run on %s is let go to make room (%d may run at once); its conversation's next turn resumes its session", idle.model, most)
idle.abortFor(why)
return true
}
if parked != nil {
log.Printf("claude: a run on %s waiting on tool results since %s is let go to make room (%d may run at once)", parked.model, parkedAt.Format(time.TimeOnly), most)
parked.abortFor(why)
return true
}
return false
}