From 22a7f86c9b17693a2fa78106137418ed18879ff5 Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 25 Oct 2026 04:07:16 +0800 Subject: [PATCH] sessions: the Requests page walks the Codex folders once a rebuild readRequestState asked CallSources and then CodexIdentities, and each walked ~/.codex/sessions and archived_sessions, lstat()ing every rollout. Over the 12,069 rollouts of a real Codex folder that was two walks of ~75ms on every rebuild, which runs on each 5s refresh while an agent works. CallSourcesAndCodexIdentities walks once and hands the list to both; CallSources and CodexIdentities stay as they were for their other callers. Run at this head: - go test -timeout 45m ./internal/sessions ./internal/usage ./internal/usageapi: ok. TestCodexIdentityParentChainsAndSummaryCache now checks the one-walk pair equals CallSources and CodexIdentities. - GOOS=linux and GOOS=windows go vet -tags nogui on both packages, gofmt. - Real data (sandbox HOME over the owner's ~/.codex, 12,069 rollouts, ~416k rows): CPU profile of two rebuilds, codexFiles 0.31s before, 0.12s after. Wall times on a loaded machine were 1.0-2.1s both ways, too noisy to compare. Not tried: Windows or WSL folders (wslFiles is still walked as before). --- internal/sessions/calls.go | 29 +++++++++++++++++++----- internal/sessions/codex.go | 9 ++++++-- internal/sessions/codex_identity_test.go | 3 +++ internal/usage/request_page.go | 3 +-- 4 files changed, 34 insertions(+), 10 deletions(-) diff --git a/internal/sessions/calls.go b/internal/sessions/calls.go index 915e46ce8..afd96d23a 100644 --- a/internal/sessions/calls.go +++ b/internal/sessions/calls.go @@ -286,7 +286,10 @@ func callsFor(since time.Time, session, agent string) []Call { // callFiles are the session files that hold calls: Claude Code's, Cowork's // and Desktop's own, and Codex's. -func callFiles() []file { +func callFiles() []file { return callFilesWith(codexFiles()) } + +// callFilesWith is callFiles with codex the rollouts codexFiles found. +func callFilesWith(codex []file) []file { var out []file seen := map[string]bool{} add := func(fs []file) { @@ -305,7 +308,7 @@ func callFiles() []file { add(ccFiles("claude-desktop", h)) } } - add(codexFiles()) + add(codex) add(wslFiles("claude", "codex")) return out } @@ -320,8 +323,11 @@ func callFiles() []file { // that may be packed in zstd frames, is read on from the frame it was // left in (readDshCalls). Every agent here needs an entry in // wholeCallReaders, lineCallReaders or resumeCallReaders. -func callSources() []file { - out := callFiles() +func callSources() []file { return callSourcesWith(codexFiles()) } + +// callSourcesWith is callSources with codex the rollouts codexFiles found. +func callSourcesWith(codex []file) []file { + out := callFilesWith(codex) out = append(out, openCodeCallFiles()...) out = append(out, zcodeCallFiles()...) out = append(out, dshCallFiles()...) @@ -1070,8 +1076,19 @@ type CallSource struct { Modified time.Time } -func CallSources() []CallSource { - fs := callSources() +func CallSources() []CallSource { return callSourcesOf(callSources()) } + +// CallSourcesAndCodexIdentities is CallSources and CodexIdentities from one +// walk of the Codex folders. The Requests page asks for both on every +// rebuild, and the walk lstat()s each rollout: twice over 12k rollouts was +// ~150ms of each rebuild. +func CallSourcesAndCodexIdentities() ([]CallSource, map[string]CodexIdentity) { + codex := codexFiles() + sources := callSourcesOf(callSourcesWith(codex)) + return sources, codexIdentitiesOf(codex) +} + +func callSourcesOf(fs []file) []CallSource { pruneCalls(fs) out := make([]CallSource, 0, len(fs)) for _, f := range fs { diff --git a/internal/sessions/codex.go b/internal/sessions/codex.go index 8d27e7bc0..fff986e5e 100644 --- a/internal/sessions/codex.go +++ b/internal/sessions/codex.go @@ -3,6 +3,7 @@ package sessions import ( "bytes" "encoding/json" + "slices" "strings" "time" @@ -276,14 +277,18 @@ func noteCodexIdentity(s *state, identity CodexIdentity, conflict bool) { } // CodexIdentities returns an independent snapshot of the native thread identities. -func CodexIdentities() map[string]CodexIdentity { +func CodexIdentities() map[string]CodexIdentity { return codexIdentitiesOf(codexFiles()) } + +// codexIdentitiesOf is CodexIdentities with codex the rollouts codexFiles +// found. +func codexIdentitiesOf(codex []file) map[string]CodexIdentity { storeMu.RLock() defer storeMu.RUnlock() mu.Lock() defer mu.Unlock() loadCache() defer useDBs()() - want := append(codexFiles(), wslFiles("codex")...) + want := append(slices.Clip(codex), wslFiles("codex")...) refresh(want, nil) return codexIdentitySnapshot(want) } diff --git a/internal/sessions/codex_identity_test.go b/internal/sessions/codex_identity_test.go index c87552957..e3282e8ad 100644 --- a/internal/sessions/codex_identity_test.go +++ b/internal/sessions/codex_identity_test.go @@ -94,6 +94,9 @@ func TestCodexIdentityParentChainsAndSummaryCache(t *testing.T) { if got := CodexIdentities()["main-a"]; got.Role != codexRoleMain || got.RootSession != "main-a" { t.Fatalf("identity snapshot shares its map: %+v", got) } + if sources, ids := CallSourcesAndCodexIdentities(); !reflect.DeepEqual(sources, CallSources()) || !reflect.DeepEqual(ids, CodexIdentities()) { + t.Fatalf("one walk for both differs: %d sources, %d identities", len(sources), len(ids)) + } listed := map[string]Session{} for _, s := range List(All) { diff --git a/internal/usage/request_page.go b/internal/usage/request_page.go index cec40ae06..cafa1e597 100644 --- a/internal/usage/request_page.go +++ b/internal/usage/request_page.go @@ -560,8 +560,7 @@ type requestState struct { } func readRequestState(now time.Time) requestState { - sources := sessions.CallSources() - codexIdentities := sessions.CodexIdentities() + sources, codexIdentities := sessions.CallSourcesAndCodexIdentities() slices.SortFunc(sources, func(a, b sessions.CallSource) int { if n := a.Modified.Compare(b.Modified); n != 0 { return n