Two follow-up rulings on the explain tool:
1. No 'I don't know' row — not needed in free recall. If he doesn't
know, he says so in his own words; his text is the only answer
channel. Dock: text mode is rowless again (the composer IS the
surface); doctrine and the shared prompt chunk updated; the format
branch and card revert to quiz-only dontKnow.
2. explanation + correctAnswer collapse into ONE field — the separate
explanation was quiz remnant (MC needs the why outside its options;
a free-text commitment carries its own reasoning). Schema is now
{question, correctAnswer, details?}; the reveal shows the single
committed answer; no explanation snapshots anywhere on the explain
path.
1047/1047 tests, typecheck/lint/build green.
405 lines
20 KiB
TypeScript
405 lines
20 KiB
TypeScript
import { Type } from "typebox";
|
|
import type { AgentTool } from "@earendil-works/pi-agent-core";
|
|
import { questionResult, serverExecute, ToolInputError } from "./tool-execute.ts";
|
|
import {
|
|
askInputSchema,
|
|
askOptionProblems,
|
|
explainInputBase,
|
|
explainInputObject,
|
|
explainInputProblems,
|
|
parseOptionsIfString,
|
|
probeQuizInputSchema,
|
|
quizInputObject,
|
|
quizInputProblems,
|
|
} from "../core/question-core.ts";
|
|
import { filterEnabledTools, type EnabledTools } from "../core/pending.ts";
|
|
import type { JobRecord } from "../core/contracts.ts";
|
|
import type { JobStore } from "../core/ports.ts";
|
|
import { jobTools, type DemoInput, type PdfInput, type ResearchInput, type VisualInput } from "./tools/jobs.ts";
|
|
import { planTools, type PlanToolContext } from "./tools/plan.ts";
|
|
import { probeDoneTool, type ProbeDoneContext } from "./tools/probe.ts";
|
|
import { proberGraphTools } from "./tools/graph.ts";
|
|
import { readTool } from "./tools/read.ts";
|
|
import { lsTool } from "./tools/ls.ts";
|
|
import { findTool } from "./tools/find.ts";
|
|
import { grepTool } from "./tools/grep.ts";
|
|
import type { GraphStore } from "../core/ports.ts";
|
|
|
|
export { jobTools };
|
|
export type { ProbeDoneContext } from "./tools/probe.ts";
|
|
|
|
/**
|
|
* The agent-split tool composition (docs/probe-teach-split.md §1): the
|
|
* PROBER and the TEACHER are different agents with different tool sets.
|
|
* The question tools share a NAME but are genuinely DIFFERENT tools —
|
|
* the prober's quiz has NO nodeId field at all (its declaration is built
|
|
* from quizInputBase; the model's schema never mentions nodeId), the
|
|
* teacher's carries the validated position field.
|
|
*
|
|
* - Question tools (quiz / ask_user_question): CLIENT-ANSWERED. Their
|
|
* execute validates the authoring refinements (the cross-field rules no
|
|
* schema carries — quizInputProblems / askOptionProblems; a failure
|
|
* throws ToolInputError, which pi's loop converts to an error tool
|
|
* result the model retries on), then returns the synthetic
|
|
* "question posed" placeholder (questionResult: terminate: true — the
|
|
* turn ends; the call streams to the browser in `input-available`
|
|
* state; the learner answers in the dock; the client grades and sends
|
|
* the verdict with addToolOutput). Input schemas live in
|
|
* core/question-core. Question tools opt into SEQUENTIAL execution so
|
|
* parallel calls resolve in the model's call order (the dock's subject
|
|
* can never flip mid-turn).
|
|
* - probe_done (src/agent/tools/probe.ts): SERVER-SIDE seam tool.
|
|
* - planTools (src/agent/tools/plan.ts): SERVER-SIDE — author-once
|
|
* plan_init. Teacher only.
|
|
* - jobTools (src/agent/tools/jobs.ts): SERVER-EXECUTED dispatch. Teacher
|
|
* only.
|
|
*/
|
|
|
|
/** GLM wobble guard (owner-approved, 2026-09-26): models sometimes emit
|
|
* `options` as a JSON-encoded string instead of an array; pi runs this
|
|
* BEFORE schema validation, so a double-encoded call lands instead of
|
|
* bouncing back as a validation error + wasted retry turn. NOTE: pi
|
|
* persists the tool call with its RAW arguments — the client coerces
|
|
* again in normalizeOptions (question-core), the same parseOptionsIfString.
|
|
* Anything unparseable passes through untouched: validation then reports
|
|
* honestly and the agent retries with a real error to learn from. */
|
|
function prepareQuestionArguments<T extends object>(args: unknown): T {
|
|
if (args && typeof args === "object" && !Array.isArray(args)) {
|
|
const o = args as { options?: unknown };
|
|
if (typeof o.options === "string") {
|
|
const parsed = parseOptionsIfString(o.options);
|
|
if (parsed !== o.options) return { ...o, options: parsed } as T;
|
|
}
|
|
return o as T;
|
|
}
|
|
return args as T;
|
|
}
|
|
|
|
export const askUserQuestionTool: AgentTool<typeof askInputSchema> = {
|
|
name: "ask_user_question",
|
|
label: "ask_user_question",
|
|
description:
|
|
"Ask the learner a single question and pause until they answer. NOT graded — there is no correct answer. " +
|
|
"Use when requirements are ambiguous, preferences are needed, a decision would materially affect the lesson, " +
|
|
"or you need an open-ended response. ONE question per turn: call a single ask_user_question, then end your " +
|
|
"turn — his answer determines the next.",
|
|
parameters: askInputSchema,
|
|
prepareArguments: prepareQuestionArguments,
|
|
executionMode: "sequential",
|
|
execute: async (_toolCallId, input) => {
|
|
const problems = askOptionProblems(input.options ?? []);
|
|
if (problems.length > 0) throw new ToolInputError("ask_user_question", problems);
|
|
return questionResult();
|
|
},
|
|
};
|
|
|
|
// ─── the prober's question tools (no nodeId — no map exists yet) ─────────────
|
|
|
|
/** The option-building craft (owner 2026-09-27: moved OUT of the system
|
|
* prompt into the tool description — pi-native doctrine travels with
|
|
* the armed tool). Shared verbatim by both quiz variants. */
|
|
const QUIZ_OPTION_CRAFT =
|
|
"Building options — the giveaway is never the distractors' wrongness, it's asymmetry: the correct option " +
|
|
"carrying its reasoning while the others are bare, or key-term bolding that flags it. Build evenness in: " +
|
|
"(1) every option is a bare claim — zero 'why' in any of them; all reasoning goes in explanation, shown only " +
|
|
"after he answers; (2) write the correct claim first, then mutate it into each distractor — what would someone " +
|
|
"holding a specific misconception claim? Same skeleton, same grain, same register; (3) each distractor is a real " +
|
|
"error he might actually make (his pick is diagnostic) yet unambiguously wrong; (4) no asymmetric bolding. If " +
|
|
"you can tell the right one cold without knowing the material, regenerate — don't patch.";
|
|
|
|
/** The explain doctrine (owner 2026-09-27): free-recall quizzing — the
|
|
* sycophancy mitigation is the commitment. Carried by the DESCRIPTION
|
|
* (pi-native ruling), not the system prompt. Shared by both variants;
|
|
* the teach variant appends the nodeId tagging paragraph. */
|
|
const EXPLAIN_DOCTRINE =
|
|
"Ask the learner to EXPLAIN something in his own words — a graded check with NO options: he types a free-text " +
|
|
"answer. Unlike ask_user_question (no right answer), explain always has one. THE COMMITMENT RULE: write correctAnswer — the FULL " +
|
|
"correct answer in your own words, reasoning included — committed before he ever sees the question. When his answer returns, grade it strictly against your committed answer in your reply: never " +
|
|
"revise the commitment after seeing his words, and never mark a wrong answer right to be kind — a near-miss is " +
|
|
"partial understanding; name exactly what he got and what he missed. He sees the committed answer on submit. If he genuinely doesn't know, he says so in his own words — that is a genuine " +
|
|
"gap, not a wrong answer. " +
|
|
"ONE question per turn: call a single explain, then end your turn — his answer determines the next.";
|
|
|
|
export const probeQuizTool: AgentTool<typeof probeQuizInputSchema> = {
|
|
name: "quiz",
|
|
label: "quiz",
|
|
description:
|
|
"Ask the learner a GRADED question with a known correct answer, then instantly grade and give feedback. " +
|
|
"Unlike ask_user_question (which collects preferences/decisions with no right answer), quiz always has a " +
|
|
"correct answer supplied by you, marks the learner's selection right/wrong, reveals the correct answer, and " +
|
|
"can show an explanation. Use it to locate the edge of his understanding: bracket each strand the lesson will " +
|
|
"depend on — something he gets right (a floor), something he gets wrong or doesn't know (a ceiling). " +
|
|
"Options-only: single-select or multi-select, plus an automatic 'I don't know' choice so the learner can " +
|
|
"signal a genuine gap instead of guessing. An always-present optional note field lets the learner attach a " +
|
|
"free-text note to ANY answer; it reaches you only when non-empty. No free-text answers — for non-graded " +
|
|
"questions use ask_user_question instead. ONE question per turn: call a single quiz, then end your turn — " +
|
|
"his answer determines the next question to ask. " + QUIZ_OPTION_CRAFT,
|
|
parameters: probeQuizInputSchema,
|
|
prepareArguments: prepareQuestionArguments,
|
|
executionMode: "sequential",
|
|
execute: async (_toolCallId, input) => {
|
|
const problems = quizInputProblems(input);
|
|
if (problems.length > 0) throw new ToolInputError("quiz", problems);
|
|
return questionResult();
|
|
},
|
|
};
|
|
|
|
/** The teacher's free-recall variant (nodeId-validated, same seam as the
|
|
* teach quiz — phases/plan recognize BOTH tool names). */
|
|
export function teachExplainToolFor(
|
|
ctx: Pick<PlanToolContext, "chatId" | "chatStore">,
|
|
): AgentTool<typeof explainInputObject> {
|
|
return {
|
|
name: "explain",
|
|
label: "explain",
|
|
description:
|
|
EXPLAIN_DOCTRINE +
|
|
" Set `nodeId` (required) to the plan node this question is about — the lesson's position on the map derives " +
|
|
"from those tags; a node takes as many questions as it deserves, so don't rush forward. Off-map checks use " +
|
|
"ask_user_question.",
|
|
parameters: explainInputObject,
|
|
prepareArguments: prepareQuestionArguments,
|
|
executionMode: "sequential",
|
|
execute: async (_toolCallId, input) => {
|
|
const problems = [...explainInputProblems(input)];
|
|
const nodeIdProblemText = nodeIdProblem(ctx, input.nodeId);
|
|
if (nodeIdProblemText) problems.push(nodeIdProblemText);
|
|
if (problems.length > 0) throw new ToolInputError("explain", problems);
|
|
return questionResult();
|
|
},
|
|
};
|
|
}
|
|
|
|
/** The prober's free-recall variant (no nodeId — same pattern as the
|
|
* probe quiz). */
|
|
export const probeExplainTool: AgentTool<typeof explainInputBase> = {
|
|
name: "explain",
|
|
label: "explain",
|
|
description: EXPLAIN_DOCTRINE,
|
|
parameters: explainInputBase,
|
|
prepareArguments: prepareQuestionArguments,
|
|
executionMode: "sequential",
|
|
execute: async (_toolCallId, input) => {
|
|
const problems = explainInputProblems(input);
|
|
if (problems.length > 0) throw new ToolInputError("explain", problems);
|
|
return questionResult();
|
|
},
|
|
};
|
|
|
|
/** The prober's registry context: the probe's own needs (probe_done —
|
|
* ProbeDoneContext) PLUS the job-dispatch wiring, since the owner ruling
|
|
* (2026-09-20) puts research + pdf in the probe phase's registry too. */
|
|
export interface ProberToolContext extends ProbeDoneContext {
|
|
/** Creates the job record (deduped by toolCallId). */
|
|
jobStore: JobStore;
|
|
/** Fire the background runner for a freshly created job. */
|
|
spawn: (job: JobRecord, input: ResearchInput | DemoInput | VisualInput | PdfInput) => void;
|
|
/** Search availability — the research dispatch tool's honest-disable
|
|
* signal (same gate as the teacher's). Absent = available. */
|
|
searchAvailable?: () => boolean;
|
|
/** The prober's graph-map reader (wave 7): present = the two map tools
|
|
* (list_sessions / list_propositions) are registered. The composition
|
|
* root passes it only when graphToolsEnabled(effective) — the graph
|
|
* master switch AND the agent.graphTools toggle (settings-resolution).
|
|
* Absent = the gate is closed, no map tools (tests drive this seam). */
|
|
graphStore?: GraphStore;
|
|
}
|
|
|
|
/** The prober's tool set: probe quiz (no nodeId) + ask + the learner-fs
|
|
* set + probe_done + the two phase-omnipresent job dispatch tools +
|
|
* (wave 7, gated) the graph-map reader. `enabled` is the resolved
|
|
* settings seam (core/pending.ts): any tool explicitly false is dropped
|
|
* from the registry. probe_done stays always-on regardless — it is how
|
|
* the phase ENDS; a prober that can never conclude is a lockout. */
|
|
export function createProberTools(
|
|
ctx: ProberToolContext,
|
|
enabled?: EnabledTools,
|
|
): Record<string, AgentTool<any>> {
|
|
// probe_done is re-added AFTER the filter, always-on by construction — it
|
|
// is how the phase ENDS; a prober that can never conclude is a lockout.
|
|
const done = probeDoneTool(ctx);
|
|
// The job tools build over the SAME context/gates as the teacher's
|
|
// (one builder, one gating semantic); the prober carries a SUBSET:
|
|
const jobs = jobTools({
|
|
chatId: ctx.chatId,
|
|
jobStore: ctx.jobStore,
|
|
spawn: ctx.spawn,
|
|
searchAvailable: ctx.searchAvailable,
|
|
});
|
|
// Quizzing mechanics (owner 2026-09-27): free-recall swaps the probe
|
|
// quiz for the probe explain — one quizzing tool at a time, whichever
|
|
// the settings arm. ask stays always-on.
|
|
const mechanics = quizMechanics(enabled);
|
|
// The armed quizzing tool, widened to the registry's heterogeneous
|
|
// AgentTool record BEFORE the filter (a conditional spread of two
|
|
// differently-typed literals makes the whole object's type a union —
|
|
// unusable as Record<string, AgentTool<any>> downstream).
|
|
const quizzing: Record<string, AgentTool<any>> =
|
|
mechanics === "free" ? { explain: probeExplainTool } : { quiz: probeQuizTool };
|
|
return {
|
|
...filterEnabledTools(
|
|
{
|
|
...quizzing,
|
|
ask_user_question: askUserQuestionTool,
|
|
// The learner-filesystem set is always-on (user-decided 2026-09,
|
|
// sources v1): the prober inspects and LOCATES learner-referenced
|
|
// files while assessing the edge. Read-only by construction.
|
|
read: readTool,
|
|
ls: lsTool,
|
|
find: findTool,
|
|
grep: grepTool,
|
|
// Job dispatch in the probe phase (owner ruling, 2026-09-20):
|
|
// research + pdf are ALWAYS available in every phase — the
|
|
// prober can verify facts or read learner PDFs while assessing.
|
|
// demo/visual stay teach-phase-only (they need the lesson's plan
|
|
// context). Same gating as the teacher: toggle-filtered here,
|
|
// research subject to the searchAvailable gate (honestly absent
|
|
// when no engine is live).
|
|
...(jobs.research ? { research: jobs.research } : {}),
|
|
pdf: jobs.pdf!,
|
|
// The map reader (wave 7): present only when the settings gate
|
|
// opened — same registration-time drop as the search gate.
|
|
...(ctx.graphStore
|
|
? proberGraphTools({ chatStore: ctx.chatStore, graphStore: ctx.graphStore })
|
|
: {}),
|
|
...done,
|
|
},
|
|
withoutMechanics(enabled),
|
|
),
|
|
...done,
|
|
};
|
|
}
|
|
|
|
// ─── the teacher's question tools (quiz carries the position field) ──────────
|
|
|
|
/** The nodeId-against-live-tree check the teacher's quiz chains after the
|
|
* shared authoring contract (the old superRefine, verbatim). */
|
|
function nodeIdProblem(ctx: Pick<PlanToolContext, "chatId" | "chatStore">, nodeId: string): string | null {
|
|
const record = ctx.chatStore.getChat(ctx.chatId);
|
|
const known = record?.tree?.nodes.map((n) => n.id) ?? [];
|
|
if (!known.includes(nodeId)) {
|
|
return (
|
|
`nodeId "${nodeId}" matches no node in this session's plan map. ` +
|
|
(known.length > 0
|
|
? `Copy one of your plan_init slugs exactly (${known.slice(0, 5).join(", ")}${known.length > 5 ? ", …" : ""}).`
|
|
: "No plan map exists yet — call plan_init before posing quizzes.")
|
|
);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/** Build the teach-side quiz tool for one request (exported for tests): the authoring contract
|
|
* + the per-session plan-nodeId validation (both run in execute — the
|
|
* grading contract is untouched). Reads the LIVE chat record, so rewrites
|
|
* mid-turn are honored, and with no tree yet every nodeId is rejected.
|
|
* nodeId is REQUIRED (owner ruling 2026-09-26): the untagged ad-hoc quiz is
|
|
* gone — off-map checks route through ask_user_question, and the teach
|
|
* seam (first tagged quiz) is structurally guaranteed. */
|
|
export function teachQuizToolFor(ctx: Pick<PlanToolContext, "chatId" | "chatStore">): AgentTool<typeof quizInputObject> {
|
|
return {
|
|
name: "quiz",
|
|
label: "quiz",
|
|
description:
|
|
"Ask the learner a GRADED question with a known correct answer, then instantly grade and give feedback. " +
|
|
"Unlike ask_user_question (which collects preferences/decisions with no right answer), quiz always has a " +
|
|
"correct answer supplied by you, marks the learner's selection right/wrong, reveals the correct answer, and " +
|
|
"can show an explanation. Use it to (1) confirm a taught node actually landed, or (2) probe understanding " +
|
|
"whenever you're unsure. Set `nodeId` (required) to the plan node this question is about — the lesson's " +
|
|
"position on the map derives from those tags; a node takes as many questions as it deserves, so don't rush " +
|
|
"forward. Off-map checks use ask_user_question. Options-only: single-select or multi-select, " +
|
|
"plus an automatic 'I don't know' choice so the learner can signal a genuine gap instead of guessing. " +
|
|
"An always-present optional note field lets the learner attach a free-text note to ANY answer; it reaches you " +
|
|
"only when non-empty. No free-text answers — for non-graded questions use ask_user_question instead. " +
|
|
"ONE question per turn: call a single quiz, then end your turn — his answer determines the next. " +
|
|
QUIZ_OPTION_CRAFT,
|
|
parameters: quizInputObject,
|
|
prepareArguments: prepareQuestionArguments,
|
|
executionMode: "sequential",
|
|
execute: async (_toolCallId, input) => {
|
|
const problems = [...quizInputProblems(input)];
|
|
const nodeIdProblemText = nodeIdProblem(ctx, input.nodeId);
|
|
if (nodeIdProblemText) problems.push(nodeIdProblemText);
|
|
if (problems.length > 0) throw new ToolInputError("quiz", problems);
|
|
return questionResult();
|
|
},
|
|
};
|
|
}
|
|
|
|
export interface TeacherToolContext extends PlanToolContext {
|
|
/** Job-tool wiring (sub-agent dispatch): the job store + the runner's
|
|
* spawn seam. The composition root injects both. */
|
|
jobStore: JobStore;
|
|
/** Fire the background runner for a freshly created job. The tool input
|
|
* rides along — the spawn fires mid-stream, before the tool part is
|
|
* persisted to the transcript (a transcript scan would race it). */
|
|
spawn: (job: JobRecord, input: ResearchInput | DemoInput | VisualInput | PdfInput) => void;
|
|
/** Search availability (search port wave, 2026-09-20): the research
|
|
* dispatch tool's honest-disable signal — no live engine = the tool
|
|
* drops from this registry (JobToolContext.searchAvailable). */
|
|
searchAvailable?: () => boolean;
|
|
}
|
|
|
|
/**
|
|
* Per-request teacher tool registry: teach questions (validated nodeId) +
|
|
* plan tools + job tools, all closing over the ctx.
|
|
*
|
|
* `enabled` is the resolved settings seam (core/pending.ts EnabledTools —
|
|
* Platform's chat-handler wiring passes it through from settings). Today
|
|
* only the sub-agent tools (research/demo/visual) are user-toggleable, but
|
|
* the filtering is GENERIC: any name explicitly false is dropped, so
|
|
* future toggleables (question-tool variants, …) need no change here.
|
|
* Absent/undefined = enabled — tests and old records see today's set.
|
|
*/
|
|
/** The quizzing mechanics (owner 2026-09-27): `tools.explain === true` in
|
|
* the resolved settings ⇒ free-recall checks (explain armed, quiz dropped)
|
|
* — anything else ⇒ multiple choice, exactly today's set. Mechanics-
|
|
* managed, NOT individually toggleable: quiz/explain never pass through
|
|
* filterEnabledTools (the 2026-09-19 wedge ruling stands — a quiz-less
|
|
* prober collapses its assessment flow). */
|
|
export function quizMechanics(enabled?: EnabledTools): "choice" | "free" {
|
|
return enabled?.explain === true ? "free" : "choice";
|
|
}
|
|
|
|
/** Strip the mechanics keys so filterEnabledTools can never drop the
|
|
* quizzing tools (the derivation above is their only switch). */
|
|
function withoutMechanics(enabled?: EnabledTools): EnabledTools | undefined {
|
|
if (!enabled) return undefined;
|
|
const { explain: _e, quiz: _q, ...rest } = enabled;
|
|
return rest;
|
|
}
|
|
|
|
export function createTeacherTools(
|
|
ctx: TeacherToolContext,
|
|
enabled?: EnabledTools,
|
|
): Record<string, AgentTool<any>> {
|
|
// Quizzing mechanics (owner 2026-09-27): free-recall swaps the teach
|
|
// quiz for the teach explain — one quizzing tool at a time, whichever
|
|
// the settings arm. ask stays always-on.
|
|
const mechanics = quizMechanics(enabled);
|
|
// Same widening as the prober's (see above) — the conditional spread of
|
|
// two differently-typed literals collapses into an unusable union type.
|
|
const quizzing: Record<string, AgentTool<any>> =
|
|
mechanics === "free" ? { explain: teachExplainToolFor(ctx) } : { quiz: teachQuizToolFor(ctx) };
|
|
return filterEnabledTools(
|
|
{
|
|
...quizzing,
|
|
ask_user_question: askUserQuestionTool,
|
|
// The learner-filesystem set is always-on (user-decided 2026-09,
|
|
// sources v1): the teacher locates and reads learner-referenced
|
|
// files on its own terms — nothing auto-injects. Read-only by
|
|
// construction (fs-shared.ts).
|
|
read: readTool,
|
|
ls: lsTool,
|
|
find: findTool,
|
|
grep: grepTool,
|
|
...planTools(ctx),
|
|
// Fix wave (2026-09-20): searchAvailable was RECEIVED here but
|
|
// DROPPED when building the inner JobToolContext — the drop-gate
|
|
// always took its ?? true default. Threaded now: the live signal
|
|
// reaches jobTools' research gate.
|
|
...jobTools({ chatId: ctx.chatId, jobStore: ctx.jobStore, spawn: ctx.spawn, searchAvailable: ctx.searchAvailable }),
|
|
},
|
|
withoutMechanics(enabled),
|
|
);
|
|
}
|