Implement A2A task cancellation and verify the final state
Handle CancelTask per the A2A specification snapshot: confirm eligibility, read the response and errors, then verify the state with GetTask.
Prerequisites and input · Engineering checks · 1. Confirm task identity and routing
At a glance
This note follows the A2A specification snapshot read on 2026-11-12. The specification defines cancellation as a request: the server will attempt to cancel, but success is not guaranteed, because the task may already be completed or failed, or cancellation may be unsupported at its current stage. A caller therefore cannot treat one cancel call as proof the task stopped. The host handles the cancel response, the not-cancelable error and the later state separately, and records the outcome, including one that remains undecided.
Runtime and applicability
- Article type
- Engineering practice
- Verified
- Applies to
- A2A Implement A2A task cancellation and verify the final state
Execution locations and responsibilities
- A2A client and remote agent Use the workflow below to distinguish responsibilities of the caller, runtime, and external actions.
Arrows show the order of checks in the guide. Follow each step’s prerequisites, branches and stop conditions; unexecuted steps are not completed work.
When to use
- The terminal states are
TASK_STATE_COMPLETED,TASK_STATE_FAILED,TASK_STATE_CANCELEDandTASK_STATE_REJECTED; a task in one of them can no longer be cancelled. But not cancelable does not mean terminal: cancellation can also be refused because the current processing stage does not support it, and the task keeps running. The task ID must refer to an existing task or the result isTaskNotFoundError. Servers should not distinguish between does not exist and not authorized, so seeing NotFound does not prove the task was deleted.
When not to use
- The specification does not say how cancellation affects external actions the remote agent already triggered, such as rollback; do not promise rollback or undo in host messaging.
- The specification does not say how long a server keeps a task after cancellation, so
TaskNotFoundErrormay mean purged or may mean inaccessible. - Polling intervals and waiting limits are not part of the protocol; the host must choose and record them.
Prerequisites and input
- You hold a server-generated task ID taken from an earlier Task object; the specification does not support client-chosen task IDs for creating tasks.
- You have selected an interface from the Agent Card and know its
tenant(if any) and binding. - The caller is authorized for the task; a task the caller cannot access appears as nonexistent.
Engineering checks
Cancellation eligibility and ownership — The terminal states are TASK_STATE_COMPLETED, TASK_STATE_FAILED, TASK_STATE_CANCELED and TASK_STATE_REJECTED; a task in one of them can no longer be cancelled. But not cancelable does not mean terminal: cancellation can also be refused because the current processing stage does not support it, and the task keeps running. The task ID must refer to an existing task or the result is TaskNotFoundError. Servers should not distinguish between does not exist and not authorized, so seeing NotFound does not prove the task was deleted.
Request and response semantics — The cancel request has a required id and optional tenant and metadata. The methods are CancelTask for JSON-RPC and gRPC, and POST /tasks/{id}:cancel for HTTP+JSON. Success returns the updated Task. The specification says the operation attempts to cancel and returns the updated state; it does not promise the returned state is TASK_STATE_CANCELED, so the state has to be read, not assumed. Cancellation is idempotent, so repeated requests have the same effect, but once a cancelled task has been purged a repeat may return TaskNotFoundError.
Error mapping and downstream effects — TaskNotCancelableError is -32002 in JSON-RPC, a code used by that error alone; it is FAILED_PRECONDITION in gRPC and 400 in HTTP. Several A2A errors share FAILED_PRECONDITION and 400, so gRPC and HTTP must both read the reason of ErrorInfo (in status.details for gRPC, in the details array for HTTP), and only the value TASK_NOT_CANCELABLE identifies not cancelable. TaskNotFoundError maps to -32001, NOT_FOUND and 404. A terminal task no longer accepts messages, and sending one returns UnsupportedOperationError; calling SubscribeToTask on a terminal task returns the same error. A stream must close when the task reaches a terminal state.
1. Confirm task identity and routing
Where: the caller host's task registry. Input: the Task id from an earlier response and the selected interface's tenant. Action: verify the ID came from the server and that the request will carry a tenant matching the interface. Completion check: the registry has the ID, the interface, and the contextId when the Task carried one. Stop: the ID's origin is unknown or it was constructed by hand; do not send the cancel request.
2. Read the current state before cancelling
Where: the client, calling GetTask. Input: the task id and optionally historyLength. Action: read status.state and decide whether it is already terminal. Completion check: the host recorded the state, read time and an initial cancellation_attempted=false. Stop: the state is terminal, so skip cancelling and go to the last step. The read is only a snapshot and the state can still change afterwards, so cancel-time errors must still be handled.
3. Send the cancel request
Where: the call site for the chosen binding. Input: id and optional metadata. Action: set cancellation_attempted=true only when actually issuing the request, then call CancelTask (for HTTP+JSON, POST /tasks/{id}:cancel). Completion check: a Task response or a recognizable error comes back. Stop: authentication or authorization fails; do not retry until credentials are fixed. On a network timeout, check state with GetTask before sending the request again.
4. Interpret the cancel response
Where: the client's response handling. Input: the returned Task. Action: read status.state, separate already TASK_STATE_CANCELED, still running, and other states, and keep status.timestamp when present. Completion check: the host has an explicit branch for each state. Stop: if the state is not terminal, do not announce success and move to the last step to keep verifying; a body that violates the method contract can be reported as an InvalidAgentResponseError-type problem.
5. Handle not-cancelable and not-found errors
Where: the client's error mapping layer. Input: the error code, status and ErrorInfo.reason. Action: classify JSON-RPC -32002 directly as not cancelable; for gRPC and HTTP read ErrorInfo.reason first and classify as not cancelable only when it is TASK_NOT_CANCELABLE, never from FAILED_PRECONDITION or 400 alone; classify -32001, NOT_FOUND and 404 as nonexistent, purged or inaccessible. For a not-cancelable task, read the current state with GetTask and do not assume it is terminal. Completion check: the two error classes have different records, and a not-cancelable task carries its current state. Stop: after a not-cancelable error, do not loop on retries.
6. Verify the later state and clean up subscriptions
Where: host monitoring and cleanup. Input: actual Task state, cancellation_attempted, client streams and push configurations registered by this integration. On a terminal state record the observed result; at the deadline without one record unresolved. Both paths close this client’s observation streams. If notifications should stop, explicitly delete verified owned configurations intended for removal with DeleteTaskPushNotificationConfig and verify the result. If monitoring continues, assign an owner and record retained streams/configurations without claiming cleanup complete. Completion: retain outcome, attempted flag and disposition for streams and each owned configuration. Stop with “not attempted; already terminal” for a skipped cancel; only an attempted cancel followed by COMPLETED/FAILED records ineffective cancellation. Unreadable status remains unresolved and failed deletion remains a follow-up. Investigate unfamiliar configurations rather than deleting another subscriber’s setup.
Task example
This is a synthetic manual walkthrough; the observations are hypothetical expectations, not verified results. Suppose the host has a running report-generation task whose ID came from an earlier Task response. It first reads TASK_STATE_WORKING with GetTask, then sends CancelTask. Several branches are expected. The response already shows TASK_STATE_CANCELED, so the host closes the stream and records it. Or the response still shows TASK_STATE_WORKING, so the host reads again with GetTask until a terminal state or its own waiting limit. Or the task happens to finish in the meantime, cancellation returns a not-cancelable error, the host reads TASK_STATE_COMPLETED, and it records that the cancel had no effect. Or the task is refused because its current stage does not support cancellation, the same error comes back, GetTask still shows TASK_STATE_WORKING, and the host keeps monitoring and records undecided once its limit passes. These branches are derived from the specification text and have not been observed on any real service.
Completion checks
- Eligibility: the state was read before cancelling, and no terminal task was cancelled again.
- Response and error reading: the host decides from the returned
status.state, not from the call merely succeeding, and identifies not-cancelable errors over gRPC and HTTP byErrorInfo.reason. - Each observation has an actual terminal result or reasoned unresolved status, with cleanup results for client streams and owned push configurations or an assigned continued-monitoring owner. Unknown or undeleted configurations do not count as completed cleanup.
Verification scope
This note is based on the A2A specification snapshot read on 2026-11-12. Documentation was read only: no SDK was installed, no service was logged into, no API call was executed, and no real output exists. The branches in the walkthrough are hypothetical expectations that still need verification in a real environment.
Sources
All links are official pages; verification dates are listed per source, and links point to the version checked.