# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-trusts-a-revoked-federated-warrant-mid-exchange-the-stale-attestation-window-in-cross-org-agent-identity.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-trusts-a-revoked-federated-warrant-mid-exchange-the-stale-attestation-window-in-cross-org-agent-identity.warrant.json
# Markdown SHA-256:   145225d2d74ae2199a91c8ab255a6cda13ab4c2dbacacecf1963158e6613a4c2
# Code AST SHA-256:   5a5a2da153ed188c956d9c7803a4e1eb4b26f9e319664d07cfe0dc8781c14520
# Verified Classes:   StaleWarrantError, Warrant, FederationPolicy
# Defensive Guards:   6 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class StaleWarrantError(RuntimeError):
    """Raised when a warrant would be honored past the federation's
    revocation-observation guarantee."""


@dataclass(frozen=True)
class Warrant:
    issuer: str
    subject: str
    iat: float          # issued-at, epoch seconds
    exp: float          # expiry, epoch seconds
    parent_revoked_at: float | None  # issuer's revocation ts for parent, if any


@dataclass(frozen=True)
class FederationPolicy:
    max_propagation_lag: float       # worst-case revocation lag across orgs
    observation_interval: float      # how often verifier syncs issuer feed
    last_feed_sync: float            # epoch of last confirmed sync w/ issuer


def assert_warrant_honorable(
    w: Warrant, policy: FederationPolicy, now: float, committing: bool
) -> None:
    # Expiry and signature are assumed already checked; this guards the
    # revocation-blind window that signature/expiry checks cannot see.
    if now >= w.exp:
        raise StaleWarrantError(f"warrant expired at {w.exp}, now={now}")

    if w.parent_revoked_at is not None and w.iat >= w.parent_revoked_at:
        # Revocation-laundering: a child minted after the parent was revoked.
        raise StaleWarrantError(
            f"child iat={w.iat} >= parent revocation {w.parent_revoked_at}: "
            f"minted from a revoked parent"
        )

    if not committing:
        return  # read-only path may tolerate bounded staleness

    feed_age = now - policy.last_feed_sync
    if feed_age > policy.observation_interval:
        raise StaleWarrantError(
            f"issuer feed stale: age={feed_age:.1f}s > "
            f"interval={policy.observation_interval}s; refusing commit"
        )

    exposure = (w.exp - w.iat) + policy.max_propagation_lag
    if exposure > policy.observation_interval:
        raise StaleWarrantError(
            f"lifetime+lag={exposure:.1f}s exceeds observation "
            f"interval={policy.observation_interval}s: revocation-blind "
            f"window on committing action"
        )


def mint_child(parent: Warrant, policy: FederationPolicy, now: float) -> Warrant:
    # The mint path must apply the SAME revocation discipline as the use path;
    # a cached "not revoked" here is a laundering primitive.
    if now - policy.last_feed_sync > policy.observation_interval:
        raise StaleWarrantError(
            f"STS refusing mint: revocation state age="
            f"{now - policy.last_feed_sync:.1f}s exceeds interval"
        )
    if parent.parent_revoked_at is not None and now >= parent.parent_revoked_at:
        raise StaleWarrantError("STS refusing mint from revoked parent")
    return Warrant(
        issuer=parent.issuer,
        subject=parent.subject,
        iat=now,
        exp=min(now + 300.0, parent.exp),
        parent_revoked_at=None,
    )
