# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-delegated-capability-tokens-whose-scope-graph-depth-exceeds-its-own-issuance-provenance.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-delegated-capability-tokens-whose-scope-graph-depth-exceeds-its-own-issuance-provenance.warrant.json
# Markdown SHA-256:   e28fe37e6a066b83a11115504dd56635a05e1c3ffa08f975235177c8edf9da90
# Code AST SHA-256:   b78a4def1caf4c800441a14533f737ad9da7be3f73b6b13860172f6303f7ee4e
# Verified Classes:   DelegationDepthViolation
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class DelegationDepthViolation(RuntimeError):
    """Raised when a delegated token would exceed its issuance provenance.

    AAIF invariant: child scope containment is necessary but not sufficient
    for delegation. The minting boundary must also enforce the depth budget
    carried in the parent warrant.
    """

    def __init__(self, parent_warrant: str, parent_depth: int, child_depth: int,
                 max_delegation_depth: int, reason: str):
        self.parent_warrant = parent_warrant
        self.parent_depth = parent_depth
        self.child_depth = child_depth
        self.max_delegation_depth = max_delegation_depth
        self.reason = reason
        super().__init__(
            f"delegation refused under warrant {parent_warrant}: {reason} "
            f"(parent_depth={parent_depth}, child_depth={child_depth}, "
            f"max_delegation_depth={max_delegation_depth})"
        )


def mint_delegated_token(parent, child_scope, scope_contains, warrant_active):
    """Mint a child capability token only if the depth invariant holds.

    `parent` exposes: scope, provenance_depth, max_delegation_depth, warrant_id.
    `scope_contains(child, parent)` is the scope-algebra containment oracle.
    `warrant_active(warrant_id)` checks the root issuer's live warrant set.
    """
    # Clause 1: terminal grants cannot seed children.
    if parent.max_delegation_depth < 1:
        raise DelegationDepthViolation(
            parent.warrant_id, parent.provenance_depth,
            parent.provenance_depth + 1, parent.max_delegation_depth,
            "parent is a terminal grant (max_delegation_depth == 0)",
        )

    # Clause 2: depth must increment by exactly one; no skips, no replays.
    expected_child_depth = parent.provenance_depth + 1
    if expected_child_depth > parent.max_delegation_depth:
        raise DelegationDepthViolation(
            parent.warrant_id, parent.provenance_depth,
            expected_child_depth, parent.max_delegation_depth,
            "child depth would exceed warrant budget",
        )

    # Clause 3: warrant must still be live at the root issuer.
    if not warrant_active(parent.warrant_id):
        raise DelegationDepthViolation(
            parent.warrant_id, parent.provenance_depth,
            expected_child_depth, parent.max_delegation_depth,
            "parent warrant is revoked or expired at root issuer",
        )

    # Clause 4: scope containment is necessary but checked last so that
    # depth violations surface with a precise, attributable reason.
    if not scope_contains(child_scope, parent.scope):
        raise DelegationDepthViolation(
            parent.warrant_id, parent.provenance_depth,
            expected_child_depth, parent.max_delegation_depth,
            f"child scope {child_scope!r} not contained in parent scope {parent.scope!r}",
        )

    return {
        "scope": child_scope,
        "provenance_depth": expected_child_depth,
        "max_delegation_depth": parent.max_delegation_depth - 1,
        "parent_warrant": parent.warrant_id,
    }
