Repository navigation
Fix Pi Windows npm shim probing and RPC pipes - #5271
Conversation
d014fb2 to
606364b
Compare
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 606364b2d0
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| /** | ||
| * Runs a CLI with buffered stdout/stderr, resolving Windows npm `.cmd` shims. | ||
| * Bounds output with `maxBuffer` and stops descendants on timeout or abort. | ||
| * Throws errors carrying `code`, `signal`, `killed`, `stdout` and `stderr`. |
There was a problem hiding this comment.
Preserve the promised error shape for pre-aborted signals
When a bridge passes an AbortSignal that is already aborted, execPortableFile exits at packages/process-utils/src/exec-portable-file.ts:25 by throwing signal.reason directly; that value may be a string or DOMException, so it lacks the five fields this new public declaration and audit entry promise on failure. Callers following the exported contract can therefore fail while handling this cancellation path; normalize the early-abort branch into the same structured AbortError used after launch (or explicitly narrow the guarantee).
AGENTS.md reference: AGENTS.md:L28-L28
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
I've removed the SDK export from this PR, so it no longer introduces the AbortSignal error contract you flagged. The provider now uses its own Pi process-launch resolver with plain execFile. This removes the newly exposed contract rather than changing the existing internal early-abort behavior. Updated in 7f3043b.
|
hey @yteruel31, i hit the same bug (#5332) with pi in windows. i raised a small fix PR in MayankBansal12#7 Same idea, but it leaves |
|
@MayankBansal12 Thanks, you're right about preserving Pi's installation identity. I've separated configuration resolution from the actual process launch and kept the fix provider-local, using plain |
The npm shim removes .JS from PATHEXT before it looks up `node`, so a `node.js` file in the working directory is never mistaken for the Node executable. The unwrapped launch searched the thread's working directory with the full PATHEXT, so a workspace with a root-level `node.js` had that file chosen as the command and Pi could not start. Co-Authored-By: Claude Code <[email protected]>
## Human comments ## What was wrong Pi 1.4.0's Windows installer (`irm https://pi.dev/install.ps1 | iex`) creates a managed install, not an npm global one. `~\.pi\agent\bin\pi.cmd` runs `cmd.exe`, then `node pi-launcher.js`, which re-spawns the Pi CLI with `stdio: "inherit"`. On Windows neither hop forwards descriptors 3 and 4, which bb's Pi bridge extension uses, so the extension fails with `Unsupported fd type: UNKNOWN` and neither the model catalog nor Pi threads can start. #5271 bypassed only npm's own `pi.cmd` template, so managed launchers still went through the broken chain. Unix managed launchers are unaffected: `exec` and `spawnSync` inherit the descriptors there (checked on macOS). ## What changed `plugins/provider-pi` only. No host protocol, SDK, CLI or configuration changes. - `windows-pi-launch.ts`: `resolvePiManagedLauncher` recognises a managed `pi.cmd` by Pi's own marker (`<agent>\install\managed-install.json` with `kind: "pi-managed-install"`, `schemaVersion: 1`, `layout: "releases-v1"`) plus the sibling `pi-launcher.js`. It resolves the active release CLI the same way `pi-launcher.js` does (`current-version`, the package `bin`, containment check), and selects the same Node: `%LOCALAPPDATA%\pi-node\current\node.exe` when the launcher references it and it exists, otherwise `node.exe` from the child's PATH. The package-CLI resolution is shared with the existing npm shim path. - The resolved launch now carries its child env. Managed launches add `PI_MANAGED_INSTALL_ROOT`, as `pi-launcher.js` does; Pi's startup uses it to clean up managed installs. `rpc-child.ts` and the version probe in `provider-maintenance.ts` spawn with that env. - Anything unrecognised launches exactly as before. ## How you verified - Tests: unit tests for managed-launcher resolution, standalone-Node selection (case-insensitive `LOCALAPPDATA`), and rejection of an unknown layout, a missing launcher script, and an escaping version. A Windows-only round-trip test sends RPC plus both extra pipes through the installer's real `pi.cmd` and `pi-launcher.js` (fixture contents copied from `install.ps1`), and asserts that `PI_MANAGED_INSTALL_ROOT` reaches the child. Without the fix it would hit the descriptor loss. - `pnpm exec turbo run typecheck lint test --filter=bb-plugin-provider-pi`: 333 passed, 8 skipped (Windows-only tests skipped on macOS; they should run in the Windows CI plugin shard). - Native Windows 11 ARM64 VM (Node 24.22.0, Pi 1.4.0): built a managed install with the installer's exact launcher files and ran a standalone bundle of this branch's `getPiCatalog` with the real bb Pi extension: | Case | Before | After | |---|---|---| | Managed `pi.cmd` via `BB_PI_BRIDGE_COMMAND` | `CATALOG FAILED: … Unsupported fd type: UNKNOWN` | `CATALOG OK`, version probe `1.4.0` | | Managed `pi.cmd` found on `Path` | (same failure) | `CATALOG OK`, version probe `1.4.0` | | npm global `pi.cmd` (control) | `CATALOG OK` | `CATALOG OK` | I did not run a full thread turn on Windows (the VM's Pi has no models configured). Thread start uses the same `PiRpcChild` launch path. Fixes #6790 > AGENT GENERATED 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <[email protected]>
) ## Human comments ## What was wrong bb's Pi extension talked to the bridge over extra stdio descriptors: the bridge spawned Pi with five pipes, and the injected extension read fd 4 and wrote fd 3. Launchers pass on only stdin, stdout and stderr. That includes `cmd.exe` shims, Bun's `pi.exe` shim, and any wrapper that starts Pi as a child process (such as the Pi installer's `pi-launcher.js`). Behind any of them the extension failed to load with `Failed to load extension: Unsupported fd type: UNKNOWN` (#6790, #5332). #5271 and #6839 worked around two specific launchers by unwrapping them in `windows-pi-launch.ts`. Other launchers were still broken. Reproduced on the Windows 11 ARM64 VM on main 5758191 with Pi 1.4.0 and Node 24.22.0. A harness bundles the bridge with the real extension, waits for the extension's `ready`, then sends a `model-scope` request over the channel and waits for the reply: ```text pnpm pi.cmd CHANNEL FAILED: pi exited (code 1, signal null): Error: Failed to load extension "...\bb-pi-extension.mjs": Failed to load extension: Unsupported fd type: UNKNOWN bun pi.exe CHANNEL FAILED: pi exited (code 1, signal null): Error: Failed to load extension "...\bb-pi-extension.mjs": Failed to load extension: Unsupported fd type: UNKNOWN ``` On macOS, main fails the same way behind the Pi installer's launcher (`extension ready timed out`), because it starts Pi as a child process with only standard stdio. ## What changed - **`plugins/provider-pi/src/bridge/bridge-channel.ts` (new)**: the bridge listens on a Unix socket in a private `mkdtemp` directory, or on a random named pipe on Windows. It passes the address and a one-time token to Pi in `BB_PI_BRIDGE_CHANNEL` and `BB_PI_BRIDGE_CHANNEL_TOKEN`. - It accepts only a connection whose first line is the token. Named pipes are listable, and other users can read them by default. - It queues bridge-to-extension messages until that connection arrives, then stops listening. - It closes the socket and removes the directory when Pi exits. - **`rpc-child.ts`**: spawns Pi with plain stdio (`pipe` ×3) and routes `sendChannel` and channel lines through the socket. A listen error is appended to the stderr tail and kills the child. - **`bb-pi-extension.ts`**: - Reads both env vars and removes them, so Pi's own child processes don't inherit them. - Waits for the connection before finishing its load; Pi 0.84 and 1.4 both wait for extension loading. - Keeps one connection per Pi process on `globalThis`, so the factory runs Pi repeats on `ctx.reload()`, `newSession()` and `switchSession()` reuse it. On main, a second load threw `EEXIST` opening fd 4 again, so reloads were already broken. - Drops the Bun-only `Bun.file(4)` reader, because `net.connect` works under Node and Bun. - **Deleted `windows-pi-launch.ts` and its tests.** Env vars reach Pi through every launcher, so the npm `pi.cmd` and Pi-installer unwrapping (#5271, #6839) is no longer needed. The version probe already launches `.cmd` shims through the host toolchain (`experimental_execHostFile`, since #6194), so `resolvePiProcessLaunch` is gone too. - **`packages/provider-bridge-protocol/src/testing/replay-provider-child.mjs`**: the `pi-rpc` replay dialect connects to the same socket instead of fds 3 and 4. - **Fake Pi** (`fake-pi-rpc.mjs`): waits for the extension factory, as Pi does, and adds a `/reload` prompt that reloads the extension. - **README**: documents the bridge channel and `/reload`. Killing a session leaves no Pi processes behind, so no unwrapping had to stay: - **Windows**: bb's kill path (`experimental_killPortableProcess`, i.e. `taskkill /T /F`) kills the shim's whole process tree. Closing stdin also ends Pi through `cmd.exe` and Bun's shim. - **POSIX**: kill signals the launcher, and Pi exits on stdin EOF, because bb closes stdin before signalling. Measured on the VM: pnpm `pi.cmd` and Bun `pi.exe`, each killed by tree kill, by stdin EOF, and by killing the root only, all left zero Pi processes. The managed launcher on macOS also left zero under both kill and graceful close. No wire, CLI, SDK-surface or user-configuration changes. The two new env vars pass between the bridge and its Pi child only. `check-plugin-sdk-version.mjs` reports the surface unchanged. Known limitation: `pnpm parity` uses one replay child for both checkouts. A Pi parity comparison between a checkout from before this PR and one after it shows the older side as empty. Comparisons where both checkouts include this PR work. ## How you verified New tests: - `bridge.launchers.test.ts`, all platforms: - A dynamic-tool round trip through a Node launcher that starts Pi as a child with `stdio: "inherit"`. - The same round trip after `/reload`. - `bridge.launchers.test.ts`, Windows only (runs in the `plugins-1` Windows shard): the same round trip through a pnpm-template `pi.cmd`, and through the Pi installer's `pi.cmd` + `pi-launcher.js`. - `bridge-channel.test.ts`: a wrong-token connection is closed, gets no data, and its lines are never delivered. Queued messages reach the authenticated connection. Both all-platform launcher tests fail against main's transport: the child-launcher test times out, and the reload test fails with `open EEXIST`. The existing Windows npm-shim tests (`bridge.install-gate` model/list and `provider-maintenance.version` probe) stay and now run through `cmd.exe` without unwrapping. Commands: ```sh pnpm exec turbo run typecheck lint test --filter=bb-plugin-provider-pi --filter=@bb/provider-bridge-protocol --filter=@bb/provider-parity node .github/workflows/check-plugin-sdk-version.mjs # Plugin SDK surface unchanged. ``` All tasks pass on macOS. The provider-pi run includes the Bun runtime test (Bun 1.3.0). Recorded Pi sessions: I ran `pnpm parity --provider pi` main-vs-main and branch-vs-branch with `--dump-dir`. Both pass 9/9 cells (1 skipped), and `diff -r` of the two dump directories is empty: the normalized events and rows are byte-identical. Native Windows 11 ARM64 VM (Node 24.22.0, Pi 1.4.0, Bun 1.4.4), real Pi with the branch's bridge and extension: ```text npm pi.cmd graceful CHANNEL OK VERSION PROBE 1.4.0 leftover pi processes=0 managed pi.cmd graceful CHANNEL OK VERSION PROBE 1.4.0 leftover pi processes=0 pnpm pi.cmd graceful CHANNEL OK VERSION PROBE 1.4.0 leftover pi processes=0 bun pi.exe graceful CHANNEL OK VERSION PROBE 1.4.0 leftover pi processes=0 node cli.js graceful CHANNEL OK VERSION PROBE 1.4.0 leftover pi processes=0 bun.exe cli.js graceful CHANNEL OK VERSION PROBE 1.4.0 leftover pi processes=0 (same six with kill: CHANNEL OK, leftover pi processes=0) ``` On the same VM I ran vitest directly on the branch source, because Turbo's binary crashes on this ARM64 VM. `bridge-channel`, `bridge.launchers` (all 5, including both Windows-only tests), `bridge.install-gate`, `provider-maintenance.version` and `bridge.bun-runtime` pass. Three `bridge.relocation` tests fail there because they shell out to `cygpath` and the VM has no Git for Windows; this change doesn't touch them. macOS with real Pi 1.4.0, for npm global `pi`, the Pi installer launcher and `bun cli.js`: the channel round trip succeeds under both graceful close and kill, with no leftover processes. On main, the installer launcher fails. Not tested: Volta, which isn't installed on the VM. Refs #6790, #5332 > AGENT GENERATED 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
Human comments
What was wrong
Two Windows npm-shim issues remain after #4661.
probePiVersion()usesexecFile("pi", ["--version"]), which cannot launch the npmpi.cmdshim and can reportENOENTdespite Pi being installed. The RPC launcher can execute that shim throughcmd.exe, but the intermediate shell does not forward BB's extra stdio descriptors 3 and 4; the extension then fails withUnsupported fd type: UNKNOWN. These were reproduced with BB 0.45.0 and Pi 0.85.1, and the version-probe issue also reproduces with Pi 1.1.0. The unchanged probe is visible in the branch base.What changed
resolvePiLaunch()as the original configuration resolver. Installation identity, executable metadata, cache keys, and npm/Bun maintenance continue to describe Pi, not its Node interpreter.pi.cmdshims into Node plus the validated package CLI. Resolve against the child's environment/cwd, prefer siblingnode.exe, otherwise resolve Node from the child's PATH, and preserve configured prefix arguments.node_modules/.binlayouts. Validate manifest/bin contents and physical CLI containment; only accept standard npm shim templates targeting that manifest CLI. Custom wrappers remain unchanged, including custom files namedpi.cmdbeside a valid Pi package.node.exe, look upnodewith.JSremoved fromPATHEXT, as the npm shim does, so anode.jsfile in the thread's working directory is not chosen as the Node executable.node:child_processexecFilefor probing, including its existing 15 s timeout, stdout-only parsing, output bound, and failure diagnostics. RPC launches retain both the ordinary stream and the extra bidirectional extension channel.Following review feedback, this PR no longer introduces an SDK export or version bump. The earlier SDK/docs changes and unrelated attachment-preview fix have been removed. The resulting diff is confined to
provider-piand itswhich-commandlockfile entry. No host protocol, CLI, user-facing configuration, or installed-plugin changes.How you verified
Linux:
The final Turbo run passed 11/11 tasks: 233 tests passed, 7 skipped, plus lint, typecheck and bundled preparation. Windows-only tests are skipped on Linux. Both accepted npm templates and LF/CRLF handling are covered; custom-wrapper tests retain a valid npm target while injecting additional commands, so they test actual wrapper rejection rather than merely a mismatched target.
Native Windows, Node 24.18.0: bundled the final upstream implementation into a temporary standalone module without installing dependencies or modifying the running BB service. Global and local canonical npm fixtures verify version probing/install-gate success, original
pi.cmdinstallation identity, configured arguments, custom-wrapper preservation, ordinary RPC, and descriptor 4 → CLI → descriptor 3 round trips.The live smoke uses the machine's existing Pi 1.1.0,
--no-session --no-extensions, and an explicit temporary BB extension. An initial run with automatically loaded user extensions timed out; disabling those extensions isolated this launch fix and passed. No model prompt or full thread turn was sent, no native session was persisted, and installed providers/configuration were not changed. Windows regressions were exercised through equivalent standalone bundles, not a full native Windows Vitest run. CI and review still validate integration before merge.Fixes #5332