"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EvidenceCollector = void 0;
exports.extractHealingHistory = extractHealingHistory;
exports.extractTaskHistory = extractTaskHistory;
const node_fs_1 = require("node:fs");
const node_path_1 = require("node:path");
/**
 * Manages evidence collection and persistence
 */
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 = (0, node_path_1.join)(projectRoot, ".uso", "evidence");
        // Create directory if it doesn't exist
        (0, node_fs_1.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 = (0, node_path_1.join)(evidenceDir, filename);
        // Write evidence to file
        (0, node_fs_1.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();
    }
}
exports.EvidenceCollector = EvidenceCollector;
/**
 * Helper to extract healing events from lifecycle events
 */
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
 */
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