Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
usage: a session file two queries read at once is made once (#2278)
yetone6104e88aa958
diff --git a/internal/usage/block_price.go b/internal/usage/block_price.goindex d6c8258cb..84cbbcd16 100644--- a/internal/usage/block_price.go+++ b/internal/usage/block_price.go@@ -1,7 +1,6 @@package usageimport (- "crypto/sha256""slices""sync"@@ -70,9 +69,10 @@ var sharedPriced = struct {blocks map[pricedKey]*pricedBlock}{blocks: map[pricedKey]*pricedBlock{}}-// share has b's blocks shared with the other queries of meta.-func (b *blockPricer) share(meta string) {- b.meta, b.shared = sha256.Sum256([]byte(meta)), true+// share has b's blocks shared with the other queries of meta, a hash of+// requestState's root and meta.+func (b *blockPricer) share(meta [32]byte) {+ b.meta, b.shared = meta, true}// release hands the rows of the blocks it priced back, once the query nodiff --git a/internal/usage/block_price_test.go b/internal/usage/block_price_test.goindex ad685285c..830b89eaa 100644--- a/internal/usage/block_price_test.go+++ b/internal/usage/block_price_test.go@@ -1,6 +1,7 @@package usageimport (+ "crypto/sha256""testing""time"@@ -24,7 +25,7 @@ func TestPricedBlocksSharedWhileHeld(t *testing.T) {}pricer := func(meta string) *blockPricer {b := newBlockPricer(provider.Renames{}, nil, func(Record) *catalog.Price { return nil })- b.share(meta)+ b.share(sha256.Sum256([]byte(meta)))return b}held := func() int {diff --git a/internal/usage/request_build_share.go b/internal/usage/request_build_share.gonew file mode 100644index 000000000..2d4d4f0ea--- /dev/null+++ b/internal/usage/request_build_share.go@@ -0,0 +1,68 @@+package usage++import "sync"++// chunkBuildKey is a session file as one query reads it: what prices and+// names a call (requestState's root and meta, hashed), and the file's+// path, size and time.+type chunkBuildKey struct {+ meta [32]byte+ path string+ size int64+ modified int64+}++type chunkBuild struct {+ done chan struct{}+ c *rowChunk // as made, never changed; nil when the make panicked+}++// chunkBuilds are the session files' chunks being made now, by the+// queries reading at once. At a cold start with the app open the+// warm-up's page and the GUI's heatmap each read every session file+// beside the other, and each held a chunk of each: twice the CPU, and+// 76 MB of chunks in the making at the peak (#2278).+var chunkBuilds = struct {+ sync.Mutex+ m map[chunkBuildKey]*chunkBuild+}{m: map[chunkBuildKey]*chunkBuild{}}++// buildJoined, in tests, is told each time a query waits for another's+// chunk.+var buildJoined func()++// sharedBuild is the chunk of k that build makes, or one another query is+// making of k now, once it is made. Each caller is handed a copy of its+// own, since a query writes its chunk's serial, its use and where its+// archive is; the rows and the archive are shared, and only read.+func sharedBuild(k chunkBuildKey, build func() *rowChunk) *rowChunk {+ chunkBuilds.Lock()+ if b := chunkBuilds.m[k]; b != nil {+ chunkBuilds.Unlock()+ if buildJoined != nil {+ buildJoined()+ }+ <-b.done+ if b.c == nil {+ return build()+ }+ c := *b.c+ return &c+ }+ b := &chunkBuild{done: make(chan struct{})}+ chunkBuilds.m[k] = b+ chunkBuilds.Unlock()+ defer func() {+ chunkBuilds.Lock()+ delete(chunkBuilds.m, k)+ chunkBuilds.Unlock()+ close(b.done)+ }()+ made := build()+ if made == nil {+ return nil+ }+ b.c = made+ c := *made+ return &c+}diff --git a/internal/usage/request_build_share_test.go b/internal/usage/request_build_share_test.gonew file mode 100644index 000000000..5fb65cdef--- /dev/null+++ b/internal/usage/request_build_share_test.go@@ -0,0 +1,78 @@+package usage++import (+ "testing"+ "time"+)++// Queries reading one session file at once make one chunk of it, each+// handed a copy of its own: at a cold start with the app open, the+// warm-up's page and the heatmap each read every file (#2278). A make+// that panics leaves the waiter to make its own.+func TestSharedBuildMakesAFileOnce(t *testing.T) {+ t.Cleanup(func() { buildJoined = nil })+ joined := make(chan struct{}, 1)+ buildJoined = func() { joined <- struct{}{} }+ at := time.Date(2026, 10, 28, 12, 0, 0, 0, time.UTC)+ made := func() *rowChunk {+ var c rowChunk+ for i := 0; i < logBlockRows; i++ {+ c.add(Row{Record: Record{Time: at, Provider: "openai", Model: "gpt-5", Input: i}}, "", int64(i), false)+ }+ return c.pack()+ }+ k := chunkBuildKey{path: "/s/rollout.jsonl", size: 10, modified: 1}+ builds := 0+ inside, release := make(chan struct{}), make(chan struct{})+ first := make(chan *rowChunk)+ go func() {+ first <- sharedBuild(k, func() *rowChunk {+ builds+++ close(inside)+ <-release+ return made()+ })+ }()+ <-inside+ second := make(chan *rowChunk)+ go func() {+ second <- sharedBuild(k, func() *rowChunk { builds++; return made() })+ }()+ select { // the second waits for the first's chunk+ case <-joined:+ case <-second:+ t.Fatal("a file made again while another query was making it")+ }+ close(release)+ a, b := <-first, <-second+ if builds != 1 {+ t.Fatalf("one file made %d times", builds)+ }+ if a == b || len(a.Archive) == 0 || &a.Archive[0] != &b.Archive[0] {+ t.Fatalf("copies %p and %p of one archive (%d bytes)", a, b, len(a.Archive))+ }+ a.serial, a.Archive = 7, nil+ if b.serial == 7 || len(b.Archive) == 0 || b.unpack().row(3).Input != 3 {+ t.Fatal("one query's copy changed under another's")+ }+ chunkBuilds.Lock()+ n := len(chunkBuilds.m)+ chunkBuilds.Unlock()+ if n != 0 {+ t.Fatalf("%d builds left after both were done", n)+ }++ // a make that panics: the one waiting makes its own+ inside, release = make(chan struct{}), make(chan struct{})+ go func() {+ defer func() { recover() }()+ sharedBuild(k, func() *rowChunk { close(inside); <-release; panic("read") })+ }()+ <-inside+ go func() { second <- sharedBuild(k, made) }()+ <-joined+ close(release)+ if c := <-second; c == nil || c.unpack().row(5).Input != 5 {+ t.Fatalf("after a panicked make, read %v", c)+ }+}diff --git a/internal/usage/request_page.go b/internal/usage/request_page.goindex 90d1307be..441fc31b9 100644--- a/internal/usage/request_page.go+++ b/internal/usage/request_page.go@@ -703,9 +703,19 @@ func hashCodexIdentities(h hash.Hash, ids map[string]sessions.CodexIdentity) {func requestRoot() string { return Path() + "|" + catalog.CachePath() }func QueryPage(p Period, f Filter, offset, limit int) RequestPage {- return queryPage(p, f, offset, limit, sessions.ReadCallSource)+ return queryPageOf(p, f, offset, limit, sessions.ReadCallSource, true)}-func queryPage(p Period, f Filter, offset, limit int, readSource func(sessions.CallSource) []sessions.Call) (page RequestPage) {++// queryPage is QueryPage of the session files as readSource reads them,+// its chunks its own.+func queryPage(p Period, f Filter, offset, limit int, readSource func(sessions.CallSource) []sessions.Call) RequestPage {+ return queryPageOf(p, f, offset, limit, readSource, false)+}++// queryPageOf is queryPage; share says readSource is the files' own+// reader, so a chunk another query is making of a file is waited for+// (sharedBuild).+func queryPageOf(p Period, f Filter, offset, limit int, readSource func(sessions.CallSource) []sessions.Call, share bool) (page RequestPage) {if limit <= 0 {limit = 100}@@ -766,7 +776,7 @@ func queryPage(p Period, f Filter, offset, limit int, readSource func(sessions.Cshared := idxidx = shared.private()shared.Unlock()- b := readRequestBlocks(idx, st, p, now, readSource)+ b := readRequestBlocks(idx, st, p, now, readSource, share)defer b.priced.release() // on a panic too: a shared block is held until let govar names map[string]stringif someShared {@@ -819,7 +829,7 @@ func codexEffortOfIndex(st requestState, p Period, now time.Time) map[string][]smemo := shared.pairs[p]idx := shared.private()shared.Unlock()- b := readRequestBlocks(idx, st, p, now, sessions.ReadCallSource)+ b := readRequestBlocks(idx, st, p, now, sessions.ReadCallSource, true)defer b.priced.release()sums := effortSums{}ps, next := pairRowsMemo(p, now, b.gateways, b.chunks, b.others, st.codexIdentities, memo)@@ -849,8 +859,10 @@ type requestBlocks struct {// readRequestBlocks reads the blocks of p at now into idx, a private copy// of the request index (private): a session file or another computer's-// day is read again only when it changed since its chunk was made.-func readRequestBlocks(idx *requestIndex, st requestState, p Period, now time.Time, readSource func(sessions.CallSource) []sessions.Call) requestBlocks {+// day is read again only when it changed since its chunk was made. share+// says readSource is sessions.ReadCallSource: a file another query is+// making a chunk of now is waited for, not read beside it (sharedBuild).+func readRequestBlocks(idx *requestIndex, st requestState, p Period, now time.Time, readSource func(sessions.CallSource) []sessions.Call, share bool) requestBlocks {sources, cfg, snapshot, days := st.sources, st.cfg, st.snapshot, st.daysprice := Pricer()// Swapped's names are a few hundred pairs over thousands of files' calls,@@ -895,7 +907,8 @@ func readRequestBlocks(idx *requestIndex, st requestState, p Period, now time.Tivar gateways []*rowChunkrenames := provider.UsageRenames()priced := newBlockPricer(renames, cfg.ModelWires, price)- priced.share(st.root + "|" + st.meta)+ meta := sha256.Sum256([]byte(st.root + "|" + st.meta))+ priced.share(meta)for _, packed := range snapshot.blocks {if !gatewaySince.IsZero() && packed.Latest.Before(gatewaySince) {continue@@ -935,7 +948,7 @@ func readRequestBlocks(idx *requestIndex, st requestState, p Period, now time.Ti// another, they were most of a cold build's wait (#3493).makeChunk := func() func(sessions.CallSource) *rowChunk {priceRow := pricers()- return func(s sessions.CallSource) *rowChunk {+ build := func(s sessions.CallSource) *rowChunk {cs := readSource(s)c := &rowChunk{Source: s, Rows: make([]packedRow, 0, len(cs))}for _, call := range cs {@@ -951,6 +964,12 @@ func readRequestBlocks(idx *requestIndex, st requestState, p Period, now time.Tic.dict, c.shapeIDs = nil, nil // local chunks are immutable until the source changesreturn c.pack()}+ if !share {+ return build+ }+ return func(s sessions.CallSource) *rowChunk {+ return sharedBuild(chunkBuildKey{meta, s.Path, s.Size, s.Modified.UnixNano()}, func() *rowChunk { return build(s) })+ }}read := prefetchEach(sources, stale, makeChunk)for i, s := range sources {diff --git a/internal/usage/request_page_test.go b/internal/usage/request_page_test.goindex 95a5f2007..c8204ece0 100644--- a/internal/usage/request_page_test.go+++ b/internal/usage/request_page_test.go@@ -345,7 +345,7 @@ func TestRequestBlocksKeepOneChunkASource(t *testing.T) {shared.adopt(st)read := func() (*rowChunk, *requestIndex) {idx := shared.private()- b := readRequestBlocks(idx, st, All, now, sessions.ReadCallSource)+ b := readRequestBlocks(idx, st, All, now, sessions.ReadCallSource, true)defer b.priced.release()if len(b.chunks) != 1 {t.Fatalf("%d chunks, want 1", len(b.chunks))