Files
magpie/internal/usage/growing_rewrite_test.go
T
yetone 6359b2c27c usage: Vias hands every caller the one copy it keeps, and a session's list is copied only when read (#2776)
Vias cloned every session's list for each ask, from a cache built for
exactly this. The Sessions list, the manage pages and a session's
detail each asked, and read the lists of the sessions they show, or of
one. Over ~720k calls one ask was ~66 MB.

Vias now answers a ViaIndex over the kept map. Of(key) is a session's
list as a copy of its own, so a caller still can't change the kept
one; Len is how many sessions have one. The three GUI callers and the
tests read through Of.

TestViasHandOutTheKeptCopy writes 20,000 sessions and asks 20 times,
reading one list each: 2,590 bytes an ask. With the clone of every
list put back by hand (under ViaIndex): red, 3,817,492 bytes an ask.
It also changes a list read through Of and checks the next ask still
says 1 call.

Full suite on this series' final tree (aa678b482 before this note was
added), rebased on origin/main f76201a84: go test -p 4 ./... -timeout
45m, exit 0, 64 packages ok and none failed (gateway 1783 s, gui 361 s),
41 minutes at load ~190-200. gofmt -l: no files. go vet: darwin, linux
(-tags nogui) and windows exit 0. The GUI suite wasn't run: no file
under internal/gui/assets changed.
2026-10-19 14:34:18 +08:00

87 lines
2.7 KiB
Go

package usage
import (
"bytes"
"encoding/json"
"os"
"strings"
"testing"
"time"
)
func TestLogGrowingRewriteThenAppend(t *testing.T) {
for _, writer := range []string{"this process", "another writer"} {
t.Run(writer, func(t *testing.T) {
pageHome(t)
now := holdClock(t, time.Date(2026, 9, 30, 12, 0, 0, 0, time.Local))
historyLog(t, logBlockRows*2+10)
for _, period := range []Period{Today, Week, Month, All} {
Summarize(period)
QueryPage(period, Filter{}, 0, 100)
}
Vias(time.Time{})
old := readLogSnapshot()
data, err := os.ReadFile(Path())
if err != nil {
t.Fatal(err)
}
_, rest, ok := bytes.Cut(data, []byte{'\n'})
if !ok {
t.Fatal("fixture has no complete first line")
}
// Change an early record and grow the same inode. Treating this as an
// append would reuse stale blocks and seek into the rewritten prefix.
edited := Record{Time: now, Agent: "codex", Provider: "relay", Model: "m", Session: "edited-prefix", Input: 73, Status: 200, RequestID: strings.Repeat("r", 2048)}
first, err := json.Marshal(edited)
if err != nil {
t.Fatal(err)
}
rewritten := append(append(first, '\n'), rest...)
if len(rewritten) <= len(data) {
t.Fatal("rewrite must grow the log")
}
if err := os.WriteFile(Path(), rewritten, 0600); err != nil {
t.Fatal(err)
}
added := Record{Time: now, Agent: "codex", Provider: "relay", Model: "m", Session: "appended-tail", Input: 19, Status: 200}
if writer == "this process" {
Append(added)
} else {
f, err := os.OpenFile(Path(), os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
t.Fatal(err)
}
err = json.NewEncoder(f).Encode(added)
closeErr := f.Close()
if err != nil {
t.Fatal(err)
}
if closeErr != nil {
t.Fatal(closeErr)
}
}
cold := Load(time.Time{})
if len(cold) != logBlockRows*2+11 {
t.Fatalf("fixture rows: %d", len(cold))
}
for _, period := range []Period{Today, Week, Month, All} {
got, want := Summarize(period), summarize(period, now, cold)
if got.Totals != want.Totals {
t.Fatalf("%s summary stale: got %+v want %+v", period, got.Totals, want.Totals)
}
equalPage(t, QueryPage(period, Filter{}, 0, 100), pageFromLedger(period, Filter{}, 0, 100, LedgerOf(period, Filter{})))
}
if next := readLogSnapshot(); next.blocks[0] == old.blocks[0] {
t.Fatal("growing rewrite reused the old prefix")
}
vias := Vias(time.Time{})
for session, input := range map[string]int{"edited-prefix": 73, "appended-tail": 19} {
v := vias.Of("codex|" + session)
if len(v) != 1 || v[0].Calls != 1 || v[0].Tokens != input {
t.Fatalf("%s vias stale: %+v", session, v)
}
}
})
}
}