Files
loki/scripts/test/migration-journal-coverage.ts
CatoandClaude Opus 5 a02d31d996 Every public claim matches the code, and two gates can now fail (#802)
* fix(truth): every public claim now matches what the code and the licence say

The /license page forbade what LICENSE grants. #791 relicensed the repo to MIT
and fixed LICENSE, the README and the Terms — but missed the page, which still
said "source-available", listed hosting and repackaging under "you may not",
and promised a replacement licence that had already shipped. On a public repo,
that revoked rights we had granted.

Same shape, five more places:

- Three live /thoughts pages and four front-matters carried "Mao Nakamoto",
  the retired identity, into the rendered byline and the OG social card.
- legal/ named that identity as operator, data controller and IP assignor, and
  assigned the work to bitbaum AG / orangecat GmbH / loki GmbH — none of which
  were ever incorporated. Rewritten as a record of authorship and the MIT
  grant, which is what is actually true.
- The desktop About dialog and the installer copyright metadata shipped
  "© 2026 Mao Nakamoto" in every build. No audit pass had looked there.
- /pricing and the feedback-widget docs said the report loop "closes itself"
  months after close-loop.ts deliberately removed auto-resolve for being
  untrue; resolve is an operator act and has zero call sites.
- /roadmap claimed signed installers under "Shipping now"; no signing secret
  exists and /download says the opposite two pages away.
- /releases claimed "every shipped version" while missing nine, and listed a
  0.8.25 that was never tagged — its "Binaries →" link 404'd. Removed per the
  file's own rule: one entry per tag pushed to loki-releases.
- desktop/README pointed all five install commands at bitbaum/loki, which
  404s; releases publish to loki-releases. Version pin replaced with a glob so
  it cannot rot again.
- DEFAULT_USER_NAME greeted nameless OAuth users as "Mao".

And the two that lied to agents rather than users: CLAUDE.md claimed pre-push
runs the full verify bundle so "green locally means green in CI", and
AGENTS.md claimed pre-commit runs tsc. The hook's own header documents why it
stopped doing both — a docs-only push runs nothing. An agent trusting either
line would skip the verification it needed.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* fix(gates): close the two gaps that let deleted things keep passing

Two gates could not fail, and one migration ledger had silently drifted.

MIGRATION JOURNAL — 13 files missing, next db:generate breaks the deploy.
drizzle/ held 78 .sql files; meta/_journal.json had 65 entries. Production
was fine and always had been: apply-schema.sh globs the directory and keeps
its own _deploy_schema_history, so it never reads the journal. But
drizzle-kit does, and its newest snapshot was 0072 — so the next generate
would re-emit 0073-0077, including CREATE TABLE "calendar_busy" with no
IF NOT EXISTS. apply-schema.sh rewrites ADD COLUMN only, and the batch is one
transaction, so the deploy would abort on a change unrelated to it.

Reconciled to 78 via bootstrap-migration-ledger.ts --write-journal, and added
scripts/test/migration-journal-coverage.ts so it cannot drift again: files vs
journal in both directions, plus idx contiguity. Proven by mutation — drop one
entry and it exits 1; restore it and it exits 0.

SMOKE — every authenticated probe passed whether or not the route existed.
The auth middleware answers 401 before routing, and check_route accepted 401
for everything in AUTH_API_ROUTES. Measured: /api/definitely-not-a-real-route
returns 401 exactly like /api/orgs. So the list proved nothing, and
/api/checkout/personal sat in it for 15 days after the Stripe rail was
removed. A 401 now only passes if a route file actually resolves on disk
(dynamic and catch-all segments included); the dead entry is gone, and all
remaining probes resolve.

Also: bumping desktop/package.json to 0.8.29, because the retired-identity fix
in the previous commit touched desktop/src/main/index.ts and desktop-release-
drift.ts correctly refused to let a shipped file change without a release to
carry it. That gate was right and is the model for the two above.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-09-20 08:40:34 -07:00

81 lines
3.2 KiB
TypeScript

/**
* Every migration FILE is in the journal, and every journal tag has a FILE.
* Run: npx tsx scripts/test/migration-journal-coverage.ts
*
* The class this closes: `drizzle/meta/_journal.json` is the only thing
* `drizzle-kit generate` reads to know what already exists. The box applier
* (`scripts/hetzner/apply-schema.sh`) does NOT read it — it globs the
* directory and keeps its own `public._deploy_schema_history`. So a migration
* can be written, applied to production, and serve traffic for months while
* the journal never learns about it. Nothing fails, nothing warns.
*
* It surfaces on the NEXT `pnpm db:generate`, which re-emits every migration
* the journal is missing. The re-emitted `CREATE TABLE` carries no
* IF NOT EXISTS (drizzle-kit never emits it), apply-schema.sh only rewrites
* ADD COLUMN, and the batch is one transaction — so the deploy aborts with
* `relation "..." already exists` on a change that had nothing to do with it.
*
* Measured on 2026-09-20: 78 files, 65 journal entries, 13 missing — drifted
* across ~5 weeks of migrations with prod perfectly healthy the whole time.
*
* The reverse direction matters too: a journal tag with no file means the
* snapshot chain references a migration that is gone, and generate will
* produce a diff against a history that cannot be replayed.
*
* Fix when this fails:
* npx tsx scripts/db/bootstrap-migration-ledger.ts --write-journal
*/
import assert from "node:assert/strict";
import { readdirSync, readFileSync } from "node:fs";
const MIG_DIR = "drizzle";
const JOURNAL = "drizzle/meta/_journal.json";
type JournalEntry = { idx: number; tag: string };
const journal = JSON.parse(readFileSync(JOURNAL, "utf8")) as { entries: JournalEntry[] };
const fileTags = readdirSync(MIG_DIR)
.filter((f) => /^\d+_.*\.sql$/.test(f))
.map((f) => f.slice(0, -4))
.sort();
const journalTags = journal.entries.map((e) => e.tag).sort();
const missingFromJournal = fileTags.filter((t) => !journalTags.includes(t));
const orphanJournalTags = journalTags.filter((t) => !fileTags.includes(t));
assert.deepEqual(
missingFromJournal,
[],
`${missingFromJournal.length} migration file(s) are not in ${JOURNAL}:\n` +
missingFromJournal.map((t) => ` ${t}.sql`).join("\n") +
`\n\nThe next \`pnpm db:generate\` will re-emit these and the deploy will abort.\n` +
`Fix: npx tsx scripts/db/bootstrap-migration-ledger.ts --write-journal`,
);
assert.deepEqual(
orphanJournalTags,
[],
`${orphanJournalTags.length} journal tag(s) have no .sql file:\n` +
orphanJournalTags.map((t) => ` ${t}`).join("\n") +
`\n\nThe migration history cannot be replayed. Restore the file or rebuild` +
` the journal.`,
);
// idx must be contiguous from 0 or 1 — a gap means an entry was hand-deleted,
// which silently shifts what drizzle-kit believes the latest snapshot covers.
const idxs = journal.entries.map((e) => e.idx).sort((a, b) => a - b);
for (let i = 1; i < idxs.length; i++) {
assert.equal(
idxs[i],
idxs[i - 1]! + 1,
`journal idx is not contiguous: ${idxs[i - 1]} → ${idxs[i]}. An entry was` +
` removed by hand; rebuild with --write-journal.`,
);
}
console.log(
`✓ migration journal: ${fileTags.length} files, ${journalTags.length} journal entries, idx contiguous`,
);