From fd85c0a1e9c363df173fe9e947b4136ffe5b0600 Mon Sep 17 00:00:00 2001 From: yinwu01 Date: Wed, 4 Nov 2026 03:59:45 -0800 Subject: [PATCH] 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). --- internal/gateway/body_room.go | 3 +-- internal/gateway/claude_room.go | 7 +++++++ internal/gateway/claude_room_test.go | 23 +++++++++++++++++++++++ internal/gateway/claude_rss.go | 3 +-- internal/gateway/request_bounds.go | 3 +-- 5 files changed, 33 insertions(+), 6 deletions(-) diff --git a/internal/gateway/body_room.go b/internal/gateway/body_room.go index ee2eb35a8..6c98b78c2 100644 --- a/internal/gateway/body_room.go +++ b/internal/gateway/body_room.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "log" - "runtime" "slices" "sync" "time" @@ -65,7 +64,7 @@ const bodyRoomFloor = 64 << 20 // is not a small one. func bodyRoomSize() (int64, string) { limit, why := memLimit(memGOOS, memRoot) - if limit <= 0 && memGOOS == runtime.GOOS { + if limit <= 0 && memOwn() { limit, why = proc.MemTotal(), "the computer's memory" } if limit <= 0 { diff --git a/internal/gateway/claude_room.go b/internal/gateway/claude_room.go index 234a5a0c8..ec68dcbc6 100644 --- a/internal/gateway/claude_room.go +++ b/internal/gateway/claude_room.go @@ -130,6 +130,13 @@ var ( 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) { diff --git a/internal/gateway/claude_room_test.go b/internal/gateway/claude_room_test.go index 958ad43c8..eca4a8754 100644 --- a/internal/gateway/claude_room_test.go +++ b/internal/gateway/claude_room_test.go @@ -10,6 +10,7 @@ import ( "os" "os/exec" "path/filepath" + "runtime" "strconv" "strings" "sync" @@ -343,6 +344,28 @@ func TestClaudeMemRunsUnknownBoundsNothing(t *testing.T) { } } +// A test's root is not this computer, even one naming this computer's +// system: holding nothing, it leaves memory unknown to every reader that +// falls back on the computer's memory. The computer's memory stood in for +// such a root, so each read the test machine's (#5118: a Codex turn's +// limit on an "unknown" machine was 998 MiB on ubuntu CI, 1994 MiB on WSL). +func TestAFixtureRootIsNotThisComputer(t *testing.T) { + oldGOOS, oldRoot := memGOOS, memRoot + memGOOS, memRoot = runtime.GOOS, t.TempDir() + t.Cleanup(func() { memGOOS, memRoot = oldGOOS, oldRoot }) + if n, why := bodyRoomSize(); n != 0 { + t.Errorf("body room %d MiB (%s), want none", n>>20, why) + } + runBudget.Once = sync.Once{} + t.Cleanup(func() { runBudget.Once = sync.Once{} }) + if n, why := claudeRunBudget(); n != claudeRunUnknown { + t.Errorf("run budget %d MiB (%s), want the unknown %d MiB", n>>20, why, claudeRunUnknown>>20) + } + if n := codexBodyLimit(); n != defaultBodyLimit { + t.Errorf("Codex turn limit %d MiB, want %d MiB", n>>20, defaultBodyLimit>>20) + } +} + // The user's claudeRunsMost wins over what the memory holds. func TestClaudeRunsMostTheUsersOwn(t *testing.T) { testenv.SetHome(t, t.TempDir()) diff --git a/internal/gateway/claude_rss.go b/internal/gateway/claude_rss.go index c8f6e0944..cb7fbd1e7 100644 --- a/internal/gateway/claude_rss.go +++ b/internal/gateway/claude_rss.go @@ -3,7 +3,6 @@ package gateway import ( "fmt" "log" - "runtime" "sync" "time" @@ -43,7 +42,7 @@ var runBudget struct { func claudeRunBudget() (int64, string) { runBudget.Do(func() { limit, why := memLimit(memGOOS, memRoot) - if limit <= 0 && memGOOS == runtime.GOOS { + if limit <= 0 && memOwn() { limit, why = proc.MemTotal(), "the computer's memory" } if limit <= 0 { diff --git a/internal/gateway/request_bounds.go b/internal/gateway/request_bounds.go index 34ee42448..740908ffc 100644 --- a/internal/gateway/request_bounds.go +++ b/internal/gateway/request_bounds.go @@ -7,7 +7,6 @@ import ( "io" "net" "net/http" - "runtime" "strings" "sync" "time" @@ -53,7 +52,7 @@ func codexBodyLimit() int64 { defer codexLimit.Unlock() if codexLimit.goos != memGOOS || codexLimit.root != memRoot || codexLimit.n == 0 { limit, _ := memLimit(memGOOS, memRoot) - if limit <= 0 && memGOOS == runtime.GOOS { + if limit <= 0 && memOwn() { limit = proc.MemTotal() } codexLimit.goos, codexLimit.root = memGOOS, memRoot