# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-delegated-scope-amplification-when-an-aaif-capability-warrant-is-re-issued-across-agent-hops.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-delegated-scope-amplification-when-an-aaif-capability-warrant-is-re-issued-across-agent-hops.warrant.json
# Markdown SHA-256:   7450bc56abb01d88b7ca8a3641b2124fbc2f3a5c61a476a608f00dbe4896aed2
# Code AST SHA-256:   5c8ed91104b08efbb59f6f452b48e7c14080340b8c19b8ee7625a032e1a95351
# Verified Classes:   WarrantAttenuationError, Warrant
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Mapping


class WarrantAttenuationError(RuntimeError):
    """Raised when a child warrant does not strictly narrow its parent's effective authority."""


@dataclass(frozen=True)
class Warrant:
    scopes: frozenset[str]
    constraints: Mapping[str, frozenset[str]]
    audience: str
    expiry: int


# Closed constraint vocabulary shared by issuer and all intermediates.
KNOWN_CONSTRAINT_KEYS = frozenset({"repo", "path_prefix", "tenant"})


def issue_child(parent: Warrant, scopes: frozenset[str], constraints: Mapping[str, frozenset[str]],
                audience: str, expiry: int) -> Warrant:
    # 1. Scope names must be a subset (necessary, not sufficient).
    if not scopes.issubset(parent.scopes):
        raise WarrantAttenuationError(
            f"scope escalation: child {scopes} not subset of parent {parent.scopes}"
        )

    # 2. Constraints are mandatory and must be a subset of the parent's per key.
    #    An omitted key is interpreted as the empty authority set, never universal.
    unknown = set(constraints) - KNOWN_CONSTRAINT_KEYS
    if unknown:
        raise WarrantAttenuationError(
            f"unknown constraint keys {unknown}; refusing to delegate with unrepresentable predicates"
        )
    for key, parent_vals in parent.constraints.items():
        child_vals = constraints.get(key)
        if child_vals is None:
            # Dropping a parent predicate widens authority -> reject.
            raise WarrantAttenuationError(
                f"constraint '{key}' dropped on re-issue; child authority would exceed parent"
            )
        if not child_vals.issubset(parent_vals):
            raise WarrantAttenuationError(
                f"constraint '{key}' widened: child {child_vals} not subset of parent {parent_vals}"
            )

    # 3. Expiry must not extend.
    if expiry > parent.expiry:
        raise WarrantAttenuationError(
            f"expiry extended: child {expiry} > parent {parent.expiry}"
        )

    return Warrant(scopes=scopes, constraints=dict(constraints), audience=audience, expiry=expiry)


def demo() -> None:
    w0 = Warrant(
        scopes=frozenset({"read:repo"}),
        constraints={"repo": frozenset({"acme/web", "acme/api"}), "path_prefix": frozenset({"src/"})},
        audience="planner",
        expiry=1000,
    )
    # Planner omits the constraint map -> must fail closed, not silently widen.
    issue_child(w0, frozenset({"read:repo"}), {}, "executor", 900)


if __name__ == "__main__":
    demo()
