fix(turns): regenerate a turn with attachments without breaking TurnRequest validation (#1484)

This commit is contained in:
ZQR1101
2026-09-16 14:46:50 +08:00
committed by Bingxi Zhao (Frank)
parent 087ec1e393
commit c9d9c8a973
2 changed files with 72 additions and 1 deletions

View File

@@ -863,7 +863,21 @@ class TurnRequestPreparer:
"skills": skills,
"mcp": mcp,
"language": language,
"attachments": list(last_user.get("attachments") or []),
"attachments": [
{
key: att.get(key)
for key in ("type", "url", "base64", "filename", "mime_type")
if isinstance(att, dict) and key in att
}
for att in (last_user.get("attachments") or [])
if isinstance(att, dict)
],
# The persisted message-row attachments carry upload-time extras
# (``id``, ``extracted_text``, ``extracted_chars``) that the wire
# contract ``OutgoingAttachment`` forbids; project to the allowed
# fields so start_turn's TurnRequest validation passes on retry
# (#1484 — otherwise the WS closed with no terminal event and the
# UI hung on "Thinking..." forever).
"notebook_references": list(
overrides.get("notebook_references")
if overrides.get("notebook_references") is not None

View File

@@ -205,6 +205,63 @@ class TestRegenerateLastTurn:
assert [m["id"] for m in remaining] == [user_id]
assert assistant_id is not None and assistant_id not in {m["id"] for m in remaining}
def test_replays_rich_attachment_payload_without_breaking_turn_request(
self, store: SQLiteSessionStore
) -> None:
"""A turn whose persisted attachment carries the rich fields stored at
upload (id, extracted_chars, extracted_text) must still produce a
valid TurnRequest on regenerate.
#1484: regenerate echoed the stored message-row attachments verbatim,
whose extra fields OutgoingAttachment forbids, so start_turn's
TurnRequest validation raised and the WS closed with no terminal
event — the UI hung on "Thinking..." forever.
"""
session = asyncio.run(store.create_session())
sid = session["id"]
asyncio.run(store.update_session_preferences(sid, {"capability": "chat", "language": "en"}))
asyncio.run(
store.add_message(
sid,
role="user",
content="summarize the pdf",
capability="chat",
attachments=[
{
"type": "file",
"url": "/files/attachments/s1/att-1/a.pdf",
"base64": "",
"filename": "a.pdf",
"mime_type": "application/pdf",
# Extra fields persisted by the executor at upload.
"id": "att-1",
"extracted_chars": 67619,
"extracted_text": "--- Page 1 --- body text",
}
],
)
)
runtime = TurnRuntimeManager(store=store)
recorder = _FakeStartTurnRecorder()
with patch.object(runtime, "start_turn", new=recorder):
asyncio.run(runtime.regenerate_last_turn(sid))
from deeptutor.core.turn_request import TurnRequest
payload = recorder.calls[0]
# The payload must validate against the wire contract...
TurnRequest.model_validate(payload)
# ...with the extra fields stripped to the allowed attachment shape.
assert payload["attachments"] == [
{
"type": "file",
"url": "/files/attachments/s1/att-1/a.pdf",
"base64": "",
"filename": "a.pdf",
"mime_type": "application/pdf",
}
]
def test_replays_book_references_from_request_snapshot(self, store: SQLiteSessionStore) -> None:
sid, _, _ = _seed_session(
store,