Skip to content

fix(pi): load under pi's Bun-compiled binary - #23

Open
0x7067 wants to merge 1 commit into
huggingface:mainfrom
0x7067:fix/pi-bun-compiled-host
Open

0x7067 wants to merge 1 commit into
huggingface:mainfrom
0x7067:fix/pi-bun-compiled-host

Conversation

@0x7067

@0x7067 0x7067 commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

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:

  1. The extension fails to load. convert.mjs runs 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 throws:

    Error: Failed to load extension "~/.funes/agents/pi/index.ts": Failed to load extension: ENOENT: no such file or directory, lstat '/$bunfs/root/pi'
    

    The check now treats an argv[1] that does not resolve as "not run directly".

  2. It loads with no tools. ensureStarted() calls child.stdin.unref(), but Bun's child stdin is a bare Writable without unref (plain bun too: typeof spawn(...).stdin.unref === "undefined"). The TypeError rejects tools/list, so nothing is registered, and recall is unavailable without an error showing in -p mode. The call is now unref?.().

Checked

  • New pi/test/compiled-host.mjs, run by run.sh, loads the extension the way that host does under any runtime: argv[1] set to /$bunfs/root/pi, and child stdin with no unref. Its fake funes mcp lists one tool, and the test checks that the tool gets registered. The existing startup test's fake lists none, so it never covered registration.
    • It passes under node, bun and deno.
    • With the convert.mjs change reverted, it fails with ENOENT … lstat '/$bunfs/root/pi'. With the index.ts change reverted, it fails with expected the recall tool registered, got: [].
  • sh pi/test/run.sh bun on main fails at pi startup because of the same unref. It passes on this branch.
  • In pi 1.3.0 (the Bun binary), pi -e ./pi registers get, recall, scan, sessions, sketch, status, a real status call returns, and pi -p still exits once the turn is done.
  • pi/test/setup.sh fails the same way on main and on this branch on my machine. It expects no JS runtime on PATH once its fake node is 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

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.
Comment thread pi/convert.mjs
Comment on lines +292 to +294
// (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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer terser comments. The pr itself explains already why these changes, and so does the commit message.

Comment thread pi/index.ts
Comment on lines +91 to +92
// 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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

@dacorvo dacorvo left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 funes still takes pi down (index.ts).
  • The bare catch in invokedDirectly() 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.

Comment thread pi/index.ts
Comment on lines 94 to 95
child.stdout.unref();
child.stderr.unref();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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).

Comment thread pi/index.ts
Comment on lines +91 to +93
// 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?.();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread pi/convert.mjs
Comment on lines +299 to +301
} catch {
return false;
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Suggested change
} catch {
return false;
}
} catch (e) {
if (e?.code === "ENOENT") return false;
throw e;
}

Comment thread pi/test/compiled-host.mjs
@@ -0,0 +1,54 @@
// The extension, loaded the way pi's Bun-compiled binary loads it: argv[1] is a path inside the

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread pi/test/compiled-host.mjs
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));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread pi/test/compiled-host.mjs
Object.defineProperty(child.stdin, "unref", { value: undefined });
return child;
};
module.syncBuiltinESMExports?.();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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".

Comment thread pi/test/compiled-host.mjs
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);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Suggested change
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.)

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.

2 participants