# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-roll-back-a-speculative-branch-that-was-already-canonically-committed-and-how-does-the-commit-shadow-leak-corrupt-sibling-reconci.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-roll-back-a-speculative-branch-that-was-already-canonically-committed-and-how-does-the-commit-shadow-leak-corrupt-sibling-reconci.warrant.json
# Markdown SHA-256:   b0364fa7f09df7b5b33bdbae1e64a96457777d2839658bf542cdf40a11ecee37
# Code AST SHA-256:   4caa7f9136ecfd1687804a42f47dfa8c72a6f722cb4d35e49c6a3501228c364b
# Verified Classes:   CommitShadowLeak, Artifact, Reconciler
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Dict, Set


class CommitShadowLeak(RuntimeError):
    """Raised when a commit's justification closure reaches a rolled-back artifact."""
    pass


@dataclass
class Artifact:
    id: str
    status: str                      # "speculative" | "committed" | "aborted"
    read_set: Set[str] = field(default_factory=set)   # artifact ids observed at read time
    committed: bool = False


class Reconciler:
    def __init__(self) -> None:
        self.artifacts: Dict[str, Artifact] = {}

    def register(self, art: Artifact) -> None:
        self.artifacts[art.id] = art

    def _closure(self, art_id: str, seen: Set[str]) -> Set[str]:
        if art_id in seen or art_id not in self.artifacts:
            return set()
        seen.add(art_id)
        out = {art_id}
        for dep in self.artifacts[art_id].read_set:
            out |= self._closure(dep, seen)
        return out

    def rollback(self, art_id: str) -> None:
        self.artifacts[art_id].status = "aborted"

    def commit(self, art_id: str) -> None:
        art = self.artifacts[art_id]
        # Invariant: no artifact in the justification closure may be aborted
        # or still speculative. Observed reads, not declared edges, define the closure.
        for dep_id in self._closure(art_id, set()):
            dep = self.artifacts[dep_id]
            if dep.status == "aborted":
                raise CommitShadowLeak(
                    f"commit of {art_id!r} depends (transitively) on aborted "
                    f"artifact {dep_id!r}; commit-shadow leak"
                )
            if dep.status == "speculative" and dep_id != art_id:
                raise CommitShadowLeak(
                    f"commit of {art_id!r} reads uncommitted speculative "
                    f"artifact {dep_id!r}; commit barrier violated"
                )
        art.status = "committed"
        art.committed = True


# Reproduction of the observed trace:
r = Reconciler()
r.register(Artifact("a1", "speculative"))
r.register(Artifact("b1", "speculative", read_set={"a1"}))
r.register(Artifact("c1", "speculative", read_set={"a1"}))

r.commit("b1")        # raises: b1 reads speculative a1 (commit barrier)
# If the barrier were absent, b1 would commit, then:
r.rollback("a1")
r.commit("b1")        # raises CommitShadowLeak: b1's closure hits aborted a1
