sessions: a Hermes store's path is resolved once per listing, not per session (#2776)

hermesFiles built each session's id with hermesID(path, sid), which
resolves the store's path (filepath.Abs and EvalSymlinks, a stat per
folder) and hashes it, for every session of the store, on every listing.
Every Sessions endpoint enumerates every agent, so this ran on each poll.
hermesStoreID now resolves and hashes once per store; each session's id
is that prefix and its own id, the same string as before.

Test: TestHermesStoreResolvedOncePerListing counts resolves for a store
of 10 sessions (red on the old code: 12 resolves for 10 sessions) and
checks a file's key still equals hermesID's.

Measured on the synthetic sandbox HOME (2000 Hermes sessions beside 3000
Claude Code and 1500 Codex), warm index: a CPU profile of 9 warm
requests (manage?agent=claude, manage?agent=codex, /api/sessions, three
times; 33 enumerations) put 0.96 s CPU in hermesID, ~30 ms per
enumeration of 2000 sessions. After, that is one resolve per store.

Run: go test ./internal/sessions -run TestHermes -count=1 (ok); gofmt -l
clean. Full suite and cross-OS vet are recorded on the last commit of
this series.
This commit is contained in:
yetone
2026-10-19 03:57:52 +08:00
parent 9893ae8216
commit 2b2d5fd8e4
2 changed files with 54 additions and 5 deletions
+17 -5
View File
@@ -52,7 +52,13 @@ func HermesDirs() []string {
return out
}
// canonicalHook, when a test sets it, runs on each canonicalHermesPath.
var canonicalHook func()
func canonicalHermesPath(path string) string {
if canonicalHook != nil {
canonicalHook()
}
if abs, err := filepath.Abs(path); err == nil {
path = abs
}
@@ -62,11 +68,16 @@ func canonicalHermesPath(path string) string {
return filepath.Clean(path)
}
func hermesID(path, sid string) string {
// Include the store even for the default profile so selecting another home
// cannot collide with a previously indexed store or a copied session id.
func hermesID(path, sid string) string { return hermesStoreID(path) + "-" + sid }
// hermesStoreID is what a store's session ids begin with. Include the store
// even for the default profile so selecting another home cannot collide
// with a previously indexed store or a copied session id. Resolving the
// path walks every folder in it (EvalSymlinks), so it is done once per
// store, not once per session.
func hermesStoreID(path string) string {
h := sha256.Sum256([]byte(canonicalHermesPath(path)))
return "h" + hex.EncodeToString(h[:8]) + "-" + sid
return "h" + hex.EncodeToString(h[:8])
}
type hermesDB struct {
@@ -291,6 +302,7 @@ func hermesFiles() []file {
continue
}
prints := hermesPrintsFor(path, store)
storeID := hermesStoreID(path)
messageLast, usageLast := "0", "0"
if store.messages["session_id"] && store.messages["timestamp"] {
messageLast = "COALESCE((SELECT MAX(timestamp) FROM messages m WHERE m.session_id = sessions.id), 0)"
@@ -313,7 +325,7 @@ func hermesFiles() []file {
if rows.Scan(&sid, &start, &end, &last, &messageAt, &usageAt) != nil || sid == "" {
continue
}
id := hermesID(path, sid)
id := storeID + "-" + sid
out = append(out, file{
agent: "hermes",
key: "hermes:" + id,
@@ -97,3 +97,40 @@ func TestHermesUnreadFingerprintsParseAgain(t *testing.T) {
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].sid); files[0].key != want {
t.Fatalf("key %q, want %q", files[0].key, want)
}
}