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.
JavaScript 3d-generationai-agentsanimationavatardigital-humangenerative-aiglbgltfimage-to-3dllmmcpmcp-servermodel-context-protocolsolanatext-to-3dthreejsweb-componentswebglwebxrx402
fix(buyback): lease-gate the live run-three-buyback spend path
nirholasefe545b384d2
diff --git a/api/cron/[name].js b/api/cron/[name].jsindex b14ff13d5..a5d986971 100644--- a/api/cron/[name].js+++ b/api/cron/[name].js@@ -2657,6 +2657,19 @@ async function handleRunBuyback(req, res) {// single dollar moves.// ═══════════════════════════════════════════════════════════════════════════+// Same over-invocation disease fixed above for run-buyback: economy-tick.js+// fires /api/cron/run-three-buyback every minute (fan-out TARGETS) on top of+// its own dedicated vercel.json `0 6 * * *` (once daily) Cloud Scheduler job.+// Unlike run-buyback's burn-only default, a live run here sizes its spend off+// the buyback wallet's CURRENT USDC balance (computeSpend), signs a real+// Jupiter swap, and sweeps to treasury: nothing stops two overlapping real+// runs from both reading the same pre-spend balance and each deploying up to+// maxUsdPerRun(), doubling the per-run spend cap the policy relies on. The+// lease only gates the live path; a dry run never signs, so monitoring/status+// callers with ?dry_run=1 are never blocked by it.+const THREE_BUYBACK_LEASE_KEY = 'cron:run-three-buyback:lease';+const THREE_BUYBACK_LEASE_SECONDS = 23 * 60 * 60;+async function handleRunThreeBuyback(req, res) {if (cors(req, res, { methods: 'GET,POST,OPTIONS', origins: '*' })) return;if (!method(req, res, ['GET', 'POST'])) return;@@ -2665,6 +2678,11 @@ async function handleRunThreeBuyback(req, res) {const bb = await import('../_lib/token/buyback.js');const dryRunRaw = String(req.query?.dry_run ?? '').toLowerCase();const dryRun = dryRunRaw === '1' || dryRunRaw === 'true';++ if (!dryRun && !(await acquireLock(THREE_BUYBACK_LEASE_KEY, THREE_BUYBACK_LEASE_SECONDS))) {+ return json(res, 200, { ok: true, skipped: true, reason: 'lease_held' });+ }+const slippage = bb.slippageBps();const revenueAtomics = await bb.revenueFeeAtomicsToDate();const revStr = revenueAtomics.toString();diff --git a/tests/run-three-buyback-lease.test.js b/tests/run-three-buyback-lease.test.jsnew file mode 100644index 000000000..2ae8d2459--- /dev/null+++ b/tests/run-three-buyback-lease.test.js@@ -0,0 +1,71 @@+// Cadence-lease test for handleRunThreeBuyback in api/cron/[name].js.+//+// economy-tick.js fires /api/cron/run-three-buyback every minute (fan-out+// TARGETS) on top of its own dedicated vercel.json `0 6 * * *` (once daily)+// Cloud Scheduler job. A live run sizes its spend off the buyback wallet's+// CURRENT USDC balance and signs a real Jupiter swap, so two overlapping real+// runs would each read the same pre-spend balance and could each deploy up to+// maxUsdPerRun(), doubling the per-run spend cap the policy relies on. A+// lease on the live path (mirroring run-buyback's fix above it in the same+// file) makes the daily cadence durable regardless of how often the route is+// invoked, without blocking ?dry_run=1 status checks, which never sign.++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 () => [{ id: 'run-1' }]),+ isDbUnavailableError: vi.fn(() => false),+}));+vi.mock('../api/_lib/token/buyback.js', () => ({+ slippageBps: vi.fn(() => 50),+ revenueFeeAtomicsToDate: vi.fn(async () => 0n),+ isEnabled: vi.fn(() => true),+ loadBuybackSigner: vi.fn(async () => ({ publicKey: { toBase58: () => 'Signer111' } })),+ planBuyback: vi.fn(async () => ({ ok: false, reason: 'below_min', walletUsdcAtomics: 0n, spendUsdcAtomics: 0n })),+ usdcAtomicsToUsd: vi.fn(() => 0),+}));++const { default: handler } = await import('../api/cron/[name].js');++function call(query = {}) {+ const req = { method: 'GET', url: '/api/cron/run-three-buyback?name=run-three-buyback', query: { name: 'run-three-buyback', ...query }, 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-three-buyback handler lease', () => {+ beforeEach(() => {+ lockHeld.value = false;+ });++ it('skips the live spend path without touching the signer while another run holds the lease', async () => {+ lockHeld.value = true;+ const { loadBuybackSigner } = await import('../api/_lib/token/buyback.js');+ loadBuybackSigner.mockClear();+ const res = await call();+ expect(res.statusCode).toBe(200);+ expect(JSON.parse(res.body)).toEqual({ ok: true, skipped: true, reason: 'lease_held' });+ expect(loadBuybackSigner).not.toHaveBeenCalled();+ });++ it('never gates a dry run behind the lease', async () => {+ lockHeld.value = true;+ const res = await call({ dry_run: '1' });+ const body = JSON.parse(res.body);+ expect(body.reason).not.toBe('lease_held');+ });+});