Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

refactor(hermes): let the mapper key event ids by a Hermes home

Saurav Singh2126f80457f7
diff --git a/cli/beacon/internal/hermessession/collect.go b/cli/beacon/internal/hermessession/collect.go
index 0adbcd76..7e28acbf 100644
--- a/cli/beacon/internal/hermessession/collect.go
+++ b/cli/beacon/internal/hermessession/collect.go
@@ -581,7 +581,14 @@ func (s *Store) LastMessageID(sessionID string) (int64, error) {
// cursor is what earlier sweeps collected; a zero Cursor maps the whole session, session-total
// usage included, as a first sync does.
func MapSession(session Session, messages []Message, cursor *Cursor) ([]MappedEvent, map[string]json.RawMessage) {
- m := &mapper{session: session, cursor: cursor, calls: map[string]toolCall{}}
+ return mapSession(session, messages, cursor, "")
+}
+
+// mapSession is MapSession with the event ids keyed by idHome, the canonical path of the Hermes
+// home the session was read from. An empty idHome derives the ids from the session alone, as every
+// sync did before homes could collide.
+func mapSession(session Session, messages []Message, cursor *Cursor, idHome string) ([]MappedEvent, map[string]json.RawMessage) {
+ m := &mapper{session: session, cursor: cursor, calls: map[string]toolCall{}, idHome: idHome}
for id, raw := range cursor.PendingCalls {
var call toolCall
if json.Unmarshal(raw, &call) == nil && call.ID != "" {
@@ -618,6 +625,7 @@ func encodePendingCalls(calls map[string]toolCall) map[string]json.RawMessage {
type mapper struct {
session Session
cursor *Cursor
+ idHome string
out []MappedEvent
calls map[string]toolCall
lastTimestampMS int64
@@ -850,7 +858,13 @@ func (m *mapper) toolEvent(timestampMS int64, call toolCall, action, category st
}
func (m *mapper) append(sourceMessageID int64, suffix string, ev schema.Event) {
- ev.Event.ID = hermesEventID(fmt.Sprintf("%s:%d:%s", m.session.ID, sourceMessageID, suffix))
+ dedupID := fmt.Sprintf("%s:%d:%s", m.session.ID, sourceMessageID, suffix)
+ if m.idHome != "" {
+ // The home ends at a newline, which Hermes session ids do not contain, so a keyed id never
+ // equals an unkeyed one.
+ dedupID = m.idHome + "\n" + dedupID
+ }
+ ev.Event.ID = hermesEventID(dedupID)
m.out = append(m.out, MappedEvent{SourceMessageID: sourceMessageID, Event: ev})
}
diff --git a/cli/beacon/internal/hermessession/event_id_test.go b/cli/beacon/internal/hermessession/event_id_test.go
index 7f5c5ec6..f0c6130b 100644
--- a/cli/beacon/internal/hermessession/event_id_test.go
+++ b/cli/beacon/internal/hermessession/event_id_test.go
@@ -57,3 +57,51 @@ func assertFixtureEventIDs(t *testing.T, got map[string]string) {
}
}
}
+
+// Keying by home renames every event, and two homes never share a keyed id, while the unkeyed ids
+// are the ones MapSession has always derived.
+func TestMapSessionKeysEventIDsByHome(t *testing.T) {
+ dbPath := filepath.Join(t.TempDir(), "state.db")
+ createHermesFixtureDB(t, dbPath)
+ store, err := OpenStore(dbPath)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer store.Close()
+ sessions, err := store.ListSessions()
+ if err != nil || len(sessions) != 1 {
+ t.Fatalf("sessions = %v, %v", sessions, err)
+ }
+ messages, err := store.ReadMessages(sessions[0].ID, 0)
+ if err != nil {
+ t.Fatal(err)
+ }
+ unkeyed, _ := mapSession(sessions[0], messages, &Cursor{}, "")
+ assertFixtureEventIDs(t, mappedEventIDs(unkeyed))
+
+ seen := map[string]string{}
+ for _, item := range unkeyed {
+ seen[item.Event.Event.ID] = "unkeyed"
+ }
+ for _, home := range []string{"/home/a/.hermes", "/home/a/.hermes/profiles/work"} {
+ keyed, _ := mapSession(sessions[0], messages, &Cursor{}, home)
+ if len(keyed) != len(unkeyed) {
+ t.Fatalf("keyed by %s mapped %d events, want %d", home, len(keyed), len(unkeyed))
+ }
+ for i, item := range keyed {
+ if item.Event.Event.Action != unkeyed[i].Event.Event.Action {
+ t.Fatalf("keyed by %s event %d is %s, want %s", home, i, item.Event.Event.Action, unkeyed[i].Event.Event.Action)
+ }
+ if other, dup := seen[item.Event.Event.ID]; dup {
+ t.Fatalf("%s id %s keyed by %s repeats one keyed by %s", item.Event.Event.Action, item.Event.Event.ID, home, other)
+ }
+ seen[item.Event.Event.ID] = home
+ }
+ again, _ := mapSession(sessions[0], messages, &Cursor{}, home)
+ for i := range again {
+ if again[i].Event.Event.ID != keyed[i].Event.Event.ID {
+ t.Fatalf("keyed ids for %s are not deterministic", home)
+ }
+ }
+ }
+}