pi-mono/packages/codemode/examples/cloudflare
Armin Ronacher 91676d5a70 feat(codemode): one session protocol, simpler scope, djs module bundled out of the box
- A remote execute() is a session with the worker thread's protocol
  (proxy-run.ts): the client drives the VM, as for every other sandbox.
  The transport, execute(request, exchange), is unchanged; its messages
  are opaque apart from { type: "stop" }. worker-protocol.ts is gone.
- CodemodeScope takes tools, globals, and modules in the shapes of the
  options without their functions; identifiers are derived inside, every
  field is optional.
- djs 0.3.0-dev.9 carries its module in its code (Wrangler: a compiled
  module), so no host passes or ships the wasm. pi-codemode imports
  @earendil-works/djs/core everywhere but its one default loader
  (wasm.ts): the worker bundle carries no copy (773 KB -> 104 KB).
- New entry @earendil-works/pi-codemode/remote, the client alone: the
  example agent Worker uploads 35 KiB instead of 1,175 KiB. The remote's
  side moved to runtime/serve.ts.
- Removed cloudflareCodemodeWasm and cloudflareQuickJSWasm; deprecated
  createInlineCodemodeSandbox(). coding-agent drops its wasm path and
  Bun embed.
2026-11-30 19:08:13 +01:00
..
agent feat(codemode): one session protocol, simpler scope, djs module bundled out of the box 2026-11-30 19:08:13 +01:00
sandbox
README.md

pi-codemode on Cloudflare

This example has two Workers:

  • sandbox/ deploys CodemodeSandboxDurableObject as its own Worker, pi-codemode-sandbox. Scripts run there, in a separate isolate.
  • agent/ has a Session Durable Object that owns the tools. It runs scripts through RemoteCodemodeSandbox, which keeps the tools and results in Session while the sandbox Worker runs the VM.

The README of the package explains why the sandbox must be a separate Worker, and which limits apply.

The same sandbox serves pi's durable code mode: pass the RemoteCodemodeSandbox to createCodemodeExtension({ sandbox }) of @earendil-works/pi-durable/codemode. Its scripts then run on the sandbox Worker one step at a time (CodemodeSandboxDurableObject.step()), and nothing stays alive there while tools run or a script is parked.

Try it locally

Install @earendil-works/pi-codemode and wrangler in each directory, then start both Workers in one process:

npx wrangler dev -c agent/wrangler.jsonc -c sandbox/wrangler.jsonc
curl -X POST http://localhost:8787 --data 'const n = await tools.count({ add: 2 }); text("n=" + n); return n'

Deploy

Deploy the sandbox first, because the agent's binding refers to it by name:

(cd sandbox && npx wrangler deploy)
(cd agent && npx wrangler deploy)