Every 15 s Sessions poll (and every Requests page) lists every agent's
session files and stat()s each one: on a real history of ~21.7k files
in 462 folders that was ~21.7k lstat()/stat() calls, ~10 µs each on
APFS, and the largest part of the poll's CPU. Codex's day folders were
also read twice, once by WalkDir and once more for the stat()s.
dirent.Stats reads a folder's names, kinds, sizes and modification
times with getattrlistbulk, a few calls for the whole folder. statFiles
looks each file up in its folder's listing and stat()s alone only a link
it follows (ccFiles) or a folder that couldn't be listed so. Codex's
rollouts are walked with the listings themselves (listedRollouts), in
WalkDir's order, falling back to walkLinked and statFiles when a folder
can't be read so. Other systems keep the stat()s as they were.
Measured on the real history (~/.claude/projects and ~/.codex
sessions, read only):
- Stats against os.ReadDir + os.Lstat over all 21,672 entries in 462
folders: 0 mismatched kinds, sizes or times; 43 ms against 257 ms.
- `magpie serve` on the real history (sandbox HOME, its projects a link
to the real ones), /api/sessions asked back to back with
X-Magpie-Had for a 20 s CPU profile: before, 177 ms a poll, process
CPU 48.5 s for the run, 17.5 s of samples in statFiles; after, 157 ms,
34.3 s, and no per-file stat() in the profile beyond links.
Tests:
- TestStatsAsLstat: a file's size and time to the ns, a folder, links
to a file, a folder and nothing, a unicode name, and 3000 long names
(more than one call's 64 KB answer), each as os.Lstat says.
- TestStatFilesAsStat: follow and not, the same files kept with the same
sizes and times as os.Stat/os.Lstat; on macOS only the two links are
stat()ed alone. Red (7 alone, want 0) with Stats turned off by hand.
- TestListedRolloutsAsWalked: nested days, a folder and a link named as
rollouts, a .zst, a non-rollout, and a sessions folder that is a link
(#2233): the same rollouts, order, sizes and times as walkLinked plus
statFiles. Red with the listing's sort taken out by hand.
- TestCodexFilesStatInOrder (existing) passes on the new path.
Run at this head:
- gofmt -l clean; GOOS=linux and GOOS=windows go vet -tags nogui on
internal/sessions and internal/dirent ok.
- go test -tags nogui -count=1 -timeout 45m ./internal/dirent/
./internal/sessions/ ./internal/usage/ ./internal/gui/: ok (2.4 s,
13.9 s, 15.9 s, 51.1 s).
- go test -tags nogui -count=1 -timeout 45m at fe903798f, the packages that
import sessions: agent 293s, cli 57s, davsync 73s, gateway 493s,
library 192s, otlp 19s, tui 31s, tune 7s, all ok
Not tried: Windows and Linux run the stat()s as before, so no change
there; an SMB or other network volume on macOS, where getattrlistbulk
fails and the per-file path is taken.