Skip to content

Carry the Pi bridge channel over a socket so every launcher works - #6857

Merged
ymichael merged 2 commits into
mainfrom
bb/fix-pi-bridge-stdio-transport-on-windows-thr_dbjt58x49g
Nov 2, 2026
Merged

ymichael merged 2 commits into
mainfrom
bb/fix-pi-bridge-stdio-transport-on-windows-thr_dbjt58x49g

Conversation

@ymichael

@ymichael ymichael commented Nov 2, 2026

Copy link
Copy Markdown
Collaborator

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:

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 (Fix Pi Windows npm shim probing and RPC pipes #5271, Run Pi installer managed launches directly on Windows #6839) is no longer needed. The version probe already launches .cmd shims through the host toolchain (experimental_execHostFile, since One host toolchain for executable discovery, child PATH, and proxy/CA defaults #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:

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:

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

ymichael and others added 2 commits November 2, 2026 12:42
…s 3 and 4

bb's Pi extension talked to the bridge over extra stdio descriptors, which
cmd.exe shims (pnpm), Bun's pi.exe, and launchers that start pi as a child
process do not forward, so the extension failed to load with "Unsupported fd
type: UNKNOWN" (#6790, #5332). The bridge now listens on a Unix socket in a
private temp directory (a named pipe on Windows) and passes its address and a
one-time token through BB_PI_BRIDGE_CHANNEL and BB_PI_BRIDGE_CHANNEL_TOKEN.
The extension connects once per pi process, so reloads reuse the connection
instead of opening a second reader on the same descriptor.

Environment variables survive every launcher, so the Windows npm and Pi
installer unwrapping in windows-pi-launch.ts is deleted; the version probe
already launches .cmd shims through the host toolchain.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit 35700c3 into main Nov 2, 2026
51 checks passed
@ymichael
ymichael deleted the bb/fix-pi-bridge-stdio-transport-on-windows-thr_dbjt58x49g branch November 2, 2026 22:25
ymichael added a commit that referenced this pull request Nov 2, 2026
## Human comments

## What was wrong

Pi's installers (`pi.dev/install.sh`, `install.ps1`) create a managed
install: a launcher in `~/.pi/agent/bin` (on Unix, symlinked from a PATH
directory such as `~/.local/bin`) and releases under
`~/.pi/agent/install`, marked by `managed-install.json`. Pi updates it
with `pi update`. The Pi provider's maintenance bridge only knew Bun and
npm global installs. It reported the managed install as `external` but
still offered `npm install -g @earendil-works/pi-coding-agent@latest`
for any install that wasn't Bun. Running that adds a second,
npm-installed Pi and leaves the managed one on the old version.

Reproduced with Pi's real installers pinned to 1.4.0 (served from a
local API) on macOS with a sandbox `HOME`, and on the Windows 11 ARM64
VM with a sandbox `PI_CODING_AGENT_DIR`. Both called
`getPiProviderInstallationStatus` / `getPiProviderInstallationRun` on
main:

```text
macOS    installSource "external"  installAction.command "npm install -g @earendil-works/pi-coding-agent@latest"
Windows  installSource "external"  run.command "npm.cmd install -g @earendil-works/pi-coding-agent@latest"   (executablePath ...\agent\bin\pi.cmd)
```

## What changed

- **`plugins/provider-pi/src/bridge/pi-managed-install.ts` (new)**:
`resolvePiManagedInstallRoot` resolves the `pi` on PATH with `realpath`
and reads `<agent>/install/managed-install.json` (BOM tolerated,
validated with zod: `kind`, `schemaVersion: 1`, `layout:
"releases-v1"`). #6857 removed the earlier Windows-only marker check, so
this is now the only one.
- **`provider-maintenance.ts`**: chooses the install or update command
in this order:
  1. Fresh install: npm, as before.
2. Managed install: `pi update --self`. This is checked before npm, so a
managed launcher placed in npm's bin directory isn't treated as an npm
install.
  3. Bun: `bun add -g`, as before.
  4. npm global: npm, as before.
5. Anything else: `pi update --self` instead of npm. Pi detects its own
npm, pnpm, yarn or Bun install, or a configured update command, and
fails with its own instructions when it can't.
- **Install gate**: the "older than the supported minimum" message now
gives the command that matches the install.
- **`installSource` is unchanged.** Managed installs report `external`,
as Claude's native installs do, so there's no wire or SDK change and no
protocol bump.
- README updated.

Related to #3166: this stops bb from creating a duplicate npm Pi. It
doesn't add the general detection #3166 asks for, which would recognise
installs owned by another package manager across all providers.

## How you verified

- New `provider-maintenance.install-source.test.ts`:
- covers a managed launcher inside npm's bin directory, the installer's
relative PATH symlink, an invalid marker layout (still npm), an
unclassified install, and the install-gate message;
- four of the five tests fail on main; the fifth checks that a real npm
install still gets npm.
- `pnpm exec turbo run test typecheck lint
--filter=bb-plugin-provider-pi`: 39 files, 348 passed, 5 skipped.
- **macOS, rebased onto c236529:** the 1.4.0 managed sandbox now
offers `pi update --self`. Before the rebase, running that command
updated the managed install to 1.5.0 and left the npm Pi at 1.4.0.
Status afterwards showed `currentVersion 1.5.0`, `installAction null`.
- **Windows VM, before the rebase:** the bundled status reported `pi
update --self` for `...\agent\bin\pi.cmd`. `cmd.exe /d /s /c "pi update
--self"` updated it from 1.4.0 to 1.5.0, the npm Pi stayed at 1.4.0, and
status afterwards was clean. The user PATH was unchanged and the sandbox
was removed.

> AGENT GENERATED

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

---------

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.

1 participant