import { appendAutopilotLogEntry } from '../../../storage/autopilot-log-store.js';
import { hashProjectPath } from '../../../storage/base-store.js';
async function handler(ctx) {
    const { projectPath, specId } = ctx;
    if (!projectPath) {
        return;
    }
    const start = Date.now();
    try {
        // Dynamic import keeps module graph lean — housekeeping-sweep is only needed on 'done'.
        const { runHousekeepingSweep } = await import('../../housekeeping/index.js');
        // Housekeeping also auto-triggers via SPEC-790 in github-release-handler.ts
        void runHousekeepingSweep({ projectPath, dryRun: false });
    }
    catch (err) {
        const error = err instanceof Error ? err.message : String(err);
        void appendAutopilotLogEntry(hashProjectPath(projectPath), {
            specId,
            hookName: 'housekeeping-on-done',
            result: 'fail',
            error,
            timestamp: new Date().toISOString(),
            durationMs: Date.now() - start,
        });
        // Do NOT re-throw — cascade must continue with remaining hooks
    }
}
export const housekeepingOnDoneHook = {
    id: 'housekeeping-on-done',
    triggers: ['done'],
    requiresProjectPath: true,
    label: 'Auto-run housekeeping_sweep on spec done — delete orphan stashes/branches (SPEC-782)',
    handler,
};
//# sourceMappingURL=housekeeping-on-done.hook.js.map