Expand description
Terminal states are final in the store, not only in the handler.
§Why the store has to enforce it
TaskState::can_transition_to already says a terminal state goes
nowhere, and the handler checks it — against the task it holds in
memory. With two writers that is not enough. Replica B cancels a task and
writes Canceled; replica A’s executor, which never heard of the cancel,
emits Completed; A checks Working → Completed against its own copy,
finds it legal, and writes. Before this module every shipped store applied
that write unconditionally, so the client B had told “canceled” was looking
at a task that went on to end Completed. tests/cross_replica_cancel.rs
reproduced it on the in-memory, SQLite and PostgreSQL stores.
The only place that sees both writes is the store, so the rule lives there,
and every shipped store applies it atomically — inside the write lock
for the in-memory stores, as a condition on the UPDATE / upsert for the
SQL ones — never as a read followed by a write, which would reopen the same
race one round trip wide.
§The rule
A write is refused when the stored task is terminal and the write carries a
different state (refuses_write). Every write path carries a state: a
whole-record save and each delta method act on a Task, and its
status.state is the writer’s belief about where the task is. So an
artifact delta from an executor that still believes Working is refused on
a task stored Canceled, exactly as its status write would be.
Re-writing the same terminal state is allowed. A local cancel reaches the
store twice — the handler’s own save, and the Canceled event the
executor’s cancel emits, persisted by the event processor — and refusing
the second would turn every ordinary cancel into a reported conflict. It
also leaves one thing open, deliberately: two writers that both reach the
same terminal state (two replicas that both admitted a continuation, see
horizontal-scaling.md) still race on the rest of the document. The state
they agree on cannot be lost; which artifacts land is last-writer-wins.
The interrupted states, InputRequired and AuthRequired, are not
sticky: a continuation moves them back to Working, which is their point.
§What the losing writer observes
An A2aError with code UnsupportedOperation — the code the
specification gives for an operation on a task in a terminal state
(§3.1.1, §3.1.2, §3.1.6) — carrying a TerminalStateConflict in its
data. A typed error rather than a silent no-op, because a no-op would
tell a caller its write landed when it did not; that is the exact lie this
module exists to remove. TerminalStateConflict::from_error recognises
it without matching on prose. The server’s own writers handle it: the event
processors adopt the stored state and stop, and CancelTask answers
TaskNotCancelable.
§Custom stores
A store outside this crate is not made sticky by any of this, and nothing
in the trait can force it to be. One that wants the guarantee applies
refuses_write inside its own atomic write, and reports a refusal with
TerminalStateConflict::into_error so the server recognises it.
Structs§
- Terminal
State Conflict - A write the store refused because the task it names is already terminal.
Constants§
- TERMINAL_
STATE_ CONFLICT_ MARKER - Key in
A2aError::datamarking a write refused because the stored task is already terminal. PreferTerminalStateConflict::from_errorover matching this string.
Functions§
- refuses_
write - Whether a write carrying
writtenmust be refused over a task stored instored.