DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
fix(session): replay saved request on failed turn resend
Bingxi Zhao (Frank)5868e8acfadd
diff --git a/deeptutor/core/turn_request.py b/deeptutor/core/turn_request.pyindex a9a43680a..a34afd2bb 100644--- a/deeptutor/core/turn_request.py+++ b/deeptutor/core/turn_request.py@@ -169,7 +169,9 @@ class TurnRequest(BaseModel):course_id: str | None = Nonepersist_user_message: bool = Trueregenerate: bool = False- regenerated_from_message_id: int | None = None+ # SQLite message rowids are integers; PocketBase message record ids are+ # opaque strings. Preserve either form in the SESSION event for clients.+ regenerated_from_message_id: int | str | None = Nonesuperseded_turn_id: str | None = Nonefollowup_question_context: dict[str, Any] | None = Noneselection_tutor_context: dict[str, Any] | None = Nonediff --git a/deeptutor/services/session/_turn_runtime_shared.py b/deeptutor/services/session/_turn_runtime_shared.pyindex d0aacab62..d1fe65140 100644--- a/deeptutor/services/session/_turn_runtime_shared.py+++ b/deeptutor/services/session/_turn_runtime_shared.py@@ -883,7 +883,29 @@ def _request_snapshot_metadata("enabledTools": _string_list(payload.get("tools")),"knowledgeBases": _string_list(payload.get("knowledge_bases")),"language": str(payload.get("language", "en") or "en"),+ # Keep empty values too. A failed turn can be resent after the+ # conversation preferences have changed; absence would otherwise+ # cause the retry to pick up the newer tools, sources, or persona.+ "config": dict(config),+ "notebookReferences": list(notebook_references),+ "historyReferences": list(history_references),+ "partnerGroupReferences": list(partner_group_references),+ "questionNotebookReferences": list(question_notebook_references),+ "bookReferences": list(book_references),+ "readingReferences": list(reading_references),+ "memoryReferences": list(memory_references),+ "skills": _string_list(payload.get("skills")),+ "mcp": _string_list(payload.get("mcp")),+ "persona": persona,}+ for payload_key, snapshot_key in (+ ("workspace_id", "workspaceId"),+ ("course_id", "courseId"),+ ("mastery_session_mode", "masterySessionMode"),+ ("auto_route", "autoRoute"),+ ):+ if payload_key in payload:+ snapshot[snapshot_key] = payload[payload_key]for payload_key, snapshot_key in (("consult_partner_id", "consultPartnerId"),("partner_discussion_group_id", "partnerDiscussionGroupId"),@@ -891,30 +913,14 @@ def _request_snapshot_metadata(if payload_key in payload:snapshot[snapshot_key] = payload[payload_key]workspace_mode = _workspace_mode(payload.get("workspace_mode"), capability=capability)- if workspace_mode:- snapshot["workspaceMode"] = workspace_mode+ snapshot["workspaceMode"] = workspace_modeif attachments:snapshot["attachments"] = attachments- if config:- snapshot["config"] = dict(config)capability_route = payload.get("capability_route")if isinstance(capability_route, dict):snapshot["capabilityRoute"] = dict(capability_route)- if notebook_references:- snapshot["notebookReferences"] = notebook_references- if history_references:- snapshot["historyReferences"] = history_references- if partner_group_references:- snapshot["partnerGroupReferences"] = partner_group_references- if question_notebook_references:- snapshot["questionNotebookReferences"] = question_notebook_references- if book_references:- snapshot["bookReferences"] = book_references- if reading_references:- snapshot["readingReferences"] = list(reading_references)mastery_path_id = _mastery_path_id(payload.get("mastery_path_id"))- if mastery_path_id:- snapshot["masteryPathId"] = mastery_path_id+ snapshot["masteryPathId"] = mastery_path_id# Persisted so a regenerate re-runs with the same document open. Without it# the reading capability would be inactive on the retry and the answer would# silently lose its grounding.@@ -927,15 +933,10 @@ def _request_snapshot_metadata(if reading_material_revision is not None:snapshot["readingMaterialRevision"] = reading_material_revisionreading_workspace_id = _reading_workspace_id(payload.get("reading_workspace_id"))- if reading_workspace_id:- snapshot["readingWorkspaceId"] = reading_workspace_id+ snapshot["readingWorkspaceId"] = reading_workspace_idtimed_media_id = _timed_media_id(payload.get("timed_media_id"))if timed_media_id:snapshot["timedMediaId"] = timed_media_id- if persona:- snapshot["persona"] = persona- if memory_references:- snapshot["memoryReferences"] = memory_referencesif llm_selection:snapshot["llmSelection"] = llm_selectionreturn {"request_snapshot": snapshot}diff --git a/deeptutor/services/session/turns/request_preparer.py b/deeptutor/services/session/turns/request_preparer.pyindex f78dca46c..9b8494a7a 100644--- a/deeptutor/services/session/turns/request_preparer.py+++ b/deeptutor/services/session/turns/request_preparer.py@@ -42,6 +42,41 @@ if TYPE_CHECKING:from deeptutor.services.session.protocol import SessionStoreProtocol+# Request snapshots use the browser's display keys. Only these saved turn+# inputs are restored by Resend; runtime bookkeeping and message identity are+# always assembled from the persisted message and current session instead.+_REPLAY_SNAPSHOT_FIELDS: dict[str, str] = {+ "capability": "capability",+ "tools": "enabledTools",+ "knowledge_bases": "knowledgeBases",+ "language": "language",+ "config": "config",+ "notebook_references": "notebookReferences",+ "history_references": "historyReferences",+ "partner_group_references": "partnerGroupReferences",+ "question_notebook_references": "questionNotebookReferences",+ "book_references": "bookReferences",+ "reading_references": "readingReferences",+ "memory_references": "memoryReferences",+ "skills": "skills",+ "mcp": "mcp",+ "persona": "persona",+ "llm_selection": "llmSelection",+ "workspace_mode": "workspaceMode",+ "workspace_id": "workspaceId",+ "course_id": "courseId",+ "mastery_path_id": "masteryPathId",+ "mastery_session_mode": "masterySessionMode",+ "reading_material_id": "readingMaterialId",+ "reading_material_revision": "readingMaterialRevision",+ "reading_workspace_id": "readingWorkspaceId",+ "timed_media_id": "timedMediaId",+ "consult_partner_id": "consultPartnerId",+ "partner_discussion_group_id": "partnerDiscussionGroupId",+ "auto_route": "autoRoute",+}++class TurnRequestPreparer:if TYPE_CHECKING:store: SessionStoreProtocol@@ -759,6 +794,11 @@ class TurnRequestPreparer:turn with ``persist_user_message=False`` and ``regenerate=True`` sothe runtime knows not to duplicate the user row or refresh long-termmemory a second time. The original user message stays in place.++ ``overrides.replay_snapshot`` opts into restoring the saved per-turn+ request for a persisted failed-turn Resend. Without it, Regenerate+ retains its session-preference behavior. Explicit overrides win over+ saved fields, including empty lists and strings."""session_id = str(session_id or "").strip()if not session_id:@@ -786,7 +826,8 @@ class TurnRequestPreparer:breakpreferences = session.get("preferences") or {}- overrides = overrides or {}+ overrides = dict(overrides or {})+ replay_snapshot = overrides.pop("replay_snapshot", False) is Truesnapshot = {}metadata = last_user.get("metadata") or {}if isinstance(metadata, dict):@@ -941,13 +982,25 @@ class TurnRequestPreparer:**consultation_fields,"persist_user_message": False,"regenerate": True,- "regenerated_from_message_id": int(last_user["id"]),+ "regenerated_from_message_id": last_user["id"],}if previous_turn_id:payload["superseded_turn_id"] = previous_turn_idif llm_selection:payload["llm_selection"] = llm_selection+ if replay_snapshot:+ # The ordinary Regenerate path intentionally uses current session+ # preferences for many fields. Resend instead repeats the failed+ # message's saved request. Keys absent from an older snapshot keep+ # the legacy fallback, while explicit overrides always take+ # precedence, even when they clear a field.+ for field, snapshot_key in _REPLAY_SNAPSHOT_FIELDS.items():+ if field in overrides:+ payload[field] = overrides[field]+ elif snapshot_key in snapshot:+ payload[field] = snapshot[snapshot_key]+# Validate the complete replay request before removing the answer it# supersedes. A malformed stored snapshot or override must leave the# visible conversation intact so the learner can retry safely.diff --git a/tests/api/test_unified_ws_turn_runtime.py b/tests/api/test_unified_ws_turn_runtime.pyindex 4c37e0d74..517fcf819 100644--- a/tests/api/test_unified_ws_turn_runtime.py+++ b/tests/api/test_unified_ws_turn_runtime.py@@ -585,7 +585,12 @@ async def test_turn_runtime_session_persona_persists_falls_back_and_clears(await run_turn(session["id"], {"persona": ""})detail = await store.get_session_with_messages(session["id"])assert detail["preferences"]["persona"] == ""- assert "persona" not in detail["messages"][4]["metadata"]["request_snapshot"]+ cleared_snapshot = detail["messages"][4]["metadata"]["request_snapshot"]+ assert cleared_snapshot["persona"] == ""+ assert cleared_snapshot["config"] == {}+ assert cleared_snapshot["enabledTools"] == []+ assert cleared_snapshot["knowledgeBases"] == []+ assert cleared_snapshot["memoryReferences"] == []@pytest.mark.asynciodiff --git a/tests/services/session/test_regenerate.py b/tests/services/session/test_regenerate.pyindex 565435540..343d6f2ad 100644--- a/tests/services/session/test_regenerate.py+++ b/tests/services/session/test_regenerate.py@@ -205,8 +205,9 @@ 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}+ @pytest.mark.parametrize("replay_snapshot", [False, True])def test_replays_rich_attachment_payload_without_breaking_turn_request(- self, store: SQLiteSessionStore+ self, store: SQLiteSessionStore, replay_snapshot: bool) -> None:"""A turn whose persisted attachment carries the rich fields stored atupload (id, extracted_chars, extracted_text) must still produce a@@ -244,7 +245,12 @@ class TestRegenerateLastTurn:runtime = TurnRuntimeManager(store=store)recorder = _FakeStartTurnRecorder()with patch.object(runtime, "start_turn", new=recorder):- asyncio.run(runtime.regenerate_last_turn(sid))+ asyncio.run(+ runtime.regenerate_last_turn(+ sid,+ overrides={"replay_snapshot": True} if replay_snapshot else None,+ )+ )from deeptutor.core.turn_request import TurnRequest@@ -263,6 +269,155 @@ class TestRegenerateLastTurn:}]+ @pytest.mark.parametrize("replay_snapshot", [False, True])+ def test_saved_request_is_opt_in_and_explicit_clears_win(+ self, store: SQLiteSessionStore, replay_snapshot: bool+ ) -> None:+ snapshot = {+ "capability": "deep_solve",+ "enabledTools": ["web_search"],+ "knowledgeBases": ["old-kb"],+ "language": "zh",+ "config": {"steps": 3},+ "notebookReferences": [{"notebook_id": "notebook-1", "record_ids": ["r1"]}],+ "historyReferences": ["earlier-session"],+ "partnerGroupReferences": [{"group_id": "group-1", "session_key": "s1"}],+ "questionNotebookReferences": [42],+ "bookReferences": [{"book_id": "book-1", "page_ids": ["p1"]}],+ "readingReferences": [+ {"material_id": "0123456789abcdef", "revision": 2, "locators": [1]}+ ],+ "memoryReferences": ["summary"],+ "skills": [],+ "mcp": ["server-1"],+ "persona": "",+ "llmSelection": {"profile_id": "old-profile", "model_id": "old-model"},+ "workspaceMode": "",+ "courseId": "",+ "masteryPathId": "",+ "masterySessionMode": "study",+ "autoRoute": False,+ }+ sid, _, _ = _seed_session(store, user_metadata={"request_snapshot": snapshot})+ asyncio.run(+ store.update_session_preferences(+ sid,+ {+ "capability": "chat",+ "tools": ["brainstorm"],+ "knowledge_bases": ["new-kb"],+ "language": "en",+ "skills": ["current-skill"],+ "mcp": [],+ "persona": "current-persona",+ },+ )+ )+ runtime = TurnRuntimeManager(store=store)+ recorder = _FakeStartTurnRecorder()+ overrides = {"replay_snapshot": True} if replay_snapshot else None+ with patch.object(runtime, "start_turn", new=recorder):+ asyncio.run(runtime.regenerate_last_turn(sid, overrides=overrides))++ payload = recorder.calls[0]+ assert "replay_snapshot" not in payload+ if not replay_snapshot:+ assert payload["capability"] == "chat"+ assert payload["tools"] == ["brainstorm"]+ assert payload["knowledge_bases"] == ["new-kb"]+ assert payload["language"] == "en"+ assert payload["config"] == {}+ assert "persona" not in payload+ return++ assert payload["capability"] == "deep_solve"+ assert payload["tools"] == ["web_search"]+ assert payload["knowledge_bases"] == ["old-kb"]+ assert payload["language"] == "zh"+ assert payload["config"] == {"steps": 3}+ assert payload["notebook_references"] == snapshot["notebookReferences"]+ assert payload["history_references"] == snapshot["historyReferences"]+ assert payload["partner_group_references"] == snapshot["partnerGroupReferences"]+ assert payload["question_notebook_references"] == [42]+ assert payload["book_references"] == snapshot["bookReferences"]+ assert payload["reading_references"] == snapshot["readingReferences"]+ assert payload["memory_references"] == ["summary"]+ assert payload["skills"] == []+ assert payload["mcp"] == ["server-1"]+ assert payload["persona"] == ""+ assert payload["llm_selection"] == snapshot["llmSelection"]+ assert payload["workspace_mode"] == ""+ assert payload["course_id"] == ""+ assert payload["mastery_path_id"] == ""+ assert payload["mastery_session_mode"] == "study"+ assert payload["auto_route"] is False++ def test_resend_overrides_saved_fields_including_empty_values(+ self, store: SQLiteSessionStore+ ) -> None:+ sid, _, _ = _seed_session(+ store,+ user_metadata={+ "request_snapshot": {+ "enabledTools": ["web_search"],+ "knowledgeBases": ["old-kb"],+ "language": "zh",+ "config": {"steps": 3},+ "memoryReferences": ["summary"],+ "persona": "socratic",+ "llmSelection": {"profile_id": "old-profile", "model_id": "old-model"},+ }+ },+ )+ runtime = TurnRuntimeManager(store=store)+ recorder = _FakeStartTurnRecorder()+ with patch.object(runtime, "start_turn", new=recorder):+ asyncio.run(+ runtime.regenerate_last_turn(+ sid,+ overrides={+ "replay_snapshot": True,+ "tools": [],+ "knowledge_bases": [],+ "language": "fr",+ "config": {},+ "memory_references": [],+ "persona": "",+ "llm_selection": {"profile_id": "new-profile", "model_id": "new-model"},+ },+ )+ )+ payload = recorder.calls[0]+ assert payload["tools"] == []+ assert payload["knowledge_bases"] == []+ assert payload["language"] == "fr"+ assert payload["config"] == {}+ assert payload["memory_references"] == []+ assert payload["persona"] == ""+ assert payload["llm_selection"] == {+ "profile_id": "new-profile",+ "model_id": "new-model",+ }++ def test_regenerate_preserves_pocketbase_message_id(self, store: SQLiteSessionStore) -> None:+ sid, _, _ = _seed_session(store, assistant_content=None)+ runtime = TurnRuntimeManager(store=store)+ recorder = _FakeStartTurnRecorder()++ async def last_message(_session_id: str, role: str | None = None):+ if role == "user":+ return {"id": "pbRecord123abc", "role": "user", "content": "try again"}+ return None++ with (+ patch.object(store, "get_last_message", new=last_message),+ patch.object(runtime, "start_turn", new=recorder),+ ):+ asyncio.run(runtime.regenerate_last_turn(sid, overrides={"replay_snapshot": True}))++ payload = recorder.calls[0]+ assert payload["regenerated_from_message_id"] == "pbRecord123abc"+def test_validation_failure_preserves_existing_assistant_message(self, store: SQLiteSessionStore) -> None: