Skip to content

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

fix(mastery): keep learner-facing teaching visible across tool work

yepyhunc8b9bef8737b
diff --git a/web/features/chat/messages/ChatMessageList.tsx b/web/features/chat/messages/ChatMessageList.tsx
index 88150b741..d945555b4 100644
--- a/web/features/chat/messages/ChatMessageList.tsx
+++ b/web/features/chat/messages/ChatMessageList.tsx
@@ -377,27 +377,36 @@ type MessageBlock =
* Split a message into its blocks.
*
* A mastery question ends its turn, so the prose introducing it is teaching,
- * not commentary awaiting a later answer. Keep that text outside the fold as
- * well as the card. Earlier exploration and any intervening tool rows still
- * fold normally. Answered clarifications belong to the same process as the
+ * not commentary awaiting a later answer. Keep learner-facing text since the
+ * previous card outside the fold even when tool work separates its parts.
+ * Reasoning and tool payloads are trace segments and still fold normally.
+ * Answered clarifications belong to the same process as the
* work before and after them; only pending cards stand outside the fold.
*/
function buildMessageBlocks(
segments: MessageSegment[],
answerStart: number,
+ isMastery: boolean,
): MessageBlock[] {
const teaching = new Set<number>();
- segments.forEach((segment, index) => {
- if (segment.kind !== "mastery_question") return;
+ const preserveTeachingBefore = (index: number) => {
let before = index - 1;
- // Recording a grade or updating state can share the round with the quiz.
- // Those rows do not turn the preceding teaching into working notes.
- while (before >= 0 && segments[before].kind === "trace") before -= 1;
- while (before >= 0 && segments[before].kind === "text") {
- teaching.add(before);
+ // Later tool work cannot demote an already-written lesson into working
+ // notes. Other cards delimit the exchange; trace remains internal work.
+ while (before >= 0) {
+ const preceding = segments[before];
+ if (preceding.kind === "text") teaching.add(before);
+ else if (preceding.kind !== "trace") break;
before -= 1;
}
+ };
+ segments.forEach((segment, index) => {
+ if (segment.kind === "mastery_question") preserveTeachingBefore(index);
});
+ // 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[] = [];
const flush = () => {
@@ -957,8 +966,8 @@ export const AssistantMessage = memo(function AssistantMessage({
// Once answered, a clarification joins the surrounding process so resumed
// work continues under the original activity header.
const messageBlocks = useMemo(
- () => buildMessageBlocks(messageSegments, answerStart),
- [messageSegments, answerStart],
+ () => buildMessageBlocks(messageSegments, answerStart, msg.capability === "mastery_path"),
+ [messageSegments, answerStart, msg.capability],
);
// The leading run rides in the activity header, which is already pinned at
// the top and already is a disclosure — giving it the process keeps the
diff --git a/web/tests/mastery-teaching-visibility.spec.tsx b/web/tests/mastery-teaching-visibility.spec.tsx
new file mode 100644
index 000000000..da4ae2894
--- /dev/null
+++ b/web/tests/mastery-teaching-visibility.spec.tsx
@@ -0,0 +1,130 @@
+import { render, screen } from "@testing-library/react";
+import { describe, expect, it, vi } from "vitest";
+
+import { AssistantMessage } from "@/features/chat/messages/ChatMessageList";
+import type { StreamEvent } from "@/features/chat/model/protocol";
+import { initI18n } from "@/i18n/init";
+
+vi.mock("@/hooks/useVoiceAutoplay", () => ({
+ useVoiceAutoplay: () => ({ autoplayEnabled: false }),
+}));
+// jsdom does not compute the CSS grid fold's zero-height clipping. Keep the real
+// disclosure state and contents, substituting only its animated wrapper.
+vi.mock("@/components/activity", async (original) => ({
+ ...(await original<typeof import("@/components/activity")>()),
+ ActivityFold: ({ open, children }: { open: boolean; children: React.ReactNode }) => <div hidden={!open}>{children}</div>,
+}));
+initI18n("en");
+
+const lesson = "A filter lets some particles through and retains others. Its selectivity depends on particle size.";
+const transition = "Apply that principle to a new example.";
+const question = {
+ question_id: "question-1",
+ prompt: "Which particles pass through the filter?",
+ question_type: "short",
+ objective: { id: "objective-1", name: "Selective filters" },
+ difficulty: "medium",
+ attempt: 1,
+ options: [],
+ allow_free_text: true,
+};
+
+function event(type: StreamEvent["type"], content: string, metadata: Record<string, unknown> = {}): StreamEvent {
+ return { type, content, metadata, source: "mastery_path", stage: "responding", timestamp: 1 } as StreamEvent;
+}
+
+function lessonEvents(reloaded: boolean, bookkeeping = "mastery_note_explained"): StreamEvent[] {
+ const events = [
+ event("content", lesson, { call_id: "round-1", call_kind: "agent_loop_round" }),
+ event("tool_call", bookkeeping, { call_id: "note-1", tool_name: bookkeeping, assistant_content_offset: lesson.length }),
+ event("tool_result", "Recorded", { call_id: "note-1", tool_name: bookkeeping }),
+ event("thinking", "Internal assessment planning", { call_id: "round-2" }),
+ event("content", transition, { call_id: "round-2", call_kind: "agent_loop_round" }),
+ event("tool_call", "mastery_quiz", { call_id: "quiz-1", tool_name: "mastery_quiz", assistant_content_offset: (lesson + transition).length }),
+ event("tool_result", "", { tool_call_id: "quiz-1", tool_metadata: { mastery_question: question } }),
+ event("done", "", { status: "completed" }),
+ ];
+ // A persisted semantic preview uses offsets and authoritative message text,
+ // without replaying every content delta or reasoning event.
+ return reloaded ? events.filter((item) => item.type !== "content" && item.type !== "thinking") : events;
+}
+
+describe("Mastery teaching visibility", () => {
+ it("keeps teaching visible while a quiz is pending and when the turn settles", () => {
+ const events = lessonEvents(false);
+ const props = (trace: StreamEvent[], streaming: boolean) => ({
+ msg: { content: lesson + transition, capability: "mastery_path", events: trace },
+ isStreaming: streaming,
+ });
+ const view = render(<AssistantMessage {...props(events.slice(0, 5), true)} />);
+ expect(screen.getByText(lesson)).toBeVisible();
+ const pendingQuiz = [
+ ...events.slice(0, 6),
+ event("progress", "", { trace_kind: "call_status", call_state: "complete", call_role: "finish", call_id: "round-2" }),
+ ];
+ view.rerender(<AssistantMessage {...props(pendingQuiz, true)} />);
+ expect(screen.getByText(lesson)).toBeVisible();
+ expect(screen.getByText(transition)).toBeVisible();
+ view.rerender(<AssistantMessage {...props(events, false)} />);
+ expect(screen.getByText(lesson)).toBeVisible();
+ expect(screen.getByText(transition)).toBeVisible();
+ expect(screen.getByText(question.prompt)).toBeVisible();
+ });
+
+ it("keeps a teaching-only turn visible after its final bookkeeping tool", () => {
+ const events = lessonEvents(false).slice(0, 3);
+ render(<AssistantMessage msg={{ content: lesson, capability: "mastery_path", events }} />);
+ expect(screen.getByText(lesson)).toBeVisible();
+ });
+
+ it.each([false, true])("keeps the lesson visible across bookkeeping (reloaded=%s)", (reloaded) => {
+ render(<AssistantMessage msg={{ content: lesson + transition, capability: "mastery_path", events: lessonEvents(reloaded) }} />);
+ expect(screen.getByText(lesson)).toBeVisible();
+ expect(screen.getByText(transition)).toBeVisible();
+ expect(screen.getByText(question.prompt)).toBeVisible();
+ const reasoning = screen.queryByText("Internal assessment planning");
+ if (reasoning) expect(reasoning).not.toBeVisible();
+ });
+
+ it("does not hide an existing lesson when later work retrieves more sources", () => {
+ render(<AssistantMessage msg={{ content: lesson + transition, capability: "mastery_path", events: lessonEvents(false, "rag") }} />);
+ expect(screen.getByText(lesson)).toBeVisible();
+ expect(screen.getByText(transition)).toBeVisible();
+ expect(screen.getByText(question.prompt)).toBeVisible();
+ });
+
+ it("keeps ordinary chat commentary folded when no Mastery question is posed", () => {
+ const events = lessonEvents(false, "rag").filter((item) => !(item.type === "tool_result" && item.metadata?.tool_metadata));
+ render(<AssistantMessage msg={{ content: lesson + transition, capability: "chat", events }} />);
+ expect(screen.queryByText(lesson)).not.toBeVisible();
+ });
+
+ it("does not promote the process before an earlier resolved clarification", () => {
+ const planning = "First choose which source to use.";
+ const events = [
+ event("content", planning, { call_id: "planning-round", call_kind: "agent_loop_round" }),
+ event("tool_call", "ask_user", { call_id: "clarification-1", tool_name: "ask_user", assistant_content_offset: planning.length }),
+ event("tool_result", "", { tool_call_id: "clarification-1", tool_metadata: { ask_user: { questions: [{ id: "source", prompt: "Which source?" }] } } }),
+ event("progress", "", { ask_user_resolved: true, ask_user_tool_call_id: "clarification-1", answers: [{ questionId: "source", text: "Textbook" }], assistant_content_offset: planning.length }),
+ ...lessonEvents(false),
+ ];
+ render(<AssistantMessage msg={{ content: planning + lesson + transition, capability: "mastery_path", events }} />);
+ expect(screen.queryByText(planning)).not.toBeVisible();
+ expect(screen.getByText(lesson)).toBeVisible();
+ expect(screen.getByText(question.prompt)).toBeVisible();
+ });
+
+ it("does not restore a rejected answer as visible teaching", () => {
+ const rejected = "A rejected explanation that must not become lesson text.";
+ 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();
+ expect(screen.getByText(lesson)).toBeVisible();
+ expect(screen.getByText(question.prompt)).toBeVisible();
+ });
+});