DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
feat(chat): honor end_loop metadata after ask_user resume (#924)
alanguan736de38f519f3d
diff --git a/deeptutor/agents/chat/agentic_pipeline.py b/deeptutor/agents/chat/agentic_pipeline.pyindex e3f6b3584..e3f49df7a 100644--- a/deeptutor/agents/chat/agentic_pipeline.py+++ b/deeptutor/agents/chat/agentic_pipeline.py@@ -925,6 +925,24 @@ class AgenticChatPipeline:reply_text=reply_text,answers=answers,)+ # The reply happened, so it belongs in the transcript whether or not the+ # loop goes on — emit the trace before deciding to stop.+ meta: dict[str, Any] = {+ "trace_kind": "user_reply",+ "ask_user_resolved": True,+ "ask_user_tool_call_id": dispatch.pause_tool_call_id,+ "reply_preview": (reply_text or "")[:200],+ }+ if answers:+ meta["answers"] = list(answers)+ await stream.progress("", source="chat", stage="responding", metadata=meta)++ # Neutral stop signal for loop plugins (e.g. a crisis redirect): the+ # outer capability owns the final message, so skip further LLM rounds.+ # Everything below only exists to feed the answer back to the model.+ if context.metadata.get("end_loop"):+ return False+body_text = _format_user_reply_body(reply_text,answers,@@ -943,15 +961,6 @@ class AgenticChatPipeline:if tm.get("tool_call_id") == dispatch.pause_tool_call_id:tm["content"] = directivebreak- meta: dict[str, Any] = {- "trace_kind": "user_reply",- "ask_user_resolved": True,- "ask_user_tool_call_id": dispatch.pause_tool_call_id,- "reply_preview": (reply_text or "")[:200],- }- if answers:- meta["answers"] = list(answers)- await stream.progress("", source="chat", stage="responding", metadata=meta)return Truedef _augment_tool_kwargs(diff --git a/tests/agents/chat/test_agent_loop.py b/tests/agents/chat/test_agent_loop.pyindex 4c7fb6c1e..ec4b460d3 100644--- a/tests/agents/chat/test_agent_loop.py+++ b/tests/agents/chat/test_agent_loop.py@@ -1210,6 +1210,102 @@ async def test_ask_user_pause_resumes_and_streams_interleaved(assert result.metadata["completed"] is True+async def test_ask_user_resume_end_loop_skips_further_llm(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """Neutral ``metadata['end_loop']`` after resume stops without another LLM round."""++ class _EndLoopOnResume:+ name = "end_loop_probe"+ owned_tools: tuple[str, ...] = ()++ def is_active(self, context: UnifiedContext) -> bool:+ return True++ def system_block(self, context, *, language, prompts): # noqa: ANN001+ return None++ def augment_kwargs(self, tool_name, kwargs, context): # noqa: ANN001+ return kwargs++ def pre_loop_seed(self, context: UnifiedContext) -> str:+ return ""++ async def on_user_resume(self, context, ask_user, *, reply_text, answers): # noqa: ANN001+ _ = ask_user, reply_text, answers+ context.metadata["end_loop"] = True++ class _PausingRegistry(_Registry):+ async def execute(self, name: str, **kwargs):+ self.executed.append({"name": name, "kwargs": kwargs})+ if name == "ask_user":+ return ToolResult(+ content="Asked the user.",+ success=True,+ pause_for_user={"questions": [{"id": "q1", "prompt": "Which topic?"}]},+ )+ return await super().execute(name, **kwargs)++ registry = _PausingRegistry()+ client = _ScriptedChatClient(+ [+ [+ _llm_chunk(content="One question."),+ _llm_chunk(+ tool_calls=[+ {+ "id": "call-1",+ "name": "ask_user",+ "arguments": json.dumps(+ {"questions": [{"id": "q1", "prompt": "Which topic?"}]}+ ),+ }+ ]+ ),+ ],+ # Must not be reached when end_loop is set on resume.+ [_llm_chunk(content="Should not stream.")],+ ]+ )+ pipeline = AgenticChatPipeline(language="en")+ pipeline.registry = registry+ monkeypatch.setattr(pipeline, "_compose_enabled_tools", lambda _context: ["ask_user"])+ monkeypatch.setattr(pipeline, "_build_openai_client", lambda: client)+ monkeypatch.setattr(+ pipeline,+ "_active_loop_capabilities",+ lambda _context: (_EndLoopOnResume(),),+ )++ async def _waiter():+ return {"text": "abort please"}++ events = await _run(+ pipeline,+ UnifiedContext(+ session_id="s-end-loop",+ user_message="Quick question",+ enabled_tools=["ask_user"],+ metadata={"wait_for_user_reply": _waiter},+ ),+ )++ assert client.call_count == 1+ assert "Should not stream." not in _contents(events)+ result = _result(events)+ assert result.metadata["completed"] is False+ # The user did answer, so the reply must still reach the transcript — only+ # the further LLM rounds are skipped.+ replies = [+ event+ for event in events+ if (event.metadata or {}).get("trace_kind") == "user_reply"+ and (event.metadata or {}).get("reply_preview") == "abort please"+ ]+ assert len(replies) == 1++@pytest.mark.asyncioasync def test_unresolved_ask_user_halts_turn(monkeypatch: pytest.MonkeyPatch) -> None:class _PausingRegistry(_Registry):