import { join } from "path";
import { MemoryStore } from "../memory/memory-store.js";
export async function saveEnsembleDecision(goal, runState, ensemble, root, meta) {
    const memoryStore = new MemoryStore(join(root, ".omk", "memory"), {
        projectRoot: root,
        source: "ensemble-goal",
    });
    const timestamp = new Date().toISOString();
    const payload = {
        goalId: goal.goalId,
        runId: runState.runId,
        timestamp,
        action: ensemble.action,
        confidence: ensemble.confidence,
        shouldContinue: ensemble.shouldContinue,
        candidateVotes: ensemble.candidateVotes,
        rationale: ensemble.rationale,
        nextPrompt: ensemble.nextPrompt,
        divergence: meta?.divergence ?? ensemble.divergence,
        confidenceTrend: meta?.confidenceTrend,
    };
    const content = JSON.stringify(payload, null, 2);
    const path = `ensemble/${goal.goalId}/${runState.runId}/${timestamp}.json`;
    try {
        await memoryStore.write(path, content);
    }
    catch (err) {
        // Non-fatal: log but do not block goal evaluation
        console.warn(`[ensemble-memory] Failed to save ensemble decision: ${err instanceof Error ? err.message : String(err)}`);
    }
}
export async function recallRecentEnsembleDecisions(goalId, root, limit = 5) {
    const memoryStore = new MemoryStore(join(root, ".omk", "memory"), {
        projectRoot: root,
        source: "ensemble-goal",
    });
    try {
        const results = await memoryStore.search(`ensemble ${goalId}`, limit);
        return results
            .map((r) => {
            try {
                return JSON.parse(r.content);
            }
            catch {
                return null;
            }
        })
            .filter((d) => d !== null)
            .sort((a, b) => b.timestamp.localeCompare(a.timestamp))
            .slice(0, limit);
    }
    catch {
        return [];
    }
}
