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).
137 lines
4.3 KiB
Go
137 lines
4.3 KiB
Go
package sessions
|
|
|
|
import (
|
|
"database/sql"
|
|
"path/filepath"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// A write to one Hermes session moves the whole store's revision. Only that
|
|
// session is parsed again (#2776): every session was, each poll while
|
|
// Hermes ran. A message rewritten in place, with no row added, still is.
|
|
func TestHermesWriteParsesOnlyItsSession(t *testing.T) {
|
|
setup(t)
|
|
root := filepath.Join(t.TempDir(), "hermes")
|
|
t.Setenv("HERMES_HOME", root)
|
|
path := hermesFixture(t, root, "", "busy", true)
|
|
db, err := sql.Open("sqlite", path+"?_pragma=journal_mode(WAL)")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer db.Close()
|
|
base := float64(time.Date(2026, 9, 28, 10, 0, 0, 0, time.UTC).Unix())
|
|
for _, sid := range []string{"idle1", "idle2", "idle3"} {
|
|
if _, err := db.Exec(`INSERT INTO sessions (id, title, started_at) VALUES (?, NULL, ?)`, sid, base); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := db.Exec(`INSERT INTO messages (session_id, role, content, tool_calls, timestamp) VALUES (?, 'user', 'first words', '[]', ?)`, sid, base+1); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
var parsed atomic.Int32
|
|
parseHook = func() { parsed.Add(1) }
|
|
t.Cleanup(func() { parseHook = nil })
|
|
|
|
List(0)
|
|
parsed.Store(0)
|
|
List(0)
|
|
if n := parsed.Load(); n != 0 {
|
|
t.Fatalf("nothing changed, yet %d parsed again", n)
|
|
}
|
|
|
|
if _, err := db.Exec(`INSERT INTO messages (session_id, role, content, tool_calls, timestamp) VALUES ('busy', 'user', 'one more', '[]', ?)`, base+20); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
List(0)
|
|
if n := parsed.Load(); n != 1 {
|
|
t.Fatalf("one session written, %d parsed again", n)
|
|
}
|
|
|
|
// in place: the row count, ids and times all stay
|
|
parsed.Store(0)
|
|
if _, err := db.Exec(`UPDATE messages SET content = 'rewritten first words' WHERE session_id = 'idle2'`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := hermesSessionByID(t, hermesID(path, "idle2")).Title; got != "rewritten first words" {
|
|
t.Errorf("in-place rewrite not read: title %q", got)
|
|
}
|
|
if n := parsed.Load(); n != 1 {
|
|
t.Fatalf("one message rewritten in place, %d parsed again", n)
|
|
}
|
|
}
|
|
|
|
// Fingerprints that can't be read are unknown: no session's parse is kept.
|
|
func TestHermesUnreadFingerprintsParseAgain(t *testing.T) {
|
|
setup(t)
|
|
root := filepath.Join(t.TempDir(), "hermes")
|
|
path := hermesFixture(t, root, "", "s", true)
|
|
db, err := sql.Open("sqlite", path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
h := newHermesDB(db)
|
|
h.revision = hermesRevision(path)
|
|
if p := hermesPrintsFor(path, h); p["s"] == "" {
|
|
t.Fatalf("fingerprints of a readable store: %v", p)
|
|
}
|
|
db.Close()
|
|
h.revision = "moved"
|
|
if p := hermesPrintsFor(path, h); p != nil {
|
|
t.Fatalf("a closed store's fingerprints read as %v", p)
|
|
}
|
|
// the same size and time as what was parsed: only the fingerprint can
|
|
// tell it changed, and it is unknown
|
|
f := file{agent: "hermes", path: path + "#s", more: &fileMore{hermes: h}, mod: time.Unix(1790000000, 0)}
|
|
s := state{Mod: f.mod.UnixNano()}
|
|
cache = map[string]*state{f.path: &s}
|
|
t.Cleanup(func() { cache = nil })
|
|
parsed := 0
|
|
parseHook = func() { parsed++ }
|
|
t.Cleanup(func() { parseHook = nil })
|
|
mu.Lock()
|
|
refresh([]file{f}, nil)
|
|
mu.Unlock()
|
|
if parsed != 1 {
|
|
t.Fatalf("an unknown fingerprint reused a parse (%d parsed)", parsed)
|
|
}
|
|
}
|
|
|
|
// A store's path is resolved (EvalSymlinks walks every folder in it) once
|
|
// per listing, not once per session: 2000 sessions spent ~30ms on it per
|
|
// poll (#2776).
|
|
func TestHermesStoreResolvedOncePerListing(t *testing.T) {
|
|
setup(t)
|
|
root := filepath.Join(t.TempDir(), "hermes")
|
|
t.Setenv("HERMES_HOME", root)
|
|
path := hermesFixture(t, root, "", "s0", true)
|
|
db, err := sql.Open("sqlite", path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer db.Close()
|
|
for i := 1; i < 10; i++ {
|
|
sid := "s" + string(rune('0'+i))
|
|
if _, err := db.Exec(`INSERT INTO sessions (id, title, started_at) VALUES (?, 'x', 1790000000)`, sid); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
var n atomic.Int32
|
|
canonicalHook = func() { n.Add(1) }
|
|
t.Cleanup(func() { canonicalHook = nil })
|
|
mu.Lock()
|
|
files := hermesFiles()
|
|
closeDBs()
|
|
mu.Unlock()
|
|
if len(files) != 10 {
|
|
t.Fatalf("%d files, want 10", len(files))
|
|
}
|
|
if got := int(n.Load()); got >= len(files) {
|
|
t.Fatalf("the store's path was resolved %d times for %d sessions", got, len(files))
|
|
}
|
|
if want := "hermes:" + hermesID(path, files[0].m().sid); files[0].key != want {
|
|
t.Fatalf("key %q, want %q", files[0].key, want)
|
|
}
|
|
}
|