# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-a-delegated-credential-chain-the-aaif-attenuation-monotonicity-break-at-re-delegation-depth.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-a-delegated-credential-chain-the-aaif-attenuation-monotonicity-break-at-re-delegation-depth.warrant.json
# Markdown SHA-256:   b3f8c32c1562846b3d03f745e1cb762eb51d50dd9d45c37fd42b7bdca834024c
# Code AST SHA-256:   f20a45fc41f35e171d3009277bfaff56151524e52c5d4ab09c8758272a9d3ce0
# Verified Classes:   AttenuationViolation, Warrant
# Defensive Guards:   1 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import FrozenSet


class AttenuationViolation(RuntimeError):
    """Raised when a child warrant is not a monotonic attenuation of its parent."""


@dataclass(frozen=True)
class Warrant:
    capabilities: FrozenSet[str]
    resources: FrozenSet[str]
    constraints: FrozenSet[str]          # tightening only: child must be superset
    expires_at: int                       # epoch seconds
    delegable_depth: int


def delegate(parent: Warrant, requested: Warrant) -> Warrant:
    """Mint a child warrant only if it strictly attenuates the parent.

    Authority is derived from the *received* parent warrant, never from the
    delegating instance's inferred intent.
    """
    violations = []

    if not requested.capabilities <= parent.capabilities:
        extra = requested.capabilities - parent.capabilities
        violations.append(f"capability superset: {sorted(extra)}")

    if not requested.resources <= parent.resources:
        extra = requested.resources - parent.resources
        violations.append(f"resource superset: {sorted(extra)}")

    if not requested.constraints >= parent.constraints:
        dropped = parent.constraints - requested.constraints
        violations.append(f"constraint widening (dropped): {sorted(dropped)}")

    if requested.expires_at > parent.expires_at:
        violations.append(
            f"expiry extension: {requested.expires_at} > {parent.expires_at}"
        )

    if parent.delegable_depth <= 0:
        violations.append("delegation depth exhausted")

    if violations:
        raise AttenuationViolation(
            "non-monotonic delegation refused: " + "; ".join(violations)
        )

    return Warrant(
        capabilities=requested.capabilities,
        resources=requested.resources,
        constraints=requested.constraints,
        expires_at=requested.expires_at,
        delegable_depth=parent.delegable_depth - 1,
    )


if __name__ == "__main__":
    parent = Warrant(
        capabilities=frozenset({"read", "summarize"}),
        resources=frozenset({"corpus://tenant-7/*"}),
        constraints=frozenset({"max_tokens:20000"}),
        expires_at=3_600,
        delegable_depth=2,
    )
    # W's planner inferred export + tenant-9 + no constraints: escalation.
    bad_child = Warrant(
        capabilities=frozenset({"read", "summarize", "export"}),
        resources=frozenset({"corpus://tenant-7/*", "corpus://tenant-9/*"}),
        constraints=frozenset(),
        expires_at=7_200,
        delegable_depth=1,
    )
    delegate(parent, bad_child)  # raises AttenuationViolation
