fix(gateway): real user text stays clean in the transcript on resume-pending turns (#86580)

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.
This commit is contained in:
Teknium
2026-08-18 16:37:25 -07:00
parent 0cc26777bb
commit c69b6471e6
2 changed files with 37 additions and 5 deletions
+13 -5
View File
@@ -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
@@ -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