# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-signed-skill-manifest-as-insufficient-proof-of-skill-provenance-at-load-time.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-signed-skill-manifest-as-insufficient-proof-of-skill-provenance-at-load-time.warrant.json
# Markdown SHA-256:   b9da7976d474ed1f5b0783e14b79f068d7af3c935b68be84f8793df9ba2ccf48
# Code AST SHA-256:   c5692d59444e58c237cd0f8afb342c7c912d344abbf1e7187a0ad8834a03ef0d
# Verified Classes:   ProvenanceBoundaryError
# Defensive Guards:   5 fail-closed invariants (zero external deps)

class ProvenanceBoundaryError(RuntimeError):
    """Raised when a signed manifest does not bound the executing skill."""
    pass


def load_skill(manifest, bundle, sandbox):
    # 1. Signature and hash checks (necessary, not sufficient).
    if not verify_signature(manifest):
        raise ProvenanceBoundaryError("manifest signature invalid")
    if not hash_matches(manifest.content_hash, bundle):
        raise ProvenanceBoundaryError("bundle hash mismatch")

    # 2. Hash closure: does the manifest cover everything that can execute?
    resolved = bundle.resolve_runtime_dependencies()
    if not manifest.covers(resolved):
        # Signed, but the signature does not attest to the executing artifact.
        raise ProvenanceBoundaryError(
            "hash closure violated: runtime-resolved artifacts "
            f"{resolved.uncovered()} are not covered by manifest hash"
        )

    # 3. Capability gate must enforce at the execution layer, not the API layer.
    if not sandbox.enforces_at_execution_layer(manifest.capabilities):
        raise ProvenanceBoundaryError(
            "capability gate attached to API layer; declared "
            f"{manifest.capabilities} are not enforced on skill internals"
        )

    # 4. Default-deny: declared set must be closed.
    if not manifest.capabilities.is_closed():
        raise ProvenanceBoundaryError(
            "capability set is open; default-allow cannot bound behavior"
        )

    # 5. Output provenance: skill output is labeled, not promoted to instruction.
    skill = sandbox.load(bundle, manifest.capabilities)
    skill.output_tier = "untrusted-skill-output"
    return skill
