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).
73 lines
2.7 KiB
Go
73 lines
2.7 KiB
Go
package sessions
|
|
|
|
import (
|
|
"encoding/json"
|
|
"slices"
|
|
"strconv"
|
|
"testing"
|
|
)
|
|
|
|
func dshUserTurn(seq int, text string) string {
|
|
return `{"type":"user/message","seq":` + strconv.Itoa(seq) + `,"time":1000,"data":{"source":{"kind":"user"},"content":[{"type":"text","text":"` + text + `"}]}}`
|
|
}
|
|
|
|
func dshTurnStart(seq, turn int) string {
|
|
return `{"type":"turn/start","seq":` + strconv.Itoa(seq) + `,"time":1000,"data":{"turn":` + strconv.Itoa(turn) + `}}`
|
|
}
|
|
|
|
// dshTurnID is the id a session keeps of a message of its turn: the one
|
|
// spelling the gateway writes a request's message in (dshLastConvs).
|
|
func dshTurnID(text string) string {
|
|
return dshConvs(json.RawMessage(`[{"type":"text","text":"` + text + `"}]`))[0]
|
|
}
|
|
|
|
// A session's TurnConvs are the ids of its newest turn's user messages: a
|
|
// request still answering carries them and has no reply id yet.
|
|
func TestDshTurnConvsAreTheNewestTurns(t *testing.T) {
|
|
head := `{"type":"session","version":4,"id":"session-one","cwd":"/w","createdAt":1000,"delegationDepth":0,"isSeeded":false}`
|
|
s := dshSessionFile(t, []string{
|
|
head,
|
|
dshTurnStart(1, 1),
|
|
dshUserTurn(2, "first"),
|
|
dshTurnStart(3, 2),
|
|
dshUserTurn(4, "second"),
|
|
dshUserTurn(5, "context"),
|
|
})
|
|
if want := []string{dshTurnID("second"), dshTurnID("context")}; !slices.Equal(s.more().TurnConvs, want) {
|
|
t.Fatalf("newest turn's messages %v, want %v", s.more().TurnConvs, want)
|
|
}
|
|
}
|
|
|
|
// The seeded turn is its source's: a fork's TurnConvs are its own turn's
|
|
// messages, which its source's file does not have.
|
|
func TestDshTurnConvsSkipTheSeed(t *testing.T) {
|
|
head := `{"type":"session","version":4,"id":"session-child","cwd":"/w","createdAt":1000,"delegationDepth":0,"isSeeded":true}`
|
|
s := dshSessionFile(t, []string{
|
|
head,
|
|
dshTurnStart(1, 1),
|
|
dshUserTurn(2, "seed"),
|
|
`{"type":"session/end-seed","seq":3,"time":1000,"data":{"inherited":true}}`,
|
|
dshTurnStart(4, 2),
|
|
dshUserTurn(5, "own"),
|
|
})
|
|
if want := []string{dshTurnID("own")}; !slices.Equal(s.more().TurnConvs, want) {
|
|
t.Fatalf("fork's newest turn %v, want %v", s.more().TurnConvs, want)
|
|
}
|
|
}
|
|
|
|
// A file whose dsh writes no turn/start (version 3) keeps only its newest
|
|
// few messages, not one id for every message it was ever sent.
|
|
func TestDshTurnConvsKeepTheNewestFew(t *testing.T) {
|
|
lines := []string{`{"type":"session","version":3,"id":"session-old","cwd":"/w","createdAt":1000}`}
|
|
var want []string
|
|
for i := range 10 {
|
|
text := "m" + strconv.Itoa(i)
|
|
lines = append(lines, dshUserTurn(i+1, text))
|
|
want = append(want, dshTurnID(text))
|
|
}
|
|
s := dshSessionFile(t, lines)
|
|
if want = want[len(want)-dshTurnKept:]; !slices.Equal(s.more().TurnConvs, want) {
|
|
t.Fatalf("turn messages %v, want the newest %d %v", s.more().TurnConvs, dshTurnKept, want)
|
|
}
|
|
}
|