# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-delegation-chain-has-silently-re-widened-privileges-at-an-attenuation-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-delegation-chain-has-silently-re-widened-privileges-at-an-attenuation-boundary.warrant.json
# Markdown SHA-256:   e1e7aeae2691445a88887c7e7cdd20632ddfbc0447e88d65cc6de66d1e96f6d9
# Code AST SHA-256:   a571cefe6f3b8d47a2404a9bbd00e754d3fb272705d945da3db02a4bd6381910
# Verified Classes:   AttenuationViolation, Warrant, AuditTrail
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field


class AttenuationViolation(RuntimeError):
    """Raised when a delegation hop grants capabilities outside its parent's grant."""


@dataclass
class Warrant:
    agent_id: str
    granted_tools: frozenset[str]
    budget_tokens: int
    parent_id: str | None = None


@dataclass
class AuditTrail:
    records: list[Warrant] = field(default_factory=list)

    def append(self, w: Warrant) -> None:
        self.records.append(w)

    def validate_monotone(self) -> None:
        by_id = {w.agent_id: w for w in self.records}
        for w in self.records:
            if w.parent_id is None:
                continue
            parent = by_id.get(w.parent_id)
            if parent is None:
                raise AttenuationViolation(
                    f"hop {w.agent_id}: parent {w.parent_id} absent from trail"
                )
            excess = w.granted_tools - parent.granted_tools
            if excess:
                raise AttenuationViolation(
                    f"hop {w.agent_id} re-widened privileges beyond parent "
                    f"{parent.agent_id}: ungranted tools {sorted(excess)}"
                )
            if w.budget_tokens > parent.budget_tokens:
                raise AttenuationViolation(
                    f"hop {w.agent_id} budget {w.budget_tokens} exceeds "
                    f"parent {parent.agent_id} budget {parent.budget_tokens}"
                )


def materialize_child(
    parent: Warrant,
    child_id: str,
    requested_tools: frozenset[str],
    requested_budget: int,
) -> Warrant:
    # Projection, not generation: refuse to mint anything outside the parent token.
    excess = requested_tools - parent.granted_tools
    if excess:
        raise AttenuationViolation(
            f"cannot mint warrant for {child_id}: requested tools {sorted(excess)} "
            f"not present in parent {parent.agent_id} grant"
        )
    if requested_budget > parent.budget_tokens:
        raise AttenuationViolation(
            f"cannot mint warrant for {child_id}: budget {requested_budget} "
            f"exceeds parent {parent.agent_id} budget {parent.budget_tokens}"
        )
    return Warrant(
        agent_id=child_id,
        granted_tools=requested_tools,
        budget_tokens=requested_budget,
        parent_id=parent.agent_id,
    )
