/**
 * definition-io.ts — Read frozen DEFINITION.yaml from a run directory.
 *
 * Extracted from custom-workflow-engine.ts to break the circular dependency
 * between context-injector.ts and custom-workflow-engine.ts.
 */
import { readFileSync } from "node:fs";
import { join } from "node:path";
import { parse } from "yaml";
import { getErrorMessage } from "./error-utils.js";
/** Read and parse the frozen DEFINITION.yaml from a run directory. */
export function readFrozenDefinition(runDir) {
	const defPath = join(runDir, "DEFINITION.yaml");
	try {
		const raw = readFileSync(defPath, "utf-8");
		return parse(raw, { schema: "core" });
	} catch (err) {
		const message = getErrorMessage(err);
		const wrapped = new Error(
			`Failed to read/parse DEFINITION.yaml at ${defPath}: ${message}`,
			{ cause: err },
		);
		// Forward errno code so callers that check e.code (e.g. ENOENT) still work.
		if (err && typeof err === "object" && "code" in err) {
			wrapped.code = err.code;
		}
		throw wrapped;
	}
}
