Skip to content

The agent that grows with you

fix(compression): publish the /compress here N tail with a rotated child

kshitijk4poorb75c29d9b4ac
diff --git a/agent/conversation_compression.py b/agent/conversation_compression.py
index 2abee76f4fb..f09d07e7b4e 100644
--- a/agent/conversation_compression.py
+++ b/agent/conversation_compression.py
@@ -3721,7 +3721,7 @@ def _commit_compaction(
Failures roll the live list back and arm the split-failure cooldown; a refused (would-grow) candidate returns
``refused_prompt`` so the caller hands back the input unchanged. ``verbatim_tail`` (``/compress here N``) is
- re-inserted after the compacted head by the in-place commit and stamped once durable; rotation ignores it.
+ re-inserted after the compacted head by either commit and stamped once durable.
"""
session_commit_succeeded = False
compacted_in_place = False
@@ -3826,10 +3826,18 @@ def _commit_compaction(
# rollback off this name, so anything that fails from here on rolls the transcript back
# instead of leaving the failed attempt's compacted snapshot in place.
old_session_id = agent.session_id
+ if verbatim_tail:
+ # Publish the kept exchanges with the head, as the in-place branch stores them: the
+ # gateway no longer rewrites a published child, so a head-only handoff loses the tail.
+ from hermes_cli.partial_compress import rejoin_compressed_head_and_tail
+ compressed = rejoin_compressed_head_and_tail(compressed, verbatim_tail)
_publish_rotated_compaction(
agent, messages, compressed, new_system_prompt=new_system_prompt, lease=lease,
old_session_id=old_session_id, compressed_user_turn_outcome=compressed_user_turn_outcome,
)
+ if verbatim_tail:
+ from agent.context_compressor import stamp_db_persisted_markers
+ stamp_db_persisted_markers(verbatim_tail)
split_status = "rotated_committed"
agent._last_flushed_db_idx = len(compressed)
agent._flushed_db_message_session_id = agent.session_id
diff --git a/agent/conversation_compression_manual.py b/agent/conversation_compression_manual.py
index 658cafc6fb8..8b8ef36e2a6 100644
--- a/agent/conversation_compression_manual.py
+++ b/agent/conversation_compression_manual.py
@@ -141,8 +141,8 @@ def compress_now(
finalize_context_engine_compression_notification(agent, committed=False)
return CompressResult("lock_skipped", before, before, before_tokens, before_tokens, request,
lock_holder=lock_signal if isinstance(lock_signal, str) else None)
- # Stamped copies mean the in-place commit stored the tail and already returned head + tail. Rotation, a
- # no-op or a rolled-back commit leave them unstamped, and the tail is then only in the caller's dicts.
+ # Stamped copies mean the commit (in place or rotated) stored the tail and already returned head + tail.
+ # A no-op or a rolled-back commit leaves them unstamped, and the tail is then only in the caller's dicts.
if tail and not all(row.get(_DB_PERSISTED_MARKER) is True for row in tail_rows):
compressed = rejoin_compressed_head_and_tail(compressed, tail)
after_tokens = estimate_request_tokens(agent, compressed)
diff --git a/tests/agent/test_conversation_compression_manual.py b/tests/agent/test_conversation_compression_manual.py
index 6ff7a095afc..f515667c001 100644
--- a/tests/agent/test_conversation_compression_manual.py
+++ b/tests/agent/test_conversation_compression_manual.py
@@ -200,6 +200,20 @@ def test_in_place_here_n_stores_the_kept_exchanges(session_db):
assert _live(session_db.get_messages_as_conversation("sid")) == _live(next_turn)
+def test_rotated_here_n_publishes_the_kept_exchanges(session_db):
+ """Rotation publishes the child atomically and the gateway no longer rewrites it, so the handoff itself must
+ carry the kept tail: a head-only child loses the exchanges the user asked to keep on the next resume."""
+ history = _exchanges(10)
+ agent, loaded = _stored_agent(session_db, history)
+ agent.compression_in_place = False
+ result = _compress_here(agent, loaded, 2)
+ assert result.status == "compressed" and agent.session_id != "sid"
+
+ durable = session_db.get_messages_as_conversation(agent.session_id)
+ assert _live(durable) == _live(result.after_messages)
+ assert _live(durable[-4:]) == _live(history[-4:])
+
+
def test_in_place_here_n_folds_the_seam_once(session_db):
"""A head ending on a user turn folds the tail's first message into it; the stored transcript carries the
same fold, and the tail is not rejoined a second time."""