perf(tool): add caching and retry logic to web tools #7036
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.
Summary
Add LRU cache with disk persistence and retry with exponential backoff to
websearch,codesearch, andwebfetchtools to improve performance and reliability.Problem
The web tools currently:
Solution
1. LRU Cache with Disk Persistence
New utility at
packages/opencode/src/util/cache.ts:~/.cache/opencode/{namespace}/for warm restartslivecrawl: "preferred"2. Retry with Exponential Backoff
Extended
packages/util/src/retry.tswith new exports:isRetryableError()- Combines all retryable error checksisRateLimitError()- Detects 429 errorsisServerError()- Detects 5xx errorsConfiguration: 3 attempts, 1s → 2s → 4s backoff, max 10s delay
Retries: Network errors, rate limits (429), server errors (5xx)
Does NOT retry: Client errors (4xx) - those indicate request issues
Performance Impact
Observability
Cache operations are logged at INFO level:
Stats available via
cache.stats():Changes
packages/opencode/src/util/cache.tspackages/util/src/retry.tsisRetryableError,isRateLimitError,isServerErrorpackages/opencode/src/tool/websearch.tspackages/opencode/src/tool/codesearch.tspackages/opencode/src/tool/webfetch.tspackages/opencode/test/tool/cache.test.tspackages/opencode/test/util/retry.test.tsTesting
Breaking Changes
None. Existing behavior preserved, just faster and more reliable.