Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

usage: many rows made since the last write have it written once a run (#3493)

yetoneac149224f93b
diff --git a/internal/usage/request_disk_test.go b/internal/usage/request_disk_test.go
index 3f0585e18..687a66004 100644
--- a/internal/usage/request_disk_test.go
+++ b/internal/usage/request_disk_test.go
@@ -143,3 +143,57 @@ func TestRequestDiskNamesItsSource(t *testing.T) {
t.Fatalf("named %q, want %q of %d source files", requestDiskBinary(), want, sources)
}
}
+
+// Chunks of many rows made since the last write have the file written
+// once a run, though they are of few files: long sessions that ended
+// since were made again at every start. Once, as a session at work is
+// made again for each call.
+func TestRequestDiskRowsWrittenOnce(t *testing.T) {
+ pageHome(t)
+ now := holdClock(t, time.Date(2026, 9, 30, 12, 0, 0, 0, time.Local))
+ least, rows := requestDiskLeast, requestDiskLeastRows
+ requestDiskLeast, requestDiskLeastRows = 100, 3
+ requestCache.Lock()
+ requestCache.savedRows = false
+ requestCache.Unlock()
+ t.Cleanup(func() {
+ requestDiskLeast, requestDiskLeastRows = least, rows
+ requestCache.Lock()
+ requestCache.savedRows = false
+ requestCache.Unlock()
+ })
+ dir := filepath.Join(sessions.CodexDir(), "sessions", "2026", "09", "30")
+ rollout := func(i int) string {
+ return filepath.Join(dir, fmt.Sprintf("rollout-2026-09-30T10-00-0%d-s%d.jsonl", i, i))
+ }
+ write := func(i, input int) {
+ writeCodexCall(t, rollout(i), fmt.Sprintf("s%d", i), fmt.Sprintf("native-%d", i), `"cli"`, fmt.Sprintf("resp-%d", i), "m", "high", now.Add(-time.Duration(i+1)*time.Hour), input, i)
+ }
+ for i := range 5 {
+ write(i, 10)
+ }
+ page := func() RequestPage {
+ t.Helper()
+ got := queryPage(All, Filter{}, 0, 100, sessions.ReadCallSource)
+ requestDiskSaves.Wait()
+ return got
+ }
+ if p := page(); p.Total != 5 {
+ t.Fatalf("first page: %d rows, want 5", p.Total)
+ }
+ saved, err := os.ReadFile(requestDiskPath())
+ if err != nil {
+ t.Fatalf("5 rows of 5 files made, and no file of them: %v", err)
+ }
+ later := time.Now().Add(time.Minute)
+ for i := range 4 {
+ write(i, 20)
+ os.Chtimes(rollout(i), later, later)
+ }
+ if p := page(); p.Sum.Input != 20*4+10 {
+ t.Fatalf("second page: %d input, want %d", p.Sum.Input, 20*4+10)
+ }
+ if b, _ := os.ReadFile(requestDiskPath()); string(b) != string(saved) {
+ t.Fatalf("the file was written again for 4 rows")
+ }
+}
diff --git a/internal/usage/request_page.go b/internal/usage/request_page.go
index a8f445c50..f97360a25 100644
--- a/internal/usage/request_page.go
+++ b/internal/usage/request_page.go
@@ -495,10 +495,13 @@ type requestIndex struct {
pairs map[Period]*pairMemo
// on are the paths the last read listed; unsaved counts the chunks
// made since the file of them was last written (toDisk), and saving
- // says a write is under way
- on map[string]bool
- unsaved int
- saving bool
+ // says a write is under way; unsavedRows counts their rows, and
+ // savedRows says a write was made for them (once a run)
+ on map[string]bool
+ unsaved int
+ unsavedRows int
+ saving bool
+ savedRows bool
}
// heatPage is a heatmap's page, of the gateway log's snapshot version,
@@ -767,8 +770,9 @@ type requestBlocks struct {
gateways, chunks, others []*rowChunk
on map[string]bool
priced *blockPricer
- // built counts the session files' chunks made again
- built int
+ // built counts the session files' chunks made again, and builtRows
+ // their rows
+ built, builtRows int
}
// readRequestBlocks reads the blocks of p at now into idx, a private copy
@@ -850,7 +854,7 @@ func readRequestBlocks(idx *requestIndex, st requestState, p Period, now time.Ti
if len(want) >= requestDiskLeast {
idx.fromDisk(want)
}
- built := 0
+ built, builtRows := 0, 0
// A file's rows are made by the reader that read it: made one after
// another, they were most of a cold build's wait (#3493).
read := prefetchEach(sources, stale, func() func(sessions.CallSource) *rowChunk {
@@ -884,6 +888,7 @@ func readRequestBlocks(idx *requestIndex, st requestState, p Period, now time.Ti
c = read(i)
c.serial = nextChunkSerial()
built++
+ builtRows += c.Count
}
kept, next := *c, *c
kept.Used, next.Used = idx.tick, idx.tick
@@ -933,7 +938,7 @@ func readRequestBlocks(idx *requestIndex, st requestState, p Period, now time.Ti
delete(idx.chunks, path)
}
}
- return requestBlocks{gateways: gateways, chunks: chunks, others: others, on: on, priced: priced, built: built}
+ return requestBlocks{gateways: gateways, chunks: chunks, others: others, on: on, priced: priced, built: built, builtRows: builtRows}
}
// adopt drops what idx keeps of a state other than st: every chunk when
@@ -989,15 +994,28 @@ func (idx *requestIndex) keep(read *requestIndex, on map[string]bool) bool {
// noteBuilt counts the chunks b made, and writes the file of them once
// they are requestDiskLeast or more (a first read of the history): fewer
// are a few shard reads at the next start, and not worth writing the
-// whole file (~44MB for 15.6k sessions) again. idx is locked.
+// whole file (~44MB for 15.6k sessions) again. Or once a run, when they
+// hold requestDiskLeastRows rows: a few long sessions that ended since
+// the last write (9 files, 84.6k calls, ~1s) were made again at every
+// start, and the next ones that end joined them, until they were
+// requestDiskLeast files. Once, as a session still at work grows with
+// every call and is made again for each. idx is locked.
func (idx *requestIndex) noteBuilt(b requestBlocks) {
idx.on = b.on
idx.unsaved += b.built
+ idx.unsavedRows += b.builtRows
if idx.unsaved >= requestDiskLeast {
idx.save()
+ } else if !idx.savedRows && idx.unsavedRows >= requestDiskLeastRows {
+ idx.savedRows = true
+ idx.save()
}
}
+// requestDiskLeastRows is how many rows of chunks made since the last
+// write have the file written once a run, whatever their files.
+var requestDiskLeastRows = 20000
+
// requestDiskSaves are the writes under way, for the tests to wait on.
var requestDiskSaves sync.WaitGroup
@@ -1008,7 +1026,7 @@ func (idx *requestIndex) save() {
return
}
idx.saving = true
- idx.unsaved = 0 // a write that fails is tried again only for new chunks
+ idx.unsaved, idx.unsavedRows = 0, 0 // a write that fails is tried again only for new chunks
root, meta, chunks, on := idx.root, idx.meta, maps.Clone(idx.chunks), idx.on
requestDiskSaves.Add(1)
go func() {