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.

16.2 KB
Raw Blame
// Tests for the caller-side fee admission gate
// (assessFeeAdmission in api/_lib/x402/wallet-fee-meter.js).
//
// The settle-path meter refuses an unfundable settle at the LAST step of the
// x402 handshake, after the caller has already read the receiver ATA, signed a
// transfer, and paid for a facilitator `verify` that simulates against an RPC
// node. Measured on production 2026-08-01: 85,264 of 90,041 daily settle
// attempts were refused with `fee_runway_exhausted`, each having burned that
// work first. assessFeeAdmission answers the same question before any of it.
//
// These tests pin the three properties that make the gate safe to put in front
// of every paid call:
//   1. It never refuses a call the settle path would have funded (parity).
//   2. It fails OPEN on every unreadable input, so it can only remove doomed
//      work, never block a fundable payment.
//   3. Its refusal cache collapses a storm without outliving the UTC-midnight
//      budget reset that ends the refusal.
//
// DB, alerts, allowlist, RPC, and env are mocked at the module boundary the way
// the neighboring x402 tests mock theirs; time is driven with fake timers so the
// cache-window tests never sleep.

import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';

const h = vi.hoisted(() => ({
	sql: vi.fn(),
	sendOpsAlert: vi.fn(async () => {}),
	ringAllowedAddresses: vi.fn(),
	sponsorSolLamports: vi.fn(),
	solanaConnection: vi.fn(() => ({})),
}));

vi.mock('../api/_lib/db.js', () => ({ sql: h.sql }));
vi.mock('../api/_lib/alerts.js', () => ({ sendOpsAlert: h.sendOpsAlert }));
vi.mock('../api/_lib/env.js', () => ({ env: { SOLANA_RPC_URL: 'https://rpc.test' } }));
vi.mock('../api/_lib/solana/connection.js', () => ({ solanaConnection: h.solanaConnection }));
vi.mock('../api/_lib/x402/ring-allowlist.js', () => ({
	ringAllowedAddresses: h.ringAllowedAddresses,
}));
// Pinning the floor keeps the budget arithmetic readable below; stubbing the
// balance read keeps the heavy web3 module and a real RPC out of a unit test.
vi.mock('../api/_lib/x402/self-facilitator.js', () => ({
	SPONSOR_SOL_FLOOR_LAMPORTS: 20_000_000,
	sponsorSolLamports: h.sponsorSolLamports,
}));

const {
	assessFeeAdmission,
	reserveFeeAdmissions,
	facilitatorFeeMeter,
	resetWalletFeeMeterCaches,
} = await import('../api/_lib/x402/wallet-fee-meter.js');

const GOV = 'GovWa11etAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';
const ORGANIC = 'OrganicBuyer111111111111111111111111111111111';

// With the mocked 0.02 SOL floor and runwayDays 1, this balance yields exactly a
// 1_000_000-lamport daily budget: (21_000_000 - 20_000_000) / 1.
const SOL_FOR_1M_BUDGET = 21_000_000;

// Pacing is exercised in its own test; the arithmetic tests turn it off so the
// budget is the full daily figure regardless of what time the suite runs.
const CFG = {
	enabled: true,
	runwayDays: 1,
	minBudgetLamports: 0,
	spentCacheMs: 20_000,
	paceDay: false,
	paceMinSliceLamports: 0,
};

const T0 = new Date('2026-07-28T12:00:00.000Z').getTime();

function admit(overrides = {}) {
	return assessFeeAdmission({
		feeWalletB58: GOV, estFeeLamports: 5_000, config: CFG, ...overrides,
	});
}

beforeEach(() => {
	vi.useFakeTimers();
	vi.setSystemTime(T0);
	resetWalletFeeMeterCaches();
	h.sql.mockReset().mockResolvedValue([{ spent: '0' }]);
	h.sendOpsAlert.mockReset().mockResolvedValue(undefined);
	h.ringAllowedAddresses.mockReset().mockResolvedValue(new Set([GOV]));
	h.sponsorSolLamports.mockReset().mockResolvedValue(SOL_FOR_1M_BUDGET);
	h.solanaConnection.mockClear();
});

afterEach(() => {
	vi.useRealTimers();
});

describe('scope: only platform-controlled wallets are gated', () => {
	it('admits an organic buyer without reading its balance or the ledger', async () => {
		const v = await admit({ feeWalletB58: ORGANIC });
		expect(v.ok).toBe(true);
		// Refusing an external self-payer would be refusing revenue, and pricing it
		// would cost an RPC read per organic call for a verdict we never act on.
		expect(h.sponsorSolLamports).not.toHaveBeenCalled();
		expect(h.sql).not.toHaveBeenCalled();
	});

	it('admits everything when the governor is disabled', async () => {
		const v = await admit({ config: { ...CFG, enabled: false } });
		expect(v.ok).toBe(true);
		expect(h.sponsorSolLamports).not.toHaveBeenCalled();
	});

	it('admits when the caller has no fee wallet to price', async () => {
		expect((await admit({ feeWalletB58: null })).ok).toBe(true);
		expect(h.sponsorSolLamports).not.toHaveBeenCalled();
	});
});

describe('budget arithmetic', () => {
	it('refuses once the day spend plus this fee exceeds the budget', async () => {
		h.sql.mockResolvedValue([{ spent: '996000' }]);
		const v = await admit({ estFeeLamports: 5_000 });
		expect(v.ok).toBe(false);
		expect(v.reason).toBe('fee_runway_exhausted:996000+5000>1000000');
		expect(v.budgetLamports).toBe(1_000_000);
		expect(v.spentTodayLamports).toBe(996_000);
	});

	it('admits while the fee still fits', async () => {
		h.sql.mockResolvedValue([{ spent: '990000' }]);
		expect((await admit({ estFeeLamports: 5_000 })).ok).toBe(true);
	});

	it('the boundary is inclusive: exactly filling the budget still admits', async () => {
		h.sql.mockResolvedValue([{ spent: '995000' }]);
		expect((await admit({ estFeeLamports: 5_000 })).ok).toBe(true);
	});

	it('honors intraday pacing when enabled: midday unlocks half the day budget', async () => {
		// T0 is 12:00 UTC, so half of the 1_000_000 budget (500_000) is unlocked.
		// A spend that fits the full day budget but not the paced slice is refused.
		h.sql.mockResolvedValue([{ spent: '600000' }]);
		const v = await admit({
			estFeeLamports: 5_000,
			config: { ...CFG, paceDay: true, paceMinSliceLamports: 0 },
		});
		expect(v.ok).toBe(false);
		expect(v.budgetLamports).toBe(500_000);
	});
});

describe('fail-open contract', () => {
	it('admits when the balance read throws (RPC lane exhausted)', async () => {
		h.sponsorSolLamports.mockRejectedValue(new Error('429 rate limited'));
		const v = await admit();
		expect(v.ok).toBe(true);
		// A wallet we cannot price is not a wallet that is broke. The settle path's
		// own meter and hard SOL floor still stand behind this.
		expect(h.sql).not.toHaveBeenCalled();
	});

	it('admits when the settle ledger is unreadable', async () => {
		h.sql.mockRejectedValue(new Error('db down'));
		expect((await admit()).ok).toBe(true);
	});

	it('admits when the allowlist is unreachable and nothing was cached', async () => {
		h.ringAllowedAddresses.mockRejectedValue(new Error('db down'));
		expect((await admit()).ok).toBe(true);
	});
});

describe('verdict cache', () => {
	it('serves a refusal from cache for a minute instead of re-reading every call', async () => {
		h.sql.mockResolvedValue([{ spent: '996000' }]);
		const first = await admit();
		expect(first.ok).toBe(false);
		expect(first.cached).toBe(false);

		// The storm this collapses is ~3,700 calls an hour, all with the same answer.
		h.sponsorSolLamports.mockClear();
		for (let i = 0; i < 5; i++) {
			const v = await admit();
			expect(v.ok).toBe(false);
			expect(v.cached).toBe(true);
		}
		expect(h.sponsorSolLamports).not.toHaveBeenCalled();
	});

	it('re-reads after the refusal window so a top-up reopens the rail', async () => {
		h.sql.mockResolvedValue([{ spent: '996000' }]);
		expect((await admit()).ok).toBe(false);

		vi.setSystemTime(T0 + 60_001);
		// The wallet was topped up: same spend, much larger budget.
		h.sponsorSolLamports.mockResolvedValue(SOL_FOR_1M_BUDGET + 10_000_000);
		const v = await admit();
		expect(v.ok).toBe(true);
		expect(v.cached).toBe(false);
	});

	it('caches an admission only for the spent-read window', async () => {
		expect((await admit()).cached).toBe(false);
		expect((await admit()).cached).toBe(true);

		// An admission decays faster than a refusal: the budget drains as settles
		// land, so a stale "yes" is the dangerous direction.
		vi.setSystemTime(T0 + 20_001);
		expect((await admit()).cached).toBe(false);
	});

	it('never serves a verdict across the UTC-midnight budget reset', async () => {
		const beforeMidnight = new Date('2026-07-28T23:59:40.000Z').getTime();
		vi.setSystemTime(beforeMidnight);
		h.sql.mockResolvedValue([{ spent: '996000' }]);
		expect((await admit()).ok).toBe(false);

		// 30s later is a new UTC day: the refusal cache would still be inside its
		// 60s TTL, but the budget has reset, so the verdict must be recomputed.
		vi.setSystemTime(beforeMidnight + 30_000);
		h.sql.mockResolvedValue([{ spent: '0' }]);
		const v = await admit();
		expect(v.ok).toBe(true);
		expect(v.cached).toBe(false);
	});
});

describe('headroom: a cached admission funds only what the budget can pay', () => {
	// Production 2026-09-24, paced wallet: every fresh read found room for about one
	// settle, then the cached "yes" admitted every call for 20 seconds. ~230 settles
	// an hour landed and ~1,700 an hour were refused at the END of the handshake by
	// the settle-path meter. The snapshot now spends each admission's fee out of its
	// own headroom, so the calls past it are held back before any work is done.
	it('admits exactly the settles that fit, then refuses from the same snapshot', async () => {
		// 1_000_000 budget, 985_000 spent: room for three 5_000-lamport settles.
		h.sql.mockResolvedValue([{ spent: '985000' }]);
		const verdicts = [];
		for (let i = 0; i < 6; i++) verdicts.push(await admit({ estFeeLamports: 5_000 }));
		expect(verdicts.map((v) => v.ok)).toEqual([true, true, true, false, false, false]);
		// The refusal names the arithmetic, including what this window already admitted.
		expect(verdicts[3].reason).toBe('fee_runway_exhausted:1000000+5000>1000000');
		// One ledger read and one balance read served the whole burst.
		expect(h.sql).toHaveBeenCalledTimes(1);
		expect(h.sponsorSolLamports).toHaveBeenCalledTimes(1);
	});

	it('a headroom refusal holds for the refusal window, then a fresh read reopens the rail', async () => {
		h.sql.mockResolvedValue([{ spent: '995000' }]);
		expect((await admit()).ok).toBe(true);
		expect((await admit()).ok).toBe(false);

		// Still inside the 20s admission window, but the snapshot is now a refusal:
		// it must not flip back to "yes" while nothing has changed.
		vi.setSystemTime(T0 + 15_000);
		expect((await admit()).ok).toBe(false);

		vi.setSystemTime(T0 + 15_000 + 60_001);
		h.sponsorSolLamports.mockResolvedValue(SOL_FOR_1M_BUDGET + 10_000_000);
		const v = await admit();
		expect(v.ok).toBe(true);
		expect(v.cached).toBe(false);
	});

	it('concurrent callers share one snapshot and still admit no more than it funds', async () => {
		// A ring tick fires its calls in parallel. Before single-flight, every one of
		// them missed the cache together, read the same headroom, and all said yes.
		h.sql.mockResolvedValue([{ spent: '990000' }]);
		const verdicts = await Promise.all(Array.from({ length: 8 }, () => admit({ estFeeLamports: 5_000 })));
		expect(verdicts.filter((v) => v.ok)).toHaveLength(2);
		expect(h.sql).toHaveBeenCalledTimes(1);
		expect(h.sponsorSolLamports).toHaveBeenCalledTimes(1);
	});

	it('an unreadable ledger still fails open for every caller in the window', async () => {
		h.sql.mockRejectedValue(new Error('db down'));
		for (let i = 0; i < 4; i++) expect((await admit()).ok).toBe(true);
	});
});

describe('parity with the settle-path meter', () => {
	// The gate's whole value is that it predicts the settle path's answer. A laxer
	// admission re-creates the wasted handshakes it exists to remove; a stricter
	// one silently drops settles the rail could have funded. Pin them together so
	// a future change to either budget path has to move both.
	const cases = [
		{ name: 'refuses', spent: '996000', est: 5_000 },
		{ name: 'admits', spent: '100000', est: 5_000 },
		{ name: 'at the boundary', spent: '995000', est: 5_000 },
		{ name: 'with an unreadable ledger', spent: null, est: 5_000 },
	];

	for (const c of cases) {
		it(`agrees with facilitatorFeeMeter when it ${c.name}`, async () => {
			if (c.spent === null) h.sql.mockRejectedValue(new Error('db down'));
			else h.sql.mockResolvedValue([{ spent: c.spent }]);

			const meterVerdict = await facilitatorFeeMeter({ config: CFG })({
				feeWalletB58: GOV,
				solLamports: SOL_FOR_1M_BUDGET,
				estFeeLamports: c.est,
			});
			resetWalletFeeMeterCaches();
			const admissionVerdict = await admit({ estFeeLamports: c.est });

			expect(admissionVerdict.ok).toBe(meterVerdict.ok);
			expect(admissionVerdict.reason ?? null).toBe(meterVerdict.reason ?? null);
		});
	}
});

// The batch gate the x402 seed cron sizes its tick with. Production 2026-09-29:
// 60 sponsor-mode tips every two minutes against a budget that funded about one,
// 43,662 governor refusals a day at the facilitator, and the paid pipelines on
// the same wallet skipped about 7,400 times a day because the seeder got there
// first. reserveFeeAdmissions admits only what fits after the kept headroom.
describe('reserveFeeAdmissions: batch sizing against the shared snapshot', () => {
	const reserve = (overrides = {}) => reserveFeeAdmissions({
		feeWalletB58: GOV, estFeeLamports: 10_000, count: 60, config: CFG, ...overrides,
	});

	it('admits only the settles the budget can fund', async () => {
		h.sql.mockResolvedValue([{ spent: '970000' }]);
		const r = await reserve();
		expect(r).toMatchObject({ admitted: 3, governed: true, reason: null, budgetLamports: 1_000_000 });
	});

	it('leaves the kept headroom, without refusing anyone else', async () => {
		h.sql.mockResolvedValue([{ spent: '900000' }]);
		const r = await reserve({ keepLamports: 200_000 });
		expect(r.admitted).toBe(0);
		expect(r.reason).toMatch(/^fee_budget_kept_for_paid_calls:/);
		// The budget still has 100,000 lamports; a paid call must still get in.
		const paid = await admit({ estFeeLamports: 10_000 });
		expect(paid.ok).toBe(true);
	});

	it('debits admitted fees from the snapshot other callers read', async () => {
		h.sql.mockResolvedValue([{ spent: '950000' }]);
		expect((await reserve()).admitted).toBe(5);
		// The seeder took every settle the budget funds; the next caller is refused
		// from the same snapshot without another ledger read.
		const next = await admit({ estFeeLamports: 10_000 });
		expect(next.ok).toBe(false);
		expect(next.reason).toMatch(/^fee_runway_exhausted:/);
		expect(h.sql).toHaveBeenCalledTimes(1);
	});

	it('an exhausted budget becomes the same cached refusal every caller sees', async () => {
		h.sql.mockResolvedValue([{ spent: '1000000' }]);
		const r = await reserve({ keepLamports: 200_000 });
		expect(r.admitted).toBe(0);
		expect(r.reason).toMatch(/^fee_runway_exhausted:/);
		const next = await admit({ estFeeLamports: 5_000 });
		expect(next).toMatchObject({ ok: false, cached: true });
	});

	it('fails open: ungoverned wallet, unreadable balance, disabled governor', async () => {
		expect((await reserve({ feeWalletB58: ORGANIC })).admitted).toBe(60);
		expect((await reserve({ config: { ...CFG, enabled: false } })).admitted).toBe(60);
		resetWalletFeeMeterCaches();
		h.sponsorSolLamports.mockRejectedValue(new Error('rpc down'));
		expect((await reserve()).admitted).toBe(60);
	});
});

// Pipelines that hand-build a sponsor-mode payment (the MCP canary, the
// builder-code and payment-proof audits) ask through admitSponsorSettle(). It
// must price at the sponsor-mode worst case, two signatures, because that is
// what the settle-path meter will charge against the same budget.
describe('admitSponsorSettle: hand-built sponsor payments ask the same gate', () => {
	it('prices at the two-signature sponsor fee and gates the accept\'s fee payer', async () => {
		const { admitSponsorSettle } = await import('../api/_lib/x402/pay.js');
		// 995,000 spent of a 1,000,000 budget: a self-pay fee (5,000) would fit,
		// the sponsor-mode fee (10,000) does not.
		h.sql.mockResolvedValue([{ spent: '995000' }]);
		vi.stubEnv('X402_WALLET_FEE_RUNWAY_DAYS', '1');
		vi.stubEnv('X402_WALLET_FEE_MIN_BUDGET_LAMPORTS', '0');
		vi.stubEnv('X402_WALLET_FEE_PACE_DAY', 'false');
		try {
			const v = await admitSponsorSettle({ accept: { extra: { feePayer: GOV } } });
			expect(v.ok).toBe(false);
			expect(v.reason).toBe('fee_runway_exhausted:995000+10000>1000000');
		} finally {
			vi.unstubAllEnvs();
		}
	});

	it('admits when the accept names no fee payer to price', async () => {
		const { admitSponsorSettle } = await import('../api/_lib/x402/pay.js');
		expect((await admitSponsorSettle({ accept: { extra: {} } })).ok).toBe(true);
		expect(h.sponsorSolLamports).not.toHaveBeenCalled();
	});
});