diff --git a/internal/gui/sessions.go b/internal/gui/sessions.go index 21d242488..d04185322 100644 --- a/internal/gui/sessions.go +++ b/internal/gui/sessions.go @@ -709,7 +709,7 @@ func warmUp(steps ...func()) { for _, step := range steps { func() { coldTurns.turn <- struct{}{} - defer func() { <-coldTurns.turn }() + defer coldTurnDone() step() }() } @@ -728,6 +728,14 @@ var coldTurns = struct { over atomic.Bool }{turn: make(chan struct{}, 1)} +// coldTurnDone ends a turn. What the request matching kept for a next +// read goes with it: kept for scratchLinger, it sat under the next +// turn's read of the history. +func coldTurnDone() { + usage.ReleaseMatchScratch() + <-coldTurns.turn +} + // coldTurnMost is how long a page waits for its turn: a warm-up stuck on // one read never keeps the pages from answering. const coldTurnMost = 2 * time.Minute @@ -744,7 +752,7 @@ func coldTurn(h http.HandlerFunc) http.HandlerFunc { t := time.NewTimer(coldTurnMost) select { case coldTurns.turn <- struct{}{}: - defer func() { <-coldTurns.turn }() + defer coldTurnDone() case <-t.C: } t.Stop() diff --git a/internal/usage/block_price_test.go b/internal/usage/block_price_test.go index 7802acfe4..0919241fb 100644 --- a/internal/usage/block_price_test.go +++ b/internal/usage/block_price_test.go @@ -116,3 +116,43 @@ func TestPricedBlocksGoWithTheScratch(t *testing.T) { holding.release() ReleaseScratch() } + +// A turn of a cold start lets go of the matcher's scratch, so the next +// turn's read doesn't sit on it, and keeps the priced blocks the next +// turn's queries read again (#2278). +func TestMatchScratchGoesPricedBlocksStay(t *testing.T) { + t.Cleanup(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) + if keptMatch.Load() == nil || keptPicks.Load() == nil { + t.Fatal("nothing kept for the next call") + } + keepRows(new(rowBuffer)) + var raw rowChunk + for i := 0; i < logBlockRows; i++ { + raw.add(Row{Record: Record{Time: now, Provider: "openai", Model: "gpt-5", Input: 100 + i, Output: 10}}, "", int64(i), false) + } + packed := raw.pack() + meta := sha256.Sum256([]byte("turn")) + b := newBlockPricer(provider.Renames{}, nil, func(Record) *catalog.Price { return nil }) + b.share(meta) + c := b.block(packed) + b.release() + ReleaseMatchScratch() + if keptMatch.Load() != nil || keptPicks.Load() != nil || keptSeen.Load() != nil { + t.Fatal("the matcher's scratch was kept") + } + for i := range keptRows { + if keptRows[i].Load() != nil { + t.Fatal("a row buffer was kept") + } + } + next := newBlockPricer(provider.Renames{}, nil, func(Record) *catalog.Price { return nil }) + next.share(meta) + defer next.release() + if next.block(packed) != c { + t.Fatal("the priced block went with the scratch") + } +} diff --git a/internal/usage/request_page.go b/internal/usage/request_page.go index 3bf53d734..ac2d9dbf0 100644 --- a/internal/usage/request_page.go +++ b/internal/usage/request_page.go @@ -1788,6 +1788,14 @@ func ReleaseScratch() { releasePriced() } +// ReleaseMatchScratch is ReleaseScratch keeping the priced blocks. +func ReleaseMatchScratch() { + keptMatch.Store(nil) + keptPicks.Store(nil) + keptSeen.Store(nil) + releaseRows() +} + // scratchLinger is how long the matcher's scratch is kept once the last // pairing put it back. While an agent works every poll pairs again and // finds it kept; with nothing new to pair, a poll is answered from the