loggedAnswers, which leadFirst falls back to when affinity has lost a Codex subagent's lead, read thirty days of the log into a Record a row and kept the rows of one conversation. usage.VisitSessions reads only those rows: a block whose Session and NativeSession strings don't name the conversation is skipped, a row of another session is passed over by its shape's string ids without becoming a Record, and a matched row's strings are cloned out of the lent decode buffer. Each packed block's sessions are kept once it is decoded (blockSessions, by weak pointer, so no block the log let go is held), so a lookup decodes only the blocks the conversation is in and the open tail. A page's copy with Codex identities (which rewrite Session as a row is read) is read whole and filtered on its records. Run on mote (macOS 27.2), at load ~70: - BenchmarkLeadLookup (40 blocks of 3000 rows, one conversation of 300): visit+filter 39-56ms and 2.9MB an op; VisitSessions 0.38-0.52ms and 12KB. - A copy of the owner's real usage.jsonl (8,853 rows, 1,270 sessions, 18 blocks): every session looked up both ways gives the same records; 6.9ms a lookup before, 3.4ms for the first and 0.39ms after, 14us for a conversation that isn't there. - TestVisitSessionsIsVisitOfThoseSessions, red with the shape filter's session ids taken out; TestVisitSessionsRecordsKeepTheirStrings, red with the clone taken out; TestVisitSessionsSkipsBlocksOfOtherSessions, red with the known-sessions skip taken out. - GOOS=windows go vet; GOOS=linux -tags nogui go vet: ok. gofmt -l: clean. - go test -race of the new tests and the gateway tests of a lead's answers, in internal/usage and internal/gateway: ok (4.4s, 31.3s). - go test ./internal/usage ./internal/gateway (whole packages) was still running on mote at load ~190 when this was pushed; its result goes on #4370. Not tried: a real Codex subagent whose lead lost affinity; the lookup's callers and filters are as they were, and the gateway's loggedAnswers tests cover them.
127 lines
4.2 KiB
Go
127 lines
4.2 KiB
Go
package usage
|
|
|
|
import (
|
|
"fmt"
|
|
"slices"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// sessionChunks are benchChunks with a row in every tenth told its
|
|
// agent's own conversation id, "conv-<chunk>".
|
|
func sessionChunks(t testing.TB, n, size int) []*rowChunk {
|
|
t.Helper()
|
|
at := time.Date(2026, 10, 1, 0, 0, 0, 0, time.UTC)
|
|
var chunks []*rowChunk
|
|
for k := range n {
|
|
c := &rowChunk{}
|
|
for i := range size {
|
|
at = at.Add(time.Second)
|
|
r := Record{Time: at, Agent: "codex", Provider: fmt.Sprint("p", k%3), Model: "gpt-5", Input: i, Session: fmt.Sprint("s", k), RequestID: fmt.Sprint("req-", k, "-", i)}
|
|
if i%10 == 0 {
|
|
r.NativeSession = fmt.Sprint("conv-", k)
|
|
}
|
|
c.add(Row{Record: r}, fmt.Sprint("resp-", k, "-", i), int64(i), false)
|
|
}
|
|
packed := c.pack()
|
|
if len(packed.Archive) == 0 {
|
|
t.Fatal("not packed")
|
|
}
|
|
chunks = append(chunks, packed)
|
|
}
|
|
return chunks
|
|
}
|
|
|
|
// visitSessions hands on the records visit does whose Session or
|
|
// NativeSession is named, from since on, and nothing else.
|
|
func TestVisitSessionsIsVisitOfThoseSessions(t *testing.T) {
|
|
s := &logSnapshot{blocks: sessionChunks(t, 12, 200)}
|
|
since := s.blocks[3].Latest.Add(-50 * time.Second)
|
|
// twice: the second pass reads only the blocks it learnt name them
|
|
cases := [][]string{{"s5"}, {"conv-7"}, {"s4", "conv-9", "nobody"}, {"s3"}, {"nobody"}, {""}}
|
|
for _, named := range append(cases, cases...) {
|
|
var want, got []Record
|
|
s.visit(since, func(r Record) {
|
|
if r.Session != "" && slices.Contains(named, r.Session) || r.NativeSession != "" && slices.Contains(named, r.NativeSession) {
|
|
want = append(want, r)
|
|
}
|
|
})
|
|
s.visitSessions(since, named, func(r Record) { got = append(got, r) })
|
|
if len(got) != len(want) {
|
|
t.Fatalf("%q: %d records, want %d", named, len(got), len(want))
|
|
}
|
|
for i := range want {
|
|
if got[i].RequestID != want[i].RequestID || !got[i].Time.Equal(want[i].Time) || got[i].Provider != want[i].Provider {
|
|
t.Fatalf("%q record %d: %+v, want %+v", named, i, got[i], want[i])
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// A record visitSessions hands on keeps its strings after the next
|
|
// block is decoded over the one it came from.
|
|
func TestVisitSessionsRecordsKeepTheirStrings(t *testing.T) {
|
|
s := &logSnapshot{blocks: sessionChunks(t, 6, 300)}
|
|
var got []Record
|
|
s.visitSessions(time.Time{}, []string{"conv-0", "conv-1", "conv-2", "conv-3", "conv-4", "conv-5"}, func(r Record) { got = append(got, r) })
|
|
if len(got) != 6*30 {
|
|
t.Fatalf("%d records, want %d", len(got), 6*30)
|
|
}
|
|
for i, r := range got {
|
|
k, n := i/30, i%30*10
|
|
if r.RequestID != fmt.Sprint("req-", k, "-", n) || r.NativeSession != fmt.Sprint("conv-", k) || r.Session != fmt.Sprint("s", k) {
|
|
t.Fatalf("record %d reads %q %q %q after the blocks after it were decoded", i, r.RequestID, r.NativeSession, r.Session)
|
|
}
|
|
}
|
|
}
|
|
|
|
// BenchmarkLeadLookup is Codex's lead lookup over a month's log: every
|
|
// record, then the ones of one conversation, against those alone (#4370).
|
|
func BenchmarkLeadLookup(b *testing.B) {
|
|
s := &logSnapshot{blocks: sessionChunks(b, 40, 3000)}
|
|
conv := []string{"conv-17"}
|
|
b.Run("visit", func(b *testing.B) {
|
|
b.ReportAllocs()
|
|
for range b.N {
|
|
n := 0
|
|
s.visit(time.Time{}, func(r Record) {
|
|
if slices.Contains(conv, r.NativeSession) || slices.Contains(conv, r.Session) {
|
|
n++
|
|
}
|
|
})
|
|
if n != 300 {
|
|
b.Fatal(n)
|
|
}
|
|
}
|
|
})
|
|
b.Run("visitSessions", func(b *testing.B) {
|
|
b.ReportAllocs()
|
|
for range b.N {
|
|
n := 0
|
|
s.visitSessions(time.Time{}, conv, func(Record) { n++ })
|
|
if n != 300 {
|
|
b.Fatal(n)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
// A block that names none of the sessions looked up is not decoded once
|
|
// its sessions are known.
|
|
func TestVisitSessionsSkipsBlocksOfOtherSessions(t *testing.T) {
|
|
s := &logSnapshot{blocks: sessionChunks(t, 20, 2000)}
|
|
decoded := 0
|
|
sessionsDecoding = func() { decoded++ }
|
|
t.Cleanup(func() { sessionsDecoding = nil })
|
|
s.visitSessions(time.Time{}, []string{"nobody"}, func(Record) {})
|
|
if decoded != 20 {
|
|
t.Fatalf("the first lookup decoded %d blocks of 20", decoded)
|
|
}
|
|
decoded = 0
|
|
n := 0
|
|
s.visitSessions(time.Time{}, []string{"conv-19", "nobody"}, func(Record) { n++ })
|
|
if decoded != 1 || n != 200 {
|
|
t.Fatalf("a lookup of one block's conversation decoded %d blocks of 20 and read %d records, want 1 and 200", decoded, n)
|
|
}
|
|
}
|