# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-to-sign-a-plan-digest-equivocation-detection-under-asymmetric-quorum-observation-in-cross-org-agent-commitment.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-to-sign-a-plan-digest-equivocation-detection-under-asymmetric-quorum-observation-in-cross-org-agent-commitment.warrant.json
# Markdown SHA-256:   0abd84b125e8ff847a4b0d1d3dbba5caf9a703edd421779009177e1e6b283b18
# Code AST SHA-256:   0739dc993f21c2c72919649dcec1827a2029dc2b68448993c0e878d8fee82433
# Verified Classes:   QuorumObservabilityDeficit, EpochEquivocationAttempt, VoteView, CommitmentState
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import FrozenSet

class QuorumObservabilityDeficit(RuntimeError):
    """Raised when an instance cannot bound competing-quorum intersection from its own view."""
    pass

class EpochEquivocationAttempt(RuntimeError):
    """Raised when an instance is asked to sign a second, conflicting digest in one epoch."""
    pass

@dataclass(frozen=True)
class VoteView:
    epoch: str
    digest: str
    reachable_agents: FrozenSet[str]      # agents with gossip lag < epoch_window/2
    lagging_agents: FrozenSet[str]        # agents reachable but lagging
    unreachable_agents: FrozenSet[str]

@dataclass
class CommitmentState:
    quorum: int                            # q = 2f+1
    signed: dict = field(default_factory=dict)  # epoch -> digest already signed

    def sign_plan_digest(self, view: VoteView, self_id: str) -> dict:
        # Local equivocation guard: never sign two digests in one epoch.
        prior = self.signed.get(view.epoch)
        if prior is not None and prior != view.digest:
            raise EpochEquivocationAttempt(
                f"epoch {view.epoch}: already signed {prior[:12]}, "
                f"refusing to co-sign conflicting {view.digest[:12]}"
            )

        # Safety guard: observable set (incl. self) must cover the quorum,
        # else competing-quorum intersection is unbounded from this view.
        observable = view.reachable_agents | {self_id}
        if len(observable) < self.quorum:
            raise QuorumObservabilityDeficit(
                f"epoch {view.epoch}: observable={len(observable)} < quorum={self.quorum}; "
                f"lagging={sorted(view.lagging_agents)} unreachable={sorted(view.unreachable_agents)}; "
                f"cannot bound intersection of competing quorums — refusing to sign"
            )

        self.signed[view.epoch] = view.digest
        # Envelope carries the observed view so downstream verifiers can
        # reconstruct the decision, not merely the digest claim.
        return {
            "epoch": view.epoch,
            "digest": view.digest,
            "signer": self_id,
            "observed_vote_set": sorted(observable),
            "lagging": sorted(view.lagging_agents),
        }
