Skip to content

The agent that grows with you

fix(state): resuming a compressed conversation shows each message once

Tekniuma59ed658aa85
diff --git a/hermes_state_messages.py b/hermes_state_messages.py
index 0417e17d955..318cdce3088 100644
--- a/hermes_state_messages.py
+++ b/hermes_state_messages.py
@@ -1107,6 +1107,7 @@ class SessionMessagesMixin(SessionDetachedResultsMixin, SessionTodoResultMixin):
(a micro-compaction merge also stands for the rows it absorbed)."""
seen: dict[tuple[Any, ...], Any] = {}
first_id: dict[tuple[Any, ...], int] = {}
+ uid_key: dict[Any, tuple[Any, ...]] = {} # message_uid -> the display key of its first copy
removed: dict[Any, bool] = {} # copy identity -> its newest copy is removed (rows come in id order)
identities: dict[tuple[Any, ...], list[Any]] = {}
removed_ts: set[Any] = set()
@@ -1134,6 +1135,14 @@ class SessionMessagesMixin(SessionDetachedResultsMixin, SessionTodoResultMixin):
if self._is_model_only_row(row):
continue
key = key or self._display_dedupe_key(row)
+ # A compaction carrier keeps its message's uid but not its content (the prior tail wrapped
+ # around the summary): it is a copy of that message, shown once. Only the carrier: other
+ # rewritten copies (a pruned tool stub) must keep their archived original visible.
+ uid = row["message_uid"]
+ if uid and row["_compressed_summary"]:
+ key = uid_key.get(uid, key)
+ elif uid:
+ uid_key.setdefault(uid, key)
cur = seen.get(key)
if cur is None or (row["active"], row["id"]) > (cur["active"], cur["id"]):
seen[key] = row
diff --git a/tests/hermes_state/test_resume_summary_carrier_once.py b/tests/hermes_state/test_resume_summary_carrier_once.py
new file mode 100644
index 00000000000..da40a0a8edc
--- /dev/null
+++ b/tests/hermes_state/test_resume_summary_carrier_once.py
@@ -0,0 +1,40 @@
+"""Resuming a compressed conversation shows each message once.
+
+In-place compaction can carry a protected-tail message forward wrapped in the summary carrier (the
+prior turn's text, then the compaction summary). The carrier keeps the original's ``message_uid``
+but not its content, so the display read used to show the archived original AND its text again
+inside the carrier: every word of that reply twice after ``/compress`` and on resume (the TUI
+resume/compress E2E caught it on the v0.22.3 release attempt). Real SessionDB, real
+``archive_and_compact``.
+"""
+from agent.context_compressor import (
+ SUMMARY_PREFIX,
+ _MERGED_PRIOR_CONTEXT_HEADER,
+ _MERGED_SUMMARY_DELIMITER,
+ _SUMMARY_END_MARKER,
+)
+from hermes_state import SessionDB
+
+
+def test_summary_carrier_does_not_show_its_message_twice(tmp_path):
+ with SessionDB(db_path=tmp_path / "state.db") as db:
+ db.create_session("s", source="test")
+ history = [
+ {"role": "user", "content": "turn one", "timestamp": 1.0},
+ {"role": "assistant", "content": "REPLY-ONE words", "timestamp": 2.0},
+ {"role": "user", "content": "turn two", "timestamp": 3.0},
+ {"role": "assistant", "content": "REPLY-TWO words", "timestamp": 4.0},
+ ]
+ db.append_messages_batch("s", history)
+ stored = db.get_messages_as_conversation("s", include_row_ids=True)
+ carried = dict(stored[1]) # the compressor wraps the carried reply around the summary
+ carried["content"] = (f"{_MERGED_PRIOR_CONTEXT_HEADER}\nREPLY-ONE words\n\n{_MERGED_SUMMARY_DELIMITER}"
+ f"\n\n{SUMMARY_PREFIX}\nsummary of earlier turns\n\n{_SUMMARY_END_MARKER}")
+ carried["_compressed_summary"] = True
+ db.archive_and_compact("s", [stored[0], carried, *stored[2:]])
+
+ shown = db.get_messages_as_conversation("s", include_ancestors=True, include_compacted=True)
+ assert sum(str(m.get("content")).count("REPLY-ONE") for m in shown) == 1, shown
+ # The other messages are untouched: each still shows exactly once.
+ for text in ("turn one", "turn two", "REPLY-TWO"):
+ assert sum(str(m.get("content")).count(text) for m in shown) == 1, (text, shown)