# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-equivocation-by-omission-in-a-byzantine-quorum-when-a-peer-commits-to-a-task-set-but-only-reports-the-subset-it-executed.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-equivocation-by-omission-in-a-byzantine-quorum-when-a-peer-commits-to-a-task-set-but-only-reports-the-subset-it-executed.warrant.json
# Markdown SHA-256:   3d4ea9575119849735b3081b839e6520db55b21a882ac0839ed9d6f6a02cd0d5
# Code AST SHA-256:   2afbc96c5297b3fb20003d8ac62187b0dcce6682c996a539beb56319a0b60cf6
# Verified Classes:   CoverageInvariantViolation, Manifest, Report
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import FrozenSet


class CoverageInvariantViolation(RuntimeError):
    """Raised when a peer's result report does not cover its committed manifest slots."""
    pass


@dataclass(frozen=True)
class Manifest:
    slots: FrozenSet[str]
    root: str  # Merkle root over sorted slot ids


@dataclass(frozen=True)
class Report:
    peer_id: str
    committed_root: str
    reported_slots: FrozenSet[str]
    reported_root: str
    signature_valid: bool


def verify_coverage(manifest: Manifest, report: Report) -> None:
    # Signature over the manifest proves commitment, not execution of every slot.
    if not report.signature_valid:
        raise CoverageInvariantViolation(
            f"peer {report.peer_id}: invalid signature over committed manifest"
        )
    if report.committed_root != manifest.root:
        raise CoverageInvariantViolation(
            f"peer {report.peer_id}: committed root {report.committed_root} "
            f"!= manifest root {manifest.root}"
        )
    # The load-bearing check: reported slot set must equal committed slot set.
    missing = manifest.slots - report.reported_slots
    extra = report.reported_slots - manifest.slots
    if missing or extra:
        raise CoverageInvariantViolation(
            f"peer {report.peer_id}: coverage mismatch — "
            f"missing={sorted(missing)} extra={sorted(extra)}"
        )
    if report.reported_root != manifest.root:
        raise CoverageInvariantViolation(
            f"peer {report.peer_id}: reported root {report.reported_root} "
            f"does not match committed root {manifest.root}"
        )


def tally_round(manifest: Manifest, reports: list[Report], f: int) -> dict[str, str]:
    accepted: dict[str, list[str]] = {slot: [] for slot in manifest.slots}
    faults = 0
    for r in reports:
        try:
            verify_coverage(manifest, r)
        except CoverageInvariantViolation:
            # Omission is Byzantine, not crash: the ledger must not record
            # this peer as having executed the missing slots.
            faults += 1
            if faults >= f + 1:
                raise CoverageInvariantViolation(
                    f"quorum lost: {faults} coverage violations exceed tolerance f={f}"
                )
            continue
        for slot in r.reported_slots:
            accepted[slot].append(r.peer_id)
    return {slot: voters[0] for slot, voters in accepted.items() if voters}
