/**
 * Persists the last-scan timestamp for the skill-proposal background runner so
 * the daily cooldown survives across process restarts. A missing or
 * unparseable state file is treated as "never scanned" — the next session
 * start will trigger a scan.
 */
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
const DAILY_SCAN_INTERVAL_MS = 24 * 60 * 60 * 1000;
/** Return the on-disk path of the scan-state JSON file. */
export function scanStatePath(roots) {
    return join(roots.agentDir, 'skill-proposals.state.json');
}
/** Load the persisted scan state, returning an empty record if the file is missing or corrupt. */
export function readSkillProposalScanState(roots) {
    try {
        const parsed = JSON.parse(readFileSync(scanStatePath(roots), 'utf-8'));
        return typeof parsed === 'object' && parsed !== null ? parsed : {};
    }
    catch {
        return {};
    }
}
/** Atomically persist the latest scan state JSON to disk. */
export function writeSkillProposalScanState(roots, state) {
    const path = scanStatePath(roots);
    mkdirSync(dirname(path), { recursive: true });
    writeFileSync(path, `${JSON.stringify(state, null, 2)}\n`, 'utf-8');
}
/** True if no prior scan exists or the daily cooldown has elapsed since the last scan. */
export function shouldRunDailySkillProposalScan(state, now = new Date()) {
    if (!state.lastScanAt)
        return true;
    const lastScanMs = Date.parse(state.lastScanAt);
    if (!Number.isFinite(lastScanMs))
        return true;
    return now.getTime() - lastScanMs >= DAILY_SCAN_INTERVAL_MS;
}
//# sourceMappingURL=scan-state.js.map