Wave B: the native message record — input events, projection, adapter death
The chat record is now pi-native agent history (LhMessage[]), the AI SDK's
UIMessage transcript contract is gone from the server, and every client turn
arrives as ONE explicit input event.
Core (new):
- src/core/native-messages.ts — the LhMessage taxonomy (user/assistant/
toolResult + lhMarker customs: verdict, probe-handoff, subagent-steer,
subagent-abort, plan-block), constructors, and toLlmMessages — the single
model-facing seam (convertToLlm): strips ids, flattens markers to user
text, drops UI-only plan-blocks, synthesizes INTERRUPTED results for
dangling aborted calls, never throws.
- src/core/projection.ts — projectNative: the one-way native → UIMessage
projection (consecutive assistant+toolResult runs group into one
assistant view message; toolResults attach backward by toolCallId;
markers project to data parts; plan-blocks splice after the last
successful plan_init). Every existing consumer reads through it.
- src/core/input-events.ts — the ChatInput union (text/answer/verdict/
continue), parseChatInput validation, and inputAppends (answer targets
the dangling question call or 400s; verdict = optional note user
message + native verdict marker).
Server:
- chat-handler.ts — the turn root takes {chatId, input}; write-ahead
persists input appends + claimed steers before the turn; claimSteers
coalesces the whole queue with the handler's steerDelivered filter as
the dedupe seam; teacher-vs-prober branch on hasProbeDone.
- turn-service.ts — persistTurn stamps ids, drops clean question
placeholder toolResults (the record ends at the unanswered call),
emits plan-block markers idempotently (plan-blocks.ts rewrite), and
persists abort partials.
- ui-stream.ts — PiTurnSpec takes the native base; the wire dialect is
unchanged. jobs.ts steers are native markers (steer_<jobId> dedup ids);
waitForIdle reads danglingQuestionOf. teacher-agent.ts wires
convertToLlm: toLlmMessages. record-adapter.ts DELETED wholesale.
- GET /api/chats/[id] serves messages: projectNative(record.messages).
Client:
- chat-transport.ts — the 409 retry is IDENTITY (re-POST the same event
body after the turn settles — the event was never persisted
pre-acceptance; the merge logic is gone). The transport consumes the
stashed input event via prepareSendMessagesRequest and throws loudly on
a missing stash.
- chat.tsx — every send call site stashes exactly one ChatInput event
(text, answer, verdict, continue) before invoking the SDK send.
Tests: full native migration (chat-flow rewritten to input events and
native record pins; jobs/extraction/demos/graph/usage/plan-blocks/
probe-done/reattach-transport converted; record-adapter.test.ts deleted;
chat-transport tests pin the identity retry). 982/982, typecheck, lint,
build all green.
This commit is contained in:
+154
-95
@@ -1,15 +1,20 @@
|
||||
/**
|
||||
* plan-blocks — the INTERIM plan presentation seam (docs/probe-teach-split.md
|
||||
* §6): the full block renders IN FLOW exactly once, attached to the
|
||||
* assistant message carrying the successful plan_init call. The
|
||||
* neighborhood-block machinery is retired (position derives live from
|
||||
* derivePlanStatuses); the persistent DAG window (UI wave) succeeds this.
|
||||
* assistant group carrying the successful plan_init call. Wave B: emission
|
||||
* is a NATIVE plan-block marker (one per TURN, for the last successful
|
||||
* call), and the one-way projection splices its data part right after the
|
||||
* last successful plan_init tool part. The neighborhood-block machinery is
|
||||
* retired (position derives live from derivePlanStatuses); the persistent
|
||||
* DAG window (UI wave) succeeds this.
|
||||
*/
|
||||
import assert from "node:assert";
|
||||
import test from "node:test";
|
||||
import type { UIMessage } from "ai";
|
||||
import { withPlanBlocks, type PlanBlockPart } from "../src/core/plan-blocks.ts";
|
||||
import type { PlanTree } from "../src/core/contracts.ts";
|
||||
import type { LhMessage } from "../src/core/native-messages.ts";
|
||||
import { planBlocksFor } from "../src/core/plan-blocks.ts";
|
||||
import { projectNative } from "../src/core/projection.ts";
|
||||
import type { PlanBlockData } from "../src/core/contracts.ts";
|
||||
|
||||
// 5-node fixture: a → b → c, b → d, d → e.
|
||||
const TREE: PlanTree = {
|
||||
@@ -40,133 +45,187 @@ const TREE2: PlanTree = {
|
||||
version: 2,
|
||||
};
|
||||
|
||||
/** An assistant message carrying a successful plan_init with the fixture tree. */
|
||||
function planInitMessage(id: string, tree: PlanTree = TREE): UIMessage {
|
||||
/** A native assistant message with one tool call (the projection's tool
|
||||
* part source) — minimal fixture shape, same as the record holds. */
|
||||
function assistantWithCall(id: string, name: string, args: unknown, ...text: string[]): LhMessage {
|
||||
const content: unknown[] = [];
|
||||
for (const t of text) content.push({ type: "text", text: t });
|
||||
content.push({ type: "toolCall", id: `call_${id}`, name, arguments: args as never });
|
||||
return {
|
||||
id,
|
||||
role: "assistant",
|
||||
parts: [
|
||||
{ type: "text", text: "Here is the plan." },
|
||||
{ type: "tool-plan_init", state: "output-available", output: { ok: true, tree } },
|
||||
],
|
||||
} as unknown as UIMessage;
|
||||
content,
|
||||
api: "openai-completions",
|
||||
provider: "openrouter",
|
||||
model: "test",
|
||||
usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
||||
stopReason: "stop",
|
||||
timestamp: Date.now(),
|
||||
} as never;
|
||||
}
|
||||
|
||||
/** A message built from raw parts (positional tests need exact part arrays). */
|
||||
function msg(role: string, parts: unknown[]): UIMessage {
|
||||
return { id: `m_${Math.random().toString(36).slice(2, 8)}`, role, parts } as unknown as UIMessage;
|
||||
/** A native tool result for a call (details = what the record + projection
|
||||
* read; the plan tool persists {ok, tree} here). */
|
||||
function resultOf(callId: string, toolName: string, details: unknown, isError = false): LhMessage {
|
||||
return {
|
||||
id: `r_${callId}${isError ? "_e" : ""}`,
|
||||
role: "toolResult",
|
||||
toolCallId: `call_${callId}`,
|
||||
toolName,
|
||||
content: [{ type: "text", text: isError ? "failed" : "ok" }],
|
||||
details,
|
||||
isError,
|
||||
timestamp: Date.now(),
|
||||
} as never;
|
||||
}
|
||||
|
||||
function blocksOf(messages: UIMessage[]): PlanBlockPart[] {
|
||||
return messages.flatMap((m) => m.parts.filter((p) => p.type === "data-plan-block") as PlanBlockPart[]);
|
||||
/** Emit + persist + project: the full persistTurn slice for a turn whose
|
||||
* new messages are `turn`, run over `record`. Returns the projection of
|
||||
* the whole record after the write. */
|
||||
function projectAfter(record: LhMessage[], turn: LhMessage[], tree: PlanTree | null) {
|
||||
const blocks = planBlocksFor(record, turn, tree, projectNative([...record, ...turn]));
|
||||
return projectNative([...record, ...turn, ...blocks]);
|
||||
}
|
||||
|
||||
test("withPlanBlocks: a successful plan_init message gains ONE full block (all nodes, statuses from the transcript)", () => {
|
||||
const out = withPlanBlocks([planInitMessage("m1")], TREE);
|
||||
function blocksOf(messages: ReturnType<typeof projectNative>): PlanBlockData[] {
|
||||
return messages.flatMap((m) =>
|
||||
(m.parts as { type: string; data?: PlanBlockData }[])
|
||||
.filter((p) => p.type === "data-plan-block")
|
||||
.map((p) => p.data!),
|
||||
);
|
||||
}
|
||||
|
||||
test("a successful plan_init turn gains ONE full block (all nodes, statuses from the transcript)", () => {
|
||||
const turn = [
|
||||
assistantWithCall("m1", "plan_init", { tree: TREE }, "Here is the plan."),
|
||||
resultOf("m1", "plan_init", { ok: true, tree: TREE }),
|
||||
];
|
||||
const out = projectAfter([], turn, TREE);
|
||||
const blocks = blocksOf(out);
|
||||
assert.equal(blocks.length, 1);
|
||||
const data = blocks[0]!.data;
|
||||
const data = blocks[0]!;
|
||||
assert.equal(data.kind, "full");
|
||||
assert.equal(data.current, null, "at authoring nothing is current");
|
||||
assert.equal(data.nodes.length, 5);
|
||||
assert.ok(data.nodes.every((n) => n.status === "upcoming"));
|
||||
assert.equal(data.edges.length, 4);
|
||||
// attached to the plan_init message itself
|
||||
assert.equal(blocksOf([out[0]!]).length, 1);
|
||||
});
|
||||
|
||||
test("withPlanBlocks: a FAILED plan_init gains nothing", () => {
|
||||
const failed: UIMessage = {
|
||||
id: "m1",
|
||||
role: "assistant",
|
||||
parts: [
|
||||
{ type: "text", text: "oops" },
|
||||
{ type: "tool-plan_init", state: "output-error", output: { ok: false, error: "bad" } },
|
||||
],
|
||||
} as unknown as UIMessage;
|
||||
assert.equal(blocksOf(withPlanBlocks([failed], TREE)).length, 0);
|
||||
test("a FAILED plan_init gains nothing", () => {
|
||||
const turn = [
|
||||
assistantWithCall("m1", "plan_init", { tree: TREE }, "oops"),
|
||||
resultOf("m1", "plan_init", { ok: false, error: "bad" }, true),
|
||||
];
|
||||
assert.equal(blocksOf(projectAfter([], turn, TREE)).length, 0);
|
||||
});
|
||||
|
||||
test("withPlanBlocks: idempotent — a message that already carries a block is never given another", () => {
|
||||
const once = withPlanBlocks([planInitMessage("m1")], TREE);
|
||||
const twice = withPlanBlocks(once, TREE);
|
||||
assert.equal(blocksOf(twice).length, 1, "no duplicate block on persist replay");
|
||||
test("idempotent — a call whose block the record already carries is never re-emitted", () => {
|
||||
const turn = [
|
||||
assistantWithCall("m1", "plan_init", { tree: TREE }),
|
||||
resultOf("m1", "plan_init", { ok: true, tree: TREE }),
|
||||
];
|
||||
const first = projectAfter([], turn, TREE);
|
||||
assert.equal(blocksOf(first).length, 1);
|
||||
// Persist replay: the same turn's messages land again over a record
|
||||
// that already emitted the marker → no duplicate.
|
||||
const record: LhMessage[] = [...turn, ...planBlocksFor([], turn, TREE, projectNative(turn))];
|
||||
const blocks2 = planBlocksFor(record, turn, TREE, projectNative(record));
|
||||
assert.equal(blocks2.length, 0, "no duplicate block on persist replay");
|
||||
});
|
||||
|
||||
test("withPlanBlocks: a plan REWRITE (second successful plan_init) gets its own full block; the old stays frozen", () => {
|
||||
const rewrittenTree: PlanTree = { nodes: TREE.nodes.slice(0, 3), edges: [{ from: "a", to: "b" }], version: 2 };
|
||||
const out = withPlanBlocks([planInitMessage("m1"), planInitMessage("m2", rewrittenTree)], rewrittenTree);
|
||||
test("a plan REWRITE (a later turn's successful plan_init) gets its own full block; the old stays frozen", () => {
|
||||
const first = [
|
||||
assistantWithCall("m1", "plan_init", { tree: TREE }),
|
||||
resultOf("m1", "plan_init", { ok: true, tree: TREE }),
|
||||
];
|
||||
const record = [...first, ...planBlocksFor([], first, TREE, projectNative(first))];
|
||||
const second = [
|
||||
assistantWithCall("m2", "plan_init", { tree: TREE2 }),
|
||||
resultOf("m2", "plan_init", { ok: true, tree: TREE2 }),
|
||||
];
|
||||
const out = projectAfter(record, second, TREE2);
|
||||
const blocks = blocksOf(out);
|
||||
assert.equal(blocks.length, 2, "one full block per authoring event");
|
||||
assert.equal(blocks[1]!.data.nodes.length, 3);
|
||||
assert.equal(blocks.length, 2, "one full block per authoring turn");
|
||||
assert.equal(blocks[1]!.nodes.length, 3);
|
||||
});
|
||||
|
||||
test("withPlanBlocks: the block renders IN FLOW immediately after the plan_init part — not appended under the turn's later parts", () => {
|
||||
test("the block renders IN FLOW immediately after the plan_init part — not appended under the turn's later parts", () => {
|
||||
// Real-world shape (user session chat_mtwgb9lj): the authoring turn
|
||||
// carries plan_init, then presentation prose, then quiz calls — emission
|
||||
// runs at persistTurn AFTER all of that exists.
|
||||
const turn = msg("assistant", [
|
||||
{ type: "step-start" },
|
||||
{ type: "tool-plan_init", state: "output-available", output: { ok: true, tree: TREE } },
|
||||
{ type: "step-start" },
|
||||
{ type: "text", state: "done", text: "Here's the plan." },
|
||||
{ type: "tool-quiz", state: "input-available", input: { question: "q?" } },
|
||||
]);
|
||||
const out = withPlanBlocks([turn], TREE);
|
||||
const types = out[0]!.parts.map((p) => p.type);
|
||||
assert.equal(types[2], "data-plan-block", "the block directly follows the plan_init part");
|
||||
assert.equal(types.indexOf("data-plan-block"), 2);
|
||||
assert.ok(types.indexOf("data-plan-block") < types.indexOf("text"), "block before the prose");
|
||||
assert.ok(types.indexOf("data-plan-block") < types.indexOf("tool-quiz"), "block before the first check");
|
||||
// carries plan_init + prose + a quiz call — emission runs at persistTurn
|
||||
// AFTER all of that exists.
|
||||
const turn = [
|
||||
assistantWithCall("m1", "plan_init", { tree: TREE }),
|
||||
resultOf("m1", "plan_init", { ok: true, tree: TREE }),
|
||||
assistantWithCall("m1b", "quiz", { question: "q?" }, "Here's the plan."),
|
||||
resultOf("m1", "plan_init", { ok: true, tree: TREE }), // (kept out via emitted set — see next test)
|
||||
];
|
||||
turn.pop(); // drop the stray duplicate — build the real shape below
|
||||
const real = [
|
||||
assistantWithCall("m1", "plan_init", { tree: TREE }),
|
||||
resultOf("m1", "plan_init", { ok: true, tree: TREE }),
|
||||
assistantWithCall("m1b", "quiz", { question: "q?" }, "Here's the plan."),
|
||||
];
|
||||
const out = projectAfter([], real, TREE);
|
||||
const group = out.find((m) => m.role === "assistant")!;
|
||||
const types = (group.parts as unknown[]).map((p) => (p as { type: string }).type);
|
||||
const blockAt = types.indexOf("data-plan-block");
|
||||
assert.ok(blockAt > types.indexOf("tool-plan_init"), "the block directly follows the plan_init part");
|
||||
assert.ok(blockAt < types.indexOf("text"), "block before the prose");
|
||||
assert.ok(blockAt < types.indexOf("tool-quiz"), "block before the first check");
|
||||
assert.equal(types.at(-1), "tool-quiz", "the quiz is still the turn's last part");
|
||||
});
|
||||
|
||||
test("withPlanBlocks: with multiple plan_inits in one turn (a rewrite), the block follows the LAST one", () => {
|
||||
const turn = msg("assistant", [
|
||||
{ type: "tool-plan_init", state: "output-available", output: { ok: true, tree: TREE } },
|
||||
{ type: "text", state: "done", text: "v1" },
|
||||
{ type: "tool-plan_init", state: "output-available", output: { ok: true, tree: TREE2 } },
|
||||
{ type: "text", state: "done", text: "v2" },
|
||||
]);
|
||||
const out = withPlanBlocks([turn], TREE2);
|
||||
const types = out[0]!.parts.map((p) => p.type);
|
||||
assert.equal(types[3], "data-plan-block", "block directly after the last successful init");
|
||||
assert.equal(types[4], "text", "the trailing prose stays after the block");
|
||||
assert.equal(out[0]!.parts.filter((p) => p.type === "data-plan-block").length, 1, "one block per message even on a same-turn rewrite");
|
||||
test("with multiple plan_inits in one turn (a same-turn rewrite), ONE block follows the LAST one", () => {
|
||||
const turn = [
|
||||
assistantWithCall("m1", "plan_init", { tree: TREE }, "v1"),
|
||||
resultOf("m1", "plan_init", { ok: true, tree: TREE }),
|
||||
assistantWithCall("m2", "plan_init", { tree: TREE2 }, "v2"),
|
||||
resultOf("m2", "plan_init", { ok: true, tree: TREE2 }),
|
||||
];
|
||||
const out = projectAfter([], turn, TREE2);
|
||||
const group = out.find((m) => m.role === "assistant")!;
|
||||
const parts = group.parts as unknown[];
|
||||
const blocks = parts.filter((p) => (p as { type: string }).type === "data-plan-block");
|
||||
assert.equal(blocks.length, 1, "one block per turn even on a same-turn rewrite");
|
||||
const types = parts.map((p) => (p as { type: string }).type);
|
||||
assert.ok(
|
||||
types.indexOf("data-plan-block") > types.indexOf("tool-plan_init"),
|
||||
"the block follows the last successful init",
|
||||
);
|
||||
});
|
||||
|
||||
test("withPlanBlocks: quizzes alone emit nothing (no neighborhood blocks anymore)", () => {
|
||||
const quizTurn: UIMessage = {
|
||||
id: "m1",
|
||||
role: "assistant",
|
||||
parts: [
|
||||
{ type: "text", text: "check" },
|
||||
{ type: "tool-quiz", state: "output-available", input: { nodeId: "b" }, output: { correct: true } },
|
||||
],
|
||||
} as unknown as UIMessage;
|
||||
assert.equal(blocksOf(withPlanBlocks([quizTurn], TREE)).length, 0);
|
||||
test("quizzes alone emit nothing (no neighborhood blocks anymore)", () => {
|
||||
const turn = [
|
||||
assistantWithCall("m1", "quiz", { nodeId: "b", question: "q?" }),
|
||||
resultOf("m1", "quiz", { correct: true }),
|
||||
];
|
||||
assert.equal(blocksOf(projectAfter([], turn, TREE)).length, 0);
|
||||
});
|
||||
|
||||
test("withPlanBlocks: no tree / empty tree / no messages are no-ops", () => {
|
||||
assert.equal(withPlanBlocks([planInitMessage("m1")], null).length, 1, "passthrough");
|
||||
test("no tree / empty tree / no messages are no-ops", () => {
|
||||
const turn = [
|
||||
assistantWithCall("m1", "plan_init", { tree: TREE }),
|
||||
resultOf("m1", "plan_init", { ok: true, tree: TREE }),
|
||||
];
|
||||
assert.equal(planBlocksFor([], turn, null, []).length, 0, "no tree → nothing");
|
||||
const emptyTree: PlanTree = { nodes: [], edges: [], version: 0 };
|
||||
assert.equal(blocksOf(withPlanBlocks([planInitMessage("m1")], emptyTree)).length, 0);
|
||||
assert.equal(withPlanBlocks([], TREE).length, 0);
|
||||
assert.equal(planBlocksFor([], turn, emptyTree, []).length, 0, "empty tree → nothing");
|
||||
assert.equal(planBlocksFor([], [], TREE, []).length, 0, "no messages → nothing");
|
||||
});
|
||||
|
||||
test("withPlanBlocks: mid-lesson rewrite snapshot derives statuses from the whole transcript", () => {
|
||||
test("mid-lesson rewrite snapshot derives statuses from the whole transcript", () => {
|
||||
// A tagged quiz call on node b happened before the rewrite: b is current.
|
||||
const quizBefore: UIMessage = {
|
||||
id: "m0",
|
||||
role: "assistant",
|
||||
parts: [
|
||||
{ type: "text", text: "about b" },
|
||||
{ type: "tool-quiz", state: "output-available", input: { nodeId: "b" }, output: { correct: true } },
|
||||
],
|
||||
} as unknown as UIMessage;
|
||||
const out = withPlanBlocks([quizBefore, planInitMessage("m1")], TREE);
|
||||
const data = blocksOf(out)[0]!.data;
|
||||
const before = [
|
||||
assistantWithCall("m0", "quiz", { nodeId: "b", question: "q?" }),
|
||||
resultOf("m0", "quiz", { correct: true }),
|
||||
];
|
||||
const rewrite = [
|
||||
assistantWithCall("m1", "plan_init", { tree: TREE }),
|
||||
resultOf("m1", "plan_init", { ok: true, tree: TREE }),
|
||||
];
|
||||
const out = projectAfter(before, rewrite, TREE);
|
||||
const data = blocksOf(out)[0]!;
|
||||
assert.equal(data.current, "b");
|
||||
const b = data.nodes.find((n) => n.id === "b")!;
|
||||
assert.equal(b.status, "current");
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user