Skip to content

The agent that grows with you

fix(sessions): compacted display history keeps protected-tail copies in original chronological order

ColDSnit63316c74a863
diff --git a/hermes_state_messages.py b/hermes_state_messages.py
index 9f09886b5f4..07cb4c69ee5 100644
--- a/hermes_state_messages.py
+++ b/hermes_state_messages.py
@@ -588,6 +588,7 @@ class SessionMessagesMixin:
into each generation: same role/content/timestamp, different ``active``/id); prefer the live row, then
the newest. The ONE definition every display projection shares. *rows* must be ordered by ``id``."""
seen: Dict[Tuple[Any, ...], Any] = {}
+ first_id: Dict[Tuple[Any, ...], int] = {}
for row in rows:
dedupe_content = row["content"]
if row["role"] == "user":
@@ -604,7 +605,10 @@ class SessionMessagesMixin:
cur = seen.get(key)
if cur is None or (row["active"], row["id"]) > (cur["active"], cur["id"]):
seen[key] = row
- return sorted(seen.values(), key=lambda r: r["id"])
+ first_id[key] = min(first_id.get(key, row["id"]), row["id"])
+ # Order by the logical message's FIRST row, not the chosen representative's: a protected-tail
+ # copy in a newer generation has a higher id than messages emitted after the original.
+ return [seen[key] for key in sorted(seen, key=first_id.__getitem__)]
def _row_to_message_dict(self, row, *, warn_context: str, summary_flag: bool) -> Dict[str, Any]:
"""``dict(row)`` with content/tool_calls/display_metadata decoded; *summary_flag* keeps
diff --git a/tests/hermes_state/test_get_messages_include_compacted.py b/tests/hermes_state/test_get_messages_include_compacted.py
index 4b09c7a3aea..c7386ee35a4 100644
--- a/tests/hermes_state/test_get_messages_include_compacted.py
+++ b/tests/hermes_state/test_get_messages_include_compacted.py
@@ -158,6 +158,32 @@ class TestDisplayDedupe:
assert len(msgs) == 2
assert [m["content"] for m in msgs] == ["turn 1", "answer 1"]
+ def test_new_generation_copy_keeps_original_chronological_position(self, db):
+ """A protected-tail copy inserted after a newer message stays in its
+ original position in the display read (C, A, B regression)."""
+ sid = "s1"
+ db.create_session(sid, source="cli")
+ db.append_messages_batch(
+ sid,
+ [
+ {"role": "assistant", "content": "A", "timestamp": 100.0},
+ {"role": "assistant", "content": "B", "timestamp": 200.0},
+ ],
+ )
+ original = _row_ids(db, sid)
+ db._execute_write(
+ lambda conn: conn.execute(
+ "UPDATE messages SET active = 0, compacted = 1 WHERE session_id = ?",
+ [sid],
+ )
+ )
+ db.append_message(sid, role="user", content="C", timestamp=300.0)
+ self._copy_tail_as_new_generation(db, sid, original)
+
+ msgs = db.get_messages(sid, include_compacted=True)
+
+ assert [m["content"] for m in msgs] == ["A", "B", "C"]
+
def test_dedupe_prefers_live_row_then_newest_generation(self, db):
"""When generations conflict, the live row wins; otherwise the newest
generation (highest id) wins."""