# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-that-a-speculative-branch-s-rollback-checkpoint-has-been-invalidated-by-a-sibling-branch-s-committed-side-effect.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-that-a-speculative-branch-s-rollback-checkpoint-has-been-invalidated-by-a-sibling-branch-s-committed-side-effect.warrant.json
# Markdown SHA-256:   462800c45376dea401d0c887320ada65dec4650b41bee0d5422dc2e4f47c223c
# Code AST SHA-256:   711cabd75d962ffefea237a6ede194e35f61b6b5050d6b0ec99abb36d82a586a
# Verified Classes:   ContaminatedCheckpointError
# Defensive Guards:   1 fail-closed invariants (zero external deps)

class ContaminatedCheckpointError(RuntimeError):
    """Raised when a rollback checkpoint's read-set intersects a sibling commit."""
    pass


def issue_rollback(branch, checkpoint, sibling_commit_log):
    """
    Restore `branch` to `checkpoint` only if no sibling commit since the
    checkpoint's timestamp touched the branch's read-set.
    """
    read_set = checkpoint.read_set_manifest  # resources B read since C
    if not read_set:
        # No reads recorded: rollback is write-only, safe to restore.
        checkpoint.restore()
        return "clean"

    contaminated = [
        commit for commit in sibling_commit_log
        if commit.timestamp > checkpoint.timestamp
        and commit.resource in read_set
        and commit.branch_id != branch.id
    ]
    if contaminated:
        # Do NOT restore. Escalate with the offending commits attached.
        raise ContaminatedCheckpointError(
            f"Rollback of branch {branch.id} to checkpoint {checkpoint.id} "
            f"is unsound: sibling commits touched read-set "
            f"{sorted({c.resource for c in contaminated})} "
            f"at timestamps {[c.timestamp for c in contaminated]}. "
            f"Re-fork from latest committed state or escalate to planner."
        )

    checkpoint.restore()
    return "clean"
