Files
magpie/internal/sessions/state_more_test.go
T
yetone b8270850df sessions: a kept parse's rarely set fields live in a StateMore made only when one is set (#2278)
Each of the ~18k files the Sessions cache keeps had a 760-byte state, and
~330 bytes of it were fields almost no file sets: the Codex spawn and agent
maps, Named/Custom/First, the dsh and Claude conversation lists, Root/Leaf,
Compacts, and the six per-agent tails (Grok, Droid, Kimi, Agy, Dsh, Cline).
On the owner's real cache (18,331 files) Root and Leaf are set in 150,
Named in 117, Compacts in 78, First in 35. They move to an embedded
*StateMore, nil unless something is in it: reads go through more() (a
shared empty one when nil), writes through own(). The JSON is the same
(embedded fields are inlined), so the cache version stays.

- parseWith drops an extra that only had empty values written to it, and
  share() drops one the decode made empty.
- clone() deep-copies the extra, so growing a clone leaves the kept state
  alone. Copies that never write (withoutRevisions, the Agent rename)
  share it, as they shared the maps before.

Run on mote at this tree:
- gofmt -l internal/sessions: clean. go vet ./internal/sessions/ on
  darwin, GOOS=linux and GOOS=windows: ok.
- go test -count=1 ./internal/sessions/: ok (26s).
- Every selector was checked by renaming the embedding to a named field
  and compiling with -gcflags=-e until only the struct literals were left,
  so no field is reached through a nil *StateMore.
- New tests in state_more_test.go: zero() knows every field (reflect), a
  read makes no extra and a write never reaches the shared empty one, a
  clone owns its extra, and a cache round trip keeps no empty extra.
  Broken by hand: dropping Cline from zero() and sharing the extra in
  clone() each turn their test red; restored.
- Real-account idle heap (copy of the owner's config and 24.6MB sessions
  cache, gateway idle 180s, heap after GC): decodeCache's retained heap
  35.0MB -> 22.5MB, reflect.unsafe_New 20.0MB -> 10.5MB. The total
  (128.7MB -> 133.6MB) moves by more than that between runs of one binary
  (118-140MB), from late passes that happened to be live.

Not tried: the full ./... suite (state is unexported; only internal/sessions
uses it, and the whole binary builds).
2026-11-09 18:08:28 +08:00

104 lines
3.7 KiB
Go

package sessions
import (
"bytes"
"reflect"
"testing"
"time"
)
// TestStateMoreZeroKnowsEveryField: a StateMore with any one field set is
// not zero, so a parse or a decode never drops what it holds; a field
// added to StateMore and not to zero() fails here.
func TestStateMoreZeroKnowsEveryField(t *testing.T) {
if !(*StateMore)(nil).zero() || !(&StateMore{}).zero() {
t.Fatal("an empty StateMore isn't zero")
}
typ := reflect.TypeFor[StateMore]()
for i := range typ.NumField() {
var m StateMore
f := reflect.ValueOf(&m).Elem().Field(i)
switch f.Kind() {
case reflect.String:
f.SetString("x")
case reflect.Bool:
f.SetBool(true)
case reflect.Slice:
f.Set(reflect.MakeSlice(f.Type(), 1, 1))
case reflect.Map:
f.Set(reflect.MakeMap(f.Type()))
f.SetMapIndex(reflect.New(f.Type().Key()).Elem(), reflect.New(f.Type().Elem()).Elem())
case reflect.Pointer:
f.Set(reflect.New(f.Type().Elem()))
case reflect.Struct:
if f.Type() != reflect.TypeFor[time.Time]() {
t.Fatalf("%s: a struct field zero() can't be checked for", typ.Field(i).Name)
}
f.Set(reflect.ValueOf(time.Unix(1, 0)))
default:
t.Fatalf("%s: kind %s not set by this test", typ.Field(i).Name, f.Kind())
}
if m.zero() || moreOf(m) == nil {
t.Errorf("%s set, and zero() says the StateMore is empty", typ.Field(i).Name)
}
}
}
// TestStateMoreReadsLeaveTheSharedEmptyOne: reading a state with no extra
// makes none and finds every field empty, and a write makes its own.
func TestStateMoreReadsLeaveTheSharedEmptyOne(t *testing.T) {
s := &state{}
if s.more().Named != "" || s.more().Agy != nil || s.StateMore != nil {
t.Fatal("a read of an empty state made or found an extra")
}
s.own().Named = "n"
if s.StateMore == nil || s.more().Named != "n" || !noMore.zero() || (&state{}).more().Named != "" {
t.Fatal("a write through own() reached the shared empty StateMore")
}
}
// TestStateCloneOwnsItsMore: a clone's extra is its own, so growing the
// clone (as every parse after the first does) leaves the kept state alone.
func TestStateCloneOwnsItsMore(t *testing.T) {
s := &state{StateMore: &StateMore{Named: "a", Convs: []string{"c1"}, CodexSpawnNames: map[string]string{"k": "v"}, Compacts: []string{"u"}}}
c := s.clone()
if c.StateMore == s.StateMore {
t.Fatal("the clone shares the kept state's StateMore")
}
c.own().Named = "b"
c.own().Convs[0] = "c2"
c.own().CodexSpawnNames["k"] = "w"
c.own().Compacts = append(c.more().Compacts, "u2")
if s.Named != "a" || s.Convs[0] != "c1" || s.CodexSpawnNames["k"] != "v" || len(s.Compacts) != 1 {
t.Fatalf("a change to the clone reached the kept state: %+v", *s.StateMore)
}
if (&state{}).clone().StateMore != nil {
t.Fatal("a clone of a state with no extra made one")
}
}
// TestCacheKeepsNoEmptyMore: a cache written with states that have no
// extra, or an empty one, reads back with none, and one with an extra
// reads back with it.
func TestCacheKeepsNoEmptyMore(t *testing.T) {
c := cacheFile{Version: cacheVersion, Zone: "UTC", Parsers: parseVersions, Files: map[string]*state{
"/a": {Agent: "claude", Size: 1, Title: "t"},
"/b": {Agent: "claude", Size: 2, StateMore: &StateMore{}},
"/c": {Agent: "claude", Size: 3, StateMore: &StateMore{Named: "named", Root: "r"}},
}}
var b bytes.Buffer
if err := encodeCache(&b, c); err != nil {
t.Fatal(err)
}
files, ok := decodeCache(b.Bytes(), "UTC")
if !ok || len(files) != 3 {
t.Fatalf("decode: %v %d", ok, len(files))
}
if files["/a"].StateMore != nil || files["/b"].StateMore != nil {
t.Fatal("an empty extra was kept by the decode")
}
if m := files["/c"].StateMore; m == nil || m.Named != "named" || m.Root != "r" {
t.Fatalf("an extra was lost: %+v", m)
}
}