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.

refactor: migrate unstoppable agent tables to new migration path

ni3aa10d7dd007ae
diff --git a/.gitignore b/.gitignore
index e6d9e33bf..f2b69473c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -277,3 +277,4 @@ api/_lib/migrations/20260623170000_agent_bodies.sql
# NVIDIA TRELLIS demo outputs (local only)
demo/
+three-ws-prod-log-export-2026-06-24T04-15-25.json
diff --git a/data/migrations/2026-05-27-unstoppable-agent-tables.sql b/api/_lib/migrations/20260527000000_unstoppable_agent_tables.sql
similarity index 82%
rename from data/migrations/2026-05-27-unstoppable-agent-tables.sql
rename to api/_lib/migrations/20260527000000_unstoppable_agent_tables.sql
index 25fad7d4a..602b63db4 100644
--- a/data/migrations/2026-05-27-unstoppable-agent-tables.sql
+++ b/api/_lib/migrations/20260527000000_unstoppable_agent_tables.sql
@@ -1,6 +1,11 @@
-- Unstoppable Agent tables
-- Provisions treasury, activity log, and daily reflection log for the
-- self-sustaining agent that funds itself through x402 micropayments.
+--
+-- Folded into the api/_lib/migrations runner path so a single `npm run db:migrate`
+-- provisions these tables. (Previously orphaned under data/migrations/, applied by
+-- no script — which left unstoppable_treasury / unstoppable_activity missing in
+-- production and the /api/agents/* + treasury/earnings routes erroring on every tick.)
-- Treasury snapshot — single row, upserted on each tick
CREATE TABLE IF NOT EXISTS unstoppable_treasury (
diff --git a/docs/deployment.md b/docs/deployment.md
index aaca076be..fbc33c365 100644
--- a/docs/deployment.md
+++ b/docs/deployment.md
@@ -170,20 +170,24 @@ The free tier handles early traffic comfortably. When you scale, switch to the p
### 2. Apply the schema
-Three migration scripts apply the schema. All are idempotent — safe to re-run if something fails mid-way.
+One command provisions the **entire** schema — core tables, indexer/delegation state, and every incremental migration — in dependency order. It is idempotent: safe to re-run any time, a no-op for anything already applied.
```bash
-# Core schema — agents, users, widgets, API keys, sessions, usage events
-node scripts/apply-schema.mjs
+npm run db:bootstrap
+```
-# ERC-8004 indexer state — tracks on-chain crawl progress
-node scripts/apply-indexer-schema.js
+This runs, in order:
-# ERC-7710 delegation tables — permission grants and redemptions
-node scripts/apply-delegations-schema.js
-```
+1. `scripts/apply-schema.mjs` — core schema (agents, users, avatars, agent_identities, sessions, widgets, usage_events, …)
+2. `scripts/apply-indexer-schema.js` — ERC-8004 indexer state
+3. `scripts/apply-delegations-schema.js` — ERC-7710 delegation tables
+4. `scripts/apply-migrations.mjs --apply` — every incremental migration under `api/_lib/migrations/` (agent_custody_events, forge_creations, x_triggers, pump_coin_intel, club_tips, unstoppable_*, …)
+
+> **Do not skip step 4.** The base schema alone does **not** create the migration-defined tables. A database that has only had `apply-schema.mjs` run against it will throw `relation "…" does not exist` for dozens of routes (forge, custody, club tips, x-triggers, the unstoppable agent, …). `npm run db:bootstrap` runs all four steps; running them piecemeal is what leaves a half-provisioned database.
+
+Each step reads `DATABASE_URL` from `.env.local` then `.env` then the process environment. If you get a connection error, verify the URL includes `?sslmode=require` — Neon requires SSL.
-Each script reads `DATABASE_URL` from `.env.local` then `.env` then the process environment. If you get a connection error, verify the URL includes `?sslmode=require` — Neon requires SSL.
+To preview pending migrations without writing, run `npm run db:status`.
### Schema overview
@@ -283,8 +287,8 @@ npm install
cp .env.example .env.local
# Edit .env.local — minimum: DATABASE_URL, JWT_SECRET, ANTHROPIC_API_KEY
-# Apply the schema
-node scripts/apply-schema.mjs
+# Apply the full schema (core + indexer + delegations + migrations)
+npm run db:bootstrap
# Start the dev server
npm run dev
diff --git a/package.json b/package.json
index c3107fabe..73606e4b7 100644
--- a/package.json
+++ b/package.json
@@ -169,6 +169,7 @@
"deploy": "npm run build:all && npm run check:dist && vercel --local-config vercel.json --prod",
"postversion": "git push && git push --tags",
"validate:cards": "node scripts/validate-agent-cards.mjs",
+ "db:bootstrap": "node scripts/db-bootstrap.mjs",
"db:migrate": "node scripts/apply-migrations.mjs --apply",
"db:status": "node scripts/apply-migrations.mjs",
"worker:oracle": "node workers/oracle/index.js",
diff --git a/scripts/db-bootstrap.mjs b/scripts/db-bootstrap.mjs
new file mode 100644
index 000000000..0637834df
--- /dev/null
+++ b/scripts/db-bootstrap.mjs
@@ -0,0 +1,103 @@
+#!/usr/bin/env node
+/**
+ * One-command database bootstrap / heal.
+ *
+ * Brings an empty or partially-provisioned Postgres (Neon) database fully up to
+ * date by running every schema step in dependency order. Each step is idempotent
+ * and reads DATABASE_URL itself, so this is safe to re-run any time — on a fresh
+ * production database it provisions the entire schema; on a live one it is a no-op
+ * for everything already applied.
+ *
+ * Order matters:
+ * 1. Core schema (api/_lib/schema.sql) — agents, users, avatars,
+ * agent_identities, sessions,
+ * widgets, usage_events, …
+ * 2. Indexer schema (specs/schema/indexer_state.sql)
+ * 3. Delegation schema(specs/schema/agent_delegations.sql)
+ * 4. Migrations (api/_lib/migrations/*.sql) — every incremental table:
+ * agent_custody_events,
+ * forge_creations, x_triggers,
+ * pump_coin_intel, club_tips,
+ * unstoppable_* …
+ *
+ * Steps 1–3 must precede step 4 because later migrations ALTER tables those base
+ * files create.
+ *
+ * Usage:
+ * DATABASE_URL=postgres://… node scripts/db-bootstrap.mjs # apply everything
+ * npm run db:bootstrap # same, via package.json
+ *
+ * Reads DATABASE_URL from .env.local → .env → process env (same as every step).
+ */
+
+import { spawn } from 'node:child_process';
+import { readFileSync } from 'node:fs';
+import { resolve, dirname } from 'node:path';
+import { fileURLToPath } from 'node:url';
+
+const __dirname = dirname(fileURLToPath(import.meta.url));
+const root = resolve(__dirname, '..');
+
+function loadEnvFile(path) {
+ let raw;
+ try {
+ raw = readFileSync(path, 'utf8');
+ } catch {
+ return;
+ }
+ for (const line of raw.split('\n')) {
+ const m = line.match(/^\s*([A-Z0-9_]+)\s*=\s*(.*?)\s*$/i);
+ if (!m) continue;
+ const [, k, v] = m;
+ if (process.env[k]) continue;
+ process.env[k] = v.replace(/^["']|["']$/g, '');
+ }
+}
+loadEnvFile(resolve(root, '.env.local'));
+loadEnvFile(resolve(root, '.env'));
+
+if (!process.env.DATABASE_URL) {
+ console.error('DATABASE_URL not set. Add it to .env.local or export it before running.');
+ process.exit(1);
+}
+
+const host = (() => {
+ try {
+ return new URL(process.env.DATABASE_URL).host;
+ } catch {
+ return '<DATABASE_URL>';
+ }
+})();
+
+const STEPS = [
+ { name: 'Core schema', cmd: ['node', 'scripts/apply-schema.mjs'] },
+ { name: 'Indexer state', cmd: ['node', 'scripts/apply-indexer-schema.js'] },
+ { name: 'Agent delegations', cmd: ['node', 'scripts/apply-delegations-schema.js'] },
+ { name: 'Migrations', cmd: ['node', 'scripts/apply-migrations.mjs', '--apply'] },
+];
+
+function run({ name, cmd }) {
+ return new Promise((resolveStep, rejectStep) => {
+ console.log(`\n━━ ${name} ━━`);
+ const child = spawn(cmd[0], cmd.slice(1), { cwd: root, stdio: 'inherit', env: process.env });
+ child.on('error', rejectStep);
+ child.on('close', (code) => {
+ if (code === 0) resolveStep();
+ else rejectStep(new Error(`${name} exited with code ${code}`));
+ });
+ });
+}
+
+console.log(`Bootstrapping database schema on ${host} …`);
+
+for (const step of STEPS) {
+ try {
+ await run(step);
+ } catch (err) {
+ console.error(`\n✗ ${err.message}`);
+ console.error('Bootstrap halted. Fix the error above and re-run — completed steps are idempotent.');
+ process.exit(1);
+ }
+}
+
+console.log('\n✓ Database fully bootstrapped — all schema and migrations applied.');