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:
+29
-26
@@ -15,11 +15,14 @@ import type { UIMessage } from "ai";
|
||||
import type { PlanTree, PlanToolResult } from "../src/core/contracts.ts";
|
||||
import type { QuestionAnswerData } from "../src/core/question-core.ts";
|
||||
import { createTree, markDone, removeNode } from "../src/core/plan-tree.ts";
|
||||
import { emitChat, foldStaleChats, getGraph, graphSnapshot } from "../src/core/graph.ts";
|
||||
import { graphLedgers } from "../src/store/graph.ts";
|
||||
import { createChat, getChat, saveMessages, saveTree } from "../src/store/chats.ts";
|
||||
import { emitChat, foldStaleChats, getGraph, graphSnapshot, type GraphDeps } from "../src/core/graph.ts";
|
||||
import { graphStore } from "../src/store/graph.ts";
|
||||
import { chatStore } from "../src/store/chats.ts";
|
||||
import { LEDGER_FILES, PATHS } from "../config.ts";
|
||||
|
||||
/** The wired fs adapters, exactly what src/server/stores.ts passes in production. */
|
||||
const graphDeps = { chatStore, graphStore } as const satisfies GraphDeps;
|
||||
|
||||
// ─── ledger isolation (backup-and-clear of data/graph/*.jsonl) ──────────────
|
||||
|
||||
const LEDGER_PATHS = Object.values(LEDGER_FILES);
|
||||
@@ -95,11 +98,11 @@ function answer(over: Partial<QuestionAnswerData>): QuestionAnswerData {
|
||||
|
||||
/** Create a chat in the real store with a tree and messages; returns the record. */
|
||||
function fixtureChat(title: string, tree: PlanTree | null, messages: UIMessage[]) {
|
||||
const chat = createChat(title);
|
||||
const chat = chatStore.createChat(title);
|
||||
createdChats.push(chat.id);
|
||||
if (tree) saveTree(chat.id, tree);
|
||||
saveMessages(chat.id, messages);
|
||||
return getChat(chat.id)!;
|
||||
if (tree) chatStore.saveTree(chat.id, tree);
|
||||
chatStore.saveMessages(chat.id, messages);
|
||||
return chatStore.getChat(chat.id)!;
|
||||
}
|
||||
|
||||
/** The fixture tree: alpha → beta, gamma free-floating, alpha done. */
|
||||
@@ -188,9 +191,9 @@ let chat2Id = "";
|
||||
/** Everything the graph holds for a set of chats (other processes' test
|
||||
* emissions run concurrently against the same files — scope, never assume). */
|
||||
function scopedGraph(chatIds: string[]) {
|
||||
const graph = getGraph();
|
||||
const graph = getGraph(graphStore);
|
||||
const nodeIds = new Set(
|
||||
graphLedgers
|
||||
graphStore
|
||||
.readMappings()
|
||||
.filter((m) => chatIds.includes(m.chatId))
|
||||
.map((m) => m.graphNodeId),
|
||||
@@ -204,7 +207,7 @@ function scopedGraph(chatIds: string[]) {
|
||||
|
||||
describe("graph emission (emitChat)", () => {
|
||||
it("folds tree nodes/edges and quiz/mark_done events with the probe/check boundary", () => {
|
||||
const stats = emitChat(chat1.id);
|
||||
const stats = emitChat(chat1.id, graphDeps);
|
||||
assert.deepEqual(stats, { nodes: 3, edges: 1, events: 6 });
|
||||
|
||||
const graph = scopedGraph([chat1.id]);
|
||||
@@ -262,14 +265,14 @@ describe("graph emission (emitChat)", () => {
|
||||
});
|
||||
|
||||
it("is idempotent — re-emission appends nothing", () => {
|
||||
const stats = emitChat(chat1.id);
|
||||
const stats = emitChat(chat1.id, graphDeps);
|
||||
assert.deepEqual(stats, { nodes: 0, edges: 0, events: 0 });
|
||||
const graph = scopedGraph([chat1.id]);
|
||||
assert.equal(graph.nodes.length, 3);
|
||||
assert.equal(graph.edges.length, 1);
|
||||
assert.equal(graph.events.length, 6);
|
||||
// exactly one checkpoint line per fold, though — checkpoints are a log
|
||||
assert.equal(graphLedgers.readCheckpoints().filter((c) => c.chatId === chat1.id).length, 2);
|
||||
assert.equal(graphStore.readCheckpoints().filter((c) => c.chatId === chat1.id).length, 2);
|
||||
});
|
||||
|
||||
it("accumulates edge provenance when another chat maps onto the same nodes", () => {
|
||||
@@ -288,13 +291,13 @@ describe("graph emission (emitChat)", () => {
|
||||
assistant("a1", [planPart("p1", "plan_init", { nodes: [], edges: [] }, tree2)]),
|
||||
]);
|
||||
|
||||
const before = getGraph();
|
||||
const before = getGraph(graphStore);
|
||||
const gAlpha = before.nodes.find((n) => n.statement === "Alpha intuition")!.id;
|
||||
const gBeta = before.nodes.find((n) => n.statement === "Beta rule")!.id;
|
||||
graphLedgers.appendMapping({ chatId: chat2.id, treeNodeId: "alpha", graphNodeId: gAlpha });
|
||||
graphLedgers.appendMapping({ chatId: chat2.id, treeNodeId: "beta", graphNodeId: gBeta });
|
||||
graphStore.appendMapping({ chatId: chat2.id, treeNodeId: "alpha", graphNodeId: gAlpha });
|
||||
graphStore.appendMapping({ chatId: chat2.id, treeNodeId: "beta", graphNodeId: gBeta });
|
||||
|
||||
const stats = emitChat(chat2.id);
|
||||
const stats = emitChat(chat2.id, graphDeps);
|
||||
assert.deepEqual(stats, { nodes: 0, edges: 1, events: 0 });
|
||||
chat2Id = chat2.id;
|
||||
|
||||
@@ -305,7 +308,7 @@ describe("graph emission (emitChat)", () => {
|
||||
});
|
||||
|
||||
it("derives node states, sessions, and node provenance in graphSnapshot", () => {
|
||||
const snap = graphSnapshot();
|
||||
const snap = graphSnapshot(graphDeps);
|
||||
const idByStatement = new Map(snap.nodes.map((n) => [n.statement, n.id]));
|
||||
assert.equal(snap.nodeStates[idByStatement.get("Alpha intuition")!], "taught");
|
||||
assert.equal(snap.nodeStates[idByStatement.get("Beta rule")!], "missed");
|
||||
@@ -336,7 +339,7 @@ describe("graph emission (emitChat)", () => {
|
||||
assistant("a2", [planPart("p2", "plan_remove_node", { id: "y" }, prunedTree)]),
|
||||
]);
|
||||
|
||||
const stats = emitChat(chat.id);
|
||||
const stats = emitChat(chat.id, graphDeps);
|
||||
assert.deepEqual(stats, { nodes: 1, edges: 0, events: 0 }); // y pruned, edge cascaded away
|
||||
|
||||
const graph = scopedGraph([chat.id]);
|
||||
@@ -345,7 +348,7 @@ describe("graph emission (emitChat)", () => {
|
||||
});
|
||||
|
||||
it("returns zeros for unknown chats", () => {
|
||||
assert.deepEqual(emitChat("chat_does_not_exist"), { nodes: 0, edges: 0, events: 0 });
|
||||
assert.deepEqual(emitChat("chat_does_not_exist", graphDeps), { nodes: 0, edges: 0, events: 0 });
|
||||
});
|
||||
});
|
||||
|
||||
@@ -366,11 +369,11 @@ describe("catch-up (foldStaleChats)", () => {
|
||||
]),
|
||||
]);
|
||||
|
||||
const folded = foldStaleChats();
|
||||
const folded = foldStaleChats(graphDeps);
|
||||
assert.ok(folded >= 1, "the catch-up fixture was folded");
|
||||
assert.equal(graphLedgers.readCheckpoints().filter((c) => c.chatId === chat.id).length, 1);
|
||||
assert.equal(graphStore.readCheckpoints().filter((c) => c.chatId === chat.id).length, 1);
|
||||
|
||||
const snap = graphSnapshot();
|
||||
const snap = graphSnapshot(graphDeps);
|
||||
const solo = snap.nodes.find((n) => n.statement === "Solo node");
|
||||
assert.ok(solo, "solo node was emitted by the catch-up fold");
|
||||
assert.equal(snap.nodeStates[solo.id], "known"); // correct check, no taught event
|
||||
@@ -378,8 +381,8 @@ describe("catch-up (foldStaleChats)", () => {
|
||||
|
||||
// Already checkpointed → not folded again (other test files' chats may
|
||||
// legitimately fold here, so assert on THIS chat only).
|
||||
foldStaleChats();
|
||||
assert.equal(graphLedgers.readCheckpoints().filter((c) => c.chatId === chat.id).length, 1);
|
||||
foldStaleChats(graphDeps);
|
||||
assert.equal(graphStore.readCheckpoints().filter((c) => c.chatId === chat.id).length, 1);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -388,14 +391,14 @@ describe("catch-up (foldStaleChats)", () => {
|
||||
describe("graph ledger store", () => {
|
||||
it("round-trips lines and skips corrupt / foreign-shaped lines (strict reads)", () => {
|
||||
const node = { id: "n_storetest", statement: "Store test", aliases: [], kind: "truth" as const, createdAt: "2026-08-22T00:00:00.000Z" };
|
||||
graphLedgers.appendNode(node);
|
||||
graphStore.appendNode(node);
|
||||
// A torn append and a legacy V0-shaped node (different schema) — both skipped.
|
||||
fs.appendFileSync(
|
||||
LEDGER_FILES.nodes,
|
||||
`{not json}\n${JSON.stringify({ id: "legacy", statement: "Legacy shape", domain: "x", state: "known" })}\n`,
|
||||
"utf-8",
|
||||
);
|
||||
const nodes = graphLedgers.readNodes().filter((n) => n.id === "n_storetest" || n.id === "legacy");
|
||||
const nodes = graphStore.readNodes().filter((n) => n.id === "n_storetest" || n.id === "legacy");
|
||||
assert.equal(nodes.length, 1);
|
||||
assert.equal(nodes[0]!.statement, "Store test");
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user