@reddoorla/maintenance 0.98.0 → 0.98.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli/bin.js CHANGED
@@ -446,7 +446,7 @@ cli.command(
446
446
  "--by <who>",
447
447
  "replay-deadletters --abandon: who decided (default: OPERATOR_EMAIL, else 'operator')."
448
448
  ).action(
449
- async (action, opts) => runOrExit(async () => (await import("../db-7XYTBIPH.js")).runDbCommand(action, opts), opts)
449
+ async (action, opts) => runOrExit(async () => (await import("../db-SDNI2F62.js")).runDbCommand(action, opts), opts)
450
450
  );
451
451
  cli.command(
452
452
  "submissions <action>",
@@ -233,7 +233,7 @@ async function runDbCommand(action, opts, deps = {}) {
233
233
  const cfg = opts.url ? { url: opts.url } : readDbConfig();
234
234
  const { createClient } = await import("@libsql/client");
235
235
  const client = createClient(cfg.url === ":memory:" ? { url: ":memory:" } : cfg);
236
- const { dumpDatabase } = await import("./dump-QK7LNOGC.js");
236
+ const { dumpDatabase } = await import("./dump-WVCFUOL3.js");
237
237
  const sql = await dumpDatabase(
238
238
  {
239
239
  execute: async (q) => {
@@ -241,7 +241,18 @@ async function runDbCommand(action, opts, deps = {}) {
241
241
  return { columns: r.columns, rows: r.rows };
242
242
  }
243
243
  },
244
- (/* @__PURE__ */ new Date()).toISOString()
244
+ // A FUNCTION, not a string: re-evaluated per attempt, so a dump that
245
+ // succeeded on attempt 3 is stamped with attempt 3's time instead of
246
+ // carrying the timestamp of two dumps that were discarded.
247
+ () => (/* @__PURE__ */ new Date()).toISOString(),
248
+ {
249
+ // MED-12: a torn snapshot is re-taken, not shipped. Report each discard
250
+ // on STDERR — stdout is the dump itself, and a diagnostic written there
251
+ // would land inside the SQL (the `pnpm exec` trap, one layer down).
252
+ onTorn: (attempt, moved) => console.error(
253
+ `[db dump] attempt ${attempt} discarded: the database changed while it was being read (${moved.join("; ")}). Re-taking the dump.`
254
+ )
255
+ }
245
256
  );
246
257
  return { output: sql, code: 0 };
247
258
  }
@@ -254,7 +265,7 @@ async function runDbCommand(action, opts, deps = {}) {
254
265
  code: 1
255
266
  };
256
267
  }
257
- const { parseDumpManifest, requiresAuthToken } = await import("./dump-QK7LNOGC.js");
268
+ const { parseDumpManifest, requiresAuthToken } = await import("./dump-WVCFUOL3.js");
258
269
  const authToken = deps.restoreAuthToken ?? process.env.TURSO_RESTORE_AUTH_TOKEN ?? "";
259
270
  if (requiresAuthToken(opts.url) && !authToken) {
260
271
  return { output: "RESTORE refused=auth-token-absent", code: 1 };
@@ -272,7 +283,7 @@ async function runDbCommand(action, opts, deps = {}) {
272
283
  return { output: "RESTORE refused=target-not-empty", code: 1 };
273
284
  }
274
285
  await target.executeMultiple(sql);
275
- const { tableCounts, headerImageBytes } = await import("./dump-QK7LNOGC.js");
286
+ const { tableCounts, headerImageBytes } = await import("./dump-WVCFUOL3.js");
276
287
  const exec = {
277
288
  execute: async (q) => {
278
289
  const r = await target.execute(q);
@@ -332,7 +343,7 @@ async function runDbCommand(action, opts, deps = {}) {
332
343
  } catch (err) {
333
344
  return { output: `DUMP_VERIFY loaded=false error=${String(err)}`, code: 1 };
334
345
  }
335
- const { tableCounts, headerImageBytes, parseDumpManifest } = await import("./dump-QK7LNOGC.js");
346
+ const { tableCounts, headerImageBytes, parseDumpManifest } = await import("./dump-WVCFUOL3.js");
336
347
  const manifest = parseDumpManifest(sql);
337
348
  if (!manifest) {
338
349
  return {
@@ -382,4 +393,4 @@ export {
382
393
  freezeGuardsDbWrite,
383
394
  runDbCommand
384
395
  };
385
- //# sourceMappingURL=db-7XYTBIPH.js.map
396
+ //# sourceMappingURL=db-SDNI2F62.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/cli/commands/db.ts"],"sourcesContent":["export type DbCommandOptions = {\n /** Override the libSQL url (tests use \":memory:\"); otherwise read from env. */\n url?: string;\n /** verify-dump: path to the dump file to load into a scratch engine. */\n file?: string;\n /** usage: org slug override; defaults to TURSO_ORG, else discovered. */\n org?: string;\n /** import-airtable / sync: run despite the freeze — a deliberate\n * rollback-window converge from the frozen Airtable shadow. */\n force?: boolean;\n /** replay-deadletters: abandon this slug's queued leads (or one `dl_…` row id)\n * as resolved-by-decision instead of replaying them (#786). */\n abandon?: string;\n /** replay-deadletters --abandon: why. Required — an undocumented write-off of\n * a client's leads is the thing this is meant to stop being necessary. */\n reason?: string;\n /** replay-deadletters --abandon: who decided. Defaults to OPERATOR_EMAIL. */\n by?: string;\n cwd?: string;\n verbose?: boolean;\n};\n\n/** Injected seams — deliberately NOT part of DbCommandOptions, which is the set\n * of things a shell can type (a registration gate asserts exactly that). The\n * platform token lives here rather than on a flag because a secret passed on\n * argv is readable from `ps`. */\nexport type DbCommandDeps = {\n /** Tests pass \"\" to exercise the unconfigured path, so the implementation\n * must use `?? env` and never `|| env`. */\n platformToken?: string;\n fetchImpl?: (url: string, init?: RequestInit) => Promise<Response>;\n now?: Date;\n /** restore: auth token for the TARGET database; defaults to\n * TURSO_RESTORE_AUTH_TOKEN. Deliberately does NOT fall back to the ambient\n * TURSO_AUTH_TOKEN — that one belongs to production, and inheriting it would\n * undo the whole point of making --url explicit. */\n restoreAuthToken?: string;\n};\n\n/** #643 (the freeze): the scheduled import retired with the flip, but the\n * MANUAL import survives as the rollback-window converge tool — and run out of\n * habit it would overwrite authoritative Turso rows with the frozen Airtable\n * archive, including any post-flip write whose best-effort shadow was\n * swallowed. So the writing actions refuse under the freeze unless the\n * operator says `--force`. `parity` stays unguarded: it only compares, and\n * \"did the shadow drift?\" is exactly the rollback-window question.\n *\n * Pure and exported so the test injects BOTH switch states; `runDbCommand`\n * passes the shipped constant. Returns the refusal, or null to proceed. */\nexport function freezeGuardsDbWrite(\n action: string,\n force: boolean,\n authoritative: boolean,\n): { output: string; code: number } | null {\n if (!authoritative) return null;\n if (action !== \"import-airtable\" && action !== \"sync\") return null;\n if (force) return null;\n return {\n output:\n `db ${action} refused: TURSO_IS_AUTHORITATIVE is on (the freeze, 2026-08-31). ` +\n `An import now OVERWRITES authoritative Turso rows with the frozen Airtable ` +\n `archive. Pass --force only for a deliberate rollback-window converge.`,\n code: 1,\n };\n}\n\n/** `db <action>` — migrate | replay-deadletters | import-airtable | parity | sync | dump | verify-dump. The db layer is imported\n * dynamically so a non-db CLI invocation (and `--help`) never loads\n * @libsql/client. Config is resolved inside each branch so an unknown action\n * returns without needing any Turso env. */\nexport async function runDbCommand(\n action: string,\n opts: DbCommandOptions,\n deps: DbCommandDeps = {},\n): Promise<{ output: string; code: number }> {\n if (action === \"migrate\") {\n const { readDbConfig } = await import(\"../../db/client.js\");\n const cfg = opts.url ? { url: opts.url } : readDbConfig();\n const { runMigrations } = await import(\"../../db/migrate.js\");\n const { createClient } = await import(\"@libsql/client\");\n const client = createClient(cfg.url === \":memory:\" ? { url: \":memory:\" } : cfg);\n const ran = await runMigrations(client);\n return {\n output: ran.length ? `Applied migrations: ${ran.join(\", \")}` : \"Already up to date.\",\n code: 0,\n };\n }\n\n // Re-run every lead that dead-lettered during a site-lookup outage (#539\n // Phase 0) through the normal ingest pipeline. Exit 1 while any row is STILL\n // owed — still failing, re-ingested but unmarked (MED-10b), or undecodable\n // (MED-10c). The operator should not read the run as \"all leads landed\".\n // Zero rows is a clean 0: nothing owed.\n if (action === \"replay-deadletters\") {\n const { readDbConfig, openDb } = await import(\"../../db/client.js\");\n // #786. `--abandon` is the escape hatch for the queue #785 deliberately\n // stopped draining: a slug that is genuinely dead but still deployed keeps\n // dead-lettering leads, holds this command at exit 1, and leaves a standing\n // CRITICAL cockpit item. Validated BEFORE opening any store, so a missing\n // reason refuses without needing Turso creds.\n if (opts.abandon !== undefined) {\n const reason = (opts.reason ?? \"\").trim();\n if (reason === \"\") {\n return {\n output:\n 'db replay-deadletters --abandon refused: pass --reason \"…\". Abandoning writes ' +\n \"off a client's captured leads, and the record of WHY has to outlive the decision.\",\n code: 1,\n };\n }\n const by = opts.by ?? process.env.OPERATOR_EMAIL ?? \"operator\";\n const db = await openDb(opts.url ? { url: opts.url } : readDbConfig());\n const { abandonDeadLetters } = await import(\"../../db/deadletter.js\");\n // Row ids are minted `dl_…` (newDeadLetterId), and a site slug never is —\n // so the one argument can name either without a second flag.\n const target = opts.abandon.startsWith(\"dl_\") ? { id: opts.abandon } : { slug: opts.abandon };\n const ids = await abandonDeadLetters(db, { ...target, by, reason, now: new Date() });\n const lines = [\n ...ids.map((id) => `abandoned ${id}`),\n `DEADLETTER_ABANDONED target=${opts.abandon} rows=${ids.length} by=${by}`,\n ];\n return { output: lines.join(\"\\n\"), code: 0 };\n }\n const db = await openDb(opts.url ? { url: opts.url } : readDbConfig());\n const { openBase, readAirtableConfig } = await import(\"../../reports/airtable/client.js\");\n const { getWebsiteBySlug } = await import(\"../../reports/airtable/websites.js\");\n const { getSiteBySlug } = await import(\"../../db/fleet-state.js\");\n const { makeLazySiteLookup } = await import(\"../../forms/site-lookup.js\");\n // #645. Recovery now resolves sites through the SAME lookup the live ingest\n // path uses. It used to call the Airtable `getWebsiteBySlug` directly, so\n // post-#643 the two disagreed about what the fleet is: a site created since\n // the freeze was invisible to the replay, and a row only Airtable still held\n // would have attached a recovered lead to a site the system no longer\n // believes in. `openBase` is passed UNCALLED — under the freeze no Airtable\n // credential is read at all, where before a missing PAT refused the whole\n // replay (`readAirtableConfig()` throws) with real leads sitting in the queue.\n const lookupSite = makeLazySiteLookup({\n fromDb: (s) => getSiteBySlug(db, s),\n openAirtable: () => openBase(readAirtableConfig()),\n fromAirtable: (base, s) => getWebsiteBySlug(base, s),\n });\n const {\n createSubmission,\n stampNotified,\n stampFanout,\n findRecentDuplicateSubmissions,\n listRecentSubmissionsForEmail,\n markSubmissionsSpamRetro,\n } = await import(\"../../db/submissions.js\");\n const { makeNotify } = await import(\"../../forms/notify.js\");\n const { classifySpam } = await import(\"../../forms/spam-classifier.js\");\n const { forwardNewsletterToWebhook } = await import(\"../../forms/webhook.js\");\n const { addMailchimpMember, mailchimpTagsFor } = await import(\"../../forms/mailchimp.js\");\n const { defaultResendClient } = await import(\"../../reports/send/resend.js\");\n const { replayDeadLetters } = await import(\"../../forms/replay.js\");\n\n // Same degradation as the ingest handler: an unconfigured Resend key means\n // replayed leads land un-emailed (notify=failed) rather than blocking replay.\n let send = null;\n try {\n send = defaultResendClient().send;\n } catch (err) {\n console.error(`[db] Resend unconfigured; replaying without email: ${String(err)}`);\n }\n\n // Mirrors the production handler's deps minus `deadLetter` (replayDeadLetters\n // forbids and strips it — a throwing lookup must retry, not duplicate) and\n // minus `defer` (a CLI has no post-response phase; the inline tail is fine).\n const result = await replayDeadLetters(db, {\n getWebsiteBySlug: lookupSite,\n createSubmission: (input) => createSubmission(db, input),\n notify: makeNotify(send),\n stampNotified: (id, status, messageId) => stampNotified(db, id, status, messageId),\n now: () => new Date(),\n classifySpam: (n, outcome) =>\n classifySpam({\n name: n.name,\n email: n.email,\n ...(n.message !== undefined ? { message: n.message } : {}),\n formType: n.formType,\n extraFields: n.extraFields,\n turnstile: outcome,\n }),\n findRecentDuplicates: (message, since) =>\n findRecentDuplicateSubmissions(db, message, since.toISOString()),\n listRecentSubmissionsForEmail: (email, since) =>\n listRecentSubmissionsForEmail(db, email, since.toISOString()),\n retroBucket: (ids, reason) => markSubmissionsSpamRetro(db, ids, reason),\n forwardNewsletter: (url, submission, site) =>\n forwardNewsletterToWebhook(url, submission, site),\n addToMailchimp: (site, submission) =>\n addMailchimpMember({\n apiKey: site.mailchimpApiKey ?? \"\",\n audienceId: site.mailchimpAudienceId ?? \"\",\n email: submission.email,\n name: submission.name,\n tags: mailchimpTagsFor(submission.formType),\n }),\n stampFanout: (id, fanoutStatus) => stampFanout(db, id, fanoutStatus),\n });\n\n // MED-10. Four buckets, three of them non-zero-is-not-nothing. `still_failing`\n // is a STANDING condition (a slug awaiting `ensure-site`, or a dead one\n // awaiting `--abandon`) and already has an alarm — the `deadletter` attention\n // item. `unmarked` and `unreadable` are DEFECTS with no other surface at all:\n // this output is the only place either is ever named.\n const lines = [\n ...result.replayed.map(\n (r) => `replayed ${r.id} → ${r.outcome}${r.submissionId ? ` (${r.submissionId})` : \"\"}`,\n ),\n ...result.stillFailing.map((r) => `still failing ${r.id}: ${r.error}`),\n ...result.unmarked.map(\n (r) =>\n `UNMARKED ${r.id}: re-ingested as ${r.submissionId ?? \"(no submission)\"} → ${r.outcome}, ` +\n `but the terminal mark could NOT be written (${r.error}). The row is still queued, so ` +\n `replaying again before it is reconciled will mint a DUPLICATE of this lead.`,\n ),\n ...result.unreadable.map(\n (r) =>\n `UNREADABLE ${r.id} (site '${r.siteSlug}', received ${r.receivedAt}): ${r.error}. ` +\n `The row is untouched — repair the stored JSON, or retire it with ` +\n `\\`db replay-deadletters --abandon ${r.id} --reason \"…\"\\`.`,\n ),\n `DEADLETTER_REPLAY replayed=${result.replayed.length} still_failing=${result.stillFailing.length} unmarked=${result.unmarked.length} unreadable=${result.unreadable.length}`,\n ];\n const owed = result.stillFailing.length + result.unmarked.length + result.unreadable.length > 0;\n return { output: lines.join(\"\\n\"), code: owed ? 1 : 0 };\n }\n\n // Phase 1.3/1.4 of #539. Both read the same two Airtable tables raw (id +\n // fields, no mapRow coercion — the importer's mapping is the authority) and\n // share that mapping, so parity is definitionally checked against what the\n // importer writes.\n if (action === \"import-airtable\" || action === \"parity\" || action === \"sync\") {\n const { TURSO_IS_AUTHORITATIVE } = await import(\"../../db/freeze.js\");\n const refused = freezeGuardsDbWrite(action, opts.force === true, TURSO_IS_AUTHORITATIVE);\n if (refused) return refused;\n const { readDbConfig, openDb } = await import(\"../../db/client.js\");\n const db = await openDb(opts.url ? { url: opts.url } : readDbConfig());\n const { openBase, readAirtableConfig } = await import(\"../../reports/airtable/client.js\");\n const base = openBase(readAirtableConfig());\n const listRaw = async (table: string) =>\n (await base(table).select().all()).map((r) => ({\n id: r.id,\n fields: r.fields as Record<string, unknown>,\n }));\n const io = {\n listWebsiteRecords: () => listRaw(\"Websites\"),\n listReportRecords: () => listRaw(\"Reports\"),\n now: () => new Date(),\n };\n\n if (action === \"import-airtable\") {\n const { importFleetState, formatReapSummary } = await import(\"../../db/import-airtable.js\");\n const summary = await importFleetState(db, {\n ...io,\n // Attachment bodies ride expiring signed URLs; a failed fetch imports the\n // row with rendered_html null and is NAMED in the summary, never silent.\n fetchAttachment: async (url) => {\n try {\n const res = await fetch(url);\n return res.ok ? await res.text() : null;\n } catch {\n return null;\n }\n },\n });\n const lines = [\n `imported ${summary.sites} site(s) → sites/site_health/site_schedule`,\n `imported ${summary.reports} report(s)`,\n ];\n if (summary.renderedHtmlMisses.length > 0) {\n lines.push(\n `⚠ ${summary.renderedHtmlMisses.length} report(s) imported WITHOUT Rendered HTML ` +\n `(fetch failed / URL expired): ${summary.renderedHtmlMisses.join(\", \")}`,\n );\n }\n // The import deletes rows Airtable no longer has, so this one-shot path\n // reports the reap exactly as `db sync` does — same formatter, no second\n // copy to fall out of step.\n lines.push(...formatReapSummary(summary.reaped));\n return { output: lines.join(\"\\n\"), code: 0 };\n }\n\n // Phase 2 backbone (#539): one hourly pass = import (attachment fetches\n // only where the stored row lacks a body) + parity + one retry to absorb\n // the import-read/parity-read race. Exit 1 on persistent mismatch.\n if (action === \"sync\") {\n const { syncFleetState, formatSyncResult } = await import(\"../../db/sync.js\");\n const result = await syncFleetState(db, {\n ...io,\n fetchAttachment: async (url) => {\n try {\n const res = await fetch(url);\n return res.ok ? await res.text() : null;\n } catch {\n return null;\n }\n },\n });\n return {\n output: formatSyncResult(result),\n code: result.parity.mismatches.length > 0 ? 1 : 0,\n };\n }\n\n const { checkFleetParity, formatParityResult } = await import(\"../../db/parity.js\");\n const result = await checkFleetParity(db, io);\n return { output: formatParityResult(result), code: result.mismatches.length > 0 ? 1 : 0 };\n }\n\n // One-shot completion of design D5 (#539 Phase 2): copy every site's CURRENT\n // Airtable \"Header image\" attachment into sites.header_image*. Idempotent —\n // an already-populated BLOB is never overwritten (a re-run must not clobber\n // a freshly generated image with a stale Airtable copy). Exit 1 when any\n // fetch failed, so a partial backfill is never read as complete.\n if (action === \"backfill-header-images\") {\n const { readDbConfig, openDb } = await import(\"../../db/client.js\");\n const db = await openDb(opts.url ? { url: opts.url } : readDbConfig());\n const { openBase, readAirtableConfig } = await import(\"../../reports/airtable/client.js\");\n const base = openBase(readAirtableConfig());\n const { backfillHeaderImages, formatBackfillResult } =\n await import(\"../../db/header-images.js\");\n const result = await backfillHeaderImages(db, {\n listWebsiteRecords: async () =>\n (await base(\"Websites\").select().all()).map((r) => ({\n id: r.id,\n fields: r.fields as Record<string, unknown>,\n })),\n fetchBytes: async (url) => {\n try {\n const res = await fetch(url);\n return res.ok ? new Uint8Array(await res.arrayBuffer()) : null;\n } catch {\n return null;\n }\n },\n });\n return { output: formatBackfillResult(result), code: result.failed.length > 0 ? 1 : 0 };\n }\n\n // #609: one-shot copy of the single Airtable \"Digest State\" row into Turso,\n // so the first digest run after the read repoint sees yesterday's snapshot\n // instead of an empty one. An empty read is not a crash — it badges EVERY\n // item NEW, which lands in the operator's inbox reading as \"the whole fleet\n // degraded overnight\". REFUSES to overwrite a snapshot Turso already holds:\n // a re-run must never replace a fresher snapshot with a stale Airtable copy.\n if (action === \"backfill-digest-state\") {\n const { readDbConfig, openDb } = await import(\"../../db/client.js\");\n const db = await openDb(opts.url ? { url: opts.url } : readDbConfig());\n const { readDigestState: readTurso, writeDigestState: writeTurso } =\n await import(\"../../db/digest-state.js\");\n const existing = await readTurso(db);\n if (Object.keys(existing).length > 0) {\n return {\n output: `DIGEST_BACKFILL skipped=1 reason=turso-already-populated keys=${Object.keys(existing).length}`,\n code: 0,\n };\n }\n const { openBase, readAirtableConfig } = await import(\"../../reports/airtable/client.js\");\n const base = openBase(readAirtableConfig());\n const { readDigestState: readAirtable, DIGEST_STATE_TABLE } =\n await import(\"../../alerts/digest-state.js\");\n // `source` separates \"Airtable has no row\" from \"Airtable has a row holding\n // an empty snapshot\" — the reader collapses BOTH to {}, so copied=0 alone\n // cannot tell a quiet fleet from a failed read. Learned by running this: the\n // first real run printed copied=0 and only a hand probe showed the row was\n // there and genuinely empty.\n const rows = await base(DIGEST_STATE_TABLE).select({ maxRecords: 1, pageSize: 1 }).all();\n const snap = await readAirtable(base);\n const keys = Object.keys(snap).length;\n await writeTurso(db, snap);\n // Read it BACK. A returning write is not evidence the row landed — the same\n // rule forms-notify-target learned on 2026-08-03. `stored` counts the ROW,\n // not its keys, so an empty-but-present snapshot verifies as written.\n const stored = (await db.selectFrom(\"digest_state\").selectAll().execute()).length;\n const after = Object.keys(await readTurso(db)).length;\n return {\n output:\n `DIGEST_BACKFILL source=${rows.length > 0 ? \"row\" : \"absent\"} ` +\n `copied=${keys} verified=${after} rows=${stored}`,\n code: after === keys && stored === 1 ? 0 : 1,\n };\n }\n\n // Phase 1.5 of #539: platform-auth-free SQL dump to stdout-adjacent output.\n // The nightly backup workflow redirects this to a file, encrypts, uploads;\n // the rehearsed restore loads it into stock sqlite3 and compares row counts.\n if (action === \"dump\") {\n const { readDbConfig } = await import(\"../../db/client.js\");\n const cfg = opts.url ? { url: opts.url } : readDbConfig();\n const { createClient } = await import(\"@libsql/client\");\n const client = createClient(cfg.url === \":memory:\" ? { url: \":memory:\" } : cfg);\n const { dumpDatabase } = await import(\"../../db/dump.js\");\n const sql = await dumpDatabase(\n {\n execute: async (q) => {\n const r = await client.execute(q);\n return { columns: r.columns, rows: r.rows as Array<Record<string, unknown>> };\n },\n },\n // A FUNCTION, not a string: re-evaluated per attempt, so a dump that\n // succeeded on attempt 3 is stamped with attempt 3's time instead of\n // carrying the timestamp of two dumps that were discarded.\n () => new Date().toISOString(),\n {\n // MED-12: a torn snapshot is re-taken, not shipped. Report each discard\n // on STDERR — stdout is the dump itself, and a diagnostic written there\n // would land inside the SQL (the `pnpm exec` trap, one layer down).\n onTorn: (attempt, moved) =>\n console.error(\n `[db dump] attempt ${attempt} discarded: the database changed while it was being ` +\n `read (${moved.join(\"; \")}). Re-taking the dump.`,\n ),\n },\n );\n return { output: sql, code: 0 };\n }\n\n // Load a dump back into a REAL libSQL target (#612 review). The nightly\n // rehearsal loads into `:memory:`, which proves the SQL parses — it does not\n // prove you can get the data back into Turso, and that is the operation an\n // actual recovery needs. Replaying ~17 MB of SQL with megabytes of inline hex\n // over HTTP is materially different from an in-process load, and it had never\n // been done. Refuses to touch a database that already holds rows: a restore\n // is for an EMPTY target, and pointing this at production by mistake should\n // cost nothing.\n if (action === \"restore\") {\n const file = opts.file;\n if (!file) return { output: \"restore: pass the dump path via --file\", code: 1 };\n if (!opts.url) {\n return {\n output:\n \"restore: pass the TARGET database via --url (never defaults, to keep production out of reach)\",\n code: 1,\n };\n }\n // Classify the target BEFORE reading the dump, so a missing token names\n // itself instead of arriving as an opaque 401 (or, worse, as an ENOENT that\n // sends you hunting for the dump file). This command built its client from\n // a url alone until 2026-08-26, which worked against every target the tests\n // and rehearsals used — `:memory:` and a local `turso dev` — and failed\n // against every target an actual recovery has.\n const { parseDumpManifest, requiresAuthToken } = await import(\"../../db/dump.js\");\n const authToken = deps.restoreAuthToken ?? process.env.TURSO_RESTORE_AUTH_TOKEN ?? \"\";\n if (requiresAuthToken(opts.url) && !authToken) {\n return { output: \"RESTORE refused=auth-token-absent\", code: 1 };\n }\n const { readFile } = await import(\"node:fs/promises\");\n const sql = await readFile(file, \"utf-8\");\n const manifest = parseDumpManifest(sql);\n if (!manifest) return { output: \"RESTORE refused=manifest-absent\", code: 1 };\n const { createClient } = await import(\"@libsql/client\");\n const target = createClient(authToken ? { url: opts.url, authToken } : { url: opts.url });\n const existing = await target.execute(\n \"SELECT COUNT(*) AS n FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'\",\n );\n if (Number(existing.rows[0]?.n ?? 0) > 0) {\n return { output: \"RESTORE refused=target-not-empty\", code: 1 };\n }\n await target.executeMultiple(sql);\n const { tableCounts, headerImageBytes } = await import(\"../../db/dump.js\");\n const exec = {\n execute: async (q: string) => {\n const r = await target.execute(q);\n return { columns: r.columns, rows: r.rows as Array<Record<string, unknown>> };\n },\n };\n const counts = await tableCounts(exec);\n const bytes = await headerImageBytes(exec);\n // Same origin-anchored comparison as verify-dump: a restore that \"succeeded\"\n // with fewer rows than the origin held is not a restore.\n const bad: string[] = [];\n for (const [t, want] of Object.entries(manifest.tables)) {\n if ((counts[t] ?? 0) !== want) bad.push(`${t}: origin=${want} restored=${counts[t] ?? 0}`);\n }\n if (bytes !== manifest.blobBytes)\n bad.push(`header_image bytes: origin=${manifest.blobBytes} restored=${bytes}`);\n const rows = Object.values(counts).reduce((a, b) => a + b, 0);\n return {\n output: [\n ...bad.map((b) => `✗ ${b}`),\n `RESTORE loaded=true tables=${Object.keys(counts).length} rows=${rows} blob_bytes=${bytes} mismatches=${bad.length}`,\n ].join(\"\\n\"),\n code: bad.length > 0 ? 1 : 0,\n };\n }\n\n // How much of the plan's quota the fleet has burned this billing cycle\n // (#539 HIGH-10). The starter plan carries `overages: false`, so crossing a\n // quota BLOCKS reads and writes rather than billing for them — and once the\n // Airtable cutover lands, Turso is the only store there is. This is the one\n // alarm that fires before a wall rather than after it.\n //\n // Needs a PLATFORM token, which is a different credential from the\n // database-level TURSO_AUTH_TOKEN the rest of the fleet runs on: the database\n // token cannot read quota state at all.\n if (action === \"usage\") {\n const token = deps.platformToken ?? process.env.TURSO_FLEET_USAGE ?? \"\";\n // An unconfigured alarm must not read as a quiet, healthy one. The tell for\n // \"never ran\" has to be a failure, not a missing line (#585).\n if (!token) {\n return {\n output:\n \"FLEET_DB_USAGE verdict=no-token — set TURSO_FLEET_USAGE (turso auth api-tokens mint …). \" +\n \"This is the PLATFORM token, not the database TURSO_AUTH_TOKEN.\",\n code: 1,\n };\n }\n const { collectUsage, assessUsage } = await import(\"../../db/usage.js\");\n const input = await collectUsage({\n token,\n org: opts.org ?? process.env.TURSO_ORG,\n fetchImpl: deps.fetchImpl,\n now: deps.now ?? new Date(),\n });\n const r = assessUsage(input);\n const window = `${input.cycleStart.toISOString().slice(0, 10)} → ${input.cycleEnd\n .toISOString()\n .slice(0, 10)}`;\n return {\n output: [`Turso plan=${input.plan} billing cycle ${window}`, ...r.lines, \"\", r.marker].join(\n \"\\n\",\n ),\n code: r.code,\n };\n }\n\n // The restore rehearsal (Phase 1.5's hard gate), runnable every night: load\n // the dump into a FRESH in-memory engine and compare what came back against\n // the ORIGIN MANIFEST the dump carries.\n //\n // It used to compare against INSERT counts parsed out of the dump text — i.e.\n // the dump against itself. Both sides derived from one artifact, so a dump\n // that collected 5 of 44 sites shrank both numbers together and verified\n // clean. The manifest is read from the live database before any row is\n // serialised, which is the only measurement that can notice a short dump.\n //\n // A dump that cannot restore is not a backup — and per the repo's instrument\n // rule the check emits its machine line on every run, clean included.\n if (action === \"verify-dump\") {\n const file = opts.file;\n if (!file) return { output: \"verify-dump: pass the dump path via --file\", code: 1 };\n const { readFile } = await import(\"node:fs/promises\");\n const sql = await readFile(file, \"utf-8\");\n const { createClient } = await import(\"@libsql/client\");\n const scratch = createClient({ url: \":memory:\" });\n try {\n await scratch.executeMultiple(sql);\n } catch (err) {\n return { output: `DUMP_VERIFY loaded=false error=${String(err)}`, code: 1 };\n }\n const { tableCounts, headerImageBytes, parseDumpManifest } = await import(\"../../db/dump.js\");\n const manifest = parseDumpManifest(sql);\n if (!manifest) {\n // Refuse rather than fall back to self-comparison. Falling back would\n // re-enable exactly the blind spot the manifest exists to close, and it\n // would do so silently on the one artifact nobody was watching.\n return {\n output: \"DUMP_VERIFY loaded=true manifest=absent — dump predates the origin manifest\",\n code: 1,\n };\n }\n const exec = {\n execute: async (q: string) => {\n const r = await scratch.execute(q);\n return { columns: r.columns, rows: r.rows as Array<Record<string, unknown>> };\n },\n };\n const restored = await tableCounts(exec);\n const restoredBlobBytes = await headerImageBytes(exec);\n const mismatches: string[] = [];\n for (const [table, want] of Object.entries(manifest.tables)) {\n if ((restored[table] ?? 0) !== want) {\n mismatches.push(`${table}: origin=${want} restored=${restored[table] ?? 0}`);\n }\n }\n // A table the origin held and the dump never mentioned would otherwise be\n // invisible: absent from `restored` AND absent from the loop above.\n for (const table of Object.keys(restored)) {\n if (!(table in manifest.tables)) mismatches.push(`${table}: not in origin manifest`);\n }\n // Coverage: every table the APP owns must be present. `tables=N` used to be\n // printed and never asserted, so a table a migration failed to create — or\n // that the dump lost — rode green forever. `digest_state` and\n // `prospect_audits` were both absent from every artifact the night this was\n // found, purely because they postdated the last run.\n const { DATABASE_TABLES } = await import(\"../../db/schema.js\");\n for (const table of DATABASE_TABLES) {\n if (!(table in restored)) mismatches.push(`${table}: MISSING from the backup entirely`);\n }\n if (restoredBlobBytes !== manifest.blobBytes) {\n mismatches.push(\n `header_image bytes: origin=${manifest.blobBytes} restored=${restoredBlobBytes}`,\n );\n }\n const total = Object.values(restored).reduce((a, b) => a + b, 0);\n const lines = [\n ...mismatches.map((m) => `✗ ${m}`),\n `DUMP_VERIFY loaded=true tables=${Object.keys(restored).length} rows=${total} blob_bytes=${restoredBlobBytes} mismatches=${mismatches.length}`,\n ];\n return { output: lines.join(\"\\n\"), code: mismatches.length > 0 ? 1 : 0 };\n }\n\n return {\n output: `unknown db action '${action}'. Use: migrate, replay-deadletters, import-airtable, parity, sync, backfill-header-images, backfill-digest-state, dump, verify-dump, restore.`,\n code: 1,\n };\n}\n"],"mappings":";AAiDO,SAAS,oBACd,QACA,OACA,eACyC;AACzC,MAAI,CAAC,cAAe,QAAO;AAC3B,MAAI,WAAW,qBAAqB,WAAW,OAAQ,QAAO;AAC9D,MAAI,MAAO,QAAO;AAClB,SAAO;AAAA,IACL,QACE,MAAM,MAAM;AAAA,IAGd,MAAM;AAAA,EACR;AACF;AAMA,eAAsB,aACpB,QACA,MACA,OAAsB,CAAC,GACoB;AAC3C,MAAI,WAAW,WAAW;AACxB,UAAM,EAAE,aAAa,IAAI,MAAM,OAAO,sBAAoB;AAC1D,UAAM,MAAM,KAAK,MAAM,EAAE,KAAK,KAAK,IAAI,IAAI,aAAa;AACxD,UAAM,EAAE,cAAc,IAAI,MAAM,OAAO,uBAAqB;AAC5D,UAAM,EAAE,aAAa,IAAI,MAAM,OAA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ACA,WAAO,EAAE,QAAQ,MAAM,KAAK,IAAI,GAAG,MAAM,WAAW,SAAS,IAAI,IAAI,EAAE;AAAA,EACzE;AAEA,SAAO;AAAA,IACL,QAAQ,sBAAsB,MAAM;AAAA,IACpC,MAAM;AAAA,EACR;AACF;","names":["db","lines","result"]}
@@ -22,7 +22,26 @@ function sqlLiteral(v) {
22
22
  }
23
23
  return `'${str.replaceAll("'", "''")}'`;
24
24
  }
25
- async function dumpDatabase(db, generatedAt) {
25
+ var DUMP_ATTEMPTS = 3;
26
+ var BLOB_TABLE = "sites";
27
+ var BLOB_COLUMN = "header_image";
28
+ async function dumpDatabase(db, generatedAt, opts = {}) {
29
+ const attempts = Math.max(1, opts.attempts ?? DUMP_ATTEMPTS);
30
+ let moved = [];
31
+ for (let attempt = 1; attempt <= attempts; attempt++) {
32
+ const taken = await dumpOnce(
33
+ db,
34
+ typeof generatedAt === "function" ? generatedAt() : generatedAt
35
+ );
36
+ if (taken.sql !== null) return taken.sql;
37
+ moved = taken.moved;
38
+ opts.onTorn?.(attempt, moved);
39
+ }
40
+ throw new Error(
41
+ `db dump: the database changed under every one of ${attempts} attempts \u2014 ${moved.join("; ")}. The dump was DISCARDED rather than written with a manifest it disagrees with. Re-run when writes are quiet; a fleet that never goes quiet needs a snapshot mechanism, not more retries.`
42
+ );
43
+ }
44
+ async function dumpOnce(db, generatedAt) {
26
45
  const manifest = {
27
46
  tables: await tableCounts(db),
28
47
  blobBytes: await headerImageBytes(db),
@@ -42,8 +61,14 @@ async function dumpDatabase(db, generatedAt) {
42
61
  out.push(`${String(row.sql).trim().replace(/;?$/, "")};`);
43
62
  if (row.type === "table") tables.push(String(row.name));
44
63
  }
64
+ const dumped = {};
65
+ let dumpedBlobBytes = 0;
45
66
  for (const table of tables) {
46
67
  const data = await db.execute(`SELECT * FROM ${quoteIdent(table)} ORDER BY rowid`);
68
+ dumped[table] = data.rows.length;
69
+ if (table === BLOB_TABLE) {
70
+ for (const row of data.rows) dumpedBlobBytes += storedLength(row[BLOB_COLUMN]);
71
+ }
47
72
  if (data.rows.length === 0) continue;
48
73
  const cols = data.columns.map(quoteIdent).join(", ");
49
74
  for (const row of data.rows) {
@@ -51,13 +76,36 @@ async function dumpDatabase(db, generatedAt) {
51
76
  out.push(`INSERT INTO ${quoteIdent(table)} (${cols}) VALUES (${values});`);
52
77
  }
53
78
  }
79
+ const moved = [];
80
+ for (const [table, claimed] of Object.entries(manifest.tables)) {
81
+ const written = dumped[table];
82
+ if (written !== claimed) {
83
+ moved.push(`${table}: manifest ${claimed}, dumped ${written ?? "not dumped at all"}`);
84
+ }
85
+ }
86
+ for (const table of Object.keys(dumped)) {
87
+ if (!(table in manifest.tables)) {
88
+ moved.push(`${table}: absent from the manifest, dumped ${dumped[table]}`);
89
+ }
90
+ }
91
+ if (dumpedBlobBytes !== manifest.blobBytes) {
92
+ moved.push(`header_image bytes: manifest ${manifest.blobBytes}, dumped ${dumpedBlobBytes}`);
93
+ }
94
+ if (moved.length > 0) return { sql: null, moved };
54
95
  out.push("COMMIT;");
55
- return out.join("\n") + "\n";
96
+ return { sql: out.join("\n") + "\n", moved: [] };
97
+ }
98
+ function storedLength(v) {
99
+ if (v === null || v === void 0) return 0;
100
+ if (v instanceof Uint8Array) return v.byteLength;
101
+ if (v instanceof ArrayBuffer) return v.byteLength;
102
+ if (typeof v === "string") return [...v].length;
103
+ return String(v).length;
56
104
  }
57
105
  var MANIFEST_PREFIX = "-- REDDOOR_DUMP_MANIFEST ";
58
106
  async function headerImageBytes(db) {
59
107
  const r = await db.execute(
60
- "SELECT COALESCE(SUM(LENGTH(header_image)), 0) AS n FROM sites WHERE header_image IS NOT NULL"
108
+ `SELECT COALESCE(SUM(LENGTH(${BLOB_COLUMN})), 0) AS n FROM ${BLOB_TABLE} WHERE ${BLOB_COLUMN} IS NOT NULL`
61
109
  );
62
110
  return Number(r.rows[0]?.n ?? 0);
63
111
  }
@@ -95,6 +143,7 @@ function requiresAuthToken(url) {
95
143
  return !LOCAL_HOSTS.has(parsed.hostname);
96
144
  }
97
145
  export {
146
+ DUMP_ATTEMPTS,
98
147
  MANIFEST_PREFIX,
99
148
  dumpDatabase,
100
149
  headerImageBytes,
@@ -103,4 +152,4 @@ export {
103
152
  sqlLiteral,
104
153
  tableCounts
105
154
  };
106
- //# sourceMappingURL=dump-QK7LNOGC.js.map
155
+ //# sourceMappingURL=dump-WVCFUOL3.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/db/dump.ts"],"sourcesContent":["/**\n * Phase 1.5 of #539: a platform-auth-free SQL dump of the whole database.\n *\n * `turso db dump` needs a PLATFORM login (browser OAuth) — a nightly workflow\n * has only the DATABASE-level url+token it already holds for every other job.\n * So the dump speaks plain SQL through the same client: schema straight from\n * `sqlite_master`, then every row as an INSERT. The output loads into stock\n * `sqlite3` (libSQL IS SQLite), which is exactly how the rehearsed restore\n * proves it — and how a real disaster would replay it into a fresh database.\n *\n * Determinism: tables and rows are emitted in stable order (name, then rowid)\n * so two dumps of an unchanged database are byte-identical — a diffable backup.\n */\n\n/** Minimal execute surface: the @libsql/client `execute` we need. Injectable so\n * tests run against :memory: without the real network client. */\nexport type SqlExecutor = {\n execute: (sql: string) => Promise<{\n columns: string[];\n rows: Array<Record<string, unknown>>;\n }>;\n};\n\nconst IDENT = /^[A-Za-z_][A-Za-z0-9_]*$/;\n\nfunction quoteIdent(name: string): string {\n return IDENT.test(name) ? name : `\"${name.replaceAll('\"', '\"\"')}\"`;\n}\n\nexport function sqlLiteral(v: unknown): string {\n if (v === null || v === undefined) return \"NULL\";\n if (typeof v === \"number\") return Number.isFinite(v) ? String(v) : \"NULL\";\n if (typeof v === \"bigint\") return v.toString();\n if (v instanceof Uint8Array || v instanceof ArrayBuffer) {\n const bytes = v instanceof ArrayBuffer ? new Uint8Array(v) : v;\n let hex = \"\";\n for (const b of bytes) hex += b.toString(16).padStart(2, \"0\");\n return `X'${hex}'`;\n }\n const str = String(v);\n // A NUL cannot ride a single-quoted SQL literal. This used to strip them,\n // justified as \"they cannot legitimately appear in this schema's TEXT\n // columns\" — but `submissions` free text is attacker-supplied and SQLite\n // stores NUL inside TEXT quite happily, so the backup would have silently\n // differed from the origin with no signal. Emit the whole value as a hex blob\n // cast back to text instead: lossless, and it round-trips through the\n // rehearsal like any other literal.\n if (str.includes(\"\\u0000\")) {\n const bytes = new TextEncoder().encode(str);\n let hex = \"\";\n for (const b of bytes) hex += b.toString(16).padStart(2, \"0\");\n return `CAST(X'${hex}' AS TEXT)`;\n }\n // Strings: standard SQL escaping — double the single quotes.\n return `'${str.replaceAll(\"'\", \"''\")}'`;\n}\n\n/** How many times a torn dump is re-taken before the run is failed.\n *\n * The budget, measured 2026-09-20 across the last five nightly runs: the\n * \"Dump + rehearse the restore\" step takes 10–16 s against the workflow's\n * `timeout-minutes: 15`, so three full attempts sit roughly fiftyfold under\n * the cap and no workflow change is owed to this retry loop. The reason not to\n * raise the number is not time — it is that a database which will not settle\n * in three passes needs a snapshot mechanism, not more retries. */\nexport const DUMP_ATTEMPTS = 3;\n\nexport type DumpOptions = {\n /** Bounded retries on a torn snapshot. Default `DUMP_ATTEMPTS`. */\n attempts?: number;\n /** Called after each discarded attempt, with the disagreements between the\n * origin manifest and what that attempt actually serialised. The CLI routes\n * this to stderr — stdout carries the dump itself. */\n onTorn?: (attempt: number, moved: string[]) => void;\n};\n\n/** The one table+column `blobBytes` measures. Named once so the manifest side\n * (SQL `LENGTH()`, below) and the dump side (what was serialised) cannot drift\n * apart into measuring two different things. */\nconst BLOB_TABLE = \"sites\";\nconst BLOB_COLUMN = \"header_image\";\n\n/**\n * Dump schema + data as executable SQL. Skips SQLite's internal tables\n * (`sqlite_*`); includes indexes. Wrapped in a transaction so a partial load\n * fails atomically instead of leaving a half-restored scratch that could be\n * mistaken for a good one.\n *\n * MED-12 — THE TORN SNAPSHOT, and why it is detected rather than prevented.\n *\n * `SqlExecutor` is a deliberately minimal `{ execute(sql) }`: every call is its\n * own round trip, and there is no way to hold a transaction across them —\n * production is hosted Turso over HTTP, where every statement is its own\n * implicit transaction and a held read transaction is not on offer. So the\n * manifest read and the row reads cannot be made one point in time, and the\n * window between them is inherent to the contract. Over a ~17 MB dump that\n * window is seconds, and at 04:30 UTC (21:30 PT) visitor traffic is the only\n * writer — one form submission landing in it produced\n * `submissions: origin=354 restored=355`, reddened the job BEFORE the encrypt\n * and upload steps, and uploaded no backup that night.\n *\n * So: read the live manifest, serialise, and compare that manifest against WHAT\n * THIS DUMP ACTUALLY CONTAINS — the rows written per table, and the\n * header-image bytes written. Disagreement means the dump is internally\n * inconsistent: discard it, take the whole dump again a bounded number of\n * times, then fail loudly naming the table. A dump is only ever emitted when\n * its manifest describes the rows it carries.\n *\n * THE COMPARISON THAT DID NOT WORK, because the shape of the mistake is the\n * useful part: the first cut re-read the live counts after serialising and\n * compared them to the live counts before. That asks \"did the database move?\",\n * not \"do the manifest and the rows agree?\", and the two come apart BOTH ways.\n *\n * - A row inserted AFTER its table's `SELECT *` but before the second count\n * makes a perfectly consistent dump (manifest 354, 354 INSERTs) look torn.\n * A good backup is discarded; three such nights in a row and there is no\n * backup at all, which is the outcome MED-12 exists to remove.\n * - A row inserted BEFORE the read and deleted after it returns the live\n * count to where it started. Nothing looks torn, the dump ships carrying\n * 355 INSERTs under a manifest claiming 354, and the nightly reds anyway.\n * - Neither live read can see an UNDER-COLLECTION — a `SELECT *` that returns\n * fewer rows than exist, or every `header_image` coming back NULL — which\n * is the failure the manifest was built for in the first place (see\n * MANIFEST_PREFIX below).\n *\n * THE RESIDUAL, stated rather than papered over: the comparison is per-table\n * row counts plus one blob-byte total. An UPDATE moves neither, so a row edited\n * between the manifest read and its table's `SELECT *` is serialised in its new\n * form under a manifest taken before it; an insert and a delete inside the same\n * table and the same window cancel out the same way. So a dump still is NOT a\n * point-in-time snapshot of the whole database — what it now is, provably, is a\n * dump whose manifest describes the rows it carries, which is the only property\n * the nightly `verify-dump` actually checks. A checksum per table is what would\n * close the rest, at a cost this does not currently earn.\n *\n * This is NOT the self-comparison MANIFEST_PREFIX warns against. The EXPECTED\n * side is still measured on the LIVE database before a single row is\n * serialised; only the ACTUAL side changed, from a second live read to the\n * dump's own contents — which is exactly what the manifest exists to be checked\n * against. Nothing is parsed back out of the emitted text.\n */\nexport async function dumpDatabase(\n db: SqlExecutor,\n /** Stamped into the manifest. A function is re-evaluated per attempt, so a\n * dump that succeeds on attempt 3 carries attempt 3's time rather than the\n * time of a dump that was discarded; a plain string keeps a caller (and the\n * determinism test) able to pin it. */\n generatedAt: string | (() => string),\n opts: DumpOptions = {},\n): Promise<string> {\n const attempts = Math.max(1, opts.attempts ?? DUMP_ATTEMPTS);\n let moved: string[] = [];\n for (let attempt = 1; attempt <= attempts; attempt++) {\n const taken = await dumpOnce(\n db,\n typeof generatedAt === \"function\" ? generatedAt() : generatedAt,\n );\n if (taken.sql !== null) return taken.sql;\n moved = taken.moved;\n opts.onTorn?.(attempt, moved);\n }\n throw new Error(\n `db dump: the database changed under every one of ${attempts} attempts — ` +\n `${moved.join(\"; \")}. The dump was DISCARDED rather than written with a manifest ` +\n `it disagrees with. Re-run when writes are quiet; a fleet that never goes quiet ` +\n `needs a snapshot mechanism, not more retries.`,\n );\n}\n\n/** One attempt. Returns the dump, or the ways the manifest and the rows this\n * attempt actually serialised disagree. */\nasync function dumpOnce(\n db: SqlExecutor,\n generatedAt: string,\n): Promise<{ sql: string; moved: [] } | { sql: null; moved: string[] }> {\n // Read the ORIGIN's own numbers BEFORE serialising anything. This is the\n // whole point: it is the only measurement that does not come from the dump,\n // so it is the only one that can notice the dump is short.\n const manifest: DumpManifest = {\n tables: await tableCounts(db),\n blobBytes: await headerImageBytes(db),\n generatedAt,\n };\n const out: string[] = [\n // First line, so a truncated dump still carries what it CLAIMED to hold.\n `${MANIFEST_PREFIX}${JSON.stringify(manifest)}`,\n \"PRAGMA foreign_keys=OFF;\",\n \"BEGIN TRANSACTION;\",\n ];\n\n const schema = await db.execute(\n \"SELECT name, type, sql FROM sqlite_master \" +\n \"WHERE sql IS NOT NULL AND name NOT LIKE 'sqlite_%' \" +\n \"ORDER BY CASE type WHEN 'table' THEN 0 ELSE 1 END, name\",\n );\n\n const tables: string[] = [];\n for (const row of schema.rows) {\n out.push(`${String(row.sql).trim().replace(/;?$/, \"\")};`);\n if (row.type === \"table\") tables.push(String(row.name));\n }\n\n // What this dump ACTUALLY carries, accumulated as it is written — the side\n // the manifest has to agree with, and the only side that can settle the\n // question. Recorded before the empty-table `continue`, so a table that\n // vanished between the manifest read and here reads as 0, not as absent.\n const dumped: Record<string, number> = {};\n let dumpedBlobBytes = 0;\n\n for (const table of tables) {\n const data = await db.execute(`SELECT * FROM ${quoteIdent(table)} ORDER BY rowid`);\n dumped[table] = data.rows.length;\n if (table === BLOB_TABLE) {\n for (const row of data.rows) dumpedBlobBytes += storedLength(row[BLOB_COLUMN]);\n }\n if (data.rows.length === 0) continue;\n const cols = data.columns.map(quoteIdent).join(\", \");\n for (const row of data.rows) {\n const values = data.columns.map((c) => sqlLiteral(row[c])).join(\", \");\n out.push(`INSERT INTO ${quoteIdent(table)} (${cols}) VALUES (${values});`);\n }\n }\n\n // The manifest was measured on the LIVE database before any of the above ran.\n // Equal on both sides means this dump holds exactly what its first line\n // claims; unequal means the manifest and the INSERTs describe two different\n // databases, which is the one thing the nightly `verify-dump` cannot tell\n // apart from a genuine under-collection.\n const moved: string[] = [];\n for (const [table, claimed] of Object.entries(manifest.tables)) {\n const written = dumped[table];\n if (written !== claimed) {\n moved.push(`${table}: manifest ${claimed}, dumped ${written ?? \"not dumped at all\"}`);\n }\n }\n for (const table of Object.keys(dumped)) {\n if (!(table in manifest.tables)) {\n moved.push(`${table}: absent from the manifest, dumped ${dumped[table]}`);\n }\n }\n if (dumpedBlobBytes !== manifest.blobBytes) {\n moved.push(`header_image bytes: manifest ${manifest.blobBytes}, dumped ${dumpedBlobBytes}`);\n }\n if (moved.length > 0) return { sql: null, moved };\n\n out.push(\"COMMIT;\");\n return { sql: out.join(\"\\n\") + \"\\n\", moved: [] };\n}\n\n/** SQLite's own `LENGTH()` rule applied to a value the driver handed back:\n * BYTES for a blob, CHARACTERS for text.\n *\n * `headerImageBytes` measures the manifest side with `LENGTH()` in SQL, so the\n * dump side has to measure the same quantity or the two are not comparable and\n * every dump is \"torn\" forever. `sites.header_image` is declared BLOB and only\n * ever written bytes (`src/db/header-images.ts`), so in practice both sides\n * are byte counts of the same blobs; the text branch is here because SQLite is\n * dynamically typed and the cost of guessing wrong is discarding good backups\n * three times a night. */\nfunction storedLength(v: unknown): number {\n if (v === null || v === undefined) return 0;\n if (v instanceof Uint8Array) return v.byteLength;\n if (v instanceof ArrayBuffer) return v.byteLength;\n if (typeof v === \"string\") return [...v].length;\n return String(v).length;\n}\n\n/** Marker for the origin manifest line, first line of every dump. */\nexport const MANIFEST_PREFIX = \"-- REDDOOR_DUMP_MANIFEST \";\n\n/** What the ORIGIN database held at dump time.\n *\n * This exists because the restore rehearsal used to compare the dump against\n * ITSELF: expected counts were parsed out of the dump text, and actual counts\n * came from loading that same text. Both sides moved together, so a dump that\n * collected 5 of 44 sites verified clean. A manifest read from the LIVE\n * database before any rows are serialised is the only thing that can catch\n * under-dumping.\n *\n * `blobBytes` is the cheap content check. Row counts alone pass a dump in\n * which every `header_image` came back NULL — and those bytes exist in no\n * other store once Airtable is frozen. */\nexport type DumpManifest = {\n tables: Record<string, number>;\n blobBytes: number;\n generatedAt: string;\n};\n\n/** Total stored header-image bytes, the one content signal cheap enough to\n * check on every nightly run. */\nexport async function headerImageBytes(db: SqlExecutor): Promise<number> {\n const r = await db.execute(\n `SELECT COALESCE(SUM(LENGTH(${BLOB_COLUMN})), 0) AS n ` +\n `FROM ${BLOB_TABLE} WHERE ${BLOB_COLUMN} IS NOT NULL`,\n );\n return Number(r.rows[0]?.n ?? 0);\n}\n\n/** Parse the manifest line from a dump, or null when the dump predates it. */\nexport function parseDumpManifest(sql: string): DumpManifest | null {\n const line = sql.split(\"\\n\").find((l) => l.startsWith(MANIFEST_PREFIX));\n if (!line) return null;\n try {\n return JSON.parse(line.slice(MANIFEST_PREFIX.length)) as DumpManifest;\n } catch {\n return null;\n }\n}\n\n/** Per-table row counts — the cheap integrity check the restore rehearsal\n * compares across original vs restored. */\nexport async function tableCounts(db: SqlExecutor): Promise<Record<string, number>> {\n const schema = await db.execute(\n \"SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name\",\n );\n const counts: Record<string, number> = {};\n for (const row of schema.rows) {\n const name = String(row.name);\n const r = await db.execute(`SELECT COUNT(*) AS c FROM ${quoteIdent(name)}`);\n counts[name] = Number(r.rows[0]?.c ?? 0);\n }\n return counts;\n}\n\n/** Hosts that serve libSQL without authentication: `turso dev`, a local file,\n * and the in-process scratch engine the nightly rehearsal loads into. */\n// `[::1]` keeps its brackets: WHATWG URL reports an IPv6 hostname bracketed,\n// so the bare form alone would classify a local IPv6 target as hosted.\nconst LOCAL_HOSTS = new Set([\"localhost\", \"127.0.0.1\", \"[::1]\", \"::1\"]);\n\n/** Does this restore target need an auth token?\n *\n * `db restore` used to build its client from a url alone, which works against\n * every target the tests and rehearsals had ever used — `:memory:` and a local\n * `turso dev` — and 401s against every target a real recovery would have.\n * Classifying the url lets the command refuse with a named reason before the\n * network instead of surfacing an opaque SERVER_ERROR.\n *\n * Fails CLOSED: a url we cannot parse is treated as needing a token, because\n * the alternative is silently skipping auth and getting the 401 anyway. */\nexport function requiresAuthToken(url: string): boolean {\n if (url === \":memory:\") return false;\n let parsed: URL;\n try {\n parsed = new URL(url);\n } catch {\n return true;\n }\n if (parsed.protocol === \"file:\") return false;\n return !LOCAL_HOSTS.has(parsed.hostname);\n}\n"],"mappings":";AAuBA,IAAM,QAAQ;AAEd,SAAS,WAAW,MAAsB;AACxC,SAAO,MAAM,KAAK,IAAI,IAAI,OAAO,IAAI,KAAK,WAAW,KAAK,IAAI,CAAC;AACjE;AAEO,SAAS,WAAW,GAAoB;AAC7C,MAAI,MAAM,QAAQ,MAA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ACT;AACF;AAIA,eAAsB,YAAY,IAAkD;AAClF,QAAM,SAAS,MAAM,GAAG;AAAA,IACtB;AAAA,EACF;AACA,QAAM,SAAiC,CAAC;AACxC,aAAW,OAAO,OAAO,MAAM;AAC7B,UAAM,OAAO,OAAO,IAAI,IAAI;AAC5B,UAAM,IAAI,MAAM,GAAG,QAAQ,6BAA6B,WAAW,IAAI,CAAC,EAAE;AAC1E,WAAO,IAAI,IAAI,OAAO,EAAE,KAAK,CAAC,GAAG,KAAK,CAAC;AAAA,EACzC;AACA,SAAO;AACT;AAMA,IAAM,cAAc,oBAAI,IAAI,CAAC,aAAa,aAAa,SAAS,KAAK,CAAC;AAY/D,SAAS,kBAAkB,KAAsB;AACtD,MAAI,QAAQ,WAAY,QAAO;AAC/B,MAAI;AACJ,MAAI;AACF,aAAS,IAAI,IAAI,GAAG;AAAA,EACtB,QAAQ;AACN,WAAO;AAAA,EACT;AACA,MAAI,OAAO,aAAa,QAAS,QAAO;AACxC,SAAO,CAAC,YAAY,IAAI,OAAO,QAAQ;AACzC;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reddoorla/maintenance",
3
- "version": "0.98.0",
3
+ "version": "0.98.1",
4
4
  "description": "Canonical maintenance configs, audits, and recipes for the reddoor stack.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/cli/commands/db.ts"],"sourcesContent":["export type DbCommandOptions = {\n /** Override the libSQL url (tests use \":memory:\"); otherwise read from env. */\n url?: string;\n /** verify-dump: path to the dump file to load into a scratch engine. */\n file?: string;\n /** usage: org slug override; defaults to TURSO_ORG, else discovered. */\n org?: string;\n /** import-airtable / sync: run despite the freeze — a deliberate\n * rollback-window converge from the frozen Airtable shadow. */\n force?: boolean;\n /** replay-deadletters: abandon this slug's queued leads (or one `dl_…` row id)\n * as resolved-by-decision instead of replaying them (#786). */\n abandon?: string;\n /** replay-deadletters --abandon: why. Required — an undocumented write-off of\n * a client's leads is the thing this is meant to stop being necessary. */\n reason?: string;\n /** replay-deadletters --abandon: who decided. Defaults to OPERATOR_EMAIL. */\n by?: string;\n cwd?: string;\n verbose?: boolean;\n};\n\n/** Injected seams — deliberately NOT part of DbCommandOptions, which is the set\n * of things a shell can type (a registration gate asserts exactly that). The\n * platform token lives here rather than on a flag because a secret passed on\n * argv is readable from `ps`. */\nexport type DbCommandDeps = {\n /** Tests pass \"\" to exercise the unconfigured path, so the implementation\n * must use `?? env` and never `|| env`. */\n platformToken?: string;\n fetchImpl?: (url: string, init?: RequestInit) => Promise<Response>;\n now?: Date;\n /** restore: auth token for the TARGET database; defaults to\n * TURSO_RESTORE_AUTH_TOKEN. Deliberately does NOT fall back to the ambient\n * TURSO_AUTH_TOKEN — that one belongs to production, and inheriting it would\n * undo the whole point of making --url explicit. */\n restoreAuthToken?: string;\n};\n\n/** #643 (the freeze): the scheduled import retired with the flip, but the\n * MANUAL import survives as the rollback-window converge tool — and run out of\n * habit it would overwrite authoritative Turso rows with the frozen Airtable\n * archive, including any post-flip write whose best-effort shadow was\n * swallowed. So the writing actions refuse under the freeze unless the\n * operator says `--force`. `parity` stays unguarded: it only compares, and\n * \"did the shadow drift?\" is exactly the rollback-window question.\n *\n * Pure and exported so the test injects BOTH switch states; `runDbCommand`\n * passes the shipped constant. Returns the refusal, or null to proceed. */\nexport function freezeGuardsDbWrite(\n action: string,\n force: boolean,\n authoritative: boolean,\n): { output: string; code: number } | null {\n if (!authoritative) return null;\n if (action !== \"import-airtable\" && action !== \"sync\") return null;\n if (force) return null;\n return {\n output:\n `db ${action} refused: TURSO_IS_AUTHORITATIVE is on (the freeze, 2026-08-31). ` +\n `An import now OVERWRITES authoritative Turso rows with the frozen Airtable ` +\n `archive. Pass --force only for a deliberate rollback-window converge.`,\n code: 1,\n };\n}\n\n/** `db <action>` — migrate | replay-deadletters | import-airtable | parity | sync | dump | verify-dump. The db layer is imported\n * dynamically so a non-db CLI invocation (and `--help`) never loads\n * @libsql/client. Config is resolved inside each branch so an unknown action\n * returns without needing any Turso env. */\nexport async function runDbCommand(\n action: string,\n opts: DbCommandOptions,\n deps: DbCommandDeps = {},\n): Promise<{ output: string; code: number }> {\n if (action === \"migrate\") {\n const { readDbConfig } = await import(\"../../db/client.js\");\n const cfg = opts.url ? { url: opts.url } : readDbConfig();\n const { runMigrations } = await import(\"../../db/migrate.js\");\n const { createClient } = await import(\"@libsql/client\");\n const client = createClient(cfg.url === \":memory:\" ? { url: \":memory:\" } : cfg);\n const ran = await runMigrations(client);\n return {\n output: ran.length ? `Applied migrations: ${ran.join(\", \")}` : \"Already up to date.\",\n code: 0,\n };\n }\n\n // Re-run every lead that dead-lettered during a site-lookup outage (#539\n // Phase 0) through the normal ingest pipeline. Exit 1 while any row is STILL\n // owed — still failing, re-ingested but unmarked (MED-10b), or undecodable\n // (MED-10c). The operator should not read the run as \"all leads landed\".\n // Zero rows is a clean 0: nothing owed.\n if (action === \"replay-deadletters\") {\n const { readDbConfig, openDb } = await import(\"../../db/client.js\");\n // #786. `--abandon` is the escape hatch for the queue #785 deliberately\n // stopped draining: a slug that is genuinely dead but still deployed keeps\n // dead-lettering leads, holds this command at exit 1, and leaves a standing\n // CRITICAL cockpit item. Validated BEFORE opening any store, so a missing\n // reason refuses without needing Turso creds.\n if (opts.abandon !== undefined) {\n const reason = (opts.reason ?? \"\").trim();\n if (reason === \"\") {\n return {\n output:\n 'db replay-deadletters --abandon refused: pass --reason \"…\". Abandoning writes ' +\n \"off a client's captured leads, and the record of WHY has to outlive the decision.\",\n code: 1,\n };\n }\n const by = opts.by ?? process.env.OPERATOR_EMAIL ?? \"operator\";\n const db = await openDb(opts.url ? { url: opts.url } : readDbConfig());\n const { abandonDeadLetters } = await import(\"../../db/deadletter.js\");\n // Row ids are minted `dl_…` (newDeadLetterId), and a site slug never is —\n // so the one argument can name either without a second flag.\n const target = opts.abandon.startsWith(\"dl_\") ? { id: opts.abandon } : { slug: opts.abandon };\n const ids = await abandonDeadLetters(db, { ...target, by, reason, now: new Date() });\n const lines = [\n ...ids.map((id) => `abandoned ${id}`),\n `DEADLETTER_ABANDONED target=${opts.abandon} rows=${ids.length} by=${by}`,\n ];\n return { output: lines.join(\"\\n\"), code: 0 };\n }\n const db = await openDb(opts.url ? { url: opts.url } : readDbConfig());\n const { openBase, readAirtableConfig } = await import(\"../../reports/airtable/client.js\");\n const { getWebsiteBySlug } = await import(\"../../reports/airtable/websites.js\");\n const { getSiteBySlug } = await import(\"../../db/fleet-state.js\");\n const { makeLazySiteLookup } = await import(\"../../forms/site-lookup.js\");\n // #645. Recovery now resolves sites through the SAME lookup the live ingest\n // path uses. It used to call the Airtable `getWebsiteBySlug` directly, so\n // post-#643 the two disagreed about what the fleet is: a site created since\n // the freeze was invisible to the replay, and a row only Airtable still held\n // would have attached a recovered lead to a site the system no longer\n // believes in. `openBase` is passed UNCALLED — under the freeze no Airtable\n // credential is read at all, where before a missing PAT refused the whole\n // replay (`readAirtableConfig()` throws) with real leads sitting in the queue.\n const lookupSite = makeLazySiteLookup({\n fromDb: (s) => getSiteBySlug(db, s),\n openAirtable: () => openBase(readAirtableConfig()),\n fromAirtable: (base, s) => getWebsiteBySlug(base, s),\n });\n const {\n createSubmission,\n stampNotified,\n stampFanout,\n findRecentDuplicateSubmissions,\n listRecentSubmissionsForEmail,\n markSubmissionsSpamRetro,\n } = await import(\"../../db/submissions.js\");\n const { makeNotify } = await import(\"../../forms/notify.js\");\n const { classifySpam } = await import(\"../../forms/spam-classifier.js\");\n const { forwardNewsletterToWebhook } = await import(\"../../forms/webhook.js\");\n const { addMailchimpMember, mailchimpTagsFor } = await import(\"../../forms/mailchimp.js\");\n const { defaultResendClient } = await import(\"../../reports/send/resend.js\");\n const { replayDeadLetters } = await import(\"../../forms/replay.js\");\n\n // Same degradation as the ingest handler: an unconfigured Resend key means\n // replayed leads land un-emailed (notify=failed) rather than blocking replay.\n let send = null;\n try {\n send = defaultResendClient().send;\n } catch (err) {\n console.error(`[db] Resend unconfigured; replaying without email: ${String(err)}`);\n }\n\n // Mirrors the production handler's deps minus `deadLetter` (replayDeadLetters\n // forbids and strips it — a throwing lookup must retry, not duplicate) and\n // minus `defer` (a CLI has no post-response phase; the inline tail is fine).\n const result = await replayDeadLetters(db, {\n getWebsiteBySlug: lookupSite,\n createSubmission: (input) => createSubmission(db, input),\n notify: makeNotify(send),\n stampNotified: (id, status, messageId) => stampNotified(db, id, status, messageId),\n now: () => new Date(),\n classifySpam: (n, outcome) =>\n classifySpam({\n name: n.name,\n email: n.email,\n ...(n.message !== undefined ? { message: n.message } : {}),\n formType: n.formType,\n extraFields: n.extraFields,\n turnstile: outcome,\n }),\n findRecentDuplicates: (message, since) =>\n findRecentDuplicateSubmissions(db, message, since.toISOString()),\n listRecentSubmissionsForEmail: (email, since) =>\n listRecentSubmissionsForEmail(db, email, since.toISOString()),\n retroBucket: (ids, reason) => markSubmissionsSpamRetro(db, ids, reason),\n forwardNewsletter: (url, submission, site) =>\n forwardNewsletterToWebhook(url, submission, site),\n addToMailchimp: (site, submission) =>\n addMailchimpMember({\n apiKey: site.mailchimpApiKey ?? \"\",\n audienceId: site.mailchimpAudienceId ?? \"\",\n email: submission.email,\n name: submission.name,\n tags: mailchimpTagsFor(submission.formType),\n }),\n stampFanout: (id, fanoutStatus) => stampFanout(db, id, fanoutStatus),\n });\n\n // MED-10. Four buckets, three of them non-zero-is-not-nothing. `still_failing`\n // is a STANDING condition (a slug awaiting `ensure-site`, or a dead one\n // awaiting `--abandon`) and already has an alarm — the `deadletter` attention\n // item. `unmarked` and `unreadable` are DEFECTS with no other surface at all:\n // this output is the only place either is ever named.\n const lines = [\n ...result.replayed.map(\n (r) => `replayed ${r.id} → ${r.outcome}${r.submissionId ? ` (${r.submissionId})` : \"\"}`,\n ),\n ...result.stillFailing.map((r) => `still failing ${r.id}: ${r.error}`),\n ...result.unmarked.map(\n (r) =>\n `UNMARKED ${r.id}: re-ingested as ${r.submissionId ?? \"(no submission)\"} → ${r.outcome}, ` +\n `but the terminal mark could NOT be written (${r.error}). The row is still queued, so ` +\n `replaying again before it is reconciled will mint a DUPLICATE of this lead.`,\n ),\n ...result.unreadable.map(\n (r) =>\n `UNREADABLE ${r.id} (site '${r.siteSlug}', received ${r.receivedAt}): ${r.error}. ` +\n `The row is untouched — repair the stored JSON, or retire it with ` +\n `\\`db replay-deadletters --abandon ${r.id} --reason \"…\"\\`.`,\n ),\n `DEADLETTER_REPLAY replayed=${result.replayed.length} still_failing=${result.stillFailing.length} unmarked=${result.unmarked.length} unreadable=${result.unreadable.length}`,\n ];\n const owed = result.stillFailing.length + result.unmarked.length + result.unreadable.length > 0;\n return { output: lines.join(\"\\n\"), code: owed ? 1 : 0 };\n }\n\n // Phase 1.3/1.4 of #539. Both read the same two Airtable tables raw (id +\n // fields, no mapRow coercion — the importer's mapping is the authority) and\n // share that mapping, so parity is definitionally checked against what the\n // importer writes.\n if (action === \"import-airtable\" || action === \"parity\" || action === \"sync\") {\n const { TURSO_IS_AUTHORITATIVE } = await import(\"../../db/freeze.js\");\n const refused = freezeGuardsDbWrite(action, opts.force === true, TURSO_IS_AUTHORITATIVE);\n if (refused) return refused;\n const { readDbConfig, openDb } = await import(\"../../db/client.js\");\n const db = await openDb(opts.url ? { url: opts.url } : readDbConfig());\n const { openBase, readAirtableConfig } = await import(\"../../reports/airtable/client.js\");\n const base = openBase(readAirtableConfig());\n const listRaw = async (table: string) =>\n (await base(table).select().all()).map((r) => ({\n id: r.id,\n fields: r.fields as Record<string, unknown>,\n }));\n const io = {\n listWebsiteRecords: () => listRaw(\"Websites\"),\n listReportRecords: () => listRaw(\"Reports\"),\n now: () => new Date(),\n };\n\n if (action === \"import-airtable\") {\n const { importFleetState, formatReapSummary } = await import(\"../../db/import-airtable.js\");\n const summary = await importFleetState(db, {\n ...io,\n // Attachment bodies ride expiring signed URLs; a failed fetch imports the\n // row with rendered_html null and is NAMED in the summary, never silent.\n fetchAttachment: async (url) => {\n try {\n const res = await fetch(url);\n return res.ok ? await res.text() : null;\n } catch {\n return null;\n }\n },\n });\n const lines = [\n `imported ${summary.sites} site(s) → sites/site_health/site_schedule`,\n `imported ${summary.reports} report(s)`,\n ];\n if (summary.renderedHtmlMisses.length > 0) {\n lines.push(\n `⚠ ${summary.renderedHtmlMisses.length} report(s) imported WITHOUT Rendered HTML ` +\n `(fetch failed / URL expired): ${summary.renderedHtmlMisses.join(\", \")}`,\n );\n }\n // The import deletes rows Airtable no longer has, so this one-shot path\n // reports the reap exactly as `db sync` does — same formatter, no second\n // copy to fall out of step.\n lines.push(...formatReapSummary(summary.reaped));\n return { output: lines.join(\"\\n\"), code: 0 };\n }\n\n // Phase 2 backbone (#539): one hourly pass = import (attachment fetches\n // only where the stored row lacks a body) + parity + one retry to absorb\n // the import-read/parity-read race. Exit 1 on persistent mismatch.\n if (action === \"sync\") {\n const { syncFleetState, formatSyncResult } = await import(\"../../db/sync.js\");\n const result = await syncFleetState(db, {\n ...io,\n fetchAttachment: async (url) => {\n try {\n const res = await fetch(url);\n return res.ok ? await res.text() : null;\n } catch {\n return null;\n }\n },\n });\n return {\n output: formatSyncResult(result),\n code: result.parity.mismatches.length > 0 ? 1 : 0,\n };\n }\n\n const { checkFleetParity, formatParityResult } = await import(\"../../db/parity.js\");\n const result = await checkFleetParity(db, io);\n return { output: formatParityResult(result), code: result.mismatches.length > 0 ? 1 : 0 };\n }\n\n // One-shot completion of design D5 (#539 Phase 2): copy every site's CURRENT\n // Airtable \"Header image\" attachment into sites.header_image*. Idempotent —\n // an already-populated BLOB is never overwritten (a re-run must not clobber\n // a freshly generated image with a stale Airtable copy). Exit 1 when any\n // fetch failed, so a partial backfill is never read as complete.\n if (action === \"backfill-header-images\") {\n const { readDbConfig, openDb } = await import(\"../../db/client.js\");\n const db = await openDb(opts.url ? { url: opts.url } : readDbConfig());\n const { openBase, readAirtableConfig } = await import(\"../../reports/airtable/client.js\");\n const base = openBase(readAirtableConfig());\n const { backfillHeaderImages, formatBackfillResult } =\n await import(\"../../db/header-images.js\");\n const result = await backfillHeaderImages(db, {\n listWebsiteRecords: async () =>\n (await base(\"Websites\").select().all()).map((r) => ({\n id: r.id,\n fields: r.fields as Record<string, unknown>,\n })),\n fetchBytes: async (url) => {\n try {\n const res = await fetch(url);\n return res.ok ? new Uint8Array(await res.arrayBuffer()) : null;\n } catch {\n return null;\n }\n },\n });\n return { output: formatBackfillResult(result), code: result.failed.length > 0 ? 1 : 0 };\n }\n\n // #609: one-shot copy of the single Airtable \"Digest State\" row into Turso,\n // so the first digest run after the read repoint sees yesterday's snapshot\n // instead of an empty one. An empty read is not a crash — it badges EVERY\n // item NEW, which lands in the operator's inbox reading as \"the whole fleet\n // degraded overnight\". REFUSES to overwrite a snapshot Turso already holds:\n // a re-run must never replace a fresher snapshot with a stale Airtable copy.\n if (action === \"backfill-digest-state\") {\n const { readDbConfig, openDb } = await import(\"../../db/client.js\");\n const db = await openDb(opts.url ? { url: opts.url } : readDbConfig());\n const { readDigestState: readTurso, writeDigestState: writeTurso } =\n await import(\"../../db/digest-state.js\");\n const existing = await readTurso(db);\n if (Object.keys(existing).length > 0) {\n return {\n output: `DIGEST_BACKFILL skipped=1 reason=turso-already-populated keys=${Object.keys(existing).length}`,\n code: 0,\n };\n }\n const { openBase, readAirtableConfig } = await import(\"../../reports/airtable/client.js\");\n const base = openBase(readAirtableConfig());\n const { readDigestState: readAirtable, DIGEST_STATE_TABLE } =\n await import(\"../../alerts/digest-state.js\");\n // `source` separates \"Airtable has no row\" from \"Airtable has a row holding\n // an empty snapshot\" — the reader collapses BOTH to {}, so copied=0 alone\n // cannot tell a quiet fleet from a failed read. Learned by running this: the\n // first real run printed copied=0 and only a hand probe showed the row was\n // there and genuinely empty.\n const rows = await base(DIGEST_STATE_TABLE).select({ maxRecords: 1, pageSize: 1 }).all();\n const snap = await readAirtable(base);\n const keys = Object.keys(snap).length;\n await writeTurso(db, snap);\n // Read it BACK. A returning write is not evidence the row landed — the same\n // rule forms-notify-target learned on 2026-08-03. `stored` counts the ROW,\n // not its keys, so an empty-but-present snapshot verifies as written.\n const stored = (await db.selectFrom(\"digest_state\").selectAll().execute()).length;\n const after = Object.keys(await readTurso(db)).length;\n return {\n output:\n `DIGEST_BACKFILL source=${rows.length > 0 ? \"row\" : \"absent\"} ` +\n `copied=${keys} verified=${after} rows=${stored}`,\n code: after === keys && stored === 1 ? 0 : 1,\n };\n }\n\n // Phase 1.5 of #539: platform-auth-free SQL dump to stdout-adjacent output.\n // The nightly backup workflow redirects this to a file, encrypts, uploads;\n // the rehearsed restore loads it into stock sqlite3 and compares row counts.\n if (action === \"dump\") {\n const { readDbConfig } = await import(\"../../db/client.js\");\n const cfg = opts.url ? { url: opts.url } : readDbConfig();\n const { createClient } = await import(\"@libsql/client\");\n const client = createClient(cfg.url === \":memory:\" ? { url: \":memory:\" } : cfg);\n const { dumpDatabase } = await import(\"../../db/dump.js\");\n const sql = await dumpDatabase(\n {\n execute: async (q) => {\n const r = await client.execute(q);\n return { columns: r.columns, rows: r.rows as Array<Record<string, unknown>> };\n },\n },\n new Date().toISOString(),\n );\n return { output: sql, code: 0 };\n }\n\n // Load a dump back into a REAL libSQL target (#612 review). The nightly\n // rehearsal loads into `:memory:`, which proves the SQL parses — it does not\n // prove you can get the data back into Turso, and that is the operation an\n // actual recovery needs. Replaying ~17 MB of SQL with megabytes of inline hex\n // over HTTP is materially different from an in-process load, and it had never\n // been done. Refuses to touch a database that already holds rows: a restore\n // is for an EMPTY target, and pointing this at production by mistake should\n // cost nothing.\n if (action === \"restore\") {\n const file = opts.file;\n if (!file) return { output: \"restore: pass the dump path via --file\", code: 1 };\n if (!opts.url) {\n return {\n output:\n \"restore: pass the TARGET database via --url (never defaults, to keep production out of reach)\",\n code: 1,\n };\n }\n // Classify the target BEFORE reading the dump, so a missing token names\n // itself instead of arriving as an opaque 401 (or, worse, as an ENOENT that\n // sends you hunting for the dump file). This command built its client from\n // a url alone until 2026-08-26, which worked against every target the tests\n // and rehearsals used — `:memory:` and a local `turso dev` — and failed\n // against every target an actual recovery has.\n const { parseDumpManifest, requiresAuthToken } = await import(\"../../db/dump.js\");\n const authToken = deps.restoreAuthToken ?? process.env.TURSO_RESTORE_AUTH_TOKEN ?? \"\";\n if (requiresAuthToken(opts.url) && !authToken) {\n return { output: \"RESTORE refused=auth-token-absent\", code: 1 };\n }\n const { readFile } = await import(\"node:fs/promises\");\n const sql = await readFile(file, \"utf-8\");\n const manifest = parseDumpManifest(sql);\n if (!manifest) return { output: \"RESTORE refused=manifest-absent\", code: 1 };\n const { createClient } = await import(\"@libsql/client\");\n const target = createClient(authToken ? { url: opts.url, authToken } : { url: opts.url });\n const existing = await target.execute(\n \"SELECT COUNT(*) AS n FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'\",\n );\n if (Number(existing.rows[0]?.n ?? 0) > 0) {\n return { output: \"RESTORE refused=target-not-empty\", code: 1 };\n }\n await target.executeMultiple(sql);\n const { tableCounts, headerImageBytes } = await import(\"../../db/dump.js\");\n const exec = {\n execute: async (q: string) => {\n const r = await target.execute(q);\n return { columns: r.columns, rows: r.rows as Array<Record<string, unknown>> };\n },\n };\n const counts = await tableCounts(exec);\n const bytes = await headerImageBytes(exec);\n // Same origin-anchored comparison as verify-dump: a restore that \"succeeded\"\n // with fewer rows than the origin held is not a restore.\n const bad: string[] = [];\n for (const [t, want] of Object.entries(manifest.tables)) {\n if ((counts[t] ?? 0) !== want) bad.push(`${t}: origin=${want} restored=${counts[t] ?? 0}`);\n }\n if (bytes !== manifest.blobBytes)\n bad.push(`header_image bytes: origin=${manifest.blobBytes} restored=${bytes}`);\n const rows = Object.values(counts).reduce((a, b) => a + b, 0);\n return {\n output: [\n ...bad.map((b) => `✗ ${b}`),\n `RESTORE loaded=true tables=${Object.keys(counts).length} rows=${rows} blob_bytes=${bytes} mismatches=${bad.length}`,\n ].join(\"\\n\"),\n code: bad.length > 0 ? 1 : 0,\n };\n }\n\n // How much of the plan's quota the fleet has burned this billing cycle\n // (#539 HIGH-10). The starter plan carries `overages: false`, so crossing a\n // quota BLOCKS reads and writes rather than billing for them — and once the\n // Airtable cutover lands, Turso is the only store there is. This is the one\n // alarm that fires before a wall rather than after it.\n //\n // Needs a PLATFORM token, which is a different credential from the\n // database-level TURSO_AUTH_TOKEN the rest of the fleet runs on: the database\n // token cannot read quota state at all.\n if (action === \"usage\") {\n const token = deps.platformToken ?? process.env.TURSO_FLEET_USAGE ?? \"\";\n // An unconfigured alarm must not read as a quiet, healthy one. The tell for\n // \"never ran\" has to be a failure, not a missing line (#585).\n if (!token) {\n return {\n output:\n \"FLEET_DB_USAGE verdict=no-token — set TURSO_FLEET_USAGE (turso auth api-tokens mint …). \" +\n \"This is the PLATFORM token, not the database TURSO_AUTH_TOKEN.\",\n code: 1,\n };\n }\n const { collectUsage, assessUsage } = await import(\"../../db/usage.js\");\n const input = await collectUsage({\n token,\n org: opts.org ?? process.env.TURSO_ORG,\n fetchImpl: deps.fetchImpl,\n now: deps.now ?? new Date(),\n });\n const r = assessUsage(input);\n const window = `${input.cycleStart.toISOString().slice(0, 10)} → ${input.cycleEnd\n .toISOString()\n .slice(0, 10)}`;\n return {\n output: [`Turso plan=${input.plan} billing cycle ${window}`, ...r.lines, \"\", r.marker].join(\n \"\\n\",\n ),\n code: r.code,\n };\n }\n\n // The restore rehearsal (Phase 1.5's hard gate), runnable every night: load\n // the dump into a FRESH in-memory engine and compare what came back against\n // the ORIGIN MANIFEST the dump carries.\n //\n // It used to compare against INSERT counts parsed out of the dump text — i.e.\n // the dump against itself. Both sides derived from one artifact, so a dump\n // that collected 5 of 44 sites shrank both numbers together and verified\n // clean. The manifest is read from the live database before any row is\n // serialised, which is the only measurement that can notice a short dump.\n //\n // A dump that cannot restore is not a backup — and per the repo's instrument\n // rule the check emits its machine line on every run, clean included.\n if (action === \"verify-dump\") {\n const file = opts.file;\n if (!file) return { output: \"verify-dump: pass the dump path via --file\", code: 1 };\n const { readFile } = await import(\"node:fs/promises\");\n const sql = await readFile(file, \"utf-8\");\n const { createClient } = await import(\"@libsql/client\");\n const scratch = createClient({ url: \":memory:\" });\n try {\n await scratch.executeMultiple(sql);\n } catch (err) {\n return { output: `DUMP_VERIFY loaded=false error=${String(err)}`, code: 1 };\n }\n const { tableCounts, headerImageBytes, parseDumpManifest } = await import(\"../../db/dump.js\");\n const manifest = parseDumpManifest(sql);\n if (!manifest) {\n // Refuse rather than fall back to self-comparison. Falling back would\n // re-enable exactly the blind spot the manifest exists to close, and it\n // would do so silently on the one artifact nobody was watching.\n return {\n output: \"DUMP_VERIFY loaded=true manifest=absent — dump predates the origin manifest\",\n code: 1,\n };\n }\n const exec = {\n execute: async (q: string) => {\n const r = await scratch.execute(q);\n return { columns: r.columns, rows: r.rows as Array<Record<string, unknown>> };\n },\n };\n const restored = await tableCounts(exec);\n const restoredBlobBytes = await headerImageBytes(exec);\n const mismatches: string[] = [];\n for (const [table, want] of Object.entries(manifest.tables)) {\n if ((restored[table] ?? 0) !== want) {\n mismatches.push(`${table}: origin=${want} restored=${restored[table] ?? 0}`);\n }\n }\n // A table the origin held and the dump never mentioned would otherwise be\n // invisible: absent from `restored` AND absent from the loop above.\n for (const table of Object.keys(restored)) {\n if (!(table in manifest.tables)) mismatches.push(`${table}: not in origin manifest`);\n }\n // Coverage: every table the APP owns must be present. `tables=N` used to be\n // printed and never asserted, so a table a migration failed to create — or\n // that the dump lost — rode green forever. `digest_state` and\n // `prospect_audits` were both absent from every artifact the night this was\n // found, purely because they postdated the last run.\n const { DATABASE_TABLES } = await import(\"../../db/schema.js\");\n for (const table of DATABASE_TABLES) {\n if (!(table in restored)) mismatches.push(`${table}: MISSING from the backup entirely`);\n }\n if (restoredBlobBytes !== manifest.blobBytes) {\n mismatches.push(\n `header_image bytes: origin=${manifest.blobBytes} restored=${restoredBlobBytes}`,\n );\n }\n const total = Object.values(restored).reduce((a, b) => a + b, 0);\n const lines = [\n ...mismatches.map((m) => `✗ ${m}`),\n `DUMP_VERIFY loaded=true tables=${Object.keys(restored).length} rows=${total} blob_bytes=${restoredBlobBytes} mismatches=${mismatches.length}`,\n ];\n return { output: lines.join(\"\\n\"), code: mismatches.length > 0 ? 1 : 0 };\n }\n\n return {\n output: `unknown db action '${action}'. Use: migrate, replay-deadletters, import-airtable, parity, sync, backfill-header-images, backfill-digest-state, dump, verify-dump, restore.`,\n code: 1,\n };\n}\n"],"mappings":";AAiDO,SAAS,oBACd,QACA,OACA,eACyC;AACzC,MAAI,CAAC,cAAe,QAAO;AAC3B,MAAI,WAAW,qBAAqB,WAAW,OAAQ,QAAO;AAC9D,MAAI,MAAO,QAAO;AAClB,SAAO;AAAA,IACL,QACE,MAAM,MAAM;AAAA,IAGd,MAAM;AAAA,EACR;AACF;AAMA,eAAsB,aACpB,QACA,MACA,OAAsB,CAAC,GACoB;AAC3C,MAAI,WAAW,WAAW;AACxB,UAAM,EAAE,aAAa,IAAI,MAAM,OAAO,sBAAoB;AAC1D,UAAM,MAAM,KAAK,MAAM,EAAE,KAAK,KAAK,IAAI,IAAI,aAAa;AACxD,UAAM,EAAE,cAAc,IAAI,MAAM,OAAO,uBAAqB;AAC5D,UAAM,EAAE,aAAa,IAAI,MAAM,OAAO,gBAAgB;AACtD,UAAM,SAAS,aAAa,IAAI,QAAQ,aAAa,EAAE,KAAK,WAAW,IAAI,GAAG;AAC9E,UAAM,MAAM,MAAM,cAAc,MAAM;AACtC,WAAO;AAAA,MACL,QAAQ,IAAI,SAAS,uBAAuB,IAAI,KAAK,IAAI,CAAC,KAAK;AAAA,MAC/D,MAAM;AAAA,IACR;AAAA,EACF;AAOA,MAAI,WAAW,sBAAsB;AACnC,UAAM,EAAE,cAAc,OAAO,IAAI,MAAM,OAAO,sBAAoB;AAMlE,QAAI,KAAK,YAAY,QAAW;AAC9B,YAAM,UAAU,KAAK,UAAU,IAAI,KAAK;AACxC,UAAI,WAAW,IAAI;AACjB,eAAO;AAAA,UACL,QACE;AAAA,UAEF,MAAM;AAAA,QACR;AAAA,MACF;AACA,YAAM,KAAK,K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UAAM,EAAE,gBAAgB,IAAI,MAAM,OAAO,sBAAoB;AAC7D,eAAW,SAAS,iBAAiB;AACnC,UAAI,EAAE,SAAS,UAAW,YAAW,KAAK,GAAG,KAAK,oCAAoC;AAAA,IACxF;AACA,QAAI,sBAAsB,SAAS,WAAW;AAC5C,iBAAW;AAAA,QACT,8BAA8B,SAAS,SAAS,aAAa,iBAAiB;AAAA,MAChF;AAAA,IACF;AACA,UAAM,QAAQ,OAAO,OAAO,QAAQ,EAAE,OAAO,CAAC,GAAG,MAAM,IAAI,GAAG,CAAC;AAC/D,UAAM,QAAQ;AAAA,MACZ,GAAG,WAAW,IAAI,CAAC,MAAM,UAAK,CAAC,EAAE;AAAA,MACjC,kCAAkC,OAAO,KAAK,QAAQ,EAAE,MAAM,SAAS,KAAK,eAAe,iBAAiB,eAAe,WAAW,MAAM;AAAA,IAC9I;AACA,WAAO,EAAE,QAAQ,MAAM,KAAK,IAAI,GAAG,MAAM,WAAW,SAAS,IAAI,IAAI,EAAE;AAAA,EACzE;AAEA,SAAO;AAAA,IACL,QAAQ,sBAAsB,MAAM;AAAA,IACpC,MAAM;AAAA,EACR;AACF;","names":["db","lines","result"]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/db/dump.ts"],"sourcesContent":["/**\n * Phase 1.5 of #539: a platform-auth-free SQL dump of the whole database.\n *\n * `turso db dump` needs a PLATFORM login (browser OAuth) — a nightly workflow\n * has only the DATABASE-level url+token it already holds for every other job.\n * So the dump speaks plain SQL through the same client: schema straight from\n * `sqlite_master`, then every row as an INSERT. The output loads into stock\n * `sqlite3` (libSQL IS SQLite), which is exactly how the rehearsed restore\n * proves it — and how a real disaster would replay it into a fresh database.\n *\n * Determinism: tables and rows are emitted in stable order (name, then rowid)\n * so two dumps of an unchanged database are byte-identical — a diffable backup.\n */\n\n/** Minimal execute surface: the @libsql/client `execute` we need. Injectable so\n * tests run against :memory: without the real network client. */\nexport type SqlExecutor = {\n execute: (sql: string) => Promise<{\n columns: string[];\n rows: Array<Record<string, unknown>>;\n }>;\n};\n\nconst IDENT = /^[A-Za-z_][A-Za-z0-9_]*$/;\n\nfunction quoteIdent(name: string): string {\n return IDENT.test(name) ? name : `\"${name.replaceAll('\"', '\"\"')}\"`;\n}\n\nexport function sqlLiteral(v: unknown): string {\n if (v === null || v === undefined) return \"NULL\";\n if (typeof v === \"number\") return Number.isFinite(v) ? String(v) : \"NULL\";\n if (typeof v === \"bigint\") return v.toString();\n if (v instanceof Uint8Array || v instanceof ArrayBuffer) {\n const bytes = v instanceof ArrayBuffer ? new Uint8Array(v) : v;\n let hex = \"\";\n for (const b of bytes) hex += b.toString(16).padStart(2, \"0\");\n return `X'${hex}'`;\n }\n const str = String(v);\n // A NUL cannot ride a single-quoted SQL literal. This used to strip them,\n // justified as \"they cannot legitimately appear in this schema's TEXT\n // columns\" — but `submissions` free text is attacker-supplied and SQLite\n // stores NUL inside TEXT quite happily, so the backup would have silently\n // differed from the origin with no signal. Emit the whole value as a hex blob\n // cast back to text instead: lossless, and it round-trips through the\n // rehearsal like any other literal.\n if (str.includes(\"\\u0000\")) {\n const bytes = new TextEncoder().encode(str);\n let hex = \"\";\n for (const b of bytes) hex += b.toString(16).padStart(2, \"0\");\n return `CAST(X'${hex}' AS TEXT)`;\n }\n // Strings: standard SQL escaping — double the single quotes.\n return `'${str.replaceAll(\"'\", \"''\")}'`;\n}\n\n/**\n * Dump schema + data as executable SQL. Skips SQLite's internal tables\n * (`sqlite_*`); includes indexes. Wrapped in a transaction so a partial load\n * fails atomically instead of leaving a half-restored scratch that could be\n * mistaken for a good one.\n */\nexport async function dumpDatabase(db: SqlExecutor, generatedAt: string): Promise<string> {\n // Read the ORIGIN's own numbers BEFORE serialising anything. This is the\n // whole point: it is the only measurement that does not come from the dump,\n // so it is the only one that can notice the dump is short.\n const manifest: DumpManifest = {\n tables: await tableCounts(db),\n blobBytes: await headerImageBytes(db),\n generatedAt,\n };\n const out: string[] = [\n // First line, so a truncated dump still carries what it CLAIMED to hold.\n `${MANIFEST_PREFIX}${JSON.stringify(manifest)}`,\n \"PRAGMA foreign_keys=OFF;\",\n \"BEGIN TRANSACTION;\",\n ];\n\n const schema = await db.execute(\n \"SELECT name, type, sql FROM sqlite_master \" +\n \"WHERE sql IS NOT NULL AND name NOT LIKE 'sqlite_%' \" +\n \"ORDER BY CASE type WHEN 'table' THEN 0 ELSE 1 END, name\",\n );\n\n const tables: string[] = [];\n for (const row of schema.rows) {\n out.push(`${String(row.sql).trim().replace(/;?$/, \"\")};`);\n if (row.type === \"table\") tables.push(String(row.name));\n }\n\n for (const table of tables) {\n const data = await db.execute(`SELECT * FROM ${quoteIdent(table)} ORDER BY rowid`);\n if (data.rows.length === 0) continue;\n const cols = data.columns.map(quoteIdent).join(\", \");\n for (const row of data.rows) {\n const values = data.columns.map((c) => sqlLiteral(row[c])).join(\", \");\n out.push(`INSERT INTO ${quoteIdent(table)} (${cols}) VALUES (${values});`);\n }\n }\n\n out.push(\"COMMIT;\");\n return out.join(\"\\n\") + \"\\n\";\n}\n\n/** Marker for the origin manifest line, first line of every dump. */\nexport const MANIFEST_PREFIX = \"-- REDDOOR_DUMP_MANIFEST \";\n\n/** What the ORIGIN database held at dump time.\n *\n * This exists because the restore rehearsal used to compare the dump against\n * ITSELF: expected counts were parsed out of the dump text, and actual counts\n * came from loading that same text. Both sides moved together, so a dump that\n * collected 5 of 44 sites verified clean. A manifest read from the LIVE\n * database before any rows are serialised is the only thing that can catch\n * under-dumping.\n *\n * `blobBytes` is the cheap content check. Row counts alone pass a dump in\n * which every `header_image` came back NULL — and those bytes exist in no\n * other store once Airtable is frozen. */\nexport type DumpManifest = {\n tables: Record<string, number>;\n blobBytes: number;\n generatedAt: string;\n};\n\n/** Total stored header-image bytes, the one content signal cheap enough to\n * check on every nightly run. */\nexport async function headerImageBytes(db: SqlExecutor): Promise<number> {\n const r = await db.execute(\n \"SELECT COALESCE(SUM(LENGTH(header_image)), 0) AS n FROM sites WHERE header_image IS NOT NULL\",\n );\n return Number(r.rows[0]?.n ?? 0);\n}\n\n/** Parse the manifest line from a dump, or null when the dump predates it. */\nexport function parseDumpManifest(sql: string): DumpManifest | null {\n const line = sql.split(\"\\n\").find((l) => l.startsWith(MANIFEST_PREFIX));\n if (!line) return null;\n try {\n return JSON.parse(line.slice(MANIFEST_PREFIX.length)) as DumpManifest;\n } catch {\n return null;\n }\n}\n\n/** Per-table row counts — the cheap integrity check the restore rehearsal\n * compares across original vs restored. */\nexport async function tableCounts(db: SqlExecutor): Promise<Record<string, number>> {\n const schema = await db.execute(\n \"SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name\",\n );\n const counts: Record<string, number> = {};\n for (const row of schema.rows) {\n const name = String(row.name);\n const r = await db.execute(`SELECT COUNT(*) AS c FROM ${quoteIdent(name)}`);\n counts[name] = Number(r.rows[0]?.c ?? 0);\n }\n return counts;\n}\n\n/** Hosts that serve libSQL without authentication: `turso dev`, a local file,\n * and the in-process scratch engine the nightly rehearsal loads into. */\n// `[::1]` keeps its brackets: WHATWG URL reports an IPv6 hostname bracketed,\n// so the bare form alone would classify a local IPv6 target as hosted.\nconst LOCAL_HOSTS = new Set([\"localhost\", \"127.0.0.1\", \"[::1]\", \"::1\"]);\n\n/** Does this restore target need an auth token?\n *\n * `db restore` used to build its client from a url alone, which works against\n * every target the tests and rehearsals had ever used — `:memory:` and a local\n * `turso dev` — and 401s against every target a real recovery would have.\n * Classifying the url lets the command refuse with a named reason before the\n * network instead of surfacing an opaque SERVER_ERROR.\n *\n * Fails CLOSED: a url we cannot parse is treated as needing a token, because\n * the alternative is silently skipping auth and getting the 401 anyway. */\nexport function requiresAuthToken(url: string): boolean {\n if (url === \":memory:\") return false;\n let parsed: URL;\n try {\n parsed = new URL(url);\n } catch {\n return true;\n }\n if (parsed.protocol === \"file:\") return false;\n return !LOCAL_HOSTS.has(parsed.hostname);\n}\n"],"mappings":";AAuBA,IAAM,QAAQ;AAEd,SAAS,WAAW,MAA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