The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): make retry archive preservation fail-safe
poisdahl30c1421acfb3
diff --git a/gateway/session.py b/gateway/session.pyindex 83dad95befa..d0e96461d6f 100644--- a/gateway/session.py+++ b/gateway/session.py@@ -3362,22 +3362,6 @@ class SessionStore:logger.debug("has_platform_message_id lookup failed", exc_info=True)return False- def has_archived_messages(self, session_id: str) -> bool:- """Check if a session has any soft-archived (``active = 0``) rows.-- Thin wrapper over SessionDB.has_archived_messages(). Returns False- when no DB is available (in-memory sessions). Used by transcript- rewrite callers (e.g. /retry) to decide whether the rewrite must- pass ``active_only=True`` so archived compaction history survives.- """- if not self._db:- return False- try:- return self._db.has_archived_messages(session_id)- except Exception:- logger.debug("has_archived_messages lookup failed", exc_info=True)- return False-def rewrite_transcript(self,session_id: str,diff --git a/gateway/slash_commands.py b/gateway/slash_commands.pyindex 5305afc6508..08202a8eef1 100644--- a/gateway/slash_commands.py+++ b/gateway/slash_commands.py@@ -2582,18 +2582,15 @@ class GatewaySlashCommandsMixin:if not last_user_msg:return t("gateway.retry.no_previous")- # Truncate history to before the last user message and persist. Probe- # for soft-archived rows first: after an in-place compaction the- # pre-compaction transcript lives on as active=0/compacted=1 rows- # under this same session id, and a bare rewrite (active_only=False)- # would DELETE them (same class as #61145; ACP _persist and TUI- # prompt.submit already guard their rewrite paths).+ # Truncate history to before the last user message and persist only the+ # live view. After in-place compaction the pre-compaction transcript+ # lives on as active=0/compacted=1 rows under this same session id, and+ # a bare rewrite (active_only=False) would DELETE them (same class as+ # #61145). /retry never intends to purge archived history, so avoid a+ # separate existence probe: it could fail open or race with the write.truncated = history[:last_user_idx]- has_archived = await self.async_session_store.has_archived_messages(- session_entry.session_id- )await self.async_session_store.rewrite_transcript(- session_entry.session_id, truncated, active_only=has_archived+ session_entry.session_id, truncated, active_only=True)# Reset stored token count — transcript was truncatedsession_entry.last_prompt_tokens = 0diff --git a/tests/gateway/test_retry_replacement.py b/tests/gateway/test_retry_replacement.pyindex da219a7ef56..c83fafbe46d 100644--- a/tests/gateway/test_retry_replacement.py+++ b/tests/gateway/test_retry_replacement.py@@ -68,8 +68,10 @@ async def test_gateway_retry_replaces_last_user_turn_in_transcript(tmp_path, mon@pytest.mark.asyncio-async def test_gateway_retry_preserves_archived_compaction_rows(tmp_path, monkeypatch):- """/retry must not DELETE soft-archived compaction history.+async def test_gateway_retry_preserves_archived_compaction_rows_when_probe_fails(+ tmp_path, monkeypatch+):+ """/retry must not DELETE archives when an existence probe would fail.With compression.in_place (the default, #38763) archive_and_compact()keeps the pre-compaction transcript on disk as active=0/compacted=1 rows@@ -77,9 +79,9 @@ async def test_gateway_retry_preserves_archived_compaction_rows(tmp_path, monkeybare rewrite_transcript(), whose replace_messages(active_only=False)DELETEs every row for the session and reinserts only the truncated livetail, wiping the archived history permanently (same class as #61145;- #57803 named this call site as a residual gap). The handler must probe- has_archived_messages() and pass active_only=True so only the live rows- are replaced.+ #57803 named this call site as a residual gap). /retry never intends to+ purge archived history, so it must pass active_only=True unconditionally:+ a separate existence probe can fail open or race with the rewrite."""import hermes_statemonkeypatch.setattr(hermes_state, "DEFAULT_DB_PATH", tmp_path / "state.db")@@ -104,6 +106,11 @@ async def test_gateway_retry_preserves_archived_compaction_rows(tmp_path, monkey)assert store._db.has_archived_messages(session_id) is True+ # A failed preflight lookup must not turn this data-preservation path back+ # into a destructive full-history rewrite. The write itself still works.+ archived_probe = MagicMock(side_effect=OSError("transient archive lookup failure"))+ monkeypatch.setattr(store._db, "has_archived_messages", archived_probe)+gw = GatewayRunner.__new__(GatewayRunner)gw.config = configgw.session_store = store@@ -125,6 +132,7 @@ async def test_gateway_retry_preserves_archived_compaction_rows(tmp_path, monkey)assert result == "new answer"+ archived_probe.assert_not_called()# The archived pre-compaction rows survive the rewrite untouched.archived = [m for m in store._db.get_messages(session_id, include_inactive=True)@@ -141,5 +149,3 @@ async def test_gateway_retry_preserves_archived_compaction_rows(tmp_path, monkey"first question","retry me",]--