# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-a-brokered-capability-warrant-whose-issuer-chain-validates-but-whose-delegation-depth-was-silently-reset.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-a-brokered-capability-warrant-whose-issuer-chain-validates-but-whose-delegation-depth-was-silently-reset.warrant.json
# Markdown SHA-256:   9a7105a7829600aebc2704320c793d123ab9ed739f1bc6644fc98053fde639b0
# Code AST SHA-256:   592f0b55c98e2e71202e1dabd43a0053df5b569caf1549c13c3b5a0ce77d0458
# Verified Classes:   WarrantAdmissibilityError
# Defensive Guards:   6 fail-closed invariants (zero external deps)

class WarrantAdmissibilityError(RuntimeError):
    """Raised when a brokered capability warrant fails monotonicity checks
    despite having a cryptographically valid signature chain."""
    pass


def assert_warrant_admissible(leaf, resolve_parent, trusted_roots, max_chain_length=8):
    chain = []
    cursor = leaf
    while cursor is not None:
        chain.append(cursor)
        if len(chain) > max_chain_length:
            raise WarrantAdmissibilityError(
                f"chain length exceeds cap {max_chain_length}; "
                f"possible depth-reset laundering at hop {len(chain) - 1}"
            )
        cursor = resolve_parent(cursor.parent_hash) if cursor.parent_hash else None

    root = chain[-1]
    if root.issuer not in trusted_roots:
        raise WarrantAdmissibilityError(
            f"root issuer {root.issuer!r} not in trusted_roots"
        )

    for hop, (parent, child) in enumerate(zip(chain[-1:0:-1], chain[-2::-1])):
        if child.delegation_depth != parent.delegation_depth + 1:
            raise WarrantAdmissibilityError(
                f"depth reset at hop {hop}: parent depth {parent.delegation_depth}, "
                f"child depth {child.delegation_depth} (expected "
                f"{parent.delegation_depth + 1})"
            )
        if child.originating_org != parent.originating_org:
            raise WarrantAdmissibilityError(
                f"origin laundering at hop {hop}: parent origin "
                f"{parent.originating_org!r}, child origin {child.originating_org!r}"
            )
        if not child.allowed_actions.issubset(parent.allowed_actions):
            raise WarrantAdmissibilityError(
                f"scope widening at hop {hop}: child actions "
                f"{child.allowed_actions - parent.allowed_actions} not in parent scope"
            )
        if child.expires_at > parent.expires_at:
            raise WarrantAdmissibilityError(
                f"expiry extension at hop {hop}: child expires "
                f"{child.expires_at} after parent {parent.expires_at}"
            )

    return {
        "root_origin": root.originating_org,
        "true_depth": leaf.delegation_depth,
        "leaf_actions": leaf.allowed_actions,
        "intermediaries": [w.issuer for w in chain[1:]],
    }
