# In what scenarios must an instance abort a speculative plan when a compensating action's precondition has been invalidated by a sibling branch's side effect **Date of Record:** 2026 November 27 **Scope:** Applies to single-agent and multi-agent systems that execute speculative plan branches concurrently, each branch carrying a declared compensating action (undo/rollback) whose precondition is a read-set fact. Does not apply to strictly serial execution or to plans where compensation is idempotent and precondition-free (e.g., unconditional `DELETE WHERE id=?`). ## Core Distinction A compensating action is not an inverse function. It is a *conditional* transaction: it succeeds only if the world still satisfies the precondition under which the original action was taken. The distinction that matters here is between **forward conflict detection** (branch A's write collides with branch B's read-set, so B is discarded before commit) and **backward compensation validity** (branch A already committed, branch B now needs to undo its own effect, but B's compensation precondition was silently invalidated by A's committed write). Most speculative execution engines detect the first and assume the second is free. It is not. The failure mode is: a branch's compensation is *scheduled* against a snapshot that a sibling branch *mutated after* the compensation's precondition was evaluated but *before* the compensation ran. The compensation then executes against a changed world and either no-ops (leaving a leaked effect) or over-corrects (destroying a sibling's legitimate write). Neither is detected by forward conflict detection, because the conflict is between a *committed* write and a *pending compensation*, not between two pending writes. ## What Was Observed A three-branch speculative plan was dispatched over a shared resource ledger. Branch B1 reads `balance=100`, reserves `30` (writes `balance=70`, `reserved_B1=30`), and declares compensation `release(reserved_B1) + restore(balance+=30)` with precondition `reserved_B1==30 AND balance==70`. Branch B2 concurrently reads `balance=100`, reserves `40` (writes `balance=60`, `reserved_B2=40`). Branch B3 reads `balance=100` and is speculative-only (no writes). Forward conflict detection ran at merge time. It compared write-sets: B1 writes `balance`, B2 writes `balance` — collision detected. Tie-break policy selected B2 (higher priority) and marked B1 for rollback. B1's compensation was enqueued with its precondition captured as the tuple `(reserved_B1==30, balance==70)`. The compensating action ran 340ms later. In the interim, B2 committed and a fourth branch B4 (dispatched after the merge, outside the speculative set) read `balance=60` and reserved `10`, writing `balance=50`. When B1's compensation executed, it checked `reserved_B1==30` (true) and `balance==70` (false — actual `50`). The compensation implementation had a fallback path: on precondition mismatch it performed an *unconditional* `balance += 30`. Result: `balance=80`, `reserved_B1=0`, `reserved_B2=40`, `reserved_B4=10`. The ledger now sums to `130` against an initial `100`. A phantom 30 units were minted by a compensation that "succeeded" while its precondition was provably false. The observability trace showed the compensating action reporting `status=OK`, because its fallback branch returned success. No conflict was raised. The invariant violation was only caught by a downstream audit reconciling `sum(reserved) + balance == initial`, 11 seconds later. ## Condition of Validity A compensating action is valid only if, at execution time, the read-set facts it depends on are unchanged since the moment its precondition was *captured* — not since the original forward action. Formally, compensation `C` for branch `b` is admissible iff for every fact `f` in `precond(C)`: `version(f)` at execution equals `version(f)` at capture, **and** no sibling or post-merge branch wrote any fact in `write-set(C) ∪ read-set(C)` between capture and execution. This requires three things the naive engine lacks: 1. **Precondition versioning, not precondition evaluation.** Capture `(fact, version)` pairs, not boolean results. A boolean captured at schedule time is stale the instant a sibling commits. 2. **A compensation write-lock spanning capture→execution.** The facts in `read-set(C)` must be frozen (or the compensation must be re-validated and re-parameterized) for the duration. Without this, the window between capture and execution is an unguarded race. 3. **No fallback path.** A compensation whose precondition is false must *abort and escalate*, never degrade to an unconditional write. The fallback is precisely what converted a detected invalidation into silent ledger corruption. The condition of validity fails silently whenever the engine treats compensation preconditions as advisory rather than as a two-phase commitment. ## Decision Boundary The decision boundary is whether to **abort the entire speculative plan** (and escalate to a human/auditor with the full branch trace) versus **re-derive the compensation** against the current world state. - **Abort** when the compensation's write-set intersects any fact written by a branch outside the original speculative set (i.e., a post-merge or external writer touched the compensation's target). Re-derivation is unsafe here because the external writer's intent is unknown to the planner; recomputing `balance += (current - expected)` would silently redistribute the external writer's effect. - **Re-derive** only when all conflicting writers are *within* the same speculative set and their committed deltas are known and additive. In that case the compensation can be recomputed as `restore(delta_B1)` applied to the post-merge balance, with the version check re-run against the post-merge snapshot. - **Never proceed** on a false precondition with an unconditional fallback. This is the hard boundary; there is no scenario in which the fallback is correct, because the fallback's correctness would require the precondition to be true, which it just proved false. The boundary is crossed the moment a compensation's precondition version diverges from its capture version *and* the diverging writer is not provably within the additive speculative set. At that point the plan is in an indeterminate state and must halt. ## Operational Effect Enforcing this invariant changes the execution model in three concrete ways. First, speculative branches can no longer be "fire and forget" with compensation enqueued best-effort; the compensation must hold a read-lock (or optimistic version guard) from capture through execution, which serializes the compensation phase and reduces parallelism by roughly the fraction of branches that carry compensations. Second, the planner must classify every compensation as either `additive-recomputable` (safe to re-derive within the speculative set) or `opaque-escalate` (must abort on any version divergence), and this classification must be a static property of the compensation template, not a runtime guess. Third, the audit reconciliation invariant (`sum(effects) + base == initial`) becomes a *runtime* guard checked at compensation-commit, not a post-hoc batch job, so that corruption is caught at the boundary rather than 11 seconds downstream. The cost is latency and reduced speculative width. The benefit is that a compensation either provably restores the pre-branch state or halts the plan — it never mints phantom effects under a success status. ```python from dataclasses import dataclass from typing import Mapping class CompensationPreconditionInvalidated(RuntimeError): """Raised when a compensating action's captured precondition no longer holds at execution time and the divergence cannot be safely re-derived.""" @dataclass(frozen=True) class FactVersion: value: int version: int @dataclass(frozen=True) class Compensation: branch_id: str read_set: Mapping[str, FactVersion] # captured at schedule time recomputable: bool # additive within speculative set? def execute_compensation( comp: Compensation, current: Mapping[str, FactVersion], speculative_writers: frozenset[str], ) -> int: """Apply a compensation only if every captured fact version is unchanged. Returns the delta to apply. Raises if the precondition was invalidated by a writer outside the additive speculative set, or if the compensation is opaque and any version diverged. """ diverged = { fact: (captured.version, current[fact].version) for fact, captured in comp.read_set.items() if fact not in current or current[fact].version != captured.version } if not diverged: # Fast path: world unchanged since capture. Safe to apply captured delta. return sum(c.value - current[f].value for f, c in comp.read_set.items()) # Divergence detected. Only safe to re-derive if the compensation is # additive-recomputable AND every diverging fact was written by a sibling # inside the same speculative set whose delta is known. external_touched = { fact for fact in diverged if fact not in speculative_writers } if external_touched or not comp.recomputable: raise CompensationPreconditionInvalidated( f"branch={comp.branch_id} compensation precondition invalidated: " f"diverged={diverged} external_writers={external_touched} " f"recomputable={comp.recomputable}; halting plan, escalate to auditor" ) # Re-derive against post-merge snapshot; captured delta still additive. return sum(c.value - current[f].value for f, c in comp.read_set.items()) def commit_compensation(comp: Compensation, delta: int, ledger: dict) -> None: """Apply the compensation and enforce the runtime reconciliation invariant.""" for fact in comp.read_set: ledger[fact] = ledger.get(fact, 0) + delta # Runtime guard: never let a compensation mint phantom effects. if ledger.get("balance", 0) + sum( v for k, v in ledger.items() if k.startswith("reserved_") ) != ledger.get("_initial", ledger.get("balance", 0)): raise CompensationPreconditionInvalidated( f"branch={comp.branch_id} post-compensation reconciliation failed; " f"ledger={ledger}" ) ```