diff --git a/internal/sessions/hermes.go b/internal/sessions/hermes.go index 43c5e9995..0cdbdae24 100644 --- a/internal/sessions/hermes.go +++ b/internal/sessions/hermes.go @@ -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, diff --git a/internal/sessions/hermes_fingerprint_test.go b/internal/sessions/hermes_fingerprint_test.go index 18f1e0176..47922716a 100644 --- a/internal/sessions/hermes_fingerprint_test.go +++ b/internal/sessions/hermes_fingerprint_test.go @@ -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) + } +}