# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-revoked-capability-warrant-as-still-live-during-in-flight-tool-execution-and-how-does-the-revocation-propag.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-revoked-capability-warrant-as-still-live-during-in-flight-tool-execution-and-how-does-the-revocation-propag.warrant.json
# Markdown SHA-256:   2f0cddee7efe96a642f89b654f0e7b46076a84c00161b726bf925ede642f12c2
# Code AST SHA-256:   e2669fad5ab8a108c6550077786c4b14c4b05afc7d3a463dda620b316dd7cfaa
# Verified Classes:   RevocationWindowViolation, Warrant, EpochObservation, EnforcementPoint
# Defensive Guards:   6 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass, field


class RevocationWindowViolation(RuntimeError):
    """Raised when an effect would commit under an epoch that cannot be proven
    to postdate the last known revocation, or when a sub-warrant lowers the
    epoch floor below its parent's."""
    pass


@dataclass(frozen=True)
class Warrant:
    warrant_id: str
    scope: frozenset
    epoch_floor: int          # root epoch at mint; monotone non-decreasing on attenuation
    issued_at: float
    expires_at: float
    delegation_depth: int


@dataclass
class EpochObservation:
    epoch: int
    observed_at: float
    ttl: float

    def is_fresh(self, now: float) -> bool:
        return (now - self.observed_at) < self.ttl


@dataclass
class EnforcementPoint:
    name: str
    observation: EpochObservation
    _last_root_epoch: int = field(default=0)

    def refresh(self, root_epoch: int, now: float) -> None:
        if root_epoch < self._last_root_epoch:
            raise RevocationWindowViolation(
                f"{self.name}: epoch regressed {self._last_root_epoch}->{root_epoch}; "
                f"refusing to trust a non-monotone revocation feed"
            )
        self._last_root_epoch = root_epoch
        self.observation = EpochObservation(root_epoch, now, self.observation.ttl)

    def attenuate(self, parent: Warrant, child_scope: frozenset,
                  child_id: str, now: float) -> Warrant:
        # Epoch floor is copied forward, never reset. A child that lowers the
        # floor creates a second staleness surface independent of the root feed.
        if parent.epoch_floor < self._last_root_epoch:
            # parent was already stale at attenuation time; child inherits the
            # staleness and must be marked accordingly, not silently re-signed.
            raise RevocationWindowViolation(
                f"{self.name}: cannot attenuate {parent.warrant_id}; parent epoch_floor "
                f"{parent.epoch_floor} < observed root {self._last_root_epoch}"
            )
        return Warrant(
            warrant_id=child_id,
            scope=child_scope,
            epoch_floor=parent.epoch_floor,
            issued_at=now,
            expires_at=parent.expires_at,
            delegation_depth=parent.delegation_depth + 1,
        )

    def admit_dispatch(self, warrant: Warrant, max_exec_time: float,
                       now: float) -> None:
        # Admission predicate: refuse to dispatch if the effect could outlive
        # the cache validity. This is the only bound that holds without a
        # commit-time round trip.
        if warrant.epoch_floor > self.observation.epoch:
            raise RevocationWindowViolation(
                f"{self.name}: warrant {warrant.warrant_id} floor "
                f"{warrant.epoch_floor} > local epoch {self.observation.epoch}"
            )
        remaining = self.observation.ttl - (now - self.observation.observed_at)
        if max_exec_time > remaining:
            raise RevocationWindowViolation(
                f"{self.name}: dispatch refused for {warrant.warrant_id}; "
                f"max_exec_time {max_exec_time:.3f}s exceeds cache validity "
                f"{remaining:.3f}s — effect could land after revocation"
            )

    def commit_effect(self, warrant: Warrant, dispatch_time: float,
                      now: float) -> int:
        # Commit-time check: epoch must have been re-observed AFTER dispatch,
        # and must be >= warrant floor. Returns the fence stamp.
        if self.observation.observed_at < dispatch_time:
            raise RevocationWindowViolation(
                f"{self.name}: epoch observation predates dispatch for "
                f"{warrant.warrant_id}; cannot prove authorization at commit"
            )
        if self.observation.epoch < warrant.epoch_floor:
            raise RevocationWindowViolation(
                f"{self.name}: commit under epoch {self.observation.epoch} "
                f"< floor {warrant.epoch_floor} for {warrant.warrant_id}"
            )
        # Effect is stamped with the authorizing epoch so the authority can
        # reconcile post-hoc whether it landed inside a propagation window.
        return self.observation.epoch
