Files
learnharness/src/core/input-events.ts
T
amosblomqvist 145b470752 fix: verdict death — plan→teach seam inferred from first nodeId-tagged quiz
The Accept/Decline verdict gate is gone (owner ruling 2026-09-26):
presenting the plan ends the teacher's turn; the learner's next message
is a natural reply. Teaching begins at the first quiz call tagged with a
nodeId (probe quizzes carry none) — both folds (view phaseAt3, native
phaseAt3Lh) advance on the call itself, and the teach divider renders
before that whole assistant block. No backwards compat: verdict markers,
the verdict input event, optimistic appends, composer buttons, and
projection all deleted; prompts and docs rewritten to the natural flow.
2026-09-27 00:48:46 +03:00

110 lines
4.6 KiB
TypeScript

/**
* THE INPUT-EVENTS CONTRACT (Wave B, owner ruling 2026-09-26: "input
* events now, in the same pass"): POST /api/chat carries `{chatId, input}`
* — ONE explicit event, never a client transcript. The server owns the
* record; a send tells it what HAPPENED, not what the client's list looks
* like. The 409 merge-retry died with the full-list body (an input event
* retries idempotently: nothing was appended when a POST is refused).
*
* The four events (the complete client-originated input taxonomy):
* text — a learner-authored user message (composer send, initial
* topic, the demo error-fix loop)
* answer — the pending question's graded result (QuestionAnswerData,
* built client-side exactly as today; a dismissal is an answer
* carrying its flag). Server-side this becomes the call's
* native tool result — fork 1's normal agent history.
* continue — run the next turn on the current record (mount
* continuations, job-settle re-syncs: the record ends at an
* answered question with no teacher reaction yet).
*
* (The plan verdict input died 2026-09-26 with the verdict itself — the
* learner's post-plan reply is a natural text message.)
*
* Server-originated delivery (the wake pipeline's steers) is NOT a client
* input — the handler accepts it on the same path (TurnInput) so every
* turn rides one composition.
*/
import type { QuestionAnswerData } from "./question-core.ts";
import type { LhMessage } from "./native-messages.ts";
import {
danglingQuestionOf,
newAnswerResult,
newUserMessage,
} from "./native-messages.ts";
import { QUESTION_TOOLS } from "./pending.ts";
/** One client-originated input event (the POST body's `input`). */
export type ChatInput =
| { type: "text"; text: string }
| { type: "answer"; toolCallId: string; answer: QuestionAnswerData }
| { type: "continue" };
/** A wake-pipeline steer delivery — same turn composition, server-origin.
* Wave C (2026-09-26): the idle wake carries EVERY queued marker as its
* input appends — with claimSteers deleted, the pump's wake is the only
* steer appender left (the in-run path hands markers to pi's own
* follow-up queue instead, so it never builds an input event). */
export type SteerInput = { type: "steers"; steers: LhMessage[] };
/** Every input the turn composition accepts. */
export type TurnInput = ChatInput | SteerInput;
/** A malformed input event (the honest 400 — the orphan-trap discipline). */
export class ChatInputError extends Error {}
/** Parse and validate an input event off a request body. */
export function parseChatInput(input: unknown): ChatInput {
if (typeof input !== "object" || input === null) throw new ChatInputError("input must be an object");
const i = input as Record<string, unknown>;
switch (i.type) {
case "text":
if (typeof i.text !== "string" || (i.text as string).trim().length === 0) {
throw new ChatInputError("text input requires non-empty text");
}
return { type: "text", text: i.text };
case "answer":
if (typeof i.toolCallId !== "string" || i.toolCallId.length === 0) {
throw new ChatInputError("answer input requires toolCallId");
}
if (typeof i.answer !== "object" || i.answer === null) {
throw new ChatInputError("answer input requires the graded answer object");
}
return { type: "answer", toolCallId: i.toolCallId, answer: i.answer as QuestionAnswerData };
case "continue":
return { type: "continue" };
default:
throw new ChatInputError(`unknown input type: ${String(i.type)}`);
}
}
/**
* The native appends an input event makes to the record (pure). The caller
* persists them write-ahead; the agent's context is the record plus these.
*
* Answer validation is the orphan-400 discipline: the call must be a
* DANGLING QUESTION call in the record (posed, never answered) — anything
* else 400s honestly instead of silently interrupting a question or
* double-answering.
*/
export function inputAppends(record: { messages: LhMessage[] }, input: ChatInput): LhMessage[] {
switch (input.type) {
case "text":
return [newUserMessage(input.text)];
case "answer": {
const pending = danglingQuestionOf(record.messages);
if (!pending || pending.toolCallId !== input.toolCallId) {
throw new ChatInputError("answer input targets no pending question call");
}
return [newAnswerResult(input.toolCallId, pending.tool, input.answer)];
}
case "continue":
return [];
}
}
/** Is this tool name a question tool (dock-owned, answer-gated)? */
export function isQuestionTool(name: string): boolean {
return (QUESTION_TOOLS as readonly string[]).includes(name);
}