The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(compression): a merged user dict names only rows inside the compression snapshot
John Paul Soliva701e9e6fa430
diff --git a/hermes_state_messages.py b/hermes_state_messages.pyindex 4de69acb889..56545c93a32 100644--- a/hermes_state_messages.py+++ b/hermes_state_messages.py@@ -994,7 +994,9 @@ class SessionMessagesMixin:"SELECT id FROM messages WHERE session_id = ? AND active = 1 AND role = ? AND content = ?",(session_id, message.get("role"), stored)).fetchall()]- def _merged_user_run(self, conn, session_id: str, message: Dict[str, Any]) -> Optional[List[int]]:+ def _merged_user_run(+ self, conn, session_id: str, message: Dict[str, Any], watermark: Optional[int] = None,+ ) -> Optional[List[int]]:"""Active rows an alternation repair merged into *message*: ``[]`` for none, None when ambiguous.A reload without row ids turns a durable ``user;user`` pair (a prompt that never got its reply)@@ -1002,17 +1004,21 @@ class SessionMessagesMixin:after the compacted set like concurrent appends, behind the turn that is running.Only a dict the repair stamped: a prompt that was never persisted can carry the same text asrows another surface appended, and those were never held.+ Only rows at or below *watermark*: the repair ran on a load taken before the snapshot, so a+ run appended later that joins to the same text is another surface's, not the merged one."""from agent.conversation_compression_archive import MERGED_DURABLE_ROWScontent, width = message.get("content"), message.get(MERGED_DURABLE_ROWS)if message.get("role") != "user" or not isinstance(content, str) or type(width) is not int or width < 2:return []+ bound = watermark is not Nonerows = [(int(row["id"]), row["role"], self._loaded_view_content(row["role"], self._decode_content(row["content"])))for row in conn.execute(- "SELECT id, role, content FROM messages WHERE session_id = ? AND active = 1 ORDER BY id",- (session_id,)).fetchall()]+ f"SELECT id, role, content FROM messages WHERE session_id = ? AND active = 1"+ f"{' AND id <= ?' if bound else ''} ORDER BY id",+ (session_id, *((int(watermark),) if bound else ()))).fetchall()]runs: List[List[int]] = []for start in range(len(rows)):merged = ""@@ -1033,7 +1039,7 @@ class SessionMessagesMixin:def _proved_coverage(self, conn, session_id: str, covered_ids: Optional[List[int]],- unresolved_held: Optional[List[Dict[str, Any]]],+ unresolved_held: Optional[List[Dict[str, Any]]], watermark: Optional[int] = None,) -> Optional[Tuple[List[int], Set[int]]]:"""``(ids safe to archive as summarized, ids merged into another held dict)``, or None whena durable held row cannot be named.@@ -1060,7 +1066,7 @@ class SessionMessagesMixin:# The stamp is provenance and text is not: a surface may have re-rendered the dict since the# repair (gateway timestamps), and a later row can equal the merged text. So the run is# resolved first, and a stamped dict that names none is still durable, never an unpersisted turn.- run = self._merged_user_run(conn, session_id, message)+ run = self._merged_user_run(conn, session_id, message, watermark)if message.get(MERGED_DURABLE_ROWS) and not run:return Nonematches = [] if run else self._matching_active_ids(conn, session_id, message)@@ -1191,7 +1197,7 @@ class SessionMessagesMixin:# on_missing="raise": never commit against a vanished session row (caller keeps the original).patched_model_config = self._merge_model_config_json(conn, session_id, model_config_patch, on_missing="raise") if patch else None- proved = self._proved_coverage(conn, session_id, covered_ids, unresolved_held)+ proved = self._proved_coverage(conn, session_id, covered_ids, unresolved_held, watermark)if proved is not None:return self._archive_named_rows(conn, session_id, compacted_messages, proved[0], tail_count=tail_count,diff --git a/tests/hermes_state/test_archive_merged_user_rows.py b/tests/hermes_state/test_archive_merged_user_rows.pyindex 692dc15389a..463593edcac 100644--- a/tests/hermes_state/test_archive_merged_user_rows.py+++ b/tests/hermes_state/test_archive_merged_user_rows.py@@ -56,6 +56,25 @@ def test_carried_merged_prompt_rendered_with_a_timestamp_keeps_one_copy_of_each_db.close()+def test_merged_prompt_never_names_a_run_appended_after_the_snapshot(tmp_path):+ from gateway.run import _build_gateway_agent_history++ db, watermark = _unanswered_pair(tmp_path)+ restored = db.get_messages_as_conversation("sid", repair_alternation=True)+ held, _ = _build_gateway_agent_history(restored, inject_timestamps=True)+ late = held[0]["content"].split("\n\n")+ assert len(late) == 2 and late[0] != "first prompt" # only the late run joins to the rendered text+ for part in late:+ db.append_message("sid", "user", part)++ db.archive_and_compact(+ "sid", [{"role": "assistant", "content": "summary"}, held[0]], watermark=watermark, tail_count=1,+ covered_ids=[], unresolved_held=held)++ assert _live(db) == ["summary", held[0]["content"], *late]+ db.close()++def test_merged_prompt_names_its_pair_not_a_later_row_with_the_merged_text(tmp_path):db, watermark = _unanswered_pair(tmp_path, ("assistant", "A1"))held = db.get_messages_as_conversation("sid", repair_alternation=True)