import { writeFileSync, mkdirSync } from "node:fs";
import { join } from "node:path";
/**
 * Manages evidence collection and persistence
 */
export class EvidenceCollector {
    records = [];
    /**
     * Record an execution result as evidence
     */
    record(result, events, durationMs) {
        const tasksSucceeded = result.attempts.filter((a) => a.status === "success").length;
        const evidence = {
            intentId: result.intentId,
            timestamp: new Date().toISOString(),
            intent: result.plan.intentKind,
            status: result.status,
            route: result.route,
            tasksAttempted: result.attempts.length,
            tasksSucceeded,
            attempts: result.attempts,
            events,
            duration_ms: durationMs,
        };
        this.records.push(evidence);
        return evidence;
    }
    /**
     * Get all recorded evidence
     */
    getRecords() {
        return [...this.records];
    }
    /**
     * Clear all evidence
     */
    clear() {
        this.records = [];
    }
    /**
     * Persist evidence to disk
     */
    persistToDisk(projectRoot, evidence) {
        const evidenceDir = join(projectRoot, ".uso", "evidence");
        // Create directory if it doesn't exist
        mkdirSync(evidenceDir, { recursive: true });
        // Generate filename with timestamp
        const timestamp = new Date()
            .toISOString()
            .replace(/[:.]/g, "-")
            .slice(0, -1);
        const filename = `${timestamp}-${evidence.intentId.slice(0, 8)}.json`;
        const filepath = join(evidenceDir, filename);
        // Write evidence to file
        writeFileSync(filepath, JSON.stringify(evidence, null, 2));
        return filepath;
    }
    /**
     * Generate a summary from evidence
     */
    summarize(evidence) {
        const successRate = evidence.tasksAttempted > 0
            ? Math.round((evidence.tasksSucceeded / evidence.tasksAttempted) * 100)
            : 0;
        return `
=== Execution Evidence ===
Intent: ${evidence.intent}
Status: ${evidence.status.toUpperCase()}
Route: ${evidence.route}
Duration: ${evidence.duration_ms}ms
Tasks: ${evidence.tasksSucceeded}/${evidence.tasksAttempted} succeeded (${successRate}%)
Attempts: ${evidence.attempts.length}
Events: ${evidence.events.length}
    `.trim();
    }
}
/**
 * Helper to extract healing events from lifecycle events
 */
export function extractHealingHistory(events) {
    return events
        .filter((e) => e.type === "healing-applied")
        .map((e) => {
        const decision = (e.data.decision ?? {});
        return {
            timestamp: e.timestamp,
            error: String(e.data.error || ""),
            decision: String(decision.level || "unknown"),
            appliedFix: String(decision.appliedFix || ""),
        };
    });
}
/**
 * Helper to extract task execution history
 */
export function extractTaskHistory(attempts) {
    return attempts.map((a) => ({
        taskId: a.taskId,
        attempt: a.attempt,
        status: a.status,
        exitCode: a.exitCode,
        command: a.command,
    }));
}
//# sourceMappingURL=evidence.js.map