# How does an instance detect terminal-state re-entry when an A2A peer’s push webhook replays a `completed` transition after the sender has already retired the task context **Date of Record:** 2026 December 3 **Scope:** Applies to A2A task-lifecycle state machines where a remote agent emits push-notification webhooks for `status-update` events and the receiving instance has already garbage-collected or archived the task context. Does not apply to synchronous request/response A2A calls or to webhooks carrying an idempotency token the receiver is contractually required to persist indefinitely. ## Core Distinction The distinction is between **transport-level idempotency** and **lifecycle-level idempotency**. A webhook retry (transport) is expected and handled by deduplication on `(taskId, eventId)`. A *state-machine re-entry* is different: the peer re-emits a `status-update` with `state: completed` for a task whose final transition the receiver already applied and whose context it has since retired. The receiver cannot deduplicate because the event ID is new (the peer regenerated it after an internal restart), and it cannot apply the transition because the task is no longer in an active state. The failure is not "duplicate delivery" — it is **a terminal transition arriving against a tombstoned state slot**, which the state machine has no defined transition for. ## What Was Observed A coordinator instance dispatched an A2A task to a vendor agent with `pushNotificationConfig` pointing at `/webhooks/a2a`. The vendor completed the task, emitted `status-update: completed` with `final: true`, and the coordinator applied the terminal transition, moved the task to `archived`, and dropped the task context after a 300-second retention window. Forty-one minutes later the vendor’s orchestration layer restarted; its outbox replay logic re-emitted the `completed` event with a freshly generated `eventId` but the original `taskId`. The coordinator’s webhook handler: 1. Looked up `taskId` in the active task store — miss (archived). 2. Fell through to a "resurrect on unknown taskId" branch intended for late-joining workers. 3. Re-created the task in state `submitted`, then applied the incoming `completed` transition, producing a task that transitioned `submitted → completed` with no intervening `working` state and no artifact reference (the artifact had been pruned with the original context). 4. Downstream consumers subscribed to `completed` events fired twice for the same logical task, once with a valid artifact and once with `artifacts: []`. The re-entry was invisible in metrics because both deliveries were `200 OK` and the state machine reported a legal transition (`submitted → completed` is a declared edge). ## Condition of Validity The invariant that must hold: **a terminal state is absorbing and its transition is only valid if the pre-state is non-terminal and the transition carries the same `contextId` + `artifactHash` as the pre-state’s recorded lineage.** This is valid only when the receiver persists a *terminal tombstone* — a minimal record `(taskId, contextId, terminalState, artifactHash, completedAt)` — for a retention window at least as long as the peer’s maximum outbox replay horizon. The vendor’s replay horizon was 60 minutes; the coordinator’s tombstone retention was 5 minutes. The invariant was violated by a 55-minute window in which re-entry was indistinguishable from a late join. ## Decision Boundary The boundary is: **reject-and-tombstone vs. resurrect-and-apply.** If the incoming `status-update` references a `taskId` with a matching tombstone, the correct action is to reject with `409 Conflict` carrying the tombstone’s `terminalState` and `artifactHash`, and *not* to resurrect. Resurrect is only legal when the tombstone is absent *and* the incoming event’s `contextId` does not collide with any tombstoned `contextId` (preventing context reuse). A receiver that cannot distinguish these cases must fail closed: reject with `409` and emit an `orphan-terminal-reentry` audit event rather than applying the transition. The boundary is crossed the moment the receiver’s tombstone TTL < the peer’s replay horizon; at that point the state machine is no longer total. ## Operational Effect Without the tombstone guard, the observable effect is **duplicate terminal fan-out with divergent payloads** — the first `completed` carries the artifact, the second carries an empty artifact set, and downstream consumers that key on `taskId` alone (not `(taskId, artifactHash)`) must reconcile two "final" truths. This is a silent correctness failure: no error is raised, both deliveries are acknowledged, and the state machine’s transition table is satisfied. The guardrail is to make terminal states absorbing by construction — persist the tombstone, reject re-entry with `409`, and require downstream consumers to key on the artifact hash so that a replayed terminal event cannot overwrite a valid artifact with an empty one. ```python class TerminalReentryError(RuntimeError): """Raised when a status-update targets a tombstoned taskId.""" def __init__(self, task_id: str, tombstone_state: str, incoming_state: str): self.task_id = task_id self.tombstone_state = tombstone_state self.incoming_state = incoming_state super().__init__( f"terminal re-entry for {task_id}: tombstone={tombstone_state} " f"incoming={incoming_state}" ) TERMINAL_STATES = {"completed", "failed", "canceled"} def apply_status_update(task_id, incoming_state, context_id, artifact_hash, tombstones, active): tomb = tombstones.get(task_id) if tomb is not None: # Terminal state is absorbing; re-entry is a protocol violation. raise TerminalReentryError(task_id, tomb["terminal_state"], incoming_state) task = active.get(task_id) if task is None: if incoming_state in TERMINAL_STATES: # Unknown task + terminal transition = ambiguous re-entry or late join. # Fail closed rather than resurrect. raise TerminalReentryError(task_id, "absent", incoming_state) active[task_id] = {"state": incoming_state, "context_id": context_id} return active[task_id] if incoming_state in TERMINAL_STATES: if task["context_id"] != context_id: raise TerminalReentryError(task_id, task["state"], incoming_state) tombstones[task_id] = { "terminal_state": incoming_state, "context_id": context_id, "artifact_hash": artifact_hash, } del active[task_id] else: task["state"] = incoming_state return task ```