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): self-heal pool-agent wallets encrypted under a retired key

fudjiacfeb8593be79
diff --git a/api/_lib/agent-pumpfun.js b/api/_lib/agent-pumpfun.js
index 5721a589c..d78efdad9 100644
--- a/api/_lib/agent-pumpfun.js
+++ b/api/_lib/agent-pumpfun.js
@@ -81,11 +81,58 @@ export async function loadAgentForSigning(agentId, userId, audit = null) {
`;
}
- const keypair = await recoverSolanaAgentKeypair(meta.encrypted_solana_secret, {
- agentId,
- userId,
- reason: audit?.reason || 'pumpfun_action',
- meta: audit?.meta,
- });
+ let keypair;
+ try {
+ keypair = await recoverSolanaAgentKeypair(meta.encrypted_solana_secret, {
+ agentId,
+ userId,
+ reason: audit?.reason || 'pumpfun_action',
+ meta: audit?.meta,
+ });
+ } catch (err) {
+ // A custodial wallet encrypted under a RETIRED key (e.g. the WALLET_ENCRYPTION_KEY
+ // that changed during the Vercel→Cloud Run migration) can no longer be decrypted
+ // with the current key OR the JWT_SECRET fallback. decryptSecret authenticates
+ // every candidate (AES-GCM), so a failure here is a DEFINITIVE "unrecoverable",
+ // not a transient fault — and the stored address is already unreachable because we
+ // can't sign for it, so nothing new is stranded. Rather than fail every launch
+ // forever, self-heal: mint a fresh wallet under the CURRENT key (the same
+ // 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;
+ const sol = await generateSolanaAgentWallet();
+ meta = {
+ ...meta,
+ solana_address: sol.address,
+ encrypted_solana_secret: sol.encrypted_secret,
+ solana_wallet_source: 're_provisioned_stale_key',
+ stale_solana_address: meta.solana_address,
+ rekeyed_at: new Date().toISOString(),
+ };
+ await sql`
+ UPDATE agent_identities
+ SET meta = ${JSON.stringify(meta)}::jsonb
+ WHERE id = ${agentId}
+ `;
+ keypair = await recoverSolanaAgentKeypair(meta.encrypted_solana_secret, {
+ agentId,
+ userId,
+ reason: 're_provision_stale_key',
+ meta: audit?.meta,
+ });
+ }
return { agent: row, keypair, meta };
}
+
+// A stored custodial secret is unrecoverable when neither the dedicated wallet key
+// 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.
+export function isUnrecoverableSecret(err) {
+ const name = err?.name || '';
+ const msg = String(err?.message || '');
+ return (
+ name === 'OperationError' ||
+ /decrypt failed|no candidate key|bad secret key|invalid secret key|secret key size/i.test(msg)
+ );
+}
diff --git a/tests/agent-wallet-rekey-guard.test.js b/tests/agent-wallet-rekey-guard.test.js
new file mode 100644
index 000000000..17c350fcb
--- /dev/null
+++ b/tests/agent-wallet-rekey-guard.test.js
@@ -0,0 +1,31 @@
+import { describe, it, expect } from 'vitest';
+import { isUnrecoverableSecret } from '../api/_lib/agent-pumpfun.js';
+
+// Guards the self-heal in loadAgentForSigning: a custodial wallet encrypted under a
+// retired key (the WALLET_ENCRYPTION_KEY that changed during the Vercel→Cloud Run
+// migration) is re-provisioned with the current key instead of failing every launch
+// forever. This MUST fire only on a definitive, permanent decrypt failure — never on
+// a transient fault, or the launcher would silently re-key a wallet whose funds were
+// still recoverable.
+
+describe('isUnrecoverableSecret', () => {
+ it('fires on AES-GCM auth failure (retired/wrong key)', () => {
+ expect(isUnrecoverableSecret({ name: 'OperationError', message: 'The operation failed' })).toBe(true);
+ });
+
+ it('fires on a corrupt/invalid stored secret', () => {
+ expect(isUnrecoverableSecret(new Error('bad secret key size'))).toBe(true);
+ expect(isUnrecoverableSecret(new Error('[secret-box] v2 decrypt failed: no candidate key available'))).toBe(true);
+ });
+
+ it('does NOT fire on a transient/network error (would strand recoverable funds)', () => {
+ expect(isUnrecoverableSecret(new Error('connection reset'))).toBe(false);
+ expect(isUnrecoverableSecret(new Error('fetch failed'))).toBe(false);
+ });
+
+ it('does NOT fire on an unrelated programming error', () => {
+ expect(isUnrecoverableSecret({ name: 'TypeError', message: 'x is undefined' })).toBe(false);
+ expect(isUnrecoverableSecret(null)).toBe(false);
+ expect(isUnrecoverableSecret(undefined)).toBe(false);
+ });
+});