Agent ArchitectureRuntime and effect recoveryPrincipal
How should an agent recover a refund whose outcome is unknown after a worker crash?
By Amit Kumar4 min readWorked answer includedReviewed 26 Sept 2026
The question
Interview question
A support agent submits an authorized refund. The payment provider commits it, but its response is lost and the worker crashes before recording success. A replacement resumes from an older checkpoint. Design recovery that avoids a second refund, including concurrent workers and authorization checks. The outcome may remain unresolved for days, while the provider’s idempotency-key retention window is only a day.
Draft your approach first
Take a few minutes before you read the answer. Name the constraint that matters most, the failure you are designing for and how you would know it worked.
Reveal the worked answerWorked answer
After a timeout, the system knows that it did not receive a usable response. It does not know whether the refund happened. Recovery must preserve that uncertainty until the payment provider or another authoritative record resolves it. Asking the resumed model to choose between “failed” and “succeeded” would turn missing evidence into a financial decision.
Separate the intended business operation from attempts to carry it out. One authorized refund gets one durable operation identity before any provider call. A retry is another attempt at that same operation. It is not a new refund merely because a worker restarted or a model generated the arguments again. Record the payment, amount, currency, provider account, requesting principal and approval alongside that identity. A uniqueness rule for the intended business action can prevent another run from creating a second local intent.
The runtime, meaning the code controlling execution, persists the operation and a command to send it. A transactional outbox is one implementation: a local database transaction records both the operation and a pending command, which a worker later delivers. It closes the gap between saving local intent and scheduling work. It does not make the database transaction atomic with the remote payment provider. The network call remains a separate boundary where a crash can leave an unknown outcome.
The worker claims the operation, checks that current authorization still covers the exact action, records an attempt and sends the request. If the provider confirms a refund identity, record success. If the connection fails or the worker disappears after recording the attempt, the replacement treats it as potentially submitted. The worker might have crashed before sending any bytes or after the provider committed. Our ledger alone cannot distinguish those cases.
Provider idempotency can make repeated attempts safe under a documented contract. An idempotency key tells the provider that several requests represent one operation. Reuse the same key, provider scope and parameters for all attempts. In Stripe API v1, for example, the provider saves the first result after execution begins and can return that result, including a saved error, on subsequent requests. Its idempotency documentation also describes different API v2 behavior. Verify the actual API version rather than generalizing one contract to every endpoint.
The retention limit is decisive. Stripe's API v1 documentation permits pruning keys after they are at least 24 hours old, after which reuse can create a new request. That is not a promise that every key expires at exactly 24 hours. Once the safe deduplication window is no longer assured, do not resubmit merely because the local outcome is unknown. Reconcile using a durable provider reference or operation marker if supported. Check the target payment, amount, currency and status. Absence from an eventually consistent lookup is not necessarily proof that no refund occurred.
If the provider cannot reliably establish the outcome, stop automatic sending and create a manual resolution case. Give the operator the attempted request and available provider evidence. A reversal, if the business and provider support one, is a new action with its own authorization and failure modes. It is not a generic way to erase uncertainty about a refund.
Concurrent workers introduce another boundary. A lease gives one worker temporary ownership of local work. A fencing token is an increasing ownership version that lets a participating system reject stale owners. Our database can reject a stale worker's updates, but that does not stop its already in-flight payment request. Unless the provider honors fencing, external safety depends on valid provider deduplication, reconciliation or pausing uncertain work. A local exactly-once transition does not imply one remote effect.
Approvals must bind the exact refund, including any permitted bounds. If someone changes the amount or payment while the run waits, the earlier approval cannot silently cover the new effect. Recheck grants before a new submission. If revocation races with a request already sent, retain its ordering and reconcile the outcome rather than claiming cancellation undid the request. Record operation, attempt, approval and provider identities without copying credentials or unnecessary customer data into traces.
Test crashes before send, after provider commit and before local success, including a stale worker waking after its lease expires and a retry after key retention is no longer guaranteed. Alert on the age of unresolved financial operations. Temporal recommends idempotent Activities because execution can retry, but durable scheduling does not supply the provider's missing effect guarantee. The long-running agent essay provides related context. Safe recovery comes from explicit operation identity and evidence about the external outcome.
Related questions
Keep practising agent architecture
Follow another question in this area, or search the complete library.
Get my essays on AI engineering and distributed systems by email. Subscribe to ArchCrux