/**
 * Nexus Prime - Darwin Journal
 *
 * Structured improvement log for the Darwin Loop self-improvement orchestrator.
 * Maintains state of hypotheses, validation gates, and outcomes.
 *
 * Phase: 8F (Darwin Loop)
 */
import * as path from 'path';
import * as fs from 'fs';
import { randomUUID } from 'crypto';
import { resolveNexusStateDir } from './runtime-registry.js';
// ─────────────────────────────────────────────────────────────────────────────
// Darwin Journal Manager
// ─────────────────────────────────────────────────────────────────────────────
export class DarwinJournal {
    journalPath;
    constructor() {
        const nexusDir = resolveNexusStateDir();
        this.journalPath = path.join(nexusDir, 'darwin-journal.json');
        if (!fs.existsSync(this.journalPath)) {
            fs.writeFileSync(this.journalPath, JSON.stringify([], null, 2), 'utf-8');
        }
    }
    /** Load all recorded cycles */
    loadCycles() {
        try {
            const raw = fs.readFileSync(this.journalPath, 'utf-8');
            return JSON.parse(raw);
        }
        catch {
            return [];
        }
    }
    /** Save cycles back to disk */
    saveCycles(cycles) {
        fs.writeFileSync(this.journalPath, JSON.stringify(cycles, null, 2), 'utf-8');
    }
    /** Create a new cycle proposal */
    propose(hypothesis, targetFile, approach) {
        const cycle = {
            id: `darwin_${randomUUID()}`,
            hypothesis,
            targetFile,
            approach,
            worktreeBranch: `darwin-${Date.now()}`,
            diffGenerated: '',
            buildPassed: false,
            testsPassed: false,
            metricsBefore: {},
            metricsAfter: {},
            guardrailResult: { passed: false, score: 0 },
            outcome: 'pending',
            learnings: [],
            timestamp: Date.now()
        };
        const cycles = this.loadCycles();
        cycles.push(cycle);
        this.saveCycles(cycles);
        return cycle;
    }
    /** Update an existing cycle */
    updateCycle(id, updates) {
        const cycles = this.loadCycles();
        const idx = cycles.findIndex(c => c.id === id);
        if (idx === -1)
            return null;
        cycles[idx] = { ...cycles[idx], ...updates };
        this.saveCycles(cycles);
        return cycles[idx];
    }
    /** Get a specific cycle by ID */
    getCycle(id) {
        return this.loadCycles().find(c => c.id === id) || null;
    }
    /** Get pending cycles requiring review */
    getPending() {
        return this.loadCycles().filter(c => c.outcome === 'pending');
    }
}
