Turn services + agent DI: chat-handler becomes the composition root (stage 4)
The chat turn is now composed, not implemented, by chat-handler.ts:
- src/server/turn-service.ts: one named, error-contained seam per turn
responsibility — buildTeacherContext (W8 read channel), persistTurn
(canonical state write), spawnTurnJobs (async protocol kick-off),
foldTurnIntoGraph (session→graph consolidation; the extraction-pipeline
stage swaps this body behind the same seam). Each takes its deps
explicitly; failures log one [chat <id>] line and never touch the
response.
- chat-handler.ts wires: storage ports (stores), the job runner, the
teacher tools (ctx = chatId + injected ChatStore), and the services.
It no longer contains any logic beyond request validation.
Agent layer now receives dependencies instead of importing them — a new
lint zone enforces it (src/agent never imports src/server or src/store;
verified to fire both ways):
- runner.ts: createJobRunner({ chatStore, jobStore }) factory returning
the spawn hook + the three job runners; the model-override test seam
stays module-level. Bodies otherwise verbatim.
- plan tools: persist through the ctx-injected ChatStore
(TeacherToolContext = { chatId, chatStore }).
- teacher-agent's dependency-free default registry is now the true
client-side set: questionTools + jobTools (jobTools was ctx-parameter-
shaped but never used its ctx — now honestly context-free). Production
always passes createTeacherTools({ chatId, chatStore }); tests that
drive a bare mock model get the client-side set unchanged.
Zero behavior change: tsc clean, 190/190 tests, eslint clean, production
build clean. Tests inject the same fs ports the chat-handler does.
This commit is contained in:
@@ -41,10 +41,13 @@ import { sanitizeSvg, VisualCard } from "../components/artifacts/visual-card.tsx
|
||||
import { demoInputSchema, jobTools, visualInputSchema } from "../src/agent/tools/jobs.ts";
|
||||
import { runDemoBuilder } from "../src/agent/subagents/demo-builder.ts";
|
||||
import { headlessOverride, type DemoRunResult, type HeadlessApi } from "../src/agent/subagents/headless.ts";
|
||||
import { demoModelOverride, runDemoJob, runVisualJob, spawnPendingJobs, visualModelOverride } from "../src/agent/subagents/runner.ts";
|
||||
import { createJobRunner, demoModelOverride, visualModelOverride } from "../src/agent/subagents/runner.ts";
|
||||
import { runVisualMaker } from "../src/agent/subagents/visual-maker.ts";
|
||||
import { chatStore } from "../src/store/chats.ts";
|
||||
import { jobStore } from "../src/store/jobs.ts";
|
||||
|
||||
/** The runner with the same wired fs ports the chat-handler injects. */
|
||||
const jobRunner = createJobRunner({ chatStore, jobStore });
|
||||
import { handleChatRequest } from "../src/server/chat-handler.ts";
|
||||
import { createTeacherAgent } from "../src/agent/teacher-agent.ts";
|
||||
import { artifactRenderers } from "../components/artifacts/index.ts";
|
||||
@@ -464,7 +467,7 @@ describe("demo/visual job runners", () => {
|
||||
// runDemoJob has no headless injection point — the module override is the seam.
|
||||
headlessOverride.current = fakeHeadless();
|
||||
try {
|
||||
await runDemoJob(job.id, {
|
||||
await jobRunner.runDemoJob(job.id, {
|
||||
model: scriptedModel([
|
||||
{ toolCalls: [{ toolCallId: "w1", toolName: "write_demo", input: { html: GOOD_HTML } }] },
|
||||
{ toolCalls: [{ toolCallId: "r1", toolName: "run_demo", input: {} }] },
|
||||
@@ -504,7 +507,7 @@ describe("demo/visual job runners", () => {
|
||||
const job = jobStore.createJob({ kind: "visual", chatId: chat.id, toolCallId: "call-v1" });
|
||||
createdJobs.push(job.id);
|
||||
|
||||
await runVisualJob(job.id, { model: scriptedModel([{ text: "cannot" }]) });
|
||||
await jobRunner.runVisualJob(job.id, { model: scriptedModel([{ text: "cannot" }]) });
|
||||
|
||||
const errored = jobStore.getJob(job.id)!;
|
||||
assert.equal(errored.status, "error");
|
||||
@@ -588,7 +591,7 @@ describe("demo/visual job runners", () => {
|
||||
assert.equal(part.state, "output-available", "demo part fulfilled by the background runner");
|
||||
|
||||
// Idempotency: re-running the spawn hook creates no second job.
|
||||
spawnPendingJobs(chat.id, saved.messages);
|
||||
jobRunner.spawnPendingJobs(chat.id, saved.messages);
|
||||
assert.equal(jobStore.listJobs(chat.id).length, 1, "no duplicate job for the same tool part");
|
||||
} finally {
|
||||
demoModelOverride.current = undefined;
|
||||
@@ -601,7 +604,7 @@ describe("demo/visual job runners", () => {
|
||||
|
||||
describe("tool + renderer registry wiring", () => {
|
||||
it("demo and visual are CLIENT-SIDE tools with the contract schemas", () => {
|
||||
const tools = jobTools({ chatId: "c" });
|
||||
const tools = jobTools();
|
||||
assert.deepEqual(Object.keys(tools).sort(), ["demo", "research", "visual"]);
|
||||
for (const name of ["demo", "visual"] as const) {
|
||||
assert.equal(typeof (tools[name] as { execute?: unknown }).execute, "undefined", `${name} has no execute — client-side like quiz/ask`);
|
||||
|
||||
+7
-4
@@ -26,10 +26,13 @@ import { createTeacherAgent } from "../src/agent/teacher-agent.ts";
|
||||
import { handleChatRequest } from "../src/server/chat-handler.ts";
|
||||
import { chatStore } from "../src/store/chats.ts";
|
||||
import { jobStore } from "../src/store/jobs.ts";
|
||||
import { researchModelOverride, runResearchJob, spawnPendingJobs } from "../src/agent/subagents/runner.ts";
|
||||
import { createJobRunner, researchModelOverride } from "../src/agent/subagents/runner.ts";
|
||||
import { createResearcherAgent } from "../src/agent/subagents/researcher.ts";
|
||||
import { PATHS } from "../config.ts";
|
||||
|
||||
/** The runner with the same wired fs ports the chat-handler injects. */
|
||||
const jobRunner = createJobRunner({ chatStore, jobStore });
|
||||
|
||||
const JOBS_DIR = path.join(PATHS.data, "jobs");
|
||||
|
||||
const createdChats: string[] = [];
|
||||
@@ -176,7 +179,7 @@ describe("spawn hook (async protocol §2)", () => {
|
||||
trackJob(job!.id);
|
||||
|
||||
// Re-running the spawn hook (idempotency) does not create a second job.
|
||||
spawnPendingJobs(chat.id, saved.messages);
|
||||
jobRunner.spawnPendingJobs(chat.id, saved.messages);
|
||||
assert.equal(jobStore.listJobs(chat.id).length, 1, "no duplicate job for the same tool part");
|
||||
});
|
||||
});
|
||||
@@ -270,7 +273,7 @@ describe("research runner (async protocol §3)", () => {
|
||||
const job = jobStore.createJob({ kind: "research", chatId: chat.id, toolCallId: "call-run1" });
|
||||
trackJob(job.id);
|
||||
|
||||
await runResearchJob(job.id, { model: researcherModel() });
|
||||
await jobRunner.runResearchJob(job.id, { model: researcherModel() });
|
||||
|
||||
// The job is done, with the activity line from the tool call.
|
||||
const done = jobStore.getJob(job.id)!;
|
||||
@@ -313,7 +316,7 @@ describe("research runner (async protocol §3)", () => {
|
||||
const job = jobStore.createJob({ kind: "research", chatId: chat.id, toolCallId: "call-err1" });
|
||||
trackJob(job.id);
|
||||
|
||||
await runResearchJob(job.id, { model: failing });
|
||||
await jobRunner.runResearchJob(job.id, { model: failing });
|
||||
|
||||
const errored = jobStore.getJob(job.id)!;
|
||||
assert.equal(errored.status, "error");
|
||||
|
||||
@@ -236,7 +236,7 @@ function newChat(title: string): string {
|
||||
describe("plan tools (execute against the real store)", () => {
|
||||
it("plan_init creates and persists the tree; result is ok with the full tree", async () => {
|
||||
const chatId = newChat("Plan tools: init");
|
||||
const tools = planTools({ chatId });
|
||||
const tools = planTools({ chatId, chatStore });
|
||||
|
||||
const res = await exec(tools, "plan_init", {
|
||||
nodes: [
|
||||
@@ -258,7 +258,7 @@ describe("plan tools (execute against the real store)", () => {
|
||||
|
||||
it("plan_init refuses to re-init and never silently wipes the existing tree", async () => {
|
||||
const chatId = newChat("Plan tools: re-init");
|
||||
const tools = planTools({ chatId });
|
||||
const tools = planTools({ chatId, chatStore });
|
||||
await exec(tools, "plan_init", { nodes: [{ id: "a", title: "A" }] });
|
||||
|
||||
const res = await exec(tools, "plan_init", {
|
||||
@@ -274,7 +274,7 @@ describe("plan tools (execute against the real store)", () => {
|
||||
|
||||
it("plan_init returns an error result on invalid input (cycle) without persisting", async () => {
|
||||
const chatId = newChat("Plan tools: bad init");
|
||||
const tools = planTools({ chatId });
|
||||
const tools = planTools({ chatId, chatStore });
|
||||
const res = await exec(tools, "plan_init", {
|
||||
nodes: [
|
||||
{ id: "a", title: "A" },
|
||||
@@ -293,7 +293,7 @@ describe("plan tools (execute against the real store)", () => {
|
||||
|
||||
it("mutation round-trip: add node with parents, set current, mark done — each persists", async () => {
|
||||
const chatId = newChat("Plan tools: round-trip");
|
||||
const tools = planTools({ chatId });
|
||||
const tools = planTools({ chatId, chatStore });
|
||||
|
||||
await exec(tools, "plan_init", {
|
||||
nodes: [
|
||||
@@ -331,7 +331,7 @@ describe("plan tools (execute against the real store)", () => {
|
||||
|
||||
it("invalid mutations return ok:false with the current tree and persist nothing", async () => {
|
||||
const chatId = newChat("Plan tools: invalid ops");
|
||||
const tools = planTools({ chatId });
|
||||
const tools = planTools({ chatId, chatStore });
|
||||
await exec(tools, "plan_init", { nodes: [{ id: "a", title: "A" }] });
|
||||
const before = chatStore.getChat(chatId)!.tree!;
|
||||
|
||||
@@ -353,7 +353,7 @@ describe("plan tools (execute against the real store)", () => {
|
||||
|
||||
it("mutation tools work on a fresh (uninitialized) chat: the tree seeds from empty", async () => {
|
||||
const chatId = newChat("Plan tools: no init");
|
||||
const tools = planTools({ chatId });
|
||||
const tools = planTools({ chatId, chatStore });
|
||||
const res = await exec(tools, "plan_add_node", { id: "a", title: "A" });
|
||||
assert.equal(res.ok, true);
|
||||
assert.equal(res.tree.nodes.length, 1);
|
||||
@@ -361,7 +361,7 @@ describe("plan tools (execute against the real store)", () => {
|
||||
});
|
||||
|
||||
it("unknown chat returns ok:false with the empty tree", async () => {
|
||||
const tools = planTools({ chatId: "chat_nonexistent" });
|
||||
const tools = planTools({ chatId: "chat_nonexistent", chatStore });
|
||||
const res = await exec(tools, "plan_mark_done", { id: "a" });
|
||||
assert.equal(res.ok, false);
|
||||
assert.match(res.error!, /Chat not found/);
|
||||
|
||||
Reference in New Issue
Block a user