magpie icon indicating copy to clipboard operation
magpie copied to clipboard

internal/agent: six "kept" allocation tests red on Windows; each StampNow hashes os.Environ(), which allocates a string per variable there

Open laweit-lxy opened this issue 1 month ago • 0 comments

Not in #4115, #1827, #1388, #2808 or #4633 (searched by test name). Likely the same cause as #4633 (TestResolveKeptAcrossRequests) and #4115's TestCodexListTagKeptWhileNothingChanged.

Run on Windows 11 ARM64, Go 1.26.8, at v0.1.2519 (8ac1c7a5), alone with -run and in the package run. StampNow/environSum are unchanged on main 8f804396fd, and all six tests are still there.

test asserts with the box's env (32 vars) +38 dummy vars 9 fewer system vars
TestClaudeViaMagpieKept kept ≤ 20, kept×5 ≤ built FAIL 67 / 461 FAIL 106 / 539 FAIL 59 / 445
TestMagpieModelsKeptWhileCatalogStays kept ≤ 200, kept×10 ≤ built PASS FAIL 106 / 871 PASS
TestViaMagpieKeptWhileCatalogStays kept ≤ 100, kept×5 ≤ built PASS FAIL 107 / 1910 PASS
TestWithViaKeepsTheJoinedList kept ≤ 60 FAIL 67 FAIL 106 PASS
TestOwnOptionsKept kept×5 ≤ built FAIL 82 / 202 FAIL 121 / 202 FAIL 74 / 202
TestIsMagpieKeptWhileCatalogStays (cache hit) PASS FAIL PASS

(numbers are allocations a run, as the assertions print them; "+38" adds TRIAGE_PAD_000…037, nothing else changes. The TestIsMagpieKeptWhileCatalogStays row was run at v0.1.2310, a377ffde, with the same envs; the others at v0.1.2519.)

Traced. Adding 38 variables adds 39 allocations to every kept path (67→106, 82→121), and the built side of TestOwnOptionsKept doesn't move (202). Every kept path goes through stampedLists.shared (route.go), which calls provider.StampNow before it looks at its cache; StampNow calls environSum (internal/provider/hold.go:80), which ranges over os.Environ(). On Windows syscall.Environ reads the environment block and makes a new string per entry each call (syscall/env_windows.go, UTF16ToString in the loop, slice cap 50), so a cache hit costs about one allocation per variable. On Unix os.Environ copies a slice of strings already made, so it is cheap.

So on Windows the bars measure the size of the machine's environment more than magpie's cache. TestClaudeViaMagpieKept can't pass there at all: syncHome (sync_test.go:25) alone sets 23 variables, more than its bar of 20.

Ways out (not tried):

  • make environSum cheap on Windows, e.g. hash the raw block from GetEnvironmentStringsW without making strings, or remember the last block's sum and compare bytes;
  • or have these tests count allocations with environSum taken out (a test hook), and keep one test for environSum's own cost.

TestOwnOptionsKept stays red even with the smallest environment tried (74 > 202/5), so it may have a cost of its own besides this one; not split out further.

laweit-lxy avatar Nov 01 '26 13:11 laweit-lxy