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(sniper): sit out below the operational wallet floor instead of looping on failed sims

nirholas3125dad47282
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ecca076af..91e831be2 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,7 @@ Public history for [three.ws](https://three.ws), newest first. New pages come fr
## 2026-07-30
+- **An underfunded trading bot now sits out instead of failing every trade** — A trading strategy whose wallet ran low could get stuck in a loop: it looked funded enough to place a shrunken buy, but not funded enough to run the safety simulation that has to pass before anything is broadcast. So it tried every candidate coin, failed at the same step every time, and filled its history with failures while never actually trading. One live strategy did this 82 times in a day without placing a single buy. Strategies now compare their balance against the amount a trade genuinely needs end to end, including the pre-trade safety checks, and quietly wait for a top-up instead. No trade behavior changes for a properly funded strategy. (`/trades`) `[fix]`
- **Payment health now names the real cause when settlements stall** — The platform watches how many of its automated payments actually settle. When that number fell, the alert always said the same thing: payments are being rejected. That was misleading in the most common case, where nothing is rejected at all. The platform simply stops offering Solana as a payment option while its sponsor wallet is topped up, so its own agents have nothing they can pay. The health report now tells these two situations apart by name, counts how often the Solana option was unavailable, and points at the one-command self-repair instead of sending whoever is on call to the wrong system. `[infra, improvement]`
## 2026-07-29
diff --git a/api/_lib/agent-trade-guards.js b/api/_lib/agent-trade-guards.js
index 2337dd08e..11e97d2d6 100644
--- a/api/_lib/agent-trade-guards.js
+++ b/api/_lib/agent-trade-guards.js
@@ -418,6 +418,46 @@ export function checkSolHeadroom(walletLamports, spendLamports, headroomLamports
return null;
}
+/**
+ * Resolve the entry size a wallet can actually trade, or decide it must sit out.
+ *
+ * Pure. Two floors apply and they are NOT the same number:
+ * - `ENTRY_HEADROOM_LAMPORTS` covers the buy's own network fee + the token
+ * ATA's rent + priority-fee/tip room. It is what the buy itself costs.
+ * - `MIN_OPERATIONAL_WALLET_SOL` additionally covers the checks that run from
+ * this wallet BEFORE any broadcast: the firewall's buy→sell round-trip probe
+ * and the pre-broadcast compute-unit simulation.
+ *
+ * A balance above the first floor and below the second looks tradeable and is
+ * operationally dead: every attempt aborts at a simulation the wallet cannot
+ * afford to run, so an arm gated only on the first floor re-attempts every
+ * candidate forever and books a failure each time. Honest behavior is to sit out
+ * until the auto-funder tops the wallet back up.
+ *
+ * Returns `{ sizeLamports }` to trade (possibly shrunk below `wantLamports`), or
+ * `{ skip: reason }` to sit this candidate out.
+ *
+ * @param {bigint|number|string} walletLamports current wallet balance
+ * @param {bigint|number|string} wantLamports the strategy's configured size
+ * @param {bigint|number|string} minTradeLamports smallest size worth placing
+ * @returns {{sizeLamports: bigint, skip?: undefined}|{skip: string, sizeLamports?: undefined}}
+ */
+export function resolveEntrySize(walletLamports, wantLamports, minTradeLamports) {
+ const wallet = BigInt(walletLamports);
+ const want = BigInt(wantLamports);
+ const minTrade = BigInt(minTradeLamports);
+ const headroom = checkSolHeadroom(wallet, want, ENTRY_HEADROOM_LAMPORTS);
+ if (!headroom) return { sizeLamports: want };
+ // Short for the configured size. Shrinking is allowed (learning > profit), but
+ // only from a wallet that can still run the pre-broadcast simulations.
+ if (wallet < BigInt(Math.round(MIN_OPERATIONAL_WALLET_SOL * 1e9))) {
+ return { skip: headroom.reason };
+ }
+ const dustCapable = wallet - ENTRY_HEADROOM_LAMPORTS;
+ if (dustCapable < minTrade) return { skip: headroom.reason };
+ return { sizeLamports: dustCapable };
+}
+
/** Price-impact circuit breaker. Blocked when impact > max. */
export function checkPriceImpact(priceImpactPct, maxPct) {
if (maxPct == null) return null;
diff --git a/data/changelog.json b/data/changelog.json
index 94f42c48d..0a335f5dc 100644
--- a/data/changelog.json
+++ b/data/changelog.json
@@ -1,6 +1,15 @@
{
"$comment": "Curated changelog entries for three.ws, the editorial layer of the public changelog. New PAGE launches are tracked automatically via the `added` field in data/pages.json; this file is for everything else holders care about: improvements, fixes, SDK releases, trading/payments capabilities, security work. scripts/build-page-index.mjs merges both sources into CHANGELOG.md, public/changelog.json, and public/changelog.xml. Newest first. Allowed tags: feature, improvement, fix, sdk, infra, docs, security. `link` is optional and must be a live three.ws path.",
"entries": [
+ {
+ "date": "2026-07-30",
+ "title": "An underfunded trading bot now sits out instead of failing every trade",
+ "summary": "A trading strategy whose wallet ran low could get stuck in a loop: it looked funded enough to place a shrunken buy, but not funded enough to run the safety simulation that has to pass before anything is broadcast. So it tried every candidate coin, failed at the same step every time, and filled its history with failures while never actually trading. One live strategy did this 82 times in a day without placing a single buy. Strategies now compare their balance against the amount a trade genuinely needs end to end, including the pre-trade safety checks, and quietly wait for a top-up instead. No trade behavior changes for a properly funded strategy.",
+ "tags": [
+ "fix"
+ ],
+ "link": "/trades"
+ },
{
"date": "2026-07-30",
"title": "Payment health now names the real cause when settlements stall",
diff --git a/public/changelog.json b/public/changelog.json
index 2f501f5c2..5ef9e8752 100644
--- a/public/changelog.json
+++ b/public/changelog.json
@@ -1,11 +1,21 @@
{
- "generated_at": "2026-07-30T01:06:33.118Z",
+ "generated_at": "2026-07-30T02:53:19.559Z",
"generated_by": "scripts/build-page-index.mjs from data/pages.json + data/changelog.json",
"site": {
"name": "three.ws",
"url": "https://three.ws"
},
"entries": [
+ {
+ "date": "2026-07-30",
+ "type": "update",
+ "title": "An underfunded trading bot now sits out instead of failing every trade",
+ "summary": "A trading strategy whose wallet ran low could get stuck in a loop: it looked funded enough to place a shrunken buy, but not funded enough to run the safety simulation that has to pass before anything is broadcast. So it tried every candidate coin, failed at the same step every time, and filled its history with failures while never actually trading. One live strategy did this 82 times in a day without placing a single buy. Strategies now compare their balance against the amount a trade genuinely needs end to end, including the pre-trade safety checks, and quietly wait for a top-up instead. No trade behavior changes for a properly funded strategy.",
+ "link": "/trades",
+ "tags": [
+ "fix"
+ ]
+ },
{
"date": "2026-07-30",
"type": "update",
diff --git a/public/changelog.xml b/public/changelog.xml
index 44dbe51a2..fc300f0ad 100644
--- a/public/changelog.xml
+++ b/public/changelog.xml
@@ -6,6 +6,14 @@
<atom:link href="https://three.ws/changelog.xml" rel="self" type="application/rss+xml"/>
<description>What's new on three.ws — features, improvements, and releases, newest first.</description>
<language>en</language>
+ <item>
+ <title>An underfunded trading bot now sits out instead of failing every trade</title>
+ <link>https://three.ws/trades</link>
+ <guid isPermaLink="false">2026-07-30:An underfunded trading bot now sits out instead of failing every trade</guid>
+ <pubDate>Thu, 30 Jul 2026 12:00:00 GMT</pubDate>
+ <category>fix</category>
+ <description>A trading strategy whose wallet ran low could get stuck in a loop: it looked funded enough to place a shrunken buy, but not funded enough to run the safety simulation that has to pass before anything is broadcast. So it tried every candidate coin, failed at the same step every time, and filled its history with failures while never actually trading. One live strategy did this 82 times in a day without placing a single buy. Strategies now compare their balance against the amount a trade genuinely needs end to end, including the pre-trade safety checks, and quietly wait for a top-up instead. No trade behavior changes for a properly funded strategy.</description>
+ </item>
<item>
<title>Payment health now names the real cause when settlements stall</title>
<link>https://three.ws/changelog</link>
diff --git a/public/features.json b/public/features.json
index 1137499e3..06d34acd5 100644
--- a/public/features.json
+++ b/public/features.json
@@ -1,5 +1,5 @@
{
- "generated_at": "2026-07-30T01:06:33.111Z",
+ "generated_at": "2026-07-30T02:53:19.552Z",
"generated_by": "scripts/build-page-index.mjs from data/pages.json",
"site": {
"name": "three.ws",
diff --git a/tests/agent-entry-size.test.js b/tests/agent-entry-size.test.js
new file mode 100644
index 000000000..c79cd6921
--- /dev/null
+++ b/tests/agent-entry-size.test.js
@@ -0,0 +1,77 @@
+// Unit tests for resolveEntrySize (api/_lib/agent-trade-guards.js): the guard
+// that decides whether an underfunded sniper arm shrinks its buy or sits out.
+//
+// The bug this pins down: a wallet holding more than the entry headroom (fee +
+// ATA rent) but less than MIN_OPERATIONAL_WALLET_SOL passed the old inline check
+// and then aborted at the firewall round-trip probe / pre-broadcast CU
+// simulation it could not afford to run. The arm re-attempted every candidate
+// and booked a SIM_FAILED each time. Observed live on the boost-ride arm: 82
+// consecutive SIM_FAILED in 24h from a 0.0048 SOL wallet, zero buys.
+//
+// Pure function, no mocks needed.
+
+import { describe, it, expect } from 'vitest';
+import {
+ resolveEntrySize,
+ ENTRY_HEADROOM_LAMPORTS,
+ MIN_OPERATIONAL_WALLET_SOL,
+} from '../api/_lib/agent-trade-guards.js';
+
+const MIN_TRADE = 10_000n; // SNIPER_MIN_TRADE_LAMPORTS default
+const OPERATIONAL = BigInt(Math.round(MIN_OPERATIONAL_WALLET_SOL * 1e9));
+const sol = (n) => BigInt(Math.round(n * 1e9));
+
+describe('resolveEntrySize', () => {
+ it('trades the configured size when the wallet fully covers it', () => {
+ const want = sol(0.05);
+ const got = resolveEntrySize(want + ENTRY_HEADROOM_LAMPORTS, want, MIN_TRADE);
+ expect(got.skip).toBeUndefined();
+ expect(got.sizeLamports).toBe(want);
+ });
+
+ it('shrinks to what is left when the wallet is short but still operational', () => {
+ // Comfortably above the operational floor, below want + headroom.
+ const wallet = OPERATIONAL * 4n;
+ const want = wallet; // more than it can afford once headroom is reserved
+ const got = resolveEntrySize(wallet, want, MIN_TRADE);
+ expect(got.skip).toBeUndefined();
+ expect(got.sizeLamports).toBe(wallet - ENTRY_HEADROOM_LAMPORTS);
+ expect(got.sizeLamports).toBeLessThan(want);
+ });
+
+ it('sits out below the operational floor even when headroom alone would pass', () => {
+ // The regression case. This balance clears headroom + minTrade, so the old
+ // headroom-only gate shrank and attempted; it cannot afford the simulations.
+ const wallet = ENTRY_HEADROOM_LAMPORTS + MIN_TRADE + 1n;
+ expect(wallet).toBeGreaterThan(ENTRY_HEADROOM_LAMPORTS + MIN_TRADE);
+ expect(wallet).toBeLessThan(OPERATIONAL);
+ const got = resolveEntrySize(wallet, sol(0.0254), MIN_TRADE);
+ expect(got.skip).toBe('insufficient_sol');
+ expect(got.sizeLamports).toBeUndefined();
+ });
+
+ it('sits out at the live boost-ride balance that produced the SIM_FAILED loop', () => {
+ const got = resolveEntrySize(sol(0.004773), sol(0.0254), MIN_TRADE);
+ expect(got.skip).toBe('insufficient_sol');
+ });
+
+ it('sits out when the wallet cannot cover even a minimum-sized trade', () => {
+ const got = resolveEntrySize(ENTRY_HEADROOM_LAMPORTS, sol(0.05), MIN_TRADE);
+ expect(got.skip).toBe('insufficient_sol');
+ });
+
+ it('accepts number and string balances, not just bigint', () => {
+ const want = sol(0.05);
+ const wallet = want + ENTRY_HEADROOM_LAMPORTS;
+ expect(resolveEntrySize(Number(wallet), Number(want), 10_000).sizeLamports).toBe(want);
+ expect(resolveEntrySize(String(wallet), String(want), '10000').sizeLamports).toBe(want);
+ });
+
+ it('never returns a size the wallet cannot pay for', () => {
+ for (const balance of [0, 1, 5_000, 6_039_280, 8_000_000, 12_000_000, 50_000_000]) {
+ const got = resolveEntrySize(balance, sol(0.05), MIN_TRADE);
+ if (got.skip) continue;
+ expect(got.sizeLamports + ENTRY_HEADROOM_LAMPORTS).toBeLessThanOrEqual(BigInt(balance));
+ }
+ });
+});
diff --git a/workers/agent-sniper/executor.js b/workers/agent-sniper/executor.js
index f461473fe..2e7358204 100644
--- a/workers/agent-sniper/executor.js
+++ b/workers/agent-sniper/executor.js
@@ -17,9 +17,8 @@ import { notifyBuy, notifySell } from '../../api/_lib/sniper/notify.js';
import { getPolicyRules } from '../../api/_lib/spend-policy-rules.js';
import {
getSpendLimits, enforceSpendLimit, SpendLimitError, recordSpend, lamportsToUsd,
- checkConcurrency, checkDailyBudgetLamports, checkSolHeadroom, checkPriceImpact,
- checkDailyLoss, recordCustodyEvent,
- ENTRY_HEADROOM_LAMPORTS,
+ checkConcurrency, checkDailyBudgetLamports, checkPriceImpact,
+ checkDailyLoss, recordCustodyEvent, resolveEntrySize,
} from '../../api/_lib/agent-trade-guards.js';
import { shouldGiveUpReconcile } from './exit-logic.js';
import { buildAmmSellInstructions, quoteAmmBuy, buildAmmBuyInstructions } from './amm-exit.js';
@@ -341,18 +340,14 @@ export async function executeBuy({ cfg, strat, mint, throttle }) {
log.warn('wallet precheck failed', { ...tag, err: err?.message });
return skip(tag, 'wallet_precheck_failed');
}
- const headroom = checkSolHeadroom(preBalance, perTrade, ENTRY_HEADROOM_LAMPORTS);
- if (headroom) {
- // Learning > profit (owner directive): a wallet too poor for the
- // strategy's configured size still trades, shrunk to whatever is left
- // after the fee/rent headroom — it only sits out once even a
- // cfg.minTradeLamports-sized buy wouldn't fit. Safe to shrink here: every
- // check already passed above (budget, spend policy) is a ceiling a
- // smaller trade only clears more easily, never less.
- const dustCapable = preBalance - ENTRY_HEADROOM_LAMPORTS;
- if (dustCapable < cfg.minTradeLamports) return skip(tag, headroom.reason);
- perTrade = dustCapable;
- }
+ // Learning > profit (owner directive): a wallet too poor for the strategy's
+ // configured size still trades, shrunk to what it can actually afford. It
+ // sits out only when even a shrunk buy could not clear the pre-broadcast
+ // simulations. Safe to shrink: every check already passed above (budget,
+ // spend policy) is a ceiling a smaller trade only clears more easily.
+ const sized = resolveEntrySize(preBalance, perTrade, cfg.minTradeLamports);
+ if (sized.skip) return skip(tag, sized.skip);
+ perTrade = sized.sizeLamports;
// 5. idempotency lock — claim the (agent,mint,network) slot BEFORE the tx.
// The wallet is known now, so it's written on the claim (no later UPDATE).