# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-draw-the-attestation-quorum-boundary-when-a-byzantine-auditor-s-signed-rejection-is-itself-the-equivocation-artifact.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-draw-the-attestation-quorum-boundary-when-a-byzantine-auditor-s-signed-rejection-is-itself-the-equivocation-artifact.warrant.json
# Markdown SHA-256:   eb45586b3debf52ac0a9da5ad600c3bc78db460b698c26ceda90014bff7d7077
# Code AST SHA-256:   dbd44254691f54d4cee86b551f054d27d2eaa0b02df32c4c4751503fe5d83dd8
# Verified Classes:   AttestationQuorumError, NonReplayableAttestation, Vote, RoundResult
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional

class AttestationQuorumError(RuntimeError):
    """Raised when a commitment round cannot reach a typed quorum."""
    pass

class NonReplayableAttestation(AttestationQuorumError):
    """A negative attestation lacked a replayable observed-hash chain."""
    pass

@dataclass(frozen=True)
class Vote:
    auditor: str
    artifact_hash: str
    executed: bool
    observed_hash: Optional[str]
    comparison_result: Optional[bool]
    replayable: bool
    signature_valid: bool

@dataclass(frozen=True)
class RoundResult:
    outcome: str  # "COMMIT" | "REJECT" | "INCONCLUSIVE"
    accept: int
    reject: int
    abstain: int
    byzantine_signals: tuple

def tally(votes, f: int) -> RoundResult:
    quorum = 2 * f + 1
    accept = reject = abstain = 0
    byz = []
    for v in votes:
        if not v.signature_valid:
            byz.append(v.auditor)
            continue
        if not v.executed:
            abstain += 1
            continue
        if v.comparison_result is True:
            accept += 1
            continue
        # comparison_result is False: candidate negative attestation
        if v.observed_hash is None or v.observed_hash == v.artifact_hash:
            # self-contradictory or empty negative -> Byzantine fault signal
            byz.append(v.auditor)
            continue
        if not v.replayable:
            # cannot be independently re-checked -> abstention, NOT rejection
            abstain += 1
            continue
        reject += 1

    if accept >= quorum:
        return RoundResult("COMMIT", accept, reject, abstain, tuple(byz))
    if reject >= quorum:
        return RoundResult("REJECT", accept, reject, abstain, tuple(byz))
    if abstain + (len(votes) - accept - reject - abstain) > f:
        # Q2: too many abstentions/missing -> inconclusive, re-dispatch
        return RoundResult("INCONCLUSIVE", accept, reject, abstain, tuple(byz))
    raise AttestationQuorumError(
        f"round neither committed nor rejected nor inconclusive: "
        f"accept={accept} reject={reject} abstain={abstain} f={f}"
    )

def seal_round(votes, f: int, late_votes=()) -> RoundResult:
    result = tally(votes, f)
    if result.outcome == "REJECT" and late_votes:
        # Q3: replay late votes; if outcome flips, void the seal
        replay = tally(tuple(votes) + tuple(late_votes), f)
        if replay.outcome != "REJECT":
            raise NonReplayableAttestation(
                "seal voided: late vote would have changed outcome; "
                "reopen round instead of committing rejection"
            )
    return result
