/**
 * Assumption recording for session audit trail.
 *
 * @task T4782
 * @epic T4654
 */
import { randomBytes } from 'node:crypto';
import { appendFileSync, existsSync, mkdirSync } from 'node:fs';
import { join } from 'node:path';
import { ExitCode } from '@cleocode/contracts';
import { CleoError } from '../errors.js';
import { getAccessor } from '../store/data-accessor.js';
/**
 * Record an assumption made during a session.
 * Appends to .cleo/audit/assumptions.jsonl (creates dir if needed).
 * Throws if required params are missing or invalid.
 */
export async function recordAssumption(projectRoot, params) {
    if (!params?.assumption) {
        throw new CleoError(ExitCode.INVALID_INPUT, 'assumption is required');
    }
    if (!params?.confidence || !['high', 'medium', 'low'].includes(params.confidence)) {
        throw new CleoError(ExitCode.INVALID_INPUT, 'confidence must be one of: high, medium, low');
    }
    const accessor = await getAccessor(projectRoot);
    const activeSession = await accessor.getActiveSession();
    const sessionId = params.sessionId || activeSession?.id || 'default';
    const id = `asm-${randomBytes(8).toString('hex')}`;
    const now = new Date().toISOString();
    const record = {
        id,
        sessionId,
        taskId: params.taskId || null,
        assumption: params.assumption,
        confidence: params.confidence,
        validatedAt: null,
        timestamp: now,
    };
    const auditDir = join(projectRoot, '.cleo', 'audit');
    if (!existsSync(auditDir)) {
        mkdirSync(auditDir, { recursive: true });
    }
    const assumptionsPath = join(auditDir, 'assumptions.jsonl');
    appendFileSync(assumptionsPath, JSON.stringify(record) + '\n', 'utf-8');
    return {
        id,
        sessionId,
        taskId: params.taskId || null,
        assumption: params.assumption,
        confidence: params.confidence,
        timestamp: now,
    };
}
//# sourceMappingURL=assumptions.js.map