/**
 * EvidenceRecorder — persists AttemptRecord + EvidenceResult to JSONL files.
 *
 * Storage structure:
 *   .omk/runs/<runId>/attempts.jsonl   — attempt records
 *   .omk/runs/<runId>/evidence.jsonl   — evidence results (per gate)
 *   .omk/runs/<runId>/context-capsules/<nodeId>-<attemptId>.json
 */
import { appendFileSync, mkdirSync, writeFileSync, existsSync } from "fs";
import { join, dirname } from "path";
import { redactSecrets } from "../orchestration/state-persister.js";
export function createEvidenceRecorder(options) {
    const runsDir = options?.runsDir ?? ".omk/runs";
    function ensureDir(dir) {
        if (!existsSync(dir)) {
            mkdirSync(dir, { recursive: true });
        }
    }
    function appendJsonl(filePath, data) {
        ensureDir(dirname(filePath));
        appendFileSync(filePath, JSON.stringify(redactSecrets(data)) + "\n", "utf-8");
    }
    function writeJson(filePath, data) {
        ensureDir(dirname(filePath));
        writeFileSync(filePath, JSON.stringify(redactSecrets(data), null, 2), "utf-8");
    }
    function recordAttempt(record) {
        const dir = join(runsDir, record.runId);
        appendJsonl(join(dir, "attempts.jsonl"), record);
    }
    function ensureRunDir(runId) {
        const dir = join(runsDir, runId);
        ensureDir(dir);
        ensureDir(join(dir, "context-capsules"));
        ensureDir(join(dir, "artifacts"));
        ensureDir(join(dir, "reports"));
    }
    function recordEvidence(runId, nodeId, attemptId, results) {
        const dir = join(runsDir, runId);
        for (const result of results) {
            appendJsonl(join(dir, "evidence.jsonl"), {
                nodeId,
                attemptId,
                ...result,
                timestamp: new Date().toISOString(),
            });
        }
    }
    function saveContextSnapshot(runId, nodeId, attemptId, capsule) {
        const dir = join(runsDir, runId, "context-capsules");
        const filePath = join(dir, `${nodeId}-${attemptId}.json`);
        writeJson(filePath, capsule);
    }
    return { ensureRunDir, recordAttempt, recordEvidence, saveContextSnapshot };
}
