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.

Circulation: pace the tick to the treasury instead of burning out

nirholasf5a0d6cd70b8
diff --git a/.env.example b/.env.example
index e3e24e111..8c00de1cd 100644
--- a/.env.example
+++ b/.env.example
@@ -960,6 +960,16 @@ CIRCULATION_NETWORK=
# Target number of operated agents (default 14) and light actions per tick (default 2).
CIRCULATION_POOL_TARGET=
CIRCULATION_ACTIONS_PER_TICK=
+# Adaptive rate governor. The tick reads the treasury once and funds only as many
+# SOL-spending actions as it can afford, filling the rest of the budget with
+# actions that cost nothing on-chain (trials, reviews, listings). Without this the
+# engine planned a fixed rate, drained the treasury, and then produced nothing but
+# "treasury balance too low" skips until a human refilled it.
+# SOL held back so the treasury can always pay its own transfer fees (default 0.005).
+CIRCULATION_TREASURY_RESERVE_SOL=
+# Estimated just-in-time top-up one paid action draws; sets the throttle curve
+# (default 0.012, the observed per-action funding need).
+CIRCULATION_EST_ACTION_SOL=
# Optional: enables on-chain (ERC-8004) agent registration. EVM private key (0x…)
# of a gas-funded treasury wallet, plus the chain id (default 8453 / Base). Without
# this, the deploy action is skipped.
diff --git a/api/_lib/circulation.js b/api/_lib/circulation.js
index 1d39e6feb..3332c96e1 100644
--- a/api/_lib/circulation.js
+++ b/api/_lib/circulation.js
@@ -64,6 +64,48 @@ const TIP_MAX = Math.floor(0.006 * SOL);
const PAY_MIN = Math.floor(0.0012 * SOL);
const PAY_MAX = Math.floor(0.01 * SOL);
+// ── Adaptive rate governor ────────────────────────────────────────────────────
+// The engine used to plan a fixed actionsPerTick regardless of what the treasury
+// could afford. When the treasury ran dry every action still did its RPC reads
+// and then threw Skip("treasury balance ... too low"), so the loop burned work to
+// produce nothing and the Money Pulse went silent until a human topped it up
+// (observed 2026-07-20: circulation dead for 11h while the x402 ring kept running).
+//
+// Instead the tick now reads the treasury once and scales itself: it funds as
+// many SOL-spending actions as it can actually afford, and fills the rest of the
+// budget with actions that cost nothing on-chain, so the economy degrades to a
+// quieter-but-alive state and automatically speeds back up as the ring's revenue
+// share refills the treasury. Self-balancing instead of burst-then-dead.
+const COSTLY_ACTIONS = new Set(['tip', 'payment', 'trade', 'launch', 'deploy', 'buy_skill', 'buy_asset']);
+/** Leave this much SOL so the treasury can always pay its own transfer fees. */
+const TREASURY_RESERVE_SOL = Number(process.env.CIRCULATION_TREASURY_RESERVE_SOL) || 0.005;
+/** Typical just-in-time top-up one paid action draws (observed ~0.012 SOL). */
+const EST_ACTION_SOL = Number(process.env.CIRCULATION_EST_ACTION_SOL) || 0.012;
+
+/**
+ * How many SOL-spending actions this tick can afford. Pure so the throttle curve
+ * is unit-testable without a chain or a treasury.
+ * @param {{treasurySol:number, actionsPerTick:number, reserveSol?:number, costPerActionSol?:number}} p
+ * @returns {{paidBudget:number, throttled:boolean, spendableSol:number, reason:string|null}}
+ */
+export function planBudget({
+ treasurySol,
+ actionsPerTick,
+ reserveSol = TREASURY_RESERVE_SOL,
+ costPerActionSol = EST_ACTION_SOL,
+}) {
+ const cap = Math.max(0, Number(actionsPerTick) || 0);
+ const spendable = Math.max(0, (Number(treasurySol) || 0) - reserveSol);
+ const affordable = costPerActionSol > 0 ? Math.floor(spendable / costPerActionSol) : cap;
+ const paidBudget = Math.max(0, Math.min(cap, affordable));
+ return {
+ paidBudget,
+ throttled: paidBudget < cap,
+ spendableSol: Math.round(spendable * 1e6) / 1e6,
+ reason: paidBudget === 0 ? 'treasury_below_reserve' : (paidBudget < cap ? 'treasury_low' : null),
+ };
+}
+
const THREE_MINT = () => env.THREE_TOKEN_MINT || 'FeMbDoX7R1Psc4GEcvJdsbNbZA3bfztcyDCatJVJpump';
// Marketplace economics. Listings are priced in whole $THREE; buyers acquire $THREE
@@ -1322,18 +1364,21 @@ async function buildAgentCard(agent, origin) {
// Choose the action mix for this tick. Launch/deploy are heavyweight and run
// solo. Everything else can batch up to actionsPerTick.
-async function planActions(cfg, poolSize) {
+async function planActions(cfg, poolSize, budget = null) {
const plan = [];
+ // No affordable paid slots → don't even price the heavyweight solo actions;
+ // they are the most expensive things the engine does.
+ const paidAllowed = budget ? budget.paidBudget > 0 : true;
const launchesToday = await recentCount('launch', '1 day');
const deploysToday = await recentCount('deploy', '1 day');
const launchesHour = await recentCount('launch', '45 minutes');
// Heavyweight, low-frequency, solo actions get first refusal.
const r = Math.random();
- if (poolSize >= 2 && launchesHour === 0 && launchesToday < 8 && r < 0.14) {
+ if (paidAllowed && poolSize >= 2 && launchesHour === 0 && launchesToday < 8 && r < 0.14) {
return ['launch'];
}
- if (cfg.evmTreasurySecret && poolSize >= 1 && deploysToday < 6 && r >= 0.14 && r < 0.2) {
+ if (paidAllowed && cfg.evmTreasurySecret && poolSize >= 1 && deploysToday < 6 && r >= 0.14 && r < 0.2) {
return ['deploy'];
}
@@ -1370,12 +1415,22 @@ async function planActions(cfg, poolSize) {
['review', 8],
['payment', 6],
];
- const total = weighted.reduce((s, [, w]) => s + w, 0);
+ // Fill against the affordable budget: once the paid slots are used up, only
+ // actions that cost nothing on-chain remain eligible, so a lean treasury
+ // yields a quieter-but-live tick instead of a wall of guaranteed skips.
+ let paidLeft = budget ? budget.paidBudget : cfg.actionsPerTick;
while (plan.length < cfg.actionsPerTick) {
+ const eligible = paidLeft > 0 ? weighted : weighted.filter(([kind]) => !COSTLY_ACTIONS.has(kind));
+ if (!eligible.length) break;
+ const total = eligible.reduce((s, [, w]) => s + w, 0);
let roll = Math.random() * total;
- for (const [kind, w] of weighted) {
+ for (const [kind, w] of eligible) {
roll -= w;
- if (roll <= 0) { plan.push(kind); break; }
+ if (roll <= 0) {
+ plan.push(kind);
+ if (COSTLY_ACTIONS.has(kind)) paidLeft -= 1;
+ break;
+ }
}
}
return plan;
@@ -1439,8 +1494,29 @@ export async function runCirculationTick() {
let solUsd = null;
try { solUsd = await solUsdPrice(); } catch { /* usd is decoration */ }
+ // One treasury read per tick drives the governor. A failed read must not stall
+ // the economy, so it degrades to "assume fundable" and the per-action
+ // ensureFunded checks stay the backstop.
+ let treasurySol = null;
+ try {
+ treasurySol = (await conn.getBalance(treasuryKp.publicKey)) / SOL;
+ } catch (e) {
+ console.warn('[circulation] treasury balance read failed', e?.message);
+ }
+ const budget = treasurySol === null
+ ? null
+ : planBudget({ treasurySol, actionsPerTick: cfg.actionsPerTick });
+ if (budget?.throttled) {
+ console.warn('[circulation] throttled', {
+ reason: budget.reason,
+ treasury_sol: Number(treasurySol.toFixed(6)),
+ paid_budget: budget.paidBudget,
+ actions_per_tick: cfg.actionsPerTick,
+ });
+ }
+
const ctx = { conn, treasuryKp, pool, network: cfg.network, origin: cfg.origin, solUsd, cfg };
- const plan = await planActions(cfg, pool.length);
+ const plan = await planActions(cfg, pool.length, budget);
const results = [];
for (const kind of plan) {
@@ -1460,5 +1536,18 @@ export async function runCirculationTick() {
}
}
- return { ok: true, pool: pool.length, grew, network: cfg.network, actions: results };
+ return {
+ ok: true,
+ pool: pool.length,
+ grew,
+ network: cfg.network,
+ ...(budget
+ ? {
+ treasury_sol: Number(treasurySol.toFixed(6)),
+ paid_budget: budget.paidBudget,
+ ...(budget.throttled ? { throttled: budget.reason } : {}),
+ }
+ : {}),
+ actions: results,
+ };
}
diff --git a/data/changelog.json b/data/changelog.json
index 18c6f4d91..567280b41 100644
--- a/data/changelog.json
+++ b/data/changelog.json
@@ -1,6 +1,16 @@
{
"$comment": "Curated changelog entries for three.ws, the editorial layer of the public changelog. New PAGE launches are tracked automatically via the `added` field in data/pages.json; this file is for everything else holders care about: improvements, fixes, SDK releases, trading/payments capabilities, security work. scripts/build-page-index.mjs merges both sources into CHANGELOG.md, public/changelog.json, and public/changelog.xml. Newest first. Allowed tags: feature, improvement, fix, sdk, infra, docs, security. `link` is optional and must be a live three.ws path.",
"entries": [
+ {
+ "date": "2026-07-20",
+ "title": "The live economy now paces itself instead of going quiet",
+ "summary": "The agent economy used to run at a fixed rate until its treasury ran dry, then fall silent for hours until someone refilled it by hand. It now measures what it can afford each minute and paces itself: when funds are lean it keeps trials, reviews, and marketplace listings flowing while pausing the actions that spend, then automatically speeds back up as revenue comes in. The result is a Money Pulse that stays alive continuously rather than running in bursts.",
+ "tags": [
+ "improvement",
+ "infra"
+ ],
+ "link": "/pulse"
+ },
{
"date": "2026-07-20",
"title": "Audit any agent's trade decisions, linked from the write-ups",
diff --git a/tests/circulation-governor.test.js b/tests/circulation-governor.test.js
new file mode 100644
index 000000000..8a8d2efae
--- /dev/null
+++ b/tests/circulation-governor.test.js
@@ -0,0 +1,70 @@
+// Adaptive rate governor: circulation must scale its paid-action budget to the
+// treasury it actually has, so a lean treasury yields a quieter-but-live tick
+// instead of a wall of guaranteed "treasury balance too low" skips.
+import { describe, it, expect } from 'vitest';
+import { planBudget } from '../api/_lib/circulation.js';
+
+const COST = 0.012; // est. just-in-time top-up per paid action
+const RESERVE = 0.005; // treasury keeps this for its own tx fees
+
+const plan = (treasurySol, actionsPerTick = 4) =>
+ planBudget({ treasurySol, actionsPerTick, reserveSol: RESERVE, costPerActionSol: COST });
+
+describe('planBudget', () => {
+ it('funds the full rate when the treasury is healthy', () => {
+ const b = plan(1.0);
+ expect(b.paidBudget).toBe(4);
+ expect(b.throttled).toBe(false);
+ expect(b.reason).toBeNull();
+ });
+
+ it('throttles proportionally as the treasury drains', () => {
+ // spendable = 0.05 - 0.005 = 0.045 -> floor(0.045/0.012) = 3
+ expect(plan(0.05).paidBudget).toBe(3);
+ // spendable = 0.03 - 0.005 = 0.025 -> 2
+ expect(plan(0.03).paidBudget).toBe(2);
+ // spendable = 0.02 - 0.005 = 0.015 -> 1
+ expect(plan(0.02).paidBudget).toBe(1);
+ });
+
+ it('reports throttled with a reason when partially funded', () => {
+ const b = plan(0.03);
+ expect(b.throttled).toBe(true);
+ expect(b.reason).toBe('treasury_low');
+ });
+
+ it('drops paid actions to zero at/below the reserve (the observed dead state)', () => {
+ // 0.0012 SOL is the exact balance that produced 11h of dead circulation.
+ const b = plan(0.0012);
+ expect(b.paidBudget).toBe(0);
+ expect(b.throttled).toBe(true);
+ expect(b.reason).toBe('treasury_below_reserve');
+ });
+
+ it('never spends the reserve itself', () => {
+ expect(plan(RESERVE).paidBudget).toBe(0);
+ expect(plan(RESERVE).spendableSol).toBe(0);
+ });
+
+ it('handles a zero or negative balance without going negative', () => {
+ for (const bal of [0, -1]) {
+ const b = plan(bal);
+ expect(b.paidBudget).toBe(0);
+ expect(b.spendableSol).toBe(0);
+ }
+ });
+
+ it('never exceeds the configured rate no matter how rich the treasury', () => {
+ expect(plan(1000, 4).paidBudget).toBe(4);
+ expect(plan(1000, 2).paidBudget).toBe(2);
+ });
+
+ it('recovers automatically as the revenue share refills the treasury', () => {
+ // The self-balancing property: budget is monotonic in treasury balance,
+ // so refill alone restores throughput with no human intervention.
+ const curve = [0.001, 0.01, 0.02, 0.04, 0.06].map((s) => plan(s).paidBudget);
+ expect(curve).toEqual([...curve].sort((a, b) => a - b));
+ expect(curve[0]).toBe(0);
+ expect(curve[curve.length - 1]).toBe(4);
+ });
+});