Writing, 21 Oct 2026, 2 min read

Running Cloudflare Code Mode outside Workers, in a Deno sandbox

A drop-in executor that runs LLM-generated Code Mode scripts in Deno with every permission denied, in any Node app.

Cloudflare’s Code Mode lets an LLM write one script that calls your tools, instead of calling them one at a time. The script needs a safe place to run, and the official sandbox only works on Cloudflare Workers. I built codemode-deno-executor, a drop-in executor that runs Code Mode in a locked-down Deno sandbox anywhere Node runs.

The problem

You are building an agent in a Node server, a CLI or CI. You want Code Mode, because one script with a loop beats twenty tool calls. Then you hit two walls.

First, @cloudflare/codemode’s main entry and /ai import cloudflare:workers, so they fail to load in Node with ERR_UNSUPPORTED_ESM_URL_SCHEME. The issue was reported upstream (cloudflare/agents#2142) and closed as not planned.

Second, the README’s example executor for Node runs the generated code with AsyncFunction in your own process. Code written by a model, possibly steered by prompt injection, gets your files, your env vars and your network.

What it does

  • Drop-in. Implements Code Mode’s own Executor interface, so createBrowserCodeTool from /browser accepts it in plain Node.
  • Every permission denied. No network, files, env vars, subprocesses, FFI or system info. Workers spawned inside inherit the same denials.
  • Hard limits. A wall-clock timeout that kills even while (true) {}, a V8 heap cap and an output cap.
  • Same behaviour as upstream. Tool name sanitizing, log prefixes, preludes, and execute never throws.

How it works

Each execute() spawns deno run with --deny-net --deny-read --deny-write --deny-env --deny-run --deny-ffi --deny-sys. Host and sandbox talk newline-delimited JSON over stdio. Inside the sandbox, codemode.getWeather(args) is a proxy that sends a call message. The host runs your real function and sends back a ret. Your API keys never enter the sandbox. Only JSON crosses the boundary.

import { createBrowserCodeTool } from "@cloudflare/codemode/browser";
import { DenoExecutor } from "codemode-deno-executor";

const codeTool = createBrowserCodeTool({
  tools: myTools,
  executor: new DenoExecutor(),
});

On my Windows laptop (i7-1165G7, Node 24, Deno 2.9.7), one execution with one tool call has a median of about 41 ms, process spawn included.

Try it

Needs Node 20+ and Deno 2.

npm install codemode-deno-executor @cloudflare/codemode

What broke along the way

Sandboxed code could crash the host. A Worker inside the sandbox still has Deno.stdout, so it could write a line like null to the protocol channel. The host’s stream handler threw and took down the Node process. Now every line from the sandbox is treated as untrusted and malformed lines are dropped.

Tool names reached Object.prototype. codemode.constructor() called Object on the host. Tools now live in a null-prototype map and only own properties match.

A weird throw hung the run. A tool that threw Object.create(null) caused an unhandled rejection on the host, and the sandbox waited until the timeout. Error formatting can no longer throw.

Each fix has a test that fails without it. The suite went from 35 to 50 tests, running on Linux, macOS and Windows in CI.

What’s next

  • Binary value encoding that matches Cloudflare’s codec
  • An optional process pool to cut startup cost
  • A shared contract test suite any Code Mode executor can run

Code is on GitHub and npm. Questions: pkomalwad@gmail.com.

#agents#mcp#sandbox#deno#cloudflare

Comments

All writing RSS Reply by email