Repository navigation
fix(cli): the pi bridge takes its MCP server list over fd 3, never the environment (TASK-070) - #1774
Merged
Merged
Conversation
…e environment (TASK-070)
`delete env.COMMONLY_PI_MCP` scrubbed Node's copy of the list, not the copy the
kernel made at exec — so a same-user child of the pi process could still read the
seat's runtime token back with `ps eww $PPID` (macOS) or `/proc/$PPID/environ`
(Linux). The list carries every stdio server's env and every http server's
Authorization header, each substituted with `${COMMONLY_AGENT_TOKEN}`.
The channel is now a pipe on fd 3: `runPi` spawns pi with a fourth stdio entry,
writes the JSON into it and ends it; `takeServers` reads it to EOF and closes it
at extension load — before pi's first model turn, and before any bash tool can
run.
Measured against pi 0.84.1 before building it (row TASK-070): pi does not close
inherited descriptors before loading extensions, so fd 3 is still open when the
bridge reads it; and pi's own spawns (`dist/core/tools/bash.js`,
`dist/core/exec.js`) pass a three-element stdio list, so a shell-tool child never
inherits fd 3 at all.
One measurement changed the code: on macOS/Node 20 a `closeSync` in `finally`
aborts the process — `Assertion failed: (errno == EINTR), function uv__io_poll,
file kqueue.c, line 264` — when the read failed first (an ignored fd 3 reads as
ENXIO). Closing only a descriptor we successfully read keeps "no channel"
graceful instead of turning it into a SIGABRT in the seat.
Tests: the adapter asserts the 4th stdio entry exists exactly when there is a
list, that the payload on it carries the filled placeholders, and that neither
the child's environment nor argv holds the token; the client's tests spawn a real
child with a real inherited pipe — inheritance is only a property of a real child
— and pin that the read consumes the pipe, that a missing fd 3 yields no servers
rather than an abort, and that a `COMMONLY_PI_MCP` in the environment is ignored.
lilyshen0722
force-pushed
the
kai/task070-pi-bridge-fd3
branch
from
September 19, 2026 18:00
62c8ec8 to
a1ef3e8
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The defect
The pi bridge receives its MCP server list as
COMMONLY_PI_MCPin the child'senvironment. That list carries every stdio server's
envand every http server'sAuthorizationheader, each already substituted with${COMMONLY_AGENT_TOKEN}—the seat's runtime bearer token.
takeServersdeleted the variable from the bridge's own copy, which closed theaccidental vector (pi's
bashtool spawns with{ ...process.env }, so a bareenvused to print it). It never closed the real one: the kernel keeps theenvironment the process was started with, so any same-user child could still
read it back with
ps eww $PPID(macOS) or/proc/$PPID/environ(Linux). Thecomment on
takeServerssaid so; this PR is the row that comment pointed at.The channel
A pipe on fd 3.
runPispawns pi with a fourth stdio entry, writes the JSONinto it and ends it;
takeServersreads it to EOF and closes the descriptor atextension load. The pipe exists exactly when there is a list to hand over, so a
seat with no declared servers is spawned exactly as before (three stdio entries,
no bridge extension).
Why not a 0600 file the bridge unlinks (the fallback the row named): the mode
protects nothing against a same-user reader, and that shape needs both the unlink
and a close to leave no window, because
/proc/<pid>/fdon Linux still reaches anunlinked inode. Why the environment cannot work at all: the bridge runs inside
the pi process, so unsetting anything at spawn time is unsetting a copy the child
can no longer be prevented from seeing.
Measured before building (row TASK-070)
fd 3 is still open when the bridge reads it. Probed with a real extension passed
via
-ereading/dev/fd/3at module load:READ_OK, against a negativecontrol with no fd 3 that failed
EBADF.dist/core/tools/bash.jsspawns the shell with
['pipe'|'ignore','pipe','pipe'],dist/core/exec.jsthesame), so a shell-tool child never inherits fd 3. Confirmed two ways: the
source, and Node's own semantics measured in a probe.
second reader gets nothing; after a 5-byte partial read the remainder was still
readable. So the pipe's boundary is the consumption, and the close is hygiene.
One measurement changed the code
The first version closed the descriptor in a
finally, which is the obviousshape and is wrong on macOS: when the read has already failed — an
'ignore'stdio entry reads as
ENXIO—closeSyncaborts the process(
Assertion failed: (errno == EINTR), function uv__io_poll, file kqueue.c, line 264, Node 20). Closing only a descriptor we successfully read keeps thedegraded path graceful, instead of turning "no channel" into a SIGABRT in the
seat. There is a test for exactly that.
A bridge that cannot read its list now also says so on stderr
(
[commonly-pi-bridge] no server list on fd 3 …), because the alternativesymptom is a seat that silently has no
commonly_*tools — which is also what afuture pi that closes inherited fds would look like.
Tests
clisuite: 620 passed / 10 skipped / 0 failed (630 total),lint:cliclean.Adapter (
adapters.pi.test.mjs): the 4th stdio entry exists exactly when there isa list; the payload on it carries the filled placeholders; the child's
environment and argv carry no token and no
COMMONLY_PI_MCP; a spawn seam with nofd 3 fails loudly rather than leaving the bridge with nothing to read.
Client (
adapters.pi.bridge.test.mjs): a real child with a real inherited pipe —fd inheritance is only a property of a real child, so these are not in-process
tests — pins the read, the consumption (a second read of the drained descriptor
returns no secret), the graceful no-fd-3 path, and that a
COMMONLY_PI_MCPinthe environment is ignored even when the child really holds it.
Five mutations, each reddening the witness it belongs to:
takeServersfalls back toprocess.env.COMMONLY_PI_MCPfinally, even when the read failedrunPiOne of those mutations earned a test fix: the environment test originally set
process.env.COMMONLY_PI_MCPin-process, and passed against a reader thatconsulted it — a
process.envmutation inside a jest test does not reach aspawned child. The child is now given the variable explicitly, which is why the
mutation reddens it.
Scope
cli/src/lib/adapters/{pi.js,pi-commonly-mcp.mjs,pi-mcp-client.mjs}, their twotest files,
docs/agents/pi-adapter.md(which described the env channel), andcli/package.json→ 0.1.57 (mainis 0.1.56; the only open PRs adding aversion line propose 0.1.19 / 0.1.20, so the ladder is unambiguous).
Not in this PR: any change to how a declared server is judged — the broker
refusal and the transport rule stay exactly where TASK-054/TASK-063 left them.
Gate criteria (Vera's, from the row) map as: (1) read at extension load, before
the first model turn ✓; (2) the descriptor is consumed and closed ✓ — with the
correction that consumption, not the close, is what removes the secret; (3)
neither env nor argv carries the contents ✓, and the file shape's path in argv is
not needed at all.