Skip to content

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

usage: a poll pairs Codex calls by id without decoding the session files the gateway names none of

yetone51af40f9cb34
diff --git a/internal/usage/codex_identity.go b/internal/usage/codex_identity.go
index 305bd4fdd..b1bc065e8 100644
--- a/internal/usage/codex_identity.go
+++ b/internal/usage/codex_identity.go
@@ -216,7 +216,21 @@ func codexChunkResponsePairs(gateways, locals []*rowChunk, duplicateLocal map[ro
}
lent := lentRows() // picks are looked up by a local's id, not keyed
defer lent.release()
+ // a local chunk is read only when one of its ids may be a pick's: on a
+ // page's poll every session file of the period was decoded again for
+ // the few the gateway named
+ picked := make(map[uint64]bool, len(picks))
+ for id := range picks {
+ picked[foldHash(id)] = true
+ }
+ known := chunkIDs(locals, &lent, false)
for _, chunk := range locals {
+ if !slices.ContainsFunc(known[chunk].calls, func(call uint64) bool { return picked[call>>foldShift] }) {
+ continue
+ }
+ if pairsOpening != nil {
+ pairsOpening(chunk)
+ }
open := lent.of(chunk)
for i := range open.Rows {
ref := rowRef{chunk, i}
@@ -244,11 +258,10 @@ 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.
+// foldIDs are each chunk's calls by a hash of their response id, kept by
+// the chunk's serial (chunkIDs). 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
@@ -256,34 +269,45 @@ var foldIDs struct {
}
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
+ // each call with a response id: the id's hash shifted left by
+ // foldShift, foldCodexLog when it is a Codex log call, and its index
+ // in the low foldIndexBits. 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
+const (
+ foldIndexBits = 24
+ foldCodexLog = 1 << foldIndexBits
+ foldShift = foldIndexBits + 1
+)
var foldSeed = maphash.MakeSeed()
+// foldHash is the part of an id's hash a foldedIDs call keeps.
+func foldHash(id string) uint64 { return maphash.String(foldSeed, id) << foldShift >> foldShift }
+
+// pairsOpening, in tests, is told each local chunk
+// codexChunkResponsePairs reads rows of.
+var pairsOpening func(*rowChunk)
+
// 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.
+// foldHashing, in tests, is told each chunk chunkIDs 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.
- first := map[uint64]rowRef{}
- again := map[uint64][]rowRef{}
- rows := lentRows() // an id is kept as its hash
- defer rows.release()
+// chunkIDs are locals' calls as foldIDs keeps them, read from a chunk
+// only when none are kept for its serial. next starts a fold: one that
+// is not is for the same chunks as the fold before it.
+func chunkIDs(locals []*rowChunk, rows *chunkRows, next bool) map[*rowChunk]*foldedIDs {
foldIDs.Lock()
- foldIDs.turn++
+ if next {
+ foldIDs.turn++
+ }
turn := foldIDs.turn
known := make(map[*rowChunk]*foldedIDs, len(locals))
for _, chunk := range locals {
@@ -295,41 +319,32 @@ func foldCodexRows(locals []*rowChunk) map[rowRef]bool {
foldIDs.Unlock()
var made map[uint64]*foldedIDs
for _, chunk := range locals {
- f := known[chunk]
- if f == nil {
- if foldHashing != nil {
- foldHashing(chunk)
- }
- 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 known[chunk] != nil {
+ continue
+ }
+ if foldHashing != nil {
+ foldHashing(chunk)
+ }
+ 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 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
+ call := foldHash(id)<<foldShift | uint64(i)
+ if row := open.row(i); isCodexRow(row) && row.Source == "log" {
+ call |= foldCodexLog
}
+ f.calls = append(f.calls, call)
}
- 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 {
- if again[h] == nil {
- again[h] = []rowRef{f}
- }
- again[h] = append(again[h], ref)
+ known[chunk] = f
+ 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
}
}
foldIDs.Lock()
@@ -339,12 +354,42 @@ func foldCodexRows(locals []*rowChunk) map[rowRef]bool {
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)
+ if next {
+ for serial, f := range foldIDs.by {
+ if f.turn+foldKept < foldIDs.turn {
+ delete(foldIDs.by, serial)
+ }
}
}
foldIDs.Unlock()
+ return known
+}
+
+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.
+ first := map[uint64]rowRef{}
+ again := map[uint64][]rowRef{}
+ rows := lentRows() // an id is kept as its hash
+ defer rows.release()
+ known := chunkIDs(locals, &rows, true)
+ for _, chunk := range locals {
+ for _, call := range known[chunk].calls {
+ if call&foldCodexLog == 0 {
+ continue
+ }
+ ref, h := rowRef{chunk, int(call & (1<<foldIndexBits - 1))}, call>>foldShift
+ if f, ok := first[h]; !ok {
+ first[h] = ref
+ } else {
+ if again[h] == nil {
+ again[h] = []rowRef{f}
+ }
+ again[h] = append(again[h], ref)
+ }
+ }
+ }
var refs []rowRef
for _, hashed := range again {
refs = append(refs, hashed...)
diff --git a/internal/usage/codex_pairs_ids_test.go b/internal/usage/codex_pairs_ids_test.go
new file mode 100644
index 000000000..bd8f66556
--- /dev/null
+++ b/internal/usage/codex_pairs_ids_test.go
@@ -0,0 +1,49 @@
+package usage
+
+import (
+ "fmt"
+ "testing"
+ "time"
+)
+
+// A poll pairs the gateway's Codex calls with the session files' by
+// response id without reading a file none of whose ids the gateway named:
+// the ids kept for the fold say which chunks can hold one.
+func TestPairsReadOnlyChunksTheGatewayNamed(t *testing.T) {
+ now := time.Date(2026, 10, 27, 12, 0, 0, 0, time.UTC)
+ chunk := func(source string, 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: source}, "", 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(source, "-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
+ }
+ gateway := chunk("", "resp-1")
+ named, other := chunk("log", "resp-2", "resp-1"), chunk("log", "resp-9")
+ locals := []*rowChunk{named, other}
+ foldCodexRows(locals) // a poll folds the chunks first
+ var hashed, opened []*rowChunk
+ foldHashing = func(c *rowChunk) { hashed = append(hashed, c) }
+ pairsOpening = func(c *rowChunk) { opened = append(opened, c) }
+ t.Cleanup(func() { foldHashing, pairsOpening = nil, nil })
+
+ pairs, _ := codexChunkResponsePairs([]*rowChunk{gateway}, locals, nil)
+ if len(hashed) != 0 {
+ t.Fatalf("pairing read the ids of %d chunks again", len(hashed))
+ }
+ if len(opened) != 1 || opened[0] != named {
+ t.Fatalf("pairing read %d local chunks; want only the one holding resp-1", len(opened))
+ }
+ if got, ok := pairs[rowRef{gateway, 0}]; !ok || got != (rowRef{named, 1}) {
+ t.Fatalf("pairs = %v; want the gateway's resp-1 paired with the file's", pairs)
+ }
+}