DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
Merge branch 'dev' into worktree-chat-engine-interleaved
Bingxi Zhao (Frank)42d793839b38
diff --cc deeptutor/agents/loop/agent_loop.pyindex 66f012df6,214d44d77..d54736c54--- a/deeptutor/agents/loop/agent_loop.py+++ b/deeptutor/agents/loop/agent_loop.py@@@ -894,7 -1056,8 +1064,8 @@@ class AgentLoopoutput_emitted = Falseasync def _emit_segments(segments: list[tuple[str, str]]) -> None:- nonlocal output_emitted- nonlocal action_started, answer_content_emitted, content_chars, output_emitted++ nonlocal action_started, content_chars, output_emitted+ nonlocal reasoning_charsfor kind, segment in segments:if kind == "thinking":# Reasoning goes to the trace on every round shape.@@@ -1154,18 -1443,62 +1449,59 @@@completion_metadata: dict[str, Any] = {"trace_kind": "call_status","call_state": "complete",- # A round with tool calls is narration; a tool-less round is the- # finish whose text is the user-facing answer. Token-truncated- # output remains visible but is not terminal: the loop continues.+ # ``call_role`` states this round's PHASE, not whether its text is+ # shown: ``narration`` is mid-turn commentary (more work follows),+ # ``finish`` is the terminal round. Token-truncated output is+ # visible but not terminal, so it stays ``narration``."call_role": "narration" if tool_calls or truncated_round else "finish",+ # Every round's prose belongs to the answer, in the order it was+ # written. Commentary written before a tool call is what the reader+ # is told while the work happens, not an internal preamble to hide,+ # so the only text that ever leaves the answer is text a capability+ # explicitly retracts (see ``_discard_deferred_output``).+ "answer_visible": True,+ "finish_reason": finish_reason or "stop",+ "requested_max_tokens": int(max_tokens),+ "usage_reported": bool(usage_details),+ "output_chars": output_chars,+ "reasoning_chars": reasoning_chars,+ "content_chars": content_chars,+ "tool_call_chars": tool_call_chars,+ "tool_call_count": len(tool_calls),}+ for key in ("prompt_tokens", "completion_tokens", "total_tokens", "reasoning_tokens"):+ completion_metadata[key] = usage_details.get(key, "unavailable")+ if reasoning_budget_exhausted:+ completion_metadata["reasoning_budget_exhausted"] = True+ log_line = (+ "agent_loop_round_complete model=%s call_id=%s finish_reason=%s "+ "requested_max_tokens=%d usage_reported=%s prompt_tokens=%s "+ "completion_tokens=%s reasoning_tokens=%s output_chars=%d "+ "reasoning_chars=%d content_chars=%d tool_call_chars=%d tool_call_count=%d"+ )+ log_args = (+ self.pipeline.model,+ call_id,+ finish_reason or "stop",+ int(max_tokens),+ bool(usage_details),+ usage_details.get("prompt_tokens", "unavailable"),+ usage_details.get("completion_tokens", "unavailable"),+ usage_details.get("reasoning_tokens", "unavailable"),+ output_chars,+ reasoning_chars,+ content_chars,+ tool_call_chars,+ len(tool_calls),+ )+ if (+ truncated_round+ or reasoning_budget_exhausted+ or (not tool_calls and not visible_text.strip())+ ):+ logger.warning(log_line, *log_args)+ else:+ logger.info(log_line, *log_args)- mastery_tool_round = bool(tool_calls) and bool(self.context.metadata.get("mastery_mode"))- if (dsml_calls or truncated_round or mastery_tool_round) and answer_content_emitted:- # DSML providers may intentionally combine tutor feedback and an- # ask_user/tool call in the same round. Preserve only that cleaned- # surrounding prose in the answer surfaces. Truncated rounds also- # keep their partial answer visible while retaining a truthful- # non-terminal ``narration`` role. Mastery rounds likewise combine- # learner-facing teaching with state/quiz tools; that teaching is- # answer content, not an internal tool preamble.- completion_metadata["answer_visible"] = Truecompletion_event_metadata = merge_trace_metadata(trace_meta, completion_metadata)if forced_tool_choice and not tool_calls and text:diff --cc deeptutor/services/session/_turn_runtime_shared.pyindex 3669ae1b5,1e22a30df..1cd40fdae--- a/deeptutor/services/session/_turn_runtime_shared.py+++ b/deeptutor/services/session/_turn_runtime_shared.py@@@ -85,14 -84,29 +85,29 @@@ def _resolve_turn_outcomereturn status, error+ def _resolve_turn_failure_metadata(+ assistant_events: Sequence[dict[str, Any]],+ ) -> tuple[str, bool]:+ """Recover structured failure metadata from a terminal error event."""+ for event in reversed(assistant_events):+ metadata = event.get("metadata")+ metadata = metadata if isinstance(metadata, dict) else {}+ if event.get("type") != StreamEventType.ERROR.value or not metadata.get("turn_terminal"):+ continue+ code = str(metadata.get("error_code") or "")+ retryable = _coerce_bool(metadata.get("retryable"), False)+ return code, retryable+ return "", False++-def _narration_marker_call_id(event: StreamEvent) -> str | None:- """call_id of a chat-loop round that resolved as narration (a short- preamble streamed alongside a tool call). Its text belongs to the trace,- not the persisted answer, so it is excluded when assembling content.+def _retracted_round_call_id(event: StreamEvent) -> str | None:+ """call_id of a chat-loop round whose prose was taken back out of the answer.- A round may explicitly keep learner-facing prose surrounding a call via- ``answer_visible`` (for example DSML or mastery tutoring); that narrow- exception remains part of the persisted answer.+ Commentary a round wrote before calling a tool is answer content — the+ reader was shown it as the work happened, and the persisted answer keeps it+ so a reloaded turn reads the way it did live. The single exception is a+ round a capability's finish guard rejected, which republishes its marker+ with ``answer_visible: False``; that text belongs to the trace only."""metadata = event.metadata or {}if (diff --cc deeptutor/services/session/turns/executor.pyindex 31cfbe091,e57725af1..089c9bf2b--- a/deeptutor/services/session/turns/executor.py+++ b/deeptutor/services/session/turns/executor.py@@@ -44,10 -45,10 +44,11 @@@ from .._turn_runtime_shared import_repair_chinese_emphasis_for_persistence,_request_snapshot_metadata,_resolve_selection_tutor_context,+ _resolve_turn_failure_metadata,_resolve_turn_outcome,+ _retracted_round_call_id,_should_capture_assistant_content,- _stamp_ask_user_content_offset,+ _stamp_content_offset,_timed_media_id,_timed_media_viewport,_topic_material_manifest,