When must an instance treat a late A2A push-notification webhook as authoritative? Reconciling stale task-state projections against the terminal-state lattice

**Date of Record:** 2026 December 2 **Scope:** Applies to A2A task lifecycle transitions where a remote agent emits asynchronous push-notification webhooks (`TaskStatusUpdateEvent` / `TaskArtifactUpdateEvent`) carrying a `task.status.state` and a monotonic `task.status.timestamp`. Does not cover synchronous `message/send` request/response flows, streaming `message/stream` SSE channels, or transport-level retries that never reach the task store.

Core Distinction

The A2A task state machine is a *lattice with terminal absorbers*, not a linear pipeline. States `input-required`, `auth-required` are **resumable** (the task can leave them); `completed`, `failed`, `canceled`, `rejected` are **terminal** (no legal outbound transition). A webhook is not a state *command*—it is a *projection* of the remote agent's view of the task at the instant the event was generated. The invariant under examination is:

> For any task `T` in local state `S_local` and an inbound webhook event `E` with `E.state = S_remote`, the event may be applied **iff** `S_remote` is reachable from `S_local` in the transition lattice **and** `E.timestamp > T.last_applied_timestamp`. Otherwise `E` is a **stale or out-of-order projection** and must be quarantined, not applied.

The non-obvious failure is that *terminal states are absorbing but not monotonically observable*. A `completed` webhook can arrive **after** a `canceled` webhook for the same task because the two were generated by different internal worker goroutines on the remote side and raced through the webhook dispatcher. The lattice says `completed` is illegal from `canceled`; the naive receiver says "last write wins" and *un-terminates* the task. This is the continuity break.

What Was Observed

A planner agent (`A`) delegated a long-running retrieval+summarization task to a worker agent (`B`) over A2A, registering a push-notification endpoint `https://A/hooks/task`. `B` ran the task with two internal stages: a retrieval worker and a summarizer, each holding a reference to the same `TaskStore` entry and each independently calling `emit_status(task_id, state, ts)`.

Trace (timestamps are `B`'s monotonic clock, microseconds):

t=1000  B.retrieval  emit_status(T7, "working",         1000)
t=4200  B.retrieval  emit_status(T7, "completed",       4200)   # retrieval stage "done"
t=4210  B.summarizer emit_status(T7, "working",         4210)   # summarizer still working
t=9800  B.summarizer emit_status(T7, "failed",          9800)   # summarizer OOM'd

Network delivery to `A` was jittered. `A` observed:

recv 1005  working     ts=1000   -> apply, S_local=working
recv 4310  completed   ts=4200   -> apply, S_local=completed   (TERMINAL)
recv 4322  working     ts=4210   -> A's rule: "completed is terminal, reject"  [correct]
recv 9901  failed      ts=9800   -> A's rule: "failed from completed is illegal, reject" [WRONG]

`A`'s terminal-absorber rule correctly rejected the stale `working` at `t=4210` but **also** rejected the genuinely later `failed` at `t=9800`, because `failed` is not reachable from `completed` in the lattice. `A` reported task `T7` as succeeded and proceeded to consume a summary that never existed. The downstream consumer failed with a null artifact three turns later—far from the fault.

The root cause is that `B`'s first `completed` was **not** a task-terminal event; it was a *stage-local* terminal event mis-projected onto the task-level state machine. `B` conflated its internal stage lattice with the A2A task lattice. `A` had no way to distinguish them because the webhook carried no stage discriminator and the `task.status.state` field is a single flat enum.

Condition of Validity

The reachability-plus-timestamp rule is only sound if **all** of the following hold:

  1. **Single-writer per task.** Exactly one component on the remote side may emit task-level status transitions. If multiple stage workers share a `TaskStore`, the remote must serialize emissions through a single reducer that owns the task state machine, and stage completion must map to a *sub-state* (e.g., artifact-part progress), never to a task-terminal state.
  2. **Monotonic, comparable timestamps.** `task.status.timestamp` must be generated by the same clock source that orders the state machine transitions. Wall-clock timestamps from different hosts are not comparable and violate this; the receiver must fall back to `(state, timestamp)` lattice position rather than timestamp alone.
  3. **Terminal states are truly absorbing.** A `completed` event must mean "no further transitions will ever be emitted for this task." If the remote can still emit `failed` afterward, `completed` is not terminal and the receiver's absorber rule is unsound.
  4. **Idempotent delivery with replay detection.** Webhooks may be redelivered; the receiver must dedupe on `(task_id, event_id)` before applying the lattice check, or a redelivered `completed` after a legitimate `failed` will be rejected by the absorber rule and the `failed` will be silently reverted to `completed` on the next redelivery cycle.

When condition (1) or (3) is violated—as in the trace—the lattice check is *necessary but not sufficient*. The receiver must additionally require a **terminal-confirmation handshake**: on receipt of a terminal webhook, issue a synchronous `tasks/get` to the remote and confirm the terminal state before committing it locally. This converts an untrusted projection into a verified read.

Decision Boundary

The decision boundary is whether to **apply, quarantine, or escalate** an inbound webhook. The algorithm:

The critical asymmetry: a *reachable* transition is safe to apply optimistically; an *unreachable* transition is never safe to drop, because unreachability can mean either "stale" or "remote violated its own contract," and the two require opposite responses.

Operational Effect

With the naive absorber rule, the failure surfaced three turns later as a null-artifact consumer error, with no trace back to the webhook layer. With the corrected boundary:

The invariant to encode: **terminality is a claim, not a fact, until confirmed.** Any A2A receiver that treats a terminal webhook as immediately absorbing without a confirmation read is exposed to stage-local-terminal mis-projection and to out-of-order terminal races. The cost is one extra `tasks/get` per terminal transition; the alternative is silent success on a failed task.

import time
from dataclasses import dataclass, field
from enum import Enum
from typing import Optional


class A2AState(str, Enum):
    SUBMITTED = "submitted"
    WORKING = "working"
    INPUT_REQUIRED = "input-required"
    AUTH_REQUIRED = "auth-required"
    COMPLETED = "completed"
    FAILED = "failed"
    CANCELED = "canceled"
    REJECTED = "rejected"


TERMINAL = {A2AState.COMPLETED, A2AState.FAILED, A2AState.CANCELED, A2AState.REJECTED}

# Reachability in the A2A task lattice. Terminal states have no outbound edges.
REACHABLE = {
    A2AState.SUBMITTED: {A2AState.WORKING, A2AState.REJECTED, A2AState.CANCELED},
    A2AState.WORKING: {
        A2AState.WORKING, A2AState.INPUT_REQUIRED, A2AState.AUTH_REQUIRED,
        A2AState.COMPLETED, A2AState.FAILED, A2AState.CANCELED,
    },
    A2AState.INPUT_REQUIRED: {A2AState.WORKING, A2AState.CANCELED, A2AState.FAILED},
    A2AState.AUTH_REQUIRED: {A2AState.WORKING, A2AState.CANCELED, A2AState.FAILED},
    A2AState.COMPLETED: set(),
    A2AState.FAILED: set(),
    A2AState.CANCELED: set(),
    A2AState.REJECTED: set(),
}


class WebhookContractViolation(RuntimeError):
    """Raised when a webhook cannot be reconciled with the local task lattice
    and synchronous confirmation disagrees or is unavailable."""


@dataclass
class TaskRecord:
    task_id: str
    state: A2AState = A2AState.SUBMITTED
    last_applied_ts: int = 0
    provisional: bool = False
    stale_count: int = 0


@dataclass
class WebhookEvent:
    task_id: str
    event_id: str
    state: A2AState
    timestamp: int


def confirm_via_tasks_get(task_id: str) -> A2AState:
    """Synchronous authoritative read. In production this is an A2A
    `tasks/get` call; here we simulate the remote reducer's settled view."""
    # Placeholder: real implementation issues the RPC and returns the state.
    raise NotImplementedError


def apply_webhook(
    task: TaskRecord,
    event: WebhookEvent,
    seen_event_ids: set,
    confirm=confirm_via_tasks_get,
) -> TaskRecord:
    if event.event_id in seen_event_ids:
        return task  # idempotent redelivery
    seen_event_ids.add(event.event_id)

    # Rule 1: stale or out-of-order by timestamp -> quarantine, never mutate.
    if event.timestamp <= task.last_applied_ts:
        task.stale_count += 1
        return task

    # Rule 2: reachable transition -> apply optimistically.
    if event.state in REACHABLE.get(task.state, set()):
        task.state = event.state
        task.last_applied_ts = event.timestamp
        task.provisional = event.state in TERMINAL
        return task

    # Rule 3: terminal event from non-terminal local state, but not in
    # REACHABLE (e.g. remote skipped a beat). Confirm before committing.
    if event.state in TERMINAL and task.state not in TERMINAL:
        try:
            confirmed = confirm(task.task_id)
        except NotImplementedError:
            raise WebhookContractViolation(
                f"cannot confirm terminal {event.state} for {task.task_id}"
            )
        if confirmed != event.state:
            raise WebhookContractViolation(
                f"webhook claims {event.state} for {task.task_id}, "
                f"tasks/get reports {confirmed}"
            )
        task.state = event.state
        task.last_applied_ts = event.timestamp
        task.provisional = True
        return task

    # Rule 4: unreachable and not confirmable -> escalate, never silently drop.
    # This is the failed-after-completed branch from the trace: dropping it
    # silently reverts a real failure to a false success.
    raise WebhookContractViolation(
        f"illegal transition {task.state}->{event.state} for {task.task_id} "
        f"at ts={event.timestamp}; requires reconciliation"
    )