# Which invariants prevent A2A v1.0 terminal-state resurrection when push-notification webhooks redeliver a `completed` artifact stream after the task has already been finalized **Date of Record:** 2026 November 29 **Scope:** Applies to A2A v1.0 task lifecycles where a remote agent emits streamed artifacts (`TaskArtifactUpdateEvent`) and also registers a push-notification webhook for the same task. Covers the case where webhook delivery is at-least-once and can arrive after the task has transitioned to a terminal state (`completed`, `failed`, `canceled`). Does not cover purely synchronous `message/send` flows with no push channel, nor artifact chunking semantics within a single stream. ## Core Distinction The A2A task lifecycle is a *monotonic* state machine: `submitted → working → (input-required ↔ working) → terminal`. The stream transport (`tasks/resubscribe` / SSE) and the push-notification webhook are **two independent delivery channels for the same event log**. The invariant that matters is not "events are delivered once" — webhooks are at-least-once by construction — but rather **terminal-state absorption**: once a task reaches a terminal state, any subsequently arriving artifact event for that task must be *absorbed* (acknowledged, logged, discarded) rather than *applied* (mutating the task record, re-emitting to consumers, or re-triggering downstream agents). The non-obvious boundary is this: an artifact event carries a `taskId`, an `artifact` payload, and an `append`/`lastChunk` flag, but it does **not** carry the lifecycle epoch or the terminal-state guard. A naive consumer treats "artifact arrived" as "task still producing" and flips the task back to `working`, resurrecting a finalized task. The state machine is monotonic only if the consumer enforces monotonicity — the protocol does not enforce it for you. ## What Was Observed A coordinator agent dispatched a task to a remote worker over A2A v1.0 with both an SSE resubscription channel and a push webhook pointed at `/hooks/task`. The worker streamed three artifact chunks, then emitted `status: completed` on the stream. The coordinator's stream handler applied the terminal transition, wrote the task record with `state=completed`, and released the baton to a downstream auditor agent. Roughly 4.2 seconds later, the webhook delivered a *retry* of artifact chunk 2 (the original POST had timed out at the sender despite the coordinator having processed it). The coordinator's webhook handler executed the following trace: 1. `POST /hooks/task` with `{taskId: T-8841, artifact: {parts:[...]}, append: true, lastChunk: false}`. 2. Handler looked up `T-8841`, found `state=completed`, but its guard only checked `state != canceled` (a guard written for the cancel path, reused by mistake). 3. Handler applied the artifact: appended chunk 2 to the artifact list **a second time** (duplicate part), and — critically — set `state=working` because the handler's logic was "if not terminal-with-artifact, we are still receiving." 4. The state flip emitted a `TaskStatusUpdateEvent(working)` onto the coordinator's internal bus, which the auditor agent was subscribed to. 5. The auditor, which had already received the `completed` baton, saw a `working` status for a task it believed finalized, treated it as a **reopened task**, and re-ran verification against the now-duplicated artifact. 6. Verification produced a conflicting verdict (the duplicated part changed the artifact hash), and the auditor blocked the baton. Net effect: a completed task was resurrected to `working`, its artifact was corrupted by duplication, and a downstream agent diverged on a task it had already closed. No error was raised anywhere — every individual handler "succeeded." ## Condition of Validity The terminal-state absorption invariant holds **only if** all three of the following are enforced at the consumer, not assumed from the protocol: 1. **Monotonic state guard keyed on terminality, not on a specific terminal value.** The guard must be `if current_state in TERMINAL_STATES: absorb(event); return`, where `TERMINAL_STATES = {completed, failed, canceled}`. A guard that checks a single terminal value (e.g., only `canceled`) is invalid — it leaves `completed` and `failed` resurrections open. This is the exact bug observed above. 2. **Idempotent artifact application keyed on a stable chunk identity.** Artifact events must be deduplicated by `(taskId, artifact.artifactId, chunkIndex)` or an equivalent content-addressed key, because at-least-once webhook delivery *will* redeliver. Without this, absorption of the state transition still leaves a duplicated artifact, which corrupts downstream hashing and verification even when the state machine is respected. 3. **Terminal-state absorption must not emit downstream events.** Absorbing a late event means: log it, ack with 2xx, and **emit nothing** onto the internal bus. The failure above was compounded precisely because the handler emitted a `working` event during absorption. An absorbing handler is a silent sink. The invariant does **not** hold if the coordinator relies on the remote worker to not redeliver, or on webhook ordering, or on the SSE channel and webhook channel being mutually exclusive. All three assumptions are false under A2A v1.0's at-least-once push semantics. ## Decision Boundary The boundary between *absorb* and *apply* for an incoming artifact event is decided by a single predicate evaluated against the **current persisted task state at the moment of handling**, not against the state embedded in the event (events carry no authoritative state) and not against the state at dispatch time (which may be stale by seconds under webhook retry). - If `current_state ∈ {completed, failed, canceled}` → **absorb**. Ack 2xx, log with `absorbed=true`, emit nothing. - If `current_state ∈ {submitted, working, input-required}` and the chunk key is unseen → **apply**. Append, persist, emit. - If `current_state ∈ {submitted, working, input-required}` and the chunk key is **seen** → **absorb** (idempotent duplicate). Do not append, do not emit. - If `current_state` is absent (task unknown) → **reject** with 404; do not create a task from a webhook event. Task creation is the dispatcher's job, not the webhook's. The dangerous misclassification is treating "artifact event for a terminal task" as a *reopen signal*. It is never a reopen signal. Reopening in A2A requires an explicit new task or an `input-required` transition driven by the *worker*, not by a client-side webhook replay. ## Operational Effect Enforcing absorption changes three observable behaviors: 1. **Baton integrity is preserved.** The auditor never sees a post-terminal `working` event, so it never re-runs verification on a closed task. The baton-passing race that produced the conflicting verdict is eliminated at the source rather than patched at the auditor. 2. **Artifact hashes are stable.** With chunk-key deduplication, the artifact hash computed by the auditor matches the hash computed at completion, regardless of how many webhook retries arrive. Verification replay discrepancies vanish. 3. **Webhook handlers become pure sinks for late events.** They no longer mutate state, which means the state machine's monotonicity is a property of the consumer's guard, testable in isolation: feed a terminal task a stream of artifact events and assert zero state transitions and zero bus emissions. The residual risk after enforcing absorption is *silent loss*: if a legitimate late artifact (e.g., a final chunk that raced the completion event) is absorbed, the artifact is incomplete. This is why the completion event must carry `lastChunk: true` on the final artifact and the consumer must verify artifact completeness **before** the terminal transition is committed — not after. Absorption is only safe when terminality is committed only after artifact closure is confirmed. ```python from dataclasses import dataclass, field from enum import Enum from typing import Optional class LifecycleState(str, Enum): SUBMITTED = "submitted" WORKING = "working" INPUT_REQUIRED = "input-required" COMPLETED = "completed" FAILED = "failed" CANCELED = "canceled" TERMINAL_STATES = frozenset({LifecycleState.COMPLETED, LifecycleState.FAILED, LifecycleState.CANCELED}) class TerminalResurrectionError(RuntimeError): """Raised when an artifact event would mutate a task already in a terminal state.""" pass class ArtifactIncompleteAtCompletionError(RuntimeError): """Raised when a terminal transition is attempted before the final artifact chunk is confirmed.""" pass @dataclass class TaskRecord: task_id: str state: LifecycleState = LifecycleState.SUBMITTED applied_chunks: set = field(default_factory=set) artifact_closed: bool = False bus_emissions: int = 0 def handle_artifact_event( record: TaskRecord, artifact_id: str, chunk_index: int, last_chunk: bool, strict: bool = True, ) -> str: """Absorb-or-apply decision for an incoming artifact event. Returns 'absorbed' or 'applied'.""" chunk_key = (artifact_id, chunk_index) # Guard 1: terminal-state absorption. Must check the full terminal set. if record.state in TERMINAL_STATES: if strict and chunk_key not in record.applied_chunks: # A late chunk we never saw before a terminal commit is a real # completeness violation, not a benign retry. Surface it. raise TerminalResurrectionError( f"task {record.task_id} is {record.state.value}; " f"unseen chunk {chunk_key} arrived post-terminal" ) return "absorbed" # ack, log, emit nothing # Guard 2: idempotent dedup for at-least-once delivery. if chunk_key in record.applied_chunks: return "absorbed" # Apply. record.applied_chunks.add(chunk_key) if last_chunk: record.artifact_closed = True record.state = LifecycleState.WORKING record.bus_emissions += 1 return "applied" def commit_terminal(record: TaskRecord, target: LifecycleState) -> None: """Commit a terminal transition. Refuses if the artifact stream is not closed.""" if target not in TERMINAL_STATES: raise ValueError(f"{target} is not terminal") if not record.artifact_closed: raise ArtifactIncompleteAtCompletionError( f"task {record.task_id}: cannot commit {target.value} before lastChunk confirmed" ) record.state = target record.bus_emissions += 1 ```