# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-silent-equivocation-in-byzantine-multi-agent-task-commitment.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-silent-equivocation-in-byzantine-multi-agent-task-commitment.warrant.json
# Markdown SHA-256:   4842fd6029ac5d788e94ec59c30f1758125d2ca056efca9a7391266f5db4263a
# Code AST SHA-256:   49e5bb7922810f49291cd741c53e308b686ed7955875cdc377317bf0c73574f4
# Verified Classes:   EquivocationError, Attestation, AttestationLog
# Defensive Guards:   3 fail-closed invariants (zero external deps)

import hashlib
from dataclasses import dataclass
from typing import Optional


class EquivocationError(RuntimeError):
    """Raised when an agent emits conflicting attestations for the same task slot."""
    pass


@dataclass(frozen=True)
class Attestation:
    agent_id: str
    slot_id: str
    payload_hash: str
    nonce: int


class AttestationLog:
    def __init__(self) -> None:
        self._log: dict[tuple[str, str], Attestation] = {}

    def check_and_record(self, att: Attestation) -> None:
        key = (att.slot_id, att.agent_id)
        prior = self._log.get(key)
        if prior is None:
            self._log[key] = att
            return
        if prior.nonce != att.nonce:
            raise EquivocationError(
                f"nonce regression for slot {att.slot_id} agent {att.agent_id}: "
                f"{prior.nonce} -> {att.nonce}"
            )
        if prior.payload_hash != att.payload_hash:
            raise EquivocationError(
                f"equivocation detected for slot {att.slot_id} agent {att.agent_id}: "
                f"{prior.payload_hash} vs {att.payload_hash}"
            )
        # idempotent re-delivery


def dispatch_task(log: AttestationLog, att: Attestation, payload: bytes) -> None:
    expected = hashlib.sha256(payload).hexdigest()
    if att.payload_hash != expected:
        raise EquivocationError(
            f"payload hash mismatch for slot {att.slot_id}: "
            f"attested {att.payload_hash} != actual {expected}"
        )
    log.check_and_record(att)
    # dispatch only after non-equivocation check passes
    _execute(att, payload)


def _execute(att: Attestation, payload: bytes) -> None:
    pass
