DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
fix(course): the recommendation is the answer, not a tool preamble
Bingxi Zhao (Frank)8070773747a7
diff --git a/deeptutor/agents/chat/agent_loop.py b/deeptutor/agents/chat/agent_loop.pyindex 6dd608f95..d3a0fb5dd 100644--- a/deeptutor/agents/chat/agent_loop.py+++ b/deeptutor/agents/chat/agent_loop.py@@ -457,11 +457,11 @@ class AgentLoop:)if final_override is not None:await self._discard_deferred_output(result)- if not self.context.metadata.get("_partner_group_formal_answer_published"):+ if not self.context.metadata.get("_capability_answer_published"):await self.pipeline._emit_protocol_fallback_final_response(self.stream, final_override)- self.context.metadata["_partner_group_formal_answer_published"] = True+ self.context.metadata["_capability_answer_published"] = Truereturn await self._finalize_finish(final_override,continued_answer_parts=continued_answer_parts,@@ -526,11 +526,11 @@ class AgentLoop:final_override = self.pipeline._capability_final_text_override(self.context, "")if final_override is not None:- if not self.context.metadata.get("_partner_group_formal_answer_published"):+ if not self.context.metadata.get("_capability_answer_published"):await self.pipeline._emit_protocol_fallback_final_response(self.stream, final_override)- self.context.metadata["_partner_group_formal_answer_published"] = True+ self.context.metadata["_capability_answer_published"] = Truereturn await self._finalize_finish(final_override,continued_answer_parts=continued_answer_parts,diff --git a/deeptutor/capabilities/course_study/capability.py b/deeptutor/capabilities/course_study/capability.pyindex 54d179290..68137b6cf 100644--- a/deeptutor/capabilities/course_study/capability.py+++ b/deeptutor/capabilities/course_study/capability.py@@ -37,6 +37,8 @@ logger = logging.getLogger(__name__)COURSE_STUDY_NAME = "course_study"COURSE_ID_KEY = "course_id"+#: Where the recommendation is parked between the hand-off round and the finish.+COURSE_ANSWER_KEY = "_course_study_answer"SUMMARY_CHAR_LIMIT = 3200#: How much of the learner's own course conventions rides in every turn's@@ -420,6 +422,63 @@ class CourseStudyLoopCapability:return kwargsreturn {**kwargs, COURSE_ID_KWARG: resolve_course_id(context)}+ def tool_round_output_policy(+ self,+ context: UnifiedContext,+ final_text: str,+ tool_names: tuple[str, ...],+ ) -> str:+ """Treat the prose accompanying ``course_handoff`` as the answer itself.++ The loop's default rule — text written alongside a tool call is a+ preamble, and the answer is whatever a later tool-less round says — is+ right for tools that *fetch* something. It is wrong here, because this+ playbook asks for the opposite shape: recommend the next action, say why+ it is timely, then call ``course_handoff``. The recommendation and the+ call are one thought, and models write them in one round.++ Left as a preamble, that round's prose was streamed into the trace and+ dropped from the answer. The model, having already said its piece, had+ nothing new for the next round and called ``course_handoff`` again —+ three times in one observed turn — until the budget ran out and the turn+ ended on "no usable response", with the real recommendation sitting+ collapsed in the trace above it.++ Same failure the partner-group capability documents for ``invoke_other``,+ and the same remedy: save the prose, publish it, and let+ :meth:`final_text_override` end the turn on it.+ """+ if not self.is_active(context) or "course_handoff" not in tool_names:+ return ""+ answer = str(final_text or "").strip()+ if not answer:+ # A bare tool call with nothing said. There is no answer to rescue,+ # so leave the loop alone: the model still gets its ordinary finish+ # round to write one.+ return ""+ context.metadata[COURSE_ANSWER_KEY] = answer+ # Deliberately *not* setting ``_capability_answer_published``. That flag+ # means "the learner has already been shown this text, do not emit it+ # again as the answer", and it is true for capabilities that buffer+ # their output behind a protocol. This mode buffers nothing: the prose+ # went out during a tool round, which the transcript files under the+ # collapsed trace rather than as the reply. Claiming it was published+ # leaves the message body empty with the recommendation hidden a click+ # away — the original symptom, differently caused.+ return "publish"++ def final_text_override(self, context: UnifiedContext, final_text: str) -> str | None:+ """End the turn on the recommendation once the hand-off exists.++ Returning a value here stops the loop, which is the point: after the card+ is prepared there is nothing left for this mode to do, and every further+ round is one more chance to repeat itself.+ """+ del final_text+ if not self.is_active(context):+ return None+ return str(context.metadata.get(COURSE_ANSWER_KEY) or "").strip() or None+def pre_loop_seed(self, context: UnifiedContext) -> str:_ = contextreturn ""diff --git a/deeptutor/capabilities/partner_group/capability.py b/deeptutor/capabilities/partner_group/capability.pyindex 9cb46037e..1a2e54c34 100644--- a/deeptutor/capabilities/partner_group/capability.py+++ b/deeptutor/capabilities/partner_group/capability.py@@ -143,7 +143,7 @@ class PartnerGroupCapability:answer, _removed_request = self._save_formal_answer(context, answer)if context.metadata.get("_partner_group_invocation_decided"):return "discard"- context.metadata["_partner_group_formal_answer_published"] = True+ context.metadata["_capability_answer_published"] = Truereturn "publish"if formal_answer and not context.metadata.get("_partner_group_invocation_decided"):diff --git a/tests/capabilities/test_course_study.py b/tests/capabilities/test_course_study.pyindex 40a6f3ecb..31870171d 100644--- a/tests/capabilities/test_course_study.py+++ b/tests/capabilities/test_course_study.py@@ -1113,3 +1113,75 @@ async def test_handoff_keeps_a_ref_that_resolves(handoff = result.metadata["course_handoff"]assert handoff["ref_id"] == "path_abc"assert handoff["label"] == "Processes"+++def _course_context(**metadata: object) -> SimpleNamespace:+ return SimpleNamespace(+ active_capability="course_study",+ metadata={"course_id": "course-1", **metadata},+ config_overrides={},+ )+++def test_recommendation_written_beside_the_handoff_becomes_the_answer() -> None:+ """Reproduces the turn that ended on "no usable response".++ This playbook asks the model to recommend, explain, and call+ `course_handoff` — one thought, which models write in one round. The loop's+ default treats prose beside a tool call as a preamble, so the whole+ recommendation was dropped from the answer and left collapsed in the trace.+ """+ capability = CourseStudyLoopCapability()+ context = _course_context()+ recommendation = "同学,考试只考进程与内存,建议直接从「进程管理」开始。"++ policy = capability.tool_round_output_policy(+ context,+ recommendation,+ ("course_handoff",),+ )++ assert policy == "publish"+ assert capability.final_text_override(context, "") == recommendation+ # Must NOT claim the answer was already published. That flag suppresses the+ # loop's final emit, and is only true for capabilities that buffer output+ # behind a protocol. This mode buffers nothing — its prose went out during a+ # tool round, which the transcript files under the collapsed trace, not as+ # the reply. Setting it leaves the message body empty with the whole+ # recommendation hidden a click away.+ assert "_capability_answer_published" not in context.metadata+++def test_a_bare_handoff_call_leaves_the_loop_alone() -> None:+ """With nothing said there is no answer to rescue.++ Ending the turn here would replace a missing recommendation with silence;+ the model still gets its ordinary finish round to write one.+ """+ capability = CourseStudyLoopCapability()+ context = _course_context()++ assert capability.tool_round_output_policy(context, " ", ("course_handoff",)) == ""+ assert capability.final_text_override(context, "") is None+++def test_prose_beside_a_sensing_tool_stays_a_preamble() -> None:+ """`course_overview` fetches; its preamble is not the recommendation."""+ capability = CourseStudyLoopCapability()+ context = _course_context()++ assert capability.tool_round_output_policy(context, "Let me look.", ("course_overview",)) == ""+ assert capability.final_text_override(context, "") is None+++def test_hooks_stay_inert_without_a_bound_course() -> None:+ """Loop capabilities are non-exclusive; an unbound turn is not ours."""+ capability = CourseStudyLoopCapability()+ context = SimpleNamespace(+ active_capability="chat",+ metadata={"course_id": "course-1"},+ config_overrides={},+ )++ assert capability.tool_round_output_policy(context, "text", ("course_handoff",)) == ""+ assert capability.final_text_override(context, "") is Nonediff --git a/tests/capabilities/test_partner_group_capability.py b/tests/capabilities/test_partner_group_capability.pyindex e64672237..3ab2b799a 100644--- a/tests/capabilities/test_partner_group_capability.py+++ b/tests/capabilities/test_partner_group_capability.py@@ -234,7 +234,7 @@ def test_answer_and_invoke_in_one_tool_round_saves_and_publishes_answer_once() -assert policy == "publish"assert context.metadata["_partner_group_formal_answer"] == "My complete answer."- assert context.metadata["_partner_group_formal_answer_published"] is True+ assert context.metadata["_capability_answer_published"] is Truedef test_any_tool_call_consumes_the_single_private_decision_round() -> None: