2026-08-22 10:26:51 +03:00
|
|
|
/**
|
2026-09-11 06:44:20 +03:00
|
|
|
* phases — the event-driven phase chain (docs/probe-teach-split.md §4).
|
2026-08-22 10:26:51 +03:00
|
|
|
*
|
2026-09-11 06:44:20 +03:00
|
|
|
* probe ends at the first CONCLUDED probe_done (output-available); planning
|
2026-09-12 05:14:40 +03:00
|
|
|
* ends at the LEARNER'S plan acceptance (a data-verdict part with accepted:true
|
2026-09-11 21:26:29 +03:00
|
|
|
* accept button's user message — plan_init only PRESENTS the map). No
|
|
|
|
|
* stored-tree seam: the phase is a pure fold over the transcript, so it can
|
|
|
|
|
* never stick (the old currentNodeId-based phaseOfTree read a field nothing
|
|
|
|
|
* ever set).
|
2026-08-22 10:26:51 +03:00
|
|
|
*/
|
2026-09-11 06:44:20 +03:00
|
|
|
import assert from "node:assert";
|
|
|
|
|
import test from "node:test";
|
|
|
|
|
import type { UIMessage } from "ai";
|
|
|
|
|
import { phaseAt3, phaseTimeline, type Phase3 } from "../src/core/phases.ts";
|
2026-08-22 10:26:51 +03:00
|
|
|
|
2026-09-11 06:44:20 +03:00
|
|
|
/** Text-only user message. */
|
|
|
|
|
function user(text: string): UIMessage {
|
|
|
|
|
return { id: `u_${text}`, role: "user", parts: [{ type: "text", text }] };
|
2026-08-22 10:26:51 +03:00
|
|
|
}
|
|
|
|
|
|
2026-09-11 06:44:20 +03:00
|
|
|
/** An assistant message carrying a tool part in the given state. */
|
|
|
|
|
function assistantTool(
|
|
|
|
|
name: string,
|
|
|
|
|
state: "input-available" | "output-available" | "output-error",
|
|
|
|
|
output?: unknown,
|
|
|
|
|
): UIMessage {
|
2026-09-01 18:24:13 +03:00
|
|
|
return {
|
2026-09-11 06:44:20 +03:00
|
|
|
id: `a_${name}_${state}_${Math.random().toString(36).slice(2, 6)}`,
|
|
|
|
|
role: "assistant",
|
|
|
|
|
parts: [{ type: `tool-${name}`, state, ...(output !== undefined ? { output } : {}) } as never],
|
2026-09-01 18:24:13 +03:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-11 06:44:20 +03:00
|
|
|
const DONE_ASSESSMENT = { assessment: { strands: [{ strand: "s", assessment: "a" }] } };
|
|
|
|
|
const PLAN_OK = { ok: true, tree: { nodes: [{ id: "n1", title: "N" }], edges: [] } };
|
2026-09-01 18:24:13 +03:00
|
|
|
|
2026-09-12 05:14:40 +03:00
|
|
|
/** The verdict button's user message (the custom-message scheme): its own
|
|
|
|
|
* message carrying ONLY the data-verdict marker part — no human-authored
|
|
|
|
|
* text; the model-facing wording is the adapter's (record-adapter.ts). */
|
|
|
|
|
function verdict(accepted: boolean): UIMessage {
|
2026-09-11 21:26:29 +03:00
|
|
|
return {
|
2026-09-12 05:14:40 +03:00
|
|
|
id: `u_verdict_${Math.random().toString(36).slice(2, 6)}`,
|
2026-09-11 21:26:29 +03:00
|
|
|
role: "user",
|
|
|
|
|
parts: [
|
2026-09-12 05:14:40 +03:00
|
|
|
{ type: "data-verdict", id: "vd_1", data: { accepted, at: "2026-09-12T00:00:00Z" } } as never,
|
2026-09-11 21:26:29 +03:00
|
|
|
],
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-12 05:14:40 +03:00
|
|
|
/** The accept button's message. */
|
|
|
|
|
function planAccepted(): UIMessage {
|
|
|
|
|
return verdict(true);
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-11 21:26:29 +03:00
|
|
|
test("phaseAt3: probe until a concluded probe_done; plan until the learner ACCEPTS; teach after", () => {
|
2026-09-11 06:44:20 +03:00
|
|
|
const probe = phaseAt3([user("teach me calculus"), assistantTool("quiz", "output-available")]);
|
|
|
|
|
assert.equal(probe, "probe", "quiz answers alone never end the probe");
|
2026-09-01 18:24:13 +03:00
|
|
|
|
2026-09-11 06:44:20 +03:00
|
|
|
const concluded = phaseAt3([
|
|
|
|
|
user("teach me"),
|
|
|
|
|
assistantTool("probe_done", "output-available", DONE_ASSESSMENT),
|
|
|
|
|
]);
|
|
|
|
|
assert.equal(concluded, "plan", "a concluded probe_done ends the probe (planning begins)");
|
|
|
|
|
|
2026-09-11 21:26:29 +03:00
|
|
|
const presented = phaseAt3([
|
2026-09-11 06:44:20 +03:00
|
|
|
user("teach me"),
|
|
|
|
|
assistantTool("probe_done", "output-available", DONE_ASSESSMENT),
|
|
|
|
|
assistantTool("plan_init", "output-available", PLAN_OK),
|
|
|
|
|
]);
|
2026-09-11 21:26:29 +03:00
|
|
|
assert.equal(presented, "plan", "a successful plan_init PRESENTS the map — planning continues until the verdict");
|
|
|
|
|
|
|
|
|
|
const accepted = phaseAt3([
|
|
|
|
|
user("teach me"),
|
|
|
|
|
assistantTool("probe_done", "output-available", DONE_ASSESSMENT),
|
|
|
|
|
assistantTool("plan_init", "output-available", PLAN_OK),
|
|
|
|
|
planAccepted(),
|
|
|
|
|
]);
|
|
|
|
|
assert.equal(accepted, "teach", "the learner's acceptance ends planning");
|
|
|
|
|
|
|
|
|
|
const declined = phaseAt3([
|
|
|
|
|
user("teach me"),
|
|
|
|
|
assistantTool("probe_done", "output-available", DONE_ASSESSMENT),
|
|
|
|
|
assistantTool("plan_init", "output-available", PLAN_OK),
|
2026-09-12 05:14:40 +03:00
|
|
|
verdict(false),
|
2026-09-11 21:26:29 +03:00
|
|
|
assistantTool("plan_init", "output-available", PLAN_OK),
|
|
|
|
|
]);
|
2026-09-12 05:14:40 +03:00
|
|
|
assert.equal(declined, "plan", "a DECLINED verdict is a real event but stays in planning until a later acceptance");
|
2026-09-11 06:44:20 +03:00
|
|
|
|
|
|
|
|
const failedPlan = phaseAt3([
|
|
|
|
|
user("teach me"),
|
|
|
|
|
assistantTool("probe_done", "output-available", DONE_ASSESSMENT),
|
|
|
|
|
assistantTool("plan_init", "output-error", { ok: false, error: "bad mermaid" }),
|
2026-09-11 21:26:29 +03:00
|
|
|
planAccepted(),
|
2026-09-11 06:44:20 +03:00
|
|
|
]);
|
2026-09-11 21:26:29 +03:00
|
|
|
assert.equal(failedPlan, "teach", "acceptance sanctions teaching even after a failed init attempt — the verdict is his");
|
2026-09-01 18:24:13 +03:00
|
|
|
});
|
|
|
|
|
|
2026-09-12 05:14:40 +03:00
|
|
|
test("phaseAt3: the verdict marker is explicit — anonymous learner text never sanctions teaching", () => {
|
|
|
|
|
const plainText = phaseAt3([
|
|
|
|
|
user("teach me"),
|
|
|
|
|
assistantTool("probe_done", "output-available", DONE_ASSESSMENT),
|
|
|
|
|
assistantTool("plan_init", "output-available", PLAN_OK),
|
|
|
|
|
user("The plan is accepted — begin teaching."), // words alone, no marker
|
|
|
|
|
]);
|
|
|
|
|
assert.equal(plainText, "plan", "only the data-verdict part is the seam — text can never fake it");
|
|
|
|
|
|
|
|
|
|
const acceptOutOfOrder = phaseAt3([
|
|
|
|
|
user("teach me"),
|
|
|
|
|
verdict(true), // before any probe_done: out of order, advances nothing
|
|
|
|
|
]);
|
|
|
|
|
assert.equal(acceptOutOfOrder, "probe", "the chain never skips");
|
|
|
|
|
});
|
|
|
|
|
|
2026-09-11 06:44:20 +03:00
|
|
|
test("phaseAt3: a mere probe_done CALL (input-available, not yet concluded) keeps the probe open", () => {
|
|
|
|
|
const still = phaseAt3([user("teach me"), assistantTool("probe_done", "input-available")]);
|
|
|
|
|
assert.equal(still, "probe");
|
2026-09-01 18:24:13 +03:00
|
|
|
});
|
2026-09-01 18:31:01 +03:00
|
|
|
|
2026-09-11 06:44:20 +03:00
|
|
|
test("phaseAt3: legacy transcripts without probe_done read as probe (no backwards compat — data wiped)", () => {
|
|
|
|
|
const legacy = phaseAt3([user("teach me lacan"), assistantTool("plan_init", "output-available", PLAN_OK)]);
|
|
|
|
|
assert.equal(legacy, "probe", "plan_init alone never flips a phase post-split");
|
2026-09-01 18:31:01 +03:00
|
|
|
});
|
2026-09-11 06:44:20 +03:00
|
|
|
|
|
|
|
|
test("phaseTimeline: folds messages into phase spans for the dividers", () => {
|
|
|
|
|
const timeline = phaseTimeline([
|
|
|
|
|
user("teach me"),
|
|
|
|
|
assistantTool("quiz", "output-available"),
|
|
|
|
|
assistantTool("probe_done", "output-available", DONE_ASSESSMENT),
|
|
|
|
|
assistantTool("plan_init", "output-available", PLAN_OK),
|
2026-09-11 21:26:29 +03:00
|
|
|
user("what does node 1 mean?"), // side question — still planning
|
|
|
|
|
planAccepted(),
|
2026-09-11 06:44:20 +03:00
|
|
|
user("go on"),
|
|
|
|
|
]);
|
2026-09-11 21:26:29 +03:00
|
|
|
assert.deepEqual(timeline satisfies Phase3[], ["probe", "probe", "plan", "plan", "plan", "teach", "teach"]);
|
|
|
|
|
assert.equal(timeline.length, 7);
|
2026-09-11 06:44:20 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("phaseAt3: empty transcript is probe", () => {
|
|
|
|
|
assert.equal(phaseAt3([]), "probe");
|
|
|
|
|
});
|