← back to Homesonspec

apps/workers/src/verify-cli.ts

82 lines

import { prisma } from "@homesonspec/database";
import { recomputeFreshness, dedupePass, refreshDue } from "./verify";

/**
 * Verification-engine CLI — the Stage-3 maintenance commands.
 *
 *   pnpm --filter @homesonspec/workers verify -- freshness
 *   pnpm --filter @homesonspec/workers verify -- dedupe            # dry-run
 *   pnpm --filter @homesonspec/workers verify -- dedupe --apply    # write
 *   pnpm --filter @homesonspec/workers verify -- refresh-due
 *   pnpm --filter @homesonspec/workers verify -- health
 *   pnpm --filter @homesonspec/workers verify -- all               # freshness + dedupe(dry) + due + health
 */
async function health() {
  const sources = await prisma.sourceRegistry.findMany({
    select: {
      key: true, name: true, health: true, active: true,
      consecutiveFailures: true, lastRunAt: true, lastSuccessAt: true,
    },
    orderBy: { key: "asc" },
  });
  return sources.map((s) => ({
    key: s.key,
    health: s.health,
    active: s.active,
    fails: s.consecutiveFailures,
    lastRun: s.lastRunAt?.toISOString() ?? null,
    lastOk: s.lastSuccessAt?.toISOString() ?? null,
  }));
}

async function main() {
  const [cmd, ...rest] = process.argv.slice(2).filter((a) => !a.startsWith("--"));
  const apply = process.argv.includes("--apply");
  const now = new Date();

  switch (cmd) {
    case "freshness": {
      console.log(JSON.stringify(await recomputeFreshness(now), null, 2));
      break;
    }
    case "dedupe": {
      console.log(JSON.stringify(await dedupePass({ apply }), null, 2));
      break;
    }
    case "refresh-due": {
      const due = await refreshDue(now);
      console.log(JSON.stringify({ due: due.map((d) => d.key), count: due.length }, null, 2));
      break;
    }
    case "health": {
      console.log(JSON.stringify(await health(), null, 2));
      break;
    }
    case "all":
    case undefined: {
      const freshness = await recomputeFreshness(now);
      const dedupe = await dedupePass({ apply: false });
      const due = await refreshDue(now);
      console.log(
        JSON.stringify(
          { freshness, dedupe, refreshDue: due.map((d) => d.key), sources: await health() },
          null,
          2,
        ),
      );
      break;
    }
    default:
      console.error(`Unknown command "${cmd}". Try: freshness | dedupe [--apply] | refresh-due | health | all`);
      process.exitCode = 1;
  }
  void rest;
}

main()
  .catch((error) => {
    console.error(error);
    process.exitCode = 1;
  })
  .finally(() => prisma.$disconnect());