From c69b6471e677d7ff23b5cef0cbc2924e900b6453 Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Tue, 18 Aug 2026 16:37:25 -0700 Subject: [PATCH] fix(gateway): real user text stays clean in the transcript on resume-pending turns (#86580) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The salvaged fix persisted the recovery note unconditionally — correct for the synthesized empty auto-resume turn, but a user who typed real text while resume was pending would get the [System note: ...] scaffold persisted as their own words, leaking scaffolding into the durable transcript (the same class as #81841 on the assistant side). _prepare_resume_pending_message now persists the note only when the original message is blank; real text persists verbatim while the model still receives the wrapped note. Whitespace-only counts as blank. Tests cover all three shapes. --- gateway/run.py | 18 +++++++++++---- tests/gateway/test_restart_resume_pending.py | 24 ++++++++++++++++++++ 2 files changed, 37 insertions(+), 5 deletions(-) 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