Under WSL2 (magpie-desktop, kernel 5.15) a sleep's /proc/<pid>/status
says "VmRSS: 0 kB" on the read right after its exec, before the loader
maps anything. parseKiB counts 0 as no reading, and the test's wait loop
stopped on the first !ok, so it failed at 0.00s every run there. A read
a few ms later says 1792 kB. The loop now waits while there is no
reading too, within the same 10s.
Product code is unchanged: the one caller (claude_rss.go's oversized)
reads a Claude Code after a whole turn, and a 0 there skips the check.
What was run (this tree):
- wsl-run go test -count=20 -run TestTreeRSS ./internal/proc/: ok
(red 3 of 3 at origin/main before).
- go test ./internal/proc/: ok on mote (6.1s) and on WSL (2.7s).
Windows: TestTreeRSS passes; TestCombinedOutputOwnRuns and
TestCombinedOutputOwnContext are red there at origin/main too
(/bin/sh), fixed in the next commit.
Refs #4033
Test (ubuntu, macos) on main has been red since 75e78a5a0 on two tests.
TestNoCommandBypassesProc: internal/tui/keep.go's serveInBackground
started `magpie serve` with os/exec. It is proc.Command now, the fix
skygunner gave on #3567 (64bd29fa on their fork). Off Windows
proc.Command is exec.Command, and this path runs on Linux only, so
nothing else changes.
TestTreeRSS (ubuntu only): the test read sleep's VmRSS right after
StartTree and wanted at least 64 KiB. Just after its exec a command
holds a few pages until its loader maps libc: CI read 16384 bytes. The
test now reads again until it holds 64 KiB, for up to 10s. Tree.RSS
itself is unchanged.
Run at this head:
- golang:1.26 docker (Linux, CI's Go): TestTreeRSS -count=50,
old test red 13 of 50 ("a running sleep holds ..."), new 50 of 50 ok.
- macOS: go test -count=3 ./internal/proc ./internal/tui ok (the old
keep.go is the guard's red on CI's runs 40199986992 and earlier).
- GOOS=linux and GOOS=windows go vet -tags nogui on both packages,
gofmt clean.
Not run: the full suite.
Co-authored-by: skygunner <skygunner@users.noreply.github.com>
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.