Skip to content

The agent that grows with you

fix(compression): a held row a merge rewrote does not bound the archive

John Paul Soliva1a632c3ab3b2
diff --git a/agent/conversation_compression.py b/agent/conversation_compression.py
index 334c31b20cf..02e60f7fdf5 100644
--- a/agent/conversation_compression.py
+++ b/agent/conversation_compression.py
@@ -3624,12 +3624,23 @@ def _held_watermark(agent: Any, watermark: Optional[int], messages: list, verbat
"""
if watermark is None:
return None
+ from agent.context_compressor import _DB_PERSISTED_MARKER
rows = [*messages, *(verbatim_tail or ())]
held = [m.get("_row_id") for m in rows if isinstance(m, dict)]
held = [rid for rid in held if isinstance(rid, int) and not isinstance(rid, bool) and rid > 0]
newest = next((m for m in reversed(rows) if isinstance(m, dict)), None)
if newest is None or newest.get("_row_id") not in held or max(held) >= watermark:
return watermark
+ # ...and, when it is a summarized row, that it still matches its durable row. A dict loaded from
+ # the DB is born carrying both the id and the persist marker; a pass that rewrote its content drops
+ # the marker and keeps the id (the user/assistant merges in `repair_message_sequence`, or a context
+ # engine that rewrites in place). Such a row absorbed later durable rows whose ids are gone from
+ # `held`, so `max(held)` no longer names what the summary covered: capped there, those rows would be
+ # cloned live beside a summary that already contains them. A `here N` tail is exempt: it is copied
+ # verbatim (without the marker), so its ids are exact and, being newest, they lift the cap above
+ # anything a merged row earlier in `messages` absorbed.
+ if not any(isinstance(m, dict) for m in (verbatim_tail or ())) and not newest.get(_DB_PERSISTED_MARKER):
+ return watermark
if agent._session_db.get_message_role(agent.session_id, max(held)) is None:
return watermark
return max(held)
diff --git a/tests/agent/test_conversation_compression_manual.py b/tests/agent/test_conversation_compression_manual.py
index 95e694b9156..3e81880cf4a 100644
--- a/tests/agent/test_conversation_compression_manual.py
+++ b/tests/agent/test_conversation_compression_manual.py
@@ -267,3 +267,20 @@ def test_in_place_compress_never_leaves_two_live_copies_of_a_row(session_db, sta
contents = [m["content"] for m in session_db.get_messages_as_conversation("sid")]
assert len(contents) == len(set(contents))
+
+
+def test_in_place_compress_never_clones_a_row_a_merge_already_carried(session_db):
+ """Resume repair merges consecutive user rows into the first one's dict: that dict keeps its row id, drops the
+ persisted marker, and the later row's id leaves the held history. Stopping the archive at the newest held id
+ would then leave the later row above the stop, cloned as a concurrent append beside the merged row that
+ already carries its content. Two strings, so the exact-duplicate check above cannot see it."""
+ agent, _ = _stored_agent(session_db, _exchanges(10))
+ session_db.append_message("sid", "user", "first half of a split prompt")
+ session_db.append_message("sid", "user", "second half 9931")
+ held = session_db.get_resume_conversations("sid")[0] # the resume path merges the two user rows
+ assert "second half 9931" in held[-1]["content"] and held[-1]["content"] != "second half 9931"
+
+ assert _compress(agent, held, "").status == "compressed"
+
+ contents = [m["content"] for m in session_db.get_messages_as_conversation("sid")]
+ assert sum("second half 9931" in c for c in contents) == 1