From 0879d5cc8f3a257e2607936ac1e4aebe7be37220 Mon Sep 17 00:00:00 2001 From: teknium1 <127238744+teknium1@users.noreply.github.com> Date: Thu, 18 Jun 2026 11:35:43 -0700 Subject: [PATCH] fix(gateway): preserve original transcript when /compress rotation is skipped MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The manual /compress handler called rewrite_transcript() unconditionally on the session id returned by _compress_context(). When rotation does not occur (e.g. _session_db unavailable, or the DB split raised), session_id is unchanged and rewrite_transcript() DELETEs the original messages and replaces them with only the compressed summary — permanent data loss (#44794, #39704). Guard the rewrite on actual rotation: only overwrite when _compress_context produced a new session id. Otherwise leave the original transcript intact and log a warning. --- gateway/slash_commands.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/gateway/slash_commands.py b/gateway/slash_commands.py index bfcef143f44..04c3f4ca89f 100644 --- a/gateway/slash_commands.py +++ b/gateway/slash_commands.py @@ -2588,14 +2588,29 @@ class GatewaySlashCommandsMixin: # session_id for the continuation. Write the compressed messages # into the NEW session so the original history stays searchable. new_session_id = tmp_agent.session_id - if new_session_id != session_entry.session_id: + rotated = new_session_id != session_entry.session_id + if rotated: session_entry.session_id = new_session_id self.session_store._save() self._sync_telegram_topic_binding( source, session_entry, reason="compress-command", ) - self.session_store.rewrite_transcript(new_session_id, compressed) + # Only rewrite the transcript when rotation actually produced a + # NEW session id. If _compress_context could not rotate (e.g. + # _session_db unavailable, or the DB split raised), session_id + # is unchanged and rewrite_transcript() would DELETE the + # original messages and replace them with only the compressed + # summary — permanent data loss (#44794, #39704). In that case + # leave the original transcript intact. + if rotated: + self.session_store.rewrite_transcript(new_session_id, compressed) + else: + logger.warning( + "Manual /compress: session rotation did not occur " + "(session_id unchanged) — preserving original transcript " + "instead of overwriting it (#44794)." + ) # Reset stored token count — transcript changed, old value is stale self.session_store.update_session( session_entry.session_key, last_prompt_tokens=0