Repository navigation
Conversation
pi ships as a Bun-compiled executable, and two things in the extension assumed Node: - convert.mjs ran realpathSync(process.argv[1]) at import to tell a CLI run from an import. Under the compiled binary argv[1] is the virtual /$bunfs/root/pi, so the import threw ENOENT and the extension failed to load. - index.ts called child.stdin.unref(), which Bun's stdin lacks. The throw rejected tools/list, so the extension loaded with no tools. compiled-host.mjs loads the extension as that host does, under any runtime, and checks the tools funes mcp lists are registered.
| // (macOS's /tmp) would otherwise miss. argv[1] need not exist at all: a host compiled into one | ||
| // executable — pi's Bun build — reports a virtual path there (`/$bunfs/root/pi`), and a throw here | ||
| // would fail the import, and with it the extension. |
There was a problem hiding this comment.
I would prefer terser comments. The pr itself explains already why these changes, and so does the commit message.
| // Bun's stdin is a bare Writable with no unref — and pi ships as a Bun-compiled binary. A throw | ||
| // here would cost every tool; a stdin Bun never refs has nothing to release. |
dacorvo
left a comment
There was a problem hiding this comment.
Agent review (Claude Code). I reviewed the review before posting it.
This fixes both bugs on pi 1.3.0 (checked with the binary). With a working funes, the extension loads, the tools register, per-turn indexing works, and pi -p still exits.
Two things to fix before merging:
- Under Bun, a failed spawn of
funesstill takes pi down (index.ts). - The bare
catchininvokedDirectly()turns a loud CLI failure into a silent exit 0 (convert.mjs).
The rest is about the test. Under deno its stdin half checks nothing, and process.exit(0) hides the regression the unref calls exist to prevent. I'd also rather extend startup.mjs than add a second harness, which would make the comment on the pi SDK stub moot.
| child.stdout.unref(); | ||
| child.stderr.unref(); |
There was a problem hiding this comment.
Under Bun, stdout and stderr have no unref either when the spawn fails (funes not on the PATH pi started with, a stale FUNES_BIN, a file that isn't executable). Bun 1.3.14:
spawn("/bin/cat") stdout.unref: function stderr.unref: function
spawn("/nonexistent/funes") stdout.unref: undefined stderr.unref: undefined
So line 94 throws before the error listener (line 109) is attached, and Bun's ENOENT then goes unhandled. pi 1.3.0 exits at startup, and pi -p exits 1 with no answer. Under Node the same setup only warns funes recall is unavailable: funes mcp failed to start: ... ENOENT.
| child.stdout.unref(); | |
| child.stderr.unref(); | |
| child.stdout.unref?.(); | |
| child.stderr.unref?.(); |
With this change the notice names the real cause. Attaching the exit and error listeners right after spawn, before any stream call, would also handle a spawn that fails with EMFILE. In that case child.stdin is undefined, so line 93 throws (on main too).
| // Bun's stdin is a bare Writable with no unref — and pi ships as a Bun-compiled binary. A throw | ||
| // here would cost every tool; a stdin Bun never refs has nothing to release. | ||
| (child.stdin as { unref?: () => void }).unref?.(); |
There was a problem hiding this comment.
Optional: this unref does nothing on any host, so deleting these three lines is simpler than guarding it. Nothing reads child.stdin, so its pipe is never an active handle. A write in flight keeps the loop alive whether the pipe is ref'd or not. Without these lines, the process still exits right after a tool call on Node 22, Node 26 and Bun 1.3.14. (Dropping child.stdout.unref() instead keeps it alive on all three.)
If it stays, child.stdin.unref?.() is the same code. The cast doesn't make the file type-check (tsc still reports TS2339 on lines 94-95), and nothing type-checks it.
| } catch { | ||
| return false; | ||
| } |
There was a problem hiding this comment.
This also swallows failures that have nothing to do with argv[1]. A CLI run then exits 0 having converted nothing. With Deno as the only runtime, setup runs deno convert.mjs <sessions> <spool> with no permission flags. Without a TTY (or when the user denies the prompt), realpathSync throws NotCapable:
- main: exits 1 with
NotCapable: Requires read access to ".../convert.mjs", run again with the --allow-read flag - this branch: exits 0 with no output
setup then removes seed-pending and reports funes installed. So the sessions from before the install are never converted (convertStale() only sweeps sessions newer than the spool record). The compiled binary throws ENOENT, so catching only that keeps your fix:
| } catch { | |
| return false; | |
| } | |
| } catch (e) { | |
| if (e?.code === "ENOENT") return false; | |
| throw e; | |
| } |
| @@ -0,0 +1,54 @@ | |||
| // The extension, loaded the way pi's Bun-compiled binary loads it: argv[1] is a path inside the | |||
There was a problem hiding this comment.
This file re-implements most of startup.mjs:
- the extension copy (line 20 here, startup.mjs:42)
- the fake
funes(lines 23-38 here,fakeFunes()) - the fake pi with
activate(lines 50-52 here,activate())
That's two harnesses to keep in step. If index.ts gains a local import or calls another pi API at activation, one of them gets missed.
Setting argv[1] and the spawn wrapper at the top of startup.mjs (about 12 lines) already fails on main for both bugs. The stdin one fails through the existing local.notices.length check. The wrapper needs a guard there, since child.stdin is null for the stdio: "ignore" worker spawns that startup.mjs drives. Moving the tool-listing fake into startup.mjs and checking the registration in activate() keeps your registration check. That's about +31/-7, against +60 for this file and its run.sh block.
| const root = mkdtempSync(join(tmpdir(), "funes-pi-compiled-")); | ||
| const ext = join(root, "ext"); | ||
| mkdirSync(ext); | ||
| for (const name of ["index.ts", "convert.mjs"]) cpSync(join(HERE, "..", name), join(ext, name)); |
There was a problem hiding this comment.
Under bun, nothing stands in for @mariozechner/pi-coding-agent here (startup.mjs writes a stub at startup.mjs:52-55). So the version import at index.ts:283 makes Bun auto-install the real pi SDK from npm and run it. On a cold cache, sh pi/test/run.sh bun downloads 451 packages (about 214 MB) at this step. It takes 3 to 7 s instead of about 40 ms. With the npm registry unreachable, it hung for over 150 s. The same 2-file stub here avoids it.
| Object.defineProperty(child.stdin, "unref", { value: undefined }); | ||
| return child; | ||
| }; | ||
| module.syncBuiltinESMExports?.(); |
There was a problem hiding this comment.
syncBuiltinESMExports is a no-op under Deno and Bun, so the patched spawn only reaches index.ts's named import under Node. Bun still fails as intended, but only because its real stdin has no unref. Deno's child stdin is a net.Socket that has unref, so under deno the stdin half checks nothing. Revert line 93 of index.ts to child.stdin.unref(); and deno run -A pi/test/compiled-host.mjs still exits 0. So "passes under node, bun and deno" and "with the index.ts change reverted, it fails" don't hold for deno.
A precondition check would catch this. Spawn a probe with the named spawn export (the one index.ts imports), and fail unless typeof probe.stdin.unref === "undefined".
| const { default: activate } = await import(pathToFileURL(join(ext, "index.ts")).href); | ||
| await activate(pi); | ||
| if (tools.join() !== "recall") throw new Error(`expected the recall tool registered, got: ${JSON.stringify(tools)}`); | ||
| process.exit(0); |
There was a problem hiding this comment.
process.exit(0) hides the failure the unref calls in ensureStarted exist for: the idle funes mcp child keeping the event loop alive. Remove child.stdout.unref() or child.unref() from index.ts and this test still passes. Without this line, the test exits on its own, and each of those regressions hangs it. An unref'd watchdog catches them without hanging CI:
| process.exit(0); | |
| setTimeout(() => { | |
| console.error("compiled host: the extension keeps the event loop alive"); | |
| process.exit(1); | |
| }, 5000).unref(); |
(startup.mjs:165 has the same issue, not for this PR.)
pi ships as a Bun-compiled executable (pi 1.3.0 here), and under it the extension gives pi no funes tools. Two Node assumptions break it, one after the other:
The extension fails to load.
convert.mjsrunsrealpathSync(process.argv[1])at import, to tell a CLI run from an import. Under the compiled binary,argv[1]is the virtual/$bunfs/root/pi, so the import throws:The check now treats an
argv[1]that does not resolve as "not run directly".It loads with no tools.
ensureStarted()callschild.stdin.unref(), but Bun's child stdin is a bare Writable withoutunref(plainbuntoo:typeof spawn(...).stdin.unref === "undefined"). TheTypeErrorrejectstools/list, so nothing is registered, and recall is unavailable without an error showing in-pmode. The call is nowunref?.().Checked
pi/test/compiled-host.mjs, run byrun.sh, loads the extension the way that host does under any runtime:argv[1]set to/$bunfs/root/pi, and child stdin with nounref. Its fakefunes mcplists one tool, and the test checks that the tool gets registered. The existing startup test's fake lists none, so it never covered registration.convert.mjschange reverted, it fails withENOENT … lstat '/$bunfs/root/pi'. With theindex.tschange reverted, it fails withexpected the recall tool registered, got: [].sh pi/test/run.sh bunonmainfails atpi startupbecause of the sameunref. It passes on this branch.pi -e ./piregistersget, recall, scan, sessions, sketch, status, a realstatuscall returns, andpi -pstill exits once the turn is done.pi/test/setup.shfails the same way onmainand on this branch on my machine. It expects no JS runtime onPATHonce its fakenodeis removed, but this box has/usr/bin/node. That failure is not related to this change.The manifest version is left for the release.
🤖 Generated with Claude Code