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(launcher): never abandon a funded wallet when self-healing a retired key

fudjia63dc8865adf5
diff --git a/api/_lib/agent-pumpfun.js b/api/_lib/agent-pumpfun.js
index d78efdad9..a6129c31f 100644
--- a/api/_lib/agent-pumpfun.js
+++ b/api/_lib/agent-pumpfun.js
@@ -100,6 +100,34 @@ export async function loadAgentForSigning(agentId, userId, audit = null) {
// self-provision the no-wallet branch above does), keep the dead address in meta
// for the audit/recovery trail, and continue.
if (!isUnrecoverableSecret(err)) throw err;
+ // Before abandoning the dead wallet, make sure it isn't holding funds that are
+ // still recoverable with the retired key. Silently re-keying a FUNDED wallet
+ // would strand real SOL. If it holds more than dust, refuse and surface it for
+ // manual recovery (decrypt + sweep with the old key, then it re-keys clean);
+ // only an empty stale wallet is safe to auto-replace.
+ let staleSol;
+ try {
+ staleSol = await staleWalletBalanceSol(meta.solana_address);
+ } catch {
+ // Can't confirm the wallet is empty — fail CLOSED rather than risk
+ // abandoning funds during an RPC hiccup. The launcher will retry next tick.
+ return {
+ error: {
+ status: 503,
+ code: 'stale_balance_unverified',
+ msg: 'could not verify the retired-key wallet is empty before re-keying — will retry',
+ },
+ };
+ }
+ if (staleSol > STALE_REKEY_DUST_SOL) {
+ return {
+ error: {
+ status: 409,
+ code: 'wallet_funds_stranded',
+ msg: `custodial wallet ${meta.solana_address} holds ${staleSol.toFixed(4)} SOL under a retired encryption key — recover it before re-keying`,
+ },
+ };
+ }
const sol = await generateSolanaAgentWallet();
meta = {
...meta,
@@ -128,6 +156,21 @@ export async function loadAgentForSigning(agentId, userId, audit = null) {
// nor the JWT_SECRET fallback can authenticate it (AES-GCM OperationError), or when
// the decrypted bytes aren't a valid 64-byte secret key. These are permanent — a
// retired key or a corrupt record — so it's safe to re-provision rather than retry.
+// A stale wallet below this SOL is treated as empty — safe to abandon and re-key.
+// Above it, re-keying would strand funds, so the self-heal refuses and flags it.
+const STALE_REKEY_DUST_SOL = 0.01;
+
+// On-chain SOL balance of an address, for the self-heal's fund-safety check. Uses
+// the public RPC (a free read). Throws on an RPC error so the caller can fail CLOSED
+// (refuse to re-key) rather than mistake an unreachable RPC for an empty wallet.
+async function staleWalletBalanceSol(address) {
+ if (!address) return 0;
+ const { PublicKey } = await import('@solana/web3.js');
+ const conn = solanaPublicConnection('mainnet');
+ const lamports = await conn.getBalance(new PublicKey(address));
+ return lamports / 1e9;
+}
+
export function isUnrecoverableSecret(err) {
const name = err?.name || '';
const msg = String(err?.message || '');
diff --git a/scripts/rekey-stale-launch-wallets.mjs b/scripts/rekey-stale-launch-wallets.mjs
index dda4b21e1..5cf752851 100644
--- a/scripts/rekey-stale-launch-wallets.mjs
+++ b/scripts/rekey-stale-launch-wallets.mjs
@@ -20,10 +20,22 @@
// Dry-run by default: reports which agents WOULD be re-keyed. Pass --apply to write.
import { neon } from '@neondatabase/serverless';
+import { Connection, PublicKey } from '@solana/web3.js';
import { generateSolanaAgentWallet, recoverSolanaAgentKeypair } from '../api/_lib/agent-wallet.js';
const APPLY = process.argv.includes('--apply');
+// Re-keying abandons the old address, so a FUNDED stale wallet is skipped by default
+// (its SOL is recoverable only with the retired key). Pass --force-drop-funds to
+// re-key anyway, knowingly abandoning whatever SOL sits behind the retired key.
+const FORCE_DROP = process.argv.includes('--force-drop-funds');
+const DUST_SOL = 0.01;
const sql = neon(process.env.DATABASE_URL);
+const rpc = new Connection(process.env.SOLANA_RPC_URL || 'https://api.mainnet-beta.solana.com', 'confirmed');
+
+async function solBalance(addr) {
+ if (!addr) return 0;
+ try { return (await rpc.getBalance(new PublicKey(addr))) / 1e9; } catch { return NaN; }
+}
async function decryptsOk(encryptedSecret) {
if (!encryptedSecret) return false;
@@ -47,14 +59,29 @@ console.log(`\nlaunch-pool agents in the global queue: ${rows.length} (${APPLY
let rekeyed = 0;
let healthy = 0;
+let fundedSkipped = 0;
+let strandedSol = 0;
for (const r of rows) {
if (await decryptsOk(r.sec)) {
healthy++;
console.log(` ✓ ${r.name.padEnd(14)} ${r.addr} — decrypts, left as-is`);
continue;
}
+ // Undecryptable. Protect funds: don't abandon a wallet that still holds SOL
+ // unless explicitly forced — that SOL is recoverable only with the retired key.
+ const bal = await solBalance(r.addr);
+ if (Number.isNaN(bal)) {
+ console.log(` ? ${r.name.padEnd(14)} ${r.addr} — balance unknown (RPC), SKIP`);
+ continue;
+ }
+ if (bal > DUST_SOL && !FORCE_DROP) {
+ fundedSkipped++;
+ strandedSol += bal;
+ console.log(` 💰 ${r.name.padEnd(14)} ${r.addr} — holds ${bal.toFixed(4)} SOL, SKIP (recover with old key first, or --force-drop-funds)`);
+ continue;
+ }
if (!APPLY) {
- console.log(` ⟳ ${r.name.padEnd(14)} ${r.addr} — WOULD re-key (undecryptable)`);
+ console.log(` ⟳ ${r.name.padEnd(14)} ${r.addr} — WOULD re-key (empty/undecryptable)`);
rekeyed++;
continue;
}
@@ -75,5 +102,10 @@ for (const r of rows) {
}
console.log(`\n${'─'.repeat(60)}`);
-console.log(`healthy: ${healthy} ${APPLY ? 're-keyed' : 'to re-key'}: ${rekeyed}`);
-if (!APPLY && rekeyed) console.log('\nRe-run with --apply to re-key the undecryptable wallets.');
+console.log(`healthy: ${healthy} ${APPLY ? 're-keyed' : 'to re-key'}: ${rekeyed} funded-skipped: ${fundedSkipped} (${strandedSol.toFixed(4)} SOL)`);
+if (fundedSkipped) {
+ console.log(`\n⚠️ ${fundedSkipped} wallet(s) hold ${strandedSol.toFixed(4)} SOL under the retired key.`);
+ console.log(' Recover them first (decrypt+sweep with the OLD WALLET_ENCRYPTION_KEY), then re-run.');
+ console.log(' Or --force-drop-funds to re-key anyway, knowingly abandoning that SOL.');
+}
+if (!APPLY && rekeyed) console.log('\nRe-run with --apply to re-key the empty undecryptable wallets.');