A library for building agents that survive their process.
Armin Ronacher · Earendil
What it is
TypeScript. Runs wherever JavaScript runs: Node, Bun, Cloudflare Durable Objects.
Every model request and tool call is a durable task with a checkpoint. A scheduler runs them.
One person in a terminal, or a whole Slack channel steering the same agent.
Kill the process at any moment; a new one continues. Your state lives next to the transcript.
Why
Pi in your terminal
You notice. You type “continue”. You are the recovery mechanism.
An agent in Slack
Nobody notices. The answer is lost, the deploy may have happened, and people ask twice.
If the human isn’t the recovery mechanism, the harness has to be.
The one rule
One line of atomic commitsTranscript entries, tasks, and state change together, or not at all.
Streaming is committed tooTokens and tool output land in storage at most every 100 ms.
So recovery is reopeningA crash loses at most 100 ms. The screen is a window onto storage, not onto the process.
Code
const registry = createRegistry();
registry.install(CodingTools); // read, write, edit, bash
const harness = await Harness.open(
await openNodeSqliteStorage("./agent.sqlite"),
{ models, registry, env },
context,
);
const root = await harness.root(context, {
agent: { model: { provider: "openai", modelId: "gpt-6-sol" } },
});
const job = { type: "input", content: "Fix the flaky login test",
requestId: "job-42" } as const;
const submission = await root.submit(job, context);
const settled = await submission.wait(context);
The registry holds the tools this process can run: here read, write, edit and bash.
A harness opens over storage. SQLite here; JSONL, memory or a Durable Object work the same.
The root conversation is created once and is the same after every restart.
submit() durably admits the input. A requestId makes it exactly-once.
wait() resolves with the answer, even across a crash.
Under the hood
What the user sees
What storage holds
Every change on this side is a commit. The chat on the left is drawn from it.
Durability
running the job
kill -9The process is gone. Every row is still in storage, including bash halfway through and the queued steer.
opens the same storage
// a new process opens the same file
const harness = await Harness.open(
await openNodeSqliteStorage("./agent.sqlite"),
{ models, registry, env },
context,
);
harness.resume(); // continue pending tasks
const root = await harness.root(context);
// a client retrying after the crash:
await root.submit(job, context); // → S1 again
No recovery code. Reopening moved one row from running to pending; the rest is the ordinary machinery.
Durability
const searchIssues = defineTool({
name: "search_issues",
description: "Search the issue tracker",
parameters: Type.Object({ query: Type.String() }),
replay: "safe", // only reads: rerun after a crash
execute: async (args, api) => {
api.output(`searching for ${args.query}\n`);
const text = await tracker.search(args.query);
return { output: [{ type: "text", text }] };
},
});
const deploy = defineTool({
name: "deploy",
description: "Deploy a version to production",
parameters: Type.Object({ version: Type.String() }),
// no replay: interrupted means the model is told
execute: async (args) => {
const text = await ci.deploy(args.version);
return { output: [{ type: "text", text }] };
},
});
replay: "safe" runs again with the stored arguments.
Otherwise the model is told “interrupted”, with the output so far. Never repeated.
api.output() streams to every client, committed every 100 ms.
Scheduling
type State = { phase: "wait" } | { phase: "deliver" };
const Reminder = defineTask<{ text: string; at: number }, State, null>({
name: "app.reminder",
version: 1,
initial: () => ({ phase: "wait" }),
phases: {
wait: async (task, runtime, ctx) => {
await runtime.sleep(task.input.at, ctx); // wall-clock deadline
await runtime.commit(() => ({
status: "running", checkpoint: { phase: "deliver" } }), ctx);
},
deliver: async (task, runtime, ctx) => {
const chat = await runtime.conversation(task.conversationId, ctx);
await chat!.submit({ type: "input", content: task.input.text,
requestId: `reminder:${task.id}` }, ctx);
await runtime.commit(() => ({ status: "terminal",
outcome: { status: "completed", result: null } }), ctx);
},
},
abort: (_task, runtime, ctx) => runtime.commit(() => ({
status: "terminal", outcome: { status: "aborted" } }), ctx),
});
Mon 18:02“Remind me tomorrow at 9.” A tool creates the task.
Mon 18:02wait sleeping until Tue 09:00
Mon 23:40Redeployed. The task is still waiting.
Tue 03:12The laptop slept. Still waiting.
Tue 09:00deliver: the reminder arrives in the conversation, once.
Model requests, tool calls and compaction are tasks like this one, in one ownership tree: Esc aborts what a task owns, bottom-up.
Single-player and multiplayer
// any client, any time: the current view, then changes
const view = await thread.viewState(context);
render(view.value);
view.subscribe((value) => render(value));
// anyone can steer the running conversation
await thread.submit({
type: "input",
content: "Check the staging logs first",
whenBusy: "steer",
}, context);
// remote clients: the exact operations of every commit
const watch = await thread.watch(context);
watch.start(async (_value, ops) =>
socket.send(JSON.stringify(ops)));
Tracking state
const Todos = defineDoc<{ items: string[] }>({
kind: "app.todos",
version: 1,
scope: "conversation",
history: "rewindable",
fork: "asOf", // a fork sees the todos as of its fork point
initial: () => ({ items: [] }),
});
// in a tool: the result and the change, one atomic commit
await api.commit(async (tx) => {
const list = await tx.doc(Todos, api.conversationId);
list.items.push(args.item);
}, ctx);
const todos = await harness.documentState(Todos, root.id, context);
todos?.subscribe((value) => renderTodos(value?.items ?? []));
What you get
recovery
Reopen the storage; every task continues from its checkpoint.
recovery
A retry with the same requestId returns the same submission.
honesty
Safe tools rerun; everything else is reported as interrupted.
scheduling
Timers, retries and child tasks survive restarts.
scheduling
Abort flows down the ownership tree; cleanup runs bottom-up.
multiplayer
Late join, steering, forks, all from committed state.
state
Documents commit with the transcript; forks see the right version.
malleable
Conversations store names, not code: reload and restart work the same.
anywhere
SQLite, JSONL, memory, Durable Objects; tools on any machine.