Skip to content

The agent that grows with you

test(state): archive-mode replace_messages keeps prefix ids and archives only dropped turns (#82956)

kshitijk4poor19843ab61adf
diff --git a/tests/hermes_state/test_replace_messages_archives_suffix_only.py b/tests/hermes_state/test_replace_messages_archives_suffix_only.py
new file mode 100644
index 00000000000..c0f019a3076
--- /dev/null
+++ b/tests/hermes_state/test_replace_messages_archives_suffix_only.py
@@ -0,0 +1,42 @@
+"""#82956: ``replace_messages(archive_dropped=True)`` must archive only the dropped suffix.
+
+The archive-mode rewind used to archive EVERY live row and re-insert the kept
+prefix as fresh rows, so the ``active=0/compacted=0`` archive grew by the whole
+transcript on every rewind/edit with nothing pruning it. Invariants pinned:
+
+- inactive rows == turns the user genuinely dropped (not dropped + prefix × rounds);
+- the kept prefix keeps its row ids across rounds (stable ids prove no re-insert).
+"""
+
+from hermes_state import SessionDB
+
+
+def _turn(i: int, tag: str) -> dict:
+ return {"role": "user" if i % 2 == 0 else "assistant", "content": f"{tag} {i}"}
+
+
+def test_archive_mode_rewind_archives_only_the_dropped_suffix(tmp_path):
+ db = SessionDB(tmp_path / "state.db")
+ sid = "rewind-suffix-only"
+ db.create_session(sid, "test")
+ history = [_turn(i, "msg") for i in range(50)]
+ for m in history:
+ db.append_message(sid, m["role"], m["content"])
+
+ def live_ids():
+ return [m["id"] for m in db.get_messages(sid)]
+
+ prefix_ids = live_ids()[:40]
+ for k in range(3):
+ db.replace_messages(sid, [dict(m) for m in history[:40]], active_only=True, archive_dropped=True)
+ assert live_ids() == prefix_ids, f"round {k}: kept prefix was re-inserted under new ids"
+ history = history[:40] + [_turn(i, f"new {k}") for i in range(10)]
+ for m in history[40:]:
+ db.append_message(sid, m["role"], m["content"])
+
+ rows = db.get_messages(sid, include_inactive=True)
+ inactive = [m for m in rows if not m["active"]]
+ assert len(inactive) == 30, f"expected the 3×10 dropped turns archived, got {len(inactive)}"
+ assert len(rows) == 80
+ assert len([m for m in rows if m["active"]]) == 50
+ assert db.get_session(sid)["message_count"] == 50