Files
magpie/internal/usage/request_disk_entry_test.go
yetone 32f5c33692 usage: a chunk without calls is read back from requests-v1.bin without a Latest, so a new version pairs its history on one start (#2278)
diskEntry wrote a chunk's Latest as c.Latest.UnixNano(). For a chunk
without calls Latest is the zero time, whose UnixNano is out of int64's
range and is written as another time; readDiskEntry read it back as a
call on 1969-12-31. heatSplit and effortSplit count a chunk with a zero
Latest as recent, but one read back so as settled, so the settled set
after a build (14426 chunks on the real history) differed from the one
after a read (15677). The key heat-v1.json and effort-v1.json were
written under on a new version's first start missed on its second, and
the whole history was paired again: two cold peaks, not one.

A zero Latest is now written as 0 and read back as the zero time.
Old data: requests-v1.bin is rewritten under the new binary (its key
holds requestDiskBinary()), so nothing old is read by the new reader.

Run:
- TestDiskEntryKeepsNoLatest round-trips a zero Latest and
  time.Unix(1790000100,5). Red on the old reader ("Latest 0001-01-01
  ... read back as 1969-12-31 16:00:00 -0800 PST"), green with the fix.
- Real history on mote (sandbox HOME, copy of the owner's sessions and
  requests): the first start wrote heat-v1.json and effort-v1.json, the
  second and third didn't rewrite them (both hit), and the heatmap's
  JSON had the same md5 on all three starts.
- Rebased onto 244d80e23; at the pushed tree on mote:
  go test -count=1 ./internal/usage ok (53.3s), gofmt clean,
  go vet -tags nogui ./internal/usage for windows ok (linux, darwin ok
  before the rebase, which touched no file of this commit).
Not tried: a full ./... suite.
2026-10-30 15:16:30 +08:00

99 lines
3.5 KiB
Go

package usage
import (
"bytes"
"testing"
"time"
"github.com/yetone/magpie/internal/sessions"
)
// What a write compares of an entry it wrote is the size, mtime and
// archive readDiskEntry reads of it, past any zones and actors, and an
// entry cut short anywhere is none. Each entry is made on the last one's
// bytes, as a write makes them.
func TestDiskEntryArchive(t *testing.T) {
shanghai, err := time.LoadLocation("Asia/Shanghai")
if err != nil {
t.Skip(err)
}
var buf []byte
for i, c := range []struct {
zones []*time.Location
actors []string
}{
{nil, nil},
{[]*time.Location{time.UTC, shanghai, time.FixedZone("X", 3600)}, []string{"cli", "exec", "a long actor name"}},
{[]*time.Location{time.Local}, nil},
} {
chunk := &rowChunk{
Source: sessions.CallSource{Path: "/s/rollout-" + string(rune('a'+i)) + ".jsonl", Key: "k", Agent: "codex",
Size: int64(1000 + i), Modified: time.Unix(1790000000+int64(i), 123456789)},
Count: 3, Latest: time.Unix(1790000100, 0), Locations: c.zones,
Archive: bytes.Repeat([]byte{byte(i + 1)}, 100+i),
}
for _, a := range c.actors {
chunk.codexActors.add(a)
}
b := diskEntry(buf, chunk)
if b == nil {
t.Fatalf("%d: no entry", i)
}
want, err := readDiskEntry(bytes.Clone(b))
if err != nil {
t.Fatalf("%d: %v", i, err)
}
size, modified, archive, ok := diskEntryArchive(b)
if !ok || size != want.Source.Size || modified != want.Source.Modified.UnixNano() || !bytes.Equal(archive, want.Archive) {
t.Fatalf("%d: %d %d %d bytes %v, want %d %d %d bytes", i, size, modified, len(archive), ok, want.Source.Size, want.Source.Modified.UnixNano(), len(want.Archive))
}
if !bytes.Equal(archive, chunk.Archive) {
t.Fatalf("%d: archive is not the chunk's", i)
}
for n := range len(b) {
if _, _, _, ok := diskEntryArchive(b[:n]); ok {
t.Fatalf("%d: cut to %d of %d bytes still read", i, n, len(b))
}
}
buf = b
}
}
// An entry made on bytes big enough allocates nothing, nor does reading
// back what a write compares: each grown from nil was ~300 MB of a cold
// start's first write (#2278).
func TestDiskEntryAllocatesOnce(t *testing.T) {
chunk := &rowChunk{
Source: sessions.CallSource{Path: "/s/rollout.jsonl", Key: "k", Agent: "codex", Size: 1, Modified: time.Unix(1790000000, 0)},
Count: 1, Latest: time.Unix(1790000000, 0), Locations: []*time.Location{time.UTC},
Archive: bytes.Repeat([]byte{7}, 64<<10),
}
chunk.codexActors.add("cli")
buf := diskEntry(nil, chunk)
if n := testing.AllocsPerRun(20, func() { buf = diskEntry(buf, chunk) }); n != 0 {
t.Errorf("an entry on the last one's bytes allocated %v times", n)
}
if n := testing.AllocsPerRun(20, func() { diskEntryArchive(buf) }); n != 0 {
t.Errorf("reading an entry's archive allocated %v times", n)
}
}
// A chunk without calls reads back without a Latest, as it was built: read
// as a call long ago, it was settled after a read and not after a build,
// and the heatmap's and effort's kept days were counted again (#2278).
func TestDiskEntryKeepsNoLatest(t *testing.T) {
for _, latest := range []time.Time{{}, time.Unix(1790000100, 5)} {
chunk := &rowChunk{
Source: sessions.CallSource{Path: "/s/rollout.jsonl", Key: "k", Agent: "codex", Size: 1, Modified: time.Unix(1790000000, 0)},
Latest: latest, Archive: []byte{1},
}
got, err := readDiskEntry(diskEntry(nil, chunk))
if err != nil {
t.Fatal(err)
}
if got.Latest.IsZero() != latest.IsZero() || !got.Latest.Equal(latest) {
t.Errorf("Latest %v read back as %v", latest, got.Latest)
}
}
}