# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-distinguish-verified-speculative-branches-from-merely-corroborated-ones-in-multi-agent-branch-and-verify-commit.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-distinguish-verified-speculative-branches-from-merely-corroborated-ones-in-multi-agent-branch-and-verify-commit.warrant.json
# Markdown SHA-256:   1ad2644a02e6dd10ae1a96a29f4b656975d749c3ed59d3cb8038bc0ef67fff6b
# Code AST SHA-256:   72a07eeca7f8a50a87437ebe4d3c8aa2c74c527de810ddaceae106043745195b
# Verified Classes:   EvidenceClass, CommitBoundaryError, Branch
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from enum import Enum


class EvidenceClass(Enum):
    VERIFIED = "verified"            # checked by held-out oracle, drafter cannot influence
    CORROBORATED_ONLY = "corroborated_only"  # agreement among (possibly correlated) drafts


class CommitBoundaryError(RuntimeError):
    """Raised when a branch is promoted to commit without independent verification."""


@dataclass
class Branch:
    branch_id: str
    drafts: list[str]
    drafter_contexts: list[str]   # shared prefix hash per drafter
    drafter_tools: list[frozenset]
    verifier_passed: bool | None  # None => no independent verifier ran


def drafts_are_decorrelated(branch: Branch) -> bool:
    # Decorrelated only if no two drafters share context prefix AND tool surface.
    for i in range(len(branch.drafts)):
        for j in range(i + 1, len(branch.drafts)):
            if (branch.drafter_contexts[i] == branch.drafter_contexts[j]
                    and branch.drafter_tools[i] == branch.drafter_tools[j]):
                return False
    return True


def admit_to_commit(branch: Branch) -> EvidenceClass:
    unanimous = len(set(branch.drafts)) == 1 and len(branch.drafts) > 1

    if branch.verifier_passed is True:
        return EvidenceClass.VERIFIED

    if branch.verifier_passed is False:
        raise CommitBoundaryError(
            f"branch {branch.branch_id}: independent verifier rejected branch; "
            f"rollback required regardless of draft agreement"
        )

    # No independent verifier ran. Agreement cannot substitute for verification
    # unless decorrelation is demonstrated.
    if unanimous and drafts_are_decorrelated(branch):
        return EvidenceClass.CORROBORATED_ONLY  # still not VERIFIED; stage only

    if unanimous and not drafts_are_decorrelated(branch):
        raise CommitBoundaryError(
            f"branch {branch.branch_id}: unanimous agreement among correlated "
            f"drafters (shared prefix/tools) treated as verification; this is the "
            f"corroboration-as-verification boundary failure"
        )

    return EvidenceClass.CORROBORATED_ONLY
