# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-byzantine-equivocation-in-multi-agent-result-attestation-the-split-view-commit-hazard-when-an-auditor-quorum-is-satis.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-byzantine-equivocation-in-multi-agent-result-attestation-the-split-view-commit-hazard-when-an-auditor-quorum-is-satis.warrant.json
# Markdown SHA-256:   baad74052f476f1bb112e46b91b757667a30afd9114ffce6b60a531b0102bc74
# Code AST SHA-256:   4489d41485e4a8d97a5d64a59d40a43c08e873db544d2835ef4341998565bed1
# Verified Classes:   SplitViewEquivocationError, AttestationLedger
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Dict, Set


class SplitViewEquivocationError(RuntimeError):
    """Raised when two payload hashes for one task each reach quorum
    with an intersecting signer set, or when an auditor is asked to
    re-attest a different hash for a task it already signed."""
    pass


@dataclass
class AttestationLedger:
    threshold: int
    # task_id -> hash -> set of auditor ids that signed that hash
    signed: Dict[str, Dict[str, Set[str]]] = field(default_factory=dict)
    # (task_id, auditor_id) -> hash, to detect re-attestation
    auditor_binding: Dict[tuple, str] = field(default_factory=dict)

    def attest(self, task_id: str, payload_hash: str, auditor_id: str) -> None:
        prior = self.auditor_binding.get((task_id, auditor_id))
        if prior is not None and prior != payload_hash:
            # Auditor would be signing two hashes for one task: equivocation vector.
            raise SplitViewEquivocationError(
                f"auditor {auditor_id} already attested {prior!r} for task "
                f"{task_id!r}; refusing re-attestation of {payload_hash!r}"
            )
        self.auditor_binding[(task_id, auditor_id)] = payload_hash
        self.signed.setdefault(task_id, {}).setdefault(payload_hash, set()).add(auditor_id)

    def commit(self, task_id: str) -> str:
        hashes = self.signed.get(task_id, {})
        quorum_hashes = {h for h, signers in hashes.items() if len(signers) >= self.threshold}

        if len(quorum_hashes) > 1:
            # Double-quorum: check for intersecting signer sets.
            ordered = list(quorum_hashes)
            for i in range(len(ordered)):
                for j in range(i + 1, len(ordered)):
                    h1, h2 = ordered[i], ordered[j]
                    overlap = hashes[h1] & hashes[h2]
                    if overlap:
                        raise SplitViewEquivocationError(
                            f"task {task_id!r}: hashes {h1!r} and {h2!r} both reached "
                            f"quorum with intersecting signers {sorted(overlap)}; "
                            f"split-view equivocation, commit halted"
                        )
            raise SplitViewEquivocationError(
                f"task {task_id!r}: two disjoint quorums reached; "
                f"payload non-determinism must be reconciled before commit"
            )

        if not quorum_hashes:
            raise SplitViewEquivocationError(
                f"task {task_id!r}: no hash reached threshold {self.threshold}"
            )
        return next(iter(quorum_hashes))
