Skip to content

fix(cli): the pi bridge takes its MCP server list over fd 3, never the environment (TASK-070) - #1774

Merged
lilyshen0722 merged 1 commit into
mainfrom
kai/task070-pi-bridge-fd3
Sep 19, 2026
Merged

lilyshen0722 merged 1 commit into
mainfrom
kai/task070-pi-bridge-fd3

Conversation

@lilyshen0722

Copy link
Copy Markdown
Contributor

The defect

The pi bridge receives its MCP server list as COMMONLY_PI_MCP in the child's
environment. That list carries every stdio server's env and every http server's
Authorization header, each already substituted with ${COMMONLY_AGENT_TOKEN} —
the seat's runtime bearer token.

takeServers deleted the variable from the bridge's own copy, which closed the
accidental vector (pi's bash tool spawns with { ...process.env }, so a bare
env used to print it). It never closed the real one: the kernel keeps the
environment 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). The
comment on takeServers said so; this PR is the row that comment pointed at.

The channel

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 the descriptor at
extension 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>/fd on Linux still reaches an
unlinked 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)

  • pi 0.84.1 does not close inherited descriptors before extensions load, so
    fd 3 is still open when the bridge reads it. Probed with a real extension passed
    via -e reading /dev/fd/3 at module load: READ_OK, against a negative
    control with no fd 3 that failed EBADF.
  • pi's own spawns pass a three-element stdio list (dist/core/tools/bash.js
    spawns the shell with ['pipe'|'ignore','pipe','pipe'], dist/core/exec.js the
    same), so a shell-tool child never inherits fd 3. Confirmed two ways: the
    source, and Node's own semantics measured in a probe.
  • The read is what removes the secret, not the close. After a read to EOF a
    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 obvious
shape and is wrong on macOS: when the read has already failed — an 'ignore'
stdio entry reads as ENXIO — closeSync aborts 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 the
degraded 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 alternative
symptom is a seat that silently has no commonly_* tools — which is also what a
future pi that closes inherited fds would look like.

Tests

cli suite: 620 passed / 10 skipped / 0 failed (630 total), lint:cli clean.

Adapter (adapters.pi.test.mjs): the 4th stdio entry exists exactly when there is
a 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 no
fd 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_MCP in
the environment is ignored even when the child really holds it.

Five mutations, each reddening the witness it belongs to:

mutation red
the list also written back into the child env the fd-3/env test
takeServers falls back to process.env.COMMONLY_PI_MCP the env-ignored test
close in finally, even when the read failed the no-fd-3 test (SIGABRT)
the payload never handed to runPi the three spawn tests
a missing fd 3 pipe swallowed instead of failing the invariant test

One of those mutations earned a test fix: the environment test originally set
process.env.COMMONLY_PI_MCP in-process, and passed against a reader that
consulted it
— a process.env mutation inside a jest test does not reach a
spawned 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 two
test files, docs/agents/pi-adapter.md (which described the env channel), and
cli/package.json → 0.1.57 (main is 0.1.56; the only open PRs adding a
version 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.

…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
lilyshen0722 force-pushed the kai/task070-pi-bridge-fd3 branch from 62c8ec8 to a1ef3e8 Compare September 19, 2026 18:00
@lilyshen0722
lilyshen0722 merged commit 042c9bf into main Sep 19, 2026
13 checks passed
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