// engine/housekeeping/ephemeral-artifacts-cleaner.ts — SPEC-762
// Removes obsolete per-spec sidecar planning artifacts (risk-register.md,
// implementation-brief.md, prompt.md). Current Planu specs keep durable content
// in spec.md; these files are no longer generated or preserved.
import { access, readdir, unlink } from 'node:fs/promises';
import { join } from 'node:path';
// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
const EPHEMERAL_FILENAMES = ['risk-register.md', 'implementation-brief.md', 'prompt.md'];
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
/** Return true when the directory name looks like a Planu spec folder. */
function isSpecDir(name) {
    return /^SPEC-\d+/.test(name);
}
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
/**
 * Scan all planu/specs/SPEC-* directories and delete (or report) obsolete
 * sidecar artifact files.
 */
export async function cleanEphemeralArtifacts(input) {
    const specsRoot = join(input.projectPath, 'planu', 'specs');
    const result = {
        found: 0,
        deleted: 0,
        wouldDelete: [],
        errors: [],
    };
    let specDirs;
    try {
        const entries = await readdir(specsRoot, { withFileTypes: true });
        specDirs = entries
            .filter((e) => e.isDirectory() && isSpecDir(e.name))
            .map((e) => join(specsRoot, e.name));
    }
    catch {
        return result; // planu/specs/ doesn't exist — nothing to clean
    }
    for (const specDir of specDirs) {
        for (const filename of EPHEMERAL_FILENAMES) {
            const filePath = join(specDir, filename);
            try {
                await access(filePath); // probe existence
            }
            catch {
                continue; // file doesn't exist — skip
            }
            result.found++;
            if (input.dryRun) {
                result.wouldDelete.push(filePath);
                continue;
            }
            try {
                await unlink(filePath);
                result.deleted++;
            }
            catch (err) {
                result.errors.push(`Failed to delete ${filePath}: ${err instanceof Error ? err.message : String(err)}`);
            }
        }
    }
    return result;
}
//# sourceMappingURL=ephemeral-artifacts-cleaner.js.map