Skip to content

The agent that grows with you

refactor(gateway): one persist_rotated_compression_child for both rotation sites

kshitijk4poord6b47ec1361a
diff --git a/gateway/run_turn.py b/gateway/run_turn.py
index b9810e02567..28504ea99ec 100644
--- a/gateway/run_turn.py
+++ b/gateway/run_turn.py
@@ -1121,18 +1121,9 @@ class GatewayTurnMixin:
# conversation silently vanishes. Persist the child transcript first; only then rebind the live
# entry.
if _hyg_rotated:
- # Same false-failure guard as manual /compress: publish_compression_child() wrote the
- # handoff + cloned foreign tail atomically, so the durable child needs no second
- # rewrite. rewrite_transcript() here DELETEs those cloned rows (they are absent from
- # _compressed) and any DB hiccup used to fail the whole rotation fail-closed onto the
- # already-superseded parent. Probe fails open: False keeps the original rewrite path.
- if await self.async_session_store.is_published_compression_child(
- session_entry.session_id, _hyg_new_sid):
- _hyg_new_sid_persisted = True
- else:
- _hyg_new_sid_persisted = await self.async_session_store.rewrite_transcript(
- _hyg_new_sid, _compressed)
- if not _hyg_new_sid_persisted:
+ # Published child is already durable; a rewrite would drop rows cloned at publish.
+ if not await self.async_session_store.persist_rotated_compression_child(
+ session_entry.session_id, _hyg_new_sid, _compressed):
logger.error(
"Session hygiene: failed to persist compressed transcript for rotated session "
"%s → %s; keeping the live entry on the original session so the "
diff --git a/gateway/session_transcript.py b/gateway/session_transcript.py
index 8b8481a49cd..1d3c9f5e056 100644
--- a/gateway/session_transcript.py
+++ b/gateway/session_transcript.py
@@ -519,11 +519,10 @@ class SessionTranscriptMixin:
foreign-tail rows) in the SAME transaction that ended the parent with
``end_reason='compression'``, so the child transcript is already durable on disk.
- /compress and hygiene rewrite the rotated child AGAIN; when this returns True that second
- rewrite is redundant AND destructive (the in-memory handoff list lacks the rows cloned
- during publication) and its failure surfaces as a false "failed to persist compressed
- transcript" over an already-committed compression. Fails open (False) on any DB error so
- callers fall back to the rewrite guard exactly as before."""
+ Rewriting such a child is redundant AND destructive: the in-memory handoff lacks the rows
+ cloned during publication, and a failed rewrite reports a false "failed to persist
+ compressed transcript" over a committed compression. Fails open (False) on any DB error so
+ callers fall back to the rewrite."""
if not parent_session_id or not child_session_id:
return False
# The child id is not routed yet; the parent is, and it was ended in the same publish transaction.
@@ -541,6 +540,13 @@ class SessionTranscriptMixin:
logger.debug("published-compression-child probe failed for %s", child_session_id, exc_info=True)
return False
+ def persist_rotated_compression_child(
+ self, parent_session_id: str, child_session_id: str, messages: List[Dict[str, Any]]) -> bool:
+ """Make a rotated compression child durable before the live entry is repointed: a published
+ child already is, anything else gets *messages* written by the destructive rewrite."""
+ return (self.is_published_compression_child(parent_session_id, child_session_id)
+ or self.rewrite_transcript(child_session_id, messages))
+
def has_input_owner(self, session_id: str, owner: str) -> bool:
"""Find this accepted input on the canonical live continuation and its ancestors.
diff --git a/gateway/slash_commands_session.py b/gateway/slash_commands_session.py
index 15921d42ff5..8fe6c23e9e3 100644
--- a/gateway/slash_commands_session.py
+++ b/gateway/slash_commands_session.py
@@ -627,17 +627,10 @@ class GatewaySessionCommandsMixin:
archive; an unchanged id without in-place means rotation FAILED."""
new_session_id = tmp_agent.session_id
if new_session_id != session_entry.session_id:
- # Rotation path: _publish_rotated_compaction() already wrote the compressed handoff
- # (plus any foreign-tail rows cloned during the same transaction) into the NEW session
- # durably. A second rewrite is redundant and destructive — `compressed` lacks those
- # cloned rows — and its failure used to surface as a false "failed to persist
- # compressed transcript" even though the compression had committed. Skip the rewrite
- # when the durable child is verified; fail-open keeps the original guard.
- if not await self.async_session_store.is_published_compression_child(
- session_entry.session_id, new_session_id):
- if not await self.async_session_store.rewrite_transcript(new_session_id, compressed):
- raise RuntimeError(
- f"failed to persist compressed transcript for session {new_session_id}")
+ # Published child is already durable; a rewrite would drop rows cloned at publish.
+ if not await self.async_session_store.persist_rotated_compression_child(
+ session_entry.session_id, new_session_id, compressed):
+ raise RuntimeError(f"failed to persist compressed transcript for session {new_session_id}")
session_entry.session_id = new_session_id
await self.async_session_store._save()
await asyncio.to_thread(self._sync_telegram_topic_binding, source, session_entry,