# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-accepts-a-delegated-task-whose-policy-predicate-was-satisfied-by-a-stale-trust-anchor.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-accepts-a-delegated-task-whose-policy-predicate-was-satisfied-by-a-stale-trust-anchor.warrant.json
# Markdown SHA-256:   0e8706862243c4c960804b0b22f5883fc240b78a1705593bdf41b9d6c657138a
# Code AST SHA-256:   616b180ece8d2758d2ba1ab1cd1485a317ea1b58bb4391b542de326f0e59a22d
# Verified Classes:   StaleTrustAnchorAdmission, TrustAnchor, Warrant
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class StaleTrustAnchorAdmission(RuntimeError):
    """Raised when an admission decision is satisfied by an anchor whose
    validity interval does not cover the predicate evaluation instant, or
    whose epoch does not match the warrant's signing epoch."""
    pass


@dataclass(frozen=True)
class TrustAnchor:
    anchor_id: str
    epoch: int
    issued_at: float
    revoked_at: Optional[float]  # None if not revoked
    scope_task_types: frozenset
    scope_data_classes: frozenset


@dataclass(frozen=True)
class Warrant:
    delegator: str
    anchor_epoch: int
    expires_at: float
    claims_task_type: str
    claims_data_class: str


def admit_delegated_task(
    anchor: TrustAnchor,
    anchor_verified_at: float,
    warrant: Warrant,
    t_eval: float,
    revocation_propagation_bound: float,
) -> bool:
    # Invariant 1: the anchor's validity interval must cover t_eval.
    if anchor.revoked_at is not None and t_eval >= anchor.revoked_at:
        raise StaleTrustAnchorAdmission(
            f"anchor {anchor.anchor_id} revoked at {anchor.revoked_at}, "
            f"predicate evaluated at {t_eval}"
        )

    # Invariant 2: the warrant must be signed under the anchor's current epoch.
    if warrant.anchor_epoch != anchor.epoch:
        raise StaleTrustAnchorAdmission(
            f"warrant epoch {warrant.anchor_epoch} != anchor epoch {anchor.epoch}"
        )

    # Invariant 3: the gap between anchor verification and predicate evaluation
    # must not exceed the revocation propagation bound. Beyond it, the anchor's
    # liveness at t_eval is unknown and the predicate cannot be trusted.
    if (t_eval - anchor_verified_at) > revocation_propagation_bound:
        raise StaleTrustAnchorAdmission(
            f"anchor verified at {anchor_verified_at}, evaluated at {t_eval}, "
            f"gap exceeds propagation bound {revocation_propagation_bound}"
        )

    # Invariant 4: warrant must not have expired at evaluation time.
    if warrant.expires_at <= t_eval:
        raise StaleTrustAnchorAdmission(
            f"warrant expired at {warrant.expires_at}, evaluated at {t_eval}"
        )

    # Only now is the predicate evaluated over a live, epoch-matched anchor.
    permitted = (
        warrant.claims_task_type in anchor.scope_task_types
        and warrant.claims_data_class in anchor.scope_data_classes
    )
    if not permitted:
        raise StaleTrustAnchorAdmission(
            f"claims ({warrant.claims_task_type}, {warrant.claims_data_class}) "
            f"outside anchor {anchor.anchor_id} scope"
        )
    return True
