Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix: preserve tutoring replies and stabilize preview runtime

Bingxi Zhao (Frank)e347c0c2e017
diff --git a/deeptutor/agents/loop/agent_loop.py b/deeptutor/agents/loop/agent_loop.py
index 9ad10deb1..16d377182 100644
--- a/deeptutor/agents/loop/agent_loop.py
+++ b/deeptutor/agents/loop/agent_loop.py
@@ -22,11 +22,10 @@ unresolved pause (or a terminator tool) halts the turn.
There is no separate respond pass and no text destination has to be guessed:
every round's text streams to the user as it is generated and stays there. The
-``call_status`` marker a completed round emits carries two independent facts —
+``call_status`` marker a completed round emits describes its phase —
``call_role`` (``narration`` = more work follows, ``finish`` = terminal) says
-where the turn is, while ``answer_visible`` says whether the text counts as
-answer content. It is ``True`` for every ordinary round; only a capability
-retracting a rejected round sets it ``False``.
+where the turn is. ``answer_visible`` remains true for compatibility; published
+answer text is never retracted by a finish check.
"""
from __future__ import annotations
@@ -421,7 +420,9 @@ class AgentLoop:
# Empty text asks capabilities about state, without inventing prose to
# validate or giving the model another chance to claim completion.
if self.pipeline._capability_finish_instruction(self.context, ""):
- return await self._reject_capability_finish()
+ outcome = await self._reject_capability_finish()
+ outcome.final_text = _join_answer_parts(continued_answer_parts or [], "")
+ return outcome
if reason == "error":
# The caller has the exception and logs it; without it here the
# reader is told a step failed and never which one or why — the
@@ -578,35 +579,11 @@ class AgentLoop:
result.deferred_completion_metadata = None
async def _discard_deferred_output(self, result: LLMCallResult) -> None:
- """Take a rejected round's prose back out of the answer.
-
- Two shapes reach here. A *buffered* round is simply never published,
- so closing its trace as retracted is the whole retraction. A round
- that already **streamed** — the ordinary case now that only protocol
- capabilities buffer — was published optimistically, so its retraction
- has to be a correction: the same ``call_id`` re-marked
- ``answer_visible: False``, which is what moves that text out of the
- answer and into the collapsed trace on the reader's side.
-
- This is the ONLY way prose leaves the answer. Ordinary commentary
- written before a tool call stays where the reader saw it.
-
- Emitting nothing in that second case was what left rejected prose
- sitting in the answer as though it had been accepted.
- """
- metadata = result.deferred_completion_metadata or result.completion_metadata
- if metadata is not None:
- metadata = dict(metadata)
+ """Discard only unpublished protocol payloads, never public answer text."""
+ if result.deferred_completion_metadata is not None:
+ metadata = dict(result.deferred_completion_metadata)
metadata["call_role"] = "narration"
- metadata["answer_visible"] = False
- metadata["finish_rejected"] = True
- await self.stream.progress(
- "",
- source=self.source,
- stage=self.stage,
- metadata=metadata,
- )
- result.completion_metadata = None
+ await self.stream.progress("", source=self.source, stage=self.stage, metadata=metadata)
result.deferred_chunk_metadata = None
result.deferred_completion_metadata = None
@@ -792,11 +769,6 @@ class AgentLoop:
if segment.strip():
action_started = True
content_chars += len(segment)
- if forced_tool_choice:
- # This round exists to produce a call, so its prose is
- # not an answer and must not reach the reader — but
- # that is a rule about *content*, not about reasoning.
- continue
output_emitted = True
visible_text_parts.append(segment)
if not defer_visible_output:
@@ -1224,8 +1196,7 @@ class AgentLoop:
# 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``).
+ # and a capability finish check must never retract it.
"answer_visible": True,
"finish_reason": finish_reason or "stop",
"requested_max_tokens": int(max_tokens),
@@ -1271,14 +1242,6 @@ class AgentLoop:
logger.info(log_line, *log_args)
completion_event_metadata = merge_trace_metadata(trace_meta, completion_metadata)
- if forced_tool_choice and not tool_calls and text:
- # Some compatibility providers accept ``tool_choice`` but ignore
- # it. Do not lose their answer merely because it was buffered.
- fallback_dsml = DSMLStreamFilter()
- fallback_filter = InlineThinkFilter()
- visible = fallback_dsml.feed(text) + fallback_dsml.flush()
- await _emit_segments(fallback_filter.feed(visible) + fallback_filter.flush())
-
if not defer_visible_output:
await self.stream.progress(
"",
diff --git a/deeptutor/agents/loop/loop_host.py b/deeptutor/agents/loop/loop_host.py
index 8c220e56f..7ece41af1 100644
--- a/deeptutor/agents/loop/loop_host.py
+++ b/deeptutor/agents/loop/loop_host.py
@@ -166,6 +166,8 @@ class AgentLoopHost:
for call in self.result.tool_calls
)
if rejected:
+ if self.result.deferred_chunk_metadata is None and self.result.visible_text:
+ self.continued.append(self.result.visible_text)
await self.loop._discard_deferred_output(self.result)
return {
"recoveryRejected": rejected,
@@ -262,6 +264,8 @@ class AgentLoopHost:
text = self.loop._clean(self.result.text)
self.redirect = self.pipeline._capability_finish_instruction(self.context, text)
if self.redirect:
+ if self.result.deferred_chunk_metadata is None and self.result.visible_text:
+ self.continued.append(self.result.visible_text)
await self.loop._discard_deferred_output(self.result)
return {"redirect": True, "override": False}
self.override = self.pipeline._capability_final_text_override(self.context, text)
@@ -275,6 +279,7 @@ class AgentLoopHost:
async def reject_finish(self) -> None:
self.outcome = await self.loop._reject_capability_finish()
+ self.outcome.final_text = values._join_answer_parts(self.continued, "")
async def finish(self, *, override: bool) -> None:
result = self.result
@@ -282,6 +287,16 @@ class AgentLoopHost:
if override:
assert self.override is not None
if not self.override_after_tools:
+ if result.deferred_chunk_metadata is None and result.visible_text:
+ self.continued.append(result.visible_text)
+ self.messages.append(values._assistant_round_message(result))
+ # An additive notice may include the original answer. That
+ # answer is already on screen, so publish only the addition.
+ if result.visible_text in self.override:
+ before, _, after = self.override.partition(result.visible_text)
+ self.override = (before + after).strip()
+ if self.override:
+ self.override = "\n\n" + self.override
await self.loop._discard_deferred_output(result)
if not self.context.capability_output.answer_published:
await self.pipeline._emit_protocol_fallback_final_response(
@@ -289,7 +304,10 @@ class AgentLoopHost:
)
self.context.capability_output.answer_published = True
self.outcome = await self.loop._finalize_finish(
- self.override, continued_answer_parts=self.continued, allow_empty=True
+ self.override,
+ visible_text=self.override,
+ continued_answer_parts=self.continued,
+ allow_empty=True,
)
return
await self.loop._release_deferred_output(result)
diff --git a/deeptutor/agents/loop/loop_state.py b/deeptutor/agents/loop/loop_state.py
index 89555d24f..0c945a7d7 100644
--- a/deeptutor/agents/loop/loop_state.py
+++ b/deeptutor/agents/loop/loop_state.py
@@ -71,10 +71,8 @@ class LLMCallResult:
content_chars: int = 0
tool_call_chars: int = 0
finish_reason: str = ""
- # This round's ``call_status`` metadata, set whether or not the round was
- # buffered. A streamed round needs it to publish a correction when its
- # finish is later rejected; the ``deferred_*`` pair below is set only while
- # the text is still withheld.
+ # Completion metadata describes the round; it never revokes public text.
+ # Deferred metadata is reserved for unpublished structured protocol output.
completion_metadata: dict[str, Any] | None = None
deferred_chunk_metadata: dict[str, Any] | None = None
deferred_completion_metadata: dict[str, Any] | None = None
diff --git a/deeptutor/agents/loop/pipeline.py b/deeptutor/agents/loop/pipeline.py
index 1897b4976..01474c145 100644
--- a/deeptutor/agents/loop/pipeline.py
+++ b/deeptutor/agents/loop/pipeline.py
@@ -1025,14 +1025,8 @@ class AgenticLoopPipeline:
republished verbatim by ``final_text_override`` and would otherwise
appear twice.
- It is the wrong trade for a capability that merely guards how a turn
- may *end*. Mastery and Partner authoring stream ordinary teaching prose
- and reject a finish only in the uncommon case (a question that was
- never posed, a draft that was never created). Buffering every round of
- theirs to catch that case removed streaming from those surfaces
- entirely; a rejected finish is instead withdrawn after the fact by the
- round's ``narration`` marker, which is how the reader's answer text is
- reconciled anyway.
+ Ordinary tutoring and authoring prose streams immediately. Finish
+ checks may request more tool work, but cannot withdraw public text.
Declared explicitly rather than inferred, because the two groups are
indistinguishable from their hooks: both define ``finish_instruction``.
diff --git a/deeptutor/api/run_server.py b/deeptutor/api/run_server.py
index 39e1d748b..d24bf96eb 100644
--- a/deeptutor/api/run_server.py
+++ b/deeptutor/api/run_server.py
@@ -91,6 +91,7 @@ def main() -> None:
proxy_headers=False,
ws_max_size=get_ws_max_size(),
timeout_keep_alive=HTTP_KEEP_ALIVE_TIMEOUT,
+ timeout_graceful_shutdown=1,
)
diff --git a/deeptutor/app/container.py b/deeptutor/app/container.py
index af8e92afd..961d1dc5e 100644
--- a/deeptutor/app/container.py
+++ b/deeptutor/app/container.py
@@ -131,7 +131,9 @@ class ApplicationContainer:
async def close(self) -> None:
if not self._started:
return
- await self.runtime_registry.close(drain_timeout_seconds=60.0)
+ # Cancel active turns now so their finally blocks can persist shutdown
+ # state and release subprocesses within the launcher grace period.
+ await self.runtime_registry.close(drain_timeout_seconds=0.0)
from deeptutor.runtime.memory_reclaim import cancel_idle_reclaim
from deeptutor.runtime.ts_engine import close_engine_clients
diff --git a/deeptutor/capabilities/mastery/loop.py b/deeptutor/capabilities/mastery/loop.py
index 0ed181d02..8a11bd4be 100644
--- a/deeptutor/capabilities/mastery/loop.py
+++ b/deeptutor/capabilities/mastery/loop.py
@@ -17,7 +17,6 @@ from __future__ import annotations
from collections.abc import Callable
import json
import logging
-import re
from typing import Any
from deeptutor.capabilities.mastery.tools import MASTERY_TOOL_NAMES
@@ -41,166 +40,6 @@ _PATH_BINDING_TOOLS = frozenset({"mastery_switch", "mastery_leave"})
NATIVE_LOOP_FLAG = "mastery_native_loop"
-# Shapes that betray a question written into the reply as prose instead of
-# posed through ``mastery_quiz`` — see ``finish_instruction``.
-_PLAIN_CHOICE_OPTION_RE = re.compile(
- r"^(?:[-*+]\s*)?(?:\*\*)?([A-D])(?:\*\*)?\s*[.、)::-]\s*(\S.*)$",
- re.IGNORECASE,
-)
-_PLAIN_QUIZ_PROMPT_RE = re.compile(
- r"\b(?:which|choose|select|answer)\b|选择|选哪个|请选择|请回答|答案",
- re.IGNORECASE,
-)
-# Defense in depth for explicit numbered assessments. Merely asking questions
-# while explaining is valid; require both answer/assessment intent and multiple
-# numbered question stems. This fallback does not replace registered groups.
-_MULTIPART_INTENT_RE = re.compile(
- r"\b(?:answer these|practice questions|will grade (?:both|your|the) answers)\b|"
- r"\b(?:válaszolj|gyakorló kérdés|ellenőrző kérdés)\w*\b|"
- r"请回答|回答下面|练习题|检查题",
- re.IGNORECASE,
-)
-_NUMBERED_QUESTION_RE = re.compile(
- r"^\s*(?:#{1,6}\s+)?(?:\*\*)?\d{1,2}[.)、](?:\*\*)?\s+", re.MULTILINE
-)
-# A reply that announces a question without posing one. Matched against the
-# tail of the reply only, where an announcement lands, so ordinary discussion
-# of "this question" while reviewing an attempt does not trip it.
-_QUESTION_PROMISE_RE = re.compile(
- # A bare "这道题" is how a *review* of the attempt just graded reads too,
- # so the Chinese branch needs a forward-looking verb alongside it.
- r"(?:来|下面|接下来|先|试试|做|回答)[^。!?\n]{0,12}(?:这道|一道|下一?道)题|"
- r"出一?道题|考考你|试试这|看看你(?:对|的|是否|能不能)|检验一下你|"
- # …and the same announcement with the noun left out: "再试一道,把这套
- # 判别规则用起来" never says 题 at all, which is exactly how two real
- # turns slipped past this guard and left the learner staring at a colon.
- r"再(?:试|来|做)一(?:道|个)|来实战|练一?练|上手试|"
- r"\bhere(?:\u2019s| is|'s) (?:a|the|this) question\b|"
- r"\btry (?:this|the following|a|another) (?:question|one)\b|"
- r"\blet(?:\u2019s|'s) (?:see|test|check) (?:if|whether|how|what) you\b",
- re.IGNORECASE,
-)
-_PROMISE_TAIL_CHARS = 160
-# A reply that stops on a colon promised whatever was meant to follow it. The
-# lead-in for a question is supposed to share its round with the
-# ``mastery_quiz`` call that fills the space underneath, so a colon with
-# nothing after it is the most reliable evidence that the call never happened —
-# more reliable than recognising the wording, which varies every turn.
-_DANGLING_LEAD_IN_CHARS = frozenset("::")
-
-# A bare instruction to pick from unseen choices is also a promise of a card.
-# #1508 ended with "四个选项,选一个。" after describing the objective, but
-# never called mastery_quiz, so no choices were available to the learner.
-_UNPOSTED_CHOICE_LEAD_IN_RE = re.compile(
- r"(?:[两三四五六七八九十]|\d+)个选项[,,、:\s]*(?:请|你)?(?:选|挑)(?:一|1)个[。!?.!?]?\s*$|"
- r"\b(?:three|four|\d+) (?:choices|options)[,:;\s]+(?:choose|pick|select) one[.!?]?\s*$",
- re.IGNORECASE,
-)
-
-# Delivery claims can precede a long explanation, unlike the vague question
-# announcements above. Check clauses across the reply, but require both a
-# concrete card/question and a delivery verb rather than the word "card" alone.
-_CARD_DELIVERY_RE = re.compile(
- r"(?:卡片|题卡|(?:这|那|一|下一)道题|题目)[^。!?\n,,;;]{0,24}"
- r"(?:重开|开出来|(?:放|挂|贴|发)(?:上来|上去|出来|上了|好了)|"
- r"(?:生成|展示)(?:了|好|出来)|(?:就)?在(?:下面|下方))|"
- r"先探一题|"
- r"\bI(?:['’]ve| have)? (?:just |now )?(?:posted|reopened|displayed) "
- r"(?:the|a|this) (?:(?:question|quiz|answer) )?card\b|"
- r"\b(?:the|this) (?:(?:question|quiz|answer) )?card "
- r"(?:is|has been) (?:ready|posted|displayed|reopened|below)\b",
- re.IGNORECASE,
-)
-_NON_DELIVERY_CLAUSE_RE = re.compile(
- r"没有|没能|未能|尚未|还没|不会|不再|不要|不能|并未|无法|失败|"
- r"上次|上一轮|刚才|之前|如果|假如|是否|你说|[吗??]|^\s*等|"
- r"\b(?:not|never|if|earlier|previously|yesterday)\b",
- re.IGNORECASE,
-)
-_QUOTED_CARD_TEXT_RE = re.compile(
- r"```.*?```|~~~.*?~~~|`[^`\n]*`|“[^”]*”|「[^」]*」|\"[^\"\n]*\"|^\s*>[^\n]*",
- re.DOTALL | re.MULTILINE,
-)
-_CONDITIONAL_DELIVERY_RE = re.compile(
- r"^\s*(?:等|如果|假如|(?:学|讲|看)完.{0,12}(?:以后|之后)|稍后|下一轮|"
- r"if\b|when\b|after\b)",
- re.IGNORECASE,
-)
-
-
-def _claims_card_delivery(text: str) -> bool:
- """Recognise current delivery claims without treating quoted reports as actions."""
- prose = _QUOTED_CARD_TEXT_RE.sub(" ", text)
- for sentence in re.split(r"[。!\n]|(?<=[.!?])\s+", prose):
- if _CONDITIONAL_DELIVERY_RE.search(sentence):
- continue
- for clause in re.split(r"[,,;;]", sentence):
- if _CARD_DELIVERY_RE.search(clause) and not _NON_DELIVERY_CLAUSE_RE.search(clause):
- return True
- return False
-
-
-def _looks_like_plain_choice_quiz(text: str) -> bool:
- """Recognise a rendered A-D option list with high precision.
-
- The model may discuss labelled options while teaching. Requiring both an
- assessment prompt and at least three distinct labelled answer bodies keeps
- ordinary prose, headings, and option-like vocabulary examples out of this
- protocol guard.
- """
- labels: set[str] = set()
- prompt_lines: list[str] = []
- for raw_line in text.splitlines():
- line = raw_line.strip()
- match = _PLAIN_CHOICE_OPTION_RE.match(line)
- if match:
- labels.add(match.group(1).upper())
- else:
- prompt_lines.append(line)
- return len(labels) >= 3 and any(_PLAIN_QUIZ_PROMPT_RE.search(line) for line in prompt_lines)
-
-
-def _looks_like_multipart_assessment(text: str) -> bool:
- prose = _QUOTED_CARD_TEXT_RE.sub(" ", text)
- if not _MULTIPART_INTENT_RE.search(prose):
- return False
- # Markdown frequently puts the title on the numbered line and the actual
- # question in the following paragraph. Count question-bearing blocks, not
- # only same-line stems; quoted/code examples were removed above.
- starts = list(_NUMBERED_QUESTION_RE.finditer(prose))
- questions = 0
- for index, start in enumerate(starts):
- end = starts[index + 1].start() if index + 1 < len(starts) else len(prose)
- if re.search(r"[??]", prose[start.end() : end]):
- questions += 1
- return questions >= 2
-
-
-def _turn_has_graded(context: UnifiedContext) -> bool:
- """Whether a ruling was made this turn, by the model or by the runtime.
-
- Two things can grade: the tutor calling ``mastery_grade``, and the runtime
- ruling on a card answer before the turn starts. Only the first passes
- through ``augment_kwargs``, so reading the extension alone made the
- guard treat post-grade feedback — option-by-option prose, which is what
- reviewing an attempt looks like — as a question written out in text, and
- a rejected finish is discarded rather than shown.
- """
- if context.extension("mastery").get("quiz_graded"):
- return True
- return bool(context.metadata.get("mastery_card_grade"))
-
-
-def _announces_an_unposed_question(text: str) -> bool:
- """Whether a reply promises a question it never put on a card."""
- body = (text or "").strip()
- if not body:
- return False
- if body[-1] in _DANGLING_LEAD_IN_CHARS:
- return True
- return bool(_QUESTION_PROMISE_RE.search(body[-_PROMISE_TAIL_CHARS:]))
-
-
class MasteryLoopCapability:
"""Turn-scoped integration for mastery-path tutoring.
@@ -487,31 +326,11 @@ class MasteryLoopCapability:
)
def finish_instruction(self, context: UnifiedContext, final_text: str) -> str | None:
- """Catch a finish that leaves the learner with nothing to answer.
-
- A quiz selected in this turn must reach its success callback before
- the turn can finish, regardless of reply wording or later grading.
- Text heuristics are only a fallback for replies that never selected
- the tool. Two other states are not an outstanding delivery:
-
- A pending question from an earlier turn does not mean it went
- unasked — it was already posed on its own card — so a learner
- who types a question instead of answering leaves the interaction open
- on purpose, and the tutor answering them is the right reply, not a
- skipped step.
-
- And once ``mastery_grade`` has run, reviewing the options one by one —
- "you picked C; A fails because…" — matches the plain-text-quiz
- heuristic exactly while being the whole point of the turn. Blocking
- that discarded the explanation and left the learner with a graded card
- and no reason for the verdict.
-
- That second exemption used to sit at the top of this method and so
- waived *every* check on a grading turn — including the unposed-question
- one. Grading turns are precisely where the tutor teaches the gap and
- then reaches for the next question, so the one turn shape most likely
- to end on an unkept promise was the one shape never examined. It now
- guards only the heuristic it was written for.
+ """Retry failed tool operations without classifying the learner-facing prose.
+
+ Discussing options, questioning a grade or quoting a question is ordinary
+ tutoring. Only an actual failed operation can require follow-up work;
+ answer wording is never evidence that the model must pose another quiz.
"""
if not self.is_active(context):
return None
@@ -534,59 +353,7 @@ class MasteryLoopCapability:
"question delivery. If delivery remains unsuccessful, this turn must "
"fail rather than report successful completion."
)
- if not state.get("card_posted") and (
- _announces_an_unposed_question(final_text)
- or _claims_card_delivery(final_text)
- or (
- not state.get("quiz_awaiting_grade")
- and _UNPOSTED_CHOICE_LEAD_IN_RE.search(final_text)
- )
- ):
- # Argument binding sets quiz_awaiting_grade even when registration
- # fails. Only the tool's success callback marks a card as posted.
- # "Let us see what you already know:" and then nothing. The learner
- # is left reading a promise with no card under it, and the turn is
- # over — this reply announced the question instead of posing it.
- return (
- "That reply announced a question but never posed one, so the "
- "learner is looking at a promise and an empty space. Write the "
- "lead-in and call mastery_quiz in the SAME round — the call is "
- "what puts the question on their card, and it ends the turn. If "
- "you did not mean to quiz them yet, say what you meant to say "
- "without announcing a question."
- )
- if not state.get("card_posted") and _looks_like_multipart_assessment(final_text):
- return (
- "That reply presented a numbered assessment as unregistered prose. "
- "For practice across all objective types, call mastery_quiz with a questions group "
- "so each answer has its own stored contract and result. CONCEPT / DESIGN cards "
- "do not open their qualitative gate; use mastery_assess for the learner's own "
- "explanation when assessing that gate. If an older question "
- "is pending and you want to show it again, call mastery_quiz with "
- "pending_action='resume' and its exact parent resume_question_id, without "
- "new questions or answer keys. Preserve it unless the learner explicitly requested replacement; "
- "then use its current question ID and a literal replacement_request quote. "
- "Do not concatenate expected answers or claim that a prose list is a card. "
- "If this is teaching rather than assessment, explain directly without "
- "instructing the learner to answer an unregistered practice set."
- )
- if _turn_has_graded(context) or not _looks_like_plain_choice_quiz(final_text):
- return None
- if state.get("quiz_awaiting_grade"):
- return (
- "That question is already on the learner's answer card — do not "
- "write it out again in prose. Either end the turn on what you "
- "have taught and let their answer arrive as the next message, or "
- "call mastery_grade with the answer they already gave you (the "
- "engine grades the question it is holding open)."
- )
- return (
- "The previous reply posed a mastery assessment as plain text. Do not "
- "write the question or its choices in prose: call mastery_quiz "
- "instead — that one call registers the expected answer and puts the "
- "question on its own answer card, and the turn stops there for the "
- "learner to answer."
- )
+ return None
def final_text_override(self, context: UnifiedContext, final_text: str) -> str | None:
"""End the turn on the card once a question has been posed.
diff --git a/deeptutor/capabilities/mastery/prompts/en/mastery_loop.yaml b/deeptutor/capabilities/mastery/prompts/en/mastery_loop.yaml
index 2a6f24d6d..144351ae6 100644
--- a/deeptutor/capabilities/mastery/prompts/en/mastery_loop.yaml
+++ b/deeptutor/capabilities/mastery/prompts/en/mastery_loop.yaml
@@ -25,6 +25,7 @@ general: |-
Do not describe internal stages, prompt blocks, or implementation details unless the learner explicitly asks.
runtime_policy: |-
+ When the learner questions a prompt, option, answer or grade, address the objection directly and explain the evidence first. Check sources and acknowledge or correct mistakes when needed. Do not rush them to answer, force another quiz, or treat an objection as an answer. A pending card may remain open; discussion alone does not change grades or mastery.
Every sentence you write in the body reaches the learner verbatim. The body is for what you say TO the learner — teaching, questions, feedback, encouragement.
Never put internal deliberation there: no narrating what a tool returned, no weighing which tool to call, whether to write a memory, or whether this attempt clears the gate, and no talking to yourself ("I should first...", "Let me...", "Wait, ...").
Deliberate in your own reasoning channel; if you absolutely must write it out, wrap the whole passage in `<think>...</think>`.
diff --git a/deeptutor/capabilities/mastery/prompts/zh/mastery_loop.yaml b/deeptutor/capabilities/mastery/prompts/zh/mastery_loop.yaml
index 319e74eaa..a98da3c54 100644
--- a/deeptutor/capabilities/mastery/prompts/zh/mastery_loop.yaml
+++ b/deeptutor/capabilities/mastery/prompts/zh/mastery_loop.yaml
@@ -24,6 +24,7 @@ general: |-
除非学习者明确询问,否则不要描述内部阶段、提示词块或实现细节。
runtime_policy: |-
+ 学习者对题目、选项、答案或评分提出异议、追问时,优先直接回应并解释依据;必要时检查来源、承认和纠正错误。不要为了答题进度催答、强行出下一题或把异议当成作答。未解决的题卡可以保持待答,讨论本身不更改评分或掌握度。
你写进正文的每一句话都会原样呈现给学习者。正文只放对学习者说的话——讲解、提问、反馈、鼓励。
不要把内部推演写进正文:不要复述工具返回了什么,不要权衡该调哪个工具、要不要写记忆、这轮算不算通过,也不要自言自语「我应该先……」「让我先……」「等等,……」。
推演在你自己的推理通道里完成;万不得已必须写出来时,把整段包进 `<think>…</think>`。
diff --git a/deeptutor/runtime/launcher.py b/deeptutor/runtime/launcher.py
index 309430b33..4b48011e7 100644
--- a/deeptutor/runtime/launcher.py
+++ b/deeptutor/runtime/launcher.py
@@ -62,6 +62,8 @@ BACKEND_READY_TIMEOUT = _ready_timeout(BACKEND_READY_TIMEOUT_ENV, 60)
FRONTEND_READY_TIMEOUT = _ready_timeout(FRONTEND_READY_TIMEOUT_ENV, 120)
FRONTEND_REUSE_PROBE_TIMEOUT = 2
KILL_SIGNAL = getattr(signal, "SIGKILL", signal.SIGTERM)
+SHUTDOWN_GRACE_SECONDS = 3.0
+SHUTDOWN_REAP_SECONDS = 1.0
WEB_CACHE_DIR = Path("data") / "user" / "runtime" / "web"
SOURCE_PRODUCTION_DIST_DIR = ".next-deeptutor"
SOURCE_BUILD_MARKER = ".deeptutor-build.json"
@@ -230,6 +232,7 @@ def _send_tree_signal(pid: int | None, pgid: int | None, sig: signal.Signals | i
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
check=False,
+ timeout=SHUTDOWN_REAP_SECONDS,
**_no_window_kwargs(),
)
# Exit code 128 is taskkill's "process not found": the tree is
@@ -271,22 +274,134 @@ def _signal_target(
)
-def _terminate(proc: ManagedProcess | None) -> None:
- """Stop ``proc`` with the SIGTERM -> SIGKILL ladder.
+def _group_alive(proc: ManagedProcess) -> bool:
+ """A group can outlive its leader (npm and worker launchers do this)."""
+ if os.name == "nt" or proc.pgid is None:
+ return False
+ try:
+ os.killpg(proc.pgid, 0)
+ return True
+ except ProcessLookupError:
+ return False
+ except PermissionError:
+ return True
+
- A signal that could not be delivered is logged rather than silently
- dropped, so a surviving child can be traced back to its failed
- termination.
+def _terminate_all(
+ processes: list[ManagedProcess], *, force: threading.Event | None = None
+) -> None:
+ """Stop owned trees together, with one grace period and bounded reaping.
+
+ Capture descendants before signalling their parents: tool subprocesses can
+ have their own sessions and become reparented as soon as the backend exits.
+ psutil's Process objects guard against PID reuse when signalling them.
+ Reused frontends are deliberately absent from ``processes``.
"""
- if proc is None or proc.process.poll() is not None:
+ import psutil
+
+ descendants: dict[int, psutil.Process] = {}
+ parents: list[psutil.Process] = []
+
+ def remember_children(parent: psutil.Process) -> None:
+ try:
+ for child in parent.children(recursive=True):
+ descendants.setdefault(child.pid, child)
+ except psutil.NoSuchProcess:
+ pass
+ except psutil.Error:
+ logger.warning("Could not enumerate children of pid=%s", parent.pid, exc_info=True)
+
+ def child_alive(child: psutil.Process) -> bool:
+ try:
+ return child.is_running() and child.status() != psutil.STATUS_ZOMBIE
+ except psutil.NoSuchProcess:
+ return False
+ except psutil.AccessDenied:
+ # Still attempt termination; signal failures are logged below.
+ return True
+
+ active = []
+ for proc in processes:
+ running = proc.process.poll() is None
+ if running:
+ try:
+ parent = psutil.Process(proc.process.pid)
+ parents.append(parent)
+ remember_children(parent)
+ except psutil.NoSuchProcess:
+ pass
+ except psutil.Error:
+ logger.warning("Could not inspect %s", proc.name, exc_info=True)
+ if running or _group_alive(proc):
+ active.append(proc)
+ if not running and os.name != "nt":
+ # Recover same-group children even when the launcher has
+ # already reaped npm or another short-lived parent.
+ for child in psutil.process_iter():
+ try:
+ if os.getpgid(child.pid) == proc.pgid:
+ descendants.setdefault(child.pid, child)
+ remember_children(child)
+ except (ProcessLookupError, PermissionError):
+ continue
+ _log(_t("start.stopping", name=proc.name, pid=proc.process.pid))
+
+ def signal_trees(sig: signal.Signals | int) -> None:
+ for proc in active:
+ _signal_target(
+ proc.process.pid,
+ proc.pgid,
+ sig,
+ target=f"{proc.name} (pid={proc.process.pid})",
+ )
+ for child in descendants.values():
+ try:
+ child.send_signal(sig)
+ except psutil.NoSuchProcess:
+ pass
+ except psutil.Error:
+ logger.warning("Could not signal child pid=%s", child.pid, exc_info=True)
+
+ def pending() -> bool:
+ for parent in parents:
+ if parent.is_running():
+ remember_children(parent)
+ # poll() also reaps direct children after SIGKILL.
+ roots_alive = any([proc.process.poll() is None for proc in active])
+ children_alive = False
+ for child in list(descendants.values()):
+ if child_alive(child):
+ children_alive = True
+ remember_children(child)
+ return roots_alive or children_alive
+
+ if not active and not descendants:
return
- _log(_t("start.stopping", name=proc.name, pid=proc.process.pid))
- target = f"{proc.name} (pid={proc.process.pid})"
- _signal_target(proc.process.pid, proc.pgid, signal.SIGTERM, target=target)
- try:
- proc.process.wait(timeout=8)
- except subprocess.TimeoutExpired:
- _signal_target(proc.process.pid, proc.pgid, KILL_SIGNAL, target=target)
+ wake = force if force is not None else threading.Event()
+ signal_trees(signal.SIGTERM)
+ deadline = time.monotonic() + SHUTDOWN_GRACE_SECONDS
+ while pending() and not wake.is_set() and time.monotonic() < deadline:
+ wake.wait(min(0.05, max(0.0, deadline - time.monotonic())))
+ if pending() or any(_group_alive(proc) for proc in active):
+ signal_trees(KILL_SIGNAL)
+ # One shared deadline, rather than a timeout for each child.
+ deadline = time.monotonic() + SHUTDOWN_REAP_SECONDS
+ for proc in active:
+ try:
+ proc.process.wait(timeout=max(0.0, deadline - time.monotonic()))
+ except subprocess.TimeoutExpired:
+ logger.warning(
+ "Process did not exit after kill: %s (pid=%s)", proc.name, proc.process.pid
+ )
+ remaining = [child for child in descendants.values() if child_alive(child)]
+ _, alive = psutil.wait_procs(remaining, timeout=max(0.0, deadline - time.monotonic()))
+ if alive:
+ logger.warning("Child processes did not exit after kill: %s", [p.pid for p in alive])
+
+
+def _terminate(proc: ManagedProcess | None) -> None:
+ if proc is not None:
+ _terminate_all([proc])
def _relax_console_encoding(streams: tuple[object, ...] | None = None) -> None:
@@ -1593,6 +1708,8 @@ def start(
# ECONNRESET ("Failed to proxy ... socket hang up" -> 500).
"--timeout-keep-alive",
str(HTTP_KEEP_ALIVE_TIMEOUT),
+ "--timeout-graceful-shutdown",
+ "1",
"--workers",
str(backend_workers),
]
@@ -1601,14 +1718,18 @@ def start(
backend: ManagedProcess | None = None
web: ManagedProcess | None = None
shutdown_requested = False
+ shutdown_wake = threading.Event()
+ force_shutdown = threading.Event()
cleanup_started = False
exit_code = 0
def request_shutdown(signal_name: str | None = None) -> None:
nonlocal shutdown_requested
if shutdown_requested:
+ force_shutdown.set()
return
shutdown_requested = True
+ shutdown_wake.set()
if signal_name:
_log(_t("start.received_signal", signal=signal_name))
@@ -1626,10 +1747,7 @@ def start(
if cleanup_started:
return
cleanup_started = True
- try:
- _terminate(backend)
- finally:
- _terminate(web)
+ _terminate_all(processes, force=force_shutdown)
if detached_paths is not None:
_clear_detached_runtime(detached_paths, detached_token)
@@ -1702,7 +1820,7 @@ def start(
exit_code = 1
shutdown_requested = True
break
- time.sleep(1)
+ shutdown_wake.wait(1)
except KeyboardInterrupt:
request_shutdown("SIGINT")
finally:
diff --git a/deeptutor/runtime/stream_bus.py b/deeptutor/runtime/stream_bus.py
index 1dcb0ffa7..135e828c7 100644
--- a/deeptutor/runtime/stream_bus.py
+++ b/deeptutor/runtime/stream_bus.py
@@ -82,19 +82,6 @@ class StreamBus:
Taking a snapshot never subscribes, waits for future output or emits an
event. Sequence zero remains zero for direct callers without sequencing.
"""
- visibility: dict[str, bool] = {}
- for event in self._history:
- if event.source != source or event.stage != stage:
- continue
- if event.type not in {StreamEventType.CONTENT, StreamEventType.PROGRESS}:
- continue
- call_id = event.metadata.get("call_id")
- if not isinstance(call_id, str) or not call_id:
- continue
- if event.metadata.get("finish_rejected") is True:
- visibility[call_id] = False
- elif isinstance(event.metadata.get("answer_visible"), bool):
- visibility[call_id] = event.metadata["answer_visible"]
return tuple(
(event.seq, event.timestamp, event.content)
for event in self._history
@@ -102,12 +89,6 @@ class StreamBus:
and event.source == source
and event.stage == stage
and event.content
- and event.metadata.get("answer_visible") is not False
- and event.metadata.get("finish_rejected") is not True
- and (
- not isinstance(event.metadata.get("call_id"), str)
- or visibility.get(event.metadata["call_id"], True)
- )
)
async def emit(self, event: StreamEvent) -> None:
diff --git a/deeptutor/services/partners/runtime.py b/deeptutor/services/partners/runtime.py
index 6476497d5..fc1aadb8b 100644
--- a/deeptutor/services/partners/runtime.py
+++ b/deeptutor/services/partners/runtime.py
@@ -593,10 +593,6 @@ class PartnerRunner:
# Already streamed live — freeze the segment.
ended_rounds.add(call_id)
await self._publish_stream_end(msg, turn_id, call_id)
- elif meta.get("answer_visible") is False:
- # A capability retracted this round; it was
- # never the reader's to see.
- pass
elif not is_im:
# The web surface renders each round itself,
# in place. Folding the text into the reply
diff --git a/deeptutor/services/session/_turn_runtime_shared.py b/deeptutor/services/session/_turn_runtime_shared.py
index f53e1a6d2..379b9f61e 100644
--- a/deeptutor/services/session/_turn_runtime_shared.py
+++ b/deeptutor/services/session/_turn_runtime_shared.py
@@ -32,8 +32,7 @@ MemoryReference = Literal["recent", "profile", "scope", "preferences", "summary"
# Content call_kinds that make up the persisted answer. The chat agent loop
# streams every round's text as ``content`` with ``agent_loop_round``, and all
# of it is the answer — the commentary a round wrote before calling a tool
-# included. Only a round a capability retracted is filtered back out (see
-# _retracted_round_call_id).
+# included. Published content is never retracted.
_ANSWER_CONTENT_CALL_KINDS = frozenset({"llm_final_response", "agent_loop_round"})
_FINAL_TURN_STATUSES = frozenset({"completed", "failed", "cancelled"})
@@ -101,38 +100,11 @@ def _resolve_turn_failure_metadata(
return "", False
-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.
-
- 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 (
- metadata.get("trace_kind") == "call_status"
- and metadata.get("call_state") == "complete"
- and metadata.get("answer_visible") is False
- ):
- call_id = metadata.get("call_id")
- return str(call_id) if call_id else None
- return None
-
-
def _assemble_persisted_answer(
content_segments: Sequence[tuple[str | None, str]],
- retracted_call_ids: set[str],
) -> str:
- """Replay visible content bytes, excluding rounds a capability retracted."""
- return clean_thinking_tags(
- "".join(
- text
- for call_id, text in content_segments
- if not (call_id and call_id in retracted_call_ids)
- )
- )
+ """Replay all published answer text, including commentary before tool calls."""
+ return clean_thinking_tags("".join(text for _call_id, text in content_segments))
_FENCED_CODE_BLOCK_RE = re.compile(r"```[\s\S]*?```")
diff --git a/deeptutor/services/session/turns/executor.py b/deeptutor/services/session/turns/executor.py
index 4f649a0f7..69635f9b1 100644
--- a/deeptutor/services/session/turns/executor.py
+++ b/deeptutor/services/session/turns/executor.py
@@ -48,7 +48,6 @@ from .._turn_runtime_shared import (
_resolve_side_chat_context,
_resolve_turn_failure_metadata,
_resolve_turn_outcome,
- _retracted_round_call_id,
_should_capture_assistant_content,
_stamp_content_offset,
_topic_material_manifest,
@@ -151,19 +150,15 @@ class TurnExecutor:
assistant_content = ""
provider_response_state: dict[str, Any] | None = None
context: UnifiedContext | None = None
- # Per-round content segments + retracted call_ids: every chat-loop
- # round's text is captured live and kept, commentary written before a
- # tool call included. Only a round a capability retracted is dropped
- # from the persisted answer (mirrors the frontend bubble).
+ # Keep every published answer segment, including commentary before tools.
content_segments: list[tuple[str | None, str]] = []
- retracted_call_ids: set[str] = set()
def _persisted_answer() -> str:
# clean_thinking_tags is a second line of defence: providers that
# inline <think> in the content channel are split at streaming
# time by the agent loop, but anything that slips through must
# never be persisted as the user-facing answer.
- return _assemble_persisted_answer(content_segments, retracted_call_ids)
+ return _assemble_persisted_answer(content_segments)
# Files the model generated this turn (exec artifacts),
# persisted as assistant-message attachments so the UI shows openable
@@ -1080,9 +1075,6 @@ class TurnExecutor:
if _should_capture_assistant_content(event):
call_id = (event.metadata or {}).get("call_id")
content_segments.append((str(call_id) if call_id else None, event.content))
- retracted_call_id = _retracted_round_call_id(event)
- if retracted_call_id:
- retracted_call_ids.add(retracted_call_id)
for attachment in artifact_attachments(event):
if attachment["url"] not in seen_artifact_urls:
seen_artifact_urls.add(attachment["url"])
@@ -1129,9 +1121,8 @@ class TurnExecutor:
# already unwinding and must not start new blocking work.
await fill_preview_text(generated_attachments)
- # The persisted answer is the captured content minus any round a
- # capability retracted (that text stayed in the trace). Apply
- # the CJK Markdown repair only after every streamed segment has
+ # Persist every published content segment. Apply the CJK Markdown
+ # repair only after every streamed segment has
# arrived, so no incomplete response is ever rewritten.
assistant_content = _repair_chinese_emphasis_for_persistence(
_persisted_answer(),
diff --git a/deeptutor_cli/common.py b/deeptutor_cli/common.py
index 8da4193df..00fb5e579 100644
--- a/deeptutor_cli/common.py
+++ b/deeptutor_cli/common.py
@@ -432,7 +432,7 @@ class TurnStreamRenderer:
if call_state == "complete":
call_role = str(metadata.get("call_role") or "")
if call_role in {"narration", "finish"}:
- if call_role == "narration" and metadata.get("answer_visible") is True:
+ if call_role == "narration":
call_role = "finish"
self._settle_round(str(metadata.get("call_id") or ""), role=call_role)
return
diff --git a/deeptutor_cli/main.py b/deeptutor_cli/main.py
index cb77f26f6..bc37cef22 100644
--- a/deeptutor_cli/main.py
+++ b/deeptutor_cli/main.py
@@ -217,6 +217,7 @@ def serve(
proxy_headers=False,
ws_max_size=get_ws_max_size(),
timeout_keep_alive=HTTP_KEEP_ALIVE_TIMEOUT,
+ timeout_graceful_shutdown=1,
)
diff --git a/packages/chat-engine/src/native/model-round.ts b/packages/chat-engine/src/native/model-round.ts
index 63ba1a386..b41e9969f 100644
--- a/packages/chat-engine/src/native/model-round.ts
+++ b/packages/chat-engine/src/native/model-round.ts
@@ -52,7 +52,6 @@ export async function modelRound(
let visible = "",
inlineReasoning = "";
const drafts = new AskUserDraft(emit, trace);
- const forced = first && !!input.initialToolChoice;
const piece = async (kind: "content" | "thinking", text: string) => {
if (!text) return;
if (kind === "content") visible += text;
@@ -62,7 +61,7 @@ export async function modelRound(
content: text,
metadata: { ...trace, trace_kind: "llm_chunk" },
};
- if (kind === "content" && (input.deferOutput || forced)) {
+ if (kind === "content" && input.deferOutput) {
const last = deferred.at(-1);
if (last?.type === "content" && last.content.length + text.length <= 8192)
last.content += text;
@@ -101,11 +100,6 @@ export async function modelRound(
}
for (const call of response.calls) await drafts.observe(call, true);
for (const [kind, text] of filter.feed("", true)) await piece(kind, text);
- if (forced && response.calls.length && !input.deferOutput)
- deferred.length = 0;
- // Providers that ignore forced tool choice may still give a valid answer.
- if (forced && !response.calls.length && !input.deferOutput)
- for (const event of deferred.splice(0)) await emit(event);
const usage = Object.keys(response.usage).length
? response.usage
: {
@@ -120,7 +114,7 @@ export async function modelRound(
trace_kind: "call_status",
call_state: "complete",
call_role: response.calls.length ? "narration" : "finish",
- answer_visible: !(forced && response.calls.length),
+ answer_visible: true,
finish_reason: response.finishReason || "stop",
requested_max_tokens: input.seed.outputTokens,
usage_reported: Object.keys(response.usage).length > 0,
@@ -134,7 +128,7 @@ export async function modelRound(
deferred.push({ type: "progress", content: "", metadata });
else await emit({ type: "progress", content: "", metadata });
return {
- text: forced && response.calls.length ? "" : visible,
+ text: visible,
reasoning: inlineReasoning,
toolCalls: response.calls,
usage,
diff --git a/packages/chat-engine/src/native/session.ts b/packages/chat-engine/src/native/session.ts
index 15e631c26..22417ca9a 100644
--- a/packages/chat-engine/src/native/session.ts
+++ b/packages/chat-engine/src/native/session.ts
@@ -100,12 +100,12 @@ export class NativeSession implements LoopHost {
}
private async release(discard = false): Promise<void> {
if (!this.round) return;
- if (discard) {
+ if (discard && this.input.deferOutput) {
this.round.deferred.length = 0;
await this.emit({
type: "progress",
content: "",
- metadata: { ...this.round.trace, answer_visible: false },
+ metadata: { ...this.round.trace, call_role: "narration" },
});
} else
for (const event of this.round.deferred.splice(0)) await this.emit(event);
@@ -147,8 +147,15 @@ export class NativeSession implements LoopHost {
}
private async finish(override: boolean): Promise<void> {
if (!this.round) throw new Error("Missing model round");
- const text = override ? (this.override ?? "") : this.round.text.trim();
+ let text = override ? (this.override ?? "") : this.round.text.trim();
if (override) {
+ if (!this.input.deferOutput && this.round.text) {
+ this.continued.push(this.round.text);
+ if (text.includes(this.round.text))
+ text = text.replace(this.round.text, "").trim();
+ if (!this.round.toolCalls.length)
+ this.conversation.appendAssistant(this.round);
+ }
await this.release(true);
await this.legacy("publish_override", { text });
this.conversation.messages.push({ role: "assistant", content: text });
@@ -253,7 +260,11 @@ export class NativeSession implements LoopHost {
this.redirect = String(policy.redirect || "");
this.override =
typeof policy.override === "string" ? policy.override : null;
- if (this.redirect) await this.release(true);
+ if (this.redirect) {
+ if (!this.input.deferOutput && this.round?.text)
+ this.continued.push(this.round.text);
+ await this.release(true);
+ }
return { redirect: !!this.redirect, override: this.override !== null };
}
case "redirect_finish":
@@ -265,6 +276,7 @@ export class NativeSession implements LoopHost {
"finish_rejected",
"The required capability action was not completed.",
);
+ this.response = this.continued.filter(Boolean).join("\n\n");
this.completed = false;
return null;
case "finish":
@@ -369,6 +381,12 @@ export class NativeSession implements LoopHost {
const policy = await this.policy(this.round.text);
if (policy.redirect) {
await this.release(true);
+ this.response = [
+ ...this.continued,
+ this.input.deferOutput ? "" : this.round.text,
+ ]
+ .filter(Boolean)
+ .join("\n\n");
this.completed = false;
return { extraRounds: 1 };
}
diff --git a/packages/chat-engine/test/native-engine.test.ts b/packages/chat-engine/test/native-engine.test.ts
index 7fd614114..0735cebb7 100644
--- a/packages/chat-engine/test/native-engine.test.ts
+++ b/packages/chat-engine/test/native-engine.test.ts
@@ -889,3 +889,150 @@ test("a learner message received during a tool is read once before the next mode
assert.deepEqual(read[0]!.metadata.user_inputs, [{ command_id: "cmd-1", content: "Only cover the discrete case." }]);
} finally { await fixture.close(); }
});
+
+for (const repeated of [false, true]) {
+ test(`finish checks preserve public explanations (repeated=${repeated})`, async () => {
+ const explanation = "B also works under the assumption you raised.";
+ const correction = "The question needs that assumption stated explicitly.";
+ const fixture = await server((_body, response, count) =>
+ answer(response, count === 1 ? explanation : correction),
+ );
+ const events: EngineEvent[] = [];
+ let checks = 0;
+ try {
+ const config = input(fixture.endpoint);
+ config.policyHooks = true;
+ const output = await runNative(
+ config,
+ async (operation, args) => {
+ if (operation === "event") events.push(args.event as EngineEvent);
+ if (operation === "policy") {
+ checks++;
+ return {
+ redirect:
+ repeated || checks === 1
+ ? "Check the failed tool operation."
+ : "",
+ override: null,
+ };
+ }
+ return null;
+ },
+ new AbortController().signal,
+ );
+ const content = events
+ .filter((e) => e.type === "content")
+ .map((e) => e.content)
+ .join("");
+ assert.equal(content, explanation + correction);
+ assert.ok(output.response.includes(explanation));
+ assert.ok(output.response.includes(correction));
+ assert.equal(output.completed, !repeated);
+ assert.ok(events.every((e) => e.metadata?.answer_visible !== false));
+ assert.equal(fixture.requests.length, 2);
+ } finally {
+ await fixture.close();
+ }
+ });
+}
+
+test("public answer survives a capability notice without duplication", async () => {
+ const explanation = "B also satisfies the stated condition.";
+ const notice = "The optional profile update was not saved.";
+ const fixture = await server((_body, response) =>
+ answer(response, explanation),
+ );
+ const events: EngineEvent[] = [];
+ try {
+ const config = input(fixture.endpoint);
+ config.policyHooks = true;
+ const output = await runNative(
+ config,
+ async (operation, args) => {
+ if (operation === "event") events.push(args.event as EngineEvent);
+ if (operation === "policy")
+ return { redirect: "", override: notice + "\n\n" + explanation };
+ if (operation === "publish_override") {
+ events.push({
+ type: "content",
+ content: String(args.text),
+ metadata: {},
+ });
+ }
+ return null;
+ },
+ new AbortController().signal,
+ );
+ const content = events
+ .filter((e) => e.type === "content")
+ .map((e) => e.content)
+ .join("");
+ assert.equal(content, explanation + notice);
+ assert.equal(output.response, explanation + "\n\n" + notice);
+ assert.equal(output.completed, true);
+ } finally {
+ await fixture.close();
+ }
+});
+
+test("forced tool choice preserves the accompanying explanation", async () => {
+ const { modelRound } = await import("../src/native/model-round.js");
+ const explanation = "Let us clarify your objection first.";
+ const fixture = await server((_body, response) => {
+ frame(response, { content: explanation });
+ frame(
+ response,
+ {
+ tool_calls: [
+ {
+ index: 0,
+ id: "ask",
+ function: {
+ name: "ask_user",
+ arguments: '{"questions":[{"id":"q","prompt":"Which premise?"}]}',
+ },
+ },
+ ],
+ },
+ "tool_calls",
+ );
+ response.end("data: [DONE]\n\n");
+ });
+ try {
+ const config = input(fixture.endpoint);
+ config.initialToolChoice = "ask_user";
+ const events: EngineEvent[] = [];
+ const round = await modelRound(
+ config,
+ new Conversation(config.seed),
+ [
+ {
+ type: "function",
+ function: {
+ name: "ask_user",
+ description: "Clarify",
+ parameters: { type: "object", properties: {} },
+ },
+ },
+ ],
+ async (event) => {
+ events.push(event);
+ },
+ new AbortController().signal,
+ false,
+ true,
+ );
+ assert.equal(round.text, explanation);
+ assert.equal(
+ events
+ .filter((e) => e.type === "content")
+ .map((e) => e.content)
+ .join(""),
+ explanation,
+ );
+ assert.ok(events.every((e) => e.metadata?.answer_visible !== false));
+ assert.equal(round.toolCalls.length, 1);
+ } finally {
+ await fixture.close();
+ }
+});
diff --git a/tests/agents/chat/test_agent_loop.py b/tests/agents/chat/test_agent_loop.py
index 248895570..d5f274ed6 100644
--- a/tests/agents/chat/test_agent_loop.py
+++ b/tests/agents/chat/test_agent_loop.py
@@ -609,41 +609,16 @@ def _contents(events: list[StreamEvent]) -> list[str]:
def _answer_text(events: list[StreamEvent]) -> str:
- """The reply a reader is left with, by the same rule the client applies.
-
- Mirrors ``recomputeAnswerContent`` in ``web/lib/stream.ts``: append the
- content of every answer-bearing round, then take back out any round whose
- completion marker retracted it (``answer_visible: False``). Commentary a
- round wrote before calling a tool is kept — the reader watched it arrive.
-
- Rounds stream optimistically, so text that a capability's finish guard
- rejects *is* briefly on the wire and then withdrawn. Asserting on this
- function rather than on ``_contents`` therefore tests the property that
- actually matters — what the reader ends up holding — instead of whether a
- rejected round was buffered, which is an implementation choice.
- """
- retracted = {
- str(event.metadata.get("call_id"))
+ """Published answer chunks remain visible regardless of completion markers."""
+ return "".join(
+ event.content
for event in events
- if event.type == StreamEventType.PROGRESS
- and event.metadata.get("trace_kind") == "call_status"
- and event.metadata.get("call_state") == "complete"
- and event.metadata.get("answer_visible") is False
- and event.metadata.get("call_id")
- }
- parts: list[str] = []
- for event in events:
- if event.type != StreamEventType.CONTENT:
- continue
- metadata = event.metadata or {}
- call_id = metadata.get("call_id")
- if call_id:
- if metadata.get("call_kind") not in ANSWER_BEARING_CALL_KINDS:
- continue
- if str(call_id) in retracted:
- continue
- parts.append(event.content)
- return "".join(parts)
+ if event.type == StreamEventType.CONTENT
+ and (
+ not event.metadata.get("call_id")
+ or event.metadata.get("call_kind") in ANSWER_BEARING_CALL_KINDS
+ )
+ )
def _call_roles(events: list[StreamEvent]) -> list[str]:
@@ -1479,11 +1454,11 @@ async def test_mastery_tool_round_keeps_teaching_markdown_visible(
),
],
)
-async def test_mastery_plain_choice_finish_gets_one_protocol_redirect(
+async def test_mastery_choice_explanation_is_not_redirected(
plain_quiz: str,
monkeypatch: pytest.MonkeyPatch,
) -> None:
- """A prose A-D quiz cannot bypass the registered-card assessment flow."""
+ """Choice-shaped prose remains visible without forcing a registered card."""
class _MasteryCardRegistry(_Registry):
def build_openai_schemas(self, enabled):
@@ -1542,19 +1517,11 @@ async def test_mastery_plain_choice_finish_gets_one_protocol_redirect(
),
)
- assert client.call_count == 2
- assert [row["name"] for row in registry.executed] == ["mastery_quiz"]
- redirect = client.calls[1]["messages"][-1]
- assert redirect["role"] == "user"
- assert "plain text" in redirect["content"]
- assert "mastery_quiz" in redirect["content"]
- # The card comes from mastery_quiz itself now; pointing the model at
- # ask_user would send it back to a flow that no longer exists.
- assert "ask_user" not in redirect["content"]
- assert _result(events).metadata["completed"] is False
- # The rejected round streamed, so its text was briefly on the wire; what
- # matters is that the redirect took it back out of the reply.
- assert plain_quiz not in _answer_text(events)
+ assert client.call_count == 1
+ assert not registry.executed
+ assert _result(events).metadata["completed"] is True
+ assert _result(events).metadata["response"] == plain_quiz
+ assert _answer_text(events) == plain_quiz
@pytest.mark.asyncio
@@ -1623,10 +1590,10 @@ async def test_graded_review_reaches_the_learner(
@pytest.mark.asyncio
-async def test_repeated_plain_choice_failure_is_never_published_as_a_finish(
+async def test_choice_prose_does_not_trigger_repeated_model_calls(
monkeypatch: pytest.MonkeyPatch,
) -> None:
- """One retry is bounded, but its second invalid quiz still cannot finish."""
+ """A choice-shaped reply must not be sent through an automatic retry."""
plain_quiz = "Which value is correct?\n\nA. one\nB. two\nC. three\nD. four"
client = _ScriptedChatClient(
[[_llm_chunk(content=plain_quiz)], [_llm_chunk(content=plain_quiz)]]
@@ -1646,21 +1613,10 @@ async def test_repeated_plain_choice_failure_is_never_published_as_a_finish(
),
)
- assert client.call_count == 2
- # Both rounds streamed and both were withdrawn by their narration marker,
- # so neither invalid quiz survives in the reply.
- assert plain_quiz not in _answer_text(events)
- rejected_calls = [
- event.metadata
- for event in events
- if event.type == StreamEventType.PROGRESS and event.metadata.get("finish_rejected") is True
- ]
- assert len(rejected_calls) == 2
- assert all(metadata["call_state"] == "complete" for metadata in rejected_calls)
- assert all(metadata["call_role"] == "narration" for metadata in rejected_calls)
- result = _result(events)
- assert result.metadata["completed"] is False
- assert result.metadata["response"] == ""
+ assert client.call_count == 1
+ assert _answer_text(events) == plain_quiz
+ assert not any(event.metadata.get("answer_visible") is False for event in events)
+ assert _result(events).metadata["completed"] is True
@pytest.mark.asyncio
@@ -1679,7 +1635,7 @@ async def test_repeated_plain_choice_failure_is_never_published_as_a_finish(
async def test_mastery_card_promise_repair_uses_real_question_tool(
repair: str, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
- """A text-only claim repairs to a durable card or a bounded failed turn."""
+ """Only a selected tool operation needs repair; prose remains visible."""
monkeypatch.setenv("DEEPTUTOR_WORKSPACE_ROOT", str(tmp_path))
def init_store(self: LearningStore, root: Path | None = None) -> None:
@@ -1816,7 +1772,16 @@ async def test_mastery_card_promise_repair_uses_real_question_tool(
if e.type == StreamEventType.TOOL_RESULT
and "mastery_question" in e.metadata.get("tool_metadata", {})
]
- assert promise not in _answer_text(events)
+ if not selected_quiz:
+ assert client.call_count == 1
+ assert promise == _answer_text(events)
+ assert not cards
+ assert _result(events).metadata["completed"] is True
+ pending = LearningStore().load("p1").pending_question
+ assert (pending.question_id if pending else None) == old_id
+ return
+ if repair != "selected_budget":
+ assert promise in _answer_text(events)
if repair == "selected_budget":
# One exploration round plus three settlement rounds; no tool-less
# salvage call can discharge an unfulfilled quiz obligation.
@@ -1840,7 +1805,8 @@ async def test_mastery_card_promise_repair_uses_real_question_tool(
else:
assert cards == []
assert LearningStore().load("p1").pending_question is None
- assert _result(events).metadata["response"] == ""
+ if repair != "selected_budget":
+ assert promise in _result(events).metadata["response"]
@pytest.mark.asyncio
@@ -2235,17 +2201,11 @@ async def test_truncated_round_with_visible_text_still_continues(
async def test_forced_tool_round_keeps_inline_reasoning_in_the_trace(
monkeypatch: pytest.MonkeyPatch,
) -> None:
- """A forced tool round withholds its prose, not the model's thinking.
-
- Some reasoning models write their deliberation inline in the content
- channel wrapped in ``<think>`` tags. A forced round (Ask Questions) skipped
- the whole emit path to keep its prose out of the answer, which discarded
- that reasoning with it — the round showed no thinking at all.
- """
+ """Forced tool rounds preserve prose while routing reasoning to the trace."""
client = _ScriptedChatClient(
[
[
- _llm_chunk(content="<think>先想清楚要问什么</think>"),
+ _llm_chunk(content="<think>先想清楚要问什么</think>先确认你的目标。"),
_llm_chunk(
tool_calls=[
{
@@ -2273,6 +2233,7 @@ async def test_forced_tool_round_keeps_inline_reasoning_in_the_trace(
assert "先想清楚要问什么" in thinking
# The reasoning is trace-only: it must not become the answer.
assert "先想清楚要问什么" not in _answer_text(events)
+ assert "先确认你的目标。" in _answer_text(events)
@pytest.mark.asyncio
@@ -3176,7 +3137,7 @@ async def test_ask_user_available_every_round(monkeypatch: pytest.MonkeyPatch) -
@pytest.mark.asyncio
-async def test_initial_tool_choice_only_forces_first_round_and_hides_preamble(
+async def test_initial_tool_choice_preserves_preamble_and_only_forces_first_round(
monkeypatch: pytest.MonkeyPatch,
) -> None:
class _PausingRegistry(_Registry):
@@ -3230,7 +3191,7 @@ async def test_initial_tool_choice_only_forces_first_round_and_hides_preamble(
"function": {"name": "ask_user"},
}
assert client.calls[1]["tool_choice"] == "auto"
- assert _contents(events) == ["Completed with the added context."]
+ assert _contents(events) == ["Let me ask one thing first.", "Completed with the added context."]
@pytest.mark.asyncio
@@ -3296,7 +3257,7 @@ async def test_ask_questions_uses_a_card_when_provider_rejects_tool_schemas(
"allow_free_text": True,
}
]
- assert _contents(events) == ["Completed after clarification."]
+ assert _contents(events) == ["Which outcome matters most?", "Completed after clarification."]
assert any(event.metadata.get("tool_schema_fallback") for event in events)
@@ -4270,3 +4231,49 @@ async def test_image_fallback_strips_all_canonical_copies_after_wire_deduplicati
assert not has_image_parts(messages)
assert context.extension("source_visual_evidence")["image_hashes"] == []
assert "Repeated image" not in str(messages)
+
+
+async def test_mastery_objection_preserves_option_by_option_explanation(monkeypatch):
+ explanation = (
+ "你的异议有道理,这道题的答案需要补充前提。\n"
+ "A. 在原来的前提下成立。\nB. 在你提出的前提下也成立。\n"
+ "C. 缺少条件,不能唯一确定。\nD. 不能直接排除。"
+ )
+ client = _ScriptedChatClient([[_llm_chunk(content=explanation)]])
+ pipeline = AgenticChatPipeline(language="zh")
+ pipeline.registry = _Registry()
+ monkeypatch.setattr(pipeline, "_compose_enabled_tools", lambda _context: ["ask_user"])
+ monkeypatch.setattr(pipeline, "_build_openai_client", lambda: client)
+ context = UnifiedContext(
+ session_id="objection",
+ user_message="我认为 B 也对,为什么答案只有 A?请先解释,不要继续出题。",
+ metadata={"mastery_mode": True, "mastery_path_id": ""},
+ )
+ context.extension("mastery")["quiz_awaiting_grade"] = True
+ events = await _run(pipeline, context)
+ assert client.call_count == 1
+ assert _answer_text(events) == explanation
+ assert _result(events).metadata["response"] == explanation
+ assert _result(events).metadata["completed"] is True
+ assert not any(e.metadata.get("answer_visible") is False for e in events)
+
+
+async def test_public_answer_with_capability_notice_is_preserved_once(monkeypatch):
+ explanation = "Your objection is valid because B also satisfies the condition."
+ notice = "The profile change could not be saved."
+ client = _ScriptedChatClient([[_llm_chunk(content=explanation)]])
+ pipeline = AgenticChatPipeline(language="en")
+ pipeline.registry = _Registry()
+ monkeypatch.setattr(pipeline, "_compose_enabled_tools", lambda _: ["ask_user"])
+ monkeypatch.setattr(pipeline, "_build_openai_client", lambda: client)
+ monkeypatch.setattr(
+ pipeline, "_capability_final_text_override", lambda _, text: notice + "\n\n" + text
+ )
+ events = await _run(pipeline, UnifiedContext(user_message="Why not B?"))
+ answer = _answer_text(events)
+ assert answer.count(explanation) == 1
+ assert answer.count(notice) == 1
+ assert answer.startswith(explanation)
+ assert _result(events).metadata["response"] == answer
diff --git a/tests/app/test_container_shutdown.py b/tests/app/test_container_shutdown.py
new file mode 100644
index 000000000..7bc8a815e
--- /dev/null
+++ b/tests/app/test_container_shutdown.py
@@ -0,0 +1,57 @@
+"""Shutdown cancels active work while still awaiting its resource cleanup."""
+
+from __future__ import annotations
+
+import asyncio
+from types import SimpleNamespace
+from unittest.mock import AsyncMock
+
+import pytest
+
+from deeptutor.app.container import ApplicationContainer, RuntimeRegistry
+from deeptutor.services.session.turns.lifecycle import TurnLifecycle
+
+
+async def test_container_cancels_turn_and_waits_for_cleanup(monkeypatch):
+ events = []
+ ready = asyncio.Event()
+ execution = SimpleNamespace(task=None, subscribers=[], shutdown_requested=False)
+
+ async def run_turn():
+ try:
+ ready.set()
+ await asyncio.Event().wait()
+ finally:
+ assert execution.shutdown_requested
+ await asyncio.sleep(0.01) # asynchronous persistence/subprocess cleanup
+ events.append("turn cleaned")
+
+ async def close_store():
+ events.append("store closed")
+
+ runtime = object.__new__(TurnLifecycle)
+ runtime._lock = asyncio.Lock()
+ runtime._accepting_turns = True
+ runtime._reply_queues = {}
+ runtime._inboxes = {}
+ runtime.store = SimpleNamespace(close=close_store)
+ execution.task = asyncio.create_task(run_turn())
+ runtime._executions = {"turn": execution}
+ registry = object.__new__(RuntimeRegistry)
+ registry._runtimes = {"workspace": runtime}
+ container = object.__new__(ApplicationContainer)
+ container._started = True
+ container.runtime_registry = registry
+ container.coordinator = SimpleNamespace(close=AsyncMock())
+ monkeypatch.setattr("deeptutor.runtime.memory_reclaim.cancel_idle_reclaim", lambda: None)
+ monkeypatch.setattr("deeptutor.runtime.ts_engine.close_engine_clients", AsyncMock())
+
+ await ready.wait()
+ await asyncio.wait_for(container.close(), timeout=0.5)
+
+ assert events == ["turn cleaned", "store closed"]
+ assert execution.task.done()
+ assert not runtime._accepting_turns
+ assert not container._started
+ container.coordinator.close.assert_awaited_once()
diff --git a/tests/capabilities/test_mastery_capability.py b/tests/capabilities/test_mastery_capability.py
index 95578d960..4972d8f5b 100644
--- a/tests/capabilities/test_mastery_capability.py
+++ b/tests/capabilities/test_mastery_capability.py
@@ -163,38 +163,29 @@ def test_open_question_still_allows_answering_the_learner():
assert capability.finish_instruction(context, "会的,路由失败时它会重写查询再试。") is None
-def test_open_question_may_not_be_restated_in_prose():
- """Re-posing the open question as text is still redirected to the card."""
+def test_open_question_allows_option_by_option_discussion():
+ """Discussing the open question does not force grading or a new card."""
capability = MasteryLoopCapability()
context = _context()
context.extension("mastery")["quiz_awaiting_grade"] = True
instruction = capability.finish_instruction(context, _GRADED_REVIEW)
- assert instruction is not None
- assert "mastery_grade" in instruction
+ assert instruction is None
-def test_plain_text_quiz_is_redirected_to_the_card():
- """Posing a question as prose is still rejected before any grading."""
+def test_choice_prose_does_not_force_a_card():
+ """Choice-shaped text is ordinary dialogue until a quiz tool is called."""
instruction = MasteryLoopCapability().finish_instruction(_context(), _GRADED_REVIEW)
- assert instruction is not None
- assert "mastery_quiz" in instruction
- assert "ask_user" not in instruction
+ assert instruction is None
-def test_announced_but_unposed_question_is_redirected():
- """An announced-but-unposed question is not a finished turn.
-
- Posing a question ends the turn, so a reply that only announces one leaves
- the learner reading a promise above an empty space with no way to answer.
- """
+def test_question_wording_does_not_force_a_tool():
+ """A colon or question wording must not trigger mandatory tool work."""
instruction = MasteryLoopCapability().finish_instruction(
_context(), "让我们通过这道题来看看你对状态定义的掌握程度:"
)
- assert instruction is not None
- assert "mastery_quiz" in instruction
- assert "SAME round" in instruction
+ assert instruction is None
@pytest.mark.parametrize(
@@ -205,11 +196,10 @@ def test_announced_but_unposed_question_is_redirected():
"Four options, choose one.",
],
)
-def test_unposted_choice_lead_in_requires_a_card(reply: str) -> None:
- """#1508: naming unseen choices cannot finish a mastery turn successfully."""
+def test_choice_wording_does_not_control_turn_completion(reply: str) -> None:
+ """Choice wording alone does not establish a pending tool operation."""
instruction = MasteryLoopCapability().finish_instruction(_context(), reply)
- assert instruction is not None
- assert "mastery_quiz" in instruction
+ assert instruction is None
def test_existing_question_can_remind_learner_to_choose() -> None:
@@ -237,14 +227,13 @@ def test_existing_question_can_remind_learner_to_choose() -> None:
"I have reopened the card. Choose an option.",
],
)
-def test_card_delivery_claim_needs_a_posted_card(reply: str) -> None:
- """Real lead-ins without any question must enter the existing repair path."""
+def test_card_wording_does_not_control_turn_completion(reply: str) -> None:
+ """Mentioning a card cannot force the tutor away from a discussion."""
context = _context()
# A previous grade must not exempt a promise to pose the next question.
context.metadata["mastery_card_grade"] = {"is_correct": True}
instruction = MasteryLoopCapability().finish_instruction(context, reply)
- assert instruction is not None
- assert "mastery_quiz" in instruction
+ assert instruction is None
@pytest.mark.parametrize(
diff --git a/tests/capabilities/test_mastery_concept_card_contract.py b/tests/capabilities/test_mastery_concept_card_contract.py
index 8859cf851..c13b43f83 100644
--- a/tests/capabilities/test_mastery_concept_card_contract.py
+++ b/tests/capabilities/test_mastery_concept_card_contract.py
@@ -18,14 +18,13 @@ def test_quiz_schema_allows_practice_cards_for_all_objective_types():
assert "use mastery_assess instead" not in description
-def test_group_finish_nudge_preserves_concept_practice_and_separate_gate():
+def test_practice_discussion_does_not_force_an_assessment():
instruction = MasteryLoopCapability().finish_instruction(
UnifiedContext(metadata={"mastery_mode": True}),
"Answer these two practice questions; I will grade both answers.\n"
"1. Which change increases the rate?\n2. Explain why it increases?",
)
- assert "all objective types" in instruction
- assert "qualitative gate" in instruction and "mastery_assess" in instruction
+ assert instruction is None
@pytest.mark.asyncio
diff --git a/tests/capabilities/test_mastery_group_delivery.py b/tests/capabilities/test_mastery_group_delivery.py
index 4e9da4c6f..2713dcf2e 100644
--- a/tests/capabilities/test_mastery_group_delivery.py
+++ b/tests/capabilities/test_mastery_group_delivery.py
@@ -4,7 +4,8 @@ import json
import pytest
-from deeptutor.capabilities.mastery.loop import _looks_like_multipart_assessment
+from deeptutor.capabilities.mastery.loop import MasteryLoopCapability
+from deeptutor.core.context import UnifiedContext
from deeptutor.learning.models import (
KnowledgePoint,
KnowledgeType,
@@ -24,8 +25,13 @@ from deeptutor.learning.storage import LearningStore
"1. What is alpha?\n2. What is beta?\nAnswer these.",
],
)
-def test_multiline_numbered_assessment_requires_registered_card(text):
- assert _looks_like_multipart_assessment(text)
+def test_numbered_prose_is_not_a_reason_to_redirect(text):
+ assert (
+ MasteryLoopCapability().finish_instruction(
+ UnifiedContext(metadata={"mastery_mode": True}), text
+ )
+ is None
+ )
@pytest.mark.parametrize(
@@ -39,7 +45,12 @@ def test_multiline_numbered_assessment_requires_registered_card(text):
],
)
def test_teaching_and_quoted_examples_are_not_assessments(text):
- assert not _looks_like_multipart_assessment(text)
+ assert (
+ MasteryLoopCapability().finish_instruction(
+ UnifiedContext(metadata={"mastery_mode": True}), text
+ )
+ is None
+ )
@pytest.fixture
@@ -187,7 +198,7 @@ async def test_tool_resume_rejects_conflicting_payloads(service, monkeypatch, ex
@pytest.mark.asyncio
-async def test_guard_redirects_markdown_assessment_to_existing_card(service, monkeypatch):
+async def test_markdown_discussion_preserves_existing_card(service, monkeypatch):
from deeptutor.capabilities.mastery import tools
from deeptutor.capabilities.mastery.loop import MasteryLoopCapability
from deeptutor.core.context import UnifiedContext
@@ -199,7 +210,7 @@ async def test_guard_redirects_markdown_assessment_to_existing_card(service, mon
capability = MasteryLoopCapability()
draft = "### 1. First\nWhat is alpha?\n### 2. Second\nWhat is beta?\nAnswer these."
instruction = capability.finish_instruction(context, draft)
- assert "resume_question_id" in instruction
+ assert instruction is None
kwargs = capability.augment_kwargs(
"mastery_quiz",
{
diff --git a/tests/capabilities/test_mastery_teaching_delivery.py b/tests/capabilities/test_mastery_teaching_delivery.py
index e5c412063..231ca9d32 100644
--- a/tests/capabilities/test_mastery_teaching_delivery.py
+++ b/tests/capabilities/test_mastery_teaching_delivery.py
@@ -265,7 +265,7 @@ async def test_unknown_objective_cannot_gain_delivered_record(lesson):
@pytest.mark.asyncio
-async def test_retracted_optimistic_prose_cannot_supply_teaching_evidence(lesson):
+async def test_finish_check_cannot_retract_delivered_teaching(lesson):
bus = StreamBus(assign_seq=True)
await bus.content(
EXPLANATION,
@@ -283,12 +283,12 @@ async def test_retracted_optimistic_prose_cannot_supply_teaching_evidence(lesson
completion_metadata={"call_id": "rejected", "answer_visible": True},
)
)
- assert not (await note(bus)).success
- assert not lesson.store.load("p").explained_objectives
+ assert (await note(bus)).success
+ assert lesson.store.load("p").explained_objectives
@pytest.mark.asyncio
-async def test_visibility_corrections_are_scoped_to_the_matching_call():
+async def test_legacy_visibility_marker_cannot_retract_public_content():
bus = StreamBus(assign_seq=True)
await bus.content(
"discarded",
@@ -310,4 +310,4 @@ async def test_visibility_corrections_are_scoped_to_the_matching_call():
metadata={"call_id": "bad", "answer_visible": False},
)
snapshot = bus.public_content_snapshot(source="mastery_path", stage="responding")
- assert [part[2] for part in snapshot] == ["accepted", "callless"]
+ assert [part[2] for part in snapshot] == ["discarded", "accepted", "callless"]
diff --git a/tests/runtime/test_launcher.py b/tests/runtime/test_launcher.py
index e5d45e37e..79a96239b 100644
--- a/tests/runtime/test_launcher.py
+++ b/tests/runtime/test_launcher.py
@@ -862,8 +862,8 @@ def test_start_uses_ipv4_loopback_for_frontend_proxy(
monkeypatch.setattr(launcher, "_wait_for_http", wait_for_http)
monkeypatch.setattr(
launcher,
- "_terminate",
- lambda process: termination_order.append(process.name) if process else None,
+ "_terminate_all",
+ lambda processes, **_kwargs: termination_order.extend(p.name for p in processes),
)
def _capture_spawn(_command, *, cwd, env, name):
@@ -1193,6 +1193,8 @@ def test_terminate_warns_when_both_signal_rounds_fail(
monkeypatch, caplog: pytest.LogCaptureFixture
) -> None:
"""A child no signal reaches must surface every failed round."""
+ monkeypatch.setattr(launcher, "SHUTDOWN_GRACE_SECONDS", 0)
+ monkeypatch.setattr(launcher, "_group_alive", lambda _proc: False)
child = _StubbornPopen()
proc = launcher.ManagedProcess(name="backend", process=child, pgid=4242)
sent: list[int] = []
@@ -1217,6 +1219,8 @@ def test_terminate_warns_on_sigterm_but_accepts_kill_escalation(
monkeypatch, caplog: pytest.LogCaptureFixture
) -> None:
"""The ladder keeps its shape: refused SIGTERM, working SIGKILL, done."""
+ monkeypatch.setattr(launcher, "SHUTDOWN_GRACE_SECONDS", 0)
+ monkeypatch.setattr(launcher, "_group_alive", lambda _proc: False)
child = _StubbornPopen()
proc = launcher.ManagedProcess(name="backend", process=child, pgid=4242)
@@ -1241,6 +1245,8 @@ def test_terminate_skips_absent_or_exited_child(
monkeypatch, caplog: pytest.LogCaptureFixture
) -> None:
"""Nothing to stop means no signal and no warning."""
+ monkeypatch.setattr(launcher, "SHUTDOWN_GRACE_SECONDS", 0)
+ monkeypatch.setattr(launcher, "_group_alive", lambda _proc: False)
sent: list[int] = []
def fake_send(pid, pgid, sig):
diff --git a/tests/runtime/test_launcher_lifecycle.py b/tests/runtime/test_launcher_lifecycle.py
index 22777082f..4e8fa5dfb 100644
--- a/tests/runtime/test_launcher_lifecycle.py
+++ b/tests/runtime/test_launcher_lifecycle.py
@@ -3,8 +3,7 @@
Covers the tree-termination path (``_terminate`` / ``_send_tree_signal``),
port-listener cleanup (``_kill_port_listeners``) and marker-file corruption
tolerance for the packaged-web cache, the source production build and the
-detached-runtime state. Every child process is mocked — no real service is
-started and no product code is changed.
+detached-runtime state. Process shutdown integration tests live in test_launcher_shutdown.py.
"""
from __future__ import annotations
@@ -145,70 +144,6 @@ def test_send_tree_signal_windows_uses_taskkill_with_force_only_on_kill(
assert commands[1] == ["taskkill", "/PID", "4242", "/T", "/F"]
-# --------------------------------------------------------------------------
-# _terminate — SIGTERM, wait, then escalate to KILL
-# --------------------------------------------------------------------------
-
-
-def test_terminate_skips_none_and_already_finished_processes(monkeypatch) -> None:
- signals = _SignalRecorder()
- monkeypatch.setattr(launcher, "_send_tree_signal", signals)
- monkeypatch.setattr(launcher, "_log", lambda _message: None)
-
- launcher._terminate(None)
- launcher._terminate(_managed(4242, returncode=0))
-
- assert signals.calls == []
-
-
-def test_terminate_sends_sigterm_to_the_process_group(monkeypatch) -> None:
- signals = _SignalRecorder()
- monkeypatch.setattr(launcher, "_send_tree_signal", signals)
- monkeypatch.setattr(launcher, "_log", lambda _message: None)
-
- launcher._terminate(_managed(4242))
-
- assert signals.calls == [(4242, 4242, signal.SIGTERM)]
-
-
-def test_terminate_escalates_to_kill_when_sigterm_times_out(monkeypatch) -> None:
- signals = _SignalRecorder()
- monkeypatch.setattr(launcher, "_send_tree_signal", signals)
- monkeypatch.setattr(launcher, "_log", lambda _message: None)
- proc = _managed(4242, wait_error=subprocess.TimeoutExpired(cmd="uvicorn", timeout=8))
-
- launcher._terminate(proc)
-
- assert signals.calls == [
- (4242, 4242, signal.SIGTERM),
- (4242, 4242, launcher.KILL_SIGNAL),
- ]
-
-
-def test_terminate_swallows_signal_errors_and_still_reaps(monkeypatch) -> None:
- waited: list[float] = []
-
- def failing_signal(_pid, _pgid, _sig) -> None:
- raise OSError("signal delivery failed")
-
- class _ReapingProcess(_FakeProcess):
- def wait(self, timeout: float | None = None) -> int:
- waited.append(timeout or 0.0)
- return 0
-
- proc = launcher.ManagedProcess(
- name="backend",
- process=_ReapingProcess(4242),
- pgid=4242,
- )
- monkeypatch.setattr(launcher, "_send_tree_signal", failing_signal)
- monkeypatch.setattr(launcher, "_log", lambda _message: None)
-
- launcher._terminate(proc)
-
- assert waited == [8]
-
-
# --------------------------------------------------------------------------
# _kill_port_listeners — SIGTERM, wait for the port, escalate to KILL
# --------------------------------------------------------------------------
diff --git a/tests/runtime/test_launcher_shutdown.py b/tests/runtime/test_launcher_shutdown.py
new file mode 100644
index 000000000..c470ca4f1
--- /dev/null
+++ b/tests/runtime/test_launcher_shutdown.py
@@ -0,0 +1,159 @@
+"""Real process regressions for bounded, tree-wide launcher shutdown."""
+
+from __future__ import annotations
+
+import os
+from pathlib import Path
+import signal
+import subprocess
+import sys
+import threading
+import time
+
+import psutil
+import pytest
+
+from deeptutor.runtime import launcher
+
+pytestmark = pytest.mark.skipif(os.name == "nt", reason="POSIX sessions and signals")
+
+
+def _wait_file(path: Path) -> str:
+ deadline = time.monotonic() + 5
+ while time.monotonic() < deadline:
+ if path.exists() and (text := path.read_text()):
+ return text
+ time.sleep(0.01)
+ pytest.fail(f"test process did not become ready: {path}")
+
+
+def _alive(pid: int) -> bool:
+ try:
+ proc = psutil.Process(pid)
+ return proc.is_running() and proc.status() != psutil.STATUS_ZOMBIE
+ except psutil.NoSuchProcess:
+ return False
+
+
+def _spawn(code: str) -> launcher.ManagedProcess:
+ proc = subprocess.Popen([sys.executable, "-c", code], start_new_session=True)
+ return launcher.ManagedProcess("test-worker", proc, proc.pid)
+
+
+def _cleanup(proc: launcher.ManagedProcess, child_pids: list[int] = ()) -> None:
+ for pid in child_pids:
+ try:
+ psutil.Process(pid).kill()
+ except psutil.NoSuchProcess:
+ pass
+ try:
+ os.killpg(proc.pgid, signal.SIGKILL)
+ except ProcessLookupError:
+ pass
+ proc.process.wait(timeout=3)
+
+
+def test_terminates_services_together_and_reaps_stubborn_children(tmp_path, monkeypatch):
+ """Both services receive TERM before either service gets its grace period."""
+ monkeypatch.setattr(launcher, "SHUTDOWN_GRACE_SECONDS", 0.3)
+ workers = []
+ try:
+ for index in range(2):
+ ready, stopped = tmp_path / f"ready{index}", tmp_path / f"term{index}"
+ workers.append(
+ _spawn(f"""
+import signal, time
+from pathlib import Path
+signal.signal(signal.SIGTERM, lambda *_: Path({str(stopped)!r}).write_text(str(time.monotonic())))
+Path({str(ready)!r}).write_text('ready')
+while True: time.sleep(1)
+""")
+ )
+ _wait_file(ready)
+ started = time.monotonic()
+ launcher._terminate_all(workers)
+ elapsed = time.monotonic() - started
+ times = [float((tmp_path / f"term{i}").read_text()) for i in range(2)]
+ assert max(times) - started < 0.25
+ assert elapsed < 1.5
+ assert all(w.process.returncode == -signal.SIGKILL for w in workers)
+ finally:
+ for worker in workers:
+ _cleanup(worker)
+
+
+def test_kills_separate_session_child_after_parent_exits(tmp_path, monkeypatch):
+ monkeypatch.setattr(launcher, "SHUTDOWN_GRACE_SECONDS", 0.2)
+ ready = tmp_path / "child"
+ child_code = f"""
+import os, signal, time
+from pathlib import Path
+signal.signal(signal.SIGTERM, signal.SIG_IGN)
+Path({str(ready)!r}).write_text(str(os.getpid()))
+while True: time.sleep(1)
+"""
+ worker = _spawn(f"""
+import subprocess, sys, time
+subprocess.Popen([sys.executable, '-c', {child_code!r}], start_new_session=True)
+while True: time.sleep(1)
+""")
+ children = []
+ try:
+ children.append(int(_wait_file(ready)))
+ assert os.getpgid(children[0]) != worker.pgid
+ launcher._terminate_all([worker])
+ assert worker.process.returncode is not None
+ assert not _alive(children[0])
+ finally:
+ _cleanup(worker, children)
+
+
+def test_cleans_group_even_when_its_leader_already_exited(tmp_path, monkeypatch):
+ monkeypatch.setattr(launcher, "SHUTDOWN_GRACE_SECONDS", 0.2)
+ ready = tmp_path / "child"
+ child_code = f"""
+import os, signal, time
+from pathlib import Path
+signal.signal(signal.SIGTERM, signal.SIG_IGN)
+Path({str(ready)!r}).write_text(str(os.getpid()))
+while True: time.sleep(1)
+"""
+ worker = _spawn(f"""
+import subprocess, sys
+subprocess.Popen([sys.executable, '-c', {child_code!r}])
+""")
+ children = []
+ try:
+ children.append(int(_wait_file(ready)))
+ worker.process.wait(timeout=3)
+ launcher._terminate_all([worker])
+ deadline = time.monotonic() + 1
+ while _alive(children[0]) and time.monotonic() < deadline:
+ time.sleep(0.01)
+ assert not _alive(children[0])
+ finally:
+ _cleanup(worker, children)
+
+
+def test_second_interrupt_skips_remaining_grace_period(tmp_path):
+ ready = tmp_path / "ready"
+ worker = _spawn(f"""
+import signal, time
+from pathlib import Path
+signal.signal(signal.SIGTERM, signal.SIG_IGN)
+Path({str(ready)!r}).write_text('ready')
+while True: time.sleep(1)
+""")
+ force = threading.Event()
+ timer = threading.Timer(0.1, force.set)
+ try:
+ _wait_file(ready)
+ started = time.monotonic()
+ timer.start()
+ launcher._terminate_all([worker], force=force)
+ assert time.monotonic() - started < 1.5
+ assert worker.process.returncode == -signal.SIGKILL
+ finally:
+ timer.cancel()
+ timer.join()
+ _cleanup(worker)
diff --git a/tests/services/session/test_mastery_group_runtime.py b/tests/services/session/test_mastery_group_runtime.py
index 7de30b7f9..96ccd302d 100644
--- a/tests/services/session/test_mastery_group_runtime.py
+++ b/tests/services/session/test_mastery_group_runtime.py
@@ -117,12 +117,11 @@ async def test_commit_keeps_group_map_in_the_native_answer_record(monkeypatch):
"Válaszolj erre a két gyakorló kérdésre.\n1. Melyik változás növeli a sebességet?\n2. Miért növeli?",
],
)
-def test_explicit_numbered_assessment_cannot_finish_as_unregistered_prose(reply):
+def test_numbered_question_discussion_can_finish_without_tools(reply):
instruction = MasteryLoopCapability().finish_instruction(
UnifiedContext(metadata={"mastery_mode": True}), reply
)
- assert instruction is not None
- assert "mastery_quiz" in instruction
+ assert instruction is None
@pytest.mark.parametrize(
diff --git a/tests/services/session/test_turn_runtime.py b/tests/services/session/test_turn_runtime.py
index 29c7b4d6f..995908f84 100644
--- a/tests/services/session/test_turn_runtime.py
+++ b/tests/services/session/test_turn_runtime.py
@@ -20,7 +20,6 @@ from deeptutor.services.session.turn_runtime import (
_format_selection_tutor_context,
_repair_chinese_emphasis_for_persistence,
_resolve_selection_tutor_context,
- _retracted_round_call_id,
_should_capture_assistant_content,
_stamp_content_offset,
)
@@ -71,63 +70,6 @@ class TestShouldCaptureAssistantContent:
assert _should_capture_assistant_content(event) is False
-class TestRetractedRoundCallId:
- @staticmethod
- def _marker(**metadata: object) -> StreamEvent:
- return StreamEvent(
- type=StreamEventType.PROGRESS,
- metadata={
- "trace_kind": "call_status",
- "call_state": "complete",
- **metadata,
- },
- )
-
- def test_retracted_round_returns_call_id(self) -> None:
- event = self._marker(
- call_id="round-rejected",
- call_role="narration",
- answer_visible=False,
- )
- assert _retracted_round_call_id(event) == "round-rejected"
-
- def test_commentary_before_a_tool_call_stays_in_the_answer(self) -> None:
- """The paradigm: a round that called tools still wrote answer text.
-
- The reader watched that sentence arrive above the tool's own row, so
- reloading the turn must show it in the same place. Only an explicit
- retraction takes text back out of the answer.
- """
- event = self._marker(
- call_id="search-round",
- call_role="narration",
- answer_visible=True,
- )
- assert _retracted_round_call_id(event) is None
-
- def test_finish_marker_is_not_retracted(self) -> None:
- event = self._marker(
- call_id="round-2",
- call_role="finish",
- answer_visible=True,
- )
- assert _retracted_round_call_id(event) is None
-
- def test_running_status_is_not_retracted(self) -> None:
- event = self._marker(
- call_id="round-1",
- call_state="running",
- call_role="narration",
- answer_visible=False,
- )
- assert _retracted_round_call_id(event) is None
-
- def test_legacy_marker_without_the_flag_is_not_retracted(self) -> None:
- """Sessions recorded before the flag existed keep their text."""
- event = self._marker(call_id="round-old", call_role="narration")
- assert _retracted_round_call_id(event) is None
-
-
class TestAssemblePersistedAnswer:
def test_repairs_chinese_emphasis_boundaries_before_persistence(self) -> None:
assert (
@@ -162,7 +104,7 @@ class TestAssemblePersistedAnswer:
("round-part-2", "Part two."),
]
- assert _assemble_persisted_answer(segments, set()) == "Part one. Part two."
+ assert _assemble_persisted_answer(segments) == "Part one. Part two."
def test_commentary_rounds_are_part_of_the_persisted_answer(self) -> None:
segments = [
@@ -170,15 +112,15 @@ class TestAssemblePersistedAnswer:
("finish-round", "Answer."),
]
- assert _assemble_persisted_answer(segments, set()) == "Checking the syllabus first. Answer."
+ assert _assemble_persisted_answer(segments) == "Checking the syllabus first. Answer."
- def test_a_retracted_round_is_excluded(self) -> None:
+ def test_all_published_rounds_are_persisted(self) -> None:
segments = [
("rejected-round", "A question I never actually posed."),
("finish-round", "Answer."),
]
- assert _assemble_persisted_answer(segments, {"rejected-round"}) == "Answer."
+ assert _assemble_persisted_answer(segments) == "A question I never actually posed.Answer."
def test_ask_user_resolution_records_the_current_answer_boundary() -> None:
diff --git a/web/components/chat/home/AskUserOptions.tsx b/web/components/chat/home/AskUserOptions.tsx
index 01d5d0950..414831c3a 100644
--- a/web/components/chat/home/AskUserOptions.tsx
+++ b/web/components/chat/home/AskUserOptions.tsx
@@ -12,10 +12,7 @@ import {
readPosedQuestion,
type MasteryQuestion,
} from "@/lib/mastery-question";
-import {
- collectRetractedCallIds,
- shouldAppendEventContent,
-} from "@/lib/stream";
+import { shouldAppendEventContent } from "@/lib/stream";
import { hasRenderableCallTrace } from "@/features/chat/trace/selectors";
import type { StreamEvent } from "@/features/chat/model/protocol";
@@ -366,9 +363,6 @@ export function extractMessageSegments(
let pendingTextIdx: number | null = null;
let pendingTraceIdx: number | null = null;
let seq = 0;
- // A round a capability rejected was streamed and then taken back: its text
- // is trace material, never answer text. Ordinary commentary stays.
- const retractedCallIds = collectRetractedCallIds(events);
// Calls the reader sees as a card. Their *payloads* are not trace material
// — the card is what those say. The call itself is: stopping to ask is a
// step the agent took, and a trace with no row for it claims the answers
@@ -441,10 +435,6 @@ export function extractMessageSegments(
for (const event of events) {
if (shouldAppendEventContent(event)) {
const callId = ((event.metadata ?? {}) as { call_id?: string }).call_id;
- if (callId && retractedCallIds.has(callId)) {
- appendTraceEvent(event);
- continue;
- }
// Keep the transition to answer text with the preceding reasoning
// trace so it can fold before the whole model call finishes. Only the
// first delta is needed; subsequent answer deltas stay in the text.
diff --git a/web/components/partners/PartnerChat.tsx b/web/components/partners/PartnerChat.tsx
index 36f9814dd..2132143a0 100644
--- a/web/components/partners/PartnerChat.tsx
+++ b/web/components/partners/PartnerChat.tsx
@@ -39,11 +39,7 @@ import {
mergeGeneratedFiles,
unlinkedGeneratedFiles,
} from "@/components/common/InlineFileCard";
-import {
- isRetractionMarker,
- recomputeAnswerContent,
- shouldAppendEventContent,
-} from "@/lib/stream";
+import { shouldAppendEventContent } from "@/lib/stream";
import { useChatAutoScroll } from "@/hooks/useChatAutoScroll";
import { AssistantActivity } from "@/features/chat/trace";
import {
@@ -659,8 +655,6 @@ export default function PartnerChat({
current.events.push(data.event);
if (shouldAppendEventContent(data.event)) {
current.content += data.event.content;
- } else if (isRetractionMarker(data.event)) {
- current.content = recomputeAnswerContent(current.events);
}
externalLive.set(activityId, current);
publishExternal();
@@ -790,10 +784,6 @@ export default function PartnerChat({
live.events.push(event);
if (shouldAppendEventContent(event)) {
live.content += event.content;
- } else if (isRetractionMarker(event)) {
- // A capability took this round's text back out of the answer.
- // Same retraction rule as product chat.
- live.content = recomputeAnswerContent(live.events);
}
publish();
} else if (data.type === "content") {
diff --git a/web/components/partners/group/useGroupSession.ts b/web/components/partners/group/useGroupSession.ts
index 03d00155c..3974ed559 100644
--- a/web/components/partners/group/useGroupSession.ts
+++ b/web/components/partners/group/useGroupSession.ts
@@ -23,11 +23,7 @@ import {
type PartnerInvocation,
} from "@/lib/partner-groups-api";
import { ReconnectingWebSocket } from "@/lib/reconnecting-websocket";
-import {
- isRetractionMarker,
- recomputeAnswerContent,
- shouldAppendEventContent,
-} from "@/lib/stream";
+import { shouldAppendEventContent } from "@/lib/stream";
import type { StreamEvent } from "@/features/chat/model/protocol";
export type SeatStatus = "waiting" | "working" | "done" | "error";
@@ -92,19 +88,8 @@ type GroupFrame =
| { type: "cancelled"; content?: string }
| { type: "error"; content: string };
-/**
- * Grow a seat's visible body from one trace event.
- *
- * Every round's text is body text, commentary written before a tool call
- * included. A capability can retract a round it rejected, and that is only
- * known when the round's marker lands — so the body is appended optimistically
- * and recomputed then. This is the same rule the product chat uses
- * (``lib/stream``), so a Partner's body reads identically in both surfaces.
- */
+/** Grow the public body without withdrawing already published content. */
function growBody(seat: Seat, event: StreamEvent): string {
- if (isRetractionMarker(event)) {
- return recomputeAnswerContent([...seat.events, event]);
- }
return shouldAppendEventContent(event)
? seat.streamed + (event.content || "")
: seat.streamed;
diff --git a/web/features/chat/ChatStateAdapter.tsx b/web/features/chat/ChatStateAdapter.tsx
index a0a68a979..ce826780c 100644
--- a/web/features/chat/ChatStateAdapter.tsx
+++ b/web/features/chat/ChatStateAdapter.tsx
@@ -80,11 +80,7 @@ import {
} from "@/lib/send-retry";
import { reconcileTurnIds } from "@/lib/turn-reconcile";
import { decideFailedTurnReplay, isFailedTurnVisible } from "@/lib/chat-resend";
-import {
- isRetractionMarker,
- recomputeAnswerContent,
- shouldAppendEventContent,
-} from "@/lib/stream";
+import { shouldAppendEventContent } from "@/lib/stream";
import { hasPendingAskUserInMessages, pendingAskUserKeyInMessages } from "@/lib/ask-user-state";
import { notify } from "@/lib/notifications";
import { forwardReaderAction } from "@/lib/reading-reader-action";
@@ -455,7 +451,7 @@ type Action =
| { type: "SET_SUBMISSION_PERSISTENCE"; key: string; submissionId: string; saved: boolean }
| { type: "POP_LAST_ASSISTANT"; key: string }
| { type: "RESTORE_ASSISTANT"; key: string; message: MessageItem }
- | { type: "STREAM_START"; key: string; startedAt: number }
+ | { type: "STREAM_START"; key: string; startedAt: number; capability?: string | null }
| { type: "QUEUE_MESSAGE"; key: string; message: QueuedMessage }
| { type: "OFFER_QUEUED"; key: string; ids: string[]; turnId: string }
| { type: "DROP_QUEUED"; key: string; ids: string[] }
@@ -1065,7 +1061,9 @@ function reduce(state: ProviderState, action: Action): ProviderState {
events: [],
// Include connection and provider latency before the first frame (#1435).
trace: { started_at: action.startedAt },
- capability: session.activeCapability || "",
+ capability: action.capability !== undefined
+ ? action.capability || ""
+ : session.activeCapability || "",
parentMessageId: tip?.id ?? null,
},
],
@@ -1114,13 +1112,7 @@ function reduce(state: ProviderState, action: Action): ProviderState {
const language = session.language;
let rawContent = last?.rawContent ?? last?.content ?? "";
let content = last?.content ?? "";
- if (isRetractionMarker(action.event)) {
- // A capability rejected this round after it had already streamed:
- // drop its text from the answer — it stays in the trace. Recompute
- // from immutable event content, never from display text.
- rawContent = recomputeAnswerContent(events);
- content = repairChineseEmphasis(rawContent, language);
- } else if (shouldAppendEventContent(action.event)) {
+ if (shouldAppendEventContent(action.event)) {
const delta = action.event.content;
rawContent += delta;
content = appendWithEmphasisRepair(content, delta, rawContent, language);
@@ -3236,7 +3228,10 @@ export function ChatStateAdapterProvider({
failedSubmissionId: submissionId,
});
}
- dispatch({ type: "STREAM_START", key, startedAt: Date.now() / 1000 });
+ dispatch({
+ type: "STREAM_START", key, startedAt: Date.now() / 1000,
+ capability: effectiveCapability,
+ });
};
if (!replacesSubmissionId) showSubmission();
const {
@@ -3534,7 +3529,10 @@ export function ChatStateAdapterProvider({
pendingRegenerateRef.current.delete(key);
}
dispatch({ type: "POP_LAST_ASSISTANT", key });
- dispatch({ type: "STREAM_START", key, startedAt: Date.now() / 1000 });
+ dispatch({
+ type: "STREAM_START", key, startedAt: Date.now() / 1000,
+ capability: lastUser.requestSnapshot?.capability ?? lastUser.capability ?? null,
+ });
sendThroughRunner(key, {
type: "regenerate",
session_id: session.sessionId,
diff --git a/web/features/chat/messages/ChatMessageList.tsx b/web/features/chat/messages/ChatMessageList.tsx
index c7242c72d..1a1bdb5d4 100644
--- a/web/features/chat/messages/ChatMessageList.tsx
+++ b/web/features/chat/messages/ChatMessageList.tsx
@@ -452,7 +452,6 @@ function buildMessageBlocks(
});
// Keep Mastery teaching/feedback visible before an assessment arrives and
// when a turn ends with bookkeeping without posing an assessment at all.
- // Explicitly retracted prose is already a trace, not a text segment.
if (isMastery) preserveTeachingBefore(segments.length);
const blocks: MessageBlock[] = [];
let run: MessageSegment[] = [];
@@ -831,9 +830,18 @@ export const AssistantMessage = memo(function AssistantMessage({
}, [msg.capability, resultEvent]);
const videoLessonResult = useMemo(() => {
- if (msg.capability !== "video_lesson" || !resultEvent) return null;
- return extractVideoLessonResult(resultEvent.metadata);
- }, [msg.capability, resultEvent]);
+ // A one-turn capability or a resumed turn may still have the composer's
+ // chat label. The capability that emitted the result is authoritative.
+ // Ignore earlier intermediate results that do not carry a playable page.
+ for (let index = events.length - 1; index >= 0; index -= 1) {
+ const event = events[index];
+ if (event.type !== "result" ||
+ (event.source !== "video_lesson" && msg.capability !== "video_lesson")) continue;
+ const lesson = extractVideoLessonResult(event.metadata);
+ if (lesson) return lesson;
+ }
+ return null;
+ }, [msg.capability, events]);
// Detect the ``ask_user`` terminator payload: when the assistant turn
// ended via the ``ask_user`` tool, this is the question the user is
@@ -936,6 +944,7 @@ export const AssistantMessage = memo(function AssistantMessage({
!outlinePreview &&
!mathAnimatorResult &&
!visualizeResult &&
+ !videoLessonResult &&
!(quizQuestions && quizQuestions.length > 0);
const messageSegments = useMemo(
() =>
diff --git a/web/features/chat/trace/TracePresentation.tsx b/web/features/chat/trace/TracePresentation.tsx
index 2dcb8b8c5..274d9114f 100644
--- a/web/features/chat/trace/TracePresentation.tsx
+++ b/web/features/chat/trace/TracePresentation.tsx
@@ -46,7 +46,6 @@ import {
hasRenderableCallTrace as selectHasRenderableCallTrace,
hasSettledFinalRound,
isChatLoopAnswerContent,
- isRetractedRound,
isTracePending,
selectTraceDisplayItems,
} from "./selectors";
@@ -449,25 +448,16 @@ function getTraceHeader(
return title;
}
-// Chat-loop `content` (call_kind "agent_loop_round") is the model's
-// user-facing text, and it stays in the answer bubble — commentary written
-// before a tool call included. So it is kept OUT of the trace by default, to
-// avoid showing the same sentence twice. The one exception is a round a
-// capability retracted: that text is no longer in the bubble, so the trace is
-// the only place left for it.
+// Chat-loop prose belongs to the answer and must not be duplicated in the trace.
function getTraceText(
events: StreamEvent[],
eventTypes: Array<StreamEvent["type"]>,
- // When the caller knows this group's text was retracted, its
- // ``agent_loop_round`` content is trace material and should NOT be
- // filtered out as answer-bubble text.
- includeChatLoopContent = false,
) {
const textEvents = events.filter(
(event) =>
eventTypes.includes(event.type) &&
event.content.trim().length > 0 &&
- (includeChatLoopContent || !isChatLoopAnswerContent(event)),
+ !isChatLoopAnswerContent(event),
);
if (!textEvents.length) return "";
@@ -783,16 +773,9 @@ function TraceRowBody({
);
const thoughtText = getTraceText(callEvents, ["thinking"]);
const observationText = getTraceText(callEvents, ["observation"]);
- // A chat round can emit BOTH reasoning (thinking) and retracted commentary
- // (content) in a single call; both are trace material and render as
- // separate stacked blocks. Other pipelines keep the legacy "thought or
- // content" fallback so their rows are unchanged.
+ // Chat-loop reasoning belongs in the trace; its public prose stays in the answer.
const isChatRound = kind === "agent_loop_round";
- const contentText = getTraceText(
- callEvents,
- ["content"],
- isRetractedRound(callEvents),
- );
+ const contentText = getTraceText(callEvents, ["content"]);
const bodyBlocks =
role === "observe"
? [observationText]
@@ -953,11 +936,7 @@ function hasExpandableContent(
);
const thoughtText = getTraceText(callEvents, ["thinking"]);
const observationText = getTraceText(callEvents, ["observation"]);
- const contentText = getTraceText(
- callEvents,
- ["content"],
- isRetractedRound(callEvents),
- );
+ const contentText = getTraceText(callEvents, ["content"]);
const genericBodyText =
role === "observe"
? observationText
@@ -1023,7 +1002,6 @@ function TraceRowItem({
const isToolRow = kind === "tool_planning" || group === "tool_call";
const isChatRound = kind === "agent_loop_round";
const isRetrieve = role === "retrieve";
- const retracted = isRetractedRound(callEvents);
// The model's own text-form deliberation — chat-loop reasoning/narration and
// pipeline "Thought"/"Plan" rounds. Unlike a tool call (whose result is
// secondary detail worth folding away), here the text IS the substance, so
@@ -1043,7 +1021,6 @@ function TraceRowItem({
const isContextExploration = kind === "context_exploration";
const answering =
isChatRound &&
- !retracted &&
callEvents.some(isChatLoopAnswerContent);
const autoOpen =
isThinking && !isContextExploration && !answering ? active : false;
@@ -1065,7 +1042,7 @@ function TraceRowItem({
| undefined;
const thoughtText = getTraceText(callEvents, ["thinking"]).trim();
- const contentText = getTraceText(callEvents, ["content"], retracted).trim();
+ const contentText = getTraceText(callEvents, ["content"]).trim();
// Resolve every row into a uniform { icon, headline, chip } triple so the
// activity feed reads consistently across pipelines. Tool calls get a human
diff --git a/web/features/chat/trace/index.ts b/web/features/chat/trace/index.ts
index 49c7f3638..273852263 100644
--- a/web/features/chat/trace/index.ts
+++ b/web/features/chat/trace/index.ts
@@ -16,7 +16,6 @@ export {
getTraceRole,
groupTraceEvents,
hasRenderableCallTrace,
- isRetractedRound,
isTracePending,
selectTraceDisplayItems,
} from "./selectors";
diff --git a/web/features/chat/trace/memory.ts b/web/features/chat/trace/memory.ts
index 593543379..f64b9f5e8 100644
--- a/web/features/chat/trace/memory.ts
+++ b/web/features/chat/trace/memory.ts
@@ -1,8 +1,5 @@
import type { StreamEvent } from "@/features/chat/model/protocol";
-import {
- collectRetractedCallIds,
- shouldAppendEventContent,
-} from "@/lib/stream";
+import { shouldAppendEventContent } from "@/lib/stream";
export const MAX_PREVIEW_EVENTS = 200;
export const MAX_PREVIEW_BYTES = 128 * 1024;
@@ -178,16 +175,13 @@ export function settleMessageTrace(
turnId: string | null,
bounds?: MessageTraceMetadata,
): { events: StreamEvent[]; trace: MessageTraceMetadata } {
- const retracted = collectRetractedCallIds(events);
let answerLength = 0;
let lastSeq = 0;
const stamped = events.map((event) => {
lastSeq = Math.max(lastSeq, event.seq ?? 0);
const meta = metadata(event);
if (shouldAppendEventContent(event)) {
- const callId = typeof meta.call_id === "string" ? meta.call_id : "";
- if (!callId || !retracted.has(callId))
- answerLength += event.content.length;
+ answerLength += event.content.length;
return event;
}
if (
diff --git a/web/features/chat/trace/selectors.ts b/web/features/chat/trace/selectors.ts
index d01a7cf0e..91450668c 100644
--- a/web/features/chat/trace/selectors.ts
+++ b/web/features/chat/trace/selectors.ts
@@ -118,24 +118,7 @@ export function isChatLoopAnswerContent(event: StreamEvent): boolean {
);
}
-/**
- * Whether this group's own text was taken back out of the answer. Only then
- * is a chat-loop round's prose trace material — ordinary commentary stays in
- * the bubble where the reader watched it arrive.
- */
-export function isRetractedRound(events: StreamEvent[]): boolean {
- return events.some((event) => {
- const meta = getTraceMeta(event);
- return (
- meta.trace_kind === "call_status" &&
- meta.call_state === "complete" &&
- meta.answer_visible === false
- );
- });
-}
-
export function groupHasTraceSubstance(events: StreamEvent[]): boolean {
- const retracted = isRetractedRound(events);
return events.some((event) => {
if (
event.type === "tool_call" ||
@@ -155,7 +138,7 @@ export function groupHasTraceSubstance(events: StreamEvent[]): boolean {
}
if (event.type === "content") {
return (
- (retracted || !isChatLoopAnswerContent(event)) &&
+ !isChatLoopAnswerContent(event) &&
Boolean(event.content.trim())
);
}
@@ -175,9 +158,7 @@ export function groupHasTraceSubstance(events: StreamEvent[]): boolean {
* token-truncated round is explicitly non-terminal, so once its own next round
* completes, that marker supersedes this one.
*
- * ``answer_visible`` is deliberately not consulted: it says whether a round's
- * text belongs to the answer, which is true of nearly every round and says
- * nothing about whether the turn is over.
+ * Legacy visibility metadata does not determine completion or suppress text.
*/
export function hasSettledFinalRound(events: StreamEvent[]): boolean {
for (let idx = events.length - 1; idx >= 0; idx -= 1) {
diff --git a/web/lib/stream.ts b/web/lib/stream.ts
index 9bb9a292d..b1a569549 100644
--- a/web/lib/stream.ts
+++ b/web/lib/stream.ts
@@ -1,80 +1,32 @@
-import type { StreamEvent } from "@/features/chat/model/protocol";
+import type { StreamEvent } from '@/features/chat/model/protocol'
type ContentMeta = {
- call_id?: string;
- call_kind?: string;
- call_state?: string;
- call_role?: string;
- answer_visible?: boolean;
- trace_kind?: string;
-};
+ call_id?: string
+ call_kind?: string
+ call_state?: string
+ call_role?: string
+ answer_visible?: boolean
+ trace_kind?: string
+}
function eventMeta(event: StreamEvent): ContentMeta {
- return (event.metadata ?? {}) as ContentMeta;
+ return (event.metadata ?? {}) as ContentMeta
}
export function shouldAppendEventContent(event: StreamEvent): boolean {
- if (event.type !== "content") return false;
- const meta = eventMeta(event);
- if (!meta.call_id) return true;
+ if (event.type !== 'content') return false
+ const meta = eventMeta(event)
+ if (!meta.call_id) return true
// The chat agent loop streams every round's text as `content`, and all of
// it is the answer — the commentary a round wrote before calling a tool
- // included. Only a round the backend retracts is filtered back out (see
- // collectRetractedCallIds).
- return (
- meta.call_kind === "llm_final_response" ||
- meta.call_kind === "agent_loop_round"
- );
-}
-
-/**
- * call_ids whose round was taken back out of the answer.
- *
- * A round that called tools still wrote answer text: the reader watched that
- * sentence arrive above the tool's own row, so it stays where they saw it.
- * The one exception is a round a capability's finish guard rejected — the
- * backend republishes that round's marker with `answer_visible: false`, and
- * the text moves into the trace.
- */
-export function collectRetractedCallIds(events: StreamEvent[]): Set<string> {
- const ids = new Set<string>();
- for (const event of events) {
- const meta = eventMeta(event);
- if (
- meta.trace_kind === "call_status" &&
- meta.call_state === "complete" &&
- meta.answer_visible === false &&
- meta.call_id
- ) {
- ids.add(meta.call_id);
- }
- }
- return ids;
-}
-
-/** True for a per-round marker that takes its content back out of the answer. */
-export function isRetractionMarker(event: StreamEvent): boolean {
- const meta = eventMeta(event);
- return (
- meta.trace_kind === "call_status" &&
- meta.call_state === "complete" &&
- meta.answer_visible === false
- );
+ // included. Completion markers cannot retract published prose.
+ return meta.call_kind === 'llm_final_response' || meta.call_kind === 'agent_loop_round'
}
-/**
- * Recompute the answer text from a turn's events: appended content minus any
- * retracted round. Cheap to call only when a retraction arrives (rare — most
- * turns never have one) rather than per streamed chunk.
- */
+/** Replay every published answer chunk in order. */
export function recomputeAnswerContent(events: StreamEvent[]): string {
- const retracted = collectRetractedCallIds(events);
- let content = "";
- for (const event of events) {
- if (!shouldAppendEventContent(event)) continue;
- const callId = eventMeta(event).call_id;
- if (callId && retracted.has(callId)) continue;
- content += event.content;
- }
- return content;
+ return events
+ .filter(shouldAppendEventContent)
+ .map(event => event.content)
+ .join('')
}
diff --git a/web/lib/video-lesson-types.ts b/web/lib/video-lesson-types.ts
index 03a4cf2de..bfea19a02 100644
--- a/web/lib/video-lesson-types.ts
+++ b/web/lib/video-lesson-types.ts
@@ -28,7 +28,8 @@ export function extractVideoLessonResult(
Boolean(item) &&
typeof item === "object" &&
(item as Record<string, unknown>).type === "html" &&
- typeof (item as Record<string, unknown>).url === "string",
+ typeof (item as Record<string, unknown>).url === "string" &&
+ Boolean(((item as Record<string, unknown>).url as string).trim()),
);
if (!page) return null;
const code =
diff --git a/web/tests/mastery-teaching-visibility.spec.tsx b/web/tests/mastery-teaching-visibility.spec.tsx
index 107352a55..bdd228756 100644
--- a/web/tests/mastery-teaching-visibility.spec.tsx
+++ b/web/tests/mastery-teaching-visibility.spec.tsx
@@ -114,16 +114,15 @@ describe("Mastery teaching visibility", () => {
expect(await screen.findByText(question.prompt)).toBeVisible();
});
- it("does not restore a rejected answer as visible teaching", async () => {
- const rejected = "A rejected explanation that must not become lesson text.";
+ it("preserves an explanation despite a legacy rejection marker", async () => {
+ const rejected = "An explanation addressing the learner’s objection.";
const events = [
event("content", rejected, { call_id: "rejected-round", call_kind: "agent_loop_round" }),
event("progress", "", { trace_kind: "call_status", call_state: "complete", answer_visible: false, call_id: "rejected-round" }),
...lessonEvents(false),
];
- render(<AssistantMessage msg={{ content: lesson + transition, capability: "mastery_path", events }} />);
- const rejectedText = screen.queryByText(rejected);
- if (rejectedText) expect(rejectedText).not.toBeVisible();
+ render(<AssistantMessage msg={{ content: rejected + lesson + transition, capability: "mastery_path", events }} />);
+ expect(screen.getByText((text) => text.includes(rejected))).toBeVisible();
expect(screen.getByText(lesson)).toBeVisible();
expect(await screen.findByText(question.prompt)).toBeVisible();
});
diff --git a/web/tests/stream.test.ts b/web/tests/stream.test.ts
index 194e4ded5..091adeb25 100644
--- a/web/tests/stream.test.ts
+++ b/web/tests/stream.test.ts
@@ -1,177 +1,163 @@
-import test from "node:test";
-import assert from "node:assert/strict";
-import {
- collectRetractedCallIds,
- isRetractionMarker,
- recomputeAnswerContent,
-} from "../lib/stream";
-import type { StreamEvent } from "../features/chat/model/protocol";
+import test from 'node:test'
+import assert from 'node:assert/strict'
+import { recomputeAnswerContent } from '../lib/stream'
+import type { StreamEvent } from '../features/chat/model/protocol'
function event(
- type: StreamEvent["type"],
+ type: StreamEvent['type'],
content: string,
- metadata: Record<string, unknown>,
+ metadata: Record<string, unknown>
): StreamEvent {
return {
type,
- source: "chat",
- stage: "responding",
+ source: 'chat',
+ stage: 'responding',
content,
metadata,
- session_id: "session-1",
- turn_id: "turn-1",
+ session_id: 'session-1',
+ turn_id: 'turn-1',
seq: 1,
timestamp: 0,
- };
+ }
}
-test("commentary written before a tool call stays in the answer", () => {
+test('commentary written before a tool call stays in the answer', () => {
// The reader watched this sentence arrive above the tool's own row. Taking
// it back out on the round's completion marker is what used to make a turn
// look like it had said nothing while it worked.
const events = [
- event("content", "Checking the syllabus first.", {
- call_id: "round-1",
- call_kind: "agent_loop_round",
- }),
- event("progress", "", {
- call_id: "round-1",
- trace_kind: "call_status",
- call_state: "complete",
- call_role: "narration",
+ event('content', 'Checking the syllabus first.', {
+ call_id: 'round-1',
+ call_kind: 'agent_loop_round',
+ }),
+ event('progress', '', {
+ call_id: 'round-1',
+ trace_kind: 'call_status',
+ call_state: 'complete',
+ call_role: 'narration',
answer_visible: true,
}),
- ];
+ ]
- assert.deepEqual([...collectRetractedCallIds(events)], []);
- assert.equal(isRetractionMarker(events[1]), false);
- assert.equal(recomputeAnswerContent(events), "Checking the syllabus first.");
-});
+ assert.equal(recomputeAnswerContent(events), 'Checking the syllabus first.')
+})
-test("a round a capability retracted leaves the answer", () => {
+test('legacy rejection markers cannot remove published explanations', () => {
const events = [
- event("content", "Great job! Choose the next topic.", {
- call_id: "round-rejected",
- call_kind: "agent_loop_round",
- }),
- event("progress", "", {
- call_id: "round-rejected",
- trace_kind: "call_status",
- call_state: "complete",
- call_role: "narration",
+ event('content', 'Great job! Choose the next topic.', {
+ call_id: 'round-rejected',
+ call_kind: 'agent_loop_round',
+ }),
+ event('progress', '', {
+ call_id: 'round-rejected',
+ trace_kind: 'call_status',
+ call_state: 'complete',
+ call_role: 'narration',
answer_visible: false,
}),
- ];
+ ]
- assert.deepEqual([...collectRetractedCallIds(events)], ["round-rejected"]);
- assert.equal(isRetractionMarker(events[1]), true);
- assert.equal(recomputeAnswerContent(events), "");
-});
+ assert.equal(recomputeAnswerContent(events), 'Great job! Choose the next topic.')
+})
-test("a round recorded before the flag existed keeps its text", () => {
+test('a round recorded before the flag existed keeps its text', () => {
// Sessions written by an older build carry no `answer_visible` at all.
const events = [
- event("content", "Searching.", {
- call_id: "round-legacy",
- call_kind: "agent_loop_round",
+ event('content', 'Searching.', {
+ call_id: 'round-legacy',
+ call_kind: 'agent_loop_round',
}),
- event("progress", "", {
- call_id: "round-legacy",
- trace_kind: "call_status",
- call_state: "complete",
- call_role: "narration",
+ event('progress', '', {
+ call_id: 'round-legacy',
+ trace_kind: 'call_status',
+ call_state: 'complete',
+ call_role: 'narration',
}),
- ];
+ ]
- assert.equal(collectRetractedCallIds(events).size, 0);
- assert.equal(recomputeAnswerContent(events), "Searching.");
-});
+ assert.equal(recomputeAnswerContent(events), 'Searching.')
+})
-test("token-limit continuation replays the exact visible answer", () => {
+test('token-limit continuation replays the exact visible answer', () => {
const events = [
- event("content", "Part one. ", {
- call_id: "round-part-1",
- call_kind: "agent_loop_round",
- }),
- event("progress", "", {
- call_id: "round-part-1",
- trace_kind: "call_status",
- call_state: "complete",
- call_role: "narration",
+ event('content', 'Part one. ', {
+ call_id: 'round-part-1',
+ call_kind: 'agent_loop_round',
+ }),
+ event('progress', '', {
+ call_id: 'round-part-1',
+ trace_kind: 'call_status',
+ call_state: 'complete',
+ call_role: 'narration',
answer_visible: true,
}),
- event("content", "Part two.", {
- call_id: "round-part-2",
- call_kind: "agent_loop_round",
+ event('content', 'Part two.', {
+ call_id: 'round-part-2',
+ call_kind: 'agent_loop_round',
}),
- event("progress", "", {
- call_id: "round-part-2",
- trace_kind: "call_status",
- call_state: "complete",
- call_role: "finish",
+ event('progress', '', {
+ call_id: 'round-part-2',
+ trace_kind: 'call_status',
+ call_state: 'complete',
+ call_role: 'finish',
}),
- ];
+ ]
- assert.equal(recomputeAnswerContent(events), "Part one. Part two.");
-});
+ assert.equal(recomputeAnswerContent(events), 'Part one. Part two.')
+})
-test("a finish that is rejected after streaming is withdrawn from the answer", () => {
- // A guarded surface (mastery, partner authoring) streams optimistically and
- // closes the round as `finish`. When its capability then rejects that finish,
- // the loop republishes the same call_id with `answer_visible: false`, and the
- // already-visible text has to come back out of the reply.
+test('a later finish check cannot withdraw an already streamed answer', () => {
+ // Old persisted rejection markers no longer suppress public text.
const events = [
- event("content", "Which value is correct?\n\nA. one\nB. two\nC. three", {
- call_id: "round-1",
- call_kind: "agent_loop_round",
- }),
- event("progress", "", {
- call_id: "round-1",
- trace_kind: "call_status",
- call_state: "complete",
- call_role: "finish",
- }),
- event("progress", "", {
- call_id: "round-1",
- trace_kind: "call_status",
- call_state: "complete",
- call_role: "narration",
+ event('content', 'Which value is correct?\n\nA. one\nB. two\nC. three', {
+ call_id: 'round-1',
+ call_kind: 'agent_loop_round',
+ }),
+ event('progress', '', {
+ call_id: 'round-1',
+ trace_kind: 'call_status',
+ call_state: 'complete',
+ call_role: 'finish',
+ }),
+ event('progress', '', {
+ call_id: 'round-1',
+ trace_kind: 'call_status',
+ call_state: 'complete',
+ call_role: 'narration',
answer_visible: false,
finish_rejected: true,
}),
- event("content", "Let us try that on a card instead.", {
- call_id: "round-2",
- call_kind: "agent_loop_round",
+ event('content', 'Let us try that on a card instead.', {
+ call_id: 'round-2',
+ call_kind: 'agent_loop_round',
}),
- event("progress", "", {
- call_id: "round-2",
- trace_kind: "call_status",
- call_state: "complete",
- call_role: "finish",
+ event('progress', '', {
+ call_id: 'round-2',
+ trace_kind: 'call_status',
+ call_state: 'complete',
+ call_role: 'finish',
}),
- ];
+ ]
- assert.deepEqual([...collectRetractedCallIds(events)], ["round-1"]);
assert.equal(
recomputeAnswerContent(events),
- "Let us try that on a card instead.",
- );
-});
+ 'Which value is correct?\n\nA. one\nB. two\nC. threeLet us try that on a card instead.'
+ )
+})
-test("an accepted finish keeps its text even after later rounds close", () => {
+test('an accepted finish keeps its text even after later rounds close', () => {
const events = [
- event("content", "The derivative is 2x.", {
- call_id: "round-1",
- call_kind: "agent_loop_round",
+ event('content', 'The derivative is 2x.', {
+ call_id: 'round-1',
+ call_kind: 'agent_loop_round',
}),
- event("progress", "", {
- call_id: "round-1",
- trace_kind: "call_status",
- call_state: "complete",
- call_role: "finish",
+ event('progress', '', {
+ call_id: 'round-1',
+ trace_kind: 'call_status',
+ call_state: 'complete',
+ call_role: 'finish',
}),
- ];
+ ]
- assert.equal(collectRetractedCallIds(events).size, 0);
- assert.equal(recomputeAnswerContent(events), "The derivative is 2x.");
-});
+ assert.equal(recomputeAnswerContent(events), 'The derivative is 2x.')
+})
diff --git a/web/tests/trace-selectors.test.ts b/web/tests/trace-selectors.test.ts
index d9adc8c7a..fd13c7026 100644
--- a/web/tests/trace-selectors.test.ts
+++ b/web/tests/trace-selectors.test.ts
@@ -5,7 +5,6 @@ import {
detectStreamingMode,
groupTraceEvents,
hasRenderableCallTrace,
- isRetractedRound,
isTracePending,
selectTraceDisplayItems,
} from "../features/chat/trace/selectors";
@@ -63,7 +62,7 @@ test("final answer and absorbed groups stay out of progressive trace disclosure"
assert.equal(hasRenderableCallTrace(events), false);
});
-test("only a retracted round's text becomes trace material", () => {
+test("published answer text never becomes trace material", () => {
// Chat-loop text lives in the answer bubble, so showing it in the trace as
// well would print the same sentence twice. The exception is a round whose
// text was taken back out — the trace is then the only place left for it.
@@ -95,10 +94,8 @@ test("only a retracted round's text becomes trace material", () => {
answer_visible: false,
}),
];
- assert.equal(isRetractedRound(commentary), false);
assert.equal(hasRenderableCallTrace(commentary), false);
- assert.equal(isRetractedRound(retracted), true);
- assert.equal(hasRenderableCallTrace(retracted), true);
+ assert.equal(hasRenderableCallTrace(retracted), false);
});
test("adjacent react rounds and their trailing trace collapse into one display step", () => {
diff --git a/web/tests/video-lesson-message.spec.tsx b/web/tests/video-lesson-message.spec.tsx
new file mode 100644
index 000000000..2e5942529
--- /dev/null
+++ b/web/tests/video-lesson-message.spec.tsx
@@ -0,0 +1,92 @@
+import { act, fireEvent, render, screen } from "@testing-library/react";
+import { describe, expect, it, vi } from "vitest";
+import { ChatStateAdapterProvider, useChatStateAdapter } from "@/features/chat/ChatStateAdapter";
+import { AssistantMessage } from "@/features/chat/messages/ChatMessageList";
+import type { StreamEvent } from "@/features/chat/model/protocol";
+import { compactTracePreview } from "@/features/chat/trace/memory";
+import { initI18n } from "@/i18n/init";
+
+initI18n("en");
+const transport = vi.hoisted(() => ({ emit: undefined as undefined | ((e: StreamEvent) => void) }));
+vi.mock("@/features/chat/transport/UnifiedTurnClient", () => ({
+ UnifiedTurnClient: class {
+ connected = true;
+ constructor(emit: typeof transport.emit) { transport.emit = emit; }
+ connect() {}
+ disconnect() {}
+ send() {}
+ setResumeState() {}
+ },
+}));
+const summary = "Derivatives measure change; integrals measure accumulation.";
+const url = "/files/outputs/workspace/chat/video_lesson/turn-1/artifacts/lesson.html";
+function event(type: StreamEvent["type"], seq: number, metadata = {}, content = ""): StreamEvent {
+ return { type, seq, metadata, content, source: "video_lesson", stage: "render_output", timestamp: Date.now() / 1000,
+ session_id: "session-1", turn_id: "turn-1" };
+}
+const result = event("result", 3, {
+ title: "Calculus", response: summary,
+ artifacts: [{ type: "html", url, filename: "lesson.html" }],
+ code: { language: "tsx", content: "export const Lesson = () => null;" },
+ render: { duration_seconds: 90, attempts: [{ stage: "done", errors: [] }] },
+});
+const done = event("done", 4, { status: "completed", user_message_id: 1, assistant_message_id: 2 });
+function Harness() {
+ const { state, sendMessage, regenerateLastMessage } = useChatStateAdapter();
+ const msg = state.messages.at(-1);
+ return <>
+ <button onClick={() => sendMessage("Explain calculus", undefined, undefined, undefined, undefined,
+ { capability: "video_lesson" })}>Generate lesson</button>
+ <button onClick={() => regenerateLastMessage()}>Regenerate lesson</button>
+ <output data-testid="session-capability">{state.activeCapability ?? "chat"}</output>
+ <output data-testid="capability">{msg?.capability}</output>
+ {msg?.role === "assistant" && <AssistantMessage msg={msg} isStreaming={state.isStreaming} />}
+ </>;
+}
+
+describe("video lesson result delivery", () => {
+ it("shows the player after a one-turn capability finishes in a normal chat", async () => {
+ render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
+ fireEvent.click(screen.getByText("Generate lesson"));
+ await act(async () => {
+ transport.emit?.(event("session", 1, { session_id: "session-1" }));
+ transport.emit?.(event("content", 2, {}, summary));
+ transport.emit?.(result);
+ transport.emit?.(done);
+ });
+ expect(await screen.findByTitle("Calculus")).toHaveAttribute("src", expect.stringContaining(url));
+ expect(screen.getByTestId("capability")).toHaveTextContent("video_lesson");
+ expect(screen.getByTestId("session-capability")).toHaveTextContent("chat");
+ fireEvent.click(screen.getByText("Regenerate lesson"));
+ expect(screen.getByTestId("capability")).toHaveTextContent("video_lesson");
+ });
+
+ it("restores the player from the persisted result preview", async () => {
+ const events = compactTracePreview([event("content", 2, {}, summary), result, done]).events;
+ render(<AssistantMessage msg={{ capability: "video_lesson", content: summary, events }} isStreaming={false} />);
+ expect(await screen.findByTitle("Calculus")).toHaveAttribute("src", expect.stringContaining(url));
+ });
+
+ it("recognizes an authoritative video result when the message still has the chat capability", async () => {
+ render(<AssistantMessage msg={{ capability: "chat", content: summary, events: [result, done] }} isStreaming={false} />);
+ expect(await screen.findByTitle("Calculus")).toHaveAttribute("src", expect.stringContaining(url));
+ });
+ it("finds the playable result after an intermediate result without a page", async () => {
+ render(<AssistantMessage msg={{ capability: "video_lesson", content: summary,
+ events: [event("result", 1, { response: "Planning" }), result, done] }} isStreaming={false} />);
+ expect(await screen.findByTitle("Calculus")).toHaveAttribute("src", expect.stringContaining(url));
+ });
+
+ it("does not turn an ordinary HTML artifact into a video player", () => {
+ render(<AssistantMessage msg={{ capability: "chat", content: summary,
+ events: [{ ...result, source: "chat" }, done] }} isStreaming={false} />);
+ expect(screen.queryByTitle("Calculus")).toBeNull();
+ });
+
+ it("does not point an empty artifact URL back at the app", () => {
+ render(<AssistantMessage msg={{ capability: "video_lesson", content: summary,
+ events: [event("result", 3, { title: "Calculus", artifacts: [{ type: "html", url: " " }] }), done] }} isStreaming={false} />);
+ expect(screen.queryByTitle("Calculus")).toBeNull();
+ });
+
+});