# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-stop-trusting-a-quorum-equivocation-induced-safety-collapse-at-the-f-1-boundary-in-multi-agent-task-commitment.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-stop-trusting-a-quorum-equivocation-induced-safety-collapse-at-the-f-1-boundary-in-multi-agent-task-commitment.warrant.json
# Markdown SHA-256:   37d99afa86900af120c648ed3f03507e347541265d21fb85451fd372cbcec50a
# Code AST SHA-256:   d769c355df644efa2707ea010c9a22d8b63345e46eb24c150650c060c5cf07cf
# Verified Classes:   EquivocationDetected, CommitmentGate
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class EquivocationDetected(RuntimeError):
    """Raised when f+1 inconsistent signed statements are observed from a replica set
    within a single view, proving equivocation and forbidding commitment."""
    pass


class CommitmentGate:
    def __init__(self, n: int, f: int):
        self.n = n
        self.f = f
        self.inconsistencies: dict[int, set[bytes]] = {}  # replica_id -> signed values
        self.decided: set[bytes] = set()

    def observe_vote(self, replica_id: int, signed_value: bytes) -> None:
        """Record a signed vote. Raises if f+1 inconsistencies are reached."""
        seen = self.inconsistencies.setdefault(replica_id, set())
        seen.add(signed_value)
        if len(seen) >= 2:
            # count how many replicas are now provably equivocating
            equivocating = sum(1 for v in self.inconsistencies.values() if len(v) >= 2)
            if equivocating >= self.f + 1:
                raise EquivocationDetected(
                    f"{equivocating} replicas equivocating (threshold {self.f + 1}); "
                    f"replica {replica_id} signed {len(seen)} distinct values in one view"
                )

    def may_act(self, value: bytes) -> bool:
        """Side effects are unlocked ONLY by a decision, never by a certificate."""
        return value in self.decided

    def record_decision(self, value: bytes) -> None:
        self.decided.add(value)


def commit_task(gate: CommitmentGate, task_value: bytes, certificate: bytes) -> None:
    """Attempt to act on a task. Refuses unless the value is DECIDED, not merely certified."""
    if not gate.may_act(task_value):
        raise EquivocationDetected(
            f"refusing to act on certified-but-undecided value {task_value!r}; "
            f"certificate {certificate!r} is a vote, not a fact"
        )
    # ... perform side effects (resource allocation, shared-state write) ...
