Principle:A2aproject A2a java Task State Machine
| Knowledge Sources | |
|---|---|
| Domains | Protocol_Design, State_Machines |
| Last Updated | 2026-10-29 00:00 GMT |
Overview
A finite state machine governing the lifecycle of A2A tasks through well-defined state transitions, with terminal states that close event streams and signal completion to all subscribers.
Description
The Task State Machine defines the set of states a task can occupy during its lifecycle and the valid transitions between them. In the A2A Protocol, every message exchange that involves task creation follows this state machine: the task progresses from initial submission through working states to a final terminal state.
The problem this solves is lifecycle coherence: without a formal state machine, agents might produce ambiguous states (e.g., "is it done or still working?"), clients couldn't know when to stop listening, and event streams would have no natural termination point. The state machine provides a shared vocabulary for task progress and defines exactly which states are terminal (and thus close event streams).
In the broader landscape, this is a classic finite state machine (FSM) pattern, comparable to HTTP request states, database transaction states, or Kubernetes Pod phases. The key design choice is that terminal states (COMPLETED, FAILED, CANCELED, REJECTED) are absorbing states — once entered, the task cannot transition out, and the event queue is closed.
The A2A SDK enforces terminal state safety via an `AtomicBoolean` in `AgentEmitter.updateStatus()`: a `compareAndSet` guard ensures only one terminal event is ever enqueued, preventing duplicate completion or failure events.
Usage
Use this principle when:
- Implementing an agent executor — use `AgentEmitter` methods to drive state transitions
- Processing task events on the client — pattern-match on `TaskState` to handle each state
- Designing stream cleanup logic — terminal states close the event queue automatically
- Building resubscription logic — check task state before subscribing to avoid subscribing to completed tasks
Theoretical Basis
The task state machine defines these states:
States:
SUBMITTED — task created, not yet started
WORKING — agent is actively processing
INPUT_REQUIRED — agent needs user input to continue (non-terminal)
AUTH_REQUIRED — agent needs authentication (non-terminal)
COMPLETED — task finished successfully (TERMINAL)
FAILED — task failed (TERMINAL)
CANCELED — task was canceled by client (TERMINAL)
REJECTED — task was rejected by agent (TERMINAL)
Valid transitions:
SUBMITTED → WORKING → COMPLETED
SUBMITTED → WORKING → FAILED
SUBMITTED → WORKING → CANCELED
WORKING → INPUT_REQUIRED → WORKING (resume after input)
WORKING → AUTH_REQUIRED → WORKING (resume after auth)
WORKING → REJECTED
Any non-terminal → CANCELED (via cancel request)
Terminal state safety:
// AgentEmitter.updateStatus() guard
if (isFinal && !terminalStateReached.compareAndSet(false, true)):
throw IllegalStateException("Terminal state already reached")
// Only one terminal event can be enqueued per task
// This prevents race conditions in concurrent agent execution
Practical Guide
To drive task state transitions as an agent:
- Call `emitter.submit()` to create a task in SUBMITTED state (only if `context.getTask() == null`)
- Call `emitter.startWork()` to transition to WORKING
- Call `emitter.addArtifact()` to stream results while in WORKING
- Call `emitter.complete()` / `emitter.fail()` / `emitter.cancel()` to reach a terminal state
- Call `emitter.requiresInput()` or `emitter.requiresAuth()` for non-terminal interruptions
To handle task states as a client:
- In your event consumer, pattern-match on `TaskUpdateEvent.getTask().status().state()`
- Terminal states end the event stream — no more events will arrive after a terminal event
Related Pages
Implemented By
- Implementation:A2aproject_A2a_java_AgentEmitter
- Implementation:A2aproject_A2a_java_ClientEvent_Pattern
- Implementation:A2aproject_A2a_java_RequestContext
- Implementation:A2aproject_A2a_java_TaskStore_SPI
- Implementation:A2aproject_A2a_java_ResultAggregator