# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-attested-by-a-quorum-as-insufficient-when-the-quorum-is-drawn-from-a-shared-reasoning-substrate.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-attested-by-a-quorum-as-insufficient-when-the-quorum-is-drawn-from-a-shared-reasoning-substrate.warrant.json
# Markdown SHA-256:   e25c6732c5aa79d303ebf8fc3b8b4b2e29ea970f5c584bbb58b72bbf782f0550
# Code AST SHA-256:   3ba89573d9dc5d1daa6f0cc15c762831d4826dd60287723ecc4582ebc8309c30
# Verified Classes:   InsufficientQuorumDiversity, Signer, Attestation
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Optional


class InsufficientQuorumDiversity(RuntimeError):
    """Raised when a quorum attestation's raw signature count meets threshold
    but its effective independent fault-domain count does not."""
    pass


@dataclass
class Signer:
    signer_id: str
    model_lineage: str          # e.g. "base-7b@ckpt-2024-11"
    corpus_hash: str            # hash of retrieval index / document set
    tool_credential_id: str
    supporting_span: Optional[str] = None


@dataclass
class Attestation:
    claim_id: str
    signers: list[Signer] = field(default_factory=list)
    threshold: int = 2          # raw 2f+1 count required

    def _independent(self, a: Signer, b: Signer) -> bool:
        # Two signers are independent only if all three surfaces differ.
        lineage_ok = a.model_lineage != b.model_lineage
        corpus_ok = a.corpus_hash != b.corpus_hash
        tool_ok = a.tool_credential_id != b.tool_credential_id
        # Byte-identical supporting span is a strong signal of shared path.
        span_collision = (
            a.supporting_span is not None
            and a.supporting_span == b.supporting_span
        )
        return lineage_ok and corpus_ok and tool_ok and not span_collision

    def effective_domains(self) -> int:
        # Greedy max independent set (small n; exact for typical quorum sizes).
        best = 0
        n = len(self.signers)
        for mask in range(1 << n):
            chosen = [self.signers[i] for i in range(n) if mask & (1 << i)]
            if all(
                self._independent(chosen[i], chosen[j])
                for i in range(len(chosen))
                for j in range(i + 1, len(chosen))
            ):
                best = max(best, len(chosen))
        return best

    def require_committing(self) -> None:
        raw = len(self.signers)
        eff = self.effective_domains()
        if raw < self.threshold:
            raise InsufficientQuorumDiversity(
                f"claim {self.claim_id}: raw signatures {raw} < threshold {self.threshold}"
            )
        if eff < self.threshold:
            raise InsufficientQuorumDiversity(
                f"claim {self.claim_id}: raw signatures {raw} meet threshold, "
                f"but effective independent domains {eff} < {self.threshold}; "
                f"attestation is advisory, not committing"
            )


def commit_task(att: Attestation) -> str:
    # Enforce the invariant before any irreversible downstream write.
    att.require_committing()
    return f"committed:{att.claim_id}"
