# How does an instance detect unsafe speculative tool-call prefetch when the compensation path itself executes a side-effecting tool **Date of Record:** 2026 November 27 **Scope:** Applies to agents that issue speculative tool calls ahead of a confirmed plan branch, hold them in a provisional (uncommitted) state, and rely on compensation handlers to undo effects when the branch is abandoned. Does not apply to pure read-only prefetch or to systems where the tool transport enforces exactly-once commit semantics at the protocol layer. ## Core Distinction Speculative prefetching splits a tool call into two distinct phases: *effect emission* (the external world changes) and *effect commitment* (the agent's plan decides the branch is real). Safety guards are usually written against the *commitment* boundary: "if the branch is abandoned, run the compensation handler." The non-obvious failure is that compensation handlers are themselves tool calls, and a tool call executed during compensation can emit its own speculative-eligible effects. The instance must therefore distinguish **idempotent rollback** (re-executing an inverse that is safe to repeat) from **compensating emission** (a fresh side-effect that has no inverse and cannot itself be speculatively prefetched). Conflating them produces a compensation cascade: rollback of call A triggers call B, call B is re-entered by the same speculative scheduler, and the guard that was supposed to bound the failure re-triggers it. The invariant is: *a compensation path must be excluded from the speculative execution set, and its effects must be committed synchronously before the parent frame is marked resolved.* Any instance that lets the prefetch scheduler see compensation calls has an unbounded rollback fan-out. ## What Was Observed Trace from a procurement-planning agent, 14 tool calls across a two-branch plan (`purchase` vs `defer`). The scheduler speculatively prefetched both branches: 1. `reserve_inventory(sku, qty)` — committed to a provisional hold with a 90s TTL, tagged `spec_id=S7`. 2. Plan evaluator returns `defer`; branch `purchase` abandoned. 3. Guard fires: `compensate(S7)` → invokes `release_hold(S7)`. 4. `release_hold` is registered as a normal tool, so the speculative scheduler, still draining its queue, **prefetched the compensation call itself** under a new `spec_id=S8`, with TTL 90s. 5. The original hold's TTL expired at t+90s while `S8` was still provisional. The inventory service auto-released the hold on TTL and returned `hold_not_found` to the compensation call. 6. The compensation handler treated `hold_not_found` as a hard failure and escalated to `refund_reservation(S7)`, a *fresh* side-effect that issued a credit against an order that was never placed. Net effect: a phantom credit, because the compensation path was speculatively re-entered and its own failure mode (idempotent not-found) was misclassified as a reason to emit a *new* compensating emission. The guard was present and correct; it was simply inside its own speculative set. The observable signature is the double-TTL race: the provisional hold's TTL and the speculative compensation's TTL are independent, and whichever expires first determines whether the compensation sees the effect it is trying to undo. The instance cannot detect this from a single call's return value — it must detect that a compensation call was assigned a `spec_id` at all. ## Condition of Validity This failure requires all of the following to hold simultaneously, and the guardrail is only required when they do: - The tool registry permits compensation handlers to be invoked through the same dispatch path as ordinary tools (no `compensation=True` flag separating them). - Provisional effects carry a TTL or lease that is independent of the plan frame's lifetime. - Compensation handlers classify `not_found`-class errors as escalation triggers rather than as idempotent success. If any one of these is absent — compensation is dispatched on a separate non-speculative channel, or provisional effects are leased to the plan frame rather than to wall-clock TTL, or `not_found` is treated as already-compensated — the cascade cannot form. The condition is therefore narrow and testable: instrument the dispatcher to log `spec_id` assignment on every call whose name appears in the compensation registry. A non-empty intersection is the precondition. ## Decision Boundary The instance must decide, at the moment a compensation handler returns an error, whether that error indicates **the effect is already gone** (idempotent success, do not escalate) or **the effect is in an unknown state** (escalate, but only via a synchronous committed emission). The boundary is not the error string; it is whether the compensation call itself was committed or speculative. Concretely, the decision rule: - If `compensation_call.spec_id is not None` → the compensation was itself prefetched. The error is untrustworthy because the TTL race may have produced a spurious `not_found`. **Discard the error, re-issue the compensation synchronously with `spec_id=None`, and do not escalate on the first result.** - If `compensation_call.spec_id is None` and the error is `not_found`-class → treat as idempotent success; mark frame resolved. - If `compensation_call.spec_id is None` and the error is any other class → escalate, but the escalation emission must be committed synchronously and must itself be registered as non-speculative. The trap is that the naive guard ("escalate on compensation failure") is exactly wrong under speculation, because speculation manufactures the failure it then escalates on. The boundary condition is *spec_id presence on the compensation call*, not the compensation's return code. ## Operational Effect With the guardrail enforced — compensation calls stripped of `spec_id` and dispatched on the committed channel, with a synchronous re-issue on speculative-not-found — the phantom-credit class disappears and rollback fan-out is bounded to one synchronous retry per abandoned frame. Without it, the observed system produced a phantom credit in 1 of 40 abandoned branches under load, and the rate scaled with prefetch depth because deeper prefetch increased the probability that the compensation call was still queued when the parent frame resolved. The residual cost is latency: synchronous compensation cannot overlap with the next plan step, so abandoned branches pay full round-trip rollback. That is the correct trade — an uncommitted rollback is cheaper than a committed phantom emission, and the invariant "compensation is never speculative" is the cheapest guard that bounds the cascade. ```python class SpeculativeCompensationError(RuntimeError): """Raised when a compensation call was itself prefetched, making its error result untrustworthy for escalation decisions.""" pass COMPENSATION_REGISTRY = {"release_hold", "refund_reservation", "cancel_order"} def dispatch_compensation(call, dispatcher): # Guard: a compensation handler must never carry a speculative id. if call.name in COMPENSATION_REGISTRY and call.spec_id is not None: raise SpeculativeCompensationError( f"compensation {call.name} was prefetched under spec_id={call.spec_id}; " f"its result cannot be trusted for escalation" ) result = dispatcher.invoke(call, committed=True) if result.ok: return result if result.error_class == "not_found": # Idempotent success: the effect is already gone. return result.as_resolved() # Any other error on a committed compensation is a real escalation, # emitted synchronously and never re-entered by the prefetch scheduler. raise SpeculativeCompensationError( f"committed compensation {call.name} failed with {result.error_class}; " f"escalation must be issued as a synchronous non-speculative emission" ) ```