Agent ArchitectureDurable runs and workflow changePrincipal
Why must replayable agent-workflow code isolate external effects in tracked activities?
By Amit Kumar3 min readWorked answer includedReviewed 28 Sept 2026
The question
Interview question
A history-replay workflow sends an email directly while computing its next step. The provider accepts it, then the worker crashes before progress is recorded. Explain why replay can resend it, design the correct activity and business-operation boundaries, and test crashes before submission, before recorded completion and after recorded completion without claiming exactly-once activities.
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
In a history-replay workflow engine, workflow code can run again to rebuild its state from recorded events. The workflow should schedule an email activity, not call the email API directly as an unrecorded side effect while calculating the next step. If a direct call sends the email and the worker crashes before recording subsequent workflow progress, replay may run that call again. Some SDKs restrict that kind of I/O in workflow code, but a wrapper or poorly isolated helper can still put the side effect in the wrong place. Temporal's workflow definition requires deterministic replay, and its AI agent reference architecture places nondeterministic external I/O in activities.
Move the provider call into a tracked activity with a stable operation ID. A recorded completed activity is represented by its result when workflow history replays, so replay of the workflow logic does not by itself resend it. Do not infer that activities are exactly once. An activity can also be retried after a timeout or worker crash with an unknown provider outcome. The email or action endpoint needs an idempotency key, a sent-message lookup or another explicit reconciliation path. Use the same stable business action identity across attempts, not a newly generated key on each retry.
For a hypothetical history, workflow replay encounters an already-recorded activity completion and supplies its stored result to the workflow logic. It does not need to contact the email provider again to recompute that result. If the provider accepted the message but the activity completion never reached durable history, the engine may instead retry the activity. This is the ambiguous external boundary that idempotency or reconciliation must cover.
The activity’s retry attempt ID can change while its business message identity stays stable. Derive or store that identity before submission and preserve the exact recipient/content intent under the provider’s contract. A fresh random key generated inside each attempted send defeats deduplication. A recorded activity failure also needs interpretation: a definite provider rejection and a lost response after possible acceptance require different next steps.
I would induce a worker crash at three moments: before scheduling, after the provider accepts the message but before activity completion is recorded, and after completion is in history. Compare provider message IDs, activity attempt IDs and workflow event history. The first should lead to one eventual send, the second may retry and must be deduplicated or reconciled, and the third must not send again on workflow replay. Also test a code deployment against a saved history for deterministic command order.
The refund succeeded but the agent crashed. What happens next? asks how to recover an ambiguous refund after an agent crash. How do you deploy new agent workflow code while 200,000 runs are waiting? deals with deploying workflow changes while many runs wait. This case is a code-placement error: a developer put a real-world side effect in code that is allowed to replay. The repair must identify the line that performs external I/O and give it a recorded execution and recovery contract.
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