# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-committed-task-disagreement-when-byzantine-agents-equivocate-across-mcp-tool-boundaries.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-committed-task-disagreement-when-byzantine-agents-equivocate-across-mcp-tool-boundaries.warrant.json
# Markdown SHA-256:   b4d58a43ef5f73a42b2c80b21a34873217651ffa41150391c6c284ae106881ee
# Code AST SHA-256:   821cf5dbe9c12078c6473ee9a81d3bf4fd98f296909722a447d11683e4add740
# Verified Classes:   CommitmentEquivocationError, CommitLedger
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class CommitmentEquivocationError(RuntimeError):
    """Raised when two disjoint quorum certificates target the same task_id."""
    pass


class CommitLedger:
    def __init__(self, quorum: int, fault_tolerance: int):
        self.quorum = quorum
        self.f = fault_tolerance
        self._committed: dict[str, frozenset[str]] = {}

    def commit(self, task_id: str, payload: str, cert_signers: frozenset[str]) -> None:
        if len(cert_signers) < self.quorum:
            raise CommitmentEquivocationError(
                f"cert for {task_id} has {len(cert_signers)} signers, need {self.quorum}"
            )
        prior = self._committed.get(task_id)
        if prior is not None:
            overlap = len(prior & cert_signers)
            # Two honest quorums must overlap by >= f+1 if no equivocation occurred.
            if overlap <= self.f:
                raise CommitmentEquivocationError(
                    f"equivocation on {task_id}: prior={sorted(prior)} "
                    f"new={sorted(cert_signers)} overlap={overlap} <= f={self.f}"
                )
            return  # idempotent re-commit by an overlapping honest quorum
        self._committed[task_id] = cert_signers


def handle_commit(ledger: CommitLedger, task_id: str, payload: str,
                  cert_signers: frozenset[str]) -> str:
    try:
        ledger.commit(task_id, payload, cert_signers)
    except CommitmentEquivocationError as e:
        # Surface to the swarm so the equivocating agent is identified and isolated.
        raise CommitmentEquivocationError(f"rejected commit: {e}") from e
    return f"committed {task_id}={payload}"
