# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-peer-s-declared-a2a-capability-descriptor-as-non-authoritative-for-tool-call-admission.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-peer-s-declared-a2a-capability-descriptor-as-non-authoritative-for-tool-call-admission.warrant.json
# Markdown SHA-256:   debb0a75a646a25023fb493ae0852ac20ebe29c1cf1578c7633fc638d549c15d
# Code AST SHA-256:   bd91d6b33d6c790c72feac533d669a51d2853857dd8cfe5f831256fd1b05827d
# Verified Classes:   DescriptorWarrantError, AgentCard, Delegation
# Defensive Guards:   4 fail-closed invariants (zero external deps)

import hashlib
from dataclasses import dataclass
from typing import Optional


class DescriptorWarrantError(RuntimeError):
    """Raised when a delegation is admitted on a descriptor that is not a valid warrant."""
    pass


@dataclass
class AgentCard:
    url: str
    skills: dict
    input_modes: list
    etag: Optional[str]
    fetched_at: float


@dataclass
class Delegation:
    skill: str
    input_mode: str
    payload_tokens: int
    side_effecting: bool


# Above-boundary thresholds: expensive actions require a fresh warrant.
BOUNDARY_TOKEN_COST = 8_000
WARRANT_MAX_AGE_S = 5.0


def admit_delegation(
    card: AgentCard,
    delegation: Delegation,
    now: float,
    runtime_probe_etag: Optional[str],
    prior_capability_rejection: bool,
) -> None:
    expensive = (
        delegation.payload_tokens >= BOUNDARY_TOKEN_COST
        or delegation.side_effecting
    )

    if delegation.input_mode not in card.input_modes:
        raise DescriptorWarrantError(
            f"input_mode {delegation.input_mode!r} absent from cached card "
            f"for {card.url}; re-negotiate before dispatch"
        )

    if delegation.skill not in card.skills:
        raise DescriptorWarrantError(
            f"skill {delegation.skill!r} not advertised by {card.url}"
        )

    if prior_capability_rejection:
        raise DescriptorWarrantError(
            "prior step in plan returned a capability-class rejection; "
            "descriptor is non-authoritative until revalidated"
        )

    if not expensive:
        return  # cheap probe: descriptor alone suffices

    # Above the boundary: require a runtime warrant, not just the card.
    warrant_fresh = (now - card.fetched_at) <= WARRANT_MAX_AGE_S
    warrant_matches = (
        runtime_probe_etag is not None
        and card.etag is not None
        and runtime_probe_etag == card.etag
    )
    if not (warrant_fresh and warrant_matches):
        raise DescriptorWarrantError(
            f"expensive delegation ({delegation.payload_tokens} tokens, "
            f"side_effecting={delegation.side_effecting}) admitted on stale or "
            f"unmatched descriptor for {card.url}: "
            f"age={now - card.fetched_at:.1f}s, "
            f"etag_match={warrant_matches}"
        )
