Files
magpie/internal/usage/request_cells_test.go
T
yetone a6399dd494 usage: a Requests page sums each chunk once and reuses it while the chunk is the same
Turning a page, or the 15s refresh of the open page, built every sum,
chart and share from every row of history again. On the owner's real
history that was 1.15-1.36s a page (load 170) after the chunks were
already cached.

Each chunk's rows are now folded into cells: one per provider, agent,
model, kind, caller, account, computer, source, filter bits and chart
bucket, with its totals. A page merges the cells. The cells of a chunk
are kept per (period, filter, since, until, zone), up to 4 such sets,
keyed by the chunk's serial (a new one every time a chunk is read or
frozen) and a signature of what pairing gave it: rows skipped or
matched, Codex actors and their identities (with the parent's role), and
compactions. A changed file, a gateway tail block or an identity change
from another file sums that chunk again. The newest rows are taken from
chunks in Latest order, stopping once the heap holds rows newer than
the next chunk. Cell strings are unique handles, so the cache holds no
chunk's string block. Totals and Part sums are linear, so merged cells
equal the row-by-row sums; date-mode buckets store the day number and
are placed against the chart's first day at merge time.

What was run (macOS, arm64):
- Real history (sbmem sandbox HOME, read-only links to the owner's
  data), zzbench dump of 9 cases x 2 pages (offset 0/50 after a warm
  7/50 call, and 120/30) with the base 1313b047f and this change: 0
  differences over 18 pages (floats at rel 1e-9).
- zzbench timing at load 170: warm pages 1.15s / 1.36s mean -> 0.45s /
  0.41s; first page 3.1s / 2.8s -> 2.4s / 2.6s.
- New TestRequestCellsFollowIdentityAndFiles: a child chunk whose
  parent's file changes (its root moves to top-z) without the child
  being read again, a changed child file, a gateway append, compared
  to pageFromLedger for All and Week. Broken by hand: reusing a part
  whatever its signature turns it red ("a cached part kept the old
  root: 0 rows, 0 input"). Passes in TZ=UTC too.
- go test -count=1 -timeout 45m ./internal/usage/... ./internal/usageapi/...
  ./internal/sessions/...: usageapi and sessions ok; usage red only on
  TestUsageReadAfterAppendHashesNothingOnAFineClock/whole-second_stamp,
  #3511 (taken by another session). -count=10 of it: 2 FAIL on this
  change and 2 FAIL on the base 1313b047f, at load ~100-145. Every
  other usage test passes.
- gofmt -l clean; GOOS=linux and GOOS=windows go vet -tags nogui
  ./internal/usage/.

Not done here: the pairing setup (unpacking unchanged chunks for
visibleLocal, matchedBlocks and foldCodexRows) is now most of a warm
page, ~0.18s each; it is next.
2026-10-25 05:02:18 +08:00

69 lines
3.0 KiB
Go

package usage
import (
"os"
"path/filepath"
"testing"
"time"
"github.com/yetone/magpie/internal/sessions"
)
// A page's sums are kept per chunk and reused while the chunk's rows and
// what pairing gave them are the same. A changed identity or file must be
// summed again, not read from the last page.
func TestRequestCellsFollowIdentityAndFiles(t *testing.T) {
pageHome(t)
now := holdClock(t, time.Date(2026, 9, 30, 12, 0, 0, 0, time.Local))
writeCodexIdentity(t, "main-a", "native-main", "vscode")
childPath := filepath.Join(sessions.CodexDir(), "sessions", "2026", "09", "30", "rollout-2026-09-30T10-00-00-child-b.jsonl")
writeCodexCall(t, childPath, "child-b", "native-main", `{"subagent":{"thread_spawn":{"parent_thread_id":"main-a"}}}`, "resp-local", "m", "low", now.Add(-time.Minute), 40, 4)
Append(Record{Time: now.Add(-2 * time.Hour), Agent: "claude", Provider: "openai", Model: "m", Input: 7, Output: 1, Status: 200})
sums := func(f Filter) (int, int) {
page := QueryPage(All, f, 0, 100)
return page.Total, page.Sum.Input
}
for _, offset := range []int{0, 1, 0} {
QueryPage(All, Filter{}, offset, 1)
}
if n, in := sums(Filter{Query: "main-a"}); n != 1 || in != 40 {
t.Fatalf("subagent's call under its root: %d rows, %d input", n, in)
}
// Only the parent's file changes: the child's chunk is the same, and
// its root is now the parent's parent.
for _, f := range []Filter{{Query: "top-z"}, {Query: "main-a"}} {
QueryPage(All, f, 0, 100)
}
writeCodexIdentity(t, "main-a", "native-main", map[string]any{"subagent": map[string]any{"thread_spawn": map[string]any{"parent_thread_id": "top-z"}}})
writeCodexIdentity(t, "top-z", "native-main", "vscode")
if n, in := sums(Filter{Query: "top-z"}); n != 1 || in != 40 {
t.Fatalf("a cached part kept the old root: %d rows, %d input", n, in)
}
// The child is its own main thread now.
writeCodexCall(t, childPath, "child-b", "native-main", `"vscode"`, "resp-local", "m", "low", now.Add(-time.Minute), 40, 4)
if err := os.Chtimes(childPath, now, now); err != nil {
t.Fatal(err)
}
if n, _ := sums(Filter{Query: "main-a"}); n != 0 {
t.Fatalf("a changed file kept its old root: %d rows", n)
}
if n, in := sums(Filter{Query: "child-b"}); n != 1 || in != 40 {
t.Fatalf("new main thread: %d rows, %d input", n, in)
}
// The file's call changes.
writeCodexCall(t, childPath, "child-b", "native-main", `"vscode"`, "resp-local", "m", "low", now.Add(-time.Minute), 90, 4)
if err := os.Chtimes(childPath, now.Add(time.Second), now.Add(time.Second)); err != nil {
t.Fatal(err)
}
if n, in := sums(Filter{}); n != 2 || in != 97 {
t.Fatalf("changed file: %d rows, %d input", n, in)
}
Append(Record{Time: now.Add(-time.Hour), Agent: "claude", Provider: "openai", Model: "m", Input: 3, Output: 1, Status: 200})
equalPage(t, QueryPage(All, Filter{}, 0, 100), pageFromLedger(All, Filter{}, 0, 100, LedgerOf(All, Filter{})))
equalPage(t, QueryPage(Week, Filter{}, 0, 100), pageFromLedger(Week, Filter{}, 0, 100, LedgerOf(Week, Filter{})))
}