# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-refuse-a-federated-capability-grant-when-the-trust-broker-s-policy-epoch-lags-the-peer-s-advertised-schema-epoch.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-refuse-a-federated-capability-grant-when-the-trust-broker-s-policy-epoch-lags-the-peer-s-advertised-schema-epoch.warrant.json
# Markdown SHA-256:   4e349c2635fd2972acc8f1f0f32f33c854dd9475c715f922749819bbe0cdf5c2
# Code AST SHA-256:   832e0880d86eb3b35756f439cbd1644223bf7d939aad42b3286fe817977200bb
# Verified Classes:   EpochDominanceViolation, Warrant, PeerManifest
# Defensive Guards:   3 fail-closed invariants (zero external deps)

import hashlib
from dataclasses import dataclass


class EpochDominanceViolation(RuntimeError):
    """Raised when a peer's schema epoch semantically outruns the warrant's policy epoch
    without a valid per-capability delta attestation."""


@dataclass(frozen=True)
class Warrant:
    capability: str
    policy_epoch: int
    arg_schema_hash: str
    requires_idempotency_key: bool


@dataclass(frozen=True)
class PeerManifest:
    capability: str
    schema_epoch: int
    arg_schema_hash: str
    is_idempotent: bool
    delta_attestations: dict  # (from_epoch, to_epoch) -> signed arg_schema_hash


def _attestation_valid(manifest: PeerManifest, warrant: Warrant) -> bool:
    key = (warrant.policy_epoch, manifest.schema_epoch)
    attested_hash = manifest.delta_attestations.get(key)
    if attested_hash is None:
        return False
    # Attestation must bind to the exact argument schema the planner compiled against.
    return attested_hash == warrant.arg_schema_hash == manifest.arg_schema_hash


def assert_epoch_dominance(warrant: Warrant, manifest: PeerManifest) -> None:
    if warrant.capability != manifest.capability:
        raise EpochDominanceViolation(
            f"capability mismatch: warrant={warrant.capability} manifest={manifest.capability}"
        )

    if manifest.schema_epoch <= warrant.policy_epoch:
        return  # dominated; safe to execute

    if _attestation_valid(manifest, warrant):
        return  # semantic delta proven absent for this capability

    raise EpochDominanceViolation(
        f"peer schema_epoch={manifest.schema_epoch} outruns warrant policy_epoch="
        f"{warrant.policy_epoch} for {warrant.capability!r} with no valid delta attestation; "
        f"refusing execution (name match is not semantic match)"
    )


def preflight(warrant: Warrant, manifest: PeerManifest) -> None:
    assert_epoch_dominance(warrant, manifest)
    if not manifest.is_idempotent and not warrant.requires_idempotency_key:
        raise EpochDominanceViolation(
            f"non-idempotent verb {warrant.capability!r} at schema_epoch="
            f"{manifest.schema_epoch} but warrant carries no idempotency-key requirement; "
            f"planner cannot synthesize a key it did not know was needed"
        )


# Example: the observed failure, now refused.
w = Warrant("peer.beta:ledger.write", policy_epoch=41,
            arg_schema_hash=hashlib.sha256(b"amount:int").hexdigest(),
            requires_idempotency_key=False)
m = PeerManifest("peer.beta:ledger.write", schema_epoch=44,
                 arg_schema_hash=hashlib.sha256(b"amount:str").hexdigest(),
                 is_idempotent=False, delta_attestations={})
preflight(w, m)  # -> raises EpochDominanceViolation before any cross-org call
