# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-to-inherit-a-federated-peer-s-capability-scope-after-a-trust-bootstrap-replay-and-what-breaks-when-it-do.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-to-inherit-a-federated-peer-s-capability-scope-after-a-trust-bootstrap-replay-and-what-breaks-when-it-do.warrant.json
# Markdown SHA-256:   e8d1af1ffc02ba358f3787ef09fac0e37279cbb06c1378b6e9df5f3861c168c4
# Code AST SHA-256:   0b9a54639bad496a8727c56048d6273ddf2ce702a1ac8ee6e0c3204d9e365c11
# Verified Classes:   FederationScopeStale, Attestation, FederatedScope
# Defensive Guards:   3 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class FederationScopeStale(RuntimeError):
    """Raised when a federated capability scope cannot be safely inherited."""
    pass


@dataclass(frozen=True)
class Attestation:
    attestation_id: str
    subject: str
    scope: frozenset
    not_after: float          # absolute wall-clock, from issuer
    policy_digest: str
    policy_url: str


@dataclass(frozen=True)
class FederatedScope:
    attestation_id: str
    subject: str
    capabilities: frozenset
    not_after: float
    policy_digest: str
    read_only: bool = False


def derive_scope(
    att: Attestation,
    seen_attestation_ids: set,
    live_policy_digest: str | None,
    max_staleness_s: float = 30.0,
    now: float | None = None,
) -> FederatedScope:
    now = time.time() if now is None else now

    # Replay detection: same attestation re-presented after a prior admission.
    if att.attestation_id in seen_attestation_ids:
        raise FederationScopeStale(
            f"attestation {att.attestation_id} replayed; refusing to inherit scope"
        )

    # Temporal anchor must come from the issuer, not from replay time.
    if now >= att.not_after:
        raise FederationScopeStale(
            f"attestation {att.attestation_id} expired at {att.not_after:.0f}, now {now:.0f}"
        )

    # Policy must be resolvable to a live endpoint before first effect.
    if live_policy_digest is None:
        raise FederationScopeStale(
            f"policy endpoint {att.policy_url} unreachable within {max_staleness_s}s; "
            "scope unverifiable, demote to read-only"
        )

    # Drift: re-derive as intersection, do not inherit asserted scope verbatim.
    if live_policy_digest != att.policy_digest:
        return FederatedScope(
            attestation_id=att.attestation_id,
            subject=att.subject,
            capabilities=att.scope & _fetch_live_capabilities(att.policy_url),
            not_after=min(att.not_after, now + max_staleness_s),
            policy_digest=live_policy_digest,
            read_only=False,
        )

    return FederatedScope(
        attestation_id=att.attestation_id,
        subject=att.subject,
        capabilities=att.scope,
        not_after=att.not_after,
        policy_digest=att.policy_digest,
        read_only=False,
    )


def _fetch_live_capabilities(policy_url: str) -> frozenset:
    # Placeholder: real implementation queries the issuer's policy endpoint.
    return frozenset()
