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.

fix(economy): lease-gate economy-reconcile so the 30-min cron cadence is durable

nirholasbe2abd3d481d
diff --git a/api/cron/economy-reconcile.js b/api/cron/economy-reconcile.js
index a9ed33e76..7032a8c9f 100644
--- a/api/cron/economy-reconcile.js
+++ b/api/cron/economy-reconcile.js
@@ -25,6 +25,7 @@ import { json, method, wrapCron } from '../_lib/http.js';
import { sendOpsAlert } from '../_lib/alerts.js';
import { withDbRetry } from '../_lib/db-retry.js';
import { sql } from '../_lib/db.js';
+import { acquireLock } from '../_lib/cache.js';
import { ECONOMY_MASTER_ADDRESS, RESERVE_SOL } from '../_lib/economy-master.js';
import { SOLANA_SIGNERS, resolveSignerPubkey } from '../_lib/solana-signers.js';
import { verifyChain } from '../_lib/economy-ledger.js';
@@ -40,6 +41,19 @@ const LEDGER_VERIFY_WINDOW_HOURS = 72;
const OUTBOUND_FEE_FLOOR_SOL = 0.0005;
const LAMPORTS_PER_SOL = 1_000_000_000;
+// This route is also one of economy-tick.js's ~36 fan-out TARGETS, fired every
+// minute, on top of its own dedicated vercel.json `*/30 * * * *` Cloud
+// Scheduler job, with nothing stopping the two callers from overlapping. Each run
+// does a real on-chain scan (hash-chain verify, up to SIG_SCAN_LIMIT
+// getParsedTransaction calls, a balance read), so without a lease, production
+// queued many overlapping full scans against the Solana RPC every minute,
+// which is exactly what pushed every run past wrapCron's 120s default budget
+// during RPC lane exhaustion (100% `economy-reconcile exceeded its 120000ms
+// budget` in the 2026-10-24 sweep). Mirrors the lease x402-ring-leak-scan.js
+// uses for the identical disease: the TTL IS the cadence, never released.
+const LEASE_KEY = 'cron:economy-reconcile:lease';
+const LEASE_SECONDS = 25 * 60;
+
async function upsertVerdict({ source, sourceRef, txSig, network, amountAtomic, dbStatus, chainStatus, reconciled, discrepancy, detail, runId }) {
try {
await withDbRetry(() => sql`
@@ -296,6 +310,12 @@ export default wrapCron(async (req, res) => {
if (!method(req, res, ['GET', 'POST'])) return;
if (!requireCron(req, res)) return;
+ // One run per cadence window, whoever fires it (the lease is never
+ // released; its TTL IS the cadence), see the comment on LEASE_KEY above.
+ if (!(await acquireLock(LEASE_KEY, LEASE_SECONDS))) {
+ return json(res, 200, { ok: true, skipped: true, reason: 'lease_held' });
+ }
+
const runId = (await import('node:crypto')).randomUUID();
const rpcUrl = process.env.SOLANA_RPC_URL || 'https://api.mainnet-beta.solana.com';
const { solanaConnection } = await import('../_lib/solana/connection.js');
diff --git a/tests/economy-reconcile-lease.test.js b/tests/economy-reconcile-lease.test.js
new file mode 100644
index 000000000..beb38cada
--- /dev/null
+++ b/tests/economy-reconcile-lease.test.js
@@ -0,0 +1,63 @@
+// Cadence-lease test for api/cron/economy-reconcile.js.
+//
+// Production 2026-10-24: economy-tick.js fires economy-reconcile every minute
+// (it is one of ~36 TARGETS in that fan-out) on top of its own dedicated */30
+// Cloud Scheduler job (vercel.json), with nothing to stop the two callers from
+// overlapping. Unlike x402-ring-leak-scan.js (which hit the identical disease
+// and was fixed with a lease, see tests/x402-ring-leak-scan-run.test.js), this
+// handler ran a full on-chain reconciliation (hash-chain verify, up to 200
+// getParsedTransaction calls, a balance read) on every single invocation, so it
+// queued many overlapping full scans against the Solana RPC. During the RPC
+// lane exhaustion windows this is exactly what blew every run past wrapCron's
+// 120s default budget, logging a 100% `[cron] economy-reconcile exceeded its
+// 120000ms budget` storm. A lease that makes the native 30-min cadence durable
+// regardless of how often the route is invoked fixes it the same way.
+
+import { describe, it, expect, beforeEach } from 'vitest';
+import { vi } from 'vitest';
+
+const lockHeld = { value: false };
+vi.mock('../api/_lib/cache.js', () => ({
+ acquireLock: vi.fn(async () => !lockHeld.value),
+}));
+vi.mock('../api/_lib/db.js', () => ({ sql: vi.fn(async () => []) }));
+vi.mock('../api/_lib/db-retry.js', () => ({ withDbRetry: (fn) => fn() }));
+vi.mock('../api/_lib/alerts.js', () => ({ sendOpsAlert: vi.fn(async () => {}) }));
+vi.mock('../api/_lib/economy-master.js', () => ({ ECONOMY_MASTER_ADDRESS: 'MASTER', RESERVE_SOL: 0.1 }));
+vi.mock('../api/_lib/solana-signers.js', () => ({ SOLANA_SIGNERS: [], resolveSignerPubkey: vi.fn(async () => ({ pubkey: null })) }));
+vi.mock('../api/_lib/economy-ledger.js', () => ({ verifyChain: vi.fn(async () => ({ ok: true, count: 0 })) }));
+vi.mock('../api/_lib/avatar-wallet.js', () => ({ getSolBalance: vi.fn(async () => ({ sol: 10 })) }));
+vi.mock('../api/_lib/solana/connection.js', () => ({
+ solanaConnection: () => ({ getSignaturesForAddress: vi.fn(async () => []), getSignatureStatuses: vi.fn(async () => ({ value: [] })) }),
+}));
+
+const { default: handler } = await import('../api/cron/economy-reconcile.js');
+
+describe('economy-reconcile handler lease', () => {
+ beforeEach(() => {
+ process.env.CRON_SECRET = 'test-cron-secret';
+ lockHeld.value = false;
+ });
+
+ function call() {
+ const req = { method: 'GET', url: '/api/cron/economy-reconcile', headers: { authorization: 'Bearer test-cron-secret' } };
+ const res = {
+ statusCode: 200,
+ headers: {},
+ body: '',
+ headersSent: false,
+ writableEnded: false,
+ setHeader(k, v) { this.headers[k.toLowerCase()] = v; },
+ getHeader(k) { return this.headers[k.toLowerCase()]; },
+ end(b) { this.body = b ?? ''; this.writableEnded = true; this.headersSent = true; },
+ };
+ return handler(req, res).then(() => res);
+ }
+
+ it('skips without touching the chain while another run holds the lease', async () => {
+ lockHeld.value = true;
+ const res = await call();
+ expect(res.statusCode).toBe(200);
+ expect(JSON.parse(res.body)).toEqual({ ok: true, skipped: true, reason: 'lease_held' });
+ });
+});