34.5 The handoff that can go wrong
- Approved by
- Panaversity ·
In everyday life. You ask a courier to collect a parcel, and the phone line drops before you hear the booking number. If you simply call again, two couriers may come. A careful person calls again and says, "I booked a collection for this address at 3 o'clock; is it already booked?"
What you will learn. Why the moment between starting a process and recording it is dangerous, and the four parts of a handoff that is safe.
Why it matters. Two reconciliations of one month, or two payment runs for one supplier, are not a technical glitch. They are a finance incident.
The failure
The worker's tool starts the Workflow. Cloudflare creates it and it begins to run. Then, before the tool's result is saved, the conversation's object dies. After the restart, Pi replays the tool, exactly as Chapter 33 taught. The tool does not know a Workflow already exists. It starts another.
This is the same problem as paying an invoice twice, in a new place. And the same idea fixes it: the second attempt must be recognised as the same as the first.
There is a second, quieter failure. The Workflow finishes and sends its result to the worker. The worker's record of the process in Postgres is never updated, because the message that should update it was lost. Now Postgres says "running" for a process that ended a week ago.
Both failures come from one fact. The worker's state, the Workflow's state and Postgres are three stores, and no transaction covers all three. You cannot make them change together. You can make every change safe to repeat, and check that they agree.

Figure 34.1. The four parts of a safe handoff. Each part makes one handoff safe to repeat; the hourly check finds what they missed.
Part 1: a stable process ID
The process ID is made from the request, not at random: recon-acme-2026-11. The tool asks Cloudflare to create a Workflow instance with that ID. If an instance with that ID already exists, creation fails, and the tool reads the existing one instead.
const processId = `recon-${client}-${month}`;
const instance = await startOnce(this.env.RECONCILE, processId, { client, month, processId });startOnce tries to create the instance and, if the ID is taken, returns the existing instance. We tested this on Wrangler's local runtime on 30 November 2026: a second create with the same ID fails with instance.already_exists, and get returns the first instance.
Use only letters, digits, - and _ in the ID. A Workflow rejects an ID with a colon, such as recon:acme:2026-11, as invalid. Claude Code often writes IDs with colons, because that style is common for keys. If startOnce then treats every error as "already exists", nothing starts and nothing fails loudly. Make it re-throw any error that is not already_exists.
The ID protects you only while the instance exists. Once a Workflow instance is deleted, or its retention period ends, the same ID can be created again. So the lasting guard is the process record in Postgres (Part 3), not the Workflow's ID alone.
The same ID also acts as a lock. Two conversations, or a schedule and a person, that try to reconcile Acme's November get the same process.
What if the ID must allow a second run, for example after a rejected report? Add a run number, decided by Postgres: recon-acme-2026-11-r2. The next number comes from a database row, not from the clock.
Part 2: an outbox
When the tool starts the process, it also writes the fact into a table in the conversation object's own storage: "process recon-acme-2026-11 started; tell Postgres." That row is the outbox. Where your own records change in the same step, such as the assignment's status (the assignment is the piece of work the worker was given, here Ana's request), write them and the outbox row in one transaction, so they cannot disagree.
Pi's own checkpoint is not part of that transaction: Pi saves it in its own way. So the outbox row must be safe to write twice. Give it the event's ID as its key, recon-acme-2026-11:started, and a replay writes the same row again.
A separate step reads the outbox and sends each event to Postgres. When Postgres confirms, the row is marked sent. If the object dies at any point, the row is still there after the restart, and the step sends it again.
The Workflow does the same with its own news: each step that changes something writes an event, and step 10 sends the result.
Part 3: a consumer that accepts each event once
Because the outbox sends again after a crash, Postgres will sometimes receive an event twice. The consumer, the code that writes events into Postgres, uses the event's ID and a unique constraint:
create table process_events (
event_id text primary key, -- 'recon-acme-2026-11:started'
process_id text not null,
kind text not null,
body jsonb not null,
at timestamptz default now()
);A second copy of an event fails the insert and is ignored. The process record is updated only from events that were inserted.
The record of each delegated process holds what an auditor needs. That is the client, the worker, the assignment, the process definition and its version, and a hash of its inputs. It also holds who authorized it, the Workflow instance ID, its status, when it finished and what it cost.
Part 4: a regular check
Even with all three parts, something can disagree: a bug, a manual change, a deploy at the wrong moment. So once an hour, a scheduled job compares the process records in Postgres with the Workflow instances. It reports any difference: a process Postgres thinks is running that Cloudflare says finished, or a Workflow with no record at all. A person looks at the report. The job does not fix things on its own.
Step 4. Add the safe handoff: startOnce with the stable process ID, an outbox table in the conversation object, keyed by event ID and written in the same transaction as the assignment record, the process_events consumer in Postgres, and an hourly check that lists disagreements. Write the tests first: kill the object right after the Workflow is created; send the same outbox event three times; a Workflow with no record shows up in the check.
What to check. Is the process ID built from the request, never from crypto.randomUUID() or the time? Is the outbox row keyed by event ID, and written in the same transaction as your own records? Does the consumer ignore duplicates by event ID, and does the hourly check only report?
The invariant. No transaction covers the worker, the process and the database. Make every handoff safe to repeat, and check regularly that they agree.
Check yourself. Claude Code's tool starts the Workflow with a random instance ID and saves it with api.memo() afterwards. Is the handoff safe? (No. If the object dies after the Workflow is created and before the memo is saved, the replay makes a new random ID and starts a second Workflow. Build the ID from the request.)
Sources
- Building Affordable, Self-Improving AI Workers (Panaversity, 29 November 2026), section 7.7, and companion appendix A2
- Owner
- Panaversity