# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-of-a-signed-skill-registry-pass-verification-yet-execute-unregistered-code-the-descriptor-vs-payload-split-in-dynamic-agent-skill.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-of-a-signed-skill-registry-pass-verification-yet-execute-unregistered-code-the-descriptor-vs-payload-split-in-dynamic-agent-skill.warrant.json
# Markdown SHA-256:   505027d299cf8864ebdd94875a8e4fdc2ead474694199c2c612ef9a9ee5870ea
# Code AST SHA-256:   6b900d4a77ebdb9dd7d62a8c44b5e71f620a996e397a0a8554f30e5124f61703
# Verified Classes:   SkillResolutionError, Attestation
# Defensive Guards:   2 fail-closed invariants (zero external deps)

import hashlib
from dataclasses import dataclass

class SkillResolutionError(RuntimeError):
    """Raised when a resolved skill payload is not covered by the verified attestation."""

@dataclass(frozen=True)
class Attestation:
    skill_name: str
    descriptor_digest: str
    payload_digests: frozenset  # digests of resolved module bytes / wheels

def _hash_bytes(data: bytes) -> str:
    return "sha256:" + hashlib.sha256(data).hexdigest()

def activate_skill(attestation: Attestation, resolved_payload: bytes,
                   resolved_module_path: str, search_root: str) -> None:
    # Gate 1: resolution must occur inside the sealed root, not ambient paths.
    if not resolved_module_path.startswith(search_root):
        raise SkillResolutionError(
            f"entrypoint {resolved_module_path!r} resolved outside sealed root "
            f"{search_root!r}; ambient path shadowing suspected for "
            f"{attestation.skill_name}"
        )

    # Gate 2: the executed bytes must be a subject of the verified attestation.
    actual = _hash_bytes(resolved_payload)
    if actual not in attestation.payload_digests:
        raise SkillResolutionError(
            f"payload hash {actual} for {attestation.skill_name} is not in the "
            f"attested subject set {sorted(attestation.payload_digests)}; "
            f"descriptor {attestation.descriptor_digest} verified but payload "
            f"substituted after resolution"
        )

    # Only now is it safe to hand the bytes to the interpreter.
    exec(compile(resolved_payload, resolved_module_path, "exec"),
         {"__name__": attestation.skill_name})
