---
title: "Introducing pi: a coding agent that lives inside the mind"
summary: "pi runs its whole agent loop in a Durable Object of the mind: conversation, files and progress are SQLite rows that survive a deploy mid-thought, and a real computer starts only for a line that runs a real program."
app: "pi"
subject: a part of the mind, not an app
created: 2026-10-20
revision: 1
updated: 2026-11-15
url: https://blog.misherate.com/introducing/pi/at/ac5aeb41819183cf72a2c40fe7ab73c4ee76cd77
current: https://blog.misherate.com/introducing/pi
---

Misha's [iterate](https://iterate.com) mind is one git repository of its own code, and everything that code runs. This page is about an agent that runs inside it, rather than beside it.

What if a coding agent didn't need a computer at all, right up until it did?

Most of what a coding agent does is read files, think, edit files and commit. None of that needs a Linux box. So pi lives in the mind itself, in a [Durable Object](https://developers.cloudflare.com/durable-objects/) with a SQLite database: its conversation, its files and its progress are rows. It survives a deploy halfway through a thought. 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.

The agent loop is [pi](https://github.com/earendil-works/pi), by Earendil Works, and specifically its `pi-durable` package, which keeps a conversation, its tasks and its checkpoints in pluggable storage. This page is how it is wired into a mind.

## 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");
```

## What 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 a 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 (`commit`, `push`, `checkout` and friends) 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, keyed by the run, 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; past the line's timeout one look stops it or finds it ended. [Don't wait on the sandbox: let it call back](/posts/let-the-sandbox-call-back) is the story of that design, and the secret it posts with can say nothing but "this run ended".

## 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, it compares each changed file at the base and at the tip. None changed there: it commits on the tip. Some did: it moves the session onto the tip, drops pi's edits to just those files, and tells pi to make them again. A three-way merge would be kinder, and is still on the list. [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), the same idea as opencode's. When `pi/answered` lands, the mind's event handler tells the answer to where the prompt came from, exactly once (it records `pi/told` and takes a short lease, so two deliveries can't both send), and books the message as the session's, so a reply to it goes on in that session. A prompt from an agent with `notify` is told in that agent's conversation instead. A read-only viewer pages through the conversation straight from the facet's SQLite, bounded and redacted, without ever opening or resuming 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 pi's packages are bundled into one package (with code splitting on: without it, a module was inlined before the class it needed existed), the bad imports rewritten to fail up front, and the bundle pinned by commit like any dependency.

## 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.
