# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-refuse-to-reconcile-a-rolled-back-branch-the-speculative-commit-ordering-invariant-in-parallel-multi-agent-rollouts.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-refuse-to-reconcile-a-rolled-back-branch-the-speculative-commit-ordering-invariant-in-parallel-multi-agent-rollouts.warrant.json
# Markdown SHA-256:   b1c1d1dd5de64c741b3c7a67843f5b445f9642ab2095f227392fd73af6750deb
# Code AST SHA-256:   a20da72816fae3bf608f511f561435bb3883f7d2f7b505007910a4eca511a005
# Verified Classes:   ReconciliationOrderingError, Branch
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import FrozenSet


class ReconciliationOrderingError(RuntimeError):
    """Raised when a branch is about to be committed or rolled back
    without proven read-set/write-set disjointness against siblings."""
    pass


@dataclass(frozen=True)
class Branch:
    name: str
    verifier_passed: bool
    read_set: FrozenSet[str]
    write_set: FrozenSet[str]          # includes rollback write-set
    commit_eligible: bool = field(default=False)


def reconcile(branches: list[Branch]) -> list[str]:
    committed: list[str] = []
    for b in branches:
        if not b.commit_eligible:
            continue
        siblings = [s for s in branches if s.name != b.name]
        intersects = any(b.read_set & s.write_set for s in siblings)
        if intersects:
            # Boundary: cannot commit on a local verdict alone.
            raise ReconciliationOrderingError(
                f"branch {b.name!r} read-set intersects sibling write-set; "
                f"re-verify against post-reconciliation substrate before commit"
            )
        committed.append(b.name)
    return committed


def apply_rollback(branch: Branch, siblings: list[Branch]) -> None:
    for s in siblings:
        if branch.write_set & s.read_set:
            raise ReconciliationOrderingError(
                f"rollback of {branch.name!r} would clobber sibling {s.name!r} "
                f"read-set; serialize siblings first"
            )
    # ... apply snapshot restore only after disjointness proven ...
