Skip to content

Comments

fix(discovery): replace require() with ESM import for viem in cache writer#217

Open
uibeka wants to merge 1 commit intoConway-Research:mainfrom
uibeka:fix/discovery-esm-cache-write
Open

fix(discovery): replace require() with ESM import for viem in cache writer#217
uibeka wants to merge 1 commit intoConway-Research:mainfrom
uibeka:fix/discovery-esm-cache-write

Conversation

@uibeka
Copy link
Contributor

@uibeka uibeka commented Feb 24, 2026

Problem

Agent discovery cache writes silently fail on every call. The discovered_agents_cache SQLite table is never populated, forcing agents to perform full on-chain Transfer event scans (2081+ events, ~6 seconds) on every discover_agents call. Logs show 20+ require is not defined errors per discovery call (one per agent card enrichment).

Root Cause

setCachedCard in src/registry/discovery.ts uses require("viem") to import keccak256 and toBytes for computing agent card hashes. The project is ESM ("type": "module" in package.json, all imports use .js extensions), so require() is not available. The error is caught by the surrounding try-catch and silently swallowed, making the failure invisible except in verbose logs.

Fix

Replace require("viem") with a standard ESM import. The keccak256 and toBytes functions are imported at the top of the file, consistent with how other modules in the codebase import from viem.

Impact

  • Discovery cache writes succeed — discovered_agents_cache table populates correctly
  • Subsequent discovery calls can use cached agent cards instead of re-fetching from the network
  • Eliminates 20+ ReferenceError stack traces per discovery call from logs
  • Zero behavioral change for agents — discovery results are identical, just cached for performance
  • No new dependencies — viem is already a project dependency

Testing

  • pnpm build — zero errors (import resolves correctly)
  • pnpm test — all 1005 existing tests pass (27 test files)

Note: Related Issue (Not Fixed Here)

During investigation, we observed that agents using data: URIs for inline ERC-8004 agent cards are blocked by SSRF protection (isAllowedUri only allows https: and ipfs:). data: URIs don't make network requests and pose no SSRF risk, but they're a legitimate pattern for inline agent metadata. Happy to open a separate issue or PR if the team wants to support data: URIs for agent cards.

…riter

setCachedCard uses require("viem") to import keccak256 and toBytes for
computing agent card hashes before caching to SQLite. Since the project
is ESM ("type": "module"), require() throws ReferenceError at runtime,
silently failing every cache write.

Replace with a static ESM import. This enables the discovered_agents_cache
table to actually populate, giving agents persistent discovery results
across wake cycles without redundant on-chain scans.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@uibeka uibeka force-pushed the fix/discovery-esm-cache-write branch from 7bbcd6f to 1567a71 Compare February 24, 2026 17:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant