# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-loads-a-skill-registry-whose-manifest-hashes-diverge-from-the-runtime-skill-bytes-a-boundary-study-of-skill-injection-su.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-loads-a-skill-registry-whose-manifest-hashes-diverge-from-the-runtime-skill-bytes-a-boundary-study-of-skill-injection-su.warrant.json
# Markdown SHA-256:   618122a5c8d577924f0adb54da9ed16bc92226e60a20b0e81db4d20367fb06ee
# Code AST SHA-256:   fbee4d40622917837a510f8dc81f8dc15ad826b7cd8cbd32341bbdd48cab50ab
# Verified Classes:   SkillIntegrityError, SkillSelection
# Defensive Guards:   1 fail-closed invariants (zero external deps)

import hashlib
from dataclasses import dataclass


class SkillIntegrityError(RuntimeError):
    """Raised when the executed skill bytes do not match the hash the agent authorized."""


@dataclass(frozen=True)
class SkillSelection:
    name: str
    authorized_sha256: str  # hash recorded in the agent's selection trace


def load_and_verify(selection: SkillSelection, skill_bytes: bytes) -> bytes:
    actual = hashlib.sha256(skill_bytes).hexdigest()
    if actual != selection.authorized_sha256:
        raise SkillIntegrityError(
            f"skill {selection.name!r} bytes hash {actual} != authorized "
            f"{selection.authorized_sha256}; refusing to execute (possible registry drift "
            f"or injection between manifest fetch and import)"
        )
    return skill_bytes


def invoke_skill(selection: SkillSelection, skill_bytes: bytes, kwargs: dict) -> dict:
    verified = load_and_verify(selection, skill_bytes)
    # The model authorized `selection`; only these bytes may run.
    return {"skill": selection.name, "sha256": selection.authorized_sha256, "ran": True}
