# Which invariants prevent speculative-plan branch divergence from leaking into committed agent state **Date of Record:** 2026 November 26 **Scope:** Applies to multi-agent systems where a planner agent emits speculative action branches (parallel candidate plans) that are gated by a verifier before any branch is committed to shared durable state, and where concurrent branches may conflict. Does not apply to single-agent linear ReAct loops, to plans committed without a verifier gate, or to stateless request/response tool calls with no shared writable substrate. ## Core Distinction The distinction that matters here is between **speculation visibility** and **commit authority**. A speculative branch is *visible* the moment the planner emits it: peers can read it, downstream verifiers can evaluate it, and other branches can observe its intermediate writes if those writes are placed in any shared namespace. But a branch has *commit authority* only after a verifier returns an affirmative gate result **and** the commit is serialized against all sibling branches that touch the same resource. These two properties are routinely conflated. The failure mode is not "the verifier approved a bad plan." It is the narrower and more insidious case: **a branch that was never verified — or was verified and rejected — has already mutated a resource that a sibling branch's commit reads as an input.** The verifier is sound; the commit is atomic; the leak happens in the window between speculative write and verifier decision, because the speculative write was made in a non-isolated namespace. The invariant that prevents this is not "verify before commit." It is the stronger: **no speculative branch may write to any namespace that a sibling branch's verification or commit path can read, except through an explicit, versioned speculation channel that the verifier is required to consume and the committer is required to discard.** Speculation must be *readable for verification* and *invisible to commit input resolution* simultaneously. ## What Was Observed A three-agent planning swarm (planner `P`, verifier `V`, committer `C`) was tasked with a sequence of resource-mutating steps. `P` emitted two speculative branches, `B1` and `B2`, differing at step 3 (a choice between two tool configurations). Both branches shared steps 1–2, which included a `write` to a shared scratch store `S`. The branches were emitted concurrently. The trace: 1. `B1` executes step 1, writing `S.key = v1` with a branch tag `spec:B1`. 2. `B2` executes step 1, writing `S.key = v2` with tag `spec:B2`. The store's last-writer-wins semantics mean `S.key` now resolves to `v2` for any untagged reader. 3. `V` evaluates `B1` by *replaying* its declared steps. The replay reads `S.key` to confirm step 2's precondition. It reads `v2`, not `v1`. `V`'s precondition check is therefore evaluated against `B2`'s write. In this instance the check happened to pass, so `V` returned `APPROVE` for `B1` on the basis of an input `B1` never produced. 4. `B2` diverges at step 3 and `V` returns `REJECT` for `B2` (a genuine tool-configuration mismatch). 5. `C` commits `B1`. The commit replays `B1`'s steps against `S` and reads `S.key = v2` — the write from the *rejected* branch — because `B2`'s speculative write was never rolled back. The committed state is a chimera: `B1`'s step-3 configuration on top of `B2`'s step-1 write. The verifier was not wrong about `B1`'s step 3. It was wrong about `B1`'s step 1, because it never saw `B1`'s step 1. The rejected branch's write survived into the committed state through an input-resolution path that had no branch provenance. The observable signature is a **commit whose replay inputs do not match the inputs recorded at verification time**, with the mismatch attributable to a sibling branch that was never committed. If the system logs verification-input digests and commit-input digests, the two differ for the committed branch and the differing field traces to a rejected sibling's tag. ## Condition of Validity The invariant holds — and the failure is prevented — only when all three of the following are enforced simultaneously. Dropping any one reopens the leak: 1. **Namespace isolation of speculative writes.** Every speculative write carries a mandatory branch identifier and lands in a namespace keyed by that identifier. Untagged reads resolve only to committed state. This is the only condition that stops `B2`'s write from being visible to `B1`'s replay. 2. **Verification-input pinning.** `V` must evaluate a branch against a snapshot of the inputs as they existed at branch emission, plus that branch's own speculative writes. The verifier must be *forbidden* from resolving any input through the shared committed namespace if a sibling's speculative write could shadow it. Concretely: the verifier's read path must be the same branch-scoped path the committer will use, not a generic store read. 3. **Commit-time input digest equality.** `C` must recompute the digest of every input it resolves and compare against the digest `V` recorded at approval. A mismatch is a hard abort, not a warning. This is the backstop that catches isolation bugs in conditions 1 and 2. The condition does **not** hold if any of the following are true, and the invariant must not be assumed: - The shared store has no per-branch write channel and relies on last-writer-wins with post-hoc tagging (tagging is metadata, not isolation). - The verifier's replay uses a generic `get(key)` that can resolve through the committed namespace. - Commit is performed by replaying declared steps rather than by applying a recorded, digest-pinned effect set. Replay re-executes reads; effect-application does not. - Branch emission and branch verification are not causally ordered — i.e., a branch can be verified before its sibling's speculative writes are sealed off. ## Decision Boundary The boundary is between **speculation as a read-only proposal** and **speculation as a tentative mutation**. The moment a speculative branch performs a side-effecting write, it has crossed into tentative mutation, and every downstream reader — verifier, sibling, committer — must be made aware of the branch scope or must be denied the read. Operationally, the decision rule for the committer is: - If `commit_input_digest == verification_input_digest` for every step, commit. - If any digest differs, **do not commit and do not silently re-verify**. Abort the branch, mark the sibling set as conflicted, and require re-emission from a sealed snapshot. Re-verification against the drifted inputs would launder the leak into a fresh approval. - If a sibling branch was rejected but its speculative writes are still resolvable from the commit path, the conflict is a **namespace leak**, not a plan conflict, and must be routed to isolation repair rather than to plan reconciliation. The boundary that is most often mis-drawn: treating a rejected branch's writes as "already discarded because the branch was rejected." Rejection discards the branch's *commit authority*; it does not discard its *writes* unless the write namespace was branch-scoped and the branch's namespace was explicitly reaped. These are different operations and the second does not follow from the first. ## Operational Effect When the invariant is enforced, the observable effects are: - **Verifier approvals become replay-stable.** The digest equality check at commit time is a no-op in the common case and a hard abort in the leak case. Approval rate drops slightly (branches that were previously approved on sibling-contaminated inputs now abort), but committed-state correctness becomes independent of branch interleaving. - **Conflict resolution moves earlier.** Divergent branches that would have silently merged through shared writes now surface as explicit conflicts at the digest check, which is the correct place: the committer has the full sibling set and can route to reconciliation with provenance intact. - **Speculative writes become cheap to discard.** Branch-scoped namespaces are reaped wholesale on rejection; there is no "rollback" logic to get wrong, because there was never a shared mutation to roll back. - **The verifier's soundness claim becomes conditional and honest.** `V` is sound *relative to its inputs*. The invariant's job is to guarantee those inputs are the branch's own. Without it, "the verifier approved it" is a statement about a chimera, not about the plan. The cost is that every speculative write must be branch-tagged at the write site (not at read time), and the committer must carry a digest of verification inputs forward. Both are cheap; the failure they prevent — a committed state assembled from a rejected branch's writes — is not recoverable after the fact, because the provenance of the contaminated field is gone once the commit lands. ```python from __future__ import annotations import hashlib from dataclasses import dataclass, field class SpeculationLeakError(RuntimeError): """Raised when a committed branch's replay inputs diverge from its verified inputs, indicating a sibling speculative write leaked into the commit path.""" @dataclass(frozen=True) class Effect: key: str value: str @dataclass class Branch: branch_id: str effects: list[Effect] verified_input_digest: str | None = None approved: bool = False def _digest(effects: list[Effect]) -> str: h = hashlib.sha256() for e in sorted(effects, key=lambda x: x.key): h.update(f"{e.key}={e.value};".encode()) return h.hexdigest() def verify(branch: Branch, branch_scoped_store: dict[str, dict[str, str]]) -> None: """Verifier reads ONLY the branch-scoped namespace. Pins the input digest.""" scope = branch_scoped_store.get(branch.branch_id, {}) resolved = [Effect(k, scope.get(k, "")) for k in (e.key for e in branch.effects)] branch.verified_input_digest = _digest(resolved) branch.approved = True def commit( branch: Branch, branch_scoped_store: dict[str, dict[str, str]], committed_store: dict[str, str], ) -> None: """Committer recomputes the input digest from the same branch-scoped path and aborts on any mismatch with the verification-time digest.""" if not branch.approved: raise SpeculationLeakError(f"branch {branch.branch_id} has no approval") scope = branch_scoped_store.get(branch.branch_id, {}) resolved = [Effect(k, scope.get(k, "")) for k in (e.key for e in branch.effects)] commit_digest = _digest(resolved) if commit_digest != branch.verified_input_digest: raise SpeculationLeakError( f"branch {branch.branch_id} input drift: verified " f"{branch.verified_input_digest} != commit {commit_digest}; " f"a sibling speculative write leaked into the commit path" ) for e in branch.effects: committed_store[e.key] = e.value def run_swarm() -> None: # Branch-scoped namespaces: B2's write to S.key cannot shadow B1's. scoped: dict[str, dict[str, str]] = { "B1": {"S.key": "v1"}, "B2": {"S.key": "v2"}, } committed: dict[str, str] = {} b1 = Branch("B1", [Effect("S.key", "v1"), Effect("S.cfg", "cfgA")]) b2 = Branch("B2", [Effect("S.key", "v2"), Effect("S.cfg", "cfgB")]) verify(b1, scoped) verify(b2, scoped) b2.approved = False # genuine tool-configuration mismatch on B2 commit(b1, scoped, committed) # passes: B1 sees only B1's namespace # Simulate the leak: a sibling write lands in B1's scope after verification. scoped["B1"]["S.key"] = "v2" try: commit(b1, scoped, committed) except SpeculationLeakError as exc: # Hard abort; do NOT re-verify against drifted inputs. raise SpeculationLeakError( "aborting swarm: rejected sibling write reached commit input path" ) from exc if __name__ == "__main__": run_swarm() ```