---
title: "Don't wait on the sandbox: let it call back"
date: 2026-10-21
summary: "Running real programs for an agent that lives in a Durable Object: keep files in step, send the command down, and have the sandbox post when the line ends instead of holding a call open."
tags: ["sandboxes", "agents", "durable-objects"]
url: https://blog.misherate.com/posts/let-the-sandbox-call-back
thumbnail: https://blog.misherate.com/thumbnails/let-the-sandbox-call-back-mwfgxo9y.jpg
---

Misha's [iterate](https://iterate.com) mind runs its agents inside itself, in Durable Objects, and some of them need a real computer to run programs on.

An agent waiting on a five-minute test suite is holding a phone line open the whole time, and the next deploy hangs up on it. So we stopped waiting.

One of the mind's agents, pi, runs its whole loop inside the mind: the conversation, its storage and its files live in a [Durable Object](https://developers.cloudflare.com/durable-objects/) facet, and model calls go through the mind's gateway. Most of its tool calls never leave that facet. `ls`, `grep`, `sed` and `jq` run in a bash written in TypeScript over a virtual filesystem; reads fall through to the config repo at the session's base commit, writes land in SQLite, and the diff is what a commit sends.

That covers reading and editing. It doesn't cover `npm install`, `tsc` or `vitest`. Those need a real machine, so phase two sent them to a sandbox.

## Routing a line

The shell decides per line. If every command in it is one the in-facet bash knows, it runs there; anything else goes to the sandbox:

```ts
const local = new Set(["ls", "cat", "grep", "find", "sed", "jq", "head", "tail", "wc"]);

function whereToRun(line: string): "facet" | "sandbox" {
  const commands = parse(line).commands.map((c) => c.name);
  return commands.every((name) => local.has(name)) ? "facet" : "sandbox";
}
```

Before a sandbox line runs, the facet's changed files go down to a plain checkout on the sandbox's disk. After it, `git status` on the sandbox says what the program changed, and those files come back up. `node_modules/` and other ignored files never move, so an install stays on the box where it is useful.

## Waiting on the sandbox

The first cut started the command on the sandbox and then waited on the sandbox for it: a long blocking call that only returned when the line had finished. That is fine for `tsc`. For a test suite that takes minutes, it means a call into the sandbox held open for minutes, and anything that interrupts it (a publication, a dropped connection) leaves the facet not knowing whether the line finished.

Misha's call was to stop waiting on the sandbox at all, even in long blocking waits. The sandbox says when the line ended.

```sh
# what the facet starts on the sandbox, simplified
( cd "$CHECKOUT" && bash -lc "$LINE" ) > "$OUT" 2>&1
code=$?
for attempt in 1 2 3 4; do
  curl -sf -X POST "$DONE_URL" -H "x-done-secret: $SECRET" \
    --data "{\"session\":\"$SESSION\",\"run\":\"$RUN\",\"code\":$code}" && break
  sleep 30
done
```

`DONE_URL` is a host of the mind. Its handler checks the secret and keeps the word once, as one event on the session's stream, `pi/sandbox-done`. The session's stream processor consumes that event and hands it to the facet, which registered its wait for that run before the line started.

```ts
// in the mind's fetch handler, simplified
if (routingSlug === SANDBOX_SLUG) {
  const done = await verifiedDone(request); // wrong or missing secret: refused
  await itx.cd(sessionContext(done.session)).append({
    type: "pi/sandbox-done",
    idempotencyKey: `done:${done.run}`, // a repeated post is kept once
    payload: done,
  });
  return new Response(null, { status: 204 });
}
```

Nothing asks the sandbox anything in between. If the post is lost, one look at the deadline either finds the line ended or stops it. The output (its last 256 KB) and the changed files come back in one look at the end.

## Why it is better

- **The wait is on an event, not a connection.** The word that a line ended is durable, on the session's stream, and it survives a publication or a restart of the facet; the platform revives the processor by its alarm, with no polling schedule.
- **Retries are free.** The sandbox can post several times; the append is keyed by the run.
- **The secret only says one thing.** It is a [secret of the mind, pinned to that one host](/posts/a-proxy-that-holds-the-body-owns-its-size), filled in by the sandbox's egress rather than written into the command, and all it lets anyone say is that a run ended.
- **The log tells the story.** Every line that ran on the sandbox has a start and a `sandbox-done` on the session's stream, so "why did that run hang" is a read, not a guess at a timed-out promise.

The general lesson keeps coming back on this platform: anything that takes longer than a request should end in an event that something waits for, not in a call someone holds open.
