Plan: web search falls back to Google when Exa throttles
Closes: #853. Relates to: #852 (pi-web-access 0.48.0), #831 and #832 (the project handed to Google’s library). Branch: feat/web-search-vertex-fallback. Status: Done (2026-11-18) — as planned, with the errata below
Erratum (2026-11-18): the seed is its own module, packages/pi-agents/web-search-seed.ts, re-exported from index.ts; and docs/modules/ROOT/pages/public-surface.adoc gained the row for XDG_CONFIG_HOME, which the seed reads to find the plugin’s directory. Neither was in Scope.
Erratum (2026-11-18): the measured rows. luchs, built distribution, no relay: consumer-sim 23 passed, none failed or skipped, its live check a session’s web_search by name through Gemini. luchs, the seeded route on the patched plugin, 16 routed searches at once: Exa answered 4 and refused 12, Gemini answered all 12 in 4 to 13 s, none failed. panther: frozen install applies the patch, validate passes, the full consumer-sim 23 passed with the Gemini search answered on that box’s own login, so the %APPDATA% path and the impersonated type work on Windows; panther has no web-search.json, so the seed applies there. Not run: Exa refusing and the route handing on for real on Windows; a domain filter on a fallback outside the spike.
Context
Web search comes from the bundled pi-web-access (0.48.0). No box has a web-search.json, so every search goes to Exa’s keyless endpoint, which is limited per address and shared by every session and helper on the box. On 2026-11-17 it refused the operator and then one of four of this agent’s own searches ("You’ve hit Exa’s free MCP rate limit"). Nothing is configured behind it, so a refused search is a failed search.
Spiked on luchs (scripts under .pi/scratch/websearch/): Gemini 3.8 Flash with Google Search grounding, through Vertex, answered 14 of 14 real queries and 20 at once, at a median 20 s against Exa’s 0.4 s. It returns a written answer with source links, not a ranked list, and ignored both domain filters tried. So it is a fallback, not a replacement. The operator’s decision (2026-11-18): keep pi-web-access, Exa first, Gemini behind it, no per-box setup, and make our login work by a tracked patch to the bundled plugin, with the same fix drafted as an upstream pull request for him to send.
Three facts read in node_modules/pi-web-access/:
-
gemini-adc.tsmints the Vertex token by hand and accepts two credential types (authorized_user,service_account) and the cloud metadata server. Any other type throws "Gemini ADC unsupported credential type". luchs and panther both holdimpersonated_service_account. -
The same file looks for the credentials file only at
~/.config/gcloud/application_default_credentials.json. On Windows gcloud keeps it under%APPDATA%\gcloud, so there the login reads as absent. -
It reads the project from
geminiProject,GOOGLE_CLOUD_PROJECTorGCLOUD_PROJECT. pi-vertex names the project for Google’s library as the lower-casegoogle_cloud_project(#832,packages/pi-vertex/gemini.ts), which this code does not read.
google-auth-library (10.9.1, Apache-2.0) is already a dependency of pi-vertex and already bundled; it handles every credential type, the Windows path and token refresh.
Design
D1. The patch to pi-web-access
One patch file, patches/pi-web-access@0.48.0.patch, applied by pnpm (patchedDependencies in pnpm-workspace.yaml). It touches gemini-adc.ts only:
-
getAdcAccessTokenasks oneGoogleAuthclient per process (scopecloud-platform,projectIdfromgetAdcProject(), so the library starts nogcloudchild to find one, the fault of #831) for its access token. The hand-written exchanges for the two types and the metadata server are removed; the library covers them.clearAdcTokenCachedrops the client. A failure whose HTTP status is 400, 401 or 403, or whose text isinvalid_grant, is thrown as the file’s existingCredentialResolutionError("Gemini ADC", "oauth-credential-rejected"); any other failure as a plainErrornaming the status, never the token. -
A new exported pure function
defaultAdcPath(platform, env, home)gives the well-known path:%APPDATA%\gcloud\application_default_credentials.jsononwin32whenAPPDATAis set, else the path of today.getAdcPathuses it. -
getAdcProjectalso readsgcloud_projectandgoogle_cloud_project, the two further names Google’s library reads, after the two it reads now.
pnpm-workspace.yaml also gains a packageExtensions entry giving pi-web-access@0.48.0 the dependency google-auth-library at pi-vertex’s range, so the import is declared and not an accident of hoisting. The bundler (tools/lib/bundle.mjs) builds 40-web-access from the patched source as it does now; the library’s packages join that bundle and its notices, all under licences already allowed.
Nothing else in the plugin changes. isGeminiAdcAvailable keeps its rule: geminiAuth is adc, no Gemini key or base URL is configured, a project and location are known, and the credentials file exists.
D2. The seeded configuration
packages/pi-agents/index.ts already seeds the bundled pi-subagents' settings file when it is absent (seedSubagentsSettings, written with flag wx), and loads before the web extension. Beside it, seedWebSearchSettings() writes the plugin’s config file when the plugin would find none.
Where that file is, is the plugin’s rule, not pi’s agent directory (getWebSearchConfigDir in node_modules/pi-web-access/utils.ts): $PI_CODING_AGENT_DIR when set; else, with XDG_CONFIG_HOME set, $XDG_CONFIG_HOME/pi, unless only the older ~/.pi/web-search.json exists; else ~/.pi/agent, with the same older file as fallback. A pure webSearchConfigDir(env, home, exists) in pi-agents states that rule, and the seed is written to web-search.json in the directory it returns, only when that file does not exist (the older file existing makes the rule return ~/.pi, where it exists, so nothing is written). A contract test holds the function equal to the plugin’s own on every branch. The seed:
{
"geminiAuth": "adc",
"geminiLocation": "global",
"searchModel": "gemini-3.8-flash",
"searchRouting": {
"providers": ["exa", "gemini"],
"fallbackOn": ["quota", "transient", "network", "invalid-response"]
},
"pdf": { "provider": "unpdf" }
}
-
Each key has a reader in the plugin:
searchModelandsearchRoutingingemini-search.ts(getSearchConfig, lines 146-235),geminiAuthandgeminiLocationingemini-adc.ts,pdf.providerinpdf-extract.ts(lines 46-138, valueunpdf). -
No project is written: D1 lets the plugin read the one pi-vertex names in the environment.
-
pdf.provideris pinned because turning the Vertex login on would otherwise make the plugin’sautoPDF order send every PDF to Gemini, where today it is read locally. Page fetching is unchanged: the plugin uses Gemini for a page only withfetchRouting.allowRemoteHostedProviders, which stays unset. -
A box with no Vertex project or no Google login keeps today’s behaviour: the route skips Gemini as unavailable.
-
The file is the user’s once written and is never rewritten; an existing file is left alone. The first session is told once that it was written (`settingsNotice’s pattern), or why it could not be.
A search that names a provider itself (provider: "exa") is not routed, as now.
D3. The upstream pull request
The same change as a patch against upstream’s main, with a description in upstream’s own terms (credential types, the Windows path, the lower-case project names; no mention of this repository), posted on the issue with its sha256 for the operator to send. Agents send nothing upstream. When upstream ships it, the patch file and the two pnpm-workspace.yaml entries are deleted with the bump.
Scope
In: patches/pi-web-access@0.48.0.patch (new), pnpm-workspace.yaml, pnpm-lock.yaml, packages/pi-agents/index.ts and its tests, packages/pi/test/ (new contract and regression tests), tools/consumer-sim.mjs, docs/modules/ROOT/pages/tuning.adoc, CHANGELOG.adoc.
Out: an Exa key; any other provider; changing what an existing web-search.json holds; domain filters under Gemini; google-auth-library 11.
Risks
| Risk | Mode | Mitigation |
|---|---|---|
A later bump of pi-web-access drops the patch without anyone noticing |
fail-closed |
The patch is keyed to the version, and a test pins that the installed |
A fallback answer ignores a domain or date filter the caller gave |
silent |
Documented in |
A revoked or expired Google login |
fail-closed |
Thrown as the plugin’s credential error, which the route does not fall past; the search fails naming "Gemini ADC", as Exa’s refusal did. Pinned by a contract test. |
The seeded file is not where the plugin reads (another config directory) |
silent |
A contract test holds |
PDFs start going to Gemini |
fail-closed |
|
The token leaks into an error or log |
fail-closed |
The plugin’s existing redaction stays on the request path; the contract test asserts no thrown message holds the token. |
Search queries reach Google’s global endpoint on a fallback |
silent |
Stated in |
Tests
Contract (packages/pi/test/web-access-adc.test.ts, the patched module loaded as the bundle loads it, fetch stubbed, a temporary credentials file named by GOOGLE_APPLICATION_CREDENTIALS):
-
An
impersonated_service_accountfile yields the impersonated token, and the requests made are the source token exchange and then the IAMgenerateAccessTokencall. Anauthorized_userfile still yields its token. -
A 400
invalid_grantis the credential error; a 503 is a plain error; neither message holds a token. -
defaultAdcPath: Windows withAPPDATA, Windows without, Linux. -
getAdcProjectreads each of the four names, in order, andgeminiProjectfirst. -
The pin: the installed source imports
google-auth-libraryand lacks the old error text.
Contract (packages/pi/test/web-search-seed.test.ts): the seed text parses under the plugin’s own readers to the route exa, gemini with the four fallback kinds, the search model, ADC selected, location global, and PDF provider unpdf; webSearchConfigDir equals the plugin’s getWebSearchConfigDir() on the seven branches named in Risks.
Regression, the case that would have caught today’s failure (same file): with the seed in force and Vertex answering a grounded result, the plugin’s search() returns Gemini’s answer attributed to gemini when Exa’s keyless endpoint answers as it did on 2026-11-17, HTTP 200 with _meta["ai.exa/rateLimited"] true (exa.ts:313-318), and again when it answers HTTP 429; with Exa answering normally, Gemini is not called.
Unit (packages/pi-agents/test/): seedWebSearchSettings writes the exact text when absent, into the directory the rule gives (under XDG_CONFIG_HOME too); leaves an existing file byte-equal; writes nothing when the older ~/.pi file exists; reports an unwritable directory; the notice says it once.
tools/consumer-sim.mjs: on a fresh install the first session leaves web-search.json with the seeded route (no model needed); with models, one session asked for a web_search with provider: "gemini" gets a result headed "Provider: gemini" and no error (skipped with --no-model).
Measured before merge, prediction on file first:
-
luchs, on the built distribution with no relay: an explicit Gemini search; then Exa driven until it refuses and a routed search falling through, with the time it took.
-
panther, by the Windows seat: whether a
web-search.jsonalready exists there; an explicit Gemini search (the Windows credentials path); the suites.
Delivery
-
Issue filed; plan committed in
docs/modules/ROOT/pages/plans/web-search-vertex-fallback.adocwith itsnav.adocline. -
One branch: the patch, the seed, the tests, the consumer-sim checks; `tuning.adoc’s Web access section (the route, the seed, the patch and when it goes, the filter and privacy notes); CHANGELOG under Unreleased, Added.
-
node tools/task.mjs validate, fullconsumer-sim, the measured rows; MR closing the issue, squash off, the merge SHA on the issue; the upstream patch and description posted on the issue for the operator.