matchedBlocks paired the gateway's calls with the session files' by request id, then by session, tokens and time, and decoded every session file of the period for each pass: twice per poll, for the few sessions the gateway had calls of. In a 24s heap profile of a fed sandbox it was 0.77 GB of the 5.3 GB allocated, and 5.4s of a minute's 42s of CPU. Each local chunk's hints are now kept by its serial (match_chunks.go, as chunkIDs keeps Codex's response ids): a 32-bit hash of each request id it names, a hash of each session a call of it can be matched in (agy's as agyMatchSession, rows of no tokens and no failure left out, as the pass does), and its last call's time. A pass decodes a chunk only when one of its ids or sessions is the gateway's and its last call is in the period. A hash that matches only decodes the chunk, so the matches are exact. Hints live 8 matchings past their last use. Run at this head (cb167fcbd+ this change), on mote: - TestMatchOpensOnlyNamedChunks (new): 20 seeds of 12 session files, the gateway naming four sessions and one id in a fifth file; the matches equal matchedLocalLegacy's and only named files are decoded. Red with namesAny always true ("matching decoded /local/session-4"). - TestMatchOpensAgyChunks (new): an agy step is matched with its file alone decoded. - go test ./internal/usage -timeout 45m: ok (26.9s). - go vet -tags nogui ./internal/usage for darwin, linux and windows: clean. A/B on mote, the sandbox of #2278's synthetic history (34k sessions, 3000 with gateway records), 10 page endpoints polled every 5s and 4 gateway appends a second, 230s each, old =dc6f6d902(v0.1.1927): old new all polls mean 4.93s 3.15s heatmap mean 11.86s 7.24s footprint peak 707.2M 632.4M avg CPU after feed 90.0% 79.6% mote's load was ~100 from other sessions' suites through both arms, so the times are slower than an idle machine's; the arms ran back to back. Then rebased ontob119f8777, which changes an internal/gateway test and LESSONS.md only. Not tried: the user's own history.
94 lines
3.5 KiB
Go
94 lines
3.5 KiB
Go
package usage
|
|
|
|
import (
|
|
"fmt"
|
|
"math/rand"
|
|
"reflect"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/sessions"
|
|
)
|
|
|
|
// Matching the gateway's calls to the session files' decodes only the
|
|
// files whose sessions or request ids the gateway names, and matches as
|
|
// reading every file did (#2278).
|
|
func TestMatchOpensOnlyNamedChunks(t *testing.T) {
|
|
for seed := int64(0); seed < 20; seed++ {
|
|
rng := rand.New(rand.NewSource(seed))
|
|
now := time.Now().Truncate(time.Second)
|
|
gateway := &rowChunk{}
|
|
var locals []*rowChunk
|
|
named := map[*rowChunk]bool{}
|
|
for s := range 12 {
|
|
session := fmt.Sprint("session-", s)
|
|
local := &rowChunk{Source: sessions.CallSource{Path: "/local/" + session}}
|
|
for i := range 60 {
|
|
r := Record{Time: now.Add(time.Duration(rng.Intn(20)) * time.Second), Agent: "claude", Provider: "relay", Session: session, Input: rng.Intn(3), Output: rng.Intn(2), Millis: 1000}
|
|
if i%4 == 0 {
|
|
r.RequestID = fmt.Sprint(session, "-", i)
|
|
}
|
|
if i%5 == 0 {
|
|
r.Status = 500
|
|
}
|
|
if s < 4 { // the gateway has calls of four sessions
|
|
gateway.add(Row{Record: r}, "", int64(len(gateway.Rows)), false)
|
|
}
|
|
if s == 0 && i == 0 { // its call is named in another session's file
|
|
r.RequestID = ""
|
|
}
|
|
if s == 5 && i == 0 {
|
|
r.RequestID = "session-0-0"
|
|
}
|
|
r.Time = r.Time.Add(time.Duration(rng.Intn(5)-2) * time.Second)
|
|
local.add(Row{Record: r, Source: "log"}, "", int64(i), r.Failed())
|
|
}
|
|
local.serial = nextChunkSerial()
|
|
locals = append(locals, local)
|
|
if s < 4 || s == 5 {
|
|
named[local] = true
|
|
}
|
|
}
|
|
for _, since := range []time.Time{{}, now.Add(5 * time.Second)} {
|
|
var opened []*rowChunk
|
|
matchOpening = func(c *rowChunk) { opened = append(opened, c) }
|
|
got := matchedBlocks([]*rowChunk{gateway}, locals, nil, nil, since, false)
|
|
matchOpening = nil
|
|
want := matchedLocalLegacy(gateway, locals, nil, since)
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Fatalf("seed %d: %d matches, want %d", seed, len(got), len(want))
|
|
}
|
|
for _, c := range opened {
|
|
if !named[c] {
|
|
t.Fatalf("seed %d: matching decoded %s, whose sessions and ids the gateway never named", seed, c.Source.Path)
|
|
}
|
|
}
|
|
if since.IsZero() && len(opened) == 0 {
|
|
t.Fatalf("seed %d: matching decoded no chunk", seed)
|
|
}
|
|
}
|
|
}
|
|
t.Cleanup(func() { matchOpening = nil })
|
|
}
|
|
|
|
// agy's gateway calls name no session, and its transcript's steps are
|
|
// matched by tokens and time (#2097): its file is one the gateway names.
|
|
func TestMatchOpensAgyChunks(t *testing.T) {
|
|
now := time.Now().Truncate(time.Second)
|
|
gateway, agy, other := &rowChunk{}, &rowChunk{}, &rowChunk{}
|
|
gateway.add(Row{Record: Record{Time: now, Agent: "agy", Provider: "relay", Input: 7, Output: 3, Millis: 1000}}, "", 0, false)
|
|
agy.add(Row{Record: Record{Time: now.Add(-time.Minute), Agent: "agy", Session: "agy-transcript", Input: 7, Output: 3}, Source: "log"}, "", 0, false)
|
|
other.add(Row{Record: Record{Time: now, Agent: "claude", Session: "s", Input: 7, Output: 3}, Source: "log"}, "", 0, false)
|
|
agy.serial, other.serial = nextChunkSerial(), nextChunkSerial()
|
|
var opened []*rowChunk
|
|
matchOpening = func(c *rowChunk) { opened = append(opened, c) }
|
|
t.Cleanup(func() { matchOpening = nil })
|
|
got := matchedBlocks([]*rowChunk{gateway}, []*rowChunk{agy, other}, nil, nil, time.Time{}, false)
|
|
if !got[rowRef{agy, 0}] || len(got) != 1 {
|
|
t.Fatalf("matched %v; want the agy step alone", got)
|
|
}
|
|
if len(opened) != 1 || opened[0] != agy {
|
|
t.Fatalf("decoded %d chunks; want the agy transcript alone", len(opened))
|
|
}
|
|
}
|