A Sessions poll copies every agent's list of ~20k files several times
(listedRollouts, heldFiles' clone, callFilesWith, allFiles, byKey,
statsRead). Each file was 232 bytes, most of it fields only OpenCode,
ZCode, Hermes, Alma, agy, Cursor's editor or WSL files fill. Those move
into fileMore behind file.more (nil when a file has none); reads go
through m(), writes through w(), which copies first, so copies of a file
never see each other's writes, as before.
A/B with the real ~/.codex and ~/.claude data (ab5.sh, 8 polls 15s
apart, alloc_space between heap-0 and heap-1), base f51ab771f vs this:
- sessionRoutes 848 MB -> 731 MB
- allFiles 426 MB -> 281 MB, heldFiles 322 MB -> 216 MB
- callFilesWith 199 MB -> 109 MB, byKey 86 MB -> 56 MB
- statsRead 467 MB -> 375 MB, listSessions 456 MB -> 352 MB
- all HTTP handlers 1422 MB -> 1379 MB; the profile's total rose
(2392 -> 2570 MB) in usage's ledger reads, a package this doesn't
touch, on live data that grew between the runs.
Footprint was not compared: a GUI suite and Go tests ran beside it.
Run before the rebase, at f51ab771f plus this change:
go test -timeout 45m ./internal/sessions/... ./internal/gui/
./internal/usage/... ./internal/cli/ ./internal/tui/ ./internal/otlp/
./internal/tune/ ./internal/agent/ ./internal/gateway/: all ok.
Run at the pushed head (rebased on 6a6c06868, which touches no
sessions file): go test ./internal/sessions/... ok; gofmt clean;
GOOS=linux, windows, darwin go vet -tags nogui ./internal/sessions/
./internal/gui/ ok; the whole tree vetted on all three before the rebase.
Not run: the GUI JS suite (no GUI file changes).
A read of the sessions (List, Stats, Manage, the agents' lists, Codex's
threads) took dbReadMu, a plain mutex, for the whole call, parse
included. The start's warm-up reads every session file with a quarter of
the cores, so the first pages and API calls after a start waited for its
whole read before they could even look at the cache.
- dbReadMu becomes storeMu, an RWMutex: reads take it shared, and only
the changes to the files (Delete, a Codex provider move, a Claude
project relocation) take it alone.
- A file another refresh is reading now is in `parsing`, with a channel
closed when its parse is in the cache. A second read serves the parse
it already has of such a file, and waits only for one it has never
read; it never reads the same file twice at once.
- Each parse is published as it finishes, not in checkpoint batches, so
a waiting reader gets it at once.
- The shared store handles (OpenCode, Hermes, Alma, Cursor) closed at the
end of every read; with reads overlapping, one could close a handle
another still used. They now live as long as their last user
(useDBs). get() and the titles path closed them without the lock at
all before.
Tests (internal/sessions/refresh_shared_test.go), each broken by hand:
- TestReadNotHeldByAnotherRead: the warm-up's parse of 12 grown files is
held; List and Manage answer within 10s from the kept parses, each file
is parsed once in all, and a later List parses none. With storeMu.Lock
in List: red, "List waited for another read of the same files to end".
- TestReadWaitsForAFirstReadUnderWay: a file never read is waited for,
not served empty, and read once. Without the waits: red, "List answered
without the files being read". Without the skip of a file being read:
panic, close of nil channel.
- TestStoresLiveAsLongAsTheirLastUser: with the release always closing:
red, "closed while a read still used it".
All three pass with -race.
Measured on a sandbox instance (600k gateway calls, ~4,600 Claude Code
and Codex sessions, 300 grown since the last start; 4 API calls asked
3.5s after the routes, when the warm-up starts), base 1a3b92688 against
this branch, back to back. The machine's load average was 350-480
(nine agents running suites), so these timings are mostly CPU wait and
vary by tens of seconds run to run:
first answer (stats?days=30) base 91s / 81s new 50s / 79s
manage?agent=claude base 112s / 95s new 83s / 96s
The pages no longer queue behind the warm-up's lock, but at this load the
answer is bound by the CPU its own parse gets, so no gain shows above the
noise in the second pair. On an idle machine the held reads are what
#2776 reports; the tests above show they are no longer held.
Run on origin/main 2ceff7370 with all four #2776 commits' changes
(this one and the three beside it), load average 291-568 (nine agents
running suites), macOS:
- go test -p 4 ./... -timeout 45m, 09:35-10:49: 62 packages ok,
among them agent (2310s), gateway (1707s), provider (764s), usage
(83s). Red:
- internal/sessions: TestWSLOmpProfiles, TestWSLStoppedDistroKept,
TestWSLStoppedSinceNotOpened, TestWSLNoneWithoutHomes, each
"TempDir: mkdir ...: no space left on device" (the disk filled
mid-run). After space was freed, ./internal/sessions alone: ok
(139s).
- internal/gui: TestDockJoinLeadsCommandTab, TestPanelCloseHidesIt,
TestPanelJoinsFullScreenSpaces, TestPluginPageLoadsItsFilesInTheWindow,
TestDockReopenKeepsTheWindowsSpace (each killed at 30s) and
TestMainWindowCorners (86s): the real-window AppKit tests under
load, already filed as #2645 and #1936. Rerun alone at load ~335:
all pass, with TestSetAnswersWhileDevinAndProvidersHang (17s).
- gofmt -l: none. go vet: darwin, GOOS=linux -tags nogui and
GOOS=windows all exit 0.
- No internal/gui/assets file changed, so the GUI Playwright suite was
not run.
- At this commit alone (2ceff7370 + this change): ./internal/sessions
ok (82s), and its three new tests with -race ok.
What changed
- traceOpenCodeWindow keeps the latest main sessions. A child is not
exported, so it no longer takes one of the Limit places. Limit newer
children were filling the window and the parent was not read. The JSON
store and the SQLite store share that window.
Run
- TestOpenCodeChildrenDoNotEvictParentTrace failed on a15e576 plus the
test (sqlite and json: parent dropped behind 200 newer children, no
spans) and passes with the window. go test -tags nogui -count=1 -p 2
./internal/sessions/ passed in a sandbox home (648s). gofmt -l on the
three files printed nothing. go vet -tags nogui and go build -tags
nogui with CGO_ENABLED=0 passed for windows, linux and darwin.
- Not run: the rest of the module, -race, a real OpenCode database, or
a Langfuse server.
Full suite: go test -tags nogui -timeout 45m ./... (pipefail, suite-lock) at the four #2494/#2504/#2505/#2490 commits on origin/main a751f5282: 65 packages ok, among them internal/agent (2124s), internal/gateway (1845s) and internal/sessions (148s). internal/gui was red on TestSetAnswersWhileDevinAndProvidersHang alone (the state after a set took 11.5s at load ~250; #1375); internal/gui run again alone: ok (206s).
Generated-by: Grok 4.7 (Cursor)