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:
+26
-2
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* Shared contracts — the single source of truth every component codes against.
|
||||
* Pure types + protocol docs; no runtime code, type-only imports (safe
|
||||
* everywhere: server, client, tests). See docs/vision.md (§ refs).
|
||||
* Pure types + protocol docs + tiny pure helpers; type-only imports of "ai"
|
||||
* (safe everywhere: server, client, tests). See docs/vision.md (§ refs).
|
||||
*/
|
||||
|
||||
import type { UIMessage } from "ai";
|
||||
@@ -83,6 +83,23 @@ export interface GraphEdge {
|
||||
provenance: string[];
|
||||
}
|
||||
|
||||
/** mappings.jsonl line — the naive-union identity map: a chat's tree node →
|
||||
* the graph node it folded into (the merge audit trail; v1 never merges two
|
||||
* sessions' tree nodes — W8 exact-title match aside). */
|
||||
export interface NodeMapping {
|
||||
chatId: string;
|
||||
treeNodeId: string;
|
||||
graphNodeId: string;
|
||||
}
|
||||
|
||||
/** sessions.jsonl line — the emission checkpoint for one chat (which chats
|
||||
* have been folded, and through which updatedAt — never fold twice). */
|
||||
export interface SessionCheckpoint {
|
||||
chatId: string;
|
||||
/** The chat record's `updatedAt` at the time of the fold. */
|
||||
foldedAt: string;
|
||||
}
|
||||
|
||||
export type LedgerEventKind =
|
||||
| "probe-correct"
|
||||
| "probe-miss"
|
||||
@@ -136,6 +153,13 @@ export interface GraphSnapshot {
|
||||
nodeSessions: Record<string, string[]>;
|
||||
}
|
||||
|
||||
/** Derive a short chat title from the learner's first message (pure).
|
||||
* Used on chat creation; deterministic so tests can pin it. */
|
||||
export function titleFromPrompt(prompt: string): string {
|
||||
const oneLine = prompt.replace(/\s+/g, " ").trim();
|
||||
return oneLine.length > 72 ? `${oneLine.slice(0, 72)}…` : oneLine;
|
||||
}
|
||||
|
||||
// ─── Chat records (the session store's on-disk format) ───────────────────────
|
||||
|
||||
/** One persisted session: data/chats/<id>.json. The stored messages are full
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
*/
|
||||
|
||||
import type { GraphEdge, GraphNode } from "./contracts.ts";
|
||||
import { deriveNodeStates, getGraph, type DerivedGraph } from "./graph.ts";
|
||||
import { deriveNodeStates, type DerivedGraph } from "./graph.ts";
|
||||
import type { NodeStateClass } from "./contracts.ts";
|
||||
|
||||
/** The section header the teacher is taught to recognize (50-graph-context.md). */
|
||||
@@ -59,7 +59,7 @@ function isHotspot(node: GraphNode, state: NodeStateClass): boolean {
|
||||
* Render the context block, or null when there is nothing worth injecting
|
||||
* (no nodes; or a truncated graph whose selection came out empty).
|
||||
*
|
||||
* Pure: callers hand in exactly what getGraph() + deriveNodeStates() produce.
|
||||
* Pure: callers hand in exactly what getGraph(graphStore) + deriveNodeStates() produce.
|
||||
*/
|
||||
export function renderGraphContext(
|
||||
nodes: GraphNode[],
|
||||
@@ -162,11 +162,11 @@ function edgeChains(edges: GraphEdge[]): string[][] {
|
||||
// ─── the per-request entry point ─────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* Build the graph context block for one request. Stateless — re-reads the
|
||||
* ledgers every call, so it self-updates as the graph grows mid-session.
|
||||
* Accepts an injected derived graph (tests); defaults to the live one.
|
||||
* Build the graph context block for one request. Pure: the caller hands in
|
||||
* the derived graph (production: getGraph(graphStore) per request in the
|
||||
* chat-handler, so it self-updates as the graph grows mid-session).
|
||||
*/
|
||||
export function buildGraphContext(graph: DerivedGraph = getGraph()): string | null {
|
||||
export function buildGraphContext(graph: DerivedGraph): string | null {
|
||||
const states = deriveNodeStates(graph.nodes, graph.events);
|
||||
return renderGraphContext(graph.nodes, graph.edges, states);
|
||||
}
|
||||
|
||||
+32
-24
@@ -16,10 +16,12 @@
|
||||
* Idempotent + incremental: mappings dedupe nodes, (from,to,type) dedupes
|
||||
* edge assertions, chat-scoped refs dedupe events, and sessions.jsonl
|
||||
* checkpoints which chats have been folded. Re-emitting a chat appends
|
||||
* nothing. The store (src/store/graph.ts) holds the append-only ledgers;
|
||||
* everything here derives from them.
|
||||
* nothing. Every function takes the storage ports (core/ports.ts) as
|
||||
* explicit parameters — pure core, no adapter imports; the composition
|
||||
* root (src/server/stores.ts) wires the fs adapters in.
|
||||
*/
|
||||
|
||||
import { randomBytes } from "node:crypto";
|
||||
import { isToolUIPart, type UIMessage } from "ai";
|
||||
import type {
|
||||
GraphEdge,
|
||||
@@ -32,13 +34,18 @@ import type {
|
||||
PlanToolResult,
|
||||
} from "./contracts.ts";
|
||||
import type { QuestionAnswerData } from "./question-core.ts";
|
||||
import { getChat, listChats } from "../store/chats.ts";
|
||||
import { graphLedgers, newGraphNodeId } from "../store/graph.ts";
|
||||
import type { ChatStore, GraphStore } from "./ports.ts";
|
||||
|
||||
/** The ledger surface emitChat needs (the real one is src/store/graph.ts).
|
||||
* Injectable so tests can fold fixtures into an in-memory ledger instead of
|
||||
* the live data/graph files. */
|
||||
export type GraphLedgers = typeof graphLedgers;
|
||||
/** The storage ports every graph operation needs, passed explicitly. */
|
||||
export interface GraphDeps {
|
||||
chatStore: ChatStore;
|
||||
graphStore: GraphStore;
|
||||
}
|
||||
|
||||
/** "n_" + 12 hex chars (contracts.ts GraphNode.id — store-assigned). */
|
||||
function newGraphNodeId(): string {
|
||||
return `n_${randomBytes(6).toString("hex")}`;
|
||||
}
|
||||
|
||||
// ─── transcript scanning ─────────────────────────────────────────────────────
|
||||
|
||||
@@ -109,11 +116,12 @@ export interface EmissionStats {
|
||||
* turn (chat-handler onFinish) and again on catch-up (GET /api/graph). Sync
|
||||
* and fast: a handful of small-file reads plus one append per new record.
|
||||
*
|
||||
* `ledgers` is injectable for tests (in-memory ledger); production always
|
||||
* uses the live store.
|
||||
* The storage ports are explicit (tests pass in-memory ledgers; production
|
||||
* passes the fs adapters from src/server/stores.ts).
|
||||
*/
|
||||
export function emitChat(chatId: string, ledgers: GraphLedgers = graphLedgers): EmissionStats {
|
||||
const record = getChat(chatId);
|
||||
export function emitChat(chatId: string, deps: GraphDeps): EmissionStats {
|
||||
const { graphStore: ledgers } = deps;
|
||||
const record = deps.chatStore.getChat(chatId);
|
||||
if (!record) return { nodes: 0, edges: 0, events: 0 };
|
||||
const stats: EmissionStats = { nodes: 0, edges: 0, events: 0 };
|
||||
|
||||
@@ -297,11 +305,11 @@ export interface DerivedGraph {
|
||||
* edges are merged by (from,to,type) with unioned provenance, first-seen
|
||||
* order. `store events, derive state` — this is the only reader that matters.
|
||||
*/
|
||||
export function getGraph(): DerivedGraph {
|
||||
const nodes = graphLedgers.readNodes();
|
||||
export function getGraph(graphStore: GraphStore): DerivedGraph {
|
||||
const nodes = graphStore.readNodes();
|
||||
const merged: GraphEdge[] = [];
|
||||
const byKey = new Map<string, GraphEdge>();
|
||||
for (const line of graphLedgers.readEdges()) {
|
||||
for (const line of graphStore.readEdges()) {
|
||||
const key = `${line.from}\u0000${line.to}\u0000${line.type}`;
|
||||
const existing = byKey.get(key);
|
||||
if (existing) {
|
||||
@@ -314,7 +322,7 @@ export function getGraph(): DerivedGraph {
|
||||
merged.push(edge);
|
||||
}
|
||||
}
|
||||
return { nodes, edges: merged, events: graphLedgers.readEvents() };
|
||||
return { nodes, edges: merged, events: graphStore.readEvents() };
|
||||
}
|
||||
|
||||
const QUIZ_EVENT_KINDS: ReadonlySet<string> = new Set([
|
||||
@@ -366,8 +374,8 @@ export function deriveNodeStates(nodes: GraphNode[], events: LedgerEvent[]): Rec
|
||||
// ─── the /api/graph payload ──────────────────────────────────────────────────
|
||||
|
||||
/** Everything the home graph view needs, in one fetch (shape: contracts). */
|
||||
export function graphSnapshot(): GraphSnapshot {
|
||||
const { nodes, edges, events } = getGraph();
|
||||
export function graphSnapshot(deps: GraphDeps): GraphSnapshot {
|
||||
const { nodes, edges, events } = getGraph(deps.graphStore);
|
||||
const nodeStates = deriveNodeStates(nodes, events);
|
||||
|
||||
const nodeChats = new Map<string, Set<string>>();
|
||||
@@ -379,7 +387,7 @@ export function graphSnapshot(): GraphSnapshot {
|
||||
}
|
||||
chats.add(chatId);
|
||||
};
|
||||
for (const m of graphLedgers.readMappings()) touch(m.graphNodeId, m.chatId);
|
||||
for (const m of deps.graphStore.readMappings()) touch(m.graphNodeId, m.chatId);
|
||||
for (const edge of edges) {
|
||||
for (const chatId of edge.provenance) {
|
||||
touch(edge.from, chatId);
|
||||
@@ -397,7 +405,7 @@ export function graphSnapshot(): GraphSnapshot {
|
||||
}
|
||||
const sessions: Record<string, string> = {};
|
||||
for (const id of chatIds) {
|
||||
const chat = getChat(id);
|
||||
const chat = deps.chatStore.getChat(id);
|
||||
if (chat) sessions[id] = chat.title;
|
||||
}
|
||||
|
||||
@@ -417,9 +425,9 @@ export function graphSnapshot(): GraphSnapshot {
|
||||
* sessions deposit value — vision §1 "frequent updates"). Called at the top
|
||||
* of GET /api/graph; the chat-handler onFinish hook covers the live path.
|
||||
*/
|
||||
export function foldStaleChats(): number {
|
||||
export function foldStaleChats(deps: GraphDeps): number {
|
||||
const checkpoints = new Map<string, string>();
|
||||
for (const checkpoint of graphLedgers.readCheckpoints()) {
|
||||
for (const checkpoint of deps.graphStore.readCheckpoints()) {
|
||||
const previous = checkpoints.get(checkpoint.chatId);
|
||||
if (previous === undefined || checkpoint.foldedAt > previous) {
|
||||
checkpoints.set(checkpoint.chatId, checkpoint.foldedAt);
|
||||
@@ -427,11 +435,11 @@ export function foldStaleChats(): number {
|
||||
}
|
||||
|
||||
let folded = 0;
|
||||
for (const chat of listChats()) {
|
||||
for (const chat of deps.chatStore.listChats()) {
|
||||
const foldedAt = checkpoints.get(chat.id);
|
||||
if (foldedAt !== undefined && chat.updatedAt <= foldedAt) continue;
|
||||
try {
|
||||
emitChat(chat.id);
|
||||
emitChat(chat.id, deps);
|
||||
folded++;
|
||||
} catch (err) {
|
||||
console.error(`[graph] fold failed for chat ${chat.id}:`, err);
|
||||
|
||||
+7
-4
@@ -9,13 +9,16 @@
|
||||
* components/** → "@/src/core" (this barrel, ONLY)
|
||||
* app/** → "@/src/core" + "@/src/server"
|
||||
*
|
||||
* IO-bound modules (core/graph.ts, core/graph-context.ts read the ledgers
|
||||
* via the store) stay internal to the server side until the storage-port
|
||||
* stage inverts their dependency; they are deliberately NOT re-exported
|
||||
* here.
|
||||
* Server-only core modules (core/graph.ts emission/derivation,
|
||||
* core/graph-context.ts) are pure but need storage ports passed in by a
|
||||
* server caller, so they are deliberately NOT re-exported here — the
|
||||
* server imports them directly. The storage PORT TYPES are types-only and
|
||||
* browser-safe, hence re-exported.
|
||||
*/
|
||||
|
||||
export * from "./contracts.ts";
|
||||
export * from "./ports.ts";
|
||||
|
||||
export * from "./question-core.ts";
|
||||
export * from "./plan-tree.ts";
|
||||
export * from "./phases.ts";
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
/**
|
||||
* Storage ports (the hexagonal seams — vision §1 "storage stays swappable").
|
||||
*
|
||||
* One port per ENTITY, with domain operations — never one generic
|
||||
* key/value interface. Core and the service layer that orbits it code
|
||||
* against these types only; concrete adapters live in src/store/ (fs
|
||||
* JSON/JSONL today, SQLite drop-in later) and are wired exactly once, in
|
||||
* the composition root (src/server/stores.ts). Dependencies point one way:
|
||||
* adapters may import core; core NEVER imports an adapter.
|
||||
*/
|
||||
|
||||
import type { UIMessage } from "ai";
|
||||
import type {
|
||||
ChatRecord,
|
||||
ChatSummary,
|
||||
GraphEdge,
|
||||
GraphNode,
|
||||
JobKind,
|
||||
JobRecord,
|
||||
LedgerEvent,
|
||||
NodeMapping,
|
||||
PlanTree,
|
||||
SessionCheckpoint,
|
||||
} from "./contracts.ts";
|
||||
|
||||
// ─── ChatStore — the session record's home (data/chats) ─────────────────────
|
||||
|
||||
export interface ChatStore {
|
||||
/** Create a session record (persisted immediately). */
|
||||
createChat(title: string): ChatRecord;
|
||||
/** The full record, or null if unknown id / corrupt file. */
|
||||
getChat(id: string): ChatRecord | null;
|
||||
/** All sessions, newest activity first (the sidebar's list view). */
|
||||
listChats(): ChatSummary[];
|
||||
/** Persist the full message list (client state + new response) after a turn. */
|
||||
saveMessages(id: string, messages: UIMessage[]): void;
|
||||
/** Persist the session plan tree (plan tools call this in execute). */
|
||||
saveTree(id: string, tree: PlanTree): void;
|
||||
}
|
||||
|
||||
// ─── GraphStore — the append-only evidence ledgers (data/graph) ──────────────
|
||||
// v1 surface = the five JSONL ledgers (nodes / edges / events / mappings /
|
||||
// sessions). Reads return the raw append-order lines; merging edge assertions
|
||||
// by (from, to, type) is DERIVED in core/graph.ts — the store never decides.
|
||||
// The extraction pipeline (next stage) becomes the sole writer; this read
|
||||
// surface stays.
|
||||
|
||||
export interface GraphStore {
|
||||
/** Append one GraphNode (ids are unique — no merging needed). */
|
||||
appendNode(node: GraphNode): void;
|
||||
/** Raw node lines in append order. */
|
||||
readNodes(): GraphNode[];
|
||||
|
||||
/** Append one edge ASSERTION (a second session asserting the same
|
||||
* (from, to, type) appends another line; readers merge). */
|
||||
appendEdge(assertion: GraphEdge): void;
|
||||
readEdges(): GraphEdge[];
|
||||
|
||||
/** Append one quiz outcome / taught event. */
|
||||
appendEvent(event: LedgerEvent): void;
|
||||
readEvents(): LedgerEvent[];
|
||||
|
||||
/** Append one identity-map line (chat's tree node → graph node). */
|
||||
appendMapping(mapping: NodeMapping): void;
|
||||
readMappings(): NodeMapping[];
|
||||
|
||||
/** Append one emission checkpoint. */
|
||||
appendCheckpoint(checkpoint: SessionCheckpoint): void;
|
||||
readCheckpoints(): SessionCheckpoint[];
|
||||
}
|
||||
|
||||
// ─── JobStore — background sub-agent jobs (data/jobs) ─────────────────────────
|
||||
|
||||
export interface CreateJobInit {
|
||||
kind: JobKind;
|
||||
chatId: string;
|
||||
toolCallId: string;
|
||||
/** First activity line (defaults to "starting"). */
|
||||
activity?: string;
|
||||
}
|
||||
|
||||
/** Mergeable fields — identity (id, kind, chatId, toolCallId, createdAt) is immutable. */
|
||||
export type JobPatch = Partial<Omit<JobRecord, "id" | "kind" | "chatId" | "toolCallId" | "createdAt">>;
|
||||
|
||||
export interface JobStore {
|
||||
/** Create a running job (keyed by toolCallId for spawn dedup). */
|
||||
createJob(init: CreateJobInit): JobRecord;
|
||||
getJob(id: string): JobRecord | null;
|
||||
/** Merge a patch into the record; null if unknown id. */
|
||||
updateJob(id: string, patch: JobPatch): JobRecord | null;
|
||||
/** All jobs for a chat, newest activity first. */
|
||||
listJobs(chatId: string): JobRecord[];
|
||||
/** The job fulfilling a given tool part, if any (spawn-hook dedup). */
|
||||
findJobByToolCallId(chatId: string, toolCallId: string): JobRecord | null;
|
||||
}
|
||||
Reference in New Issue
Block a user