Skip to content

The agent that grows with you

fix(compression): bound the rotation tail clone below the rotator's own flush

Teknium406c5daf04b0
diff --git a/agent/conversation_compression.py b/agent/conversation_compression.py
index bef36be7754..a910a05c5f4 100644
--- a/agent/conversation_compression.py
+++ b/agent/conversation_compression.py
@@ -3483,6 +3483,23 @@ def compress_context(
and 0 <= current_idx <= len(messages)
else None
)
+ # Foreign-tail ceiling (#75316): the flush below writes OUR
+ # OWN input transcript to the parent — those rows are
+ # already represented in the compacted handoff and must
+ # not be cloned into the child. Everything at or below
+ # this MAX(id) but above the start-watermark is a foreign
+ # concurrent append; everything above it is our flush.
+ try:
+ _foreign_tail_ceiling = (
+ agent._session_db.get_active_message_watermark(
+ agent.session_id
+ )
+ )
+ except Exception:
+ # Without a trustworthy ceiling the clone could
+ # duplicate the handoff — fall back to historical
+ # behavior (no tail preservation this rotation).
+ _foreign_tail_ceiling = None
try:
agent._flush_messages_to_session_db(
messages,
@@ -3524,7 +3541,12 @@ def compress_context(
profile_name=_profile_for_child,
compression_lock_holder=_lock_holder,
require_compression_lease=_lock_holder is not None,
- watermark=_commit_watermark,
+ watermark=(
+ _commit_watermark
+ if _foreign_tail_ceiling is not None
+ else None
+ ),
+ watermark_ceiling=_foreign_tail_ceiling,
)
agent.session_id = new_session_id
try:
diff --git a/hermes_state.py b/hermes_state.py
index b40537a0bdb..68011285919 100644
--- a/hermes_state.py
+++ b/hermes_state.py
@@ -5591,6 +5591,7 @@ class SessionDB(SessionSearchMixin, SessionSchemaMixin, SessionPortabilityMixin)
compression_lock_holder: str = None,
require_compression_lease: bool = True,
watermark: Optional[int] = None,
+ watermark_ceiling: Optional[int] = None,
) -> None:
"""Atomically close a parent and publish its durable compression child.
@@ -5605,6 +5606,14 @@ class SessionDB(SessionSearchMixin, SessionSchemaMixin, SessionPortabilityMixin)
same pure-SQL column clone as :meth:`archive_and_compact`, with the
session id rewritten — so a mid-compression append survives rotation
instead of stranding in the closed parent.
+
+ *watermark_ceiling* bounds the clone from above: the rotation path
+ flushes its OWN un-persisted input transcript to the parent right
+ before publishing (#47202), and those rows are already represented in
+ the compacted handoff — cloning them would duplicate the transcript.
+ The caller captures ``MAX(id)`` immediately BEFORE that flush; only
+ rows in ``(watermark, watermark_ceiling]`` are foreign concurrent
+ tail. ``None`` = unbounded (no internal flush happened).
"""
def _do(conn):
lock_row = conn.execute(
@@ -5674,13 +5683,19 @@ class SessionDB(SessionSearchMixin, SessionSchemaMixin, SessionPortabilityMixin)
)
if watermark is not None:
# Clone the parent's concurrent tail (rows landed after the
- # watermark) into the child, after the handoff. Column-exact
- # except id/session_id; originals stay in the (closed) parent
- # for lineage recovery.
+ # watermark, at or below the ceiling — see docstring) into the
+ # child, after the handoff. Column-exact except id/session_id;
+ # originals stay in the (closed) parent for lineage recovery.
+ _ceiling_clause = ""
+ _params: list = [parent_session_id, int(watermark)]
+ if watermark_ceiling is not None:
+ _ceiling_clause = " AND id <= ?"
+ _params.append(int(watermark_ceiling))
tail_rows = conn.execute(
"SELECT id, tool_calls FROM messages "
- "WHERE session_id = ? AND active = 1 AND id > ? ORDER BY id",
- (parent_session_id, int(watermark)),
+ "WHERE session_id = ? AND active = 1 AND id > ?"
+ f"{_ceiling_clause} ORDER BY id",
+ _params,
).fetchall()
if tail_rows:
tail_ids = [int(r["id"]) for r in tail_rows]
diff --git a/tests/test_compression_watermark_commit.py b/tests/test_compression_watermark_commit.py
index 5d91f331433..c7e677fbc2d 100644
--- a/tests/test_compression_watermark_commit.py
+++ b/tests/test_compression_watermark_commit.py
@@ -238,6 +238,9 @@ class TestRotationPathWatermark:
watermark = db.get_active_message_watermark("sess1")
assert db.try_acquire_compression_lock("sess1", "rotator") is True
db.append_message("sess1", role="user", content="mid-rotation steer")
+ # Ceiling captured AFTER the foreign append, BEFORE the rotation
+ # path's own pre-publish flush (which this test has none of).
+ ceiling = db.get_active_message_watermark("sess1")
db.publish_compression_child(
parent_session_id="sess1",
@@ -247,6 +250,7 @@ class TestRotationPathWatermark:
compression_lock_holder="rotator",
require_compression_lease=True,
watermark=watermark,
+ watermark_ceiling=ceiling,
)
child = db.get_messages_as_conversation("child1")
@@ -261,6 +265,37 @@ class TestRotationPathWatermark:
parent_info = db.get_session("sess1")
assert parent_info["end_reason"] == "compression"
+ def test_ceiling_excludes_the_rotators_own_flush(self, db: SessionDB) -> None:
+ """Rows the rotation path flushes AFTER the ceiling (its own input
+ transcript, already inside the handoff) must NOT be cloned."""
+ _seed(db)
+ watermark = db.get_active_message_watermark("sess1")
+ assert db.try_acquire_compression_lock("sess1", "rotator") is True
+ db.append_message("sess1", role="user", content="foreign steer")
+ ceiling = db.get_active_message_watermark("sess1")
+ # Simulates the #47202 pre-publish flush of the rotator's own input.
+ db.append_message(
+ "sess1", role="user", content="rotator's own flush",
+ compression_lock_holder="rotator",
+ )
+
+ db.publish_compression_child(
+ parent_session_id="sess1",
+ child_session_id="child1",
+ source="test",
+ messages=SUMMARY,
+ compression_lock_holder="rotator",
+ require_compression_lease=True,
+ watermark=watermark,
+ watermark_ceiling=ceiling,
+ )
+
+ child_contents = [
+ m["content"] for m in db.get_messages_as_conversation("child1")
+ ]
+ assert "foreign steer" in child_contents
+ assert "rotator's own flush" not in child_contents
+
def test_no_watermark_keeps_historical_rotation(self, db: SessionDB) -> None:
_seed(db)
assert db.try_acquire_compression_lock("sess1", "rotator") is True