The interrupted Agent resumed correctly from its last checkpoint. That’s why the side effect happened twice.
Article takeaway
Now that the Cloudflare Agents SDK supports Pi 1.4, I stopped an Agent running on a Durable Object and measured how much of its conversation history, tasks, and tool execution could be recovered. The recovery mechanisms worked as expected, but recovering an Agent and preventing duplicate external side effects are two different things.
- I confirmed Hibernation and Parked Tasks, introduced in Pi 1.4, on a real Durable Object
-
replay: "safe"does not check idempotency. Apply it to a non-idempotent tool, and the same side effect happens twice - Storage Error Recovery does not save an in-progress call; it reopens Pi starting with the next operation
-
agents@0.30.0has a known bug where a task waiting to retry cannot wake while another task is running. Use0.30.1or later
| What I observed | Result |
|---|---|
| Conversation history after restart | Restored |
| Completed tasks | Not executed twice |
Idempotent tool marked safe
|
Re-executed, but side effect occurred once |
Non-idempotent tool marked safe
|
Side effect occurred twice |
unsafe tool |
Not automatically re-executed, and did not complete |
| Hibernation | Resumed on a different instance while waiting |
And here’s the main point I want to make up front.
replay: "safe" does not make a tool safe. It only tells Pi, “If this tool is interrupted, it’s okay to run it again.” The tool implementation must guarantee that its side effects are truly idempotent, and that an external API will not process the same request twice.
This article expands on the talk “Cloudflare Agents Now Supports Pi 1.4: Testing Agent Interruption, Recovery, and Persistence” with test procedures, measured results, and failed measurements that I couldn’t cover in the presentation. It’s for people using Cloudflare Agents and Pi Durable to interact with external APIs and databases.
1. Breaking down the four items in “Pi 1.4 support”
On October 22, 2026, Cloudflare announced Pi 1.4 support in the Agents SDK. The changelog lists four items:
- Hibernation
- Parked Tasks
- Storage Error Recovery
- Model Stream Retry
At first, I thought all four were new features in Pi 1.4. But tracing the packages and change history revealed that they came from two different layers.
| Changelog item | Where the change actually comes from |
|---|---|
| Hibernation | Agents SDK wiring for Pi 1.4 sleeping tasks |
| Parked Tasks | Agents SDK exposes a Pi 1.4 feature |
| Storage Error Recovery | A fix in the Agents SDK, independent of the Pi version |
| Model Stream Retry | A fix in the Agents SDK, which also works with Pi 1.3 |
In particular, the Model Stream Retry PR says that the behavior was verified with both Pi 1.3.0 and Pi main. So comparing Storage Error Recovery and Model Stream Retry between Pi 1.3 and 1.4 is the wrong axis. The relevant comparison is agents@0.29.0 vs. agents@0.30.x.
Vendor changelogs group changes into units that are easy for users to understand. But when investigating an incident, you need to identify which package and layer is responsible.
2. The roles of Pi, PiHarness, and Lifecycle
This setup has three layers.
| Layer | Responsibility |
|---|---|
| Pi Durable | Agent execution loop, tasks, checkpoints, and replay |
| PiHarness | Connects Pi to Durable Object SQLite and startup processing |
| Lifecycle | Runs long-running work on a Durable Object and wakes it when needed |
Pi Durable is not a Cloudflare package. It’s available as @earendil-works/pi-durable and integrated into Durable Objects by the Agents SDK’s PiHarness.
The important point is that persistence and resumption are separate features.
- Conversation history and tasks are stored in SQLite tables prefixed with
pi_ - Resuming sleeping tasks is handled by Lifecycle Wake jobs and Durable Object Alarms
Having data saved is not enough to resume if there’s no mechanism to wake the task. Conversely, even if a task can resume, it won’t know where to continue from without a checkpoint. In this test, I kept these two mechanisms separate.
The Pi Durable design article puts it this way:
every step of a run is a task that stores a checkpoint before it moves on.
These checkpoints are the foundation for preventing completed tasks from running again after a restart.
3. Persist the observations first
When measuring failure recovery, the first thing to build is not the system under test but the recording mechanism. Writes to a Durable Object are not necessarily committed to disk the moment they’re issued.
The test recorder writes events to SQLite and then usually calls ctx.storage.sync(). Immediately afterward, it calls ctx.abort() and compares whether the events remain after restart.
| Recording method | Restarted | Rows remaining after restart |
|---|---|---|
With sync()
|
Yes | 1 |
Without sync()
|
Yes | 0 |
In both cases, the instance ID generated in the constructor changed. The same instance did not keep running; a newly created instance read from SQLite.
ctx.abort() is different from a regular throw. It cannot be caught in application code and immediately resets the Durable Object. If you use a regular exception to inject a failure, you’ll be measuring error handling in the same instance, not Agent recovery.
After confirming this foundation, I recorded all subsequent events with sync(). I did not treat observations missing from the logs as evidence of recovery.
4. Conversation history and tasks survive a restart
I ran an Agent that executes a tool once and called ctx.abort() after it completed to stop the Durable Object. Then, after the restart, I read the transcript and events.
| Observation | Before restart | After restart |
|---|---|---|
| Instance identifier | — | Changed |
| Transcript entries | 5 | 5 |
| Tool execution started | 1 | 1 |
| Side effects | 1 | 1 |
The completed task did not run twice. Submitting twice with the same operationId returned accepted: true the first time and accepted: false the second time. With a different ID, it returned accepted: true, so submit itself was not broken.
This shows Pi Durable using checkpoints and submit identifiers to protect completed work.
5. The Agent keeps running after the client disconnects
Next, I extended processing time through model retries and disconnected the WebSocket midway through. I checked whether execution continued after the client was gone.
The run eventually reached done. There were three model calls: two failures and one success. The instance ID did not change before or after the disconnect.
This was not a case of “crashing and recovering.” It showed that Agent execution did not depend on the client connection.
After reconnecting, I could reconstruct the current state from the snapshot. However, I could not use a cursor to replay events from the middle of the stream that arrived during the disconnection. The event stream can be used to track state, but it is not a substitute for a history stream.
6. Parked Tasks stop crash loops
I repeatedly triggered ctx.abort() at the same point and tested with restartLimit reduced to 2.
| How it stopped | Failure triggers | Instances created | pending() |
Next Alarm |
|---|---|---|---|---|
ctx.abort() |
2 | 2 | parked |
None |
| Regular exception | 1 | 0 | Empty | None |
A regular exception does not reset the Durable Object, so it does not count toward the restart limit. To reach Parked, you need a failure that actually causes a restart.
A Parked task will not keep waking the Durable Object if left alone. There are two ways to handle it manually.
| Operation | Return value | wait() |
|---|---|---|
session.retry() |
true |
done |
session.abort() |
ok |
unanswered / aborted
|
retry() retries the task; abort() abandons it. This design does more than stop a crash loop: it lets an operator decide whether to resume or abandon the task.
Also, even when a tool emitted progress eight times via api.output(), the restart counter did not reset. Pi considers a task’s transition to its next committed state to be progress, not tool output. If a single unfinished tool keeps emitting output, the crash-loop limit is not reset.
7. Storage Error Recovery takes effect on the “next operation”
Storage Error Recovery lets Pi be reopened after a storage error. I wrapped Storage in a Proxy and injected two kinds of errors:
- Return an arbitrary error from methods such as
commit() - Return a
StorageRequestErrorfrom a read
In both cases, the call already in progress failed. But the next submit() succeeded, and the run eventually reached done. For a fatal Storage error, the factory was called twice instead of once, but the Durable Object’s instance ID did not change.
So this is not Durable Object crash recovery. PiHarness removes the broken Pi instance from its cache and reopens Pi on the next operation or the next step of a Wake job.
The call already in progress is not saved. The specification also says that an in-progress call fails with SessionFailed. The factory must be written so it can initialize correctly every time it runs.
8. replay: "safe" does not protect side effects
This is the central finding. I created four types of tools and recorded their execution start, side-effect commit, and completion separately. I stopped the Durable Object immediately after the side effect was committed and compared what happened after restart.
| Tool | replay |
Idempotent | Execution started | Side effects | Completed |
|---|---|---|---|---|---|
write_record |
safe |
Yes | 2 | 1 | 1 |
bump_unsafe |
Unspecified | No | 1 | 1 | 0 |
bump_safe_unguarded |
safe |
No | 2 | 2 | 1 |
bump_safe_guarded |
safe |
Yes | 2 | 1 | 1 |
write_record writes the same value, so running it twice does not change the result. bump_safe_unguarded simply increments a counter, so each execution changes the result. When I marked this tool safe, Pi trusted that declaration and ran it again, committing the side effect twice.
On the other hand, bump_unsafe is not automatically re-executed. In the case where the Durable Object crashed after the side effect was committed, the side effect remained once, but the tool did not complete. The Agent resolves the run by passing the interrupted tool result to the model.
An interrupted tool does not get a special transcript entry. It uses the regular pi.tool-result entry, but its content changes to something like this:
<harness>
[error] Tool bump_unsafe was interrupted and may have partially run
</harness>
This is plain text telling the model that the tool may have run partially. Whether the model calls the same tool again is up to the model. In three tests with a real model, it did not call the tool again, but that’s just one model, one prompt, and three trials, so I won’t generalize from it.
I added an idempotency key to bump_safe_guarded. It was re-executed, but the second run checked the key and skipped the side effect, keeping the total number of commits to one.
The takeaway is clear. Pi will not warn you if you choose safe incorrectly. If your tools interact with external APIs, payment systems, email, databases, and so on, you need to implement idempotency keys or deduplication separately from the safe setting.
9. Model Stream Retry checks error messages
Model Stream Retry treats an interrupted model stream as a temporary error and retries the entire turn. Reading the package revealed that it uses a regular expression to match the error text.
I made the same number of attempts fail and changed only the error message.
| Error message | Result | Model calls |
|---|---|---|
The stream ended before the model finished its answer. |
unanswered |
1 |
The stream ended without the model finishing its answer. |
done |
3 |
Model stream stalled: no data received for 60s. |
unanswered |
1 |
Model stream timed out: no data received for 60s. |
done |
3 |
insufficient_quota: the account has no remaining credit. |
unanswered |
1 |
For messages that trigger a retry, there were three calls: two failures and one success. In other words, a retry starts a new model request; it does not continue a partial stream.
Even when I set baseDelayMs to 100 seconds, the wait was capped at about 60 seconds. That’s because the default maxAgentDelayMs is 60,000 milliseconds. Increasing the setting does not mean it will wait indefinitely.
10. Hibernation resumes on a different instance while waiting
For the Hibernation test, I first compared alarm times. But in local workerd, the alarm timings matched whether or not I passed onWake. I was measuring the wrong thing.
The essence of Hibernation is not the alarm time, but whether the Durable Object can be evicted from memory while waiting. So I deployed it and recorded the instance ID in the constructor to see which instance ran the retry.
When I passed onWake, the sequence was:
model_error@old-instance
→ instance_started@new-instance
→ model_request@new-instance
Without onWake, the sequence was:
model_error@same-instance
→ model_request@same-instance
onWake |
Instance that ran the retry |
|---|---|
| Passed | Different instance |
| Not passed | Same instance |
I did not send any requests while the retry was waiting. Touching the object would wake it and prevent me from observing eviction.
In-progress work and a sleep while waiting to retry are different states. While work is active, the heartbeat Alarm is rescheduled every 30 seconds. During sleep, the next Alarm is scheduled for the time of the earliest task to wake. Mixing these two up can lead to misreading the Hibernation test results.
11. Reproducing the agents@0.30.0 bug
I tried to start testing with the officially recommended agents@0.30.0. But this version has a known bug: a task waiting to retry will not wake while another task is running.
I ran these two tasks concurrently in one Harness:
- Session A: run a tool that takes 60 seconds
- Session B: return an error that triggers a retry and enter a 12-second backoff
| B running alone | With concurrent work | |
|---|---|---|
agents@0.30.1 |
12 seconds | 12 seconds |
agents@0.30.0 |
12 seconds | 57 seconds |
With 0.30.0, B’s retry was blocked until A completed. With 0.30.1, B resumed after the expected 12 seconds, even while A was running.
The cause is also clear from the package diff. Version 0.30.0 checked only whether Pi was active. Version 0.30.1 uses the sleep deadline wake.at as the next wake-up deadline and calls pi.resume() when it is reached.
This isn’t meant to be alarmist. It’s a concrete example of PiHarness being labeled Beta. If you’re testing Pi 1.4 Hibernation, you need to use agents@0.30.1 or later.
12. Once you upgrade to Pi 1.4, you can’t go back to 1.3
Pi 1.4 updates the SQLite schema for session storage, so you cannot roll back to Pi 1.3.
After running one turn with Pi 1.4, the schema version was 2. Comparing the migration definitions in the packages shows this difference:
| Version | Migrations |
|---|---|
pi-durable@1.3.0 |
Version 1 only |
pi-durable@1.4.0 |
Version 1 and version 2 sleepingStatus
|
The Pi 1.3 migration runner returns an error if it encounters a newer schema version it does not support. The added sleepingStatus is the sleeping task state that supports Pi 1.4 Hibernation.
In other words, being able to evict a Durable Object and losing the ability to roll back are not separate changes. They are two sides of the same schema change.
When comparing Pi 1.4 and 1.3, do not reuse the same Durable Object. Use separate names for experiments and treat migrations as one-way.
13. What I learned by rerunning the tests
I did not publish the initial test results as-is. After rebuilding the controls, I revised or held back three claims.
WebSocket event loss
At first, it looked like a slow event listener was missing events. But the cause was that I closed the WebSocket after eight seconds. When I extended the drain time to 30 seconds, the event counts matched even with the slow listener.
Defining progress
I had assumed that updating my own counter meant Pi had made progress. Even with a control that called api.output() eight times, an unfinished tool still became Parked. Progress means that the task committed a state, not that the tool emitted output.
A 19-minute model stream
I streamed data for about 19.2 minutes using a faux Provider inside a Worker, and it completed with one model call. However, this did not measure keeping an HTTP connection to an external model API open for 19 minutes. Since it does not disprove the official limitation that requests lasting more than 15 minutes may be disconnected, I’m leaving this conclusion open.
When testing failure recovery, “I got a result” is not enough. You need controls to rule out possibilities such as the failure never occurring in the first place or the observation side closing the connection too soon.
14. Conclusion
The Cloudflare Agents and Pi 1.4 recovery mechanisms worked in practice. Conversation history could be restored after a restart, completed tasks were not executed twice, crash loops stopped with Parked Tasks, and an Agent waiting to retry could resume on a different instance through Hibernation.
But Pi protects the Agent’s execution state. It does not automatically make your external side effects exactly-once.
When implementing this, keep these points in mind:
- Add idempotency keys or deduplication for external operations
- Set
replay: "safe"only for tools you can prove are safe to execute twice - Marking a tool
unsafedoes not undo a side effect that has already been committed - Write your factory assuming Storage Error Recovery may call it multiple times
- If you use Hibernation, use
agents@0.30.1or later - You cannot roll back a Durable Object upgraded to Pi 1.4 to Pi 1.3
If I had to distill one lesson from this test, it would be this:
“Recovered” and “completed without duplicates” are two different claims.
References
- Cloudflare Changelog — Agents SDK now supports the latest Pi 1.4 release — The four items announced as Pi 1.4 support
- Cloudflare Docs — Pi — Official documentation for PiHarness, Recovery, and Hibernation
-
Cloudflare Docs — Pi Extensions — Tool
replaysettings and behavior during recovery -
agentsCHANGELOG — Changes in0.30.0/0.30.1 -
cloudflare/agents#2683 — Known bug involving
onWakeand concurrent tasks -
pi-durableCHANGELOG 1.4.0 — Pi 1.4sleepingtasks and schema migration - Pi Durable design article — Checkpoint and task execution design
Top comments (0)