Your Retry Survived a Timeout. Does It Survive a Restart?
The worker sends an approved refund. The provider commits it. The response disappears, and the worker exits before writing a confirmed result.
A new worker receives the job. Its first task is to decide whether it is continuing an existing operation or creating another one. Generating a fresh request key at startup answers that question incorrectly before the model has said a word.
This is a synthetic scenario from AFL’s When the Agent Acts Twice workbook. The lab uses local files and an invented provider contract. Its purpose is to expose the recovery decision; its storage code is not a production database.
List what survives before choosing a retry
There are three relevant records:
| Record | What it needs to establish |
|---|---|
| Approval | The authorized tenant, target, amount, currency and operation identity |
| Local operation | The durable key and payload chosen before the request; the last known state |
| Provider record | Whether that key committed an effect, and the resulting external ID |
A local pending record does not mean nothing happened. It means the application has not confirmed the outcome. A provider may have completed the work while the application was unavailable.
Similarly, a completed local record is trustworthy only to the extent that the application records it after receiving adequate evidence and protects that record against corruption or unauthorized modification.
The key belongs to the logical approved operation. It must survive the worker, queue delivery and model session. A retry counter or the current process ID identifies an attempt, which is a different thing.
Run the recovery exercise
From the extracted workbook directory, with Node 22 or newer, run:
node lab/run.mjs recoveryThe runner limits the first execution to one attempt. After the simulated provider commits and loses the response, the application returns a pending result. The runner then discards both in-memory objects and reconstructs them from their separate files.
Inspect beforeRecovery, result, localOperation and effectCount. The second application instance reuses the stored operation key. Under this provider’s contract, a matching retry returns the original result, leaving one effect.
The runner demonstrates object recreation. The test suite goes further by checking recovery in a new operating-system process:
node --testNeither experiment simulates losing the disk, running two writers against the same JSON files, or the operating system failing before a write becomes durable. Those are separate failure models.
The dangerous gap in a local ledger
A proposed fix often sounds like this:
If operation is not completed:
call external provider
mark operation completedThis is pseudocode, not a safe execution protocol. The gap between the external call and the completion marker remains. A crash in that gap leaves an incomplete record even though the effect occurred.
Reversing the two statements creates a different defect. Marking complete before the call can lose work when the process dies before execution. Merely placing both records in the same application table does not make a remote provider participate in a database transaction.
The lab closes the duplicate-effect gap by relying on the provider’s atomic key-and-effect record. The application persists identity and approved intent first, and the provider returns the original result for a matching key. If your downstream service cannot offer comparable behavior or authoritative reconciliation, an ambiguous outcome may require stopping for investigation.
Put an expiry on the reasoning too
Consider a variation: the provider retains keys for 24 hours, but the job queue permits redelivery after seven days.
A seven-day-old replay can use exactly the same key and still create another effect if the provider treats an expired key as new. Stable identity is necessary here, but no longer sufficient.
Before releasing that integration, answer these questions:
- How is the provider’s retention window defined and measured?
- Can the application look up the original external operation after key expiry?
- Does that lookup distinguish “not found yet” from “definitively never committed”?
- What prevents automatic replay when those questions remain unresolved?
Record the retry horizon next to the provider guarantee. Test the boundary rather than assuming the retention period covers every recovery path.
Keep the release claim bounded
Event-driven handlers add another recovery issue: a claimed event may be incomplete. A duplicate-delivery check must distinguish completed work from a worker whose lease expired. Otherwise “deduplication” can become permanent suppression of unfinished work.
Use a review note that names the failure model:
Operation identity survives:
Approval is bound to:
Concurrent execution is controlled by:
Provider deduplication lasts:
Unknown outcome is reconciled by:
Recovery stops when:
Storage failures not covered:An agent can help inspect the records and propose a recovery action. Trusted execution code must enforce the identity, approval and replay rules. A model describing the operation as safe to repeat does not establish that no effect occurred.
The workbook supplies the complete recovery scenario and asks you to make a release decision before reading its answer key. Treat the local result as practice in making that decision, then identify the evidence your own provider and storage system would need.