usage: Codex effort reads only the session chunks that have Codex threads
CodexModelEffort (the Usage page's model and effort per Codex thread, read again each minute) unpacked every chunk of the ledger, Claude Code's and every other agent's too, to sum the few rows a Codex actor owns. codexEffortOfIndex now visits only chunks with a Codex actor (pairedRows.visitActors), and effortSums.add clones the model and effort it keeps, since lentRows lends its strings. Run at this head (d5f86d991+ this): go test ./internal/usage ./internal/usageapi ./internal/gui ok; TestCodexModelEffort* -race -count=3 ok; gofmt clean; GOOS=linux and GOOS=windows go vet -tags nogui ./internal/usage clean. TestCodexModelEffortIsTheWholeLedgers now checks a chunk was read and the Claude Code session's wasn't: red with the skip taken out by hand, and red with the clone taken out (the sums read the next chunk's strings). A/B on real session files, 8 sequential polls of state, sessions, stats, usage, usage/requests, quotas and providers, 15 s apart, the previous commit's build vs this: CodexModelEffort 305 -> 245 MB, codexEffortOfIndex 238 -> 176 MB; against v0.1.1661 twice, 304 -> 253 and 308 -> 245 MB. Totals moved with live sessions being written beside the runs (the sandbox HOME links ~/.codex/sessions and ~/.claude/projects): ReadCallSource read 46 to 398 MB a run on the same binaries, so the totals don't measure this change. Not changed: pairRowsMemo's misses in buildRequestBlocksIn and readRequestState's listing, the next hotspots. Rebased on33ccc6188(library, not usage) and run again at the pushed head: usage and usageapi ok.
This commit is contained in:
@@ -191,7 +191,20 @@ func TestCodexModelEffortIsTheWholeLedgers(t *testing.T) {
|
||||
if len(whole) != 2 {
|
||||
t.Fatalf("fixture: the whole ledger has %d Codex threads: %+v", len(whole), whole)
|
||||
}
|
||||
// and only the chunks a Codex thread is known for are read for it:
|
||||
// the Claude Code session's isn't
|
||||
var read []sessions.CallSource
|
||||
visitingActors = func(c *rowChunk) { read = append(read, c.Source) }
|
||||
t.Cleanup(func() { visitingActors = nil })
|
||||
if got := CodexModelEffort(All); !reflect.DeepEqual(got, whole) {
|
||||
t.Fatalf("Codex-only ledger:\n got %+v\nwant %+v", got, whole)
|
||||
}
|
||||
if len(read) == 0 {
|
||||
t.Fatal("no chunk was read for Codex's effort")
|
||||
}
|
||||
for _, src := range read {
|
||||
if src.Path == claude {
|
||||
t.Fatalf("the Claude Code session's chunk was read for Codex's effort: %v", read)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -660,7 +660,9 @@ func (groups effortSums) add(row *Row) {
|
||||
key := modelEffortKey{row.Model, row.Effort}
|
||||
entry := byModel[key]
|
||||
if entry == nil {
|
||||
entry = &effortTotal{value: sessions.ModelEffort{Model: row.Model, Effort: row.Effort}, priced: true}
|
||||
// the row's strings can be lent (visitActors): what is kept is a copy
|
||||
key = modelEffortKey{strings.Clone(row.Model), strings.Clone(row.Effort)}
|
||||
entry = &effortTotal{value: sessions.ModelEffort{Model: key.model, Effort: key.effort}, priced: true}
|
||||
byModel[key] = entry
|
||||
}
|
||||
entry.priced = entry.priced && row.Priced
|
||||
|
||||
@@ -825,7 +825,7 @@ func codexEffortOfIndex(st requestState, p Period, now time.Time) map[string][]s
|
||||
b := readRequestBlocks(idx, st, p, now, sessions.ReadCallSource)
|
||||
sums := effortSums{}
|
||||
ps, next := pairRowsMemo(p, now, b.gateways, b.chunks, b.others, st.codexIdentities, memo)
|
||||
ps.visit(func(_ timedRef, r *Row) { sums.add(r) })
|
||||
ps.visitActors(func(r *Row) { sums.add(r) })
|
||||
b.priced.release()
|
||||
shared.Lock()
|
||||
if shared.keep(idx, b.on) {
|
||||
@@ -1576,6 +1576,28 @@ func (ps *pairedRows) visit(fn func(timedRef, *Row)) {
|
||||
}
|
||||
}
|
||||
|
||||
// visitActors is visit of the rows of the chunks a Codex thread is known
|
||||
// for some row of (codexActors), the only rows Codex's effort adds up: it
|
||||
// decoded every session file's chunk, ~90 MB of what eight Sessions polls
|
||||
// allocated, for the few that are Codex's. The rows' strings are lent
|
||||
// (lentRows), so fn clones what it keeps.
|
||||
func (ps *pairedRows) visitActors(fn func(*Row)) {
|
||||
rows := lentRows()
|
||||
defer rows.release()
|
||||
var r Row
|
||||
for _, c := range ps.all {
|
||||
if slices.ContainsFunc(c.codexActors, func(a string) bool { return a != "" }) {
|
||||
if visitingActors != nil {
|
||||
visitingActors(c)
|
||||
}
|
||||
ps.visitChunk(&rows, c, &r, func(_ timedRef, r *Row) { fn(r) })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// visitingActors, in tests, is told each chunk visitActors reads.
|
||||
var visitingActors func(*rowChunk)
|
||||
|
||||
// visitChunk is visit of c's rows alone, read through rows into r.
|
||||
func (ps *pairedRows) visitChunk(rows *chunkRows, c *rowChunk, r *Row, fn func(timedRef, *Row)) {
|
||||
open := rows.of(c)
|
||||
|
||||
Reference in New Issue
Block a user