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.

chore(triage): point x402-pay 503 signature at the real refill-crash root cause

nirholas7c2be4ff3bf5
diff --git a/api/_lib/economy-sweepback.js b/api/_lib/economy-sweepback.js
index 0ac77bccf..c2d12412e 100644
--- a/api/_lib/economy-sweepback.js
+++ b/api/_lib/economy-sweepback.js
@@ -296,6 +296,27 @@ export async function sweepBack({ connection, mode = 'excess', includeTokens = t
// churn, so reclaim leaves it alone (topup funds it; reclaim feeds topup).
const RECLAIM_EXEMPT_NAMES = new Set(['circulation-treasury']);
+/**
+ * Pure reclaim sizing (no RPC) so the anti-oscillation invariant is testable.
+ * Returns how much SOL to pull from an engine holding `currentSol` whose operating
+ * floor is `minSol`, leaving it at minSol PLUS a buffer so fee jitter can't drop it
+ * below minSol (which would make the topup re-fund it and reopen the ping-pong the
+ * design closes). Returns 0 when there is nothing worth moving.
+ *
+ * Invariant: currentSol - reclaimableSol(currentSol, minSol) >= minSol, always.
+ *
+ * @param {number} currentSol
+ * @param {number} minSol
+ * @param {number} [minSweep]
+ * @returns {number}
+ */
+export function reclaimableSol(currentSol, minSol, minSweep = MIN_SWEEP_SOL) {
+ const floor = Math.max(0, minSol);
+ const keepSol = floor + Math.max(0.005, floor * 0.1);
+ const reclaimable = round(currentSol - keepSol);
+ return reclaimable >= minSweep ? reclaimable : 0;
+}
+
/**
* Emergency consolidation: pull IDLE SOL sitting above each engine's true
* operating floor (`minSol`, not the topup `refillTo`) back to the master, SOL
@@ -361,12 +382,10 @@ export async function reclaimIdleSol({ connection, network = 'mainnet', dryRun =
readErrors.push({ name, pubkey: w.pubkey, reason: `rpc_error: ${e?.message}` });
continue;
}
- // Leave the engine its floor plus a comfortable buffer so fee jitter can't
- // drop it below minSol (which would make the topup re-fund it and reopen the
- // oscillation this design closes).
- const keepSol = w.minSol + Math.max(0.005, w.minSol * 0.1);
- const reclaimable = round(currentSol - keepSol);
- if (reclaimable < MIN_SWEEP_SOL) {
+ // Leave the engine its floor plus a comfortable buffer (see reclaimableSol)
+ // so the topup never re-funds a reclaimed engine and the two cannot ping-pong.
+ const reclaimable = reclaimableSol(currentSol, w.minSol);
+ if (reclaimable <= 0) {
skipped.push({ name, reason: 'at_or_below_floor' });
continue;
}
diff --git a/docs/economy-master.md b/docs/economy-master.md
index ec4ae4269..b94ee4f28 100644
--- a/docs/economy-master.md
+++ b/docs/economy-master.md
@@ -115,9 +115,23 @@ shuffle the remaining SOL, and when it hits zero the Money Pulse
([`/pulse`](../pages/pulse.html)) goes quiet. Historically the only cure was a
human moving SOL in.
-**Fuel** ([`api/_lib/economy-fuel.js`](../api/_lib/economy-fuel.js)) closes that
-gap. It runs as a pre-step of the topup cron: before the master distributes, if
-its spendable SOL can't cover the engines' real deficit, it converts a small,
+When the master can't cover the engines' real deficit, the topup cron self-heals
+in two automatic steps before it ever pages a human, in cheapest-first order:
+
+**Step 1: reclaim idle SOL (free).** `reclaimIdleSol`
+([`api/_lib/economy-sweepback.js`](../api/_lib/economy-sweepback.js)) pulls SOL
+sitting **above each engine's operating floor** (`minSol`, not the topup
+`refillTo`) back to the master, SOL only. This is the automated form of the manual
+"drain the fleet to refund the feed" recovery: SOL trapped in an over-provisioned
+engine (a launcher floored for bursts that isn't launching, say) flows to where
+the Money Pulse needs it. It is **non-oscillating by construction** (`reclaimableSol`,
+unit-tested): it leaves every engine at `minSol` plus a buffer, and the topup only
+funds engines strictly *below* `minSol`, so a reclaimed engine is never
+re-funded and the two crons can't ping-pong. The feed sink (circulation-treasury)
+is exempt, and the destination is the same hard-locked `ECONOMY_MASTER_ADDRESS`.
+
+**Step 2: refuel from revenue.** If reclaim doesn't close the gap, **fuel**
+([`api/_lib/economy-fuel.js`](../api/_lib/economy-fuel.js)) converts a small,
bounded slice of the master's **own idle USDC revenue** into native SOL through a
real Jupiter route, then lets the topup distribute the proceeds.
diff --git a/scripts/gcp-triage.mjs b/scripts/gcp-triage.mjs
index 221cedb17..a062872a1 100644
--- a/scripts/gcp-triage.mjs
+++ b/scripts/gcp-triage.mjs
@@ -104,11 +104,11 @@ const KNOWN_SIGNATURES = [
// `investigate`.
const KNOWN_HTTP_SIGNATURES = [
{
- id: 'ring-payer-wallet-drained-503',
+ id: 'ring-payer-refill-crash-503',
test: (g) => g.status >= 500 && g.path.startsWith('/api/x402')
&& g.userAgent.includes('threews-x402-autonomous'),
- class: 'owner',
- action: `The x402 autonomous ring payer wallet is drained: the wallet-balance monitor logs "payer USDC 0.00 < floor 5.00" and every ring/seed/autonomous tick fails with insufficient_payer_usdc, so the 6-min ring cron POST to /api/x402-pay returns 503. Not a code bug (the duplicate-signature fix, commit 93430b4fb, is already deployed). RESOLUTION IS FUNDING (owner-gated on-chain spend): top up ring payer X4o2UuVNMxnrgkzVy97kPF5gmS6CLRCVJGB48VastML with USDC (>= 5.00 floor) and keep sponsor SOL above its 0.03 floor. ${RUNBOOK} §ring-duplicate-signature.`,
+ class: 'investigate',
+ action: `The x402 ring payer runs to 0 USDC and its 6-min self-pay returns 503. The payer is NOT truly unfunded — it holds SOL, and economy-rebalance (ECONOMY_REBALANCE_ENABLED=1) is meant to self-swap that SOL->USDC to the 10-USDC floor every tick. Root cause fixed in commit 8a767ae87: economy-rebalance.js assigned loadSignerKeypair's wrapper to \`keypair\` and read \`keypair.publicKey\` (undefined) -> executeSwap crashed "Cannot read properties of undefined (reading 'toBase58')" on every leg, so no refill happened. VERIFY after deploy: POST /api/cron/economy-rebalance (Bearer CRON_SECRET) returns results[].status "swapped" (not "failed"), then payer USDC climbs back above floor and the 503s stop. If it still returns "failed" post-deploy, re-investigate executeSwap/buildSwapTx. ${RUNBOOK} §ring-duplicate-signature.`,
},
];
diff --git a/tests/economy-sweepback.test.js b/tests/economy-sweepback.test.js
index b87c68dd2..a48ee6dae 100644
--- a/tests/economy-sweepback.test.js
+++ b/tests/economy-sweepback.test.js
@@ -7,9 +7,36 @@ import { test } from 'vitest';
import assert from 'node:assert/strict';
import {
planSweepback,
+ reclaimableSol,
MIN_SWEEP_SOL,
DRAIN_HEADROOM_LAMPORTS,
} from '../api/_lib/economy-sweepback.js';
+
+// reclaimableSol: the emergency consolidation's sizing. The load-bearing property
+// is anti-oscillation: it must NEVER leave an engine below minSol, or the topup
+// (which funds anything below minSol) would immediately re-fund it and the two
+// crons would ping-pong, burning fees forever.
+test('reclaimableSol never leaves an engine below its floor', () => {
+ for (const [current, min] of [
+ [3, 1], [0.6, 0.2], [1.076, 1], [0.05, 0.02], [0.3, 0.03], [10, 0.05],
+ ]) {
+ const r = reclaimableSol(current, min);
+ assert.ok(current - r >= min, `left ${current - r} below floor ${min}`);
+ }
+});
+
+test('reclaimableSol pulls the idle excess above the floor+buffer', () => {
+ // launcher floored at 1 SOL sitting on 3 → reclaim ~1.9 (leaves 1.1 = min + 10%)
+ assert.ok(Math.abs(reclaimableSol(3, 1) - 1.9) < 1e-6);
+ // a2a floored at 0.02 on 0.035 → keep 0.025, reclaim 0.01 (== dust floor)
+ assert.ok(reclaimableSol(0.035, 0.02) >= MIN_SWEEP_SOL - 1e-9);
+});
+
+test('reclaimableSol returns 0 when the engine is at or below its floor', () => {
+ assert.equal(reclaimableSol(1, 1), 0); // exactly at floor
+ assert.equal(reclaimableSol(0.5, 1), 0); // below floor
+ assert.equal(reclaimableSol(1.05, 1), 0); // within the buffer, not worth dust
+});
import { ECONOMY_MASTER_ADDRESS } from '../api/_lib/economy-master.js';
import { buildSweepbackRows, hashEntry } from '../api/_lib/economy-ledger.js';
import { SOLANA_SIGNERS } from '../api/_lib/solana-signers.js';