# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reject-a-verifier-gated-speculative-commit-when-replay-of-a-divergent-branch-produces-a-coincidentally-matching-dig.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reject-a-verifier-gated-speculative-commit-when-replay-of-a-divergent-branch-produces-a-coincidentally-matching-dig.warrant.json
# Markdown SHA-256:   7c4eda7a3873d1791178ca9093c17801dedb8b427281dbd69acadc8a9e2cb8e3
# Code AST SHA-256:   63a5beec31b35ca1f84bed65bdf7320b58aae453ed250ae592a3a4db2d65561f
# Verified Classes:   SpeculativeCommitRejected, BranchLineage, TraceRecord
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass


class SpeculativeCommitRejected(RuntimeError):
    """Raised when digest equality is insufficient to authorize a commit."""
    pass


@dataclass(frozen=True)
class BranchLineage:
    branch_id: str
    checkpoint_id: str
    fork_step: int
    seed: int


@dataclass(frozen=True)
class TraceRecord:
    lineage: BranchLineage
    trace_digest: str
    post_state_digest: str
    final_value: float


def authorize_commit(
    committed: TraceRecord,
    replayed: TraceRecord,
    sibling_digests: list[str],
    canonicalizer_injective: bool,
) -> None:
    # 1. Digest equality is necessary but not sufficient.
    if replayed.trace_digest != committed.trace_digest:
        raise SpeculativeCommitRejected(
            f"trace digest mismatch: {replayed.trace_digest} != {committed.trace_digest}"
        )

    # 2. Provenance must match. A digest match under mismatched lineage is a
    #    coincidence signal, not a confirmation signal.
    if replayed.lineage != committed.lineage:
        raise SpeculativeCommitRejected(
            "digest matched but lineage diverged: "
            f"committed={committed.lineage} replayed={replayed.lineage} "
            "-> coincidental digest, escalate to full replay"
        )

    # 3. Sibling collision proves the canonicalizer is non-injective on this
    #    fork set; invalidate the whole speculative batch.
    if any(d == committed.trace_digest for d in sibling_digests):
        raise SpeculativeCommitRejected(
            "sibling branch shares committed digest: canonicalizer non-injective "
            "on fork set, demote to full replay with provenance assertion"
        )

    # 4. Post-state must be bound into the digest, or order-sensitivity leaks.
    if not canonicalizer_injective:
        raise SpeculativeCommitRejected(
            "canonicalizer not verified injective; post-state divergence possible "
            "despite trace digest equality"
        )

    if replayed.post_state_digest != committed.post_state_digest:
        raise SpeculativeCommitRejected(
            "post-state digest mismatch: committed state cannot be reconstructed"
        )

    # Only here is the commit authorized.
    return None
