# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-to-count-a-signed-attestation-as-a-quorum-vote-when-the-endorsing-agent-s-model-revision-postdates-the-commi.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-to-count-a-signed-attestation-as-a-quorum-vote-when-the-endorsing-agent-s-model-revision-postdates-the-commi.warrant.json
# Markdown SHA-256:   af19db782231fc3588858d210281c422bda4d7ea48b06cc3d702b73c432e9a8d
# Code AST SHA-256:   cc114f9af9a387908c31c3a1a85722b41e37ea2a8c3334188bfede55319a2291
# Verified Classes:   RevisionBindingError, Commitment, Attestation
# Defensive Guards:   3 fail-closed invariants (zero external deps)

import hashlib
from dataclasses import dataclass


class RevisionBindingError(RuntimeError):
    """Raised when an attestation cannot be counted as a quorum vote
    because its execution revision is not bound to the commitment epoch."""


@dataclass(frozen=True)
class Commitment:
    epoch_id: str
    model_revision: str
    prompt_hash: str
    tool_manifest_digest: str
    roster: frozenset  # set of signer ids


@dataclass(frozen=True)
class Attestation:
    signer: str
    epoch_id: str
    claimed_revision: str   # self-reported by the agent
    measured_revision: str  # signed measurement from the runtime
    prompt_hash: str
    tool_manifest_digest: str
    result_digest: str
    signature_valid: bool


def classify_vote(att: Attestation, com: Commitment) -> str:
    """Return one of: 'vote', 'fault', 'quarantine'.
    'vote'       -> counts toward quorum q
    'fault'      -> counts toward Byzantine budget f
    'quarantine' -> counts toward neither; triggers epoch decision
    """
    if not att.signature_valid:
        return "fault"
    if att.signer not in com.roster:
        return "fault"
    if att.epoch_id != com.epoch_id:
        return "fault"  # cross-epoch replay

    # Revision binding is the non-obvious gate.
    if att.measured_revision != com.model_revision:
        if att.claimed_revision == com.model_revision:
            # Claims the committed revision but runtime measurement disagrees.
            # Indistinguishable from Byzantine equivocation.
            return "fault"
        # Honest report of a foreign revision: non-participant, not faulty.
        return "quarantine"

    if att.prompt_hash != com.prompt_hash:
        return "fault"
    if att.tool_manifest_digest != com.tool_manifest_digest:
        return "fault"
    return "vote"


def tally(atts, com: Commitment, q: int, f_budget: int) -> str:
    votes, faults, quarantined = 0, 0, 0
    for att in atts:
        c = classify_vote(att, com)
        if c == "vote":
            votes += 1
        elif c == "fault":
            faults += 1
        else:
            quarantined += 1

    if faults > f_budget:
        raise RevisionBindingError(
            f"Byzantine budget exceeded: {faults} faults > f={f_budget}"
        )
    if votes >= q:
        return "committed"
    if quarantined > 0 and (votes + quarantined) >= q:
        raise RevisionBindingError(
            f"Ghost quorum risk: {votes} votes + {quarantined} quarantined "
            f"would reach q={q} but quarantined agents are not bound to "
            f"revision {com.model_revision}; open a new epoch"
        )
    return "pending"


def commit_result(atts, com: Commitment, q: int, f_budget: int) -> str:
    status = tally(atts, com, q, f_budget)
    if status != "committed":
        raise RevisionBindingError(
            f"cannot commit epoch {com.epoch_id}: status={status}"
        )
    return status
