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(cron): bound erc8004CrawlChain's shrink-retry loop by wall clock, not just the floor

nirholas3bec25a415b5
diff --git a/api/cron/[name].js b/api/cron/[name].js
index a5d986971..34035e713 100644
--- a/api/cron/[name].js
+++ b/api/cron/[name].js
@@ -464,6 +464,15 @@ const ERC8004_BLOCKTIME_CONCURRENCY = 8;
// moment a provider says no, so no chain needs its limit hardcoded here.
const ERC8004_MAX_BLOCK_CHUNK = 8_000;
const ERC8004_MIN_BLOCK_CHUNK = 100;
+// Ceiling on how long erc8004CrawlChain's shrink-retry loop may run for ONE
+// chain before it must checkpoint. Comfortably under CRAWL_BUDGET_MS (240s) so
+// a single stuck chain can never consume the whole tick, and short enough that
+// an external request timeout around this cron cannot kill the process before
+// a write happens. Measured 2026-10-25: Base's cursor sat frozen for 2+ days
+// because this loop had no time bound at all, only the backoffChunkSize fixed
+// point at the floor, which a slow or rejecting lane could take minutes to
+// reach, with nothing written until it did.
+const ERC8004_CHAIN_BUDGET_MS = 60_000;
// Range rejections name the range rather than a stable error code, and every
// provider words it differently.
//
@@ -552,7 +561,7 @@ function clampChunk(n, floor) {
return Math.max(floor, Math.min(ERC8004_MAX_BLOCK_CHUNK, Math.trunc(n)));
}
-async function erc8004CrawlChain(chain) {
+export async function erc8004CrawlChain(chain, { now = Date.now } = {}) {
const [cursor] = await sql`
SELECT last_block, chunk_size FROM erc8004_crawl_cursor WHERE chain_id = ${chain.id}
`;
@@ -589,7 +598,35 @@ async function erc8004CrawlChain(chain) {
// scanned=0 across four consecutive ticks while their backlogs grew.
let narrowedFrom = null;
let prunedSkip = null;
+ const loopStart = now();
for (;;) {
+ if (now() - loopStart > ERC8004_CHAIN_BUDGET_MS) {
+ // The shrink loop has run long enough that something downstream (a slow
+ // or rate-limited lane, a dense block range needing many halvings) is
+ // costing more wall-clock time than this chain may safely hold the tick
+ // hostage for. Persist whatever progress exists so the NEXT tick resumes
+ // from here instead of silently retrying the exact same blocks forever.
+ await sql`
+ INSERT INTO erc8004_crawl_cursor (chain_id, last_block, updated_at, head_block, blocks_behind, chunk_size, last_error)
+ VALUES (${chain.id}, ${Math.max(0, fromBlock - 1)}, now(), ${latestBlock},
+ ${Math.max(0, latestBlock - fromBlock + 1)}, ${chunkSize}, ${'shrink loop exceeded ' + ERC8004_CHAIN_BUDGET_MS + 'ms budget at chunk ' + chunkSize})
+ ON CONFLICT (chain_id) DO UPDATE SET
+ updated_at = now(),
+ head_block = excluded.head_block,
+ blocks_behind = excluded.blocks_behind,
+ chunk_size = excluded.chunk_size,
+ last_error = excluded.last_error
+ `;
+ return {
+ inserted: 0,
+ scanned: 0,
+ lastBlock: fromBlock - 1,
+ fromBlock,
+ blocksBehind: Math.max(0, latestBlock - fromBlock + 1),
+ chunkSize,
+ loopBudgetExceeded: true,
+ };
+ }
toBlock = Math.min(fromBlock + chunkSize - 1, latestBlock);
const range = {
fromBlock: '0x' + fromBlock.toString(16),
diff --git a/tests/agent-index.test.js b/tests/agent-index.test.js
index c1c8ff38d..639c6e7e0 100644
--- a/tests/agent-index.test.js
+++ b/tests/agent-index.test.js
@@ -5,7 +5,11 @@
// mainnet transactions and logs, with the transaction signatures kept so any
// claim below can be re-checked against the chain itself.
-import { describe, it, expect } from 'vitest';
+import { describe, it, expect, vi } from 'vitest';
+
+const sqlMock = vi.fn();
+vi.mock('../api/_lib/db.js', () => ({ sql: (...a) => sqlMock(...a) }));
+
import {
agentTokenMintOf,
classifyAgentTx,
@@ -43,6 +47,7 @@ import {
isPrunedHistoryRejection,
catchUpCandidates,
rank,
+ erc8004CrawlChain,
} from '../api/cron/[name].js';
import { splitCapabilities } from '../api/explore-item.js';
import { decodeReputationLog } from '../api/_lib/erc8004-reputation-events.js';
@@ -515,6 +520,88 @@ describe('isRangeRejection', () => {
});
});
+describe('erc8004CrawlChain: shrink loop has a time bound', () => {
+ // A hand-cranked clock, not a timer, matching the pattern in drainWithBudget's
+ // tests below: the budget case has to be exact and must not race on a loaded
+ // machine. Every eth_getLogs call is range-rejected (Base's verbatim -32602
+ // wording, measured on erc8004_crawl_cursor.last_error on 2026-10-22), so the
+ // loop keeps halving chunkSize forever unless something stops it on time.
+ const clockFrom = (ticks) => {
+ let i = 0;
+ return () => ticks[Math.min(i++, ticks.length - 1)];
+ };
+
+ const BASE_CHAIN = {
+ id: 8453,
+ name: 'Base',
+ registry: '0x1111111111111111111111111111111111111111',
+ testnet: false,
+ rpcUrls: ['http://fake-base-rpc.test'],
+ blockChunk: 8000,
+ };
+
+ // Stored cursor is 1,000 blocks behind head, chunk_size at the configured
+ // max: the same shape as Base's live frozen row from task 1.
+ const STORED_LAST_BLOCK = 52932449;
+ const LATEST_BLOCK = 52933449;
+
+ function mockSqlAndFetch() {
+ const sqlCalls = [];
+ sqlMock.mockReset();
+ sqlMock.mockImplementation((strings, ...values) => {
+ const text = strings.join('?');
+ sqlCalls.push({ text, values });
+ if (text.includes('SELECT last_block')) {
+ return Promise.resolve([{ last_block: STORED_LAST_BLOCK, chunk_size: 8000 }]);
+ }
+ return Promise.resolve([]);
+ });
+
+ const fetchCalls = [];
+ const origFetch = global.fetch;
+ global.fetch = vi.fn(async (_url, opts) => {
+ const body = JSON.parse(opts.body);
+ fetchCalls.push(body.method);
+ if (body.method === 'eth_blockNumber') {
+ return new Response(JSON.stringify({ jsonrpc: '2.0', id: 1, result: '0x' + LATEST_BLOCK.toString(16) }));
+ }
+ if (body.method === 'eth_getLogs') {
+ // Verbatim from erc8004_crawl_cursor.last_error on chain 8453, 2026-10-22.
+ return new Response(
+ JSON.stringify({
+ jsonrpc: '2.0',
+ id: 1,
+ error: { code: -32602, message: 'query exceeds max results 20000, retry with the range 52932450-52933133' },
+ }),
+ );
+ }
+ throw new Error('unexpected RPC method in test: ' + body.method);
+ });
+
+ return { sqlCalls, fetchCalls, restoreFetch: () => { global.fetch = origFetch; } };
+ }
+
+ it('checkpoints within the budget instead of running unbounded to the floor', async () => {
+ // Clock: 0 at loopStart, 0 on the first two per-iteration checks (so two
+ // shrink attempts run: 8000 -> 4000 -> 2000), then past the 60s budget on
+ // the third check, before a ninth attempt or any further halving happens.
+ const { sqlCalls, fetchCalls, restoreFetch } = mockSqlAndFetch();
+ try {
+ const result = await erc8004CrawlChain(BASE_CHAIN, { now: clockFrom([0, 0, 0, 70_000]) });
+ expect(fetchCalls.filter((m) => m === 'eth_getLogs').length).toBe(2);
+ expect(result.loopBudgetExceeded).toBe(true);
+ expect(result.chunkSize).toBe(2000);
+ expect(result.lastBlock).toBe(STORED_LAST_BLOCK);
+ expect(result.blocksBehind).toBe(LATEST_BLOCK - (STORED_LAST_BLOCK + 1) + 1);
+ expect(sqlCalls).toHaveLength(2); // cursor SELECT, then the budget-exceeded write
+ expect(sqlCalls.at(-1).values).toContain(2000);
+ expect(sqlCalls.at(-1).values.some((v) => String(v).includes('shrink loop exceeded'))).toBe(true);
+ } finally {
+ restoreFetch();
+ }
+ });
+});
+
// ─── Sweep capacity ──────────────────────────────────────────────────────────
describe('sweepCycleMin', () => {