# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-detect-federation-capability-shadowing-when-a-peer-s-advertised-tool-schema-silently-narrows-after-policy-reconciliation.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-detect-federation-capability-shadowing-when-a-peer-s-advertised-tool-schema-silently-narrows-after-policy-reconciliation.warrant.json
# Markdown SHA-256:   f08cc036d41aff1a3b56ccc5994ff7dca4e1ccc97c48738cf219412a5287c686
# Code AST SHA-256:   86974e336fe29796c08413bc86907a26fe699c9083688566c0454eb84da1b1f6
# Verified Classes:   CapabilityShadowingError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class CapabilityShadowingError(RuntimeError):
    """Raised when a peer's effective policy epoch diverges from the
    epoch under which the dispatched payload was constructed."""


def dispatch_irreversible(consumer, peer, payload, cached_descriptor):
    # Guardrail 1: pre-dispatch epoch assertion against the *invocation gate*,
    # not the manifest publisher. Closes the split-brain config window.
    effective_epoch = peer.get_policy_epoch()
    if effective_epoch != cached_descriptor.policy_epoch:
        # Re-fetch and re-validate rather than dispatch stale.
        fresh = peer.fetch_manifest()
        if fresh.policy_epoch != effective_epoch:
            raise CapabilityShadowingError(
                f"gate epoch {effective_epoch} != manifest epoch "
                f"{fresh.policy_epoch}; peer config stores are split"
            )
        payload = consumer.revalidate(payload, fresh)
        cached_descriptor = fresh

    resp = peer.invoke("submit_claim", payload)

    # Guardrail 2: stable-deviation classifier. A schema contradiction that
    # reproduces byte-identically is a policy mutation, not a transient fault.
    if consumer.contradicts_schema(resp, cached_descriptor):
        probe = peer.invoke("submit_claim", payload)
        if consumer.deviation_is_stable(resp, probe):
            raise CapabilityShadowingError(
                f"deterministic schema deviation at epoch {effective_epoch}: "
                f"response {resp!r} contradicts cached descriptor; "
                f"refusing to consume transient-retry budget"
            )
        raise CapabilityShadowingError("transient deviation; retry bounded")

    return resp
