diff --git a/internal/sessions/codex_provider.go b/internal/sessions/codex_provider.go index db951ee7d..52102c6b3 100644 --- a/internal/sessions/codex_provider.go +++ b/internal/sessions/codex_provider.go @@ -213,9 +213,9 @@ func MoveCodexProvider(id, to string) (CodexMove, error) { } refresh(mine, all) var fs []file - price := pricer() + price, titles := pricer(), codexTitleOf() for _, g := range groups { - if one, _ := assembleListed(g, price); one.ID == id && !one.ReadOnly { + if one, _ := assembleListed(g, price, titles); one.ID == id && !one.ReadOnly { fs = g break } diff --git a/internal/sessions/codex_titles.go b/internal/sessions/codex_titles.go index f8d61d31c..35a30a9c0 100644 --- a/internal/sessions/codex_titles.go +++ b/internal/sessions/codex_titles.go @@ -46,6 +46,28 @@ func CodexTitleDigest(name string) string { return codexFingerprint("title", name) } +// codexTitleOf is CodexTitles of one id at a time, for a pass that +// assembles every session (pricer): the index is looked at on the first +// ask only. A stat of it for each Codex session was ~60ms of a start's +// stats (#3493). +func codexTitleOf() func(id string) string { + var path string + var asked, loaded bool + return func(id string) string { + codexTitles.Lock() + defer codexTitles.Unlock() + // again only when another index was read since, in between + if !asked || loaded && codexTitles.path != path { + asked, path = true, codexTitleIndexPath() + loaded = loadCodexTitles(path) + } + if !loaded { + return "" + } + return codexTitles.names[id] + } +} + // CodexTitles reads only the append-only name index. It caches unchanged // files and reads newly appended complete lines from the previous offset. func CodexTitles(ids []string) map[string]string { return codexTitlesAt(codexTitleIndexPath(), ids) } diff --git a/internal/sessions/codex_titles_test.go b/internal/sessions/codex_titles_test.go index d68736db9..91cb26104 100644 --- a/internal/sessions/codex_titles_test.go +++ b/internal/sessions/codex_titles_test.go @@ -148,3 +148,42 @@ func TestCodexTitleEvidenceBound(t *testing.T) { t.Fatal("evidence bound broke normal title display") } } + +// A pass's codexTitleOf looks at the index on its first ask only, there or +// not, so assembling every Codex session stats it once; the next pass sees +// it as it is then. +func TestCodexTitleOfLooksOnce(t *testing.T) { + t.Setenv("XDG_CONFIG_HOME", t.TempDir()) + dir := t.TempDir() + t.Setenv("CODEX_HOME", dir) + path := filepath.Join(dir, "session_index.jsonl") + none := codexTitleOf() + if got := none("main"); got != "" { + t.Fatalf("no index: %q", got) + } + write := func(name string, at time.Time) { + t.Helper() + if err := os.WriteFile(path, []byte(fmt.Sprintf("{\"id\":\"main\",\"thread_name\":%q}\n", name)), 0600); err != nil { + t.Fatal(err) + } + if err := os.Chtimes(path, at, at); err != nil { + t.Fatal(err) + } + } + at := time.Now().Add(-time.Hour) + write("天气", at) + if got := none("main"); got != "" { + t.Fatalf("an index made after the first ask: %q, want none in this pass", got) + } + pass := codexTitleOf() + if got := pass("main"); got != "天气" { + t.Fatalf("next pass: %q", got) + } + write("长沙", at.Add(time.Minute)) + if got := pass("main"); got != "天气" { + t.Fatalf("a rename in the pass: %q, want the name of its first ask", got) + } + if got := codexTitleOf()("main"); got != "长沙" { + t.Fatalf("a pass after the rename: %q", got) + } +} diff --git a/internal/sessions/manage.go b/internal/sessions/manage.go index fdd9f6632..e3320db90 100644 --- a/internal/sessions/manage.go +++ b/internal/sessions/manage.go @@ -218,10 +218,10 @@ func listAgent(agent string, all []file) []Managed { identities = codexIdentitySnapshot(mine) taskTitles = codexSubagentTaskTitles(mine) } - price := pricer() + price, titles := pricer(), codexTitleOf() out := []Managed{} for _, fs := range groups { - s, _ := assemble(fs, price) + s, _ := assemble(fs, price, titles) setSessionCodexIdentity(&s, identities) setSessionCodexTaskTitle(&s, taskTitles) listed(&s) @@ -325,9 +325,9 @@ func Delete(agent, id string) (Trashed, error) { // the session as the page lists it: by the id assemble gives it var fs []file var s Session - price := pricer() + price, titles := pricer(), codexTitleOf() for _, g := range groups { - if one, _ := assembleListed(g, price); one.ID == id { + if one, _ := assembleListed(g, price, titles); one.ID == id { fs, s = g, one break } diff --git a/internal/sessions/sessions.go b/internal/sessions/sessions.go index 4dfa6e6d8..47f50424e 100644 --- a/internal/sessions/sessions.go +++ b/internal/sessions/sessions.go @@ -1206,10 +1206,10 @@ func listSessions(limit int) []Session { identities := codexIdentitySnapshot(files) taskTitles := codexSubagentTaskTitles(files) - price := pricer() + price, titles := pricer(), codexTitleOf() out := []Session{} for _, k := range keys { - if s, ok := assemble(groups[k], price); ok { + if s, ok := assemble(groups[k], price, titles); ok { setSessionCodexIdentity(&s, identities) setSessionCodexTaskTitle(&s, taskTitles) out = append(out, s) @@ -1247,8 +1247,8 @@ func sessionID(fs []file) string { } // assemble puts a session together from its files' parses. -func assemble(fs []file, price func(string) *catalog.Price) (Session, bool) { - s, ok := assembleListed(fs, price) +func assemble(fs []file, price func(string) *catalog.Price, codexTitle func(string) string) (Session, bool) { + s, ok := assembleListed(fs, price, codexTitle) if !ok { return s, false } @@ -1267,7 +1267,7 @@ func assemble(fs []file, price func(string) *catalog.Price) (Session, bool) { // agent, id, title and usage. The stats ask it of every session on each // poll, and building every session's subagents there read each Claude Code // subagent's file again (3.5 GB of a live magpie's 6.3 GB of scans). -func assembleListed(fs []file, price func(string) *catalog.Price) (Session, bool) { +func assembleListed(fs []file, price func(string) *catalog.Price, codexTitle func(string) string) (Session, bool) { sortSessionFiles(fs) s := Session{Agent: fs[0].agent, Path: fs[0].path, Models: []Model{}, WSL: fs[0].wsl} for _, f := range fs { @@ -1321,7 +1321,7 @@ func assembleListed(fs []file, price func(string) *catalog.Price) (Session, bool // Codex's thread name (session_index.jsonl), else the title the agent // made, else the first prompt, else the first message if s.Agent == "codex" && s.WSL == "" && custom == "" { - custom = CodexTitles([]string{s.ID})[s.ID] + custom = codexTitle(s.ID) } if s.Agent == "claude-desktop" && s.WSL == "" { custom = cmp.Or(DesktopTitles([]string{s.ID})[s.ID], custom) @@ -2141,9 +2141,9 @@ func Titles(keys []string) map[string]string { titleFiles := append(append([]file(nil), fs...), parents...) taskTitles := codexSubagentTaskTitles(titleFiles) identities := codexIdentitySnapshot(fs) - price := pricer() + price, titles := pricer(), codexTitleOf() for k, g := range groups { - if s, ok := assembleListed(g, price); ok { + if s, ok := assembleListed(g, price, titles); ok { setSessionCodexIdentity(&s, identities) setSessionCodexTaskTitle(&s, taskTitles) if s.Title == "" { @@ -2198,7 +2198,7 @@ func get(key string) (s Session, ok, found bool) { } else { refresh(fs, files) } - s, ok = assemble(fs, pricer()) + s, ok = assemble(fs, pricer(), codexTitleOf()) setSessionCodexIdentity(&s, identities) setSessionCodexTaskTitle(&s, taskTitles) return s, ok, true diff --git a/internal/sessions/stats.go b/internal/sessions/stats.go index 85d6dc109..7d3cd66e7 100644 --- a/internal/sessions/stats.go +++ b/internal/sessions/stats.go @@ -160,10 +160,10 @@ func statsRead(days int, now time.Time, native map[string]bool) Stats { for _, f := range files { groups[f.key] = append(groups[f.key], f) } - price := pricer() + price, titles := pricer(), codexTitleOf() for _, fs := range groups { // as List: a session nothing was said in is not listed - if s, ok := assembleListed(fs, price); ok { + if s, ok := assembleListed(fs, price, titles); ok { native[s.Agent+"|"+s.ID] = true } } @@ -291,7 +291,7 @@ func statsRead(days int, now time.Time, native map[string]bool) Stats { for date := range dates { byDate[date] = &Day{Date: date, Usage: []Usage{}, Active: []Active{}} } - price := pricer() + price, titles := pricer(), codexTitleOf() for k, t := range use { u := Usage{Agent: k.agent, Cwd: k.cwd, Model: k.model, Tokens: t} if p := price(k.model); p != nil { @@ -348,7 +348,7 @@ func statsRead(days int, now time.Time, native map[string]bool) Stats { if len(sh.dates) == 0 { continue } - s, ok := assembleListed(groups[key], price) + s, ok := assembleListed(groups[key], price, titles) if !ok { continue }