Skip to content

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

usage: a row's Codex thread is kept as a 4-byte place, not a string (#2278)

yetone0dc4cec85d86
diff --git a/internal/usage/codex_actors.go b/internal/usage/codex_actors.go
new file mode 100644
index 000000000..e1ea3a5c3
--- /dev/null
+++ b/internal/usage/codex_actors.go
@@ -0,0 +1,91 @@
+package usage
+
+import (
+ "slices"
+ "unsafe"
+)
+
+// codexActors is the Codex thread of each row of a chunk, by row, "" for
+// none. A row keeps its thread's place in names, not the string: the
+// rows of a day are one thread's turns after another, so a []string held
+// a 16-byte header a row for a few hundred names, 24.5 MB of the 184 MB
+// live after 34k sessions' start (#2278). A nil ids is no row's.
+type codexActors struct {
+ ids []uint32
+ // names are the threads, names[0] "" when any. A clone shares them
+ // clipped, so adding to one never writes into the other's.
+ names []string
+}
+
+func (a *codexActors) len() int { return len(a.ids) }
+
+// at is row i's thread, "" past the rows it has
+func (a *codexActors) at(i int) string {
+ if i < len(a.ids) {
+ return a.names[a.ids[i]]
+ }
+ return ""
+}
+
+func (a *codexActors) id(actor string) uint32 {
+ if len(a.names) == 0 {
+ a.names = []string{""}
+ }
+ if actor == "" {
+ return 0
+ }
+ if n := len(a.ids); n > 0 && a.names[a.ids[n-1]] == actor {
+ return a.ids[n-1] // the row before's thread, as most are
+ }
+ if i := slices.Index(a.names, actor); i >= 0 {
+ return uint32(i)
+ }
+ a.names = append(a.names, actor)
+ return uint32(len(a.names) - 1)
+}
+
+func (a *codexActors) add(actor string) { a.ids = append(a.ids, a.id(actor)) }
+
+// set makes row i's thread actor, with n rows held
+func (a *codexActors) set(n, i int, actor string) {
+ if len(a.ids) != n {
+ ids := make([]uint32, n)
+ copy(ids, a.ids)
+ a.ids = ids
+ }
+ a.ids[i] = a.id(actor)
+}
+
+func (a codexActors) clone() codexActors {
+ return codexActors{ids: slices.Clone(a.ids), names: slices.Clip(a.names)}
+}
+
+// some says some row has a thread
+func (a *codexActors) some() bool {
+ for _, id := range a.ids {
+ if id != 0 {
+ return true
+ }
+ }
+ return false
+}
+
+func (a codexActors) equal(b codexActors) bool {
+ if len(a.ids) != len(b.ids) {
+ return false
+ }
+ for i := range a.ids {
+ if a.at(i) != b.at(i) {
+ return false
+ }
+ }
+ return true
+}
+
+func (a *codexActors) bytes() int64 {
+ n := int64(cap(a.ids))*4 + int64(cap(a.names))*int64(unsafe.Sizeof(""))
+ for _, name := range a.names {
+ n += int64(len(name))
+ }
+ return n
+}
diff --git a/internal/usage/codex_actors_test.go b/internal/usage/codex_actors_test.go
new file mode 100644
index 000000000..31b6e8af6
--- /dev/null
+++ b/internal/usage/codex_actors_test.go
@@ -0,0 +1,40 @@
+package usage
+
+import (
+ "fmt"
+ "testing"
+ "time"
+)
+
+// A day's rows are one thread's turns after another: each keeps its
+// thread in 4 bytes, not a string's 16, and reads it back (#2278).
+func TestCodexActorsTakeFourBytesARow(t *testing.T) {
+ const rows, threads = 100_000, 200
+ c := &rowChunk{}
+ at := time.Date(2026, 10, 28, 0, 0, 0, 0, time.UTC)
+ actor := func(i int) string {
+ if i%7 == 0 {
+ return "" // a row of no Codex thread
+ }
+ return fmt.Sprintf("019a%028d", i*threads/rows)
+ }
+ for i := range rows {
+ c.add(Row{Record: Record{Time: at, Agent: "codex"}, codexActor: actor(i)}, "", int64(i), false)
+ }
+ for i := range rows {
+ if got := c.codexActors.at(i); got != actor(i) {
+ t.Fatalf("row %d: actor %q, want %q", i, got, actor(i))
+ }
+ }
+ if got, most := c.codexActors.bytes(), int64(rows*4*5/4+threads*64); got > most {
+ t.Fatalf("%d rows of %d threads hold %d bytes of actors, want at most %d", rows, threads, got, most)
+ }
+ clone := c.codexActors.clone()
+ setCodexActor(c, 3, "another")
+ if clone.at(3) != actor(3) || c.codexActors.at(3) != "another" || clone.equal(c.codexActors) {
+ t.Fatalf("a clone saw its original's change: %q, %q", clone.at(3), c.codexActors.at(3))
+ }
+ if len(clone.names) != threads+1 {
+ t.Fatalf("clone names = %d, want %d", len(clone.names), threads+1)
+ }
+}
diff --git a/internal/usage/codex_identity.go b/internal/usage/codex_identity.go
index b1bc065e8..d5f4cf139 100644
--- a/internal/usage/codex_identity.go
+++ b/internal/usage/codex_identity.go
@@ -6,7 +6,6 @@ import (
"strings"
"sync"
"time"
- "unsafe"
"github.com/yetone/magpie/internal/sessions"
)
@@ -172,7 +171,7 @@ func cloneCodexChunks(chunks []*rowChunk) []*rowChunk {
out := make([]*rowChunk, len(chunks))
for i, chunk := range chunks {
copy := *chunk
- copy.codexActors = slices.Clone(chunk.codexActors)
+ copy.codexActors = chunk.codexActors.clone()
copy.codexCompactions = nil
copy.codexIdentities = nil
out[i] = &copy
@@ -426,9 +425,7 @@ func foldCodexCall(refs []rowRef, read map[rowRef]Row, skip map[rowRef]bool) {
actorConflict := false
for i, ref := range refs {
rows[i] = read[ref]
- if ref.Index < len(ref.Chunk.codexActors) {
- actors[i] = ref.Chunk.codexActors[ref.Index]
- }
+ actors[i] = ref.Chunk.codexActors.at(ref.Index)
if i == 0 {
actor = actors[i]
} else {
@@ -460,20 +457,7 @@ func foldCodexCall(refs []rowRef, read map[rowRef]Row, skip map[rowRef]bool) {
}
func setCodexActor(chunk *rowChunk, index int, actor string) {
- if n := rowsIn(chunk); len(chunk.codexActors) != n {
- actors := make([]string, n)
- copy(actors, chunk.codexActors)
- chunk.codexActors = actors
- }
- chunk.codexActors[index] = actor
-}
-
-func codexActorBytes(actors []string) int64 {
- bytes := int64(cap(actors)) * int64(unsafe.Sizeof(""))
- for _, actor := range actors {
- bytes += int64(len(actor))
- }
- return bytes
+ chunk.codexActors.set(rowsIn(chunk), index, actor)
}
// codexEffort keeps CodexModelEffort's last answer for each period: the
diff --git a/internal/usage/codex_identity_test.go b/internal/usage/codex_identity_test.go
index 9c1e6302f..510672d67 100644
--- a/internal/usage/codex_identity_test.go
+++ b/internal/usage/codex_identity_test.go
@@ -448,7 +448,7 @@ func TestCodexActorSurvivesPackedRequestChunk(t *testing.T) {
if row.SessionRole != "subagent" || row.Session != "main" || row.Subagent != "child" {
t.Fatalf("packed Codex actor identity was lost: %+v", row)
}
- if len(chunk.codexActors) != len(chunk.Rows) || chunk.codexActors[0] != "child" {
+ if chunk.codexActors.len() != len(chunk.Rows) || chunk.codexActors.at(0) != "child" {
t.Fatal("query identity changed the source chunk")
}
}
diff --git a/internal/usage/direct.go b/internal/usage/direct.go
index a1d15f287..dbb009ce9 100644
--- a/internal/usage/direct.go
+++ b/internal/usage/direct.go
@@ -73,8 +73,8 @@ func directRows(p Period, now time.Time) []Row {
matched[ref.Index] = true
}
for i, call := range logs {
- if i < len(local.codexActors) && local.codexActors[i] != call.ThreadID {
- actors[i] = local.codexActors[i]
+ if i < local.codexActors.len() && local.codexActors.at(i) != call.ThreadID {
+ actors[i] = local.codexActors.at(i)
}
}
}
diff --git a/internal/usage/request_cells.go b/internal/usage/request_cells.go
index 6affa3599..2d7e726c3 100644
--- a/internal/usage/request_cells.go
+++ b/internal/usage/request_cells.go
@@ -287,8 +287,8 @@ func (ps *pairedRows) sigs(identities map[string]sessions.CodexIdentity) map[*ro
if c.codexIdentities != nil {
h += mix(3)
}
- for i, a := range c.codexActors {
- if a != "" {
+ for i := range c.codexActors.len() {
+ if a := c.codexActors.at(i); a != "" {
h += mix(4, uint64(i), actor(a))
}
}
diff --git a/internal/usage/request_disk.go b/internal/usage/request_disk.go
index b5a7bc8d7..7fd5feb73 100644
--- a/internal/usage/request_disk.go
+++ b/internal/usage/request_disk.go
@@ -132,9 +132,9 @@ func diskEntry(c *rowChunk) []byte {
b = appendDiskString(b, name)
put(uint64(int64(offset)))
}
- put(uint64(len(c.codexActors)))
- for _, actor := range c.codexActors {
- b = appendDiskString(b, actor)
+ put(uint64(c.codexActors.len()))
+ for i := range c.codexActors.len() {
+ b = appendDiskString(b, c.codexActors.at(i))
}
put(uint64(len(archive)))
if !readMapped(c.mapped, func() { b = append(b, archive...) }) {
@@ -213,14 +213,14 @@ func readDiskEntry(b []byte) (*rowChunk, error) {
if !good {
return nil, errDiskEntry
}
- c.codexActors = append(c.codexActors, actor)
+ c.codexActors.add(actor)
}
n = take()
if b == nil || n != uint64(len(b)) || n == 0 {
return nil, errDiskEntry
}
c.Archive = b
- c.Bytes = int64(len(b)+len(c.Locations)*8) + codexActorBytes(c.codexActors)
+ c.Bytes = int64(len(b)+len(c.Locations)*8) + c.codexActors.bytes()
return c, nil
}
diff --git a/internal/usage/request_page.go b/internal/usage/request_page.go
index eda9c3262..4a8bfdecf 100644
--- a/internal/usage/request_page.go
+++ b/internal/usage/request_page.go
@@ -115,7 +115,7 @@ type rowChunk struct {
// row of the period at once. 364k calls had 7300 shapes.
Shapes [][rowTexts]uint32
shapeIDs map[[rowTexts]uint32]uint32
- codexActors []string
+ codexActors codexActors
codexCompactions map[int]*bool
codexIdentities map[string]sessions.CodexIdentity
dict map[string]uint32
@@ -275,21 +275,19 @@ func (c *rowChunk) add(r Row, msg string, order int64, failed bool) {
}
}
c.Rows = append(c.Rows, p)
- if c.codexActors != nil || r.codexActor != "" {
- if c.codexActors == nil {
- c.codexActors = make([]string, len(c.Rows)-1)
- c.Bytes += int64(len(c.codexActors)) * int64(unsafe.Sizeof(""))
+ if c.codexActors.ids != nil || r.codexActor != "" {
+ before := c.codexActors.bytes()
+ if c.codexActors.ids == nil {
+ c.codexActors.ids = make([]uint32, len(c.Rows)-1)
}
- c.codexActors = append(c.codexActors, r.codexActor)
+ c.codexActors.add(r.codexActor)
+ c.Bytes += c.codexActors.bytes() - before
}
c.Count = len(c.Rows)
if r.Time.After(c.Latest) {
c.Latest = r.Time
}
c.Bytes += int64(unsafe.Sizeof(packedRow{}))
- if c.codexActors != nil {
- c.Bytes += int64(unsafe.Sizeof("")) + int64(len(r.codexActor))
- }
}
func (c *rowChunk) row(i int) Row {
p := &c.Rows[i]
@@ -331,11 +329,7 @@ func (c *rowChunk) row(i int) Row {
}
r.Computer = c.Computer
if c.codexIdentities != nil {
- actor := ""
- if i < len(c.codexActors) {
- actor = c.codexActors[i]
- }
- r = codexIdentityRow(r, actor, c.codexIdentities)
+ r = codexIdentityRow(r, c.codexActors.at(i), c.codexIdentities)
}
return r
}
@@ -1554,11 +1548,7 @@ func pairRows(p Period, now time.Time, gateways, chunks, others []*rowChunk, ide
since, until := p.Since(now), p.Until(now)
matched := matchedBlocks(gateways, chunks, skip, pairs, since, true)
for gateway, local := range pairs {
- actor := ""
- if local.Index < len(local.Chunk.codexActors) {
- actor = local.Chunk.codexActors[local.Index]
- }
- setCodexActor(gateway.Chunk, gateway.Index, actor)
+ setCodexActor(gateway.Chunk, gateway.Index, local.Chunk.codexActors.at(local.Index))
gateway.Chunk.setCodexCompaction(gateway.Index, compactions[local])
}
all := append(append(slices.Clone(gateways), chunks...), others...)
@@ -1588,7 +1578,7 @@ func (ps *pairedRows) visitActors(fn func(*Row)) {
defer rows.release()
var r Row
for _, c := range ps.all {
- if slices.ContainsFunc(c.codexActors, func(a string) bool { return a != "" }) {
+ if c.codexActors.some() {
if visitingActors != nil {
visitingActors(c)
}
diff --git a/internal/usage/request_pairs.go b/internal/usage/request_pairs.go
index efa494fcb..e79c798e4 100644
--- a/internal/usage/request_pairs.go
+++ b/internal/usage/request_pairs.go
@@ -18,7 +18,7 @@ type pairMemo struct {
// a chunk without a serial (the probes') is named by its rows instead
serials []uint64
skip, matched []serialRef
- actors map[uint64][]string
+ actors map[uint64]codexActors
compactions map[uint64]map[int]*bool
}
@@ -85,9 +85,9 @@ func memoOf(ps *pairedRows, serials []uint64, originals []*rowChunk) *pairMemo {
}
named[c] = serials[k]
k++
- if !slices.Equal(c.codexActors, originals[i].codexActors) {
+ if !c.codexActors.equal(originals[i].codexActors) {
if m.actors == nil {
- m.actors = map[uint64][]string{}
+ m.actors = map[uint64]codexActors{}
}
m.actors[serials[k-1]] = c.codexActors
}
diff --git a/internal/usage/request_snapshot.go b/internal/usage/request_snapshot.go
index 4c18e9096..c2f08298f 100644
--- a/internal/usage/request_snapshot.go
+++ b/internal/usage/request_snapshot.go
@@ -44,7 +44,7 @@ func (c *rowChunk) pack() *rowChunk {
next.Count = len(c.Rows) // what rowsIn says of it
next.Rows, next.Strings, next.Shapes, next.dict, next.shapeIDs = nil, nil, nil, nil, nil
next.Archive, next.Locations = encoded, locations
- next.Bytes = int64(len(next.Archive)+len(locations)*8) + codexActorBytes(next.codexActors)
+ next.Bytes = int64(len(next.Archive)+len(locations)*8) + next.codexActors.bytes()
return &next
}
@@ -202,7 +202,7 @@ func (c *rowChunk) unpackReusing(dst *rowChunk, into []packedRow, strs []string,
} else {
next.Strings = make([]string, int(count))
}
- next.Bytes = int64(len(next.Rows))*int64(unsafe.Sizeof(packedRow{})) + codexActorBytes(next.codexActors)
+ next.Bytes = int64(len(next.Rows))*int64(unsafe.Sizeof(packedRow{})) + next.codexActors.bytes()
// The strings are copied out in one piece: one allocation a block, not
// one a string. The section is walked once to find its end, and its
// copy again to cut the strings from it.