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(ops): stop the custodial-key audit reporting zero stranded SOL on RPC failure

nirholase5ad854787a1
diff --git a/docs/ops/wallet-key-migration.md b/docs/ops/wallet-key-migration.md
index 2ec9aa647..484c29552 100644
--- a/docs/ops/wallet-key-migration.md
+++ b/docs/ops/wallet-key-migration.md
@@ -104,6 +104,37 @@ still unopenable, holding **0.49 SOL, of which 0.35 SOL is customer money** in t
user-owned agents. Those users cannot withdraw. Re-keying a customer wallet would abandon
their balance, so that decision is the owner's, not an agent's.
+## Still stranded, and a blind spot in how we knew that (2026-08-09)
+
+A production triage sweep hit this incident again from a new angle: `treasury-topup`'s
+live agent-reclaim leg tried to sweep two platform pool wallets (Atlas #22, Echo #22,
+both created 2026-06-26, the same batch as the original 12) that showed real balances
+(0.068 and 0.054 SOL) and failed with `secret_undecryptable` on both. Same root cause,
+same unresolved batch, not a new incident.
+
+What was new: `scripts/audit-custodial-key-health.mjs`, run locally in this codespace at
+the same time, reported "0 SOL stranded" for the whole fleet, flatly contradicting the
+live reclaim failure. The audit was wrong for two independent reasons, both now fixed:
+
+1. It hit exactly one hardcoded `SOLANA_RPC_URL` lane (`rpc.magicblock.app`) with no
+ fallback. That lane returned `403 Forbidden` for this caller's IP, so every balance
+ read failed. The script now uses `solanaConnection()` from
+ `api/_lib/solana/connection.js`, the same rotating multi-lane connection production
+ reads balances through, so one blocked or rate-limited lane no longer blinds the
+ whole audit.
+2. Worse, an unread balance was silently summed as zero (`balances.get(addr) || 0`), so
+ a total RPC failure printed a confident "SOL stranded: 0" instead of "unknown." The
+ script now tracks reads with `balances.has()` and refuses to print a stranded total
+ while any undecryptable wallet's balance is unconfirmed; `scripts/gcp-triage.mjs`'s
+ `custodial-keys` probe carries the same guard, so the deep sweep no longer reports
+ `ok` off an unread balance either.
+
+With the fix, the audit reads correctly again: **8 undecryptable wallets, 0.49 SOL
+stranded (0.35 SOL customer, 0.14 SOL platform)**, matching the 2026-08-01 measurement
+almost exactly. Nothing changed in the underlying incident: the customer-fund decision
+documented above (re-keying abandons the balance; that call is the owner's) is still
+open, and no funds were moved.
+
## Takeaways
- **A key rotation must carry the old key forward, or accompany a sweep.** This is now
diff --git a/scripts/audit-custodial-key-health.mjs b/scripts/audit-custodial-key-health.mjs
index 7f7f97750..92c1c403b 100644
--- a/scripts/audit-custodial-key-health.mjs
+++ b/scripts/audit-custodial-key-health.mjs
@@ -55,7 +55,8 @@ for (const file of ['.env', '.env.local']) {
const { sql } = await import('../api/_lib/db.js');
const { decryptSecret } = await import('../api/_lib/secret-box.js');
-const { Connection, PublicKey } = await import('@solana/web3.js');
+const { PublicKey } = await import('@solana/web3.js');
+const { solanaConnection } = await import('../api/_lib/solana/connection.js');
const rows = await sql`
SELECT a.id AS agent_id,
@@ -91,10 +92,13 @@ for (const w of wallets) {
}
}
-const connection = new Connection(
- process.env.SOLANA_RPC_URL || 'https://api.mainnet-beta.solana.com',
- 'confirmed',
-);
+// Same rotating multi-lane connection production reads balances through
+// (api/_lib/solana/connection.js), not a single bare endpoint. A script that
+// hits exactly one RPC URL dies the moment that one lane rate-limits or
+// blocks the caller's IP (observed 2026-08-09: SOLANA_RPC_URL pinned to a
+// single free lane returned 403 for every wallet), and the failure was
+// invisible because unread balances silently summed as zero (see below).
+const connection = solanaConnection({ url: process.env.SOLANA_RPC_URL || null });
const balances = new Map();
const readErrors = [];
@@ -115,13 +119,26 @@ for (let i = 0; i < wallets.length; i += 100) {
}
}
-const sum = (list) => list.reduce((t, w) => t + (balances.get(w.address) || 0), 0);
+// balances.has() is load-bearing: a wallet whose chunk hit an RPC error has no
+// entry at all, and must never be treated as a confirmed zero balance. Coalescing
+// "unread" into "0" is exactly the bug that let this audit report "0 SOL
+// stranded" while two undecryptable wallets actually held 0.12 SOL (2026-08-09;
+// caught only because the treasury-topup cron's *live* reclaim attempt, which
+// reads balances through Cloud Run's own multi-lane connection, tried to sweep
+// them and hit secret_undecryptable on wallets this audit had certified as
+// zero). `sum()` therefore only totals wallets with a confirmed read, and the
+// caller must check `unreadCount` before trusting a "0 stranded" verdict.
+const sum = (list) => list.reduce((t, w) => (balances.has(w.address) ? t + balances.get(w.address) : t), 0);
+const unread = (list) => list.filter((w) => !balances.has(w.address));
const round = (n) => Number(n.toFixed(9));
const platform = wallets.filter((w) => isPlatform(w.owner));
const customer = wallets.filter((w) => !isPlatform(w.owner));
const stranded = wallets.filter((w) => !w.decryptable);
-const strandedFunded = stranded.filter((w) => (balances.get(w.address) || 0) > 0);
+// "Funded" requires a CONFIRMED positive balance; a stranded wallet whose
+// balance we never read is neither funded nor cleared, it is unknown.
+const strandedFunded = stranded.filter((w) => balances.has(w.address) && balances.get(w.address) > 0);
+const strandedUnread = unread(stranded);
const report = {
checked_at: new Date().toISOString(),
@@ -131,6 +148,9 @@ const report = {
decryptable: wallets.length - stranded.length,
undecryptable: stranded.length,
sol: {
+ // Every figure below is a sum over CONFIRMED reads only (sum() skips
+ // addresses with no balances.has() entry) - never trust these as
+ // complete while unread_stranded_count > 0 below.
total: round(sum(wallets)),
decryptable: round(sum(wallets.filter((w) => w.decryptable))),
stranded: round(sum(stranded)),
@@ -143,6 +163,7 @@ const report = {
stranded_platform: stranded.filter((w) => isPlatform(w.owner)).length,
stranded_customer: stranded.filter((w) => !isPlatform(w.owner)).length,
stranded_funded: strandedFunded.length,
+ stranded_unread: strandedUnread.length,
},
top_stranded: strandedFunded
.sort((a, b) => (balances.get(b.address) || 0) - (balances.get(a.address) || 0))
@@ -156,6 +177,14 @@ const report = {
created_at: w.created_at,
platform: isPlatform(w.owner),
})),
+ unread_stranded: strandedUnread.map((w) => ({
+ name: w.name,
+ address: w.address,
+ owner: w.owner,
+ reason: w.reason,
+ created_at: w.created_at,
+ platform: isPlatform(w.owner),
+ })),
};
if (AS_JSON) {
@@ -168,12 +197,18 @@ console.log(`custodial key health (${report.checked_at})`);
console.log(` rpc ${report.rpc}`);
console.log(` wallets ${report.wallets} (${report.counts.platform} platform, ${report.counts.customer} customer)`);
console.log(` decryptable ${report.decryptable} (${pct(report.decryptable, report.wallets)})`);
-console.log(` undecryptable ${report.undecryptable} (${pct(report.undecryptable, report.wallets)}), ${report.counts.stranded_funded} of them funded`);
+console.log(` undecryptable ${report.undecryptable} (${pct(report.undecryptable, report.wallets)}), ${report.counts.stranded_funded} confirmed funded, ${report.counts.stranded_unread} unread`);
console.log(` read errors ${report.read_errors}`);
console.log('');
-console.log(` SOL total ${report.sol.total}`);
-console.log(` SOL spendable ${report.sol.decryptable}`);
-console.log(` SOL stranded ${report.sol.stranded} (platform ${report.sol.stranded_platform}, customer ${report.sol.stranded_customer})`);
+if (report.counts.stranded_unread > 0) {
+ console.log(` SOL stranded UNKNOWN: ${report.counts.stranded_unread} undecryptable wallet(s) never got a balance read`);
+ console.log(' (RPC failed for their chunk). Do NOT read this as "0 stranded".');
+ console.log(' Re-run once the RPC lane recovers, or set SOLANA_RPC_URL to a working lane.');
+} else {
+ console.log(` SOL total ${report.sol.total}`);
+ console.log(` SOL spendable ${report.sol.decryptable}`);
+ console.log(` SOL stranded ${report.sol.stranded} (platform ${report.sol.stranded_platform}, customer ${report.sol.stranded_customer})`);
+}
if (report.top_stranded.length) {
console.log('');
console.log(' largest stranded wallets');
diff --git a/scripts/gcp-triage.mjs b/scripts/gcp-triage.mjs
index 10e3a69b1..fbbdd9bb0 100644
--- a/scripts/gcp-triage.mjs
+++ b/scripts/gcp-triage.mjs
@@ -739,13 +739,29 @@ async function probeCustodialKeys() {
let report;
try { report = JSON.parse(r.stdout); } catch { return { status: 'error', note: 'unparseable custodial audit output' }; }
const strandedFunded = report.counts?.stranded_funded || 0;
- const note = `${report.wallets} wallets, ${report.undecryptable} undecryptable, ${report.sol?.stranded ?? 0} SOL stranded`;
- if (!strandedFunded) return { status: 'ok', note };
- const sample = (report.top_stranded || []).slice(0, 6).map((w) => `${w.sol} SOL ${w.address} ${w.platform ? 'platform' : 'CUSTOMER'} (${w.reason})`).join('\n');
- return { status: 'findings', note, findings: [deepFinding('custodial-keys', 'owner', `${strandedFunded} funded custodial wallet(s) behind undecryptable keys`, {
- count: strandedFunded, sample,
- action: 'These balances are invisible to treasury self-heal and, for customer wallets, block withdrawals (support obligation; escalate those first). Usual cause is a WALLET_ENCRYPTION_KEY rotation; scripts/rekey-stale-launch-wallets.mjs documents the recovery path.',
- })] };
+ const strandedUnread = report.counts?.stranded_unread || 0;
+ const findings = [];
+ // A "0 SOL stranded" verdict is only trustworthy if every undecryptable
+ // wallet actually got a balance read. When the audit's RPC lane fails, its
+ // own sum() now skips unread addresses instead of coalescing them to zero
+ // (2026-08-09 fix), so surface that here too rather than reporting "ok".
+ if (strandedUnread) {
+ const sample = (report.unread_stranded || []).slice(0, 6).map((w) => `${w.address} ${w.platform ? 'platform' : 'CUSTOMER'} (${w.reason})`).join('\n');
+ findings.push(deepFinding('custodial-keys-unread', 'investigate', `${strandedUnread} undecryptable wallet(s) never got a balance read, so "stranded" is unknown for them`, {
+ count: strandedUnread, sample,
+ action: 'NOT a "0 stranded" verdict: the audit could not reach a working Solana RPC lane for these wallets. Check healthz rpc_lanes for how many lanes are cooling, then re-run once a lane recovers.',
+ }));
+ }
+ const note = `${report.wallets} wallets, ${report.undecryptable} undecryptable, ${strandedUnread ? 'unknown (unread)' : `${report.sol?.stranded ?? 0}`} SOL stranded`;
+ if (strandedFunded) {
+ const sample = (report.top_stranded || []).slice(0, 6).map((w) => `${w.sol} SOL ${w.address} ${w.platform ? 'platform' : 'CUSTOMER'} (${w.reason})`).join('\n');
+ findings.push(deepFinding('custodial-keys', 'owner', `${strandedFunded} funded custodial wallet(s) behind undecryptable keys`, {
+ count: strandedFunded, sample,
+ action: 'These balances are invisible to treasury self-heal and, for customer wallets, block withdrawals (support obligation; escalate those first). Usual cause is a WALLET_ENCRYPTION_KEY rotation; scripts/rekey-stale-launch-wallets.mjs documents the recovery path.',
+ }));
+ }
+ if (findings.length) return { status: 'findings', note, findings };
+ return { status: 'ok', note };
}
const DEEP_PROBES = {