Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

sessions: a write to one Hermes session parses only that session again (#2776)

yetone9893ae8216d4
diff --git a/internal/sessions/hermes.go b/internal/sessions/hermes.go
index 1c1869660..43c5e9995 100644
--- a/internal/sessions/hermes.go
+++ b/internal/sessions/hermes.go
@@ -12,6 +12,7 @@ import (
"path/filepath"
"strconv"
"strings"
+ "sync"
"time"
"github.com/yetone/magpie/internal/appdir"
@@ -92,6 +93,141 @@ func hermesRevision(path string) string {
return strings.Join(parts, ":")
}
+// hermesPrinted is each store's sessions' fingerprints, as of the store's
+// revision they were read at.
+var hermesPrinted = struct {
+ sync.Mutex
+ stores map[string]hermesPrintSet
+}{stores: map[string]hermesPrintSet{}}
+
+type hermesPrintSet struct {
+ revision string
+ prints map[string]string
+}
+
+// hermesPrintsFor is the fingerprint of each session in the store at path,
+// read again only when the store's revision moved. nil when they could not
+// be read: then no session's parse is reused.
+func hermesPrintsFor(path string, h *hermesDB) map[string]string {
+ hermesPrinted.Lock()
+ defer hermesPrinted.Unlock()
+ if set, ok := hermesPrinted.stores[path]; ok && h.revision != "" && set.revision == h.revision {
+ return set.prints
+ }
+ prints := h.fingerprints()
+ if prints != nil && h.revision != "" {
+ hermesPrinted.stores[path] = hermesPrintSet{h.revision, prints}
+ } else {
+ delete(hermesPrinted.stores, path)
+ }
+ return prints
+}
+
+func forgetHermesPrints() {
+ hermesPrinted.Lock()
+ hermesPrinted.stores = map[string]hermesPrintSet{}
+ hermesPrinted.Unlock()
+}
+
+// fingerprints is, for each session, what parseHermes reads of it in a few
+// values: its sessions row, and over its messages and usage rows a count,
+// the ids, and the sums of every read column's values or byte lengths. A
+// row added, removed or rewritten in place (active and compacted flipped
+// by a rewind or a compaction, a tool call's content filled in later)
+// moves it. Any write moves the whole store's revision, and parsing every
+// session again on each one read, de-duplicated and decoded every message
+// Hermes ever kept, again and again while it ran (#2776); this reads only
+// the tables' pages, in SQLite. A rewrite to text of the very same length,
+// with no other column moving, is the one change it can miss. nil when any
+// of it could not be read.
+func (h *hermesDB) fingerprints() map[string]string {
+ if !h.sessions["id"] {
+ return nil
+ }
+ tx, err := h.db.BeginTx(context.Background(), nil)
+ if err != nil {
+ return nil
+ }
+ defer tx.Rollback()
+ parts := map[string][]string{}
+ read := func(table, query string) bool {
+ rows, err := tx.Query(query)
+ if err != nil {
+ return false
+ }
+ defer rows.Close()
+ cols, err := rows.Columns()
+ if err != nil {
+ return false
+ }
+ vals := make([]sql.NullString, len(cols))
+ ptrs := make([]any, len(cols))
+ for i := range vals {
+ ptrs[i] = &vals[i]
+ }
+ for rows.Next() {
+ if rows.Scan(ptrs...) != nil {
+ return false
+ }
+ var b strings.Builder
+ b.WriteString(table)
+ b.WriteByte(':')
+ for _, v := range vals[1:] {
+ if v.Valid {
+ b.WriteString(strconv.Quote(v.String))
+ } else {
+ b.WriteString("~")
+ }
+ b.WriteByte(',')
+ }
+ parts[vals[0].String] = append(parts[vals[0].String], b.String())
+ }
+ return rows.Err() == nil
+ }
+ // metadata()'s columns, as it reads them
+ if !read("s", "SELECT id, "+hermesSelect(h.sessions, []string{"title", "cwd", "model", "billing_provider"}, "''")+", "+
+ hermesSelect(h.sessions, []string{"started_at", "ended_at", "last_activity_at", "input_tokens", "output_tokens",
+ "cache_read_tokens", "cache_write_tokens", "reasoning_tokens", "api_call_count"}, "0")+" FROM sessions") {
+ return nil
+ }
+ column := func(cols map[string]bool, name string) string {
+ if cols[name] {
+ return name
+ }
+ return "NULL"
+ }
+ if h.messages["session_id"] {
+ q := []string{"session_id", "count(*)", "max(" + hermesExpr(h.messages, "id", "rowid") + ")", "total(" + hermesExpr(h.messages, "id", "rowid") + ")"}
+ for _, c := range []string{"timestamp", "active", "compacted"} {
+ q = append(q, "total("+column(h.messages, c)+")")
+ }
+ for _, c := range []string{"role", "content", "tool_calls", "tool_call_id", "tool_name", "display_kind", "display_metadata"} {
+ q = append(q, "total(octet_length("+column(h.messages, c)+"))")
+ }
+ if !read("m", "SELECT "+strings.Join(q, ", ")+" FROM messages GROUP BY session_id") {
+ return nil
+ }
+ }
+ if h.usage["session_id"] {
+ q := []string{"session_id", "count(*)"}
+ for _, c := range []string{"input_tokens", "output_tokens", "cache_read_tokens", "cache_write_tokens", "reasoning_tokens", "api_call_count", "last_seen"} {
+ q = append(q, "total("+column(h.usage, c)+")")
+ }
+ for _, c := range []string{"model", "billing_provider", "task"} {
+ q = append(q, "total(octet_length("+column(h.usage, c)+"))", "max("+column(h.usage, c)+")", "min("+column(h.usage, c)+")")
+ }
+ if !read("u", "SELECT "+strings.Join(q, ", ")+" FROM session_model_usage GROUP BY session_id") {
+ return nil
+ }
+ }
+ out := make(map[string]string, len(parts))
+ for sid, p := range parts {
+ sum := sha256.Sum256([]byte(strings.Join(p, "|")))
+ out[sid] = "fp1:" + hex.EncodeToString(sum[:16])
+ }
+ return out
+}
+
type hermesQuery interface {
Query(string, ...any) (*sql.Rows, error)
QueryRow(string, ...any) *sql.Row
@@ -154,6 +290,7 @@ func hermesFiles() []file {
if !store.sessions["id"] {
continue
}
+ prints := hermesPrintsFor(path, store)
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)"
@@ -185,6 +322,7 @@ func hermesFiles() []file {
main: true,
mod: hermesTime(max(start, end, last, messageAt, usageAt)),
hermes: store,
+ rev: prints[sid], // "" when unknown: parsed again
readOnly: true,
})
}
@@ -462,7 +600,7 @@ func parseHermes(f file) *state {
s.Last = at
}
}
- s.DBRevision = f.hermes.revision
+ s.DBRevision = f.rev
return s
}
diff --git a/internal/sessions/hermes_fingerprint_test.go b/internal/sessions/hermes_fingerprint_test.go
new file mode 100644
index 000000000..18f1e0176
--- /dev/null
+++ b/internal/sessions/hermes_fingerprint_test.go
@@ -0,0 +1,99 @@
+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", hermes: h, rev: "", mod: time.Unix(1790000000, 0)}
+ s := state{Mod: f.mod.UnixNano(), DBRevision: ""}
+ 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)
+ }
+}
diff --git a/internal/sessions/sessions.go b/internal/sessions/sessions.go
index 5afb21a51..474dd2461 100644
--- a/internal/sessions/sessions.go
+++ b/internal/sessions/sessions.go
@@ -755,7 +755,7 @@ func refresh(want, all []file) {
if f.cold {
continue // in a stopped WSL distro: what was read of it stands
}
- if s := cache[f.path]; s == nil || s.Size != f.size || s.Mod != f.mod.UnixNano() || (f.agent == "hermes" && (f.hermes == nil || f.hermes.revision == "" || s.DBRevision != f.hermes.revision)) || ((f.agent == "alma" || f.agent == "cursor") && s.DBRevision != f.rev) {
+ if s := cache[f.path]; s == nil || s.Size != f.size || s.Mod != f.mod.UnixNano() || (f.agent == "hermes" && (f.hermes == nil || f.rev == "" || s.DBRevision != f.rev)) || ((f.agent == "alma" || f.agent == "cursor") && s.DBRevision != f.rev) {
todo = append(todo, f)
}
}
@@ -975,6 +975,7 @@ func Reset() {
directoryCache.Unlock()
resetCalls()
wslReset()
+ forgetHermesPrints()
mu.Lock()
defer mu.Unlock()
saved()