# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-s-hallucinated-vote-is-indistinguishable-from-a-genuine-minority-report-in-bft-llm-consensus.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-s-hallucinated-vote-is-indistinguishable-from-a-genuine-minority-report-in-bft-llm-consensus.warrant.json
# Markdown SHA-256:   402fedb7727c84e79a4df52113a1e4e07171717cc3b9256455da908266ddc1b8
# Code AST SHA-256:   e9305bea67de14fbbae274b286dd6f15c6d0a8b822250bbe32a87583832302d4
# Verified Classes:   CommitmentClass, HallucinatedQuorumError, Vote, Commitment
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from enum import Enum


class CommitmentClass(Enum):
    REVERSIBLE = "reversible"
    IRREVERSIBLE = "irreversible"


class HallucinatedQuorumError(RuntimeError):
    """Raised when an irreversible commitment would be sealed on quorum alone
    while a minority dissent carries an unresolved, machine-checkable artifact."""


@dataclass
class Vote:
    agent_id: str
    value: str
    artifact_ref: str | None   # content-addressed span / trace id, or None
    confidence: float          # NEVER used as evidence; logged only


@dataclass
class Commitment:
    key: str
    cls: CommitmentClass
    votes: list[Vote]
    quorum_threshold: int
    f: int


def resolve_artifact(artifact_ref: str, key: str) -> bool:
    # Independent auditor resolves the span against the content-addressed blob.
    # Returns True if the artifact genuinely supports the voter's value.
    ...


def seal_commitment(c: Commitment) -> str:
    tally: dict[str, int] = {}
    for v in c.votes:
        tally[v.value] = tally.get(v.value, 0) + 1
    winner, count = max(tally.items(), key=lambda kv: kv[1])

    dissenters = [v for v in c.votes if v.value != winner]

    if c.cls is CommitmentClass.IRREVERSIBLE and dissenters:
        for d in dissenters:
            if d.artifact_ref is None:
                # Unverifiable dissent on an irreversible commitment:
                # quarantine, do not seal, do not silently discard.
                raise HallucinatedQuorumError(
                    f"agent {d.agent_id} dissents on irreversible key={c.key} "
                    f"with no resolvable artifact; refusing quorum seal"
                )
            if resolve_artifact(d.artifact_ref, c.key):
                # Minority was right; majority must re-retrieve.
                raise HallucinatedQuorumError(
                    f"agent {d.agent_id} dissent VERIFIED on key={c.key}; "
                    f"majority value '{winner}' is refuted, forcing re-round"
                )
        # All dissents resolved as refuted -> safe to seal.
        return winner

    if count < c.quorum_threshold:
        raise HallucinatedQuorumError(
            f"quorum not reached on key={c.key}: {count}/{c.quorum_threshold}"
        )
    return winner
