1
# @earendil-works/pi-durable3
> **Experimental.** The API changes without notice between releases.5
A durable agent harness. Conversations, model turns, tool calls, and your own state are committed to storage before anything is shown. If the process dies mid-turn, reopening the storage picks the work up where it stopped.7
Built on [`@earendil-works/pi-ai`](../ai/README.md) for model access and `@earendil-works/chord` for document state.9
## Table of Contents11
- [Installation](#installation)12
- [Quick Start](#quick-start)13
- [Concepts](#concepts)14
- [Persist and Resume](#persist-and-resume)15
- [Extensions](#extensions)16
- [Tools](#tools)17
- [Code Mode](#code-mode)18
- [System Prompt](#system-prompt)19
- [Per-Conversation Agent](#per-conversation-agent)20
- [Settings](#settings)21
- [Environment](#environment)22
- [Reload](#reload)23
- [Watching a Conversation](#watching-a-conversation)24
- [Busy Conversations](#busy-conversations)25
- [Reset and Handoff](#reset-and-handoff)26
- [Compaction](#compaction)27
- [Agent Events (Experimental)](#agent-events-experimental)28
- [Hooks](#hooks)29
- [More Conversations and Forks](#more-conversations-and-forks)30
- [Abort and Subagents](#abort-and-subagents)31
- [Child Tasks](#child-tasks)32
- [Task Graph](#task-graph)33
- [Your Own State](#your-own-state)34
- [Usage and Cost](#usage-and-cost)35
- [Storage](#storage)36
- [Errors](#errors)37
- [Examples](#examples)38
- [Design Documents](#design-documents)40
## Installation42
```bash43
npm install @earendil-works/pi-durable @earendil-works/pi-ai @earendil-works/chord44
```46
## Quick Start48
```typescript49
import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context";50
import { createModels } from "@earendil-works/pi-ai/models";51
import { openaiProvider } from "@earendil-works/pi-ai/providers/openai";52
import { AssistantEntry, createRegistry, Harness, MemoryStorage } from "@earendil-works/pi-durable";54
const context = BACKGROUND_CONTEXT;56
const models = createModels();57
models.setProvider(openaiProvider()); // reads OPENAI_API_KEY59
const harness = await Harness.open(new MemoryStorage(), { models, registry: createRegistry() }, context);60
const root = await harness.root(context, { agent: { model: { provider: "openai", modelId: "gpt-6-sol" } } });62
const submission = await root.submit({ type: "input", content: "What is the capital of France?" }, context);63
const settled = await submission.wait(context);64
if (settled.status === "done" && settled.type === "input") {65
const answer = await root.commit((tx) => tx.entry(AssistantEntry, settled.answer), context);66
console.log(answer?.model?.[0]);67
}68
await harness.close(context);69
```71
What happened:73
- `Harness.open()` opens a Session over a storage backend. `MemoryStorage` keeps everything in memory.74
- `root()` returns the root conversation, creating it on first use with the given agent choices. A conversation is a transcript of immutable entries.75
- `submit()` durably admits your input and returns a `Submission`. A built-in generation task calls the model and appends the answer.76
- `wait()` resolves once the input is answered (`done`) or has failed (`unanswered`, with a reason).78
Every async call takes a Chord `Context`, which carries cancellation. `BACKGROUND_CONTEXT` never cancels. Cancelling a wait only cancels that wait, never the work.80
## Concepts82
- **Harness**: one open storage plus the machinery that runs agents on it. All changes go through one line of atomic commits, and nothing is shown before its commit is stored.83
- **Conversation**: a transcript. `root()` creates the root conversation on first use; you can create more and fork them. A `Conversation` handle holds no state; compare handles by `id`.84
- **Entry**: one immutable transcript record, such as a user message (`pi.user`), a model response (`pi.assistant`), a tool result (`pi.tool-result`), a system prompt change (`pi.system`), a reset (`pi.reset`), or your own kind. The model sees the entries from the newest reset onward.85
- **Commit**: an atomic write. `conversation.commit((tx) => ...)` can append entries, edit documents, and create tasks together; either all of it is stored or none of it.86
- **Document**: typed JSON state stored next to the transcript and changed in commits. Built-in ones hold each conversation's agent choices (`pi.agent`), provider-facing session identity (`pi.provider`), running generation and tools (`pi.live`), queued submissions (`pi.inbox`), and spend (`pi.usage`).87
- **Task**: a durable state machine that saves a checkpoint at every step, so a restarted process continues from the last one. Every task has an owner: its conversation, or another task. The Harness runs answers as built-in tasks: `pi.generation` calls the model and owns the `pi.tool` tasks of its tool calls, waits for them, and hands the run to the next generation.88
- **Submission**: something you hand to a conversation, either user input or an entry to write, which you can wait for.89
- **Turn and run**: a turn is one model response and its tool calls; a run is the turns from an input to its final answer. A conversation is busy while a run is going.90
- **Extension**: a named bundle of tools, system prompt sections, hooks, wrappers, and tasks.91
- **Registry**: the extensions this process installed. It can change while the Harness runs; new work uses the new state.92
- **Agent**: what a conversation runs with: model, thinking level, selected extensions, tools, instructions, and working directory. Stored per conversation as names in `pi.agent`, resolved against the registry at each use.94
One answered input, as entries and tasks:96
```text97
submit(input) → pi.user98
pi.generation → pi.system (only if the prompt or tools changed), pi.assistant (tool calls)99
pi.tool × n → pi.tool-result × n (owned by the generation, which waits for them)100
pi.generation → pi.assistant (answer) → submission done101
```103
## Persist and Resume105
Use SQLite or JSONL storage to keep conversations across restarts:107
```typescript108
import { openNodeSqliteStorage } from "@earendil-works/pi-durable/storage/sqlite/node";110
const harness = await Harness.open(await openNodeSqliteStorage("./session.sqlite"), { models, registry }, context);111
const root = await harness.root(context); // the same root as last time112
harness.resume(); // continue any run the last process left unfinished113
```115
Work interrupted by a crash or close stays pending. `resume()` starts the task scheduler; submitting or waiting starts it too. Each conversation has its own persisted UUIDv7 in `pi.provider`, forwarded to pi-ai as `sessionId` for provider prompt-cache and session affinity. It survives reopen, retries, reset, compaction, and model changes; a child or fork receives a fresh identity. A legacy conversation receives and persists one before its first generation or compaction request.117
A retried submission with the same `requestId` returns the existing submission instead of submitting twice:119
```typescript120
const submission = await root.submit({ type: "input", content: "Hello", requestId: "greeting-1" }, context);121
// After a restart: the same request ID finds the same submission.122
const again = await root.submit({ type: "input", content: "Hello", requestId: "greeting-1" }, context);123
// again.id === submission.id124
```126
`harness.submission(id)` reacquires a submission by ID, for example to wait for it after a restart.128
## Extensions130
Code the Harness runs, other than its built-in tasks, comes in named extensions installed in a registry your process owns:132
```typescript133
import { createRegistry, defineExtension, defineTool, hook, section, ToolTask } from "@earendil-works/pi-durable";134
import { CodingTools } from "@earendil-works/pi-durable/tools";136
const Coding = defineExtension({137
name: "coding",138
sections: [section("preamble", () => "You are a concise coding assistant.", { tag: false })],139
hooks: [hook(ToolTask, { beforeTool: (call) => (isDangerous(call) ? { block: "Needs approval" } : undefined) })],140
});142
const registry = createRegistry();143
registry.install(CodingTools);144
registry.install(Coding);145
```147
An extension may bring `tools`, `sections`, `hooks`, `wraps` (decorators of a tool or section by name), and `tasks`. By default every conversation selects every installed extension, in install order. Nothing in the registry is stored; conversations store extension names.149
## Tools151
`@earendil-works/pi-durable/tools` provides `read`, `write`, `edit`, and `bash`, and the `CodingTools` extension with all four. They touch files and processes only through the call's environment (see [Environment](#environment)).153
`read` returns an image file as one image block, which programs calling it get as an `ImageContent`. Limits are the model's `inputLimits.images.resize`, by default 2000x2000 pixels and 4.5 MB of base64. Without an image processor it sends PNG, JPEG, GIF, and WebP files within them (dimensions read from the file's header) as they are, without decoding them, and refuses larger ones and other formats. With one, it decodes every image, turns it upright by its EXIF orientation, converts BMP to PNG, and shrinks it to fit. The Photon-based processor runs on WebAssembly and is only loaded when you import it:155
```typescript156
import { createNodePhotonImages } from "@earendil-works/pi-durable/images/node"; // or /images/cloudflare, or createPhotonImages(wasm) from /images157
import { createCodingTools } from "@earendil-works/pi-durable/tools";159
registry.install(createCodingTools({ images: await createNodePhotonImages() }));160
```162
A model without image input sees a placeholder instead of the image, and `read` says so in a diagnostic. `/images/node` prepares images in a worker thread, so a large photo (a second or two of work) never blocks the event loop; the worker exits after 30 s without images, freeing Photon's WebAssembly memory (`createNodePhotonImages({ worker: false, idleMs })`). Elsewhere, `createPhotonImages()` and `/images/cloudflare` work on the calling thread, and decoding takes the image's full size in WebAssembly memory, which does not shrink again; leave the processor out on memory-constrained hosts such as Cloudflare Workers unless images stay small.164
Define your own tool with a TypeBox schema. `defineTool()` types `args` from `parameters`, which the Harness validates before `execute()`. `api.output()` streams running output, which becomes the result when `execute()` returns no `output`:166
```typescript167
import { Type } from "@earendil-works/pi-ai";169
const count = defineTool({170
name: "count",171
description: "Count from 1 to n",172
parameters: Type.Object({ n: Type.Number() }),173
execute: async (args, api) => {174
for (let i = 1; i <= args.n; i++) api.output(`${i}\n`);175
return {};176
},177
});178
registry.install(defineExtension({ name: "count", tools: [count] }));179
```181
Each call runs as its own durable task. Its intent is committed before `execute()` runs. If the process dies mid-call, the tool reruns on reopen only when it is declared `replay: "safe"`; otherwise the model gets an `interrupted` error result with the output committed so far. Throwing from `execute()` gives the model an error result. A result can also return `usage`, which is added to the conversation's [usage](#usage-and-cost). It can also return `control: { terminate: true }`: when every result of the round asks for it, the run ends without another model request.183
A result has three channels: `output` for the model, `structuredOutput` for programs that call the tool (see [Calling tools from tools](#calling-tools-from-tools)), and `details` for UIs. A tool that declares `structuredOutputSchema` returns a matching `structuredOutput`, which the Harness validates. Programs calling a tool without one get its bounded output: one text item as a string, one image as its `ImageContent`, nothing as `""`, and anything else as the content list, for errors too. `structuredOutput` is always the tool's own value: a schema tool's error result keeps the value it returned, and the text of one without is in a `tool_error` diagnostic, as a thrown error's message is. The transcript stores only `output` and `details`:185
```typescript186
const lines = defineTool({187
name: "lines",188
description: "Count the lines of a file",189
parameters: Type.Object({ path: Type.String() }),190
structuredOutputSchema: Type.Object({ lines: Type.Number() }),191
execute: async (args, api, context) => {192
const text = await readText(api, args.path, context);193
const count = text.split("\n").length;194
return { output: [{ type: "text", text: `${count} lines` }], structuredOutput: { lines: count } };195
},196
});197
```199
A missing, undeclared, or invalid `structuredOutput` turns a nested call's result into an error result with an `invalid_structured_output` diagnostic; for a call the model made, which never sees it, it is dropped and reported through `onReport`. An error result may omit it, and programs then find its text in a `tool_error` diagnostic; error results the Harness writes itself (blocked, invalid, interrupted, aborted) carry none, and their `diagnostics` say what went wrong. An `afterTool` hook that redacts a schema tool's `output` must redact its `structuredOutput` too. The built-in `bash` returns `{ output, truncated, fullOutputPath?, exitCode }`, where `output` is the retained tail the model sees, read with `api.retainedOutput()`, and answers a nonzero exit with an error result that still carries it.201
A later extension's tool with the same name replaces an earlier one where both are selected, and `wrapTool()` decorates whichever tool won:203
```typescript204
const Venv = defineExtension({ name: "venv", tools: [createBashTool({ commandPrefix: "source .venv/bin/activate" })] });205
const Timing = defineExtension({206
name: "timing",207
wraps: [wrapTool(createBashTool(), (bash) => ({ ...bash, execute: (args, api, ctx) => timed(() => bash.execute(args, api, ctx)) }))],208
});209
```211
### Calling tools from tools213
A tool calls another tool with `api.executeTool()`, as a code mode script or an MCP bridge does. The nested call is its own tool task, owned by the calling one: it is validated, runs the `ToolTask` hooks, keeps its own output limits and replay policy, and returns its result to the caller instead of the transcript:215
```typescript216
const testsPass = defineTool({217
name: "tests_pass",218
description: "Run the test suite and say whether it passed",219
parameters: Type.Object({}),220
execute: async (_args, api, context) => {221
const run = await api.executeTool("bash", { command: "npm test" }, context, { progress: false });222
// Absent when bash did not finish, for example on a timeout.223
const exitCode = (run.structuredOutput as { exitCode: number } | undefined)?.exitCode;224
return { output: [{ type: "text", text: exitCode === 0 ? "passed" : "failed" }] };225
},226
});227
```229
- The caller gets the nested call's `taskId`, `structuredOutput`, `details`, `diagnostics`, `usage`, and `isError`, not the `output` meant for the model. A blocked, invalid, failed, or aborted nested call returns an `isError` result. `executeTool()` itself rejects once `execute()` has returned, when the caller is aborted, or when the Harness closes; cancelling its `context` stops only the wait.230
- Hooks see a nested call's `call.parent`, so a guard can treat calls from scripts differently.231
- While the caller runs, its nested calls show in `docs["pi.live"].nestedTools`, with the arguments they run with and a `summary` (error, duration, usage) once done, and as tool events with `parentToolCallId` and `parentTaskId`. `progress: false` keeps a nested call's running output out of `pi.live`. Before the caller settles, nested calls it left running are aborted, also those an earlier attempt made. The caller's result message does not list its nested calls; their task records keep them.232
- Each nested call has a key, its position among the caller's nested calls unless the caller passes `{ key }`. A replay-safe caller that reruns after a crash and calls in the same order, or passes the same keys, gets the nested calls it already made back, finished or still running, instead of starting them again. A nested call the crash interrupted before its result was committed follows its own `replay` policy, so a replay-safe tool with external effects still needs its own idempotency. A caller that is not replay-safe never reruns, so after a restart its unfinished nested calls are abandoned, with everything they own, before they run again.233
- A nested call's `usage` is counted under its own tool; passing it on in the caller's result counts it twice.234
- Nested results are not kept: they live in the caller's task documents (`NestedCallDoc`, one per nested call, keyed by its key, holding `{ taskId, result? }`) only until the caller settles. The nested call's task record keeps only a small receipt. A stored result is exactly what `executeTool()` returns, without the model's `output`, so an image or a `bash` tail is stored once.235
- Nothing bounds how deeply calls nest; a tool with the default `callers` may even call itself. Restrict `callers` where that matters (below).237
### Who may call a tool239
`callers` on a tool says who may call it: the model, other tools through `executeTool()`, or both, the default. A conversation's `modelTools` narrows which of its tools the model is offered, without taking them away from tools:241
```typescript242
const codemode = defineTool({ name: "codemode", callers: ["model"], ... }); // scripts cannot start scripts243
const search = defineTool({ name: "mcp__github__search", callers: ["tools"], ... }); // never offered to the model245
// The model is offered only codemode; its scripts call read, bash, and the MCP tools.246
await root.configure({ modelTools: [codemode] }, context);247
// The model is offered everything but bash, which scripts may still call.248
await root.configure({ modelTools: { remove: [bash] } }, context);249
```251
`conversation.agent()` resolves both lists: `tools`, offered to the model, and `callable`, which nested calls resolve among. `tools` still decides what is enabled at all; `modelTools` cannot add to it.253
A `deferred: true` tool is not offered to the model until it is loaded, though other tools can call it any time. The `tool_search` tool from `@earendil-works/pi-durable/tool-search` (`createToolSearchExtension()`) lets the model search the deferred tools and loads the matches with `control.addTools`, which records them in `modelTools`; loaded tools stay offered on that branch, also after a compaction, and the `codemode` section leaves deferred tools out. Use it for large tool sets, such as an MCP server's.255
### Tasks a restart abandons257
A task that its creator awaits only in memory, and that the creator never resumes after a restart, can be created with `abandonOnRestart: true`. When a later Harness starts scheduling and finds it still live, it is aborted with `abortReason: "restart"`, together with everything it owns, before any of it runs again. Nested calls of tools that are not replay-safe get this automatically, and so do the child tasks such a tool creates with `api.createTask()`. A task abandoned this way whose definition is missing waits until its extension is installed, so its abort handler still cleans up. Its caller, and the conversation, wait with it; `harness.inspect()` shows it as blocked. Aborting it then orphans it, as any abort does.259
## Code Mode261
`@earendil-works/pi-durable/codemode` adds a `codemode` tool: the model writes JavaScript that calls the conversation's tools as `await tools.<name>(args)`, in a QuickJS sandbox (`@earendil-works/pi-codemode`). Each call is a nested tool call, and only the script's output reaches the model. The script survives a restart: it resumes where it was, without running its finished calls again.263
```typescript264
import { CodemodeSandbox } from "@earendil-works/pi-codemode";265
import { createCodemodeExtension } from "@earendil-works/pi-durable/codemode";267
registry.install(createCodemodeExtension({ sandbox: new CodemodeSandbox({ memoryLimitBytes: 256 * 1024 * 1024 }) }));268
```270
The extension also adds a `codemode` prompt section listing the tools scripts can call, as TypeScript declarations within a token budget; scripts find the others with `searchTools()`, `describeTool()`, and `describeNamespace()`. Group tools by name prefix with `namespaces: [{ name, description, prefix }]`, for example an MCP server's `mcp__github__`. The result reads like the coding agent's: a header, the script's output, and on failure the calls already made; text past `max_output_tokens` is cut, with the full text in a temp file. Scripts also get `models.*`: the model catalog, `models.classify()`, and `models.generateImages()`, which run as nested calls of internal tools (`models: false` turns them off). On Cloudflare, pass a `RemoteCodemodeSandbox` instead. Closing the sandbox while a script runs (for example when shutting down) is not an outcome: the script resumes with the next process's sandbox. A failed nested call rejects in the script with its error text, unless the tool declares a schema and returned its own value with the error: `tools.bash` resolves with `{ output, exitCode, ... }` also for a nonzero exit, so scripts check `exitCode`. `store(key, value)` and `load(key)` keep JSON values per conversation; a fork sees them as they were at its entry.272
How it survives a restart: the sandbox runs the script deterministically (a virtual clock, seeded randomness, UTC), and the tool writes each result the script gets to a journal before handing it over. After a restart, the tool runs the script again and hands it the journaled results in the same order, so it makes the same calls with the same arguments; these reattach to their nested calls by key, and then the script continues. A rerun ends the call with an error instead when the script takes another path than before, when the sandbox's fingerprint or the code mode version changed, or when its `timeout_ms` passed meanwhile. A nested call running at the restart follows its own replay policy.274
## System Prompt276
The system prompt is built from the selected extensions' sections, rendered in order before each request. A section sees the resolved agent, the environment built for the request, and committed documents:278
```typescript279
section("cwd", (input) => input.env?.cwd); // rendered as <cwd>\n...\n</cwd>; undefined omits it280
```282
A conversation's `instructions` render last, as the section `instructions`. Sections and tool changes are stored as positional system entries in the transcript. Only what changed is sent again, which keeps provider prompt caches warm. A section that returns something different every time, such as the current time, defeats that.284
## Per-Conversation Agent286
Each conversation stores what it runs with in its `pi.agent` document. `configure()` changes it in one commit; unset fields follow the host:288
```typescript289
await root.configure(290
{291
model: { provider: "openai", modelId: "gpt-6-sol" },292
thinkingLevel: "high",293
extensions: { remove: [Coding] }, // edits the host default; an array selects exactly these, in order294
tools: [readTool, bashTool], // an array enables exactly these; { remove: [...] } drops some295
modelTools: [readTool], // of those, offer the model only read; tools may still call bash296
instructions: "Only read; never edit files.",297
cwd: "/work/repo",298
},299
context,300
);301
await root.configure({ tools: null }, context); // null clears a field back to the host default302
const agent = await root.agent(context); // resolved: model, extensions, tools, sections, cwd303
```305
Extensions and tools are passed as objects and stored by name, so a stored name outlives its code: after an extension is uninstalled, conversations that select it just stop getting it until it is installed again. `createConversation()`, `fork()`, and `root()` take the same change as `agent`. A task-owned conversation, such as a subagent's, starts as a copy of its owner's conversation's agent. A fork starts with the agent its parent had at the fork entry. The model, prompt, and offered tools of a request are fixed when it is prepared; a change applies from the next request. Tool calls and hooks use the agent as their task phase resolves it, and the environment is built from the current `cwd` at each use, so a `cwd` or extension change can reach calls the model already made.307
## Settings309
Run policy shared by every conversation is passed as `settings`. It is read at every use and never stored, so getters make it live, for example backed by a settings file:311
```typescript312
const harness = await Harness.open(storage, {313
models,314
registry,315
settings: {316
extensions: [CodingTools, Coding], // default selection; absent: every installed extension317
stream: { timeoutMs: 120_000 },318
retry: { maxRetries: 3 },319
compaction: { reserveTokens: 16384 },320
progress: { partialIntervalMs: 100, outputIntervalMs: 100 },321
toolExecution: "parallel",322
contextRetentionMs: 600_000, // idle conversations keep their context in memory this long (checked lazily where timers cannot be unreferenced)323
documentRetentionMs: 600_000, // loaded documents nothing observes stay in memory this long after their last use; observed ones always324
get followUpMode() {325
return userSettings.followUpMode;326
},327
},328
}, context);329
```331
## Environment333
`env` builds the execution environment for each tool call, section rendering, and `runtime.env()`. It receives the conversation's ID, its agent `cwd`, and committed reads, so one function serves a directory per conversation or a container per conversation:335
```typescript336
import { NodeExecutionEnv } from "@earendil-works/pi-durable/env/node";338
const harness = await Harness.open(storage, {339
models,340
registry,341
env: ({ cwd }) => new NodeExecutionEnv({ cwd: cwd ?? process.cwd() }),342
}, context);343
```345
A throw from `env` becomes the call's error result. Without an environment, the built-in tools fail with an error result. A fresh environment object per call is fine: `edit` and `write` serialize changes to one file by the environment's `id` and path. A custom `ExecutionEnv` sets `id` so that equal ids see the same files at the same paths, for example one id per container.347
Hosts can use the environment directly too, for example to show a project's files. `openBinaryReader()` reads byte ranges of one opened file, `openDirReader()` pages a directory, and `exec()` with an argv array runs a program without a shell, reporting which stream each output chunk came from:349
```typescript350
const status = { stdout: "", stderr: "" };351
await env.exec(["git", "status", "--porcelain=v2", "-z"], {352
onOutput: (text, _context, { stream }) => {353
status[stream] += text;354
},355
}, context);356
```358
Abort the context to stop one call; `cleanup()` is for shutting the environment down. A custom environment can check itself with `registerEnvConformance()` from `@earendil-works/pi-durable/testing`, like storage below.360
Some environments also implement the optional `listFiles()`: the files and directories below a directory at any depth, as `fd` lists them, skipping what git ignores and stopping at bounds with `truncated`. `NodeExecutionEnv` does not; pi-env's `RemoteExecutionEnv` and `LocalExecutionEnv` do. Callers fall back to their own walk when the method is missing or fails with `not_supported`:362
```typescript363
const listing = await env.listFiles?.(".", { exclude: ["node_modules"], maxEntries: 50_000 }, context);364
```366
## Reload368
Installing an extension with an installed name replaces it in place, in one step:370
```typescript371
registry.install(await loadCodingExtension()); // same name "coding": replaces the installed one372
```374
`registry.uninstall(extension)` removes the installed extension with that name, whichever object it is.376
Work that already started keeps the code it took: a running tool call finishes under its old implementation, and each task phase resolves hooks and the agent once, from the registry at the phase's start. The next phase, request, or call uses the new code. After a restart, install the same extensions again; pending tasks of an extension's `tasks` resume once it is installed.378
## Watching a Conversation380
Everything a UI needs is committed state. `viewState()` returns the conversation's structural view as a read-only Chord state, updated after every commit that touches it:382
```typescript383
const view = await root.viewState(context);384
view.subscribe((value) => {385
// value.entries: the active transcript386
// value.docs["pi.live"]: the running generation (streamed partial, retry, deferred) and tool calls (output, details)387
// value.docs["pi.inbox"], value.docs["pi.usage"], value.docs["pi.agent"], value.docs["pi.provider"]388
render(value);389
});390
// later: view.dispose();391
```393
`watch()` delivers the same view with the exact Chord operations of each commit, one callback at a time:395
```typescript396
const watch = await root.watch(context);397
render(watch.value); // the state at attachment398
watch.start(async (value, ops) => {399
await send(ops); // for example to a remote client that applies them400
});401
// later: await watch.stop();402
```404
A slow watch keeps at most 100 undelivered frames. After that, the pending frames are replaced by one frame holding the whole newest view. A client that joins late or reconnects starts from the current view; nothing is replayed.406
Partial answers and tool output are committed at most every 100 ms by default, so a crash loses at most that window. `settings.progress` changes the intervals; a host whose storage is remote can commit less often, for example `{ partialIntervalMs: 500, outputIntervalMs: 500 }`.408
## Busy Conversations410
A conversation is busy while a run is working on an input. Submitting to a busy conversation queues the submission in the conversation's inbox, `docs["pi.inbox"]` in the view:412
```typescript413
await root.submit({ type: "input", content: "Also run the tests" }, context); // follow-up (default)414
await root.submit({ type: "input", content: "Use pnpm, not npm", whenBusy: "steer" }, context);415
await root.submit({ type: "input", content: "Only if idle", whenBusy: "reject" }, context); // throws ConversationBusy416
await root.submit({ type: "write", entry: { kind: "app.note", data: "user opened a file" } }, context);417
```419
- **Steers** are placed after the current tool round and join the running work.420
- **Follow-ups** are placed when the run answers, and start the next run.421
- **Writes** append an entry without asking the model anything. A write of only `kind` and `data` changes nothing the model sees, so it is appended at once, even mid-run, and is never queued: a chat message shows in the transcript as soon as it is sent. It can therefore appear before items submitted earlier that are still queued, except behind a queued reset, where it waits so the reset does not hide it. A write with `model`, `head`, or `edits` waits for the next boundary like a steer.422
- `await submission.abort(context)` withdraws a queued submission.423
- The [settings](#settings) `steeringMode: "all"` and `followUpMode: "all"` place every queued item at once instead of one per turn.425
If a run fails, queued items stay in the inbox until the next input or queued write places them, oldest first.427
An input is placed as a `pi.user` entry. To show it as something else, give it an `entry` with your kind and data; the model still sees `content` as the user message:429
```typescript430
await root.submit(431
{432
type: "input",433
content: "ann: can you check the build?",434
entry: { kind: "chat.mention", data: { author: "ann", text: "can you check the build?" } },435
},436
context,437
);438
// placed as { kind: "chat.mention", data: { author: "ann", ... }, model: [{ role: "user", content: "ann: can you check the build?" }] }439
```441
It behaves like any other input: it starts a run, steers, follows up, or rejects, and is answered. While queued, its inbox item carries the entry. The kind must not start with `pi.`.443
## Reset and Handoff445
`reset()` starts a new context. The model no longer sees older entries, but they stay in storage:447
```typescript448
await root.reset(undefined, context); // start from nothing449
await root.reset("We were fixing the flaky login test. Continue.", context); // start from a handoff note450
```452
While busy, the reset is queued like a write. When it is placed during a tool round, the current run ends. A tool can request the same with `control: { handoff: "..." }`.454
## Compaction456
Compaction shrinks what the model sees: it summarizes older entries and appends a `pi.compaction` entry that holds the summary and heads the first entry it keeps. Older entries stay in storage.458
```typescript459
const id = await root.compact("Keep the failing test names", context); // manual, with optional instructions460
const { outcome } = (await harness.waitForTask(id, context)).state;461
if (outcome.status === "completed" && outcome.result.submissionId !== undefined) {462
const placed = await (await harness.submission(outcome.result.submissionId, context))!.wait(context);463
console.log(placed.status); // "done", or "unanswered" with reason "stale"464
}465
```467
The conversation keeps working while the summary is made. The summary is placed at once when the conversation is idle, otherwise at the next turn boundary. Esc (`abort()`) cancels a manual compaction.469
Generation also compacts on its own, controlled by the [settings](#settings):471
```typescript472
settings: {473
compaction: {474
enabled: true, // automatic compaction; manual compact() always works475
reserveTokens: 16384, // above contextWindow - reserveTokens, the next request waits for a compaction476
keepRecentTokens: 20000, // roughly how much recent context stays verbatim477
backgroundTokens: 32768, // this far below that, a compaction starts in the background; 0 disables it478
},479
}480
```482
When a provider rejects a request because the context is too long, generation compacts and retries once. A summary that would cut before the start of the current context settles as `stale` when it is placed, so when several are in flight, the furthest cut stays in effect. Summarization spend counts in `pi.usage`. A `beforeCompact` hook on `CompactionTask` can decline or supply its own summary.484
When a compaction that a generation waits for at the blocking threshold fails, the generation fails too, without sending a request: its inputs settle `unanswered` with `model_error` and detail `Compaction failed: <error>`. One that finds nothing to compact or is declined lets the request go ahead.486
Running compactions are listed in `docs["pi.live"].compactions` with their reason, attempt, and retry backoff. The newest failure stays in `docs["pi.live"].compactionFailure` (task, reason, error) until a compaction produces a summary. The agent events add `compaction_start` and `compaction_end` (with `error` when the compaction failed), and `compactions` and `compactionFailure` fields in the snapshot.488
## Agent Events (Experimental)490
For consumers that want coding-agent style events (`message_start`, `message_update`, `tool_execution_start`, ...) instead of structural state:492
```typescript493
import { watchEvents } from "@earendil-works/pi-durable";495
const stream = await watchEvents(harness, root.id, context);496
initialize(stream.snapshot); // entries, run, in-flight generation, tools, compactions, inbox, agent, usage497
stream.start(async (events) => {498
for (const event of events) console.log(JSON.stringify(event));499
});500
```502
Events are derived from commits, one batch per commit, and apply on top of the snapshot. Message and tool updates carry deltas: text and thinking appends, appended tool-call argument text, and output trims and appends. When a consumer falls more than 100 batches behind, it receives a fresh `snapshot` event instead. See `test/examples/19-json.ts` for the full stream of one run.504
## Hooks506
Hooks let extensions observe or adjust the built-in tasks, in the conversations that select them:508
```typescript509
import { GenerationTask, hook, ToolTask } from "@earendil-works/pi-durable";511
const Guard = defineExtension({512
name: "guard",513
hooks: [514
hook(ToolTask, { beforeTool: (call) => (call.name === "bash" ? { block: "bash is disabled here" } : undefined) }),515
hook(GenerationTask, { onYield: (answer) => (needsMoreWork(answer) ? { continue: "Keep going." } : undefined) }),516
],517
});518
```520
- **Generation:** `beforeRequest` (replace the messages of one request), `afterResponse`, `onYield` (continue the run with another user message), and `afterTools` (runs once a round's tools are done).521
- **Tools:** `beforeTool` (block or rewrite arguments) and `afterTool` (replace the result).523
To limit a hook to some conversations, select its extension only there, for example with `configure({ extensions: { add: [Guard] } })`.525
## More Conversations and Forks527
```typescript528
const other = await harness.createConversation({ ownership: { kind: "ownerless" } }, context);529
const fork = await root.fork(entryId, { ownership: { kind: "ownerless" } }, context);530
```532
A fork sees its parent's entries up to `entryId` and continues independently. It keeps the parent's agent as of that entry but receives a fresh provider session identity. Both take `agent` and `init`, applied in the creating commit.534
## Abort and Subagents536
`await root.abort(context)` stops a conversation: queued inputs are withdrawn (queued writes stay), every task of its current work is aborted, and the call resolves once the conversation is idle.538
A conversation can be **owned** by a task. A subagent tool creates its child inside `api.commit()` with `ownership: { kind: "task", taskId: api.taskId }`, then drives it through `api.conversation(id)`:540
```typescript541
const Subagent: Extension = defineExtension({542
name: "subagent",543
tools: [544
defineTool({545
name: "subagent",546
description: "Delegate a self-contained task to a subagent and get its answer back.",547
parameters: Type.Object({ task: Type.String() }),548
replay: "safe", // a rerun after a crash finds the same child and submission549
execute: async (args, api, context) => {550
const child = await api.commit(async (tx) => {551
// The ownership index remembers the child, so a rerun reuses it.552
const existing = (await tx.scanConversations({ ownerTaskId: api.taskId }, 1)).items[0];553
if (existing !== undefined) return existing.id;554
// Starts as a copy of this conversation's agent: model, extensions, tools, cwd.555
const created = await tx.createConversation({ ownership: { kind: "task", taskId: api.taskId } });556
// A cheaper model, and no subagents of its own.557
await configure(tx, created.id, { model: haiku, extensions: { remove: [Subagent] } });558
return created.id;559
}, context);560
await api.details({ conversationId: child }, context); // lets a UI attach to the child561
const request = { type: "input", content: args.task, requestId: `subagent:${api.taskId}` } as const;562
const settled = await (await (await api.conversation(child, context))!.submit(request, context)).wait(context);563
return { output: [{ type: "text", text: settled.status }] };564
},565
}),566
],567
});568
```570
Owned work belongs to its owner:572
- Aborting the call aborts the child. So does the call failing: `execute()` throwing, or a crash that interrupts a call that is not replay-safe.573
- The parent is idle only once the child is.574
- A task created with `{ background: true }` is a boundary: work it owns survives the parent's abort and does not keep the parent busy. `root.abort(context, { background: true })` aborts it too.576
The examples show both patterns as product code:578
- [`22-subagent-foreground.ts`](test/examples/22-subagent-foreground.ts): the tool above, returning the child's answer. The UI finds the child through the call's `details` and prints the child's events indented under the call.579
- [`23-subagent-background.ts`](test/examples/23-subagent-background.ts): persistent subagents behind one `subagent` tool that spawns, messages (steer or follow-up), waits for, stops, and lists them. Each child is owned by a background anchor task, so the parent's Esc and idle waits never reach it. Each message is delivered by a background reporter task that posts the answer back to the parent as a follow-up input once it arrives; request IDs keep a restart from sending a message or a report twice.581
## Child Tasks583
A task can own child tasks, created with `ownership: { kind: "task", taskId }`, and wait for them by committing a `waiting` state:585
```typescript586
pay: async (task, runtime, context) => {587
await runtime.commit(async (tx) => {588
const payments = [];589
for (const card of task.input.cards) {590
payments.push(await tx.createTask(Payment, { card }, { ownership: { kind: "task", taskId: task.id } }));591
}592
// Resume in `decide` once every payment is done; the first failure aborts the rest.593
return { status: "waiting", checkpoint: { phase: "decide", payments }, on: payments, policy: "failFast" };594
}, context);595
},596
decide: async (task, runtime, context) => {597
const outcomes = await runtime.outcomes(task.state.checkpoint.payments, context);598
// ...commit the checkout's own outcome599
},600
```602
- **Waiting:** the task runs no code while it waits. With `allSettled` it resumes once every task in `on` is done; with `failFast` the first failed child also aborts the others. `on` may name other tasks too, with `allSettled`.603
- **Finishing:** a task that finishes while work it owns is still running is `completing`: its outcome is decided, but it becomes terminal, and `waitForTask()` returns, only once that work is done. A failed or aborted outcome aborts that work first.604
- **Aborting:** abort runs bottom-up. Aborting a task aborts the work it owns first, and its own abort handler starts only once that work is done, so each task undoes its own effects.606
[`24-child-tasks.ts`](test/examples/24-child-tasks.ts) runs a checkout with four payments: a declined card, a cancelled checkout, and a restart while the payments run.608
## Task Graph610
`harness.taskGraph(context)` shows every live task of the Session as one Chord state, for a task panel or debugging. Each node has its owner edge (`owner` task, or none for a task its conversation owns), its status, whether it is `background` or abort-marked, and the conversations it owns. `harness.watchTaskGraph(context)` delivers the same value as a watch, like a conversation's `watch()`.612
```typescript613
const graph = await harness.taskGraph(context);614
graph.subscribe((value) => {615
for (const node of Object.values(value.tasks)) {616
const status = node.state.status === "waiting" ? `waiting on ${node.state.on.join(", ")}` : node.state.status;617
console.log(`${node.id} ${node.kind} ${status}`, node.owner ?? `conversation ${node.conversationId}`);618
}619
});620
```622
A task appears with the commit that creates it and leaves with the commit that makes it terminal. Statuses are the committed ones: `pending`, `running`, `waiting` (with `on` and `policy`), and `completing` (with the held outcome's status). After a restart, tasks that were `running` show as `pending` until they run again. Whether a pending task is blocked by a missing definition is not part of the graph; `harness.inspect()` reports that. The graph lists live tasks only: once a subagent's owner task is terminal, a later task in its conversation is a top-level node, and the conversation's `ConversationRecord.owner` (also in its view's `conversation`) links it to its parent. [`24-child-tasks.ts`](test/examples/24-child-tasks.ts) prints the checkout's tree while its payments run.624
## Your Own State626
Documents are typed JSON objects committed together with entries. Define one, and edit it in a commit:628
```typescript629
import { defineDoc } from "@earendil-works/pi-durable";631
const Todos = defineDoc<{ items: string[] }>({632
kind: "app.todos",633
version: 1,634
scope: "conversation",635
history: "latest", // or "rewindable" to read old values with snapshotAsOf()636
fork: "initial", // what a fork starts with: "initial", "current", or "asOf"637
initial: () => ({ items: [] }),638
});640
await root.commit(async (tx) => {641
(await tx.doc(Todos, root.id)).items.push("write docs");642
}, context);643
console.log(await harness.snapshot(Todos, root.id, context));644
```646
`harness.watchDoc()` and `harness.documentState()` observe one document like the view above. `HarnessOptions.conversationCreated(tx, conversation)` runs in every commit that creates or forks a conversation, including a tool's raw `tx.createConversation()`, so every conversation gets your documents; `init` in `createConversation()`, `fork()`, and `root()` writes per-call data in the same commit. An extension's tools, sections, and hooks read their own documents through `api` or `input.read`, and treat an absent one as its default ([`11-extension-state.ts`](test/examples/11-extension-state.ts)).648
## Usage and Cost650
Each conversation keeps token and cost totals in `docs["pi.usage"]`: per `provider/model` for model responses, and per tool name for tool results that report usage. Failed and aborted attempts count too. For the whole Session:652
```typescript653
const usage = await harness.usage(context); // { models: { "openai/gpt-6-sol": Usage }, tools: {...} }654
```656
## Storage658
| Backend | Import | Notes |659
|---|---|---|660
| Memory | `MemoryStorage` from the package root | Nothing is persisted. |661
| SQLite | `openNodeSqliteStorage(file)` from `@earendil-works/pi-durable/storage/sqlite/node` | One database file. WAL mode with `synchronous = NORMAL`: commits survive process crashes; the newest may be lost on power or host failure. |662
| JSONL | `openNodeJsonlStorage(directory, context)` from `@earendil-works/pi-durable/storage/jsonl/node` | Append-only files in one directory. Pass `{ fsync: true }` to flush before each commit marker. Keeps the whole history in memory too, so memory grows with it; use SQLite for long-running agents. |663
| Cloudflare Durable Object | `openDurableObjectSqliteStorage(ctx.storage)` from `@earendil-works/pi-durable/storage/sqlite/cloudflare` | The SQLite storage of one SQLite-backed Durable Object; commits are its storage transactions. |665
SQLite storage can namespace its tables and indexes when sharing a database: pass `{ prefix: "pi_" }` to666
`SqliteStorage.open(database, options)`, `openNodeSqliteStorage(path, options)`, or667
`openDurableObjectSqliteStorage(ctx.storage, options)`. Storage renders the prefixed names into its SQL, so adapters668
need no support. A prefix must be a SQLite identifier and cannot begin with `sqlite_`; a host may reserve more, such669
as `_cf_` in Durable Objects. Changing the prefix does not migrate existing data.671
Multiple Pi storages opened through the Cloudflare adapter with the same storage object automatically share an672
operation queue. To share the database with host tables, create an adapter and use it for both Pi and all host queries;673
direct storage calls bypass the queue and may be rolled back with a Pi transaction:675
```typescript676
const database = new DurableObjectSqliteDatabase(ctx.storage);677
const piStorage = await SqliteStorage.open(database, { prefix: "pi_" });678
await database.exec("CREATE TABLE IF NOT EXISTS host_notes (text TEXT)");679
await database.run("INSERT INTO host_notes (text) VALUES (?)", text);680
```682
One process owns a storage at a time; there is no cross-process locking. The portable SQLite and JSONL cores (`/storage/sqlite`, `/storage/jsonl`) run without Node APIs, for example on Bun or in Cloudflare Durable Objects, given an asynchronous `SqliteDatabase` facade or a `FileSystem` from `@earendil-works/pi-durable/env`.684
SQLite adapters implement promise-based `exec`, `run`, `get`, `all`, `transaction`, and `close`. `run`, `get`, and `all` take SQL text plus positional bindings; adapters may cache prepared statements by SQL text. A transaction callback receives a transaction handle; all work in the transaction must use it, and the handle expires when the callback settles. Adapters must queue unrelated operations and other transactions until the transaction finishes, so calling `database` itself inside the callback never settles:686
```typescript687
await database.transaction(async (transaction) => {688
await transaction.exec("CREATE TABLE example (value TEXT)");689
await transaction.run("INSERT INTO example (value) VALUES (?)", "stored atomically");690
});691
```693
Custom backends can run the shared conformance suite with any Vitest- or Jest-compatible runner:695
```typescript696
import { registerStorageConformance } from "@earendil-works/pi-durable/testing";697
import { describe, expect, it } from "vitest";699
registerStorageConformance({ describe, expect, it }, "My Storage", async (use) => {700
const storage = await openMyStorage();701
try {702
await use(storage);703
} finally {704
await closeMyStorage(storage);705
}706
});707
```709
The package root loads TypeBox, because the tool task validates arguments with pi-ai's `validateToolArguments()`. That costs about 23 MB of peak RSS unbundled, about 4 MB in a tree-shaken bundle.711
## Errors713
Three rules decide what an error does:715
1. **A storage error ends the Harness.** The call that hit it rejects with the error. Everything after it, and every pending wait, rejects with `SessionFailed` (its `cause` is that error); watches and event streams end with `session_failed`; running tasks get their abort signal; `onReport` gets the error once. The Harness then closes itself. Nothing is retried; a custom storage retries its own transient failures. Reopen to recover: commits are atomic, so the store is consistent, and resubmitting with the same `requestId` finds a submission that was already admitted.716
2. **An invalid request fails only that call:** a storage throws `StorageRequestError` for a bad cursor, an unknown conversation, or history a document does not keep. A read you cancelled fails only itself too.717
3. **Your code fails only its own unit:** a throwing task phase faults that task, a throwing tool gets an error result, a throwing hook is reported and skipped (`beforeTool` blocks the call), and a throwing commit callback rolls back that commit. Listeners that throw, or return a rejected promise, are reported and the others still run. The exception is a host `RegistryReader` whose `snapshot()` throws: the Harness cannot schedule without it, so it fails the Harness like a storage error.719
`harness.closed` settles once the Harness has closed, by `close()` or after a storage error, so a host restarts on failure like this:721
```typescript722
harness.closed.then((end) => {723
if (end.reason === "failed") reopen(end.error);724
});725
```727
A failure closes the Harness by itself; `closed` settles once that close is done, even if the storage could not close cleanly. Closing waits for running task code to return, so code that ignores its abort signal delays `closed`.729
## Examples731
Runnable examples live in [`test/examples`](test/examples). Run one from this package directory with:733
```bash734
node --conditions=source --experimental-strip-types test/examples/14-chat.ts735
```737
| Example | Shows |738
|---|---|739
| [14-chat](test/examples/14-chat.ts) | One question and answer |740
| [16-real-model](test/examples/16-real-model.ts) | Streaming an answer from OpenAI |741
| [17-coding-tools](test/examples/17-coding-tools.ts) | A tool-using turn on JSONL storage |742
| [18-print](test/examples/18-print.ts) | Print mode: submit a prompt, print the answer |743
| [19-json](test/examples/19-json.ts) | JSON mode: agent events or raw view operations, on SQLite, JSONL, or memory |744
| [20-inbox](test/examples/20-inbox.ts) | Steers, follow-ups, writes, and withdrawal while busy |745
| [21-late-join](test/examples/21-late-join.ts) | Attaching a view and an event stream mid-run |746
| [22-subagent-foreground](test/examples/22-subagent-foreground.ts) | A replay-safe subagent tool whose child the call owns, with the child's events under the call |747
| [23-subagent-background](test/examples/23-subagent-background.ts) | Persistent subagents: spawn, steer, stop, list, answers reported back, restart-safe |748
| [24-child-tasks](test/examples/24-child-tasks.ts) | A checkout that owns and waits for four payments: failFast, abort, restart |749
| [25-compaction](test/examples/25-compaction.ts) | A long chat compacted in the background, manually, and after a context overflow |750
| [26-coding-agent](test/examples/26-coding-agent.ts) | CodingTools, live settings from a settings object, an environment that follows the conversation's directory |751
| [27-plan-mode](test/examples/27-plan-mode.ts) | A read-only plan mode as an extension with its own document, switched with `configure()` |752
| [28-reviewer](test/examples/28-reviewer.ts) | A reviewer conversation with its own model, extensions, tools, directory, and review loop |753
| [29-sandbox-per-conversation](test/examples/29-sandbox-per-conversation.ts) | An environment per conversation, looked up from an app document |754
| [30-tool-override](test/examples/30-tool-override.ts) | A same-name bash for some conversations, and a wrapper that times whichever bash won |755
| [31-reload-and-restart](test/examples/31-reload-and-restart.ts) | Reloading an extension mid-call, and stored choices surviving a restart |756
| [00](test/examples/00-conversation.ts)–[13](test/examples/13-recovery.ts) | The layers underneath: sessions, documents, forks, watches, the Harness, agent configuration, reload, extension state, tasks, recovery |758
Examples that call OpenAI need `OPENAI_API_KEY`; most use the faux provider otherwise.760
## Design Documents762
- [`docs/spec.md`](https://github.com/earendil-works/pi/blob/main/packages/durable/docs/spec.md): the normative specification764
Benchmarks: `npm run bench:storage`, `npm run bench:storage:memory`, `npm run bench:tool-output`, and `npm run bench:nested-tools`.766
## License768
MIT