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
-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