With no sums on disk (a first install, or a version that counts rows
otherwise), the page's cells, the heatmap's days and Codex's effort
before the cut were built from one pairing of the whole history. Every
gateway block was priced, every session row's message id went into one
set, and every call into one match map. On the owner's history that
was ~212 MB of the warm-up's 341 MB in use and the cold start's peak.
coldSpans builds them as a day's start advances them (pageAdvance),
14 days at a time. A span pairs the chunks with a row from heatReach
before it to heatReach after. Each chunk's oldest row is kept by its
serial. A gateway block not priced yet is priced by a pricer of the
span alone, and its rows go back to the pool after the span. The
recent days are then paired as on any kept start.
The page's cells come out in another order (by span, then chunk), so
TestSettledCountedAsVersioned now sorts them before it hashes. The
sorted hash is the same for a pairing at once and in spans, so
settledCountVersion stays. Tests that count a pairing of every chunk
use pairAtOnce (coldSpan 0).
Run on mote at the pushed head (rebased on b62fa332f): gofmt -l
clean; go vet ./internal/usage for darwin, linux and windows ok;
go test -count=1 ./internal/usage ok (20.0s); go build . ok.
Broken by hand: TestFirstBuildPairsSpans goes red when a span pairs
only its own days (lo, hi := from, to): the effort differs.
Bench, real history (a year), cold first start, against v0.1.2256,
two rounds each, on mote at load ~22:
no GUI polls: footprint peak 451, 407 MB (2256: 493, 484);
live heap max 318, 289 MB (2256: 367, 362)
GUI polls: footprint peak 483, 410 MB (2256: 534, 532);
live heap max 316, 290 MB (2256: 395, 406)
The heatmap's answers are byte-equal to 2256's. The request page's
differ only in float digits from the summation order (232 floats,
largest relative difference 6.5e-14); every other value is equal.
Not tried: a start on Windows or Linux.
83 lines
4.3 KiB
Go
83 lines
4.3 KiB
Go
package usage
|
|
|
|
import (
|
|
"path/filepath"
|
|
"reflect"
|
|
"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"})))
|
|
}
|
|
}
|
|
|
|
// Codex's effort over a period whose page was just built pairs no row
|
|
// again: the warm-up's page of All paired every session file's calls, and
|
|
// the effort read after it paired them all over (#3493). It is the effort
|
|
// a pairing of its own gives.
|
|
func TestCodexEffortReusesPagePairing(t *testing.T) {
|
|
pairAtOnce(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", "high", now.Add(-2*time.Minute), 30, 3)
|
|
writeCodexCall(t, filepath.Join(dir, "rollout-2026-09-30T10-00-02-fork-c.jsonl"), "fork-c", "native-solo", `"vscode"`, "resp-solo", "m", "high", 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"})
|
|
pairings := 0
|
|
pairing = func() { pairings++ }
|
|
t.Cleanup(func() { pairing = nil })
|
|
|
|
QueryPage(All, Filter{}, 0, 50)
|
|
if pairings != 1 {
|
|
t.Fatalf("the page paired %d times, want 1", pairings)
|
|
}
|
|
got := CodexModelEffort(All)
|
|
if pairings != 1 {
|
|
t.Fatalf("the effort after the page paired again (%d pairings)", pairings)
|
|
}
|
|
if rows := got["main-a"]; len(rows) != 1 || rows[0].Input != 60 {
|
|
t.Fatalf("effort = %+v", got)
|
|
}
|
|
requestCache.Lock()
|
|
requestCache.pairs = nil
|
|
requestCache.Unlock()
|
|
if want := codexEffortOfIndex(readRequestState(now), All, now); pairings != 2 || !reflect.DeepEqual(got, want) {
|
|
t.Fatalf("effort through the page's pairing %+v, a pairing of its own %+v (%d pairings)", got, want, pairings)
|
|
}
|
|
}
|