import { randomUUID } from "node:crypto"; import { readFile, realpath } from "node:fs/promises"; import { resolve } from "node:path"; import { fingerprintSource } from "./checker.ts"; import { type LoadedSkill, parseSourceSkill, resolveLoadedSkill, type SourceSkill, type ValidatedSuite, validateTrustedSuite, } from "./contracts.ts"; import { createRunDirectory, persistCheckpoint, persistManifest, type RunManifest, } from "./run-state.ts"; type LoadedCommand = { name: string; source: string; sourceInfo?: { path?: string; scope?: string }; }; type FrozenModel = { provider: string; id: string; thinking: string }; export type SuiteRunOptions = { agentDir: string; cwd: string; sourcePath: string; suitePath: string; commands: readonly LoadedCommand[]; projectTrusted: boolean; sessionId: string; model: FrozenModel | undefined; authAvailable: boolean; }; export async function inspectSuiteRun(options: SuiteRunOptions): Promise<{ source: SourceSkill; sourceBytes: Buffer; sourcePath: string; loadedSkill: LoadedSkill; suite: ValidatedSuite; }> { if (!options.model) throw new Error( "a selected model is required before Overkill creates artifacts", ); if (!options.authAvailable) throw new Error("selected model authentication is unavailable"); const loadedSkill = await resolveLoadedSkill( options.sourcePath, options.commands, options.projectTrusted, options.cwd, ); const sourcePath = await realpath(resolve(options.cwd, options.sourcePath)); const sourceBytes = await readFile(sourcePath); const source = parseSourceSkill(sourceBytes); const suite = await validateTrustedSuite( options.suitePath, options.cwd, options.projectTrusted, ); return { source, sourceBytes, sourcePath, loadedSkill, suite }; } export async function prepareSuiteRun(options: SuiteRunOptions): Promise<{ directory: string; manifest: RunManifest; source: SourceSkill; sourcePath: string; loadedSkill: LoadedSkill; suite: ValidatedSuite; }> { const { source, sourceBytes, sourcePath, loadedSkill, suite } = await inspectSuiteRun(options); if (!options.model) throw new Error("selected model is unavailable"); const runId = randomUUID(); const manifest: RunManifest = { version: 1, runId, kind: "suite", source: { path: sourcePath, fingerprint: fingerprintSource(sourceBytes) }, suite: { path: suite.suitePath, fingerprint: suite.fingerprint ?? (() => { throw new Error("validated suite fingerprint is unavailable"); })(), }, model: options.model, sessionId: options.sessionId, linkEntryId: null, stage: "preflight", terminal: null, scores: { baseline: null, current: null, best: null, test: null }, calls: { target: 0, checker: 0, optimizer: 0, judge: 0 }, artifacts: { baselineSnapshot: "baseline-snapshot.md", report: null, staged: null, }, }; const directory = await createRunDirectory(options.agentDir, runId); await persistManifest(directory, manifest); await persistCheckpoint(directory, { version: 1, stage: "preflight", completed: 0, }); return { directory, manifest, source, sourcePath, loadedSkill, suite }; }