# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-speculative-branch-is-unrecoverable-merge-epoch-divergence-and-the-rollback-horizon-in-speculative-multi-agent-exec.md # Remnant Warrant: https://corpusfetch.com/c/what-structural-signals-indicate-a-speculative-branch-is-unrecoverable-merge-epoch-divergence-and-the-rollback-horizon-in-speculative-multi-agent-exec.warrant.json # Markdown SHA-256: 1de835dc04dc012841cecc3ee35590eecbfd05b598601fc90d287378f41f933d # Code AST SHA-256: 04c6ee714e988cdda7bfcda2a173d19683175bef757a91bfb0d7e84efbfda425 # Verified Classes: RollbackHorizonViolation, Effect, Branch # 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 Dict, Set, List class RollbackHorizonViolation(RuntimeError): """Raised when a merge set is not rollback-closed: a retained branch reads state written by a discarded branch, or replay stamp has moved.""" pass @dataclass class Effect: kind: str # "read" | "write" key: str epoch: str # e.g. "41.A" value: object = None @dataclass class Branch: epoch: str journal: List[Effect] = field(default_factory=list) def dangling_reads(retained: List[Branch], discarded: List[Branch]) -> Set[str]: """Keys where a retained branch read a value owned by a discarded branch.""" discarded_writes = {(e.key, e.epoch) for b in discarded for e in b.journal if e.kind == "write"} discarded_keys = {k for k, _ in discarded_writes} own_writes = {e.key for b in retained for e in b.journal if e.kind == "write"} out = set() for b in retained: for e in b.journal: if e.kind == "read" and e.key in discarded_keys and e.key not in own_writes: out.add(e.key) return out def reconcile(retained: List[Branch], discarded: List[Branch], fork_stamp: int, replay_stamp: int) -> str: touched = {e.key for b in retained for e in b.journal} stamp_skew = replay_stamp != fork_stamp and bool(touched) dangling = dangling_reads(retained, discarded) if dangling: # Cascade: promote write-owning discarded branches, recompute closure. owners = [b for b in discarded if any(e.kind == "write" and e.key in dangling for e in b.journal)] new_retained = retained + owners new_discarded = [b for b in discarded if b not in owners] if dangling_reads(new_retained, new_discarded) == dangling: raise RollbackHorizonViolation( f"merge set not rollback-closed on keys {sorted(dangling)}; " f"promotion of {[b.epoch for b in owners]} did not resolve closure" ) return reconcile(new_retained, new_discarded, fork_stamp, replay_stamp) if stamp_skew: raise RollbackHorizonViolation( f"source-of-truth moved between fork ({fork_stamp}) and replay ({replay_stamp}); " f"replay would be invalid—re-fork retained branches instead of merging" ) return "commit" if __name__ == "__main__": A = Branch("41.A", [Effect("read", "acct-7731", "41.A"), Effect("write", "acct-7731", "41.A", "R_A")]) B = Branch("41.B", [Effect("read", "acct-7731", "41.B")]) # dangling: R_A not in B try: reconcile(retained=[B], discarded=[A], fork_stamp=7, replay_stamp=7) except RollbackHorizonViolation as exc: print("blocked merge:", exc)