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(buyback): lease-gate run-buyback so the hourly cron cadence is durable

nirholas4dba50a0d2ba
diff --git a/api/cron/[name].js b/api/cron/[name].js
index 6c52e6486..b14ff13d5 100644
--- a/api/cron/[name].js
+++ b/api/cron/[name].js
@@ -72,6 +72,7 @@ import { runPumpAlertRules } from '../_lib/pump-alert-runner.js';
import { publishUserEvent } from '../_lib/feed.js';
import { confirmSkillPurchase } from '../_lib/purchase-confirm.js';
import { requireCron } from '../_lib/cron-auth.js';
+import { acquireLock } from '../_lib/cache.js';
import { resolveURI } from '../../src/ipfs.js';
import {
OUTCOME,
@@ -2487,12 +2488,27 @@ async function resolveCoinQuoteCurrency({ network, mint, dbQuoteMint = null }) {
return { currencyStr: usdc, quoteSymbol: 'USDC', source: 'fallback' };
}
+// Same over-invocation disease already fixed for economy-reconcile: this
+// route is also one of economy-tick.js's fan-out TARGETS, fired every minute,
+// on top of its own dedicated vercel.json `0 * * * *` Cloud Scheduler job,
+// with nothing stopping the two callers from overlapping. Each run reads and
+// can swap/burn up to 200 coins' buyback vaults, so an overlapping run risks
+// racing itself on the same vault balance, not just running long. The lease
+// makes the hourly cadence durable regardless of how often the route fires;
+// its TTL IS the cadence and is never explicitly released.
+const BUYBACK_LEASE_KEY = 'cron:run-buyback:lease';
+const BUYBACK_LEASE_SECONDS = 55 * 60;
+
async function handleRunBuyback(req, res) {
if (cors(req, res, { methods: 'GET,POST,OPTIONS', origins: '*' })) return;
if (!method(req, res, ['GET', 'POST'])) return;
if (!requireCron(req, res)) return;
+ if (!(await acquireLock(BUYBACK_LEASE_KEY, BUYBACK_LEASE_SECONDS))) {
+ return json(res, 200, { ok: true, skipped: true, reason: 'lease_held' });
+ }
+
const relayer = await buybackLoadRelayer();
// Honor per-coin autopilot policy. A missing pump_autopilot row preserves the
diff --git a/tests/run-buyback-lease.test.js b/tests/run-buyback-lease.test.js
new file mode 100644
index 000000000..a35ad40d1
--- /dev/null
+++ b/tests/run-buyback-lease.test.js
@@ -0,0 +1,50 @@
+// Cadence-lease test for handleRunBuyback in api/cron/[name].js.
+//
+// Production 2026-10-24: a single HTTP 503 (budget timeout) on run-buyback.
+// economy-tick.js fires /api/cron/run-buyback every minute (one of its ~36
+// fan-out TARGETS) on top of its own dedicated vercel.json `0 * * * *` Cloud
+// Scheduler job, with nothing stopping the two callers from overlapping.
+// Unlike economy-reconcile (fixed the same way earlier this session), each
+// run here also reads and can swap/burn up to 200 coins' buyback vaults, so
+// an overlapping run risks racing itself against the same vault balance, not
+// just running long. A lease that makes the hourly cadence durable regardless
+// of how often the route is invoked fixes both the timeout risk and the race.
+
+import { describe, it, expect, beforeEach, vi } from 'vitest';
+
+const lockHeld = { value: false };
+vi.mock('../api/_lib/cache.js', () => ({
+ acquireLock: vi.fn(async () => !lockHeld.value),
+}));
+vi.mock('../api/_lib/cron-auth.js', () => ({ requireCron: vi.fn(() => true) }));
+vi.mock('../api/_lib/db.js', () => ({ sql: vi.fn(async () => []) }));
+
+const { default: handler } = await import('../api/cron/[name].js');
+
+function call() {
+ const req = { method: 'GET', url: '/api/cron/run-buyback?name=run-buyback', query: { name: 'run-buyback' }, headers: {} };
+ 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);
+}
+
+describe('run-buyback handler lease', () => {
+ beforeEach(() => {
+ lockHeld.value = false;
+ });
+
+ it('skips the per-mint sweep 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' });
+ });
+});