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(sentiment-pulse): serve last-known-good pump.fun callouts instead of 502 on Cloudflare 403

nirholasbc5a01b954b6
diff --git a/api/_lib/pump-callouts.js b/api/_lib/pump-callouts.js
index 4861733df..bd4adc0d5 100644
--- a/api/_lib/pump-callouts.js
+++ b/api/_lib/pump-callouts.js
@@ -7,8 +7,15 @@
// the Sentiment Scout (quoted as evidence), so both read the same feed through
// the shared pump.fun fetch helper (identified user-agent, bounded timeout,
// one retry on a rate limit or 5xx).
+//
+// A last-known-good tier (cacheWrapLastGood, same pattern as
+// pump-feed-fetch.js's readThroughFeed and pump-bonding.js's getBondingStatus)
+// backs the fetch: Cloudflare now answers this route's calls with a bare 403
+// for some mints, and without a cache tier that 403 propagated straight to a
+// 502 even for a mint read successfully moments earlier.
import { PUMP_FRONTEND_BASE, pumpFetchJson } from './pump-feed-fetch.js';
+import { cacheWrapLastGood } from './cache.js';
const FETCH_TIMEOUT_MS = 8000;
@@ -16,6 +23,12 @@ const FETCH_TIMEOUT_MS = 8000;
// than 100"), so a larger caller limit is clamped rather than failing the read.
export const PUMP_CALLOUT_PAGE_MAX = 100;
+// Live cache: collapses repeat polls of the same mint+limit into one upstream
+// fetch. Last-known-good: survives a Cloudflare 403/rate-limit window so a
+// mint with a prior successful fetch degrades to stale data, never a 502.
+const LIVE_TTL_S = Number(process.env.PUMP_CALLOUTS_TTL_S || 25);
+const LKG_TTL_S = Number(process.env.PUMP_BONDING_LKG_TTL_S || 1800);
+
/**
* Map raw pump.fun callout rows into the scorer's post shape, newest first.
* Rows with no thesis carry no sentiment and are dropped rather than counted
@@ -47,15 +60,31 @@ export function calloutsToPosts(callouts, limit) {
/**
* Fetch a coin's most recent pump.fun callouts. Never throws: an upstream
- * failure is reported as `{ error, url }` for the handler to surface.
+ * failure is reported as `{ error, url }` for the handler to surface, unless
+ * a last-known-good copy for this mint+limit survives, in which case that
+ * stale copy is served instead (`{ posts, url, stale: true }`).
*/
export async function fetchPumpFunCallouts(mint, limit, { timeoutMs = FETCH_TIMEOUT_MS } = {}) {
const pageLimit = Math.max(1, Math.min(PUMP_CALLOUT_PAGE_MAX, Math.floor(limit) || 1));
const url = `${PUMP_FRONTEND_BASE}/callout/coin/${encodeURIComponent(mint)}?limit=${pageLimit}`;
- const { ok, status, body } = await pumpFetchJson(url, { timeoutMs });
- if (!ok) {
- return { error: status ? `pump.fun returned ${status}` : 'pump.fun unreachable', url };
+ const key = `pump:callouts:${mint}:${pageLimit}`;
+
+ try {
+ const { value, stale } = await cacheWrapLastGood(
+ key,
+ LIVE_TTL_S,
+ async () => {
+ const { ok, status, body } = await pumpFetchJson(url, { timeoutMs });
+ if (!ok) {
+ throw new Error(status ? `pump.fun returned ${status}` : 'pump.fun unreachable');
+ }
+ const rows = Array.isArray(body?.callouts) ? body.callouts : Array.isArray(body) ? body : [];
+ return calloutsToPosts(rows, limit);
+ },
+ { staleTtlSeconds: LKG_TTL_S, withMeta: true },
+ );
+ return stale ? { posts: value, url, stale: true } : { posts: value, url };
+ } catch (err) {
+ return { error: err?.message || 'pump.fun unreachable', url };
}
- const rows = Array.isArray(body?.callouts) ? body.callouts : Array.isArray(body) ? body : [];
- return { posts: calloutsToPosts(rows, limit), url };
}
diff --git a/api/social/sentiment-pulse.js b/api/social/sentiment-pulse.js
index e5fc09e2e..567cb7398 100644
--- a/api/social/sentiment-pulse.js
+++ b/api/social/sentiment-pulse.js
@@ -16,8 +16,12 @@
// (identified user-agent, bounded timeout, one retry on a rate limit or 5xx).
//
// This is the unauthenticated, no-key endpoint behind the paid
-// `sentiment_pulse` MCP tool. It does no caching of its own (callers should
-// hit /api/social/sentiment if they already have the texts).
+// `sentiment_pulse` MCP tool. The pump.fun read itself is cached with a
+// last-known-good tier (api/_lib/pump-callouts.js, via cacheWrapLastGood):
+// a prior successful fetch for the same mint+limit survives a Cloudflare
+// block and is served stale (`sources.pumpfunStatus: 'stale'`) rather than
+// erroring. Callers who already have the texts should still hit
+// /api/social/sentiment instead.
//
// Body:
// {
@@ -38,7 +42,10 @@
// }
//
// A pump.fun outage with no caller-supplied texts answers 502
-// `upstream_unavailable`, never a fabricated neutral score. A coin that is
+// `upstream_unavailable`, never a fabricated neutral score, UNLESS a
+// last-known-good copy of that mint+limit survives, in which case the stale
+// copy is served (sources.pumpfunStatus: 'stale') and only a true double
+// miss (upstream down AND no cached copy yet) still 502s. A coin that is
// simply quiet (upstream fine, zero callouts) still answers 200 with
// `count: 0`, because an empty feed is data, not an outage.
@@ -108,7 +115,7 @@ export default wrap(async (req, res) => {
breakdown,
sources: {
pumpfun: pumpfun.error ? null : pumpfun.url,
- pumpfunStatus: pumpfun.error ? 'unavailable' : 'ok',
+ pumpfunStatus: pumpfun.error ? 'unavailable' : pumpfun.stale ? 'stale' : 'ok',
pumpfunCount: pumpfunPosts.length,
extraCount: extraPosts.length,
},
diff --git a/tests/social-sentiment-endpoints.test.js b/tests/social-sentiment-endpoints.test.js
index 5735c4faa..c50c4223f 100644
--- a/tests/social-sentiment-endpoints.test.js
+++ b/tests/social-sentiment-endpoints.test.js
@@ -27,6 +27,38 @@ vi.mock('../api/_lib/rate-limit.js', () => ({
clientIp: () => '127.0.0.1',
}));
+// api/_lib/pump-callouts.js now reads/writes through api/_lib/cache.js's
+// cacheWrapLastGood (last-known-good tier). Fake it with a plain Map so this
+// suite's cache state is deterministic and isolated from the real module:
+// same pattern as tests/pump-bonding.test.js.
+const cache = new Map();
+vi.mock('../api/_lib/cache.js', () => ({
+ cacheGet: async (k) => (cache.has(k) ? cache.get(k) : null),
+ cacheSet: async (k, v) => { cache.set(k, v); },
+ cacheWrapLastGood: async (key, ttlSeconds, fn, opts = {}) => {
+ const { staleTtlSeconds = 24 * 3600, withMeta = false } = opts;
+ const lkgKey = `${key}:lkg`;
+ const hit = cache.has(key) ? cache.get(key) : null;
+ if (hit !== null && hit !== undefined) return withMeta ? { value: hit, stale: false } : hit;
+ try {
+ const value = await fn();
+ if (value !== null && value !== undefined) {
+ cache.set(key, value);
+ cache.set(lkgKey, value);
+ }
+ return withMeta ? { value, stale: false } : value;
+ } catch (err) {
+ const lkg = cache.has(lkgKey) ? cache.get(lkgKey) : null;
+ if (lkg === null || lkg === undefined) throw err;
+ return withMeta ? { value: lkg, stale: true } : lkg;
+ }
+ },
+}));
+
+beforeEach(() => {
+ cache.clear();
+});
+
const { default: sentimentHandler } = await import('../api/social/sentiment.js');
const { default: pulseHandler, calloutsToPosts } = await import('../api/social/sentiment-pulse.js');
@@ -234,4 +266,41 @@ describe('POST /api/social/sentiment-pulse', () => {
expect(res.statusCode).toBe(400);
expect(res.json().error).toBe('validation_error');
});
+
+ it('serves last-known-good callouts when pump.fun fails and a cached copy exists', async () => {
+ const LKG_MINT = 'ATVbubW5hCMW4EcVvM1jyQTfihw7yKqTJTLxkwRqpump';
+
+ vi.spyOn(globalThis, 'fetch').mockResolvedValue(
+ upstream(200, {
+ callouts: [
+ callout({ calloutId: 'a', thesis: 'moon soon, bullish' }),
+ callout({ calloutId: 'b', createdAt: 1_777_000_000_000, thesis: 'rug pull scam' }),
+ ],
+ }),
+ );
+ const first = await call(pulseHandler, '/api/social/sentiment-pulse', {
+ token: LKG_MINT,
+ limit: 50,
+ });
+ expect(first.statusCode).toBe(200);
+ expect(first.json().sources.pumpfunStatus).toBe('ok');
+
+ // Simulate the live tier's short TTL having lapsed (the mock cache
+ // ignores TTLs, same as tests/pump-bonding.test.js's mock, so a
+ // real-time wait would never expire it): drop only the live-tier
+ // entry and keep the LKG mirror the first call also wrote, exactly
+ // the state a real request would see minutes later.
+ cache.delete(`pump:callouts:${LKG_MINT}:50`);
+
+ vi.spyOn(globalThis, 'fetch').mockResolvedValue(upstream(403, null));
+ const second = await call(pulseHandler, '/api/social/sentiment-pulse', {
+ token: LKG_MINT,
+ limit: 50,
+ });
+ expect(second.statusCode).toBe(200);
+ const out = second.json();
+ expect(out.overall.count).toBe(2);
+ expect(out.sources.pumpfunStatus).toBe('stale');
+ expect(out.sources.pumpfunCount).toBe(2);
+ });
});