Durable by default

Commit first, then act. How a reply survives a closed tab, a restart, and a crash in the middle of a sentence.

intermediate9 min read

The reply stops halfway through a sentence. Maybe you closed the tab. Maybe the process on the host died while the model was sending text. The screen can go quiet either way, but only one of those events leaves the next process with work to recover.

In this chapter

  • Locate the point at which Pipal can remember your message across a restart.
  • Predict what happens to an in-flight model request, a running tool, and a parked approval.
  • Read the evidence from six tests that kill the process at a known point.

The receipt comes after the commit

Before returning HTTP 202 from POST /api/v1/conversations/:id/messages, Pipal goes through admissionPipal’s acceptance of an input in the commit that saves the message, its work, and its run deadline.. This is the moment a commitAn all-or-nothing write that can save transcript entries, document changes, and work together. makes the message and the work to answer it durable together. Pi records the input and the generation task. Pipal puts the run’s deadline in pipal.chat in the same write. If the host goes away after that commit, the next process has both the input and the records it needs to decide what to do. index.ts app.ts

Suppose you send the message, but the connection drops before you see the receiptThe response that identifies an accepted submission and the request ID used to send it., the response naming the accepted submission. The client can retry with the same request IDA value the client sends again when retrying the same input so the server can recognize it.. Pipal looks up the original submission in that conversation. An identical input gets the original receipt, even after restart. Reuse the ID with different content and Pipal returns HTTP 409. index.ts

The core runtime follows the admission queue and explains how Pipal budgets time and capacity for a run.

What stopped determines what needs to restart

Three ways a reply can disappear from view
FeatureClose tab Clean stop Process crash
Committed historyStays in SQLite.Stays in SQLite.Stays in SQLite.
Model request in flightKeeps running on the host.Can be sent again on restart.Counts as a failed attempt. An ordinary request can be sent again if the deadline and retry budget allow.
Tool parked for approvalKeeps waiting for a decision.The wait can resume if the location is still enabled.The wait can resume if the location is still enabled.
Browser watchEnds with the tab.Ends when the host shuts down.Connection drops.

Choose up to two columns

Choose up to two columns

FeatureClose tab Clean stop Process crash
Committed historyStays in SQLite.Stays in SQLite.Stays in SQLite.
Model request in flightKeeps running on the host.Can be sent again on restart.Counts as a failed attempt. An ordinary request can be sent again if the deadline and retry budget allow.
Tool parked for approvalKeeps waiting for a decision.The wait can resume if the location is still enabled.The wait can resume if the location is still enabled.
Browser watchEnds with the tab.Ends when the host shuts down.Connection drops.
Start with an accepted message. Read across a row to see how the same piece of work fares under three kinds of interruption.

Close the tab. Your tab has a watchA subscription that starts with the current view of a conversation or document and receives changes after that. on the conversation, which is how it sees new text and tool updates. Disconnecting closes that watch. Reopening the chat starts from a fresh snapshot. The runThe sequence of turns that takes an input through to its final answer or another recorded outcome. belongs to the host, so it can finish while the tab is gone. The HTTP test cancels an active stream, opens another, and checks that the same chat ends with a single answer. app.ts app.test.ts

Stop the process cleanly. Ctrl+C sends SIGINT, and a service manager can send SIGTERM. Pipal handles both by closing the HTTP server and then closing the harness. When the next process opens the store and resumes pending work, Pi can send an unfinished model request again without charging it as a crash attempt. main.ts README.md

Crash the process. A replacement takes ownership of the same data directory through owner.sqlite. The first process held an exclusive transaction on that file. The OS releases its lock when the process dies, so the next process can open the conversation store without clearing a marker. Leave owner.sqlite in place. It is the open transaction that holds ownership. store-lock.ts

Recovery starts from the last saved state

Four places to lose a process
  • SQLite
  • Pi before
  • Pi after
  • Model
  • Sink
  • Server
  • Tab

Model reply

  1. Pi before → Model · Request

    A model request is in flight

    The user input and generation task were already committed.

  2. Model → Pi before · Partial text

    Text starts to arrive

    A watch can already see some of it.

  3. Pi before → SQLite · Save text

    Some partial text reaches SQLite

    This scenario assumes a progress flush happens before the crash.

  4. Pi before → Pi before · Crash

    The process stops mid-reply

    Any text still waiting for a flush can be lost.

  5. Pi after → SQLite · Save aborted entry

    The next process records the interrupted attempt

    It has the saved partial. The cut-off request counts against the retry budget.

  6. Pi after → Model · Retry

    Pi makes a fresh request after backoff

    This is an ordinary streamed reply, and the run still has time and retries left. The provider may bill both requests.

Replay-safe tool

  1. Pi before → SQLite · Save 41

    The counter in the test parks with 41

    Pi records the checkpoint before the wake can happen.

  2. Pi before → Pi before · Wake

    The counter starts again with its checkpoint

    The recovery test holds that invocation open.

  3. Pi before → Pi before · SIGKILL

    The process dies inside the counter

    The result has yet to be written.

  4. Pi after → Pi after · Replay

    The replay-safe counter receives 41 again

    This invocation completes and returns 42.

  5. Pi after → SQLite · Save result

    The transcript has one result

    The restarted invocation still belongs to the original call.

External write

  1. Pi before → SQLite · Save intent

    Pi records a tool task

    The test delivery tool has no replay-safe declaration.

  2. Pi before → Sink · Send delivery

    The running tool sends the request

    The delivery goes to a separate HTTP service.

  3. Sink → Pi before · HTTP 201

    The service accepts the write

    The test waits for this acknowledgment before killing the host.

  4. Pi before → Pi before · SIGKILL

    The process dies before the tool returns

    The service has the delivery. Pi has the call but no result.

  5. Pi after → SQLite · Record error

    Pi supplies an interrupted result

    The model and the transcript receive a warning that the call may have partially run.

Parked approval

  1. Pi before → SQLite · Save wait

    The call parks for a decision

    Pi keeps the task waiting on a workspace decision signal.

  2. Pi before → Pi before · Crash

    Pipal stops while the call waits

    This scenario assumes the wait has already committed and the process stops before the call wakes.

  3. Pi after → SQLite · Read wait

    The replacement process finds the parked task

    The workspace location is still enabled and available in this example. The request remains available for a decision even if the ordinary run deadline passed while Pipal was down.

  4. Tab → Server · Send decision

    You decide the saved request

    After reconnecting and signing in, you can allow or deny the action.

  5. Server → Pi after · Record decision

    The server sends the decision to core

    Core checks that the task is still waiting on the request you saw, then signals it in a Pi commit.

  6. Pi after → SQLite · Commit decision

    The decision and deadline update commit together

    Pi wakes the call with the saved decision. The action still depends on approval, current grants, and the workspace binding.

SQLite
Pi before
Pi after
Model
Sink
Server
Tab

Pi before → Model

A model request is in flight

The user input and generation task were already committed.

Choose the point where the process stops. The next process starts from whatever was saved at that point.

During a model reply, Pi counts the interrupted request as a failed attempt. Pi can keep saved partial text as an aborted assistant entryOne permanent record in a conversation’s transcript, such as a user message or a tool result., which Pipal marks as failed in the transcript. For an ordinary streamed request, Pi can send a fresh request after backoff if the run’s deadline and retry budget allow. That can incur another model charge. README.md view.ts

If the host stays up and the provider connection drops, what the provider has already sent matters. An early transient failure can get up to two retries. Once the host has seen answer text, hosted search activity or a source citation, a live error ends that generation. The transcript can keep a partial answer or an interrupted search even when no answer text arrived. index.ts generation-retry.test.ts

During a tool call, the question is whether the tool declared replay: 'safe'. ReplayExecuting an interrupted tool invocation again under its saved task identity. means running that interrupted invocation again. A replay-safe tool can run again with its last saved checkpointThe saved information a task can use to pick up its work after waiting or restarting., if it has one, subject to the restart limit. When a call is interrupted without that declaration, Pi records an interrupted error result. A change already accepted by an external service can still stand. A later model turn can ask for the same action as a new call, so the interrupted result alone cannot tell you whether it is safe to try again. README.md recovery-worker.ts

A lookup can consume a service’s quota even if the reply never reaches Pipal. That is why the Exa search and Jina Reader tools both declare replay: 'unsafe'. public-web.ts

While the call is parked for approval, the saved wait can survive a restart. If the owner disabled the chat’s workspace locationThe place a chat uses for project tools, either the Pipal host, an account reached over SSH, or a Docker project. and the process stopped before canceling the run, startup finishes that cancellation. For a run still waiting on you, the deadline check leaves it waiting even if the original deadline passed. When Pipal accepts your decision, it adds the elapsed wait to the deadline in the same commit. This includes time the host was down. Approvals and YOLO follows the saved decision back to the tool. index.ts

Deep diveWhere Pipal sets the retry and restart budgets

These are the settings Pipal passes to Harness.open.

packages/core/src/index.ts
settings: {
extensions: [],
stream: { timeoutMs: Math.min(runTimeoutMs, 90_000), maxRetries: 0 },
retry: {
maxRetries: 2, baseDelayMs: 250, maxAgentDelayMs: 5_000,
preventsReplay(event) {
// …
}
},
restartLimit: 3, toolExecution: 'sequential', contextRetentionMs: 60_000
}

Pipal patches Pi Durable 1.7.0 to remember whether preventsReplay() matched any event in this attempt. A nonempty text delta counts even if the provider later clears the text. A search or citation on the final error counts even if the browser never saw a partial. Once set, the flag also rules out compacting on overflow to reissue the request. Empty opening frames and unfinished local tool arguments alone leave a transient error eligible for retry. A later generation after the tool finishes gets its own decision. pi-durable-1.7.0-generation-retry.patch generation-retry.test.ts

Pipal passes the smaller of the run timeout and 90 seconds as stream.timeoutMs. This is a provider request option, with support depending on the provider. The run deadline is tracked separately. Pipal sets stream.maxRetries: 0 for providers that support retries inside an attempt. It gives Pi Durable up to two generation retries with a base delay of 250 milliseconds and a cap of five seconds on the generation backoff. An interrupted model request with no retry left ends with the reason interrupted. Pi treats a deferred response differently. If the process dies while fetching one, the retry polls for the existing response. Pi’s restart limit counts consecutive cutoffs without a new saved task state. At three, a repeatedly interrupted replay-safe tool ends with restart_limit. Tool execution is sequential, so one call in a model round can make the next wait. The 60-second retention setting caches an inactive conversation’s model context in memory. The transcript lives in storage. README.md

On startup, interruptLocations() first marks runs with pending workspace work in chats pinned to disabled locations for abort. Core then inspects pending submissions, reconciles reserved tool time, and calls arm for pending submissions in chats that own their budget. Foreground specialists use their parent’s budget, and independent workers have their own. The explicit harness.resume() call comes after the loop. Inside arm, submission.wait() can already start scheduling before the expiry check. The expiry check looks at the effective deadline, including applicable reservations, and writes the time-limit marker before aborting. That marker lets the snapshot show failed with time limit reached when Pi reports aborted. The data model locates these fields among the conversation records. index.ts README.md view.ts

Six tests put the crash at a known point

Each recovery test starts Pipal core in a child process against a temporary store, using Pi AI’s local faux provider. The driver waits for the child’s ready message and, for a MCPA protocol through which a client such as Pipal can discover the tools a service offers and ask the service to run one with supplied arguments. lookup, for the tool server to receive the call. It then sends SIGKILL, verifies the exit signal, and starts a new process against the same directory. The new process checks that the run settles on its own before testing any duplicate send. recovery.test.ts recovery-worker.ts

Deep diveWhat the recovery tests actually assert
Crash point What the test checks after reopening
Live text from a model reply The same title, profile, model, and submission ID, one user entry, and a completed replacement reply. The kill follows visible progress, so the test can pass without a persisted partial. recovery-worker.ts
A one-off routine’s reply The routine is finished, its last run is completed and points to the recovered answer, and there is one scheduled input. Before the kill, the worker checks that a client request ID with the reserved routine: prefix is rejected. recovery-worker.ts
A woken counter call A second process is refused before the kill. Afterward, the replay-safe counter receives its checkpoint of 41 and produces 42, with one call and one result in the transcript. recovery.test.ts recovery-worker.ts
A specialist running that counter while its parent waits The same specialist conversation has the original assignment, one counter result of 42, and the specialist’s reply. The parent has one delegation result pointing to that conversation. recovery-worker.ts
An external delivery acknowledged before the call returns The model and transcript get the interrupted-result warning, and the independent service observes exactly one delivery. recovery-worker.ts recovery.test.ts
An MCP server received a lookup and has yet to reply The tool server sees one call to lookup-page. The recovered transcript holds one failed result explaining that the call may have partially run. recovery.test.ts recovery-worker.ts

Every scenario except the scheduled routine submits two copies of the same message concurrently in one process before the kill. After recovery an identical retry returns the original submission ID and changed text gets 409. A separate runtime test closes and reopens the runtime with a manual clock. It advances past the original deadline while the store is closed, then checks that the same approval is still waiting and that allowing it lets the run finish. recovery-worker.ts runtime.test.ts

A second runtime test reopens a chat with a parked approval after its location was disabled. The chat settles as stopped, an attempt to allow the saved request fails with status 409, and the tool never runs. runtime.test.ts

Pi documents its Node SQLite adapter as using write-ahead logging, or WAL, with synchronous=NORMAL. Pipal opens that adapter for durable.sqlite. Committed work survives process death under this policy. Sudden power loss or host failure can still cost the newest commits. When things go wrong covers restoring from a backup. README.md index.ts

Pi can recover an interrupted task, but where do the deadline and the approval rule come from? What Pipal adds to Pi places those decisions on the application side of the harness.

Recap

  • An accepted input, its work, and its deadline commit together. An identical retry can recover the original receipt.
  • A closed tab ends a watch. A clean host stop and a process crash leave pending work for startup to inspect.
  • Model attempts, running tools, and parked approvals recover according to the state Pi and Pipal saved for each.
Check yourself
  1. Your Send request loses its response. What lets you retry without adding a second copy of the message?
    • Reopen the event stream first
    • Send the identical input with the same request ID
    • Send it with a fresh request ID
    Answer and explanation

    Send the identical input with the same request ID

    Pipal can find a committed submission by its original request ID and return its receipt.

  2. The host dies just after a replay-unsafe tool’s remote service accepts a write. What does the model get on recovery?
    • A result guaranteeing that nothing changed
    • A fresh result from repeating that invocation
    • An interrupted result warning that the call may have partially run
    Answer and explanation

    An interrupted result warning that the call may have partially run

    The service’s write and Pi’s tool result are separate events. Recovery reports the missing result without replaying that invocation.

  3. A workspace call is parked for approval when the host stops. Its location is still enabled and available at startup, after the original deadline. What happens?
    • The call continues waiting for a decision
    • The tool runs automatically
    • The approval is turned into a denial
    Answer and explanation

    The call continues waiting for a decision

    The recorded approval wait survives the restart, and Pipal treats time waiting for the user separately from running time.

  1. Your Send request loses its response. What lets you retry without adding a second copy of the message?

  2. The host dies just after a replay-unsafe tool’s remote service accepts a write. What does the model get on recovery?

  3. A workspace call is parked for approval when the host stops. Its location is still enabled and available at startup, after the original deadline. What happens?

In the source

Book contents

Search the book