Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

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

ZQR1101c9d9c8a97329
diff --git a/deeptutor/services/session/turns/request_preparer.py b/deeptutor/services/session/turns/request_preparer.py
index 6e2cfd9b2..28f9d5491 100644
--- a/deeptutor/services/session/turns/request_preparer.py
+++ b/deeptutor/services/session/turns/request_preparer.py
@@ -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
diff --git a/tests/services/session/test_regenerate.py b/tests/services/session/test_regenerate.py
index 803f51165..162519d23 100644
--- a/tests/services/session/test_regenerate.py
+++ b/tests/services/session/test_regenerate.py
@@ -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,