Skip to content

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

usage: a session file's Codex ids are hashed once while it stands

yetonec272f3799ea9
diff --git a/internal/usage/codex_fold_ids_test.go b/internal/usage/codex_fold_ids_test.go
new file mode 100644
index 000000000..8298017be
--- /dev/null
+++ b/internal/usage/codex_fold_ids_test.go
@@ -0,0 +1,58 @@
+package usage
+
+import (
+ "fmt"
+ "maps"
+ "testing"
+ "time"
+)
+
+// A chunk's Codex ids are read once while its serial stands: a poll that
+// folds the same session files again reads none of them, and folds them
+// the same. A chunk made again (a live session's) is read again.
+func TestFoldReadsAChunksIDsOnce(t *testing.T) {
+ now := time.Date(2026, 10, 27, 12, 0, 0, 0, time.UTC)
+ chunk := func(ids ...string) *rowChunk {
+ c := &rowChunk{}
+ for i, id := range ids {
+ c.add(Row{Record: Record{Time: now.Add(-time.Duration(i) * time.Minute), Agent: "codex", Model: "native-model", Input: 80, Output: 8,
+ Session: "native-session", ResponseID: id}, Source: "log"}, "", int64(i), false)
+ }
+ for i := range 200 { // enough for the archive to be smaller than the rows
+ c.add(Row{Record: Record{Time: now.Add(-time.Hour), Agent: "claude", Model: "m", Session: "s", ResponseID: fmt.Sprint("other-", i)}}, "", int64(len(ids)+i), false)
+ }
+ packed := c.pack()
+ if len(packed.Archive) == 0 {
+ t.Fatal("the chunk wasn't packed")
+ }
+ packed.serial = nextChunkSerial()
+ return packed
+ }
+ a, b := chunk("resp-1", "resp-2"), chunk("resp-1", "resp-3")
+ var read []*rowChunk
+ foldHashing = func(c *rowChunk) { read = append(read, c) }
+ t.Cleanup(func() { foldHashing = nil })
+
+ first := foldCodexRows([]*rowChunk{a, b})
+ if len(read) != 2 || len(first) != 1 || !first[rowRef{b, 0}] {
+ t.Fatalf("first fold read %d chunks, skipped %v; want both read and b's resp-1 skipped", len(read), first)
+ }
+ read = nil
+ again := foldCodexRows([]*rowChunk{a, b})
+ if len(read) != 0 {
+ t.Fatalf("a fold of the same chunks read %d of them again", len(read))
+ }
+ if !maps.Equal(first, again) {
+ t.Fatalf("the same chunks folded %v, then %v", first, again)
+ }
+
+ read = nil
+ live := chunk("resp-1", "resp-4")
+ folded := foldCodexRows([]*rowChunk{a, live})
+ if len(read) != 1 || read[0] != live {
+ t.Fatalf("a chunk made again wasn't the one read: %d read", len(read))
+ }
+ if len(folded) != 1 || !folded[rowRef{live, 0}] {
+ t.Fatalf("the live chunk's copy of a's call = %v", folded)
+ }
+}
diff --git a/internal/usage/codex_identity.go b/internal/usage/codex_identity.go
index e2937cae2..88185eee1 100644
--- a/internal/usage/codex_identity.go
+++ b/internal/usage/codex_identity.go
@@ -244,26 +244,84 @@ func codexChunkResponsePairs(gateways, locals []*rowChunk, duplicateLocal map[ro
return pairs, compactions
}
+// foldIDs are each chunk's Codex log calls by a hash of their response
+// id, kept by the chunk's serial (foldCodexRows). A page polled while one
+// session was live decoded every session file of the period again to
+// hash the same ids: ~40 MB a poll of today's page, and as much again
+// for Codex's effort.
+var foldIDs struct {
+ sync.Mutex
+ by map[uint64]*foldedIDs
+ turn uint64
+}
+
+type foldedIDs struct {
+ // each call's hash shifted left by foldIndexBits, and its index in
+ // the bits that frees: calls whose hash is the same are told apart by
+ // their ids after, so a shortened hash only reads a few more rows
+ calls []uint64
+ turn uint64 // the fold that used it last
+}
+
+const foldIndexBits = 24
+
+var foldSeed = maphash.MakeSeed()
+
+// foldKept is how many folds a chunk's ids outlive the last that read
+// them: the page's and Codex effort's are of different chunks.
+const foldKept = 4
+
+// foldHashing, in tests, is told each chunk foldCodexRows reads ids from.
+var foldHashing func(*rowChunk)
+
func foldCodexRows(locals []*rowChunk) map[rowRef]bool {
// The calls are found by a hash of their response id, not the id: a map
// keyed by every Codex call's id held each decoded chunk's strings. Calls
// whose hash is the same are told apart by their ids after.
- seed := maphash.MakeSeed()
first := map[uint64]rowRef{}
again := map[uint64][]rowRef{}
rows := lentRows() // an id is kept as its hash
defer rows.release()
+ foldIDs.Lock()
+ foldIDs.turn++
+ turn := foldIDs.turn
+ known := make(map[*rowChunk]*foldedIDs, len(locals))
for _, chunk := range locals {
- open := rows.of(chunk)
- for i := range open.Rows {
- id := open.Strings[open.text(&open.Rows[i], rowResponseID)]
- if id == "" {
- continue
+ if f := foldIDs.by[chunk.serial]; chunk.serial != 0 && f != nil {
+ f.turn = turn
+ known[chunk] = f
+ }
+ }
+ foldIDs.Unlock()
+ var made map[uint64]*foldedIDs
+ for _, chunk := range locals {
+ f := known[chunk]
+ if f == nil {
+ if foldHashing != nil {
+ foldHashing(chunk)
}
- if row := open.row(i); !isCodexRow(row) || row.Source != "log" {
- continue
+ f = &foldedIDs{turn: turn}
+ open := rows.of(chunk)
+ for i := range open.Rows {
+ id := open.Strings[open.text(&open.Rows[i], rowResponseID)]
+ if id == "" {
+ continue
+ }
+ if row := open.row(i); !isCodexRow(row) || row.Source != "log" {
+ continue
+ }
+ f.calls = append(f.calls, maphash.String(foldSeed, id)<<foldIndexBits|uint64(i))
+ }
+ if chunk.serial != 0 && len(open.Rows) < 1<<foldIndexBits {
+ if made == nil {
+ made = map[uint64]*foldedIDs{}
+ }
+ f.calls = slices.Clip(f.calls)
+ made[chunk.serial] = f
}
- ref, h := rowRef{chunk, i}, maphash.String(seed, id)
+ }
+ for _, call := range f.calls {
+ ref, h := rowRef{chunk, int(call & (1<<foldIndexBits - 1))}, call>>foldIndexBits
if f, ok := first[h]; !ok {
first[h] = ref
} else {
@@ -274,6 +332,19 @@ func foldCodexRows(locals []*rowChunk) map[rowRef]bool {
}
}
}
+ foldIDs.Lock()
+ if foldIDs.by == nil {
+ foldIDs.by = map[uint64]*foldedIDs{}
+ }
+ for serial, f := range made {
+ foldIDs.by[serial] = f
+ }
+ for serial, f := range foldIDs.by {
+ if f.turn+foldKept < foldIDs.turn {
+ delete(foldIDs.by, serial)
+ }
+ }
+ foldIDs.Unlock()
var refs []rowRef
for _, hashed := range again {
refs = append(refs, hashed...)