Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

feat(db): resolve comment-only migration drift with db:restamp instead of a standoff

nirholasd6546237480a
diff --git a/CLAUDE.md b/CLAUDE.md
index b5625eb4f..28e946acf 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -34,7 +34,7 @@ Agents stopping mid-task to ask a question is a failure mode the owner explicitl
| Missing env var / credential | Look in `.env` and `.env.local`, then the Cloud Run service (`gcloud run services describe three-ws-api --region us-central1 --project aerial-vehicle-466722-p5 --format=yaml`), then Secret Manager (`gcloud secrets list`). Never trust `vercel env pull` (returns empty for secrets). Update single vars with `--update-env-vars` (merges); `--set-env-vars` REPLACES the whole set, so never use it for one key. If a credential truly exists nowhere, build the feature fully wired behind the env var, prove it with a mock-free dry run, and list the single missing var in your report. |
| A QA login for authed page testing | `AUDIT_EMAIL` / `AUDIT_PASSWORD` in `.env` are a real production QA account. Unauthenticated sweep: `npm run audit:web`. Authed sweep: `npm run audit:web:login`. |
| GCP access / project facts | Already authenticated in this workspace. Project `aerial-vehicle-466722-p5`, region `us-central1`. Fleet + quota + pre-approved scaling: `docs/ops/gcp-credits-plan.md`. Full production runbook (LB/DNS/TLS/env/rollback): `docs/ops/gcp-production.md`. Logs: `gcloud logging read 'resource.type="cloud_run_revision" resource.labels.service_name="three-ws-api" textPayload:"<term>"' --freshness=24h`. |
-| The database | `DATABASE_URL` in `.env.local` (Neon); `.env` currently holds only the QA audit login, so a script that loads `.env` alone dies on `missing required env var: DATABASE_URL`. Production's authoritative copy is on the Cloud Run service env. Migrations: `npm run db:status` previews pending ones, `npm run db:check` is the deploy gate (exit 4 if pending), and **`npm run db:migrate` APPLIES immediately, with no dry run** (the npm script hardcodes `--apply`). Read `db:status` before you ever run `db:migrate`; it applies every pending migration in `api/_lib/migrations/`, not just yours. `forge_creations` carries per-generation backend/status/error/prompt: the fastest ground truth for generation issues. |
+| The database | `DATABASE_URL` in `.env.local` (Neon); `.env` currently holds only the QA audit login, so a script that loads `.env` alone dies on `missing required env var: DATABASE_URL`. Production's authoritative copy is on the Cloud Run service env. Migrations: `npm run db:status` previews pending ones, `npm run db:check` is the deploy gate (exit 4 if pending), and **`npm run db:migrate` APPLIES immediately, with no dry run** (the npm script hardcodes `--apply`). Read `db:status` before you ever run `db:migrate`; it applies every pending migration in `api/_lib/migrations/`, not just yours. If `db:status` reports `[drift]` because an applied migration's *comments* were edited (the banned-dash rule does this), `npm run db:restamp` re-records its hash after recovering the applied bytes from git and confirming not one statement changed; a real statement change is refused and needs a NEW migration. `forge_creations` carries per-generation backend/status/error/prompt: the fastest ground truth for generation issues. |
| GCP quota hit | File the increase request immediately (`gcloud` or console link in your report), then route around it: lower minScale on an idle service, use another region, or queue behind existing capacity. Never park the task on the quota. |
| Third-party API down / throttled | Every lane has a failover chain (forge lanes, LLM chain, RPC providers). Use it. If a chain is missing a rung, adding one is part of the task. |
| Ambiguous product decision | Pick the option that is most reversible and closest to existing platform patterns, implement it, and record the decision + alternative in your report. A shipped reasonable default beats a stalled question. |
diff --git a/package.json b/package.json
index f79acb02c..72c6b67df 100644
--- a/package.json
+++ b/package.json
@@ -338,6 +338,7 @@
"db:migrate": "node scripts/apply-migrations.mjs --apply",
"db:status": "node scripts/apply-migrations.mjs",
"db:check": "node scripts/apply-migrations.mjs --check",
+ "db:restamp": "node scripts/apply-migrations.mjs --restamp",
"worker:oracle": "node --env-file-if-exists=.env workers/oracle/index.js",
"worker:sniper": "node workers/agent-sniper/index.js",
"worker:sniper:live": "SNIPER_MODE=live node workers/agent-sniper/index.js",
diff --git a/scripts/apply-migrations.mjs b/scripts/apply-migrations.mjs
index f3e14d77b..30eabadd5 100755
--- a/scripts/apply-migrations.mjs
+++ b/scripts/apply-migrations.mjs
@@ -9,6 +9,7 @@
* node scripts/apply-migrations.mjs --check # like the dry run, but exit 4 if anything is pending (deploy gate)
* node scripts/apply-migrations.mjs --apply # apply pending migrations
* node scripts/apply-migrations.mjs --apply --file 2026-04-29-onchain-unified.sql
+ * node scripts/apply-migrations.mjs --restamp --file <drifted file> # comment-only edit, see below
*
* Tracking: each applied migration is recorded in `schema_migrations`
* (filename + sha256 + applied_at). Re-running is a no-op for already-applied
@@ -20,6 +21,7 @@
import { readFile, readdir } from 'node:fs/promises';
import { readFileSync } from 'node:fs';
import { createHash } from 'node:crypto';
+import { execFileSync } from 'node:child_process';
import { fileURLToPath } from 'node:url';
import path from 'node:path';
import { neon, Pool, neonConfig } from '@neondatabase/serverless';
@@ -51,6 +53,7 @@ for (const envFile of ['.env.local', '.env']) {
const args = process.argv.slice(2);
const APPLY = args.includes('--apply');
const CHECK = args.includes('--check');
+const RESTAMP = args.includes('--restamp');
const fileArgIdx = args.indexOf('--file');
const ONLY = fileArgIdx >= 0 ? args[fileArgIdx + 1] : null;
@@ -87,12 +90,86 @@ async function listPending() {
fname,
body,
hash,
+ prior,
status: !prior ? 'pending' : prior === hash ? 'applied' : 'drift',
});
}
return out;
}
+// Everything in a migration except its whole-line comments: the part a drift
+// check is actually protecting. Only leading `--` lines are dropped, never a
+// trailing comment, because `--` can live inside a string literal and stripping
+// it there would let a real statement change pass as "comments only".
+function schemaBytes(body) {
+ return body
+ .split('\n')
+ .filter((l) => !/^\s*--/.test(l))
+ .map((l) => l.trimEnd())
+ .filter((l) => l.trim() !== '')
+ .join('\n');
+}
+
+// The exact bytes that were applied, recovered from git by hash. The ledger
+// stores only a sha256, so the file's own history is the one place the applied
+// text still exists. Returns null when no committed version matches, which is
+// the honest answer for a file edited before it was ever committed.
+function appliedBytesFromGit(fname, sha256) {
+ const rel = path.posix.join('api/_lib/migrations', fname);
+ let commits;
+ try {
+ commits = execFileSync('git', ['log', '--format=%H', '--', rel], {
+ cwd: REPO_ROOT,
+ encoding: 'utf8',
+ maxBuffer: 32 * 1024 * 1024,
+ })
+ .split('\n')
+ .filter(Boolean);
+ } catch {
+ return null;
+ }
+ for (const commit of commits) {
+ let body;
+ try {
+ body = execFileSync('git', ['show', `${commit}:${rel}`], {
+ cwd: REPO_ROOT,
+ encoding: 'utf8',
+ maxBuffer: 32 * 1024 * 1024,
+ });
+ } catch {
+ continue;
+ }
+ if (createHash('sha256').update(body).digest('hex') === sha256) return { body, commit };
+ }
+ return null;
+}
+
+// Accept a comment-only edit to an already-applied migration by re-recording its
+// hash, instead of demanding the prose be reverted.
+//
+// Drift exists to catch a schema change smuggled into a file Postgres already
+// ran. A reworded comment is not that, and the repo has rules that force such
+// edits (the banned-dash rule rewrote one applied migration's header), so
+// "restore the applied bytes" and "obey the style rules" were a standoff that
+// left `db:check` red and blocked every deploy. This resolves it without
+// weakening the check: the applied text is recovered from git by its recorded
+// hash and compared statement-for-statement, and anything short of an exact
+// match is refused.
+async function restampOne(item) {
+ const found = appliedBytesFromGit(item.fname, item.prior);
+ if (!found) {
+ console.error(` - ${item.fname}: no committed version matches the applied hash, cannot verify. Not restamped.`);
+ return false;
+ }
+ if (schemaBytes(found.body) !== schemaBytes(item.body)) {
+ console.error(` - ${item.fname}: statements differ from what was applied (${found.commit.slice(0, 9)}). Roll forward with a NEW migration.`);
+ return false;
+ }
+ await sql`update schema_migrations set sha256 = ${item.hash} where filename = ${item.fname}`;
+ console.log(` - ${item.fname}: comments only vs ${found.commit.slice(0, 9)}, hash re-recorded.`);
+ return true;
+}
+
async function applyOne({ fname, body, hash }) {
process.stdout.write(`→ applying ${fname} … `);
// Run the whole file in one round trip via the websocket `Pool`. The Neon HTTP
@@ -138,15 +215,28 @@ async function main() {
console.log(` [${i.status.padEnd(7)}] ${i.fname}`);
}
+ if (drift.length && RESTAMP) {
+ console.log(`\nRestamping ${drift.length} drifted migration(s) whose statements are unchanged:`);
+ let done = 0;
+ for (const d of drift) if (await restampOne(d)) done += 1;
+ if (done !== drift.length) {
+ console.error(`\n${drift.length - done} migration(s) could not be restamped. See above.`);
+ process.exit(3);
+ }
+ console.log('Done. Re-run without --restamp to see the status.');
+ return;
+ }
+
if (drift.length) {
console.error(
`\nERROR: ${drift.length} migration(s) have drifted (file changed after apply):`,
);
for (const d of drift) console.error(` - ${d.fname}`);
console.error(
- 'Refusing to proceed. An applied migration is an immutable record of what ran —\n' +
- 'do NOT edit it. If the change was comment-only (schema byte-identical), restore\n' +
- 'the file to its applied bytes: git show <commit-that-applied-it>^:<path> > <path>\n' +
+ 'Refusing to proceed. An applied migration is an immutable record of what ran,\n' +
+ 'so do NOT edit its statements. If the edit was comment-only, re-record the\n' +
+ 'hash: node scripts/apply-migrations.mjs --restamp (it recovers the applied\n' +
+ 'bytes from git and refuses if a single statement differs).\n' +
'If the schema genuinely needs to change, roll forward with a NEW migration.',
);
process.exit(3);