/**
 * Startup recovery for SSH audit rows in 'pending' state.
 * If the orchestrator crashed while a remote SSH operation was in flight,
 * the pending row remains. Reconciler marks it 'aborted' with a stale reason
 * so operators see "execution may have completed remotely; local outcome unknown".
 *
 * Plan: docs/superpowers/plans/2026-05-12-ssh-tool-integration.md (Phase 1).
 */
import type Database from 'better-sqlite3';
import { createAuditRepo } from './audit-repo.js';

export interface ReconcileResult {
  reconciledCount: number;
  ids: number[];
}

export function reconcileStaleSshAudit(db: Database.Database): ReconcileResult {
  const audit = createAuditRepo(db);
  const stale = audit.listPending();
  const ids: number[] = [];
  for (const row of stale) {
    audit.complete(row.id, 'aborted', {
      stale_reason: 'orchestrator_restart',
      detail: 'startup recovery marked pending row aborted; remote execution outcome is unknown',
    });
    ids.push(row.id);
  }
  return { reconciledCount: ids.length, ids };
}
