Repository navigation
fix(core): fail over random web search on quota and provider errors - #66881
rekram1-node wants to merge 4 commits into
Conversation
There was a problem hiding this comment.
I recommend merging this with one change: a single MCP isError should take a provider out for a while, not until restart.
Reproduced on the base, fixed with the PR. I ran the PR's websearch.test.ts against the base. The three new random-failover cases (MCP ToolError, 402, 432) fail there and pass with the PR. bun test on the three websearch test files gives 64 passing, and tsc --noEmit in packages/core is clean. I couldn't run it end to end because that needs real provider quota to run out.
Public API: unchanged. The only user-facing change is the failover behaviour, and the docs now describe it.
Concern: any MCP isError now skips the provider until restart. One problem query can knock out every keyless provider for the whole process (see the inline comment).
| // Quota, plan-limit and provider-reported failures (402, Tavily's 432, MCP `isError`) will not clear on their own | ||
| // soon, so the provider is skipped for the rest of the process. Auth failures (401/403) stay visible instead. | ||
| function failoverUntil(cause: unknown, now: number) { | ||
| if (Schema.is(WebSearchMcp.ToolError)(cause)) return Infinity |
There was a problem hiding this comment.
WebSearchMcp.call raises ToolError for any MCP isError, not only exhausted quota. Rejected arguments, a hiccup on the provider's side or an error specific to one query all count. Under random, one such query can move through every keyless provider and mark each one skipped until restart. After that, every later search returns the cached error without contacting any provider. On the base, only that one search failed.
A long but finite cooldown (for example an hour) would stop the repeated failures without that risk. It would also let a provider recover after the user connects a key or the quota resets. The same applies to 402/432.
There was a problem hiding this comment.
Changed to a one-hour cooldown for MCP isError, 402 and 432, so a query-specific error can't take providers out until restart and providers recover after a key is connected or quota resets. Test and docs updated.
There was a problem hiding this comment.
Your change handles it. On pr/head (95d7da7), failoverUntil in packages/core/src/websearch.ts now returns now + 3_600_000 for MCP ToolError, 402 and 432. 429 still uses Retry-After, or 60 seconds if there isn't a valid one. Everything else, including 401/403, returns undefined and fails without trying another provider. That means one query-specific isError can take keyless providers out for at most an hour, not until restart.
- Test: the extended test covers the whole cycle. At 59 minutes the failed provider is still skipped, and the error is the second provider's. At 60 minutes the first provider is used again.
- Docs:
websearch.mdxline 75 now says "skipped for an hour" and no longer mentions restarting. - Verified: I ran
bun test test/websearch.test.ts test/tool-websearch.test.ts test/plugin/websearch.test.tsinpackages/coreon a temporary worktree ofpr/head: 64 pass. I removed the worktree afterwards. I didn't re-runtsc.
The tradeoff the PR description already accepts still applies: if every provider fails, searches get the cached last error until the first cooldown expires. That's fine with a finite window. I didn't change anything in repo/; the bun.lock modification there isn't mine and I left it as is.
There was a problem hiding this comment.
The new commits fix my earlier point: providers that hit quota or plan limits (402, 432) or report an MCP isError are now skipped for an hour instead of until restart, and the docs and test match. The three websearch test files pass (64 tests) and tsc --noEmit in packages/core is clean. The public API is unchanged. No new concerns.
There was a problem hiding this comment.
The new commits fix my earlier point: 402, 432 and MCP isError now use the same short cooldown as 429 (Retry-After, or 60 seconds), and the session stays on the replacement provider. I re-ran the earlier failover tests with the timings shortened to the 60-second cooldown, and they pass with this head. The current websearch tests pass and tsc --noEmit in packages/core is clean. The public API is unchanged. No new concerns.
Issue for this PR
Closes #60176
Related: #54311
Type of change
What does this PR do?
With
websearch.provider: "random", a session sticks to one provider and only moved on HTTP 429. When that provider ran out of quota (HTTP 402 or 432) or its MCP server reported a failure (WebSearchMcp.ToolError, added in #66819, e.g. an exhausted keyless quota), the search failed, and every later search in the session hit the same provider again.Random selection now fails over on those errors too, using the cooldown map the 429 path already has:
provider fails under "random": 429 -> cool down for Retry-After (or 60s), try another provider + 402, 432, MCP isError -> same: cool down for Retry-After (or 60s), try another provider anything else (401, 403, 500, timeout) -> fail with that error every provider failed -> fail with the last provider's error ("Web search failed: <message>") fixed or explicit provider -> no failover (unchanged)Why 401/403 don't rotate: providers send either a key the user connected or no key (keyless). Keyless quota and access problems already arrive as MCP
isErroror 432, so they rotate. A 401/403 most likely means a key the user configured was rejected, and silently rotating away would hide that.Cooldown: every failover trigger uses the existing 429 cooldown (Retry-After, or 60 seconds). On failover the session's remembered provider moves to the new one, so a session doesn't go back to a failed provider unless its replacement fails too; the short cooldown only decides when new sessions can pick it again, and it recovers on its own after a quota reset, a new key, or a one-off error.
The "Limits" section in
services/site/src/docs/content/websearch.mdxnow describes these triggers without naming providers. Documenting keyless search (the other half of #54311) is left out.How did you verify your code works?
bun test test/websearch.test.ts test/tool-websearch.test.ts test/plugin/websearch.test.tsinpackages/core: 58 pass (existing tests; no new ones).bunx tsc --noEmitinpackages/core, andbunx tsc -p tsconfig.build.json --noEmitinpackages/server,packages/sdk,packages/simulation: clean.Checklist
Added by the opencode bot (AI-generated).
Related: #54311, #58327