DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
fix(session): place ask_user clarifications before assistant answer
SeleiXi213508b52653
diff --git a/deeptutor/services/session/context_builder.py b/deeptutor/services/session/context_builder.pyindex 8dd033c3e..058a5b93e 100644--- a/deeptutor/services/session/context_builder.py+++ b/deeptutor/services/session/context_builder.py@@ -65,13 +65,15 @@ def format_messages_as_transcript(messages: list[dict[str, Any]]) -> str:"system": "System",}for item in messages:+ # User clarifications answered an ask_user card *before* the assistant+ # answer was produced, so render them before the row's own content.+ clarification = extract_ask_user_clarifications(item)+ if clarification:+ lines.append(f"User: {clarification}")content = str(item.get("content", "") or "").strip()if content:role = role_map.get(str(item.get("role", "user")), "User")lines.append(f"{role}: {content}")- clarification = extract_ask_user_clarifications(item)- if clarification:- lines.append(f"User: {clarification}")return "\n\n".join(lines)@@ -173,11 +175,16 @@ class ContextBuilder:for item in messages:role = item.get("role")content = str(item.get("content", "") or "")- if role in {"user", "assistant"} and content.strip():- history.append({"role": role, "content": content})+ # Resolved ask_user clarifications happened *before* the assistant+ # produced this row's answer, so they must precede the row's own+ # content in history. Otherwise later turns see the answer first+ # and the user's clarification second, and the model wrongly+ # assumes the answer came before the user supplied the context.clarification = extract_ask_user_clarifications(item)if clarification:history.append({"role": "user", "content": clarification})+ if role in {"user", "assistant"} and content.strip():+ history.append({"role": role, "content": content})return historyasync def _append_event(diff --git a/tests/services/session/test_context_builder.py b/tests/services/session/test_context_builder.pyindex a6836ba59..533346a29 100644--- a/tests/services/session/test_context_builder.py+++ b/tests/services/session/test_context_builder.py@@ -296,8 +296,8 @@ class TestBuildHistory:history = ContextBuilder(store=MagicMock())._build_history("", [message])assert history == [- {"role": "assistant", "content": "I adapted the explanation."},{"role": "user", "content": clarification},+ {"role": "assistant", "content": "I adapted the explanation."},]