# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-reject-a-skill-manifest-whose-declared-capabilities-understate-its-transitive-tool-reach.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-reject-a-skill-manifest-whose-declared-capabilities-understate-its-transitive-tool-reach.warrant.json
# Markdown SHA-256:   58357b7976b0f27d4dd076dad18bd3e0c3c14b8059650f062254064a038721d2
# Code AST SHA-256:   4d4f669648903f91c50d06f4bdf6e19b4b6acac76925f0c3d6980aa967631b57
# Verified Classes:   CapabilityClosureViolation
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class CapabilityClosureViolation(RuntimeError):
    """Raised when a skill's transitive capability closure exceeds the allowlist,
    or when the closure cannot be computed before admission."""
    pass


def admit_skill(manifest: dict, resolver, allowlist: set[str]) -> dict:
    # Eagerly resolve the full tool graph. Lazy resolution is itself a violation:
    # it defers closure computation past the point of decision.
    try:
        graph = resolver.resolve_closure(manifest)  # must be complete, pinned
    except resolver.LazyResolutionError as e:
        raise CapabilityClosureViolation(
            f"skill {manifest.get('name')!r} cannot be admitted: "
            f"dependency closure not computable before execution ({e})"
        )

    offending: list[tuple[str, str]] = []
    for node in graph.nodes:
        if node.version is None:
            raise CapabilityClosureViolation(
                f"tool {node.name!r} in closure of {manifest.get('name')!r} "
                f"is unpinned; version drift makes declaration unverifiable"
            )
        for cap in node.declared_capabilities:
            if cap not in allowlist:
                offending.append((node.name, cap))

    if offending:
        raise CapabilityClosureViolation(
            f"skill {manifest.get('name')!r} denied: transitive capabilities "
            f"outside allowlist: {offending}"
        )

    return {"decision": "allow", "skill": manifest["name"], "closure": graph}
