# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-of-a-speculative-branch-be-treated-as-uncommitted-the-read-after-rollback-visibility-boundary-in-parallel-multi-agent-execution.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-of-a-speculative-branch-be-treated-as-uncommitted-the-read-after-rollback-visibility-boundary-in-parallel-multi-agent-execution.warrant.json
# Markdown SHA-256:   931c39019e23c6b20b8bb17dc7200059dd07a4220f6098ea17ab2b6f172f50a2
# Code AST SHA-256:   0431d1e2202980059816fa778544c514e38916502a83ce28c6f7b556520fc58f
# Verified Classes:   GhostDerivationError, ReadRecord, Branch
# Defensive Guards:   1 fail-closed invariants (zero external deps)

import hashlib
from dataclasses import dataclass, field
from typing import Optional


class GhostDerivationError(RuntimeError):
    """Raised when a branch to be committed depends on a rolled-back branch's writes."""


@dataclass
class ReadRecord:
    key: str
    producer_branch: str
    producer_status: str  # "COMMITTED" | "SPECULATIVE" | "ROLLED_BACK"


@dataclass
class Branch:
    name: str
    reads: list = field(default_factory=list)
    status: str = "SPECULATIVE"


def commit_branch(branch: Branch, status_of: dict) -> None:
    """Arbiter-side closure check before materializing a branch."""
    ghosts = []
    for r in branch.reads:
        current = status_of.get(r.producer_branch, "UNKNOWN")
        if current == "ROLLED_BACK":
            ghosts.append((r.key, r.producer_branch))
    if ghosts:
        raise GhostDerivationError(
            f"branch {branch.name!r} reads {ghosts} from rolled-back producers; "
            f"refusing commit — derivation is not closed under committed facts"
        )
    branch.status = "COMMITTED"


# Demonstration of the boundary failure being caught.
status = {"A": "ROLLED_BACK", "B": "SPECULATIVE"}
b = Branch("B", reads=[ReadRecord("f2", "A", "SPECULATIVE")])
try:
    commit_branch(b, status)
except GhostDerivationError as e:
    print("blocked:", e)
