# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-refuse-to-sign-a-commit-certificate-when-its-own-proposal-is-the-payload-self-attestation-equivocation-in-bft-agent-commitment.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-refuse-to-sign-a-commit-certificate-when-its-own-proposal-is-the-payload-self-attestation-equivocation-in-bft-agent-commitment.warrant.json
# Markdown SHA-256:   ecdf5e1e9607c0476d57d253c20729c136808a684873662c7753ff31df9bc786
# Code AST SHA-256:   e71dc780b7df676bcbecc409a7863280461fd2aedd7ee9d34e43758ce39ba353
# Verified Classes:   SelfAttestationCertificateError, Vote, Certificate
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class SelfAttestationCertificateError(RuntimeError):
    """Raised when a commit certificate counts the proposer's own vote
    toward the safety threshold without independent evidence provenance."""
    pass


class Vote:
    def __init__(self, voter_id: str, payload_hash: str, evidence_source: str):
        self.voter_id = voter_id
        self.payload_hash = payload_hash
        self.evidence_source = evidence_source  # e.g. "local_build", "replica_state", "oracle"


class Certificate:
    def __init__(self, proposer_id: str, payload_hash: str, votes: list[Vote], f: int):
        self.proposer_id = proposer_id
        self.payload_hash = payload_hash
        self.votes = votes
        self.f = f


def verify_commit_certificate(cert: Certificate) -> bool:
    # 1. All votes must be for the same payload.
    for v in cert.votes:
        if v.payload_hash != cert.payload_hash:
            raise SelfAttestationCertificateError(
                f"vote from {v.voter_id} is for a different payload hash"
            )

    # 2. Segregate the proposer's self-vote from the safety threshold.
    self_votes = [v for v in cert.votes if v.voter_id == cert.proposer_id]
    independent = [v for v in cert.votes if v.voter_id != cert.proposer_id]

    # 3. A self-vote is admissible only as intent, and only if its evidence
    #    source is NOT the proposer's own construction path.
    for sv in self_votes:
        if sv.evidence_source == "local_build":
            raise SelfAttestationCertificateError(
                f"proposer {cert.proposer_id} self-attested with tautological evidence "
                f"(local_build); vote proves nothing about payload safety"
            )

    # 4. Threshold must be met by independent votes alone: 2f+1.
    threshold = 2 * cert.f + 1
    if len(independent) < threshold:
        raise SelfAttestationCertificateError(
            f"independent quorum {len(independent)} < threshold {threshold}; "
            f"self-votes ({len(self_votes)}) cannot inflate the certificate"
        )

    # 5. Evidence provenance must not collapse to a single construction path.
    sources = {v.evidence_source for v in independent}
    if sources == {"local_build"}:
        raise SelfAttestationCertificateError(
            "all independent votes share the proposer's construction path; "
            "no causal independence"
        )

    return True


def certify_commit(proposer_id: str, payload_hash: str, votes: list[Vote], f: int) -> bool:
    cert = Certificate(proposer_id, payload_hash, votes, f)
    return verify_commit_certificate(cert)
