The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(state): avoid display metadata backfill rewrites
fangliquan7e4180d1e836
diff --git a/hermes_state_common.py b/hermes_state_common.pyindex 453b2ecef4f..ca3722696ea 100644--- a/hermes_state_common.py+++ b/hermes_state_common.py@@ -622,7 +622,14 @@ END;DROP TRIGGER IF EXISTS messages_display_identity_update;CREATE TRIGGER IF NOT EXISTS messages_display_identity_updateAFTER UPDATE OF role, content, timestamp, tool_call_id, tool_calls, tool_name,- display_kind, display_metadata ON messages+ display_kind ON messages+WHEN new.role IS NOT old.role+ OR new.content IS NOT old.content+ OR new.timestamp IS NOT old.timestamp+ OR new.tool_call_id IS NOT old.tool_call_id+ OR new.tool_calls IS NOT old.tool_calls+ OR new.tool_name IS NOT old.tool_name+ OR new.display_kind IS NOT old.display_kindBEGINUPDATE messages SET display_identity = NULL, display_order = NULLWHERE id = new.id OR (diff --git a/hermes_state_messages.py b/hermes_state_messages.pyindex 4e7b96faa7b..fc42970b618 100644--- a/hermes_state_messages.py+++ b/hermes_state_messages.py@@ -742,10 +742,16 @@ class SessionMessagesMixin:for row in rows:key = self._display_dedupe_key(row)first_id[key] = min(first_id.get(key, row["id"]), row["id"])- keyed_rows.append((row["id"], key))+ keyed_rows.append((row, key))+ updates = []+ for row, key in keyed_rows:+ order = first_id[key]+ identity = self._display_identity(key)+ if order != row["display_order"] or identity != row["display_identity"]:+ updates.append((order, identity, row["id"]))conn.executemany("UPDATE messages SET display_order = ?, display_identity = ? WHERE id = ?",- [(first_id[key], self._display_identity(key), row_id) for row_id, key in keyed_rows])+ updates)return Truereturn bool(self._execute_write(_do))diff --git a/tests/hermes_state/test_get_messages_include_compacted.py b/tests/hermes_state/test_get_messages_include_compacted.pyindex af8911b2c4b..34f342b0d6a 100644--- a/tests/hermes_state/test_get_messages_include_compacted.py+++ b/tests/hermes_state/test_get_messages_include_compacted.py@@ -250,6 +250,54 @@ class TestDisplayDedupe:assert [m["id"] for m in db.get_messages(sid, include_compacted=True)][-1:] == [newest_id]assert len([m for m in db.get_messages(sid, include_compacted=True) if m["content"] == "same"]) == 1+ def test_display_metadata_update_does_not_invalidate_display_identity(self, db):+ sid = "metadata"+ db.create_session(sid, source="desktop")+ row_ids = [+ db.append_message(sid, role="assistant", content=f"row-{index}", timestamp=100.0)+ for index in range(3)+ ]+ before = db._conn.execute(+ "SELECT id, display_order, display_identity FROM messages WHERE session_id = ? ORDER BY id",+ (sid,),+ ).fetchall()++ changes = db._conn.total_changes+ db.set_message_reaction(sid, row_ids[1], "👍", author="user")+ assert db._conn.total_changes - changes == 1+ assert db._conn.execute(+ "SELECT id, display_order, display_identity FROM messages WHERE session_id = ? ORDER BY id",+ (sid,),+ ).fetchall() == before++ changes = db._conn.total_changes+ db.get_messages(sid, include_compacted=True)+ assert db._conn.total_changes == changes++ def test_display_backfill_rewrites_only_changed_identity_group(self, db):+ sid = "identity-update"+ db.create_session(sid, source="desktop")+ first = db.append_message(sid, role="assistant", content="same", timestamp=100.0)+ db.append_message(sid, role="assistant", content="same", timestamp=100.0)+ unaffected = db.append_message(sid, role="assistant", content="other", timestamp=100.0)+ unaffected_before = db._conn.execute(+ "SELECT display_order, display_identity FROM messages WHERE id = ?", (unaffected,)+ ).fetchone()+ db._execute_write(lambda conn: conn.execute(+ "UPDATE messages SET content = ? WHERE id = ?", ('"changed"', first)))++ changes = db._conn.total_changes+ db.get_messages(sid, include_compacted=True)+ assert db._conn.total_changes - changes == 2+ assert db._conn.execute(+ "SELECT COUNT(*) FROM messages WHERE session_id = ? "+ "AND (display_order IS NULL OR display_identity IS NULL)",+ (sid,),+ ).fetchone()[0] == 0+ assert db._conn.execute(+ "SELECT display_order, display_identity FROM messages WHERE id = ?", (unaffected,)+ ).fetchone() == unaffected_before+def test_legacy_store_is_readable_then_lazily_migrated(self, tmp_path):path = tmp_path / "legacy.db"writer = SessionDB(path)