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