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:
+23
-17
@@ -1,21 +1,25 @@
|
||||
/**
|
||||
* ChatStore — the fs JSON adapter (one file per chat in data/chats/).
|
||||
*
|
||||
* Implements core/ports.ts ChatStore; the record shapes live in
|
||||
* core/contracts.ts (ChatRecord/ChatSummary). Idioms: sync fs, tiny
|
||||
* records, id-guarded filenames, JSON with tab indentation. Wired in the
|
||||
* composition root (src/server/stores.ts); nothing else constructs it.
|
||||
*/
|
||||
|
||||
import * as fs from "node:fs";
|
||||
import * as path from "node:path";
|
||||
import type { UIMessage } from "ai";
|
||||
import { PATHS } from "../../config.ts";
|
||||
import type { ChatStore } from "../core/ports.ts";
|
||||
import type { ChatRecord, ChatSummary, PlanTree } from "../core/contracts.ts";
|
||||
|
||||
/**
|
||||
* Chat store — one JSON file per chat in data/chats/. The record shape lives
|
||||
* in core/contracts (ChatRecord/ChatSummary); this module is the fs adapter's
|
||||
* implementation (the storage port formalizes this in a later stage).
|
||||
*/
|
||||
import type { UIMessage } from "ai";
|
||||
|
||||
function chatFile(id: string): string {
|
||||
if (!/^[A-Za-z0-9_-]+$/.test(id)) throw new Error(`Invalid chat id: ${id}`);
|
||||
return path.join(PATHS.chats, `${id}.json`);
|
||||
}
|
||||
|
||||
export function createChat(title: string): ChatRecord {
|
||||
function createChat(title: string): ChatRecord {
|
||||
const now = new Date().toISOString();
|
||||
const record: ChatRecord = {
|
||||
id: `chat_${Date.now().toString(36)}_${crypto.randomUUID().slice(0, 8)}`,
|
||||
@@ -29,7 +33,7 @@ export function createChat(title: string): ChatRecord {
|
||||
return record;
|
||||
}
|
||||
|
||||
export function getChat(id: string): ChatRecord | null {
|
||||
function getChat(id: string): ChatRecord | null {
|
||||
try {
|
||||
return JSON.parse(fs.readFileSync(chatFile(id), "utf-8")) as ChatRecord;
|
||||
} catch {
|
||||
@@ -37,7 +41,7 @@ export function getChat(id: string): ChatRecord | null {
|
||||
}
|
||||
}
|
||||
|
||||
export function listChats(): ChatSummary[] {
|
||||
function listChats(): ChatSummary[] {
|
||||
try {
|
||||
fs.mkdirSync(PATHS.chats, { recursive: true });
|
||||
return fs
|
||||
@@ -53,7 +57,7 @@ export function listChats(): ChatSummary[] {
|
||||
}
|
||||
|
||||
/** Persist the full message list (client state + new response) after a turn. */
|
||||
export function saveMessages(id: string, messages: UIMessage[]): void {
|
||||
function saveMessages(id: string, messages: UIMessage[]): void {
|
||||
const record = getChat(id);
|
||||
if (!record) throw new Error(`Chat not found: ${id}`);
|
||||
record.messages = messages;
|
||||
@@ -62,7 +66,7 @@ export function saveMessages(id: string, messages: UIMessage[]): void {
|
||||
}
|
||||
|
||||
/** Persist the session plan tree (plan tools call this in execute). */
|
||||
export function saveTree(id: string, tree: PlanTree): void {
|
||||
function saveTree(id: string, tree: PlanTree): void {
|
||||
const record = getChat(id);
|
||||
if (!record) throw new Error(`Chat not found: ${id}`);
|
||||
record.tree = tree;
|
||||
@@ -71,8 +75,10 @@ export function saveTree(id: string, tree: PlanTree): void {
|
||||
fs.writeFileSync(chatFile(id), JSON.stringify(record, null, "\t"), "utf-8");
|
||||
}
|
||||
|
||||
/** Derive a short title from the first user message. */
|
||||
export function titleFromPrompt(prompt: string): string {
|
||||
const oneLine = prompt.replace(/\s+/g, " ").trim();
|
||||
return oneLine.length > 72 ? `${oneLine.slice(0, 72)}…` : oneLine;
|
||||
}
|
||||
export const chatStore: ChatStore = {
|
||||
createChat,
|
||||
getChat,
|
||||
listChats,
|
||||
saveMessages,
|
||||
saveTree,
|
||||
};
|
||||
|
||||
+21
-35
@@ -1,9 +1,8 @@
|
||||
/**
|
||||
* Graph ledger store (W4; vision §1, contracts.ts "v1 emission").
|
||||
*
|
||||
* Five append-only JSONL ledgers at config.LEDGER_FILES:
|
||||
* GraphStore — the fs JSONL adapter: five append-only ledgers at
|
||||
* config.LEDGER_FILES (contracts.ts "v1 emission").
|
||||
* nodes.jsonl GraphNode — one line per graph node
|
||||
* edges.jsonl GraphEdge — one line per edge ASSERTION (a second
|
||||
* edges.jsonl GraphEdge — one line per edge ASSERTION (a second
|
||||
* session asserting the same (from,to,type)
|
||||
* appends another line; reads merge)
|
||||
* events.jsonl LedgerEvent — quiz outcomes + taught events
|
||||
@@ -11,38 +10,25 @@
|
||||
* (chatId, treeNodeId) → graphNodeId
|
||||
* sessions.jsonl SessionCheckpoint— emission checkpoints (foldedAt)
|
||||
*
|
||||
* data/graph/* is exclusively this store's (the legacy V0 scaffold lives in
|
||||
* data/legacy-graph). Reads are strict against the contracts.ts shapes:
|
||||
* corrupt lines are skipped, never guessed at — the ledgers are the source of
|
||||
* truth and everything else is derived (src/core/graph.ts).
|
||||
*
|
||||
* Same idioms as src/store/chats.ts: sync fs, tiny records, no framework.
|
||||
* Implements core/ports.ts GraphStore. data/graph/* is exclusively this
|
||||
* adapter's. Reads are strict against the contracts.ts shapes: corrupt lines
|
||||
* are skipped, never guessed at — the ledgers are the source of truth and
|
||||
* everything else is derived (core/graph.ts, which takes this port as a
|
||||
* parameter). Same idioms as src/store/chats.ts: sync fs, no framework.
|
||||
*/
|
||||
|
||||
import * as fs from "node:fs";
|
||||
import * as path from "node:path";
|
||||
import { randomBytes } from "node:crypto";
|
||||
import { LEDGER_FILES } from "../../config.ts";
|
||||
import type { GraphEdge, GraphNode, LedgerEvent, LedgerEventKind } from "../core/contracts.ts";
|
||||
|
||||
/** The naive-union identity map line: a chat's tree node → graph node. */
|
||||
export interface NodeMapping {
|
||||
chatId: string;
|
||||
treeNodeId: string;
|
||||
graphNodeId: string;
|
||||
}
|
||||
|
||||
/** sessions.jsonl line — the emission checkpoint for one chat. */
|
||||
export interface SessionCheckpoint {
|
||||
chatId: string;
|
||||
/** The chat record's `updatedAt` at the time of the fold. */
|
||||
foldedAt: string;
|
||||
}
|
||||
|
||||
/** "n_" + nanoid-like (contracts.ts GraphNode.id). */
|
||||
export function newGraphNodeId(): string {
|
||||
return `n_${randomBytes(6).toString("hex")}`;
|
||||
}
|
||||
import type {
|
||||
GraphEdge,
|
||||
GraphNode,
|
||||
LedgerEvent,
|
||||
LedgerEventKind,
|
||||
NodeMapping,
|
||||
SessionCheckpoint,
|
||||
} from "../core/contracts.ts";
|
||||
import type { GraphStore } from "../core/ports.ts";
|
||||
|
||||
// ─── JSONL primitives ────────────────────────────────────────────────────────
|
||||
|
||||
@@ -124,9 +110,9 @@ const isNodeMapping = (v: unknown): v is NodeMapping =>
|
||||
const isCheckpoint = (v: unknown): v is SessionCheckpoint =>
|
||||
isRecord(v) && isString(v.chatId) && isString(v.foldedAt);
|
||||
|
||||
// ─── typed ledger access ─────────────────────────────────────────────────────
|
||||
// ─── the port implementation ────────────────────────────────────────────────
|
||||
|
||||
export const graphLedgers = {
|
||||
export const graphStore: GraphStore = {
|
||||
appendNode(node: GraphNode): void {
|
||||
appendLine(LEDGER_FILES.nodes, node);
|
||||
},
|
||||
@@ -136,8 +122,8 @@ export const graphLedgers = {
|
||||
},
|
||||
|
||||
/** Append one edge ASSERTION; readers merge lines by (from,to,type). */
|
||||
appendEdge(edge: GraphEdge): void {
|
||||
appendLine(LEDGER_FILES.edges, edge);
|
||||
appendEdge(assertion: GraphEdge): void {
|
||||
appendLine(LEDGER_FILES.edges, assertion);
|
||||
},
|
||||
readEdges(): GraphEdge[] {
|
||||
return readValid(LEDGER_FILES.edges, isGraphEdge);
|
||||
|
||||
+26
-28
@@ -1,18 +1,19 @@
|
||||
/**
|
||||
* JobStore — the fs JSON adapter: one file per background sub-agent job in
|
||||
* data/jobs/ (contracts.ts "THE ASYNC PROTOCOL"). Keyed by toolCallId for
|
||||
* dedup: the chat-handler spawn hook never creates two jobs for the same
|
||||
* tool part. Implements core/ports.ts JobStore. Idioms: sync fs, id-guarded
|
||||
* filenames, JSON with tab indentation. Writes are small and rare (activity
|
||||
* line updates); the read-modify-write sections in the runner contain no
|
||||
* awaits, so they are atomic within the single-threaded event loop.
|
||||
*/
|
||||
|
||||
import * as fs from "node:fs";
|
||||
import * as path from "node:path";
|
||||
import { PATHS } from "../../config.ts";
|
||||
import type { JobKind, JobRecord } from "../core/contracts.ts";
|
||||
|
||||
/**
|
||||
* Job store — one JSON file per background sub-agent job in data/jobs/
|
||||
* (contracts.ts "THE ASYNC PROTOCOL"). Keyed by toolCallId for dedup: the
|
||||
* chat-handler spawn hook never creates two jobs for the same tool part.
|
||||
*
|
||||
* Follows src/store/chats.ts idioms: sync fs, id-guarded filenames, JSON
|
||||
* with tab indentation. Writes are small and rare (activity line updates);
|
||||
* the read-modify-write sections in the runner contain no awaits, so they
|
||||
* are atomic within the single-threaded event loop.
|
||||
*/
|
||||
import type { JobStore } from "../core/ports.ts";
|
||||
import type { CreateJobInit, JobPatch } from "../core/ports.ts";
|
||||
import type { JobRecord } from "../core/contracts.ts";
|
||||
|
||||
/** Background jobs (data/jobs). Not in config.ts PATHS (orchestrator-owned). */
|
||||
const JOBS_DIR = path.join(PATHS.data, "jobs");
|
||||
@@ -22,15 +23,7 @@ function jobFile(id: string): string {
|
||||
return path.join(JOBS_DIR, `${id}.json`);
|
||||
}
|
||||
|
||||
export interface CreateJobInit {
|
||||
kind: JobKind;
|
||||
chatId: string;
|
||||
toolCallId: string;
|
||||
/** First activity line (defaults to "starting"). */
|
||||
activity?: string;
|
||||
}
|
||||
|
||||
export function createJob(init: CreateJobInit): JobRecord {
|
||||
function createJob(init: CreateJobInit): JobRecord {
|
||||
const now = new Date().toISOString();
|
||||
const record: JobRecord = {
|
||||
id: `job_${Date.now().toString(36)}_${crypto.randomUUID().slice(0, 8)}`,
|
||||
@@ -47,7 +40,7 @@ export function createJob(init: CreateJobInit): JobRecord {
|
||||
return record;
|
||||
}
|
||||
|
||||
export function getJob(id: string): JobRecord | null {
|
||||
function getJob(id: string): JobRecord | null {
|
||||
try {
|
||||
return JSON.parse(fs.readFileSync(jobFile(id), "utf-8")) as JobRecord;
|
||||
} catch {
|
||||
@@ -56,10 +49,7 @@ export function getJob(id: string): JobRecord | null {
|
||||
}
|
||||
|
||||
/** Merge a partial patch into the record (identity fields are immutable). */
|
||||
export function updateJob(
|
||||
id: string,
|
||||
patch: Partial<Omit<JobRecord, "id" | "kind" | "chatId" | "toolCallId" | "createdAt">>,
|
||||
): JobRecord | null {
|
||||
function updateJob(id: string, patch: JobPatch): JobRecord | null {
|
||||
const record = getJob(id);
|
||||
if (!record) return null;
|
||||
const next: JobRecord = { ...record, ...patch, updatedAt: new Date().toISOString() };
|
||||
@@ -68,7 +58,7 @@ export function updateJob(
|
||||
}
|
||||
|
||||
/** All jobs for a chat, newest activity first. */
|
||||
export function listJobs(chatId: string): JobRecord[] {
|
||||
function listJobs(chatId: string): JobRecord[] {
|
||||
try {
|
||||
fs.mkdirSync(JOBS_DIR, { recursive: true });
|
||||
return fs
|
||||
@@ -83,6 +73,14 @@ export function listJobs(chatId: string): JobRecord[] {
|
||||
}
|
||||
|
||||
/** The job fulfilling a given tool part, if any (spawn-hook dedup). */
|
||||
export function findJobByToolCallId(chatId: string, toolCallId: string): JobRecord | null {
|
||||
function findJobByToolCallId(chatId: string, toolCallId: string): JobRecord | null {
|
||||
return listJobs(chatId).find((j) => j.toolCallId === toolCallId) ?? null;
|
||||
}
|
||||
|
||||
export const jobStore: JobStore = {
|
||||
createJob,
|
||||
getJob,
|
||||
updateJob,
|
||||
listJobs,
|
||||
findJobByToolCallId,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user