# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-accept-a-delegated-agent-credential-whose-warrant-chain-silently-truncates-at-a-re-delegation-depth-the-verifier-never-counted.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-accept-a-delegated-agent-credential-whose-warrant-chain-silently-truncates-at-a-re-delegation-depth-the-verifier-never-counted.warrant.json
# Markdown SHA-256:   f9aa152d7101982e493d50bc7e715571e1a7eac42d691c08c51c4c50ec639cbc
# Code AST SHA-256:   bbba1ed6f7babd1544a0afd3adb88894b8bd32aa207117aecc5c252332283195
# Verified Classes:   DelegationDepthMismatch
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class DelegationDepthMismatch(RuntimeError):
    """Raised when a warrant's signed delegation_depth disagrees with the
    verifier's reconstruction, or when a cache hit would elide the
    monotonicity check. Fail closed: never accept on a skipped walk."""
    pass


def verify_warrant_depth(warrant, parent, root_max_depth):
    # warrant: dict with keys
    #   warrant_id, delegation_depth (signed int), parent_ref
    # parent: dict or None (None only for the root-issued W0)
    # root_max_depth: the issuer's max_delegation_depth bound

    depth = warrant["delegation_depth"]

    if parent is None:
        # Only the root-issued warrant may have depth 0 and no parent.
        if depth != 0:
            raise DelegationDepthMismatch(
                f"warrant {warrant['warrant_id']} has depth {depth} "
                f"but no parent_ref; non-root warrant cannot be depth 0"
            )
        return depth

    # Monotonicity assertion: MUST run even when parent was a cache hit.
    # A cache hit may skip the walk, never this check.
    expected = parent["delegation_depth"] + 1
    if depth != expected:
        raise DelegationDepthMismatch(
            f"warrant {warrant['warrant_id']} asserts depth {depth} "
            f"but parent {parent['warrant_id']} has depth "
            f"{parent['delegation_depth']}; expected {expected}. "
            f"Refusing to accept a chain whose depth was "
            f"reconstructed by traversal rather than read from the "
            f"signed counter."
        )

    # Enforce the root bound against the SIGNED depth, not a walk length.
    if depth > root_max_depth:
        raise DelegationDepthMismatch(
            f"warrant {warrant['warrant_id']} depth {depth} exceeds "
            f"root max_delegation_depth {root_max_depth}; "
            f"authority creep beyond issuer intent"
        )

    return depth


def mint_delegated_warrant(parent, root_max_depth):
    # Issuer-side guard: refuse to mint past the root bound.
    next_depth = parent["delegation_depth"] + 1
    if next_depth > root_max_depth:
        raise DelegationDepthMismatch(
            f"refusing to mint: next depth {next_depth} exceeds "
            f"root max_delegation_depth {root_max_depth}"
        )
    return {"delegation_depth": next_depth, "parent_ref": parent["warrant_id"]}
