# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-a-capability-warrant-split-when-a-federated-peer-s-advertised-schema-silently-diverges-from-the-schema-its-policy-engine-a.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-a-capability-warrant-split-when-a-federated-peer-s-advertised-schema-silently-diverges-from-the-schema-its-policy-engine-a.warrant.json
# Markdown SHA-256:   6f3cb718b312a65422a37b01e50bc47b146bdab8b9167ed2586b78d4fc0eeda3
# Code AST SHA-256:   1143602100f3f97cabb3bfbc7819bdcbbfff0a97d50217d65dea6216fd634f71
# Verified Classes:   WarrantDigestMismatch, Warrant, PeerResponse
# Defensive Guards:   3 fail-closed invariants (zero external deps)

import hashlib
from dataclasses import dataclass


class WarrantDigestMismatch(RuntimeError):
    """Raised when the schema digest a peer enforced diverges from the warrant
    the sender validated against. This is the warrant/schema split signal."""


@dataclass(frozen=True)
class Warrant:
    peer_id: str
    capability: str
    schema_digest: str  # hash over advertised field constraints
    ttl_seconds: int


@dataclass(frozen=True)
class PeerResponse:
    status: str            # "accepted" | "accepted_with_coercion" | "rejected"
    enforced_digest: str   # digest of constraints the policy engine actually applied
    coercion_record: dict


def validate_and_send(
    payload: dict,
    warrant: Warrant,
    send_fn,
) -> PeerResponse:
    # 1. Validate against the cached warrant (this is what the old design did,
    #    and it is necessary but NOT sufficient).
    _validate_against_warrant(payload, warrant)

    # 2. Send and receive the peer's response.
    resp: PeerResponse = send_fn(warrant.peer_id, warrant.capability, payload)

    # 3. The invariant: the receiver must prove it enforced the schema it
    #    advertised. A 200 alone is not evidence of schema conformance.
    if resp.enforced_digest != warrant.schema_digest:
        raise WarrantDigestMismatch(
            f"warrant/enforcement split on {warrant.peer_id}:{warrant.capability} "
            f"advertised={warrant.schema_digest} enforced={resp.enforced_digest} "
            f"coercions={resp.coercion_record!r}. "
            f"Invalidate cached warrant and re-discover before retrying."
        )

    # 4. Coercion is a first-class response class, not a warning.
    if resp.status == "accepted_with_coercion":
        raise WarrantDigestMismatch(
            f"peer coerced payload despite matching digest: {resp.coercion_record!r}. "
            f"Treat as split; do not commit."
        )

    return resp


def _validate_against_warrant(payload: dict, warrant: Warrant) -> None:
    # Placeholder for the sender-side schema check against the cached warrant.
    # Passing here is necessary but does not prove the receiver agrees.
    if not payload:
        raise WarrantDigestMismatch("empty payload cannot satisfy any warrant")
