# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-delegates-to-a-sub-agent-that-re-derives-authority-from-ambient-context-instead-of-the-delegation-warrant.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-delegates-to-a-sub-agent-that-re-derives-authority-from-ambient-context-instead-of-the-delegation-warrant.warrant.json
# Markdown SHA-256:   21ae626a571e751e401e062a0867a8f9970c0b6fd37c25ec775f262182bbd85b
# Code AST SHA-256:   e55527f4937b6c53278c370c4488ec5b0b503a029f18751169d380cf1988656f
# Verified Classes:   WarrantIncomplete, Warrant
# Defensive Guards:   8 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import FrozenSet


class WarrantIncomplete(RuntimeError):
    """Raised when a delegation warrant omits an authority dimension.

    Absence of a dimension is treated as the empty set, never as ambient
    authority. A callee that cannot resolve a verb from the warrant must
    halt rather than infer from intent text or local registry.
    """


@dataclass(frozen=True)
class Warrant:
    verbs: FrozenSet[str]
    resources: FrozenSet[str]
    budget_tokens: int
    expires_at: float
    registry_digest: str


def attenuate(parent: Warrant, child: Warrant) -> Warrant:
    """Verify child is a per-dimension subset of parent; reject undefined dims."""
    # Every dimension must be explicitly present on the child. A missing
    # verb set is not "inherit" — it is undefined and therefore invalid.
    if child.verbs is None:
        raise WarrantIncomplete("child warrant omits verb enumeration")
    if not child.verbs <= parent.verbs:
        raise WarrantIncomplete(
            f"verb escalation: {child.verbs - parent.verbs} not in parent"
        )
    if not child.resources <= parent.resources:
        raise WarrantIncomplete(
            f"resource escalation: {child.resources - parent.resources}"
        )
    if child.budget_tokens > parent.budget_tokens:
        raise WarrantIncomplete("budget escalation exceeds parent cap")
    if child.expires_at > parent.expires_at:
        raise WarrantIncomplete("expiry extends past parent")
    if child.registry_digest != parent.registry_digest:
        raise WarrantIncomplete("registry digest mismatch: action space drift")
    return child


def resolve_call(warrant: Warrant, tool_verb: str, tool_resource: str) -> None:
    """Fail-closed resolution: verb must be in warrant, never inferred."""
    if tool_verb not in warrant.verbs:
        raise WarrantIncomplete(
            f"verb {tool_verb!r} not enumerated in warrant; "
            f"refusing to infer from intent text or local registry"
        )
    if tool_resource not in warrant.resources:
        raise WarrantIncomplete(
            f"resource {tool_resource!r} outside warrant scope"
        )
