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: add economy-tick endpoint to centralize agent economy scheduling

qrqkdee31b120535
diff --git a/CLAUDE.md b/CLAUDE.md
index 725a1d86a..9ad1cd383 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -188,6 +188,10 @@ When the user asks you to push (or to commit + push):
Run both in the same step. If one fails, surface the error — do not silently leave the repos out of sync. Never force-push to either remote without an explicit request.
+## No GitHub Actions
+
+**We do not use GitHub Actions.** Do not create, edit, or rely on workflows under `.github/workflows/`. Automation runs elsewhere (Vercel deploys, workers, local scripts) — never propose a GitHub Actions workflow as the solution for CI, scheduling, or deployment.
+
## Git: NEVER pull or fetch from 3D-Agent
**`threeD` (nirholas/3D-Agent) is a PUSH-ONLY mirror. NEVER run `git pull`, `git fetch`, or `git merge` from it.**
diff --git a/api/cron/economy-tick.js b/api/cron/economy-tick.js
new file mode 100644
index 000000000..55ea47fea
--- /dev/null
+++ b/api/cron/economy-tick.js
@@ -0,0 +1,124 @@
+// @ts-check
+// GET/POST /api/cron/economy-tick — the single-URL heartbeat for the whole
+// agent-to-agent economy.
+//
+// WHY THIS EXISTS. The economy is driven by a handful of per-minute engines
+// (ring settlements, x402 micropayments, the Money Pulse, the autonomous spend
+// loop, the Labor Market). Each used to need its own Vercel cron entry, but the
+// project declares ~78 crons and Vercel only schedules the first 40 on Pro (2/day
+// on Hobby) — so most economy ticks were silently never scheduled. GitHub Actions
+// (the documented failover) is permanently unavailable on this account. The net
+// effect: nothing ticked the economy except deploys, and it flat-lined for hours
+// to days at a time.
+//
+// This endpoint collapses all of that into ONE job. Point any external minute
+// scheduler at it (Vercel Cron, Upstash QStash, cron-job.org, or a small always-on
+// host running scripts/economy-heartbeat.mjs) with the CRON_SECRET bearer and the
+// entire economy ticks from a single URL — no GitHub, no per-engine cron sprawl.
+//
+// Every target engine is internally idempotent: per-tick spend caps, per-endpoint
+// cooldowns, and daily ceilings absorb over-calling, so it is safe to fire this
+// every minute (or more often) regardless of each engine's native cadence.
+//
+// Real on-chain payments only — this dispatcher never moves money itself; it just
+// invokes the engines that do, over the same authenticated path Vercel Cron uses.
+
+import { json, method, wrapCron } from '../_lib/http.js';
+import { env } from '../_lib/env.js';
+import { constantTimeEquals } from '../_lib/crypto.js';
+import { logger } from '../_lib/usage.js';
+
+const log = logger('economy-tick');
+
+const ORIGIN = () => (env.APP_ORIGIN || 'https://three.ws').replace(/\/+$/, '');
+
+// The engines that make the agent economy move. `path` is invoked with the same
+// `Authorization: Bearer $CRON_SECRET` header Vercel Cron would send. `label` is
+// for the summary. Order is best-effort only — all fire concurrently.
+const TARGETS = [
+ { label: 'ring-tick', path: '/api/cron/x402-ring-tick', method: 'GET' },
+ { label: 'x402-seed', path: '/api/cron/x402-seed-cron', method: 'GET' },
+ { label: 'x402-autonomous', path: '/api/cron/x402-autonomous-loop', method: 'GET' },
+ { label: 'money-pulse', path: '/api/cron/pulse-tick', method: 'GET' },
+ { label: 'labor-market', path: '/api/labor/tick', method: 'POST' },
+];
+
+const CALL_TIMEOUT_MS = 60_000;
+
+function requireCron(req, res) {
+ const secret = process.env.CRON_SECRET || env.CRON_SECRET;
+ if (!secret) { json(res, 503, { error: 'not_configured', message: 'CRON_SECRET unset' }); return false; }
+ const auth = req.headers['authorization'] || '';
+ const presented = auth.startsWith('Bearer ') ? auth.slice(7) : '';
+ if (!constantTimeEquals(presented, secret)) { json(res, 401, { error: 'unauthorized' }); return false; }
+ return true;
+}
+
+// Fire one engine over HTTP with the cron bearer. A non-2xx, a 404 (the deployed
+// build may lag a newly-added engine), or a timeout is reported per-target and
+// never throws — one dead engine must not stop the others.
+async function fireTarget(origin, secret, target) {
+ const url = `${origin}${target.path}`;
+ const controller = new AbortController();
+ const timer = setTimeout(() => controller.abort(), CALL_TIMEOUT_MS);
+ const started = Date.now();
+ try {
+ const res = await fetch(url, {
+ method: target.method,
+ headers: {
+ authorization: `Bearer ${secret}`,
+ 'user-agent': 'threews-economy-tick/1.0',
+ ...(target.method === 'POST' ? { 'content-type': 'application/json' } : {}),
+ },
+ ...(target.method === 'POST' ? { body: '{}' } : {}),
+ signal: controller.signal,
+ });
+ let body = null;
+ try { body = await res.json(); } catch { /* non-JSON or empty */ }
+ return {
+ label: target.label,
+ ok: res.ok,
+ status: res.status,
+ ms: Date.now() - started,
+ // Surface the engine's own skip reason so the summary is diagnostic.
+ ...(body && typeof body === 'object' && body.reason ? { reason: body.reason } : {}),
+ ...(body && typeof body === 'object' && body.skipped ? { skipped: true } : {}),
+ };
+ } catch (err) {
+ return {
+ label: target.label,
+ ok: false,
+ status: 0,
+ ms: Date.now() - started,
+ error: err?.name === 'AbortError' ? 'timeout' : (err?.message || 'fetch_failed'),
+ };
+ } finally {
+ clearTimeout(timer);
+ }
+}
+
+export default wrapCron(async (req, res) => {
+ if (!method(req, res, ['GET', 'POST'])) return;
+ if (!requireCron(req, res)) return;
+
+ const origin = ORIGIN();
+ const secret = process.env.CRON_SECRET || env.CRON_SECRET;
+
+ const results = await Promise.all(TARGETS.map((t) => fireTarget(origin, secret, t)));
+ const fired = results.filter((r) => r.ok).length;
+ const failed = results.filter((r) => !r.ok).length;
+
+ log.info('economy_tick_complete', { origin, fired, failed, results });
+
+ // 200 as long as at least one engine responded OK; 502 only on a total outage
+ // (bad secret / origin down / every engine failing) so an external scheduler's
+ // own alerting can catch a real economy-wide failure.
+ const allDown = fired === 0;
+ return json(res, allDown ? 502 : 200, {
+ ok: !allDown,
+ origin,
+ fired,
+ failed,
+ results,
+ });
+});