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(economy): exclude master-aliased signers from refill targets

nirholas35444eb511c0
diff --git a/api/cron/treasury-topup.js b/api/cron/treasury-topup.js
index ce9f96789..8c8aff4fb 100644
--- a/api/cron/treasury-topup.js
+++ b/api/cron/treasury-topup.js
@@ -72,6 +72,13 @@ export default wrapCron(async (req, res) => {
// never targets.
const targets = [];
const errors = [];
+ // A non-master signer whose SECRET resolves to the master's own wallet (env
+ // aliasing, e.g. THREE_BUYBACK_SECRET_KEY_B64 set to the master key). Its
+ // balance IS the master's balance, so it can never be a refill target — before
+ // this check, every sweep listed it as underfunded, filterToRegistry rejected
+ // the master-to-itself transfer, and the pair of alerts re-fired forever
+ // (~10k repeats each) while misreporting the count of genuinely dry engines.
+ const masterAliased = [];
// Fallback env vars can resolve two registry entries to the SAME wallet
// (e.g. x402-ring-payer falling back to the agent key) — top it up once.
const seenPubkeys = new Set();
@@ -83,6 +90,10 @@ export default wrapCron(async (req, res) => {
errors.push({ name: spec.name, reason: 'secret_decode_failed' });
continue;
}
+ if (resolved.pubkey === ECONOMY_MASTER_ADDRESS) {
+ masterAliased.push(spec.name);
+ continue;
+ }
if (seenPubkeys.has(resolved.pubkey)) continue;
seenPubkeys.add(resolved.pubkey);
let lamports;
@@ -202,6 +213,7 @@ export default wrapCron(async (req, res) => {
master_operating_sol: masterOperatingSol,
reclaim,
fuel,
+ master_aliased: masterAliased,
read_errors: errors,
});
}
@@ -260,6 +272,17 @@ export default wrapCron(async (req, res) => {
);
}
+ // A signer aliased to the master is a standing config condition, not a
+ // per-sweep event: say it once per name (stable signature → one ops_alerts
+ // row), and never count it among dry engines.
+ for (const name of masterAliased) {
+ await sendOpsAlert(
+ `ℹ️ Signer "${name}" resolves to the economy master wallet`,
+ `${name}'s secret decodes to the master (${ECONOMY_MASTER_ADDRESS}), so its balance rides the master's and it is excluded from refill targets. If this aliasing is unintended, point ${name}'s env secret at its own keypair.`,
+ { signature: `economy-alias-master:${name}`, severity: 'info' },
+ );
+ }
+
// An off-registry target reaching the sweep means a bad caller/target list.
// No SOL moved (the allowlist blocked it), but a human should know why.
for (const r of result.rejected || []) {
@@ -284,6 +307,7 @@ export default wrapCron(async (req, res) => {
master_deficit_sol: Number(masterDeficitSol.toFixed(6)),
reclaim,
fuel,
+ master_aliased: masterAliased,
read_errors: errors,
run_id: runId,
ledger,
diff --git a/tests/treasury-topup-alias.test.js b/tests/treasury-topup-alias.test.js
new file mode 100644
index 000000000..b9bcdf966
--- /dev/null
+++ b/tests/treasury-topup-alias.test.js
@@ -0,0 +1,110 @@
+// A signer whose secret resolves to the economy master's own wallet must never
+// become a refill target.
+//
+// Production hit this for three days in July 2026: THREE_BUYBACK_SECRET_KEY_B64
+// held the master's keypair, so every 30-minute sweep listed "three-buyback" as
+// an underfunded engine, filterToRegistry refused the master-to-itself transfer,
+// and the paired alerts ("could not refill 3 engine(s)" / "blocked an
+// off-registry target (is_master)") re-fired ~10k times each while the count of
+// genuinely dry engines read one too high. The alias is a standing config
+// condition, not a per-sweep event — it belongs in `master_aliased`, out of
+// `targets`, and in one info alert, not two warn storms.
+//
+// Exercised through the real handler in ?dry=1 mode: same signer resolution and
+// target building as a live sweep, no SOL movement, no alerts, no ledger.
+
+import { describe, it, expect, vi, beforeEach } from 'vitest';
+
+const { MASTER, ENGINE } = vi.hoisted(() => ({
+ MASTER: 'WwwuGbqHrwF5RG89KhUbmRWEvjnRH9k5kVM5p7T3WwW',
+ ENGINE: 'EngineEngineEngineEngineEngineEngineEngine1',
+}));
+
+vi.mock('../api/_lib/solana-signers.js', () => ({
+ SOLANA_SIGNERS: [
+ { name: 'economy-master', env: 'ECONOMY_MASTER_SECRET_BASE58', isMaster: true, minSol: 0.05, network: 'mainnet' },
+ { name: 'three-buyback', env: 'THREE_BUYBACK_SECRET_KEY_B64', minSol: 0.05, network: 'mainnet' },
+ { name: 'pump-cron-relayer', env: 'PUMP_RELAYER_SECRET', minSol: 0.05, network: 'mainnet' },
+ ],
+ resolveSignerPubkey: vi.fn(async (spec) => {
+ // The alias under test: three-buyback's secret decodes to the MASTER wallet.
+ if (spec.name === 'three-buyback') return { configured: true, pubkey: MASTER };
+ if (spec.name === 'pump-cron-relayer') return { configured: true, pubkey: ENGINE };
+ return { configured: true, pubkey: MASTER };
+ }),
+}));
+
+vi.mock('../api/_lib/economy-master.js', () => ({
+ ECONOMY_MASTER_ADDRESS: MASTER,
+ RESERVE_SOL: 0.05,
+ RUN_CAP_SOL: 0.5,
+ PER_TOPUP_MAX_SOL: 0.2,
+ sweepTopUps: vi.fn(async ({ targets }) => ({
+ configured: true,
+ plan: targets.map((t) => ({ name: t.name, pubkey: t.pubkey, sol: 0.1 })),
+ skipped: [],
+ rejected: [],
+ funded: [],
+ failed: [],
+ spentSol: 0,
+ masterSol: 0.02,
+ spendableSol: 0,
+ })),
+}));
+
+vi.mock('../api/_lib/economy-ledger.js', () => ({ recordSweep: vi.fn(async () => ({ wrote: 0 })) }));
+vi.mock('../api/_lib/economy-fuel.js', () => ({
+ refuelMasterFromUsdc: vi.fn(async () => ({ acted: false, reason: 'no_spare_usdc' })),
+}));
+vi.mock('../api/_lib/economy-sweepback.js', () => ({
+ reclaimIdleSol: vi.fn(async () => ({ reclaimedSol: 0, moves: [], skipped: [], failed: [] })),
+}));
+vi.mock('../api/_lib/alerts.js', () => ({ sendOpsAlert: vi.fn(async () => {}) }));
+vi.mock('../api/_lib/solana/connection.js', () => ({
+ // Every wallet reads under-floor so both candidate specs would qualify as
+ // targets if the alias guard failed.
+ solanaConnection: () => ({ getBalance: vi.fn(async () => 10_000_000) }),
+}));
+
+import handler from '../api/cron/treasury-topup.js';
+
+function mockRes() {
+ return {
+ statusCode: 200,
+ _headers: {},
+ _body: null,
+ setHeader(k, v) { this._headers[k.toLowerCase()] = v; },
+ getHeader(k) { return this._headers[k.toLowerCase()]; },
+ end(b) { this._body = b || ''; this.writableEnded = true; },
+ writableEnded: false,
+ headersSent: false,
+ };
+}
+
+beforeEach(() => {
+ process.env.CRON_SECRET = 'test-cron-secret';
+});
+
+describe('treasury-topup master aliasing', () => {
+ it('excludes a master-aliased signer from refill targets and reports it', async () => {
+ const res = mockRes();
+ await handler(
+ {
+ method: 'GET',
+ url: '/api/cron/treasury-topup?dry=1',
+ headers: { authorization: 'Bearer test-cron-secret' },
+ socket: { remoteAddress: '127.0.0.1' },
+ },
+ res,
+ );
+
+ const body = JSON.parse(res._body);
+ expect(body.ok).toBe(true);
+ expect(body.dry_run).toBe(true);
+
+ // The alias is surfaced, not treated as a dry engine.
+ expect(body.master_aliased).toEqual(['three-buyback']);
+ expect(body.targets.map((t) => t.name)).toEqual(['pump-cron-relayer']);
+ expect(body.targets.some((t) => t.pubkey === MASTER)).toBe(false);
+ });
+});