diff --git a/gateway/run.py b/gateway/run.py index ad7824ffc3..07952e2085 100644 --- a/gateway/run.py +++ b/gateway/run.py @@ -1234,16 +1234,24 @@ def _prepare_resume_pending_message( *, interactive: bool = True, ) -> tuple[str, str]: - """Return the recovery message and the identical persisted user text. + """Return the recovery message and the user text to persist. - Resume turns replace the empty startup event with a recovery note before - entering the agent. Persist that note too, rather than the original empty - event, so the durable transcript matches what the model received. + Resume turns replace the startup event's text with a recovery note before + entering the agent. When the original message is empty (the synthesized + auto-resume turn), persist the note too — persisting the empty string + left a blank user row in state.db that the pre-call sanitizer re-healed + on every later call forever (#86580). When the user sent REAL text while + the resume was pending, keep persisting their clean words: the transcript + stays scaffold-free (the model still receives the wrapped note), and a + non-empty row never trips the sanitizer. """ recovery_message = build_resume_recovery_note( reason, message or "", interactive=interactive, ) - return recovery_message, recovery_message + persist_message = ( + message if isinstance(message, str) and message.strip() else recovery_message + ) + return recovery_message, persist_message # Assistant-message fields that must survive transcript replay so multi-turn diff --git a/tests/gateway/test_restart_resume_pending.py b/tests/gateway/test_restart_resume_pending.py index 2f602faa21..d52b8176d8 100644 --- a/tests/gateway/test_restart_resume_pending.py +++ b/tests/gateway/test_restart_resume_pending.py @@ -326,6 +326,30 @@ class TestResumePendingSystemNote: assert persisted == message assert persisted != "" + def test_whitespace_only_message_also_persists_the_note(self): + """A whitespace-only startup event is as blank as an empty one — + persisting it verbatim would recreate the sanitizer loop (#86580).""" + message, persisted = _prepare_resume_pending_message( + "shutdown_timeout", " ", interactive=True + ) + + assert persisted == message + assert persisted.strip() + + def test_real_user_text_persists_clean_not_the_scaffolded_note(self): + """When the user typed real text while resume was pending, the durable + transcript keeps their clean words; only the MODEL sees the wrapped + recovery note (transcript stays scaffold-free).""" + message, persisted = _prepare_resume_pending_message( + "restart_timeout", "what were we doing?", interactive=True + ) + + assert persisted == "what were we doing?" + assert "[System note:" not in persisted + assert message != persisted + assert "what were we doing?" in message + assert "[System note:" in message + def test_resume_pending_fires_without_tool_tail(self): """Key improvement over PR #9934: the restart-resume note fires