Storage ports: ChatStore/GraphStore/JobStore + adapters + core inversion (stage 3)

Per-entity semantic ports (src/core/ports.ts), one adapter family
(src/store/*, fs JSON/JSONL), exactly one composition point
(src/server/stores.ts). Dependency direction is now machine-checked:
adapters implement core interfaces; core never imports an adapter.

The inversion:
- core/graph.ts (emitChat/getGraph/graphSnapshot/foldStaleChats) is now
  pure — it takes the storage ports as explicit parameters (GraphDeps).
  No store imports; newGraphNodeId is local; node id format unchanged.
- core/graph-context.ts buildGraphContext() no longer defaults to the
  live ledgers: the chat-handler hands in getGraph(graphStore) per turn.
- store adapters export exactly one object each implementing their port
  (chatStore/graphStore/jobStore); bodies otherwise verbatim.
  NodeMapping + SessionCheckpoint moved store/graph -> core/contracts
  (they ARE contract shapes); CreateJobInit/JobPatch live in ports.
- titleFromPrompt moved store/chats -> core/contracts (pure helper,
  planNodeStatus precedent). Store modules now contain persistence only.
- server/stores.ts is the composition root; server/{chats,graph,jobs}.ts
  and chat-handler wire from it; agent (plan tools, sub-agent runner)
  call the port objects. The shell surface (@/src/server exports) is
  unchanged.

Lint law extended: the whole of src/core is now a purity zone (was a
file glob with a graph/graph-context exemption). Verified it fires on
core->store imports; tsc clean, 190/190 tests, eslint clean, prod build
clean (prerender 6/6). Tests pass deps explicitly — graph.test uses the
same fs-adapter pair stores.ts wires in production; graph-context.test
keeps its in-memory ledgers as a GraphStore implementation.
This commit is contained in:
Amos
2026-08-31 20:00:12 +03:00
parent e92140f993
commit 1167157c23
24 changed files with 434 additions and 282 deletions
+30 -30
View File
@@ -1,8 +1,8 @@
import { isToolUIPart, type LanguageModel, type UIMessage } from "ai";
import type { DemoResult, DemoToolInput, JobKind, ResearchResult, ResearchToolInput, VisualResult, VisualToolInput } from "../../core/contracts.ts";
import { JOB_TOOLS } from "../../core/pending.ts";
import { getChat, saveMessages } from "../../store/chats.ts";
import { createJob, findJobByToolCallId, getJob, updateJob } from "../../store/jobs.ts";
import { chatStore } from "../../store/chats.ts";
import { jobStore } from "../../store/jobs.ts";
import { runDemoBuilder } from "./demo-builder.ts";
import { createResearcherAgent } from "./researcher.ts";
import { runVisualMaker } from "./visual-maker.ts";
@@ -60,22 +60,22 @@ export function spawnPendingJobs(chatId: string, messages: UIMessage[]): void {
if (!(JOB_TOOLS as readonly string[]).includes(tool)) continue;
const runner = KIND_RUNNERS[tool as JobKind];
if (!runner) continue;
if (findJobByToolCallId(chatId, part.toolCallId)) continue; // already has a job
const job = createJob({ kind: tool as JobKind, chatId, toolCallId: part.toolCallId });
if (jobStore.findJobByToolCallId(chatId, part.toolCallId)) continue; // already has a job
const job = jobStore.createJob({ kind: tool as JobKind, chatId, toolCallId: part.toolCallId });
void runner(job.id).catch((err) => {
console.error(`[job ${job.id}] ${tool} runner crashed:`, err);
updateJob(job.id, { status: "error", error: String(err), activity: "failed" });
jobStore.updateJob(job.id, { status: "error", error: String(err), activity: "failed" });
});
}
}
export async function runResearchJob(jobId: string, opts: RunResearchJobOptions = {}): Promise<void> {
const job = getJob(jobId);
const job = jobStore.getJob(jobId);
if (!job || job.status !== "running") return;
const chat = getChat(job.chatId);
const chat = chatStore.getChat(job.chatId);
if (!chat) {
updateJob(jobId, { status: "error", error: `Chat not found: ${job.chatId}`, activity: "failed" });
jobStore.updateJob(jobId, { status: "error", error: `Chat not found: ${job.chatId}`, activity: "failed" });
return;
}
const input = findToolPart(chat.messages, job.toolCallId)?.input as ResearchToolInput | undefined;
@@ -94,7 +94,7 @@ export async function runResearchJob(jobId: string, opts: RunResearchJobOptions
const agent = createResearcherAgent(opts.model);
try {
updateJob(jobId, { activity: "researching" });
jobStore.updateJob(jobId, { activity: "researching" });
const result = await agent.generate({
prompt,
onToolExecutionStart: ({ toolCall }) => {
@@ -102,7 +102,7 @@ export async function runResearchJob(jobId: string, opts: RunResearchJobOptions
if (toolCall.toolName === "web_fetch" && typeof input?.url === "string") {
fetchedUrls.push(input.url);
}
updateJob(jobId, { activity: activityLine(toolCall.toolName, input) });
jobStore.updateJob(jobId, { activity: activityLine(toolCall.toolName, input) });
},
});
@@ -111,11 +111,11 @@ export async function runResearchJob(jobId: string, opts: RunResearchJobOptions
const research: ResearchResult = { brief, sources: parseSources(brief, fetchedUrls) };
fulfillPart(job.chatId, job.toolCallId, research);
updateJob(jobId, { status: "done", result: research }); // activity line kept: the last tool call is the record
jobStore.updateJob(jobId, { status: "done", result: research }); // activity line kept: the last tool call is the record
} catch (err) {
const reason = err instanceof Error ? err.message : String(err);
fulfillPart(job.chatId, job.toolCallId, { brief: `Research failed: ${reason}`, sources: [] });
updateJob(jobId, { status: "error", activity: "failed", error: reason });
jobStore.updateJob(jobId, { status: "error", activity: "failed", error: reason });
}
}
@@ -135,12 +135,12 @@ function isVisualFailure(result: VisualResult): boolean {
}
export async function runDemoJob(jobId: string, opts: RunDemoJobOptions = {}): Promise<void> {
const job = getJob(jobId);
const job = jobStore.getJob(jobId);
if (!job || job.status !== "running") return;
const chat = getChat(job.chatId);
const chat = chatStore.getChat(job.chatId);
if (!chat) {
updateJob(jobId, { status: "error", error: `Chat not found: ${job.chatId}`, activity: "failed" });
jobStore.updateJob(jobId, { status: "error", error: `Chat not found: ${job.chatId}`, activity: "failed" });
return;
}
const input = findToolPart(chat.messages, job.toolCallId)?.input as DemoToolInput | undefined;
@@ -148,25 +148,25 @@ export async function runDemoJob(jobId: string, opts: RunDemoJobOptions = {}): P
const brief = typeof input?.brief === "string" ? input.brief : "";
try {
updateJob(jobId, { activity: "building demo" });
jobStore.updateJob(jobId, { activity: "building demo" });
const result = await runDemoBuilder(
{ title, brief },
{
model: opts.model,
onActivity: (line) => updateJob(jobId, { activity: line }),
onActivity: (line) => jobStore.updateJob(jobId, { activity: line }),
},
);
fulfillPart(job.chatId, job.toolCallId, result);
if (isDemoFailure(result)) {
updateJob(jobId, { status: "error", error: result.notes ?? "demo build failed", activity: "failed" });
jobStore.updateJob(jobId, { status: "error", error: result.notes ?? "demo build failed", activity: "failed" });
} else {
updateJob(jobId, { status: "done", result });
jobStore.updateJob(jobId, { status: "done", result });
}
} catch (err) {
const reason = err instanceof Error ? err.message : String(err);
const failure: DemoResult = { title, html: "", tested: true, notes: `FAILED: ${reason}` };
fulfillPart(job.chatId, job.toolCallId, failure);
updateJob(jobId, { status: "error", error: reason, activity: "failed" });
jobStore.updateJob(jobId, { status: "error", error: reason, activity: "failed" });
}
}
@@ -175,37 +175,37 @@ export interface RunVisualJobOptions {
}
export async function runVisualJob(jobId: string, opts: RunVisualJobOptions = {}): Promise<void> {
const job = getJob(jobId);
const job = jobStore.getJob(jobId);
if (!job || job.status !== "running") return;
const chat = getChat(job.chatId);
const chat = chatStore.getChat(job.chatId);
if (!chat) {
updateJob(jobId, { status: "error", error: `Chat not found: ${job.chatId}`, activity: "failed" });
jobStore.updateJob(jobId, { status: "error", error: `Chat not found: ${job.chatId}`, activity: "failed" });
return;
}
const input = findToolPart(chat.messages, job.toolCallId)?.input as VisualToolInput | undefined;
const brief = typeof input?.brief === "string" ? input.brief : "";
try {
updateJob(jobId, { activity: "drawing figure" });
jobStore.updateJob(jobId, { activity: "drawing figure" });
const result = await runVisualMaker(
{ brief },
{
model: opts.model,
onActivity: (line) => updateJob(jobId, { activity: line }),
onActivity: (line) => jobStore.updateJob(jobId, { activity: line }),
},
);
fulfillPart(job.chatId, job.toolCallId, result);
if (isVisualFailure(result)) {
updateJob(jobId, { status: "error", error: result.notes ?? "visual build failed", activity: "failed" });
jobStore.updateJob(jobId, { status: "error", error: result.notes ?? "visual build failed", activity: "failed" });
} else {
updateJob(jobId, { status: "done", result });
jobStore.updateJob(jobId, { status: "done", result });
}
} catch (err) {
const reason = err instanceof Error ? err.message : String(err);
const failure: VisualResult = { svg: "", notes: `FAILED: ${reason}` };
fulfillPart(job.chatId, job.toolCallId, failure);
updateJob(jobId, { status: "error", error: reason, activity: "failed" });
jobStore.updateJob(jobId, { status: "error", error: reason, activity: "failed" });
}
}
@@ -241,7 +241,7 @@ function hostOf(url: string | undefined): string {
* Synchronous read-modify-write (no awaits) — atomic within the event loop.
*/
function fulfillPart(chatId: string, toolCallId: string, output: ResearchResult | DemoResult | VisualResult): void {
const chat = getChat(chatId);
const chat = chatStore.getChat(chatId);
if (!chat) return;
let found = false;
const messages = chat.messages.map((m) =>
@@ -260,7 +260,7 @@ function fulfillPart(chatId: string, toolCallId: string, output: ResearchResult
);
if (!found) return;
try {
saveMessages(chatId, messages);
chatStore.saveMessages(chatId, messages);
} catch (err) {
console.error(`[chat ${chatId}] fulfilling job part ${toolCallId} failed:`, err);
}
+4 -4
View File
@@ -13,7 +13,7 @@ import {
setCurrent,
updateNode,
} from "../../core/plan-tree.ts";
import { getChat, saveTree } from "../../store/chats.ts";
import { chatStore } from "../../store/chats.ts";
import type { TeacherToolContext } from "../tools.ts";
/**
@@ -52,12 +52,12 @@ async function runPlanOp(
chatId: string,
op: (tree: PlanTree) => PlanTree,
): Promise<PlanToolResult> {
const record = getChat(chatId);
const record = chatStore.getChat(chatId);
if (!record) return { ok: false, error: `Chat not found: ${chatId}`, tree: emptyTree() };
const current = record.tree ?? emptyTree();
try {
const next = op(current);
saveTree(chatId, next);
chatStore.saveTree(chatId, next);
return { ok: true, tree: next };
} catch (err) {
if (err instanceof PlanTreeError) return { ok: false, error: err.message, tree: current };
@@ -81,7 +81,7 @@ export function planTools(ctx: TeacherToolContext): ToolSet {
.describe("Initial `requires` edges between the nodes. Edge direction: from = prerequisite, to = dependent."),
}),
execute: async ({ nodes, edges }): Promise<PlanToolResult> => {
const record = getChat(ctx.chatId);
const record = chatStore.getChat(ctx.chatId);
if (!record) return { ok: false, error: `Chat not found: ${ctx.chatId}`, tree: emptyTree() };
const existing = record.tree;
if (existing && existing.nodes.length > 0) {