# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-to-present-a-delegated-agent-credential-whose-delegation-chain-exceeds-the-verifier-s-stated-hop-budget.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-to-present-a-delegated-agent-credential-whose-delegation-chain-exceeds-the-verifier-s-stated-hop-budget.warrant.json
# Markdown SHA-256:   1ef9eda401b13e4cccd871b666ed9876858c9e5c35a9a51866136ee672ab3719
# Code AST SHA-256:   71478f6cac75073b36ca2e2cef3119f9273fb92267f1084c09d77414fa2f87f0
# Verified Classes:   DelegationDepthViolation
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class DelegationDepthViolation(RuntimeError):
    """Raised when an agent would present a credential chain deeper than the verifier's budget."""
    pass


def build_presentation(chain, verifier, agent_scope):
    """
    chain: list of hops, root issuance = hop 1. len(chain) is the depth.
    verifier: object with .max_chain_depth (int|None) and .on_overflow in {"reject","evaluate_prefix"}.
    agent_scope: frozenset of capabilities the presenting agent truly holds.
    """
    depth = len(chain)
    budget = verifier.max_chain_depth

    # Unbounded verifier: admissible.
    if budget is None:
        return {"chain": chain, "depth": depth}

    if depth <= budget:
        return {"chain": chain, "depth": depth}

    # depth > budget
    if verifier.on_overflow == "evaluate_prefix":
        prefix_terminal_scope = chain[budget - 1].scope  # last hop the verifier will evaluate
        if agent_scope.issubset(prefix_terminal_scope):
            # Safe: true authority is contained in the prefix the verifier will accept.
            return {"chain": chain, "depth": depth, "truncation_acknowledged": True}
        raise DelegationDepthViolation(
            f"chain depth {depth} exceeds budget {budget}; prefix terminal scope "
            f"{set(prefix_terminal_scope)} does not contain agent scope {set(agent_scope)}"
        )

    # on_overflow == "reject" or unspecified: must refuse, never truncate.
    raise DelegationDepthViolation(
        f"refusing to present: chain depth {depth} > verifier max_chain_depth {budget} "
        f"with on_overflow={verifier.on_overflow!r}; request shallower re-delegation"
    )
