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).
This commit is contained in:
yetone
2026-10-25 04:07:20 +08:00
parent 1b32bdcfcb
commit 22a7f86c9b
4 changed files with 34 additions and 10 deletions
+23 -6
View File
@@ -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 {
+7 -2
View File
@@ -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)
}
+3
View File
@@ -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) {
+1 -2
View File
@@ -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