# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-equivocation-by-a-peer-agent-when-the-signed-commitment-quorum-is-only-partially-observable.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-equivocation-by-a-peer-agent-when-the-signed-commitment-quorum-is-only-partially-observable.warrant.json
# Markdown SHA-256:   046b2903d42830dcc2bf2eb6d39c00b7de4a8e87859b613547c89186b0ccc7d1
# Code AST SHA-256:   57b25a9b5158238a9c529b78b2287c128066bd65660f17bcbe7ec37ace6f5aa0
# Verified Classes:   EquivocationUndetectableError, ProvisionalCommitment
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class EquivocationUndetectableError(RuntimeError):
    """Raised when an agent must finalize a commitment but its local view
    cannot exclude equivocation by a Byzantine peer within the proof horizon."""
    pass


class ProvisionalCommitment:
    def __init__(self, subtask_id, term, signatures, witnesses,
                 trusted_honest, f, proof_horizon_views):
        self.subtask_id = subtask_id
        self.term = term
        self.signatures = signatures          # list of peer ids
        self.witnesses = set(witnesses)        # peers who cosigned
        self.trusted_honest = set(trusted_honest)
        self.f = f
        self.proof_horizon_views = proof_horizon_views
        self.state = "PROVISIONAL"

    def quorum_threshold(self, n):
        # Correct BFT threshold, NOT simple majority of received messages.
        return (n + self.f + 1) // 2 + 1

    def honest_witness_count(self):
        return len(self.witnesses & self.trusted_honest)

    def can_finalize(self, current_view, equivocation_proof_present):
        n = len(self.trusted_honest) + self.f  # observed universe estimate
        if len(self.signatures) < self.quorum_threshold(n):
            return False
        # Must intersect own quorum at >= f+1 independently trusted honest nodes.
        if self.honest_witness_count() < self.f + 1:
            return False
        # Must have outlived the proof horizon OR hold a NO_CONFLICT attestation.
        if (current_view - self.term) < self.proof_horizon_views:
            return False
        if equivocation_proof_present:
            raise EquivocationUndetectableError(
                f"subtask {self.subtask_id}: conflicting signed COMMIT observed "
                f"at term {self.term}; halting before irreversible effect"
            )
        return True

    def finalize(self, current_view, equivocation_proof_present, side_effect_fn):
        if not self.can_finalize(current_view, equivocation_proof_present):
            # Detection-blind quorum: must NOT perform irreversible work.
            raise EquivocationUndetectableError(
                f"subtask {self.subtask_id}: quorum met but honest-witness clause "
                f"or proof horizon unsatisfied; commitment stays PROVISIONAL"
            )
        self.state = "FINAL"
        return side_effect_fn()
