# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-halt-when-a-revoked-agent-warrant-is-still-inside-its-sub-second-trust-cache.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-halt-when-a-revoked-agent-warrant-is-still-inside-its-sub-second-trust-cache.warrant.json
# Markdown SHA-256:   5763fdeab6638cef8ce3c8b69c90f7a619df9cec51da5818b5e0e644d1ec75e6
# Code AST SHA-256:   d3cbf55e49ef07c42a782a67ca89f936be5cc1b6d1eb75be78e4ba3c94e86b74
# Verified Classes:   RevocationBudgetExhausted, CacheEntry
# Defensive Guards:   4 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class RevocationBudgetExhausted(RuntimeError):
    """Raised when a derived warrant cannot be minted within the staleness bound."""
    pass


@dataclass
class CacheEntry:
    warrant_id: str
    cached_at: float      # monotonic seconds
    ttl: float            # local cache TTL
    warrant_expiry: float # absolute expiry of the warrant itself

    def expires_at(self) -> float:
        return min(self.cached_at + self.ttl, self.warrant_expiry)


def mint_subwarrant(
    parent: CacheEntry,
    now: float,
    local_cache_ttl: float,
    cross_domain_propagation_p99: float,
    staleness_bound: float,
    confirm_live_fn,  # callable(warrant_id) -> bool, synchronous origin check
) -> float:
    if local_cache_ttl > staleness_bound:
        raise RevocationBudgetExhausted(
            f"local cache TTL {local_cache_ttl}s exceeds staleness bound "
            f"{staleness_bound}s; verifier cannot honor the stated bound"
        )

    child_expiry = min(
        now + local_cache_ttl,
        parent.expires_at(),
        parent.warrant_expiry,
    ) - cross_domain_propagation_p99

    if child_expiry <= now:
        # Budget non-positive: must bypass cache and confirm against origin.
        if not confirm_live_fn(parent.warrant_id):
            raise RevocationBudgetExhausted(
                f"parent warrant {parent.warrant_id} not confirmed live at origin; "
                f"refusing to mint derived capability"
            )
        # Origin confirmed live: extend only by the safe residual.
        child_expiry = now + min(
            local_cache_ttl, staleness_bound
        ) - cross_domain_propagation_p99
        if child_expiry <= now:
            raise RevocationBudgetExhausted(
                "even after live confirmation, residual budget is non-positive; "
                "propagation p99 exceeds staleness bound"
            )

    return child_expiry


def verify_cross_domain_warrant(
    chain: list[CacheEntry],
    now: float,
    staleness_bound: float,
    propagation_to_self_p99: float,
) -> None:
    for link in chain:
        age = now - link.cached_at
        allowed = staleness_bound - propagation_to_self_p99
        if age > allowed:
            raise RevocationBudgetExhausted(
                f"chain link {link.warrant_id} age {age:.3f}s exceeds "
                f"allowed {allowed:.3f}s; revocation may not have arrived"
            )
