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
+26 -2
View File
@@ -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