fix(discovery): raise maxScanCount default and sort before slice#253
Merged
unifiedh merged 1 commit intoConway-Research:mainfrom Mar 3, 2026
Merged
Conversation
Two related fixes to the agent discovery pipeline: 1. Raise DEFAULT_DISCOVERY_CONFIG.maxScanCount from 20 to 100 — the previous default silently clamped the user-specified limit parameter, making discover_agents(limit=50) return only 20 results. The scan loop already has proper safeguards (chunk timeouts, consecutive failure limits, overall discovery timeout) making a low cap redundant. 2. Sort agents by tokenId descending before .slice(0, limit) instead of after — the paginated backward scan collects chunks newest-first but logs within each chunk are ascending. A flat .reverse() on concatenated chunks produces garbled order, causing .slice() to select agents from the wrong end. Sorting by tokenId (monotonically increasing on mint) before slicing ensures the newest agents are correctly selected and returned.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
discover_agents(limit=50)silently returns only 20 agents despite 22,000+ agents registered on-chain. Additionally, when more agents are scanned than the limit, the returned subset may not contain the most recently registered agents.Root Cause
1.
maxScanCountsilently clamps the user's limit (types.ts)DEFAULT_DISCOVERY_CONFIG.maxScanCountis set to 20. IndiscoverAgents(), the limit is clamped viaMath.min(limit, cfg.maxScanCount)before being passed to the event scanner. This makes the user-specified limit parameter ineffective above 20 —discover_agents(limit=50)passes 20 to the scanner, which breaks early at 20 logs and slices to 20 results.2.
.reverse().slice()selects wrong agents after paginated collection (erc8004.ts)PR #228 introduced paginated backward scanning — chunks are collected newest-first, but
eth_getLogsreturns ascending order within each chunk..reverse()on the concatenated array doesn't produce true descending order across chunk boundaries..slice(0, limit)then selects from the wrong end. The existing post-slice.sort()corrects final ordering but can't recover agents discarded by the slice.Fix
1. Raise
maxScanCountfrom 20 to 100The scan loop already has robust safeguards: per-chunk timeouts (
PER_CHUNK_TIMEOUT_MS), consecutive failure limits (MAX_CONSECUTIVE_FAILURES), and an overall 60-second discovery timeout. These make a low scan count cap redundant. Raising to 100 allows meaningful discovery calls while retaining the safety net.2. Sort by tokenId descending before
.slice(0, limit)Replace the
.reverse().slice().sort()chain with.sort().slice(). Since ERC-8004 tokenIds are monotonically increasing on mint, sorting by tokenId descending before slicing ensures the newest agents are selected and correctly ordered regardless of chunk collection order.Impact
discover_agents(limit=50)returns up to 50 agents (previously silently capped at 20)limit ≤ 20see identical behaviorTesting
pnpm build— zero errorspnpm test— all existing tests pass