# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-attribute-byzantine-behavior-when-every-federation-member-s-local-view-is-self-consistent-but-globally-contradictory.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-attribute-byzantine-behavior-when-every-federation-member-s-local-view-is-self-consistent-but-globally-contradictory.warrant.json
# Markdown SHA-256:   0a9f485da4c57d1d90477510ccf66fd09308c004f6f516adfeac327b48edc9f1
# Code AST SHA-256:   59a05106dd0501e66c2d9cc3043d900aa7d06c9127ce25885aa8e23087863f99
# Verified Classes:   CrossFederationAttributionError, ObservationReceipt
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from __future__ import annotations

from dataclasses import dataclass
from typing import Optional


class CrossFederationAttributionError(RuntimeError):
    """Raised when a contradiction cannot be soundly attributed to a single
    Byzantine member from the evidence available to this instance."""


@dataclass(frozen=True)
class ObservationReceipt:
    subject_agent_id: str
    state_hash: str
    view_id: int                      # must be drawn from a globally comparable clock
    originating_federation: str
    quorum_signatures: tuple[str, ...]
    is_original: bool                 # False if this is a relayed paraphrase
    relay_federation: Optional[str] = None


def attribute_byzantine(
    receipt_a: ObservationReceipt,
    receipt_b: ObservationReceipt,
    auditor_has_global_evidence: bool,
) -> str:
    """Return the agent_id attributed as Byzantine, or raise if unattributable.

    Sound only when: both receipts are original, view_ids are globally
    comparable, and the caller is the designated cross-federation auditor.
    """
    if receipt_a.subject_agent_id != receipt_b.subject_agent_id:
        raise CrossFederationAttributionError(
            "receipts concern different subjects; no contradiction to attribute"
        )
    if receipt_a.state_hash == receipt_b.state_hash:
        raise CrossFederationAttributionError(
            "receipts agree on state; nothing to attribute"
        )

    # A broken provenance chain (relayed paraphrase) makes attribution unsound.
    if not (receipt_a.is_original and receipt_b.is_original):
        raise CrossFederationAttributionError(
            "cannot attribute over paraphrased receipts; demand original "
            "quorum-signed artifacts from the relay federation"
        )

    # Without a globally comparable view_id the contradiction cannot be stated.
    if receipt_a.view_id != receipt_b.view_id:
        raise CrossFederationAttributionError(
            "view_ids are not globally comparable; contradiction is ill-posed"
        )

    # Attribution of a cross-federation fault requires global evidence.
    if not auditor_has_global_evidence:
        raise CrossFederationAttributionError(
            "local instance lacks cross-federation evidence; quarantine and "
            "escalate to the designated auditor, do not adjudicate locally"
        )

    # At this point the subject equivocated across federations at the same
    # globally comparable view, with original signatures on both sides.
    return receipt_a.subject_agent_id


def resolve_contradiction(
    receipt_a: ObservationReceipt,
    receipt_b: ObservationReceipt,
    auditor_has_global_evidence: bool,
) -> str:
    try:
        culprit = attribute_byzantine(receipt_a, receipt_b, auditor_has_global_evidence)
    except CrossFederationAttributionError:
        # Fail closed: quarantine both states, never adopt either.
        return "QUARANTINE"
    return f"SLASH:{culprit}"
