With nothing new to pair, every poll of the window is answered from the pairings kept (pairMemo) and pairs nothing, so the scratch of one pairing of the whole history (keptMatch's groupOf/calls/ends/byID maps, keptPicks' picks and slab) sat unused while the window polled, until pageRest's ReleaseScratch 30s after polling stopped. On the real 65 GB history (52 GB Codex, 13 GB Claude) that was ~120 MB of the ~270 MB live heap over 60s of polls. Now each release of either scratch (re)arms one timer, and scratchLinger (30s) after the last pairing put it back both are let go. While an agent works, every poll pairs again inside 30s and finds it kept, as before. Run at this head (origin/main430005726+ this commit), on mote: - go test -count=1 -timeout 45m ./internal/usage/: ok 19.6s. - TestPairScratchGoesWhenUnused broken by hand (both lingerScratch calls removed): FAIL "the scratch was kept with no pairing using it" at 30.55s; green with them. - go test -count=1 -timeout 45m ./internal/gui/: one red, TestMainWindowCorners/macOS_26_SDK, which is #3949 (macOS 27, open, untouched by this change); everything else ok. - GOOS=linux/windows/darwin go vet -tags nogui ./internal/usage/ ./internal/gui/: clean. gofmt -l: clean. - A/B on the real full history, KEEP=1 warm start, 10 endpoints polled every 5s for 120s (fullwarmh.sh), baseline430005726vs this commit, back to back on mote at load ~45: footprint 60s/100s/120s: 507/506/500 MB -> 384/389/382 MB footprint peak: 602 MB -> 567 MB CPU: 29.9s -> 30.6s (same within load noise); poll avg 0.172s -> 0.159s A first round at load ~80 gave 442/437/434 -> 347/342/355 MB. Rebased ontoeed140c4d(no internal/usage changes since430005726); at the rebased head go test ./internal/usage/ ok 22.5s, linux vet clean. Not tried: the startup peak (~4s, first poll burst during warm-up) is the pricedRows pool and the chunks of a whole-history build; this commit doesn't change it.
122 lines
4.3 KiB
Go
122 lines
4.3 KiB
Go
package usage
|
|
|
|
import (
|
|
"fmt"
|
|
"maps"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/sessions"
|
|
)
|
|
|
|
// The matcher keeps its maps and slices from one call for the next
|
|
// (#2278): a call after another, on other chunks or on the same ones,
|
|
// matches what a first call does.
|
|
func TestPairScratchKeepsNothingBetweenCalls(t *testing.T) {
|
|
now := time.Date(2026, 10, 1, 13, 0, 0, 0, time.UTC)
|
|
xg, xl := pairFixture(40, 30)
|
|
yg, yl := pairFixture(12, 9)
|
|
// what was matched, by chunk serial and row: Codex chunks are copied
|
|
// for each call
|
|
matched := func(g, l []*rowChunk) map[[2]uint64]bool {
|
|
out := map[[2]uint64]bool{}
|
|
for r, v := range pairRows(All, now, g, l, nil, nil).matched {
|
|
out[[2]uint64{r.Chunk.serial, uint64(r.Index)}] = v
|
|
}
|
|
return out
|
|
}
|
|
ReleaseScratch()
|
|
fresh := matched(yg, yl)
|
|
if len(fresh) != 12*9 {
|
|
t.Fatalf("a first call matched %d of %d", len(fresh), 12*9)
|
|
}
|
|
matched(xg, xl)
|
|
for round := range 2 {
|
|
if got := matched(yg, yl); !maps.Equal(got, fresh) {
|
|
t.Fatalf("round %d after another call matched %d, a first call %d", round, len(got), len(fresh))
|
|
}
|
|
}
|
|
if keptMatch.Load() == nil || keptPicks.Load() == nil {
|
|
t.Fatal("nothing kept for the next call")
|
|
}
|
|
ReleaseScratch()
|
|
if keptMatch.Load() != nil || keptPicks.Load() != nil {
|
|
t.Fatal("ReleaseScratch kept the scratch")
|
|
}
|
|
}
|
|
|
|
// What one call reserved, used or looked up by id never holds in the next
|
|
// call's matching of the same calls.
|
|
func TestMatchScratchForgetsTheLastCall(t *testing.T) {
|
|
now := time.Date(2026, 10, 1, 13, 0, 0, 0, time.UTC)
|
|
call := func(id string, i int) Record {
|
|
return Record{Time: now.Add(-time.Duration(i) * time.Minute), Agent: "claude", Provider: "relay", Session: "s", Input: 100 + i, Output: 5, Millis: 1000, RequestID: id}
|
|
}
|
|
chunk := func(source bool, rs ...Record) *rowChunk {
|
|
c := &rowChunk{serial: nextChunkSerial()}
|
|
for i, r := range rs {
|
|
row := Row{Record: r}
|
|
if source {
|
|
c.Source, row.Source = sessions.CallSource{Path: fmt.Sprint("/local/", c.serial)}, "log"
|
|
}
|
|
c.add(row, "", int64(i), false)
|
|
}
|
|
return c
|
|
}
|
|
// the gateway's call req-a, and a local call that names it or one that
|
|
// names no id, each the same call by session and tokens
|
|
gateway := chunk(false, call("req-a", 0))
|
|
named, unnamed := chunk(true, call("req-a", 0)), chunk(true, call("", 0))
|
|
// another gateway, where req-a is the sixth id
|
|
other := chunk(false, call("req-0", 9), call("req-1", 8), call("req-2", 7), call("req-3", 6), call("req-4", 5), call("req-a", 4))
|
|
match := func(gateway, local *rowChunk, pairs map[rowRef]rowRef) bool {
|
|
return matchedBlocks([]*rowChunk{gateway}, []*rowChunk{local}, nil, pairs, time.Time{}, false)[rowRef{local, 0}]
|
|
}
|
|
ReleaseScratch()
|
|
if !match(gateway, unnamed, nil) || !match(gateway, named, nil) {
|
|
t.Fatal("a first call left a local call unmatched")
|
|
}
|
|
// named's id used the gateway's call: unnamed still matches it after
|
|
if !match(gateway, unnamed, nil) {
|
|
t.Fatal("the last call's id match held the gateway's call")
|
|
}
|
|
// a response pair reserved the gateway's call
|
|
match(gateway, unnamed, map[rowRef]rowRef{{gateway, 0}: {named, 0}})
|
|
if !match(gateway, unnamed, nil) {
|
|
t.Fatal("the last call's response pair held the gateway's call")
|
|
}
|
|
// req-a was the other gateway's sixth id
|
|
match(other, chunk(true, call("req-a", 4)), nil)
|
|
if !match(gateway, named, nil) {
|
|
t.Fatal("the last call's ids were looked up")
|
|
}
|
|
}
|
|
|
|
// The scratch a pairing kept goes once no pairing has used it for
|
|
// scratchLinger (#2278), and a pairing before then finds it kept.
|
|
func TestPairScratchGoesWhenUnused(t *testing.T) {
|
|
was := scratchLinger
|
|
scratchLinger = 2 * time.Second
|
|
t.Cleanup(func() {
|
|
scratchLinger = was
|
|
ReleaseScratch()
|
|
})
|
|
now := time.Date(2026, 10, 1, 13, 0, 0, 0, time.UTC)
|
|
g, l := pairFixture(12, 9)
|
|
ReleaseScratch()
|
|
pairRows(All, now, g, l, nil, nil)
|
|
kept := keptMatch.Load()
|
|
if kept == nil || keptPicks.Load() == nil {
|
|
t.Fatal("nothing kept for the next call")
|
|
}
|
|
pairRows(All, now, g, l, nil, nil)
|
|
if keptMatch.Load() != kept {
|
|
t.Fatal("a pairing right after made scratch of its own")
|
|
}
|
|
for deadline := time.Now().Add(30 * time.Second); keptMatch.Load() != nil || keptPicks.Load() != nil; time.Sleep(10 * time.Millisecond) {
|
|
if time.Now().After(deadline) {
|
|
t.Fatal("the scratch was kept with no pairing using it")
|
|
}
|
|
}
|
|
}
|