sessions: a pass over every session looks at Codex's title index once (#3493)
assemble and assembleListed asked CodexTitles for each Codex session, and each ask took the index's lock and stat'ed session_index.jsonl. A start's stats step assembles every session, so on the owner's data (sbpairs copy: ~10G Claude, ~50G Codex rollouts) CodexTitles was 60ms of step 0's 860ms CPU profile. codexTitleOf gives a pass one function that looks at the index on its first ask and again only when another reader loaded a different index in between; every caller of assemble and assembleListed makes one per pass, beside its pricer(). Run at this head (rebased on origin/main below): - zzwarm step 0 CPU profile on the sbpairs real data: CodexTitles, codexTitlesAt and loadCodexTitles 60ms -> absent; statsRead 340ms -> 270ms cum. Wall times at load 40-76 were within noise both ways. - TestCodexTitleOfLooksOnce: no index, an index made mid-pass not seen in that pass, seen in the next, a rename mid-pass keeps the first name. Broken by hand (re-ask on every call): red at "an index made after the first ask". - go test ./internal/sessions ./internal/usage ./internal/gui ok; -race ./internal/sessions ok; GOOS=linux/windows go vet -tags nogui ./internal/sessions ok; gofmt clean. Not changed: gui's own CodexTitles callers (trace.go, context.go) ask for a few ids at a time already. Not tried: a Linux or Windows real-data run.
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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) }
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user