/** * Shared write pipeline for harness subagent submit tools. */ import { mkdir, readFile } from "node:fs/promises"; import { dirname, join, resolve } from "node:path"; import { parse as parseYaml } from "yaml"; import { validateAgainstHarnessSchema } from "./harness-schema-validate.js"; import { resolveGuardedRunDir } from "./harness-subagent-submit-path.js"; import { resolveArtifactRelPath, type SubmitToolSpec, } from "./harness-subagent-submit-registry.js"; import { stringifyYaml, writeYamlFile } from "./harness-yaml.js"; import { type ApplyDebateLaneResult, applyDebateLaneFromDoc, } from "./plan-debate-lane.js"; export interface SubmitPipelineResult { ok: boolean; artifact_path?: string; validation_errors?: string[]; lane_result?: ApplyDebateLaneResult; human_required?: boolean; /** Artifact already passed gate with equivalent content — do not resubmit. */ idempotent?: boolean; } async function artifactBytesMatch( absPath: string, document: Record, ): Promise { try { const existingDoc = await readYamlObject(absPath); if (!existingDoc) return false; return stringifyYaml(existingDoc).trim() === stringifyYaml(document).trim(); } catch { return false; } } async function readYamlObject( absPath: string, ): Promise | null> { try { const raw = await readFile(absPath, "utf-8"); const doc = parseYaml(raw) as Record; if (!doc || typeof doc !== "object" || Array.isArray(doc)) return null; return doc; } catch { return null; } } export async function loadSubmitDocument(opts: { projectRoot: string; runDir: string; document?: Record; source_path?: string; }): Promise< | { ok: true; document: Record } | { ok: false; validation_errors: string[] } > { if (opts.document && typeof opts.document === "object") { return { ok: true, document: opts.document }; } const rel = opts.source_path?.trim(); if (!rel) { return { ok: false, validation_errors: ["submit_* requires document or source_path"], }; } const abs = resolve(opts.runDir, rel.replace(/^\//, "")); if (!abs.startsWith(resolve(opts.runDir))) { return { ok: false, validation_errors: [ "source_path must stay under the active run directory", ], }; } try { const raw = await readFile(abs, "utf-8"); const doc = parseYaml(raw) as Record; if (!doc || typeof doc !== "object") { return { ok: false, validation_errors: ["source_path did not parse to an object"], }; } return { ok: true, document: doc }; } catch (e) { const msg = e instanceof Error ? e.message : String(e); return { ok: false, validation_errors: [`source_path read failed: ${msg}`], }; } } export async function executeSubmitPipeline(opts: { projectRoot: string; specsDir: string; spec: SubmitToolSpec; agentId: string; document: Record; runId: string; runDirEnv?: string; }): Promise { const runResolved = await resolveGuardedRunDir({ projectRoot: opts.projectRoot, runId: opts.runId, runDirEnv: opts.runDirEnv, }); if (!runResolved.ok) { return { ok: false, validation_errors: [runResolved.error] }; } const validation = await validateAgainstHarnessSchema( opts.specsDir, opts.spec.schemaFile, opts.document, ); if (!validation.ok) { return { ok: false, validation_errors: validation.errors }; } const relPath = resolveArtifactRelPath(opts.spec, opts.document); const absPath = join(runResolved.runDir, relPath); const existingDoc = await readYamlObject(absPath); if (existingDoc) { const existingValidation = await validateAgainstHarnessSchema( opts.specsDir, opts.spec.schemaFile, existingDoc, ); if ( existingValidation.ok && (await artifactBytesMatch(absPath, opts.document)) ) { let laneResult: ApplyDebateLaneResult | undefined; if (opts.spec.debateLane) { laneResult = await applyDebateLaneFromDoc({ runDir: runResolved.runDir, lane: opts.spec.debateLane, doc: opts.document, skipArtifactWrite: true, }); if (!laneResult.ok) { return { ok: false, artifact_path: relPath, validation_errors: [ ...laneResult.errors, "Artifact already on disk with the same content; fix messenger/lane fields above and resubmit once (do not loop identical submits).", ], lane_result: laneResult, }; } } return { ok: true, artifact_path: relPath, lane_result: laneResult, human_required: opts.spec.humanRequired === true, idempotent: true, }; } } if (opts.spec.debateLane) { const laneResult = await applyDebateLaneFromDoc({ runDir: runResolved.runDir, lane: opts.spec.debateLane, doc: opts.document, }); if (!laneResult.ok) { return { ok: false, artifact_path: relPath, validation_errors: laneResult.errors, lane_result: laneResult, }; } return { ok: true, artifact_path: relPath, lane_result: laneResult, human_required: opts.spec.humanRequired === true, }; } await mkdir(dirname(absPath), { recursive: true }); await writeYamlFile(absPath, opts.document); if (opts.spec.toolName === "submit_executor_handoff") { const rollback = opts.document.rollback_refs; if (rollback && typeof rollback === "object" && !Array.isArray(rollback)) { const rollbackPath = join( runResolved.runDir, "artifacts", "executor-rollback.yaml", ); await mkdir(dirname(rollbackPath), { recursive: true }); await writeYamlFile(rollbackPath, { schema_version: "1.0.0", ...(rollback as Record), }); } } return { ok: true, artifact_path: relPath, human_required: opts.spec.humanRequired === true, }; }