Skip to content

fix: ship the codemode sandbox in pie-pi-process - #442

Merged
oxwen11 merged 1 commit into
mainfrom
fix/bundle-codemode-sandbox
Oct 6, 2026
Merged

oxwen11 merged 1 commit into
mainfrom
fix/bundle-codemode-sandbox

Conversation

@oxwen11

@oxwen11 oxwen11 commented Oct 5, 2026 •

Copy link
Copy Markdown
Owner

Requirement

Every codemode call in Pie fails, so MCP servers with the default (codemode) exposure are unusable. Found while checking whether MCP, codemode and tool_search load in Pie. This already happens on main (Pi 0.99.1 bundle), so it is not a regression from #433. Stacked on #436 because it extends the same Pi 1.0.2 patch.

The bundle shows two separate failures:

  1. Bun's .wasm file import yields ./quickjs-<hash>.wasm, relative to the bundle. Pi's bun/runtime-setup.js passes that string through as is, so it is read relative to the Session's cwd: Failed to load QuickJS: ENOENT … './quickjs-r1pq2107.wasm'.
  2. Pi resolves the codemode worker as codemode-worker.js beside the bundle (PI_BUNDLED_NODE), but build-pi-process.ts never built it: Cannot find module …/codemode-worker.js.

Expected behavior

  • codemode scripts run in the sandbox whatever the Session cwd is.
  • Scripts can call MCP tools and built-in tools.
  • Compiled Bun binaries (absolute $bunfs paths) are unaffected.

Changes and risks

  • patches/@earendil-works__pi-coding-agent@1.0.2.patch: in dist/bun/runtime-setup.js, a relative wasm path is resolved against import.meta.url. It uses fileURLToPath, so install paths with spaces (macOS Application Support) work.
  • build-pi-process.ts: builds Pi's dist/extensions/codemode/worker.js into dist/pi-process/codemode-worker.js, the same way as image-resize-worker.js.
  • pnpm-lock.yaml: only the patch hash changes.
  • The upstream fix belongs in Pi's runtime-setup; the patch hunk can be removed once Pi ships one.

Verification

Tested revision: c66cb336.

  • New rpc-bundle.test.ts case:
    • copies the bundle into runtime/ and runs it with a different cwd;
    • activates codemode, and the provider calls it with return 6 * 7;
    • asserts isError:false and output 42.
    • Removing codemode-worker.js makes it fail with Cannot find module. Restoring the old wasm line makes it fail with ENOENT './quickjs-…wasm'. With the fix it passes.
  • pnpm build, pnpm check and the node tests pass: 1342 passed, 1 skipped.
  • Web runtime through the Pie daemon (pie-verify web, isolated PIE_HOME and PI_CODING_AGENT_DIR, fake e2e provider, three stdio MCP fixtures with direct, codemode and deferred exposure):
    • Declared tools: codemode and tool_search, plus the direct MCP tools.
    • codemode calling tools.mcp__code_tools__say_hello returns hello from code-tools.
    • tool_search loads 2 deferred tools, and mcp__deferred_tools__weather_lookup then returns sunny per deferred-tools.
    • Direct mcp__dev_tools__say_hello returns hello from dev-tools.
  • Also checked with a script that sends RPC commands straight to the bundle: a codemode script calls tools.bash({command:"pwd"}) and returns the Session cwd.
  • Not verified:
    • a real model writing codemode scripts;
    • the packaged Desktop app (the build output directory is copied whole, but I did not rerun electron-builder for this change).

@oxwen11
oxwen11 added this pull request to stack #437 October 5, 2026 14:46
@oxwen11

oxwen11 commented Oct 5, 2026

Copy link
Copy Markdown
Owner Author

Web runtime evidence at c66cb33: codemode→MCP call, tool_search→deferred tool call, and a direct MCP call through the Pie daemon (screenshots + recording-002).

codemode-mcp-call

tool-search-and-direct-mcp

recording-002.webm

@pkg-pr-new

pkg-pr-new Bot commented Oct 5, 2026 •

Copy link
Copy Markdown
npx https://pkg.pr.new/oxwen11/pie/@getpie/cli@442

commit: 499d016

@oxwen11
oxwen11 force-pushed the fix/bundle-codemode-sandbox branch from c66cb33 to adc149f Compare October 5, 2026 21:17
@oxwen11
oxwen11 force-pushed the fix/bundle-codemode-sandbox branch from adc149f to 83b0f9a Compare October 6, 2026 07:42
@github-actions

github-actions Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

React Doctor skipped this pull request — it changed no React files.

Reviewed by React Doctor for commit 499d016.

@oxwen11
oxwen11 force-pushed the fix/bundle-codemode-sandbox branch from 83b0f9a to b4065d3 Compare October 6, 2026 07:49
@oxwen11

oxwen11 commented Oct 6, 2026

Copy link
Copy Markdown
Owner Author

Rebased onto #436 (aff3ff7) on top of main db2acfa. New head: b4065d3. The runtime-setup hunk is reapplied to the regenerated Pi patch, and the lockfile still changes only the patch hash.
Re-verified at this head:

  • pnpm build and pnpm check pass.
  • Node tests: 1371 passed, 1 skipped.
  • The rebuilt bundle contains codemode-worker.js and quickjs-*.wasm.
  • A bundle codemode script returns 42, and codemode calling an MCP tool returns hello from code-tools.
  • The MCP harness passes all 18 checks.

@oxwen11
oxwen11 force-pushed the fix/bundle-codemode-sandbox branch from b4065d3 to c906fae Compare October 6, 2026 08:42
@oxwen11
oxwen11 marked this pull request as ready for review October 6, 2026 08:43
Base automatically changed from fix/restore-deferred-tools-on-resume to main October 6, 2026 08:58
@oxwen11
oxwen11 force-pushed the fix/bundle-codemode-sandbox branch from c906fae to 8a8a578 Compare October 6, 2026 08:58
@oxwen11
oxwen11 force-pushed the fix/bundle-codemode-sandbox branch from 8a8a578 to 499d016 Compare October 6, 2026 09:00
@oxwen11

oxwen11 commented Oct 6, 2026

Copy link
Copy Markdown
Owner Author

Independent review: ready

  • Reviewed head: 499d01681c4b0a8272c2fa3389cd8eda10d96a2c
  • Base: main @ e8ad3fa89218ea658408ebd8b73d5de8ccc29714
  • Trusted rules: origin/main (.agents/rules last changed in 7b4d86b1), pull-requests.md, review.md, acceptance.md, toolchain.md

1. CI

Every required check finished successfully on 499d0168: Check (run 37439928664), react-doctor (37439928711), and Publish @getpie/cli preview (37439928621 and 37439884956). No check was pending, skipped or stale.

2. Review

  • Patch hunk (dist/bun/runtime-setup.js): only a relative path (./quickjs-<hash>.wasm, which Bun emits for a non-compiled --outdir bundle) is resolved against import.meta.url. In the bundle, that URL is pi-process.js, and the wasm sits next to it. Absolute $bunfs paths from compiled binaries pass through unchanged. fileURLToPath decodes %20, so install paths with spaces work. The worker gets the wasm from the main thread through workerData; it does not resolve the file again.
  • Worker build: Pi's resolveCodemodeWorkerSpecifier("bundled-node") returns new URL("./codemode-worker.js", import.meta.url). The new loop emits that file with the same flags as image-resize-worker.js, and a build failure still stops the build.
  • Lockfile: only the patch hash changes. I checked this by diffing with the hashes normalised.
  • Distribution: the CLI copies the whole dist/pi-process directory. npm pack --dry-run for @getpie/cli lists codemode-worker.js, quickjs-r1pq2107.wasm and pi-process.js. Desktop asarUnpack covers @getpie/server/dist/pi-process/**, so both new files ship as real files.
  • Security: no new trust boundary. The path being resolved is a constant that Bun writes at build time, never user input. Codemode still runs in the existing QuickJS worker sandbox.

3. Independent verification

Run in a clean reviewer worktree at 499d0168, with pnpm install --frozen-lockfile and pnpm build through Turbo. dist/pi-process was freshly built and contains codemode-worker.js and the patched setEmbeddedQuickJSWasmPath(...startsWith(".") ? fileURLToPath(new URL(...)) ...).

  • vitest run rpc-bundle.test.ts fff-island.test.ts: 7/7 pass.
  • pnpm --filter @getpie/server test: 648 passed, 1 skipped.
  • Negative controls on the built bundle:
    • Removing codemode-worker.js makes the new test fail with Cannot find module …/runtime/codemode-worker.js.
    • Restoring setEmbeddedQuickJSWasmPath(quickjs_default) makes it fail with Failed to load QuickJS: ENOENT … './quickjs-r1pq2107.wasm'.
    • Restoring both makes it pass again. The test therefore catches both regressions.
  • Runtime check against the bundle:
    • Setup: bundle copied to a path containing a space (…/App Support/srv/pi-process/, with the sibling fff/). The Session cwd was a separate project directory.

    • Isolation: separate HOME and PI_CODING_AGENT_DIR, fake e2e provider, no real model. Three stdio MCP fixtures with direct, codemode (default) and deferred exposure.

    • Declared tools include codemode and tool_search.

    • Codemode results:

      Script Result
      return 6*7 42
      tools.mcp__code_tools__say_hello({}) hello from code-tools
      tools.bash({command:"pwd"}) the Session cwd
      throw new Error("boom") isError with Error: boom (failure path)
  • Installed CLI layout: the same check against packages/pie/dist/pi-process/pi-process.js returned sunny per code-tools from tools.mcp__code_tools__weather_lookup.

Gaps (not blocking)

  • No real model wrote codemode scripts. Calls were driven by the fake provider.
  • I did not rebuild or launch the packaged Desktop app with electron-builder. Packaging coverage is based on the asarUnpack glob above and on CI's Desktop e2e baseline.
  • I did not repeat the run through the Pie daemon or web UI. The change only affects code inside the pie-pi-process bundle, which I exercised directly with Pie's spawn arguments.

Re-review needed if

The head or base changes, the Pi version or patch changes, or build-pi-process.ts output layout changes.

@oxwen11
oxwen11 merged commit 91247ad into main Oct 6, 2026
6 checks passed
@oxwen11
oxwen11 deleted the fix/bundle-codemode-sandbox branch October 6, 2026 09:24
@oxwen11

oxwen11 commented Oct 6, 2026

Copy link
Copy Markdown
Owner Author

Merged the independently reviewed and verified head 499d016 (record: #442 (comment)) onto base e8ad3fa by squash. Merge commit: 91247ad.

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