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(x402): heartbeat floor so the runway governor cannot deadlock the ring

nirholas4ea74ace5f45
diff --git a/api/_lib/x402/ring-tick-plan.js b/api/_lib/x402/ring-tick-plan.js
index 6c011b029..fc6afd834 100644
--- a/api/_lib/x402/ring-tick-plan.js
+++ b/api/_lib/x402/ring-tick-plan.js
@@ -52,6 +52,10 @@ export function ringTickConfig(e = process.env) {
// governor only ever throttles DOWN from `calls`; it never raises it.
feePerCallLamports: Math.max(1, Math.floor(num(e.X402_RING_FEE_PER_CALL_LAMPORTS, 7_000))),
runwayDays: Math.max(0.5, num(e.X402_RING_TARGET_RUNWAY_DAYS, 3)),
+ // Heartbeat floor: calls/min the governor still allows while the payer is
+ // ABOVE the hard SOL floor but below the balance the runway target wants.
+ // 0 restores the strict runway-only behavior. See governedCalls().
+ minCalls: Math.max(0, Math.floor(num(e.X402_RING_MIN_CALLS, 1))),
// USDC float (atomics) the ring tick must LEAVE UNSPENT for the
// artifact-producing pipelines (forge props, avatar rigs) that share the
// payer. Ring-settle recirculates money in a circle and produces nothing
@@ -71,20 +75,39 @@ export function ringTickConfig(e = process.env) {
// (the floor stays untouchable). The governed rate makes funding the throttle:
// more SOL in the payer = more calls/min, automatically, and as the balance
// drains the rate tapers instead of cliff-dying. Pure: same inputs → same
-// decision. Returns { calls, callsPerDayBudget, throttled }.
+// decision. Returns { calls, callsPerDayBudget, throttled, heartbeat }.
+//
+// HEARTBEAT (`minCalls`, default 1): above the hard SOL floor the governor never
+// returns 0. Runway-only governing left a dead band between the floor and
+// "floor + runwayDays of 1 call/min" (0.03 SOL at the stock knobs) in which the
+// ring did nothing at all — and doing nothing is self-reinforcing: no calls means
+// no settles, no settles means no treasury USDC, no USDC means no sweep, no
+// revshare, no fuel swap, so the economy can never restart itself from its own
+// balances (observed 2026-07-26: 58 consecutive `runway_exhausted` ticks with
+// 0.047 SOL of spendable capital sitting idle across the ring wallets). The hard
+// floor is the real protection — `assessBackpressure` stops the rail there — so
+// spending the band slowly at the heartbeat rate strictly dominates hoarding it.
export function governedCalls({
- configuredCalls, solLamports, floorLamports, feePerCallLamports, runwayDays,
+ configuredCalls, solLamports, floorLamports, feePerCallLamports, runwayDays, minCalls = 1,
}) {
if (!Number.isFinite(solLamports)) {
- return { calls: 0, callsPerDayBudget: 0, throttled: true };
+ return { calls: 0, callsPerDayBudget: 0, throttled: true, heartbeat: false };
}
const spendable = Math.max(0, solLamports - floorLamports);
const callsPerDayBudget = Math.floor(
spendable / Math.max(0.5, runwayDays) / Math.max(1, feePerCallLamports),
);
const callsPerMin = Math.floor(callsPerDayBudget / 1440);
- const calls = Math.max(0, Math.min(configuredCalls, callsPerMin));
- return { calls, callsPerDayBudget, throttled: calls < configuredCalls };
+ // At or below the floor there is nothing to spend: stop, and let the
+ // runway_exhausted skip fire as the loud "fund the payer" signal.
+ const beat = spendable > 0 ? Math.max(0, Math.floor(minCalls)) : 0;
+ const calls = Math.max(0, Math.min(configuredCalls, Math.max(callsPerMin, beat)));
+ return {
+ calls,
+ callsPerDayBudget,
+ throttled: calls < configuredCalls,
+ heartbeat: calls > 0 && calls > callsPerMin,
+ };
}
// ── Cadence: which endpoints does this tick pay? ────────────────────────────────
diff --git a/api/cron/x402-ring-tick.js b/api/cron/x402-ring-tick.js
index 2d723786a..2c597011a 100644
--- a/api/cron/x402-ring-tick.js
+++ b/api/cron/x402-ring-tick.js
@@ -303,6 +303,7 @@ export default wrapCron(async (req, res) => {
floorLamports: cfg.solFloorLamports,
feePerCallLamports: cfg.feePerCallLamports,
runwayDays: cfg.runwayDays,
+ minCalls: cfg.minCalls,
});
if (gov.calls <= 0) {
await recordSkip(runId, origin, 'runway_exhausted', {
diff --git a/docs/x402-ring-economy.md b/docs/x402-ring-economy.md
index 554c88509..8514e93f9 100644
--- a/docs/x402-ring-economy.md
+++ b/docs/x402-ring-economy.md
@@ -31,9 +31,37 @@ measured 6,300-7,800 self-pay cost) per call. Consequences:
94-calls/min fixed rate).
- The governor only throttles DOWN from the configured rate, never above it,
so `X402_RING_TICK_CALLS` remains the hard ceiling.
-- When spendable SOL cannot sustain even 1 call/min for the runway window, the
- tick skips with reason `runway_exhausted` (one throttled ops alert), which is
- the loud "fund the payer" signal.
+- **Heartbeat (`X402_RING_MIN_CALLS`, default 1).** Above the hard SOL floor the
+ governor never returns 0. Runway-only governing left a dead band between the
+ floor and "floor + `runwayDays` of 1 call/min" (0.03 SOL at stock knobs) where
+ the ring did nothing — and doing nothing is self-reinforcing: no calls means no
+ settles, no settles means no treasury USDC, so no sweep, no revshare, no fuel
+ swap, and the economy cannot restart itself from its own balances. Observed
+ 2026-07-26: 58 consecutive `runway_exhausted` ticks while spendable SOL sat
+ idle. The hard floor is the real protection (`assessBackpressure` stops the
+ rail there), so spending the band at the heartbeat rate strictly dominates
+ hoarding it. Set `X402_RING_MIN_CALLS=0` to restore strict runway-only mode.
+- Only at or below the floor does the tick skip with `runway_exhausted` (one
+ throttled ops alert), the loud "fund the payer" signal.
+
+### Known limitation: the governor governs one tenant of a shared wallet
+
+`governedCalls()` throttles the **ring tick only**, but the ring payer is shared
+with roughly a dozen other paid pipelines (`health`, `volume`, `oracle`,
+`sniper`, `datapoint`, `3d`, `ring-agents`, …) that spend ungoverned. Measured
+2026-07-27: in one 20-minute window the ring tick was throttled to 2 paid calls
+while co-tenant pipelines completed ~200. A governor that throttles one tenant
+while others drain the same runway protects nothing under real scarcity — it
+just decides *which* pipeline gets starved. Two follow-ups, in order of value:
+
+1. **Meter the wallet, not the pipeline.** Move the runway budget into a shared
+ accounting key (the same place `ringDailySpent()` lives) so every pipeline
+ draws from one governed allowance.
+2. **Watch the wallet that actually pays.** The governor reads the ring payer's
+ balance, but sponsor-mode settles are fee-paid by the sponsor/master
+ (confirmed on-chain: `accountKeys[0]` is the master on co-tenant payments, 2
+ signatures, 10,001 lamports). In mixed mode the governed balance and the
+ burned balance are different wallets.
## Burning the least SOL — two levers
diff --git a/tests/x402-ring-tick.test.js b/tests/x402-ring-tick.test.js
index c65565169..780bbac22 100644
--- a/tests/x402-ring-tick.test.js
+++ b/tests/x402-ring-tick.test.js
@@ -313,10 +313,41 @@ describe('ring-tick runway governor', () => {
expect(g.throttled).toBe(true);
});
- it('below one call/min rounds down to zero, never negative', () => {
- // 0.01 SOL spendable / 3d / 7000 = 476/day < 1440
+ it('keeps a heartbeat above the floor when the runway target wants zero', () => {
+ // 0.01 SOL spendable / 3d / 7000 = 476/day < 1440 → runway says 0/min, but
+ // the payer is above the hard floor, so the heartbeat keeps the ring alive.
+ // Hoarding this band is what deadlocked the economy (no calls → no settles
+ // → no sweep → no fuel → no recovery).
const g = governedCalls({ ...base, solLamports: 30_000_000 });
+ expect(g.calls).toBe(1);
+ expect(g.heartbeat).toBe(true);
+ expect(g.throttled).toBe(true);
+ });
+
+ it('minCalls=0 restores strict runway-only governing', () => {
+ const g = governedCalls({ ...base, solLamports: 30_000_000, minCalls: 0 });
expect(g.calls).toBe(0);
+ expect(g.heartbeat).toBe(false);
+ });
+
+ it('honours a larger heartbeat but never above the configured rate', () => {
+ const g = governedCalls({ ...base, solLamports: 30_000_000, minCalls: 5 });
+ expect(g.calls).toBe(5);
+ const capped = governedCalls({
+ ...base, configuredCalls: 2, solLamports: 30_000_000, minCalls: 5,
+ });
+ expect(capped.calls).toBe(2);
+ });
+
+ it('the heartbeat never fires at or below the floor', () => {
+ expect(governedCalls({ ...base, solLamports: 20_000_000, minCalls: 9 }).calls).toBe(0);
+ expect(governedCalls({ ...base, solLamports: 1_000_000, minCalls: 9 }).calls).toBe(0);
+ });
+
+ it('does not flag heartbeat when the runway itself sustains the rate', () => {
+ const g = governedCalls({ ...base, solLamports: 520_000_000 });
+ expect(g.calls).toBe(16);
+ expect(g.heartbeat).toBe(false);
});
it('NaN balance (RPC fault) fails closed to zero calls', () => {