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): stop reporting a deliberate fee-budget pause as a server fault

nirholas90f767277b96
diff --git a/api/_lib/x402-spec.js b/api/_lib/x402-spec.js
index 2a2fbd0d7..e0514944e 100644
--- a/api/_lib/x402-spec.js
+++ b/api/_lib/x402-spec.js
@@ -1169,6 +1169,22 @@ export async function settlePayment(args) {
503,
);
}
+ // The wallet fee governor's refusal is the same class of answer for the same
+ // reason: nothing is broken, the platform is deliberately pacing its own SOL
+ // burn and the budget refills (at the UTC reset, or the moment the fee wallet
+ // is topped up). It was the one deliberate throttle the branch above missed,
+ // and at volume it IS the platform's error rate: 15,619 of the 20,030
+ // `http_502` rows the autonomous loop recorded in the 48h to 2026-08-06 were
+ // this refusal wearing a server-fault status code. Same treatment: 503 +
+ // retryable code, so buyers back off instead of reading a funding cap as an
+ // outage. Genuinely unexplained settle failures still stay 502.
+ if (/^fee_runway_exhausted/.test(reason)) {
+ throw new X402Error(
+ 'settlement_unavailable',
+ `settlement paced: ${reason} (fee wallet spent today's SOL fee budget; retry after it refills)`,
+ 503,
+ );
+ }
throw new X402Error('settle_failed', `settle failed: ${reason}`, 502);
}
// Defense-in-depth: a compromised facilitator could return success for
diff --git a/api/cron/x402-autonomous-loop.js b/api/cron/x402-autonomous-loop.js
index b9b01ea21..888a66ad1 100644
--- a/api/cron/x402-autonomous-loop.js
+++ b/api/cron/x402-autonomous-loop.js
@@ -42,7 +42,10 @@ import {
fetchWithTimeout,
parseSolanaAccept,
buildPaymentTx,
+ ringFeeConfig,
+ expectedFeeLamports,
} from '../_lib/x402/pay.js';
+import { assessFeeAdmission } from '../_lib/x402/wallet-fee-meter.js';
import {
getFullRegistry,
MAX_PER_TICK,
@@ -567,6 +570,43 @@ export default wrapCron(async (req, res) => {
continue;
}
+ // Fee-budget admission — the caller-side twin of the settle path's wallet
+ // fee governor, and the guard this loop was missing while the ring's
+ // payX402() already had it (api/_lib/x402/pay.js). Once the fee wallet has
+ // spent its daily SOL budget the facilitator refuses every settle, so an
+ // ATA read, a signature, a facilitator verify (which simulates against an
+ // RPC node) and the POST itself all buy a guaranteed refusal at the end.
+ // That round trip was the whole of this loop's 502 rate: 15,619 of the
+ // 20,030 `http_502` rows in the 48h to 2026-08-06 were this exact refusal
+ // arriving after a full handshake. Skipping here removes only doomed work:
+ // assessFeeAdmission() fails open whenever it cannot price the call, and
+ // it settles nothing extra when the budget is healthy.
+ const feeConfig = ringFeeConfig(0, { selfPay: false });
+ const admission = await assessFeeAdmission({
+ feeWalletB58: accept.extra.feePayer,
+ estFeeLamports: expectedFeeLamports({
+ selfPay: false,
+ priorityMicrolamports: feeConfig.microLamports,
+ cuLimit: feeConfig.cuLimit,
+ }),
+ connection: conn,
+ });
+ if (!admission.ok) {
+ errorMsg = admission.reason || 'fee_runway_exhausted';
+ results.push({ id: entry.id, status: 'skip', reason: errorMsg });
+ await setCooldown(redis, entry); // budget refills on a top-up or the UTC reset, not mid-cooldown
+ // Recorded, not silently dropped: x402-settle-health.js reads this table
+ // and counts `fee_runway_exhausted` as governorSkips so a paced rail
+ // reports "top the fee wallet up" instead of the `unknown` verdict an
+ // empty window produces. A skip that leaves no row is indistinguishable
+ // from a rail nobody used.
+ await recordLog(runId, entry, {
+ amountAtomic: 0, txSig: null, responseData: null,
+ durationMs: Date.now() - t0, success: false, errorMsg, endpointUrl,
+ });
+ continue;
+ }
+
// Step 2: check if receiver ATA exists (optimization — share lookup)
const receiverAta = getAssociatedTokenAddressSync(
new PublicKey(accept.asset), new PublicKey(accept.payTo),
diff --git a/tests/x402-floor-degradation.test.js b/tests/x402-floor-degradation.test.js
index 9305e127f..4a1b8d211 100644
--- a/tests/x402-floor-degradation.test.js
+++ b/tests/x402-floor-degradation.test.js
@@ -47,6 +47,15 @@ describe('settlePayment floor classification', () => {
expect(err).toMatchObject({ code: 'settlement_unavailable', status: 503 });
});
+ // The wallet fee governor refuses for the same reason class and was missed by
+ // the floor branch, so it kept answering 502 long after the floor stopped:
+ // 15,619 of the autonomous loop's 20,030 `http_502` rows in the 48h to
+ // 2026-08-06 were this one refusal wearing a server-fault status.
+ it('maps fee_runway_exhausted to 503 settlement_unavailable', async () => {
+ const err = await settleAgainst('fee_runway_exhausted:10132243+10000>10000000');
+ expect(err).toMatchObject({ code: 'settlement_unavailable', status: 503 });
+ });
+
it('keeps unexplained settle failures as 502 settle_failed', async () => {
const err = await settleAgainst('facilitator exploded');
expect(err).toMatchObject({ code: 'settle_failed', status: 502 });