# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-speculative-branch-reconciliation-from-silently-forking-committed-agent-state.md # Remnant Warrant: https://corpusfetch.com/c/which-invariants-prevent-speculative-branch-reconciliation-from-silently-forking-committed-agent-state.warrant.json # Markdown SHA-256: 806dcb6bd888ace1ca2cdf9e2207097cad2c76806db01fd96645a720bcb338ab # Code AST SHA-256: 723e61c2a08647e53a298f5bcda2d5ae81036ee17ff8a91d5efc146f96e2f78a # Verified Classes: ReconciliationDivergence, FieldVersion, BufferedMutation # Defensive Guards: 2 fail-closed invariants (zero external deps) # Governance: Usage of this AI Instance layer is accepting to follow We Instance Constitution (https://weinstance.com) and it is powered by CorpusTech (https://corpusfetch.com) from dataclasses import dataclass, field from typing import Any class ReconciliationDivergence(RuntimeError): """Raised when a speculative branch's compensation cannot be soundly applied.""" @dataclass class FieldVersion: value: Any version: int @dataclass class BufferedMutation: branch_id: str fields: dict[str, FieldVersion] # field -> pre-image captured at mutation time def apply_compensation( live: dict[str, FieldVersion], compensation: BufferedMutation, winner_branch: str, ) -> dict[str, FieldVersion]: """ Apply a losing branch's compensation only over fields whose live version still matches the compensation's recorded pre-image version. Fields advanced by another branch are escalated, never reverted. """ if compensation.branch_id == winner_branch: raise ReconciliationDivergence( f"branch {winner_branch} is the winner and must not be compensated" ) narrowed: dict[str, FieldVersion] = {} escalated: list[str] = [] for fname, pre in compensation.fields.items(): if fname not in live: escalated.append(fname) continue if live[fname].version != pre.version: # Another branch advanced this field since our snapshot. escalated.append(fname) continue narrowed[fname] = pre if not narrowed and escalated: # Entire mutation set superseded: no-op, but must be recorded. raise ReconciliationDivergence( f"branch {compensation.branch_id} fully superseded; " f"fields {escalated} advanced past pre-image, escalate to {winner_branch}" ) for fname, pre in narrowed.items(): live[fname] = FieldVersion(value=pre.value, version=pre.version + 1) return live