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).
TestClaudeRunsNamesEachProcess was red under -race on ubuntu CI at
6104e88aa (run 40698575099): /v1/magpie/claude-runs (7f827b3b0) listed a
run whose launch was still in cmd.Start, and pid() read cmd.Process
while Start wrote it. A real race, not only the test's: the Claude runs
list can be asked for at any time. pid() now reads tree under mu first;
tree is set under mu after Start returns, so cmd.Process is read after
it is written. No pid() caller holds r.mu (claude_rss.go oversized and
letGoOversized, claude_runs.go, the park path, launch's own logs).
Run on mote (M5 Pro):
- old code, -race -count=30: no race (the window is narrow)
- old code with a 50ms sleep injected at the top of launch, -race
-count=5: DATA RACE, FAIL
- this change with the same sleep, -count=5: ok
- without the sleep: go test -race -run Claude ./internal/gateway ok
(84s); go test -run Claude -count=2 ok (79s)
- go vet ./internal/gateway for darwin, linux, windows: ok
Not run: the full suite (one function changed, every Claude test above).
claudeRoom bounded how many Claude Code processes run, not how large
each one is. A run is kept for its conversation's next turn, turn after
turn, and on a service never restarted one grew for days: a
node-MainThread in magpie.service reached 8 GiB RSS on a 31 GiB host and
the OOM killer took the machine down (#3256).
Now each time a run's reply ends, its process tree's resident memory is
read (proc.Tree.RSS: /proc VmRSS on Linux, ps on macOS, the working sets
of its job's processes on Windows, so an npm claude.cmd's node counts)
and logged with its pid. Over the budget (an eighth of memLimit on
Linux, of the computer's memory elsewhere, 1 GiB at least, 4 GiB if
unknown), a whole turn's run is not kept idle: its session is shelved
and the next turn resumes it in a new Claude Code with --resume, told
only the new messages. A run parked on tool calls is let go past 1.5x
the budget (no session to shelve there; its results start a new run).
Each Claude Code also logs pid and model at start, and run time, how it
ended and peak RSS (ru_maxrss) at its end, so an OOM pid can be found in
magpie's log. Budget on the reporter's 31 GiB host: 3.9 GiB.
Run (worktree at 99de8e1ad + this change, macOS arm64):
- go test -timeout 45m ./internal/gateway -run
'Claude|Subscription|Room|Park|Idle|Resume' -count=1: ok (60.5s).
- go test ./internal/proc -count=1: every test ok except
TestNoCommandBypassesProc, red on origin/main since 75e78a5a0
(internal/tui/keep.go, filed as #3567, not this change).
- New tests: TestClaudeOversizedRunIsLetGoAndResumes (injected RSS over
budget: the run ends, the next turn runs --resume s<pid> and is not
told a1 again), TestClaudeRunWithinBudgetIsKept,
TestClaudeOversizedParkedRunIsLetGo, TestClaudeRunBudgetScalesWithMemory
(31 GiB MemTotal -> an eighth; 2 GiB -> the 1 GiB floor), TestTreeRSS
(a real sleep's RSS read; none once waited for).
- Broken by hand: with both checks turned off, the oversized idle and
parked tests go red ("the oversized run to end", "a run over its
budget was kept waiting on its tool calls").
- Real Claude Code 2.1.310 in a sandbox HOME and CLAUDE_CONFIG_DIR,
stream-json like the bridge, against DeepSeek's Anthropic endpoint
(deepseek-chat, real key, synthetic 40KB turns): RSS after turns
1/10/20/30 = 192/244/223/233 MiB, peak 245 MiB over 30 turns and
~1.2 MB of conversation. oversized() on that real process with the
budget 1 MiB under its RSS said over; with the real budget on this
64 GiB Mac (8192 MiB) it said keep.
- gofmt -l clean; go vet, GOOS=linux and GOOS=windows go vet -tags
nogui ./internal/proc ./internal/gateway clean.
Not tried: a real Claude subscription run through the bridge on Linux
until it crosses the budget (the retire path is shown with injected RSS
on the fake Claude Code, and the RSS read on a real one); the Windows
RSS reader and job listing on the Windows box (vet only); a Linux run of
the /proc reader (macOS reads through ps); the full ./internal/gateway
package.