# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-that-a-delegated-agent-warrant-has-silently-outlived-its-revocation-across-a-federated-trust-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-that-a-delegated-agent-warrant-has-silently-outlived-its-revocation-across-a-federated-trust-boundary.warrant.json
# Markdown SHA-256:   e37176d0e4dd531c6007529935657e57ef577daa84168cde689a40783c851ff5
# Code AST SHA-256:   b8a6b8bede5477cf22e7dfdca41c4e95413ce6a5476049e31bd1076c0e38bbfe
# Verified Classes:   UnboundedRevocationWarrantError, Warrant, CachedPath
# Defensive Guards:   4 fail-closed invariants (zero external deps)

import hashlib
import time
from dataclasses import dataclass


class UnboundedRevocationWarrantError(RuntimeError):
    """Raised when a materialized child warrant cannot bound its own root-status staleness."""


@dataclass(frozen=True)
class Warrant:
    issuer: str
    subject: str
    delegation_depth: int
    not_before: float
    not_after: float
    parent_hash: str | None
    # Transitive bound from the ROOT issuer's revocation infrastructure.
    # None means the root never declared one -> this warrant cannot be cached safely.
    revocation_max_propagation_seconds: float | None


@dataclass
class CachedPath:
    path_fingerprint: str
    root_status_checked_at: float
    inherited_propagation_bound: float | None


def materialize_child_warrant(child: Warrant, parent: Warrant, now: float) -> CachedPath:
    """Cache a delegated warrant's validation path, but only if it can bound root staleness.

    The bound MUST be inherited from the root issuer via the parent chain. A verifier's
    local staleness budget is NOT a substitute: it grants an authorization window the
    verifier has no authority to grant.
    """
    if child.parent_hash is None or parent is None:
        raise UnboundedRevocationWarrantError(
            "child warrant has no resolvable parent; cannot inherit a propagation bound"
        )

    # Inherit the bound transitively. If the parent itself carries no bound, the
    # child inherits None -- absence propagates, it does not get defaulted.
    inherited = parent.revocation_max_propagation_seconds

    if inherited is None:
        raise UnboundedRevocationWarrantError(
            f"warrant {child.issuer}->{child.subject} carries no inherited "
            "revocation_max_propagation_seconds from its root; caching root status "
            "would create an unbounded authorization window after root revocation. "
            "Refusing to cache; caller must fall back to pull-on-use."
        )

    fingerprint = hashlib.sha256(
        f"{parent.parent_hash or ''}|{child.issuer}|{child.subject}".encode()
    ).hexdigest()

    return CachedPath(
        path_fingerprint=fingerprint,
        root_status_checked_at=now,
        inherited_propagation_bound=inherited,
    )


def authorize_action(cached: CachedPath, now: float) -> None:
    """Authorize a tool call against a cached path, enforcing the inherited bound."""
    bound = cached.inherited_propagation_bound
    if bound is None:
        raise UnboundedRevocationWarrantError(
            "attempted authorization against a path with no inherited propagation bound"
        )

    age = now - cached.root_status_checked_at
    if age > bound:
        raise UnboundedRevocationWarrantError(
            f"root status is stale by {age:.1f}s, exceeding inherited bound {bound:.1f}s; "
            "a revocation may have propagated during this window. Re-pull root status."
        )
