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).
121 lines
4.0 KiB
Go
121 lines
4.0 KiB
Go
package gateway
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/proc"
|
|
)
|
|
|
|
// A Claude Code run is kept for its conversation's next turn, turn after
|
|
// turn, for days on a service that is never restarted, and nothing bounded
|
|
// what one process grew to: claudeRoom bounds how many run, not how large
|
|
// each is. On a 31 GiB host one reached 8 GiB resident after four days of
|
|
// a large conversation, and the kernel's OOM killer took the machine's
|
|
// other work with it (#3256). Once a turn is over, a run holding more than
|
|
// claudeRunBudget is let go instead of kept: its session is shelved, and
|
|
// the next turn resumes it in a new Claude Code, which asks Anthropic the
|
|
// same cached prefix.
|
|
//
|
|
// The budget is an eighth of what magpie's processes may use (memLimit on
|
|
// Linux, the computer's memory elsewhere), claudeRunFloor at least; where
|
|
// neither is known, claudeRunUnknown. A Claude Code holds ~100 MiB of its
|
|
// own at the start (claudeRoom).
|
|
const (
|
|
claudeRunFloor = 1 << 30
|
|
claudeRunUnknown = 4 << 30
|
|
)
|
|
|
|
// claudeRunRSS reads a run's resident memory, for tests to give one.
|
|
var claudeRunRSS = func(t *proc.Tree) (int64, bool) { return t.RSS() }
|
|
|
|
var runBudget struct {
|
|
sync.Once
|
|
n int64
|
|
why string
|
|
}
|
|
|
|
// claudeRunBudget is the most a Claude Code run may hold resident and
|
|
// still be kept for its next turn, and what sized it.
|
|
func claudeRunBudget() (int64, string) {
|
|
runBudget.Do(func() {
|
|
limit, why := memLimit(memGOOS, memRoot)
|
|
if limit <= 0 && memOwn() {
|
|
limit, why = proc.MemTotal(), "the computer's memory"
|
|
}
|
|
if limit <= 0 {
|
|
runBudget.n, runBudget.why = claudeRunUnknown, "memory not known"
|
|
return
|
|
}
|
|
runBudget.n = max(limit/8, claudeRunFloor)
|
|
runBudget.why = fmt.Sprintf("an eighth of %d MiB (%s), %d MiB at least", limit>>20, why, claudeRunFloor>>20)
|
|
})
|
|
return runBudget.n, runBudget.why
|
|
}
|
|
|
|
// oversized says the run holds more than claudeRunBudget times share/2
|
|
// (2 a whole turn, 3 a run that waits on tool calls), and logs what it
|
|
// holds either way: a run's growth across turns is in the log, for an OOM
|
|
// to be put down to the process it was. One waiting on its caller's tool
|
|
// results has no session to shelve there: let go, it is started anew with
|
|
// the whole conversation, which Anthropic's cache has not seen, so it is
|
|
// let go only past half as much again.
|
|
func (r *subscriptionRun) oversized(share int64) (string, bool) {
|
|
r.mu.Lock()
|
|
t := r.tree
|
|
r.mu.Unlock()
|
|
if t == nil {
|
|
return "", false
|
|
}
|
|
rss, ok := claudeRunRSS(t)
|
|
if !ok {
|
|
return "", false
|
|
}
|
|
budget, why := claudeRunBudget()
|
|
budget = budget * share / 2
|
|
log.Printf("claude: Claude Code pid %d (%s) holds %d MiB after its turn; %d MiB may be kept (%s)", r.pid(), r.model, rss>>20, budget>>20, why)
|
|
if rss <= budget {
|
|
return "", false
|
|
}
|
|
return fmt.Sprintf("its Claude Code held %d MiB, over the %d MiB a run may hold to be kept (%s)", rss>>20, budget>>20, why), true
|
|
}
|
|
|
|
// pid is the run's Claude Code process id, 0 before it starts. It is
|
|
// read once tree is set under mu: cmd.Process is written by cmd.Start,
|
|
// which a list of the runs may be reading through.
|
|
func (r *subscriptionRun) pid() int {
|
|
r.mu.Lock()
|
|
t := r.tree
|
|
r.mu.Unlock()
|
|
if t == nil || r.cmd == nil || r.cmd.Process == nil {
|
|
return 0
|
|
}
|
|
return r.cmd.Process.Pid
|
|
}
|
|
|
|
// letGoOversized lets go of a run whose turn was whole and that holds too
|
|
// much to be kept (oversized): its session is shelved at key, as one let go
|
|
// past idleMost is, so the next turn resumes it in a new Claude Code.
|
|
func (b *subscriptionBridge) letGoOversized(r *subscriptionRun, key, conv, why string) {
|
|
b.mu.Lock()
|
|
r.mu.Lock()
|
|
closed := r.closed
|
|
r.mu.Unlock()
|
|
if !closed {
|
|
if r.idleKey != "" && b.idle[r.idleKey] == r {
|
|
delete(b.idle, r.idleKey)
|
|
}
|
|
r.idleKey, r.idleAt = key, time.Now()
|
|
if conv != "" {
|
|
r.convKey = conv
|
|
}
|
|
b.shelve(r)
|
|
r.idleKey = ""
|
|
}
|
|
b.mu.Unlock()
|
|
log.Printf("claude: Claude Code pid %d (%s) is let go: %s; its conversation's next turn resumes its session in a new one", r.pid(), r.model, why)
|
|
r.abortFor(why)
|
|
}
|