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): unlock proportional funding for a thinly funded master

fudjia507b9b939a1e
diff --git a/api/_lib/economy-master.js b/api/_lib/economy-master.js
index 49f8b81e6..168b4746b 100644
--- a/api/_lib/economy-master.js
+++ b/api/_lib/economy-master.js
@@ -44,8 +44,14 @@ function num(envName, dflt) {
return Number.isFinite(v) && v >= 0 ? v : dflt;
}
+// The floor exists to cover the master's OWN rent-exemption (~0.00089 SOL for a
+// System account) and transaction fees (~0.000005 SOL each) — not to gate the
+// master behind an arbitrary balance. 0.02 SOL covers rent-exemption plus
+// thousands of fee-only transactions, so the guard stays real even when the
+// master is funded thin: a 0.3 SOL master should be able to spend ~0.28 of it,
+// not sit fully locked behind a 1 SOL floor sized for a much larger treasury.
/** Never spend the master below this — its own working reserve + rent + fees. */
-export const RESERVE_SOL = num('ECONOMY_MASTER_RESERVE_SOL', 1);
+export const RESERVE_SOL = num('ECONOMY_MASTER_RESERVE_SOL', 0.02);
/** Most SOL the master will move to any single engine in one sweep. */
export const PER_TOPUP_MAX_SOL = num('ECONOMY_MASTER_PER_TOPUP_MAX_SOL', 0.5);
/** Most SOL the master will move across all engines in one sweep. */
@@ -104,11 +110,16 @@ export function planTopUps(masterSol, targets) {
skipped.push({ name: t.name, reason: 'below_dust_threshold' });
continue;
}
- const want = Math.min(deficit, PER_TOPUP_MAX_SOL);
- if (total + want > runCap + 1e-9) {
+ const remaining = round(runCap - total);
+ if (remaining < MIN_TOPUP_SOL) {
skipped.push({ name: t.name, reason: 'run_cap_reached' });
continue;
}
+ // Clamp to whatever's left in the run cap rather than skipping the engine
+ // outright when its full want exceeds it — a thin budget should still fund
+ // the neediest engines as far as it goes, not sit unspent because the first
+ // engine's ask was bigger than the whole cap.
+ const want = round(Math.min(deficit, PER_TOPUP_MAX_SOL, remaining));
plan.push({ name: t.name, pubkey: t.pubkey, sol: want });
total = round(total + want);
}
diff --git a/tests/economy-master.test.js b/tests/economy-master.test.js
index 9a1523eea..ce9d079ba 100644
--- a/tests/economy-master.test.js
+++ b/tests/economy-master.test.js
@@ -11,15 +11,16 @@ import {
RUN_CAP_SOL,
} from '../api/_lib/economy-master.js';
-// Defaults (no env overrides): reserve 1, per-engine 0.5, run cap 2.
+// Defaults (no env overrides): reserve 0.02 (rent-exemption + fee headroom,
+// not a business-scale gate), per-engine 0.5, run cap 2.
test('defaults are the documented guard values', () => {
- assert.equal(RESERVE_SOL, 1);
+ assert.equal(RESERVE_SOL, 0.02);
assert.equal(PER_TOPUP_MAX_SOL, 0.5);
assert.equal(RUN_CAP_SOL, 2);
});
test('reserve floor: a master below reserve funds nothing', () => {
- const { plan, totalSol, spendableSol } = planTopUps(0.5, [
+ const { plan, totalSol, spendableSol } = planTopUps(0.01, [
{ name: 'a', pubkey: 'A', currentSol: 0, refillToSol: 0.3 },
]);
assert.equal(spendableSol, 0);
@@ -27,6 +28,17 @@ test('reserve floor: a master below reserve funds nothing', () => {
assert.equal(totalSol, 0);
});
+test('a thinly funded master still spends nearly all of it (no oversized reserve gate)', () => {
+ // 0.302 SOL master — the real prod balance this fix targets. Should be
+ // able to spend ~0.282 of it, not sit fully locked behind a 1 SOL floor.
+ const { plan, totalSol, spendableSol } = planTopUps(0.302, [
+ { name: 'engine', pubkey: 'E', currentSol: 0, refillToSol: 5 },
+ ]);
+ assert.ok(spendableSol > 0.28 && spendableSol < 0.29);
+ assert.equal(plan.length, 1);
+ assert.equal(totalSol, spendableSol);
+});
+
test('per-engine cap: one engine gets at most PER_TOPUP_MAX_SOL', () => {
const { plan } = planTopUps(10, [
{ name: 'greedy', pubkey: 'G', currentSol: 0, refillToSol: 3 },
@@ -42,12 +54,31 @@ test('per-run cap: a sweep spends at most RUN_CAP_SOL total', () => {
currentSol: 0,
refillToSol: 0.5,
}));
- const { plan, totalSol, skipped } = planTopUps(10, targets); // spendable 9, runCap 2
+ const { plan, totalSol, skipped } = planTopUps(10, targets); // spendable ~9.98, runCap 2
assert.equal(totalSol, 2);
assert.equal(plan.length, 4); // 4 × 0.5 = 2.0
assert.ok(skipped.some((s) => s.reason === 'run_cap_reached'));
});
+test('per-run cap: a want exceeding the remaining budget is clamped, not skipped', () => {
+ // master 1.32 → spendable 1.3 → runCap 1.3 (below the fixed 2 SOL cap, so the
+ // balance itself is the binding constraint). Three engines each want the
+ // per-engine max (0.5), but only 1.3 total exists: A and B get funded in
+ // full, C should get the LEFTOVER 0.3 rather than being skipped outright —
+ // a thin run cap should still be spent down, not wasted.
+ const { plan, totalSol, skipped } = planTopUps(1.32, [
+ { name: 'a', pubkey: 'A', currentSol: 0, refillToSol: 1.0 },
+ { name: 'b', pubkey: 'B', currentSol: 0, refillToSol: 1.0 },
+ { name: 'c', pubkey: 'C', currentSol: 0, refillToSol: 1.0 },
+ ]);
+ assert.equal(totalSol, 1.3);
+ assert.equal(plan.length, 3);
+ assert.equal(plan[0].sol, 0.5);
+ assert.equal(plan[1].sol, 0.5);
+ assert.equal(plan[2].sol, 0.3);
+ assert.equal(skipped.length, 0);
+});
+
test('dust: a sub-threshold deficit is skipped, not churned', () => {
const { plan, skipped } = planTopUps(10, [
{ name: 'hair', pubkey: 'H', currentSol: 0.499, refillToSol: 0.5 },
@@ -56,8 +87,7 @@ test('dust: a sub-threshold deficit is skipped, not churned', () => {
assert.deepEqual(skipped, [{ name: 'hair', reason: 'below_dust_threshold' }]);
});
-test('neediest first: the most-drained engine wins a tight run cap', () => {
- // spendable makes runCap = 0.5 → only one engine can be funded this sweep.
+test('neediest first: the most-drained engine is planned before a less-needy one', () => {
const { plan } = planTopUps(1.5, [
{ name: 'small', pubkey: 'S', currentSol: 0.4, refillToSol: 0.5 }, // deficit 0.1
{ name: 'big', pubkey: 'B', currentSol: 0.0, refillToSol: 0.5 }, // deficit 0.5