# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-bound-equivocation-in-byzantine-task-commitment-consensus-when-a-signer-s-warrant-and-its-vote-diverge.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-bound-equivocation-in-byzantine-task-commitment-consensus-when-a-signer-s-warrant-and-its-vote-diverge.warrant.json
# Markdown SHA-256:   403885a77c4cb380b8e3dce4bd676ed5965edb8d07402fb5e23e633ecdf05df6
# Code AST SHA-256:   9700ae4344ff535ba7e243956cdde07741560ce972dad92a58b9fa0e0f303ebe
# Verified Classes:   WarrantVoteDivergence, Scope, Warrant, Vote
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class WarrantVoteDivergence(RuntimeError):
    """Raised when a signed vote's scope is not entailed by its attached warrant."""
    pass


@dataclass(frozen=True)
class Scope:
    op: str
    target: str


@dataclass(frozen=True)
class Warrant:
    warrant_id: str
    scope: Scope
    parent_hash: str


@dataclass(frozen=True)
class Vote:
    signer: str
    scope: Scope
    warrant: Warrant
    causal_parent_hash: str
    signature_valid: bool


def scope_entails(warrant_scope: Scope, vote_scope: Scope) -> bool:
    return warrant_scope.op == vote_scope.op and warrant_scope.target == vote_scope.target


def admit_vote(vote: Vote, expected_parent_hash: str) -> bool:
    if not vote.signature_valid:
        raise WarrantVoteDivergence(
            f"signer {vote.signer}: signature invalid; refuse before scope check"
        )
    if vote.causal_parent_hash != expected_parent_hash:
        raise WarrantVoteDivergence(
            f"signer {vote.signer}: vote causal parent {vote.causal_parent_hash} "
            f"!= expected {expected_parent_hash}; warrant may have been narrowed after checkpoint"
        )
    if vote.warrant.parent_hash != vote.causal_parent_hash:
        raise WarrantVoteDivergence(
            f"signer {vote.signer}: warrant parent {vote.warrant.parent_hash} "
            f"does not bind to vote causal parent {vote.causal_parent_hash}"
        )
    if not scope_entails(vote.warrant.scope, vote.scope):
        raise WarrantVoteDivergence(
            f"signer {vote.signer}: warrant {vote.warrant.warrant_id} scope "
            f"{vote.warrant.scope} does not entail vote scope {vote.scope}"
        )
    return True


def tally_quorum(votes: list[Vote], expected_parent_hash: str, threshold: int) -> int:
    admitted = 0
    for v in votes:
        try:
            admit_vote(v, expected_parent_hash)
            admitted += 1
        except WarrantVoteDivergence as exc:
            # Loud reject: record divergence, do not count toward quorum.
            print(f"REJECTED: {exc}")
    if admitted < threshold:
        raise WarrantVoteDivergence(
            f"quorum not reached: {admitted} admitted < {threshold}; "
            f"task commitment blocked pending signer re-sync"
        )
    return admitted
