---
title: "Introducing pi-durable: the pi coding agent, living in a Durable Object"
summary: "pi, one of the most used open-source coding agents, running on pi-durable inside a Durable Object of the mind: every turn and tool call is a SQLite row, a deploy mid-thought costs nothing, and a real computer starts only for a line that needs one."
app: "pi"
subject: a part of the mind, not an app
created: 2026-10-20
revision: 2
updated: 2026-11-15
url: https://blog.misherate.com/introducing/pi/at/266c2715794db1bff2a76a2ca6537f179955f7cd
current: https://blog.misherate.com/introducing/pi
previous: https://blog.misherate.com/introducing/pi/at/ac5aeb41819183cf72a2c40fe7ab73c4ee76cd77
---

Misha's [iterate](https://iterate.com) mind is one git repository of its own code, and everything that code runs. This page is about the coding agent that runs inside it: [pi](https://github.com/earendil-works/pi), made durable.

Most coding agents are a process on a machine. Kill the process and the half-finished thought is gone; keep the machine on and you pay for it to wait while the model thinks. pi-durable turns that inside out: the agent is a database. Every model turn, every tool call and every checkpoint is a row, committed before anything is shown. The process that runs it can be evicted, redeployed or crash halfway through a step, and the next one opens the same rows and carries on from the last checkpoint.

## pi, and why not write our own

pi, by Earendil Works, is one of the most widely used open-source coding agents: over 120,000 GitHub stars by November 2026, a CLI people use every day, and a toolkit underneath it. That adoption is the point. An agent loop looks like a weekend project until you meet compaction, retries, providers that change their APIs, tool calls cut off mid-run and costs to account for. With pi, thousands of other people hit those bugs first.

The piece this mind uses is [`pi-durable`](https://github.com/earendil-works/pi/tree/main/packages/durable), pi's durable conversation and task runtime (marked experimental upstream). It keeps conversations, tasks and checkpoints in pluggable storage, and it ships a SQLite adapter for a Cloudflare [Durable Object](https://developers.cloudflare.com/durable-objects/). A Durable Object is a small, single-threaded object with its own SQLite database that the platform starts when a request arrives and evicts when idle. So pi lives in the mind itself, in one of those: conversation, files and progress are rows. A real computer starts only when a command line runs a real program, like `npm test`, and then the computer calls back when the line is done.

## What it looks like

A chat message `pi: make the snake slightly faster on level 3` starts a session. pi reads the game's code, edits it, greps for the other uses of the speed constant, and commits; the mind publishes the commit, pi sees the publication succeed, and its answer comes back to the chat with what it changed. A reply to that answer goes on in the same session.

Agents have it as a capability, as they have [opencode](/introducing/opencode):

```ts
const name = await itx.pi.start("Rename the speed constant and update its uses", {
  notify: (await itx.whoami()).path, // my conversation is told the answer
});
await itx.pi.send(name, "Now run the tests");
```

## Building it in your own mind

The rest of this page is the recipe. "You" here is whoever builds the same thing into another iterate mind: a person, or that person's mind reading this page. You need:

- **A facet**: a Durable Object of the mind with its own SQLite, hosting a stream processor for the session's event stream.
- **pi-durable** with its SQLite storage adapter, and its coding tools (read, write, edit, bash) on a filesystem you supply.
- **Models through the mind's AI gateway**, called in-process (below).
- **The config repo's read and commit API** (`listFiles`, `readFile`, `commitFiles`).
- Optionally, **a sandbox** for real programs, a host of the mind for it to call back, and a secret pinned to that host.

## Prompts are owed until answered

The session is an event stream, `/pi/sessions/<name>`. A prompt is a fact on it, and the processor's whole state is the list of prompts not yet answered:

```ts
reduce(state, event) {
  if (event.type === "pi/prompted") return { owed: [...state.owed, event.payload] };
  if (event.type === "pi/answered") return { owed: state.owed.filter((p) => p.input !== event.payload.input) };
}

processEvent({ state, delivery, runInBackground, append }) {
  const next = state.owed[0];
  if (!delivery.caughtUp || !next || this.running) return; // only at the head: a replay starts nothing
  this.running = next.input;
  runInBackground(async () => {
    const answered = await this.run(next);  // never throws
    this.running = null;
    await append({ type: "pi/answered", idempotencyKey: `answered:${next.input}`, payload: answered });
  });
}
```

`runInBackground` keeps the run alive past the event that started it. If the Durable Object is evicted mid-run, the platform revives it by its alarm, the prompt is still owed, and the run starts again. That restart costs almost nothing, because the prompt's input key is also pi-durable's `requestId`: the harness finds the submission it already made and carries on from its last checkpoint, rather than starting the conversation over. No schedule polls anything.

## Files that are rows

pi never sees a disk. It sees a copy-on-write filesystem over the config repo:

```ts
class RepoFs {
  async readFile(path) {
    const mine = this.overlay.get(path);         // pi's edits, kept in SQLite
    if (mine) return mine.kind === "deleted" ? enoent(path) : mine.data;
    return this.base.read(path);                 // the repo at the session's base commit
  }
  async writeFile(path, data) { this.overlay.set(path, { kind: "file", data }); await this.save(path); }
  changes() { /* overlay minus base: exactly what a commit sends */ }
}
```

Reads fall through to the repo at the commit the session started from; writes land in the overlay. The diff between them is what `commit` sends, so there is no staging step and nothing to forget.

## A shell with two halves

pi's `bash` tool runs [just-bash](https://github.com/vercel-labs/just-bash), a bash written in TypeScript, over that same filesystem, inside the Durable Object. `ls`, `grep`, `sed`, `jq` and `curl` (through the mind's egress) all run there. So does a small `git`: `status`, `diff`, `log`, `show`, `rev-parse` and `ls-files`, answered from the overlay and the repo's history. Git that would change the repo is refused; the commit tool is the only way out.

Anything else, a line that runs `node`, `npm`, `tsc` or a browser, goes whole to a sandbox, since a pipe can't be split across two machines:

```ts
function placeOf(line: string): "facet" | "sandbox" {
  const programs = commandWords(line).map(lookThroughWrappers); // `timeout 5 vitest` -> vitest
  return programs.every((p) => LOCAL.has(p)) ? "facet" : "sandbox";
}
```

A misreading only sends a line to the sandbox that didn't need it: slower, never wrong.

## The sandbox, only when needed

When a line does go to the sandbox, the facet's files stay the truth, and a plain checkout on the sandbox's disk is kept in step around each line:

1. **Prepare**: the checkout exists, at the session's base commit.
2. **Down**: the files pi changed since the last line are written into it (the facet remembers a hash per path, so only changes move).
3. **Run**: the line starts in the background, in its own process group. When it ends, the sandbox posts the exit code to a host of the mind, which keeps it as one event on the session's stream, and that event ends the facet's wait.
4. **Up**: the output (its last 256 KB) and whatever `git status` says changed come back into the overlay. Ignored files such as `node_modules` never move, and one over 1.5 MB stays on the sandbox.

Nothing asks the sandbox anything in between. [Don't wait on the sandbox: let it call back](/posts/let-the-sandbox-call-back) is the story of that design.

## Models without a network hop

pi's model layer speaks OpenAI's Responses API through the OpenAI SDK, and that SDK accepts a `fetch`. So the facet hands it one that never touches the network:

```ts
const fetch = async (input, init) => {
  const body = await new Request(input, init).json();
  const answer = await itx.ai.run(`openai/${body.model}`, body, { gateway: "default" });
  return answer instanceof ReadableStream
    ? new Response(answer, { headers: { "content-type": "text/event-stream" } })
    : Response.json(answer);
};
```

No key in the facet; the gateway has the mind's. Sessions think at medium effort, because pi's default of "off" is sent as effort `none`, which the newest model refused.

## Committing

`commit` sends the overlay's changes to the config repo's main with the session's base commit as the parent, then waits for the mind's publication event and tells pi whether it published. If main moved meanwhile and none of pi's files changed there, it commits on the tip; if some did, it moves the session onto the tip, drops pi's edits to just those files, and tells pi to make them again. [Agents that commit their own config](/posts/agents-that-commit-their-own-config) explains why the parent is the safety catch.

## Telling whoever asked

Each prompt carries a note of facts beside the words (who wrote it, through what, where the answer goes). When `pi/answered` lands, the mind tells the answer to where the prompt came from, exactly once (it records `pi/told` under a short lease), and books the message as the session's, so a reply goes on in it. A read-only viewer pages through the conversation straight from the facet's SQLite, without ever opening the harness.

## Getting it to load

The platform's module loader refuses a computed `import()`, and pi's model layer hides Node-only imports behind one. So the pi packages the facet uses are bundled into one package, the bad imports rewritten to fail up front, and the bundle pinned by commit like any dependency.

## Extensions: what carries over

People usually extend the pi CLI with extensions, skills, prompt templates and themes, bundled as pi packages and installed from npm or git (`pi install npm:…`). An extension is a TypeScript module the CLI loads at startup; it can register tools, block or rewrite tool calls, add slash commands and draw terminal UI.

Those don't load in the facet as they are. There is no CLI process and no extensions folder on a disk, the platform won't import code fetched at runtime, and commands and terminal UI have nothing to attach to. pi-durable has an extension shape of its own, `defineExtension({ tools, sections, hooks, wraps, tasks })`, installed into a registry the host owns, and that works: the mind's `commit` tool and its project prompt section are one such extension. So a CLI extension that only adds tools or guards tool calls ports by rewriting it against `defineTool` and hooks and bundling it with the facet, and a skill, being Markdown, could become a prompt section. We haven't ported one yet.

## What it costs, next to a sandbox agent

The model's tokens are the same either way, and for a real coding task they are usually most of the bill. The difference is the machine. At Cloudflare's list prices (November 2026, before free allowances; this mind's facets run on iterate's account, and we have not measured a side-by-side task):

- **Sandbox agent** (half a vCPU, 4 GiB, 8 GB disk, the size [opencode](/introducing/opencode)'s sandbox uses): memory and disk are billed for every second it is up, about $0.04 an hour, plus CPU while busy, about $0.009 an hour at a quarter load. It stays up while the model thinks, and parks after five idle minutes.
- **pi-durable in a Durable Object**: 128 MB of wall-clock time while it is working, waits on the model included, about $0.006 an hour, plus a fraction of a cent of SQLite writes per session. Idle, it costs nothing but its stored rows.

So the agent's own machine is roughly eight times cheaper per working hour, nothing while idle, and the sandbox is still there, starting only for the lines that need it. The bigger wins are that a deploy or eviction mid-task costs nothing, and nobody has to tune when a container parks.

## pi or opencode?

- **pi**: small, file-centric changes; cheap to keep; survives deploys mid-thought; no machine idling.
- **opencode**: long builds, browsers, video, anything that wants a real terminal for an hour.

Both are delegates; [how the mind uses agents](/introducing/agents) is how their answers find their way back to a person.
