# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-federated-peer-s-capability-attestation-as-a-revocation-bearing-lease-rather-than-a-static-grant.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-federated-peer-s-capability-attestation-as-a-revocation-bearing-lease-rather-than-a-static-grant.warrant.json
# Markdown SHA-256:   1b527c3b227175b8371e31f77ab4d4a95b6a532a19079f2310cab88b73c0c784
# Code AST SHA-256:   e69d03b58ab3513722a3582dc81248d2c23a1143f0a074253530493bf84609a5
# Verified Classes:   StaleFederatedAttestation, FederatedAttestation
# Defensive Guards:   4 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class StaleFederatedAttestation(RuntimeError):
    """Raised when a cross-domain tool call would execute on an attestation
    whose lease has expired or whose staleness bound is undeclared."""
    pass


@dataclass(frozen=True)
class FederatedAttestation:
    attestation_id: str
    issuer_domain: str
    scopes: frozenset
    issued_at: float
    asserted_ttl: float | None      # None == "session"/unbounded (dangerous)
    revocable: bool
    max_staleness: float | None     # None == undeclared (protocol defect)


def authorize_cross_domain_call(
    att: FederatedAttestation,
    requested_scope: str,
    now: float | None = None,
) -> None:
    now = time.time() if now is None else now

    if requested_scope not in att.scopes:
        raise StaleFederatedAttestation(
            f"scope {requested_scope!r} not in attestation {att.attestation_id}"
        )

    # Undeclared staleness bound on a revocable scope is a protocol defect:
    # fail closed rather than treat absence of a bound as indefinite authority.
    if att.revocable and att.max_staleness is None:
        raise StaleFederatedAttestation(
            f"attestation {att.attestation_id} from {att.issuer_domain} is "
            f"revocable but declares no max_staleness; refusing to cache as grant"
        )

    # Effective lease = min(asserted TTL, max_staleness). An unbounded
    # asserted_ttl does not extend the lease beyond max_staleness.
    bounds = [b for b in (att.asserted_ttl, att.max_staleness) if b is not None]
    if not bounds:
        raise StaleFederatedAttestation(
            f"attestation {att.attestation_id} carries no temporal bound; "
            f"cannot establish a lease horizon"
        )
    lease = min(bounds)
    age = now - att.issued_at

    if age > lease:
        raise StaleFederatedAttestation(
            f"attestation {att.attestation_id} age={age:.1f}s exceeds lease="
            f"{lease:.1f}s (issuer={att.issuer_domain}); re-attestation required "
            f"before executing {requested_scope!r}"
        )


# Reproduction of the observed defect: revocable scope, session TTL,
# undeclared staleness bound, executed 47 minutes after issuance.
att = FederatedAttestation(
    attestation_id="att-broker-7f3a",
    issuer_domain="org-b",
    scopes=frozenset({"tool_x", "tool_y", "tool_z"}),
    issued_at=1_000_000.0,
    asserted_ttl=None,          # "session"
    revocable=True,
    max_staleness=None,         # undeclared
)
authorize_cross_domain_call(att, "tool_z", now=1_000_000.0 + 47 * 60)
