Skip to content

fix(pi): hand pi-ai its provider modules, so a bundled host can sign in - #330

Merged
xxchan merged 2 commits into
mainfrom
fix/pi-static-modules
Oct 10, 2026
Merged

xxchan merged 2 commits into
mainfrom
fix/pi-static-modules

Conversation

@xxchan

@xxchan xxchan commented Oct 10, 2026 •

Copy link
Copy Markdown
Member

Closes #328.

What happened: a host bundled into one file (Ferry's daemon is a Node 24 single executable, one rolldown bundle with no node_modules beside it) could not sign in to any pi provider. pi-ai 1.1.0 loads its sign-in flows (auth/oauth/load.js) and Bedrock (api/bedrock-converse-stream.lazy.js) through a computed path, so that a browser bundler does not follow them into Node-only code. A bundle keeps those as run-time imports of files beside it, so every pi sign-in, every turn on an OAuth credential (its refresh loads the same flow) and every Bedrock call failed with "Cannot find module".

Change: runtimes/pi/static-modules.ts makes the two calls pi's own standalone binary makes (pi-coding-agent/dist/bun/runtime-setup.js): registerBunOAuthFlows() (@earendil-works/pi-ai/bun-oauth) and setBedrockProviderModule(bedrockProviderModule) (@earendil-works/pi-ai/compat and /bedrock-provider). OAR runs pi in-process on Node only, so the browser reason does not apply. They run once per process, before pi first reaches a provider: at the same three points that already set up pi's HTTP proxy plane (session open, piListModels, and the facade runtime behind createPiProviderAuth and createPiModelCatalog), now one preparePi(). The imports are literal, so a bundler follows them, and dynamic, so a host that never touches pi does not pay for them (Bedrock's AWS SDK takes about 60 to 140 ms to load). A bundled host no longer needs its own pi dependency for this.

Also, from Lookout's single-file build:

  • The proxy plane judged undici's stock dispatcher by its class name Agent. In the bundle esbuild renamed npm undici's Agent to Agent4 (more same-named classes once the pi-ai modules joined), so the Agent undici creates itself read as the host's own dispatcher, and pi's requests skipped HTTPS_PROXY with an OAR_PI_PROXY_PLANE warning. It is now judged by class too (instanceof undici.Agent); Node's own bundled undici is never bundled, so its name check stays.
  • Each hand-over is best effort. A host that leaves Bedrock out of its bundle (the AWS SDK adds about 1.4 MB) used to lose every pi call, because the failure was cached. Now the sign-in flows are still handed over, one OAR_PI_MODULES warning says what failed, and the next call tries again.

Not included: a host-facing self-check export (Ferry keeps its own __selftest modules). photon reads photon_rs_bg.wasm from its own directory, so a single-file host still ships that file; docs/runtimes/pi.md says so.

Caveat: setBedrockProviderModule comes from pi-ai's compat entry, which pi marks as temporary. When pi removes it, follow pi's replacement.

Tests: tests/pi/pi-static-modules.test.ts bundles a small host (tests/fixtures/pi-bundled-host.ts) into one ESM file with esbuild, runs it from an empty directory, and checks every sign-in flow: all eight fail to load without the registration and all load with it. It also checks that Bedrock's implementation is in the bundle. tests/pi/pi-http-bundled.test.ts (a renamed Agent still counts as stock; fails without the change) and tests/pi/pi-static-modules-partial.test.ts (a missing Bedrock module warns once, is retried and leaves the sign-in flows handed over). The pi and provider tests, lint and typecheck pass.

🤖 Generated with Claude Code

exe.dev user and others added 2 commits October 10, 2026 08:56
…in (#328)

pi-ai loads its sign-in flows and Bedrock through a computed path. A host
bundled into one file has no such files beside it, so every pi sign-in,
OAuth turn and Bedrock call failed with Cannot find module. Before pi
first reaches a provider (sessions, model listing, login, catalog), OAR
now makes the two calls pi's own standalone binary makes, through literal
dynamic imports a bundler follows.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…er fails

Lookout's single-file build of #330: esbuild renamed npm undici's Agent to
Agent4, so the dispatcher undici creates itself read as the host's own and
the proxy plane was skipped; it is now judged by class too. A host that
leaves Bedrock out of its bundle made the whole hand-over fail and cached
the failure; each module is now handed over on its own, a failure warns
once and is retried.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@xxchan
xxchan merged commit e85dfcf into main Oct 10, 2026
17 checks passed
@xxchan
xxchan deleted the fix/pi-static-modules branch October 10, 2026 09:10
@xxchan xxchan mentioned this pull request Oct 10, 2026
xxchan added a commit that referenced this pull request Oct 10, 2026
Release OAR and CLI 0.54.1. This PR changes only the two package version
fields.

**Fixed: a host bundled into one file can sign in to pi's providers**
(#328, #330)
- pi-ai loads its sign-in flows and its Bedrock implementation through a
computed path, so that a browser bundler does not follow them into
Node-only code. A host bundled into one file (a Node single executable,
such as Ferry's daemon) has no such files beside it, so every pi
sign-in, every turn on an OAuth credential and every Bedrock call failed
with "Cannot find module".
- Before pi first reaches a provider (session open, `piListModels`,
`createPiProviderAuth`, `createPiModelCatalog`), OAR now makes the two
calls pi's own standalone binary makes: `registerBunOAuthFlows()` and
`setBedrockProviderModule(bedrockProviderModule)`. The imports are
literal, so a bundler follows them, and dynamic, so a host that never
uses pi does not load them. A bundled host needs no pi dependency of its
own for this.
- Each hand-over is best effort. A host that leaves Bedrock out of its
bundle (its AWS SDK adds about 1.4 MB) keeps every sign-in, gets one
`OAR_PI_MODULES` process warning, and Bedrock loads pi's own way. A
failed hand-over is tried again on the next call.
- photon still reads `photon_rs_bg.wasm` from its own directory, so a
single-file host ships that file beside it or embeds it.

**Fixed: pi's proxy plane in a bundled host** (#330)
- OAR recognized undici's own default dispatcher by its class name
`Agent`. A bundler can rename that class (esbuild made it `Agent4` in a
single-file host), so OAR took it for the host's own dispatcher and
skipped the proxy plane: pi's requests ignored `HTTPS_PROXY`, with an
`OAR_PI_PROXY_PLANE` warning. It is now recognized by class as well.

**Behavior changes**
- New process warning code `OAR_PI_MODULES`, only when a pi-ai module
cannot be handed over.
- Using pi now loads pi-ai's sign-in flows and Bedrock module (with its
AWS SDK, about 60 to 140 ms) once per process, the first time pi reaches
a provider.

Validation: #330 passed all 17 checks on its final head, and its merged
tree differs from its CI tree only in the version fields from the 0.54.0
release. Lookout bundled main and the PR with a small host into one
CommonJS file each, run from an empty directory with fake credentials
and no network: on main every sign-in flow and Bedrock failed with
"Cannot find module" and pi's requests bypassed `HTTPS_PROXY`; on the PR
all loaded and requests went through the proxy, with no warning. With
Bedrock left out of the bundle, the sign-in flows still loaded and one
warning was printed. A host that never uses pi loaded none of these
modules. After this PR passes CI and merges, tag its exact squash commit
as `v0.54.1` to trigger npm trusted publishing.

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

Co-authored-by: exe.dev user <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

pi: a bundled host can't sign in to pi's providers (sign-in flows and Bedrock load by computed path); register pi's static table in oar

1 participant