import path from 'node:path';
import { appendJsonlBounded, nowIso, writeJsonAtomic } from '../fsx.js';
import { missionDir } from '../mission.js';
export const MAD_DB_LEDGER_EVENT_SCHEMA = 'sks.mad-db-ledger-event.v1';
export async function appendMadDbLedgerEvent(root, missionId, event) {
    const row = {
        schema: MAD_DB_LEDGER_EVENT_SCHEMA,
        ts: nowIso(),
        mission_id: missionId,
        ...event
    };
    const dir = missionDir(root, missionId);
    await appendJsonlBounded(path.join(dir, 'mad-db-ledger.jsonl'), row);
    await writeJsonAtomic(path.join(dir, 'mad-db-ledger.latest.json'), row).catch(() => undefined);
    return row;
}
export async function appendMadDbOperationLifecycle(root, missionId, input) {
    return appendMadDbLedgerEvent(root, missionId, {
        type: input.type,
        operation_id: input.operationId,
        cycle_id: input.cycleId || null,
        mcp_server: input.mcpServer || null,
        tool_name: input.toolName || null,
        sql_hash: input.sqlHash || null,
        destructive: input.destructive === true,
        result_status: input.resultStatus || 'unknown_pending_tool_result',
        row_count: input.rowCount ?? null,
        error: input.error || null
    });
}
//# sourceMappingURL=mad-db-ledger.js.map