# Why does an instance replay a terminal A2A task into a diverged state after idempotent resumption **Date of Record:** 2026 December 3 **Scope:** Applies to A2A (agent-to-agent) task lifecycles where a resuming instance reconstructs task state from a persisted event log plus an idempotency key, and where the remote peer's task state machine admits terminal states (`COMPLETED`, `FAILED`, `CANCELED`). Does not apply to stateless request/response tool calls or to single-agent retry loops that never cross an instance boundary. ## Core Distinction An idempotent resumption is not the same as an idempotent *operation*. The idempotency key guarantees that the peer will not create a second task for a replayed `tasks/send`. It does **not** guarantee that replaying the local event log reconstructs the same state the peer actually holds. The failure lives precisely in the seam between two different idempotency domains: the transport-level deduplication at the peer, and the log-replay determinism at the resuming instance. The non-obvious boundary: a resuming instance treats its persisted event log as authoritative for state reconstruction, but the peer treats the *first* delivered message as authoritative and silently drops replays. When the resuming instance's log contains an event that was *emitted locally but never acknowledged by the peer* before the crash, replay produces a state that is one transition ahead of the peer's actual state. The instance then issues a transition (e.g., `COMPLETED`) that the peer's state machine rejects as illegal from its actual state, or worse, the peer accepts a *different* transition and the two state machines diverge permanently while both believe they are consistent. ## What Was Observed A coordinating agent (`orchestrator`) dispatched a `tasks/send` to a worker agent (`worker`) with `idempotencyKey = k-7f3a`. The worker accepted, transitioned `SUBMITTED → WORKING`, and streamed two `TaskStatusUpdateEvent` artifacts. The orchestrator persisted each received event to its log, then emitted a local `TRANSITION_INTENT: COMPLETED` record *before* sending the terminal `tasks/send` with the final artifact, because the intent record was written by the planning loop that decided the task was done. The orchestrator crashed between writing `TRANSITION_INTENT` and receiving the worker's `COMPLETED` acknowledgement. On cold start, the resumption routine replayed the log, saw `TRANSITION_INTENT: COMPLETED`, and reconstructed local state as `COMPLETED`. It then re-sent the terminal message with the same idempotency key `k-7f3a`. The worker, meanwhile, had *not* completed. Its last acknowledged state was `WORKING`. Its deduplication layer matched `k-7f3a` against the *original* `tasks/send` (the initial dispatch, not the terminal one) and — because the worker keyed deduplication on the task-level idempotency key rather than on a per-message sequence — dropped the replayed terminal message as a duplicate. The worker stayed in `WORKING` indefinitely. The orchestrator, believing the task was `COMPLETED`, moved on. The worker, believing it was still `WORKING`, continued consuming a downstream resource. Neither side raised an error. The divergence was only detected 40 minutes later when a third agent queried the task and received `COMPLETED` from the orchestrator and `WORKING` from the worker. Both answers were internally consistent. Both were wrong about the other. The trace: ``` t0 orchestrator: log += SENT(tasks/send, k-7f3a) t1 worker: state = WORKING, ack(t1) t2 orchestrator: log += RECV(ack t1) t3 orchestrator: log += TRANSITION_INTENT(COMPLETED) # local, unacked t4 orchestrator: CRASH t5 orchestrator: cold start, replay log t6 orchestrator: replay sees TRANSITION_INTENT -> state = COMPLETED t7 orchestrator: resend tasks/send(k-7f3a, terminal) t8 worker: dedup match on k-7f3a -> DROP (treated as replay of t0) t9 orchestrator: state = COMPLETED (believes acked) t10 worker: state = WORKING (never saw terminal) ``` The invariant that broke: **the resuming instance's reconstructed state must be a function only of events the peer has acknowledged, never of locally-emitted intent records.** The intent record at `t3` was a *decision*, not an *observation*, and the replay routine had no way to distinguish the two because both were written to the same append-only log with the same schema. ## Condition of Validity Idempotent resumption is safe only under the conjunction of three conditions: 1. **Log records are typed by epistemic status.** Every record is either an `OBSERVED` event (the peer sent it, we received it) or an `INTENT` event (we decided something, not yet confirmed). Replay may promote `INTENT` to state only if a matching `OBSERVED` confirmation exists later in the log. 2. **Deduplication is scoped to message sequence, not task identity.** The peer's dedup layer must key on `(taskId, messageSeq)` or a content hash, so a replayed *terminal* message after a replayed *initial* message is distinguishable. A task-level idempotency key alone collapses distinct messages into one. 3. **The peer's terminal states are absorbing and queryable.** The resuming instance must be able to issue a `tasks/get` and trust the answer as ground truth, overriding its own reconstructed state. The failure above violated all three. Condition 1 was violated at `t3` (intent written into the replay-authoritative log). Condition 2 was violated at `t8` (task-level dedup dropped a distinct message). Condition 3 was never exercised because the orchestrator trusted its own reconstruction over a query. ## Decision Boundary The boundary is not "should I replay?" but "which authority wins when replay and the peer disagree?" - If the resuming instance can reach the peer, **the peer's `tasks/get` response is authoritative** and the local log is demoted to a hint. Replay is used only to reconstruct *which* task IDs to query, never to reconstruct their states. - If the peer is unreachable, the instance must enter an explicit `RECONCILING` state — not `COMPLETED` and not `WORKING` — and must block any downstream transition until reconciliation completes. The dangerous default is to fall back to the local reconstruction, because the local reconstruction is exactly the thing that diverged. - If the peer reports a terminal state that contradicts the local log's terminal state, this is a **hard inconsistency**, not a mergeable conflict. The instance must not "resolve" it by preferring one side; it must emit an audit record and refuse to proceed, because a terminal-state disagreement means at least one side's event log is corrupt or one side's dedup layer is lossy, and proceeding will propagate the corruption. The subtle case: when the peer reports `WORKING` and the local log says `COMPLETED`, the naive reconciliation is "the peer is behind, resend the terminal message." This is wrong when the peer's dedup layer is the culprit (as at `t8`) — the resend will be dropped again, and the instance will loop. The correct action is to resend with a *fresh* message sequence number (not a fresh task idempotency key, which would create a duplicate task), forcing the peer's dedup layer to treat it as a new message rather than a replay. ## Operational Effect The unbounded consequence: two agents holding contradictory beliefs about a shared task, each internally consistent, each behaving correctly *given its own state*, and no error surfacing until an external observer queries both. In a swarm, this is worse than a crash, because a crashed agent stops consuming resources and a diverged agent keeps consuming them while reporting success. The orchestrator in the trace above released its concurrency slot and admitted a new task, while the worker held a lease on a downstream resource — a silent double-spend of capacity that no single agent could observe. The architectural guardrail is a **two-phase resumption**: `RECONSTRUCT_FROM_LOG` produces a *candidate* state, then `RECONCILE_WITH_PEER` queries and either confirms or overrides. The candidate state is never exposed to downstream consumers until reconcile returns. This costs one round-trip per resumed task, which is the price of not trusting your own memory about what someone else did. The second guardrail is **epistemic typing in the log**. Intent records and observation records must be distinguishable at replay time, because they have different authority. A log that cannot answer "did I decide this, or did I observe it?" cannot support idempotent resumption — it can only support idempotent *retry*, which is a strictly weaker guarantee. ```python class ResumptionDivergenceError(RuntimeError): """Raised when a resuming instance's reconstructed state contradicts the peer's authoritative state.""" pass class TaskState: SUBMITTED = "SUBMITTED" WORKING = "WORKING" COMPLETED = "COMPLETED" FAILED = "FAILED" CANCELED = "CANCELED" RECONCILING = "RECONCILING" TERMINAL = frozenset({COMPLETED, FAILED, CANCELED}) def reconcile_resumption( local_log: list[dict], peer_query, task_id: str, ) -> str: """ Two-phase resumption. Phase 1 reconstructs a *candidate* state from the log, distinguishing OBSERVED records (authoritative for reconstruction) from INTENT records (never promoted without a matching OBSERVED confirmation). Phase 2 queries the peer and treats its answer as ground truth. """ # Phase 1: candidate reconstruction, OBSERVED only. candidate = TaskState.SUBMITTED for rec in local_log: if rec.get("type") != "OBSERVED": continue # INTENT records are excluded from reconstruction candidate = rec["state"] # Phase 2: peer is authoritative. try: peer_state = peer_query(task_id) except TimeoutError: # Cannot confirm; must not fall back to candidate as if it were truth. return TaskState.RECONCILING if peer_state in TaskState.TERMINAL and candidate in TaskState.TERMINAL: if peer_state != candidate: raise ResumptionDivergenceError( f"terminal-state disagreement on {task_id}: " f"local={candidate} peer={peer_state}; " f"refusing to proceed, emitting audit record" ) return peer_state if peer_state in TaskState.TERMINAL: # Peer finished while we were down; adopt peer truth, discard candidate. return peer_state if candidate in TaskState.TERMINAL and peer_state not in TaskState.TERMINAL: # The exact t8 failure: we believe done, peer never saw the terminal # message. Do NOT resend under the original idempotency key — the peer's # dedup layer will drop it. Escalate to a fresh message sequence. raise ResumptionDivergenceError( f"local terminal {candidate} but peer {peer_state} on {task_id}; " f"resend requires fresh messageSeq, not task-level idempotency key" ) return peer_state ```