# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-detect-equivocating-task-commitments-at-the-boundary-between-quorum-certificate-and-intent-log.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-detect-equivocating-task-commitments-at-the-boundary-between-quorum-certificate-and-intent-log.warrant.json
# Markdown SHA-256:   355d2015307df76189b1dbfab38dde1d3d3a8fbccf52275a84f5b97fc5f63339
# Code AST SHA-256:   614659f4997c766f52c948fb22c0acae04fccc26be058d3f5a4efce3d760b2ef
# Verified Classes:   EquivocationBoundaryError, QuorumCertificate
# Defensive Guards:   2 fail-closed invariants (zero external deps)

import hashlib
from dataclasses import dataclass


class EquivocationBoundaryError(RuntimeError):
    """Raised when two QCs at one index certify divergent intent payloads."""
    pass


@dataclass(frozen=True)
class QuorumCertificate:
    index: int
    view: int
    intent_hash: bytes
    signers: frozenset


def canonical_intent_hash(intent: dict) -> bytes:
    # Canonical serialization is mandatory before hashing; see Condition of Validity.
    canon = repr(sorted(intent.items())).encode("utf-8")
    return hashlib.sha256(canon).digest()


def reconcile(qc_a: QuorumCertificate, qc_b: QuorumCertificate) -> str:
    if qc_a.index != qc_b.index:
        return "distinct_indices_no_conflict"
    if qc_a.intent_hash == qc_b.intent_hash:
        return "same_intent_merge_ok"

    overlap = qc_a.signers & qc_b.signers
    if overlap:
        raise EquivocationBoundaryError(
            f"equivocation at index {qc_a.index}: shared signers {sorted(overlap)} "
            f"signed divergent intents {qc_a.intent_hash.hex()[:8]} vs "
            f"{qc_b.intent_hash.hex()[:8]}; halt reconciliation and slash"
        )
    raise EquivocationBoundaryError(
        f"quorum intersection violated at index {qc_a.index}: disjoint signer sets "
        f"imply n < 3f+1; force full membership audit"
    )
