2026-08-31 20:00:12 +03:00
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2026-08-22 09:36:15 +03:00
|
|
|
import * as fs from "node:fs";
|
|
|
|
|
import * as path from "node:path";
|
|
|
|
|
import { PATHS } from "../../config.ts";
|
2026-08-31 20:00:12 +03:00
|
|
|
import type { JobStore } from "../core/ports.ts";
|
|
|
|
|
import type { CreateJobInit, JobPatch } from "../core/ports.ts";
|
|
|
|
|
import type { JobRecord } from "../core/contracts.ts";
|
2026-08-22 09:36:15 +03:00
|
|
|
|
|
|
|
|
/** Background jobs (data/jobs). Not in config.ts PATHS (orchestrator-owned). */
|
|
|
|
|
const JOBS_DIR = path.join(PATHS.data, "jobs");
|
|
|
|
|
|
|
|
|
|
function jobFile(id: string): string {
|
|
|
|
|
if (!/^[A-Za-z0-9_-]+$/.test(id)) throw new Error(`Invalid job id: ${id}`);
|
|
|
|
|
return path.join(JOBS_DIR, `${id}.json`);
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-31 20:00:12 +03:00
|
|
|
function createJob(init: CreateJobInit): JobRecord {
|
2026-08-22 09:36:15 +03:00
|
|
|
const now = new Date().toISOString();
|
|
|
|
|
const record: JobRecord = {
|
|
|
|
|
id: `job_${Date.now().toString(36)}_${crypto.randomUUID().slice(0, 8)}`,
|
|
|
|
|
kind: init.kind,
|
|
|
|
|
chatId: init.chatId,
|
|
|
|
|
toolCallId: init.toolCallId,
|
|
|
|
|
status: "running",
|
|
|
|
|
activity: init.activity ?? "starting",
|
|
|
|
|
createdAt: now,
|
|
|
|
|
updatedAt: now,
|
|
|
|
|
};
|
|
|
|
|
fs.mkdirSync(JOBS_DIR, { recursive: true });
|
|
|
|
|
fs.writeFileSync(jobFile(record.id), JSON.stringify(record, null, "\t"), "utf-8");
|
|
|
|
|
return record;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-31 20:00:12 +03:00
|
|
|
function getJob(id: string): JobRecord | null {
|
2026-08-22 09:36:15 +03:00
|
|
|
try {
|
|
|
|
|
return JSON.parse(fs.readFileSync(jobFile(id), "utf-8")) as JobRecord;
|
|
|
|
|
} catch {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-11 03:36:09 +03:00
|
|
|
/** Merge a partial patch into the record (identity fields are immutable).
|
|
|
|
|
* An explicit patch.updatedAt is honored (tests pin deterministic order);
|
|
|
|
|
* otherwise the merge bumps it to now. */
|
2026-08-31 20:00:12 +03:00
|
|
|
function updateJob(id: string, patch: JobPatch): JobRecord | null {
|
2026-08-22 09:36:15 +03:00
|
|
|
const record = getJob(id);
|
|
|
|
|
if (!record) return null;
|
2026-09-11 03:36:09 +03:00
|
|
|
const next: JobRecord = { ...record, ...patch, updatedAt: patch.updatedAt ?? new Date().toISOString() };
|
2026-08-22 09:36:15 +03:00
|
|
|
fs.writeFileSync(jobFile(id), JSON.stringify(next, null, "\t"), "utf-8");
|
|
|
|
|
return next;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** All jobs for a chat, newest activity first. */
|
2026-08-31 20:00:12 +03:00
|
|
|
function listJobs(chatId: string): JobRecord[] {
|
2026-08-22 09:36:15 +03:00
|
|
|
try {
|
|
|
|
|
fs.mkdirSync(JOBS_DIR, { recursive: true });
|
|
|
|
|
return fs
|
|
|
|
|
.readdirSync(JOBS_DIR)
|
|
|
|
|
.filter((f) => f.endsWith(".json"))
|
|
|
|
|
.map((f) => getJob(f.slice(0, -5)))
|
|
|
|
|
.filter((j): j is JobRecord => j !== null && j.chatId === chatId)
|
|
|
|
|
.sort((a, b) => b.updatedAt.localeCompare(a.updatedAt));
|
|
|
|
|
} catch {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** The job fulfilling a given tool part, if any (spawn-hook dedup). */
|
2026-08-31 20:00:12 +03:00
|
|
|
function findJobByToolCallId(chatId: string, toolCallId: string): JobRecord | null {
|
2026-08-22 09:36:15 +03:00
|
|
|
return listJobs(chatId).find((j) => j.toolCallId === toolCallId) ?? null;
|
|
|
|
|
}
|
2026-08-31 20:00:12 +03:00
|
|
|
|
|
|
|
|
export const jobStore: JobStore = {
|
|
|
|
|
createJob,
|
|
|
|
|
getJob,
|
|
|
|
|
updateJob,
|
|
|
|
|
listJobs,
|
|
|
|
|
findJobByToolCallId,
|
|
|
|
|
};
|