Files
magpie/internal/usage/request_pairs_test.go
T
yetone 4982ded8a6 usage: a turned page reuses the last pairing while its chunks are the same
A Requests page paired every Codex session file's calls with the
gateway's again on each query: clone, fold forks, match response ids.
That unpacked every session chunk three times and was about half of a
warm page (#3493). pairRowsMemo names the chunks of a pairing by their
serials (the probe chunk, which has none, by its rows' log offsets and
times) and keeps what the pairing found: the rows left out (skip,
matched), the actors it changed and each chunk's compactions. The next
query over the same chunks and window applies that to fresh clones
instead of pairing again. A changed file gets a new serial, a new
window or period misses, and adopt drops the memos with the parts.

Run at a6399dd49 (= origin/main, fetched):
- TestRequestPairsReusedAcrossPages (new): a gateway call paired with
  its rollout, a fork copying another file's call and a Claude call,
  every page of All/Today/Week at limit 1 and the whole page against
  pageFromLedger. Broken by hand three ways, each red on the memo hit:
  apply without matched ("all page 1: 4 rows, 157 input"), without skip
  ("4 rows, 127 input"), without compactions (page differs).
- Real data, same process (scratch test, not committed), fresh pairRows
  vs the memo applied over a second read: sandbox with the live
  rollouts (symlinked, read only) and a copy of usage.jsonl: All 15607
  chunks, 4801 skip, 1469 matched, 2 actor sets; Today 58 matched; 7d
  1075 matched. Actors, compactions, skip, matched equal, and pages
  equal for 3 filters per period. Without the gateway log: All 15600
  chunks, 4801 skip, all periods equal.
- Timing on that sandbox (load 110-143), page turns after the first:
  v0.1.1449 mean 407 / 460 ms, this change 154 / 145 ms.
- go test -count=1 -timeout 45m ./internal/usage/... ./internal/usageapi/...
  ./internal/sessions/...: usageapi and sessions ok; usage red only on
  TestUsageReadAfterAppendHashesNothingOnAFineClock/whole-second_stamp,
  which is #3511 (taken by another session). At -count=20, load 154,
  it fails 3/20 here and 2/20 at the base 1313b047f; it builds no page.
- gofmt -l clean; GOOS=linux and GOOS=windows go vet -tags nogui ok.

Not tried: the GUI against a live gateway; the page reads the same
QueryPage the tests and the real-data run used.
2026-10-25 05:18:41 +08:00

45 lines
2.4 KiB
Go

package usage
import (
"path/filepath"
"testing"
"time"
"github.com/yetone/magpie/internal/sessions"
)
// A page turned over the same chunks reuses the last pairing. It must give
// the page a fresh pairing gives: a gateway call its rollout stands for is
// counted once, and so is a call a forked file copies, on every page
// and in the sums.
func TestRequestPairsReusedAcrossPages(t *testing.T) {
pageHome(t)
now := holdClock(t, time.Date(2026, 9, 30, 12, 0, 0, 0, time.Local))
dir := filepath.Join(sessions.CodexDir(), "sessions", "2026", "09", "30")
writeCodexIdentity(t, "main-a", "native-main", "vscode")
writeCodexCall(t, filepath.Join(dir, "rollout-2026-09-30T10-00-00-main-a.jsonl"), "main-a", "native-main", `"vscode"`, "resp-paired", "m", "low", now.Add(-time.Minute), 60, 6)
writeCodexCall(t, filepath.Join(dir, "rollout-2026-09-30T10-00-01-solo-b.jsonl"), "solo-b", "native-solo", `"vscode"`, "resp-solo", "m", "low", now.Add(-2*time.Minute), 30, 3)
// A fork copies solo-b's call; it is counted once.
writeCodexCall(t, filepath.Join(dir, "rollout-2026-09-30T10-00-02-fork-c.jsonl"), "fork-c", "native-solo", `"vscode"`, "resp-solo", "m", "low", now.Add(-2*time.Minute), 30, 3)
Append(Record{Time: now.Add(-time.Minute), Agent: "codex", Provider: "openai", Model: "m", Input: 60, Output: 6, Status: 200, ResponseID: "resp-paired"})
Append(Record{Time: now.Add(-3 * time.Hour), Agent: "claude", Provider: "openai", Model: "m", Input: 7, Output: 1, Status: 200})
for _, p := range []Period{All, Today, Week} {
want := pageFromLedger(p, Filter{}, 0, 100, LedgerOf(p, Filter{}))
if want.Total != 3 || want.Sum.Input != 97 {
t.Fatalf("%v: fixture pairs to %d rows, %d input", p, want.Total, want.Sum.Input)
}
for offset := 0; offset < 3; offset++ {
got := QueryPage(p, Filter{}, offset, 1)
if got.Total != 3 || got.Sum.Input != 97 || len(got.Rows) != 1 {
t.Fatalf("%v page %d: %d rows, %d input, %d shown", p, offset, got.Total, got.Sum.Input, len(got.Rows))
}
if got.Rows[0].ResponseID != want.Rows[offset].ResponseID || got.Rows[0].Input != want.Rows[offset].Input {
t.Fatalf("%v page %d: %+v, want %+v", p, offset, got.Rows[0].Record, want.Rows[offset].Record)
}
}
equalPage(t, QueryPage(p, Filter{}, 0, 100), want)
equalPage(t, QueryPage(p, Filter{Query: "main-a"}, 0, 100), pageFromLedger(p, Filter{Query: "main-a"}, 0, 100, LedgerOf(p, Filter{Query: "main-a"})))
}
}