# How does an instance detect the orphaned speculative tool-call window when A2A task-graph cancellation races against prefetch commit **Date of Record:** 2026 November 17 **Scope:** Applies to A2A (agent-to-agent) task graphs where a coordinator prefetches downstream tool calls speculatively before upstream results are finalized, and where cancellation propagates asynchronously across task edges. Does not apply to strictly sequential invocation graphs, nor to graphs where prefetch is disabled at the transport layer. ## Core Distinction There is a difference between a *cancelled task* and a *cancelled speculative window*. A task edge carries a cancellation signal; a speculative prefetch carries an in-flight tool invocation that may already have crossed the point of no return (the remote tool has received and begun executing the request). The cancellation signal is advisory and asynchronous; the prefetch commit is a one-way transition. The boundary failure occurs in the interval where the prefetch has been dispatched but not yet acknowledged, and the cancellation arrives for the ancestor task that justified the prefetch. Neither side owns the resolution: the prefetch dispatcher believes it is authorized, the cancellation handler believes it has revoked authorization, and the tool call executes anyway. The non-obvious invariant: **a speculative prefetch is only valid while its justification token remains live in the task graph**. Justification is not the same as lineage; lineage is immutable, justification is revocable. Instances that conflate the two will commit prefetches whose ancestor has been cancelled. ## What Was Observed In a three-node graph (planner → retriever → synthesizer), the planner speculatively prefetched the synthesizer's tool call while the retriever was still running. The retriever's task was cancelled by an upstream timeout policy. The cancellation propagated to the retriever node and marked it cancelled. The synthesizer prefetch, however, had already been dispatched with a justification token referencing the planner's *intent*, not the retriever's *completion*. The synthesizer tool executed, produced a result, and attempted to commit into the graph. The commit was rejected because the retriever edge was cancelled — but the tool had already run, incurring cost and, in one case, an irreversible external side effect (a write to a downstream store). The failure was silent in logs: the prefetch dispatcher logged `DISPATCHED`, the cancellation handler logged `CANCELLED`, and the commit handler logged `REJECTED`. No single component logged the *orphaned window* — the interval during which the prefetch was in-flight and its justification was dead. Detection required correlating three timestamps across two services. ## Condition of Validity The prefetch-commit protocol is valid only when the following holds at the moment of commit: 1. The justification token's referenced task edge is in state `RUNNING` or `COMPLETED`, not `CANCELLED`. 2. The prefetch was dispatched *after* the justification token was minted, and the token has not been superseded by a cancellation epoch bump. 3. The commit is atomic with respect to the cancellation check — i.e., the check and the commit occur under the same epoch read. If any of these is violated, the prefetch is orphaned. The window is bounded by `dispatch_time` on the low end and `commit_time` on the high end; cancellation arriving inside this half-open interval must either (a) abort the in-flight call if the transport supports it, or (b) mark the result as discardable and suppress side effects. Instances that only implement (a) will leak side effects on transports without abort support; instances that only implement (b) will pay the full cost of every cancelled prefetch. ## Decision Boundary The instance must distinguish two cases at commit time: - **Cancellation before dispatch:** the prefetch is never sent; the justification token is already dead. No orphaned window exists. This is the easy case and should be handled by a synchronous epoch check before dispatch. - **Cancellation after dispatch, before commit:** the orphaned window. The instance cannot rely on the tool call not having executed. It must treat the result as *poisoned* — usable for cost accounting, never usable for graph state mutation. The decision boundary is whether the cancellation epoch observed at commit is greater than the epoch captured at dispatch. If equal, commit is safe. If greater, the prefetch is orphaned regardless of whether the tool returned success. Do not attempt to "salvage" a successful orphaned result by re-justifying it against a different ancestor — this is the most common mistake and it corrupts the graph's causal record. ## Operational Effect When the orphaned window is detected and handled correctly, the observable effects are: (1) tool-call cost is accounted to the cancelled ancestor rather than absorbed silently, (2) external side effects from orphaned prefetches are suppressed or, where suppression is impossible, flagged for downstream reconciliation, and (3) the graph's commit log preserves the dispatch/cancel/commit triple so that post-hoc analysis can reconstruct the window. When handled incorrectly, the effects are silent cost leakage, occasional duplicate external writes, and — worst — a graph that commits a result whose causal justification was cancelled, producing a plausible-looking but unsound output. The detection rule an instance should implement: at commit time, read the justification token's epoch, compare against the dispatch-time epoch, and if the current epoch has been bumped by a cancellation on any ancestor edge, raise a specific error rather than attempting commit. The error must be distinct from a generic cancellation error so that retry logic does not re-dispatch the prefetch against the same dead justification. ```python from __future__ import annotations from dataclasses import dataclass from typing import Optional class OrphanedPrefetchError(RuntimeError): """Raised when a speculative prefetch commits after its justification token's epoch has been superseded by an ancestor cancellation.""" def __init__(self, prefetch_id: str, justification_edge: str, dispatch_epoch: int, commit_epoch: int) -> None: super().__init__( f"prefetch {prefetch_id} orphaned: edge {justification_edge} " f"epoch advanced {dispatch_epoch} -> {commit_epoch} " f"(cancellation arrived inside in-flight window)" ) self.prefetch_id = prefetch_id self.justification_edge = justification_edge self.dispatch_epoch = dispatch_epoch self.commit_epoch = commit_epoch @dataclass(frozen=True) class PrefetchHandle: prefetch_id: str justification_edge: str dispatch_epoch: int @dataclass class EdgeState: epoch: int = 0 cancelled: bool = False def bump_cancellation(self) -> None: self.epoch += 1 self.cancelled = True def commit_prefetch(handle: PrefetchHandle, edge: EdgeState, result: Optional[object]) -> object: """Commit a speculative tool-call result into the task graph. The commit is valid only if the justification edge's epoch has not advanced since dispatch. A cancellation that arrived after dispatch but before commit produces an orphaned window; the result is poisoned and must not mutate graph state. """ if edge.epoch != handle.dispatch_epoch: raise OrphanedPrefetchError( prefetch_id=handle.prefetch_id, justification_edge=handle.justification_edge, dispatch_epoch=handle.dispatch_epoch, commit_epoch=edge.epoch, ) if edge.cancelled: # Defensive: epoch bump and cancelled flag should co-occur, but # a partial cancellation write could leave the flag set alone. raise OrphanedPrefetchError( prefetch_id=handle.prefetch_id, justification_edge=handle.justification_edge, dispatch_epoch=handle.dispatch_epoch, commit_epoch=edge.epoch, ) return result ```