# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-reject-a-federated-trust-anchor-whose-policy-translation-is-semantically-lossless-but-whose-revocation-propagat.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-reject-a-federated-trust-anchor-whose-policy-translation-is-semantically-lossless-but-whose-revocation-propagat.warrant.json
# Markdown SHA-256:   27e076c00c9c0ced57305396899614d2fe8bb7d2a7a09a5f637bc4886f736ce7
# Code AST SHA-256:   4d82bbdea603922fe1850aeeb2b6619f9ca59fdc9bd4638fa07ea5147ea717b2
# Verified Classes:   FederationAnchorRejected, RemoteGrantPolicy, LocalLeaseConfig
# Defensive Guards:   3 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class FederationAnchorRejected(RuntimeError):
    """Raised when a remote trust anchor cannot be safely adopted locally."""
    pass


@dataclass(frozen=True)
class RemoteGrantPolicy:
    domain: str
    revocation_propagation_bound_s: float  # B's SLA: max time for revocation to reach A
    revocation_epoch: int                   # monotonic counter, signed by B


@dataclass(frozen=True)
class LocalLeaseConfig:
    lease_ttl_s: float
    renewal_grace_s: float
    cross_domain_skew_margin_s: float


def admit_federation_anchor(
    policy: RemoteGrantPolicy,
    lease: LocalLeaseConfig,
    translation_is_lossless: bool,
) -> None:
    # Condition 1: semantic closure. Necessary, loudly checked, NOT sufficient.
    if not translation_is_lossless:
        raise FederationAnchorRejected(
            f"anchor {policy.domain}: translation not lossless"
        )

    # Condition 3: revocation observability. Without an epoch counter A cannot
    # distinguish "not revoked" from "revocation not yet arrived".
    if policy.revocation_epoch < 0:
        raise FederationAnchorRejected(
            f"anchor {policy.domain}: no signed revocation epoch; "
            "lease scheduler cannot short-circuit renewal"
        )

    # Condition 2: temporal subordination. The silent killer.
    effective_lease = (
        lease.lease_ttl_s
        + lease.renewal_grace_s
        + lease.cross_domain_skew_margin_s
    )
    if effective_lease >= policy.revocation_propagation_bound_s:
        raise FederationAnchorRejected(
            f"anchor {policy.domain}: TEMPORAL_UNSAFE — "
            f"effective lease {effective_lease:.1f}s >= revocation bound "
            f"{policy.revocation_propagation_bound_s:.1f}s; "
            f"authority leak window up to "
            f"{effective_lease - policy.revocation_propagation_bound_s:.1f}s"
        )


def route_interdomain_task(
    policy: RemoteGrantPolicy,
    lease: LocalLeaseConfig,
    translation_is_lossless: bool,
    task_payload: dict,
) -> dict:
    admit_federation_anchor(policy, lease, translation_is_lossless)
    # Safe to dispatch: lease expires before revocation can lag behind it.
    return {
        "dispatched_to": policy.domain,
        "lease_expires_at": time.time() + lease.lease_ttl_s,
        "revocation_epoch_seen": policy.revocation_epoch,
        "payload": task_payload,
    }
