# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-verify-a-skill-manifest-s-signed-scope-before-dynamic-registration-and-why-signature-validity-alone-does-not-bound-the-injectio.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-verify-a-skill-manifest-s-signed-scope-before-dynamic-registration-and-why-signature-validity-alone-does-not-bound-the-injectio.warrant.json
# Markdown SHA-256:   fb8daea948fe2ecdfb3564dd99caa617e7ac080cdf3aca53a0d906bc701e6584
# Code AST SHA-256:   199ab74ac47a44ce1e04bf888e951adbeab08a60e6e15330c85f6758b8de1a32
# Verified Classes:   ManifestConformanceError, ResolvedSkill, SignedManifest
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Callable


class ManifestConformanceError(RuntimeError):
    """Raised when a signed manifest is valid but its resolved execution
    surface does not conform to the scope the issuer actually signed."""
    pass


@dataclass(frozen=True)
class ResolvedSkill:
    skill_id: str
    resolved_closure_sha256: str
    concrete_resources: frozenset[str]


@dataclass(frozen=True)
class SignedManifest:
    skill_id: str
    signed_closure_sha256: str
    signed_resources: frozenset[str]
    min_version: int
    version: int


def verify_signature(manifest_bytes: bytes, issuer_pubkey: bytes) -> bool:
    # Ed25519 verification against the loader-pinned key. Returns True iff
    # the issuer signed these exact bytes.
    ...


def resolve_entrypoint_closure(skill_id: str) -> ResolvedSkill:
    # Resolves the *actual* import closure the loader will execute,
    # following namespace shadowing and entrypoint aliasing.
    ...


def register_skill(
    manifest_bytes: bytes,
    issuer_pubkey: bytes,
    manifest: SignedManifest,
    last_seen_version: int,
    resolve: Callable[[str], ResolvedSkill] = resolve_entrypoint_closure,
) -> ResolvedSkill:
    # Step 1: signature is necessary, not sufficient.
    if not verify_signature(manifest_bytes, issuer_pubkey):
        raise ManifestConformanceError(
            f"{manifest.skill_id}: signature invalid against pinned issuer key"
        )

    # Step 2: freshness — refuse rollback below last-seen signed version.
    if manifest.version < manifest.min_version:
        raise ManifestConformanceError(
            f"{manifest.skill_id}: version {manifest.version} below signed "
            f"min_version {manifest.min_version}"
        )
    if manifest.version < last_seen_version:
        raise ManifestConformanceError(
            f"{manifest.skill_id}: rollback from {last_seen_version} to "
            f"{manifest.version} refused"
        )

    # Step 3: artifact binding — resolved closure must equal signed closure.
    resolved = resolve(manifest.skill_id)
    if resolved.resolved_closure_sha256 != manifest.signed_closure_sha256:
        raise ManifestConformanceError(
            f"{manifest.skill_id}: resolved closure "
            f"{resolved.resolved_closure_sha256[:12]} != signed closure "
            f"{manifest.signed_closure_sha256[:12]} (namespace shadowing or "
            f"entrypoint aliasing)"
        )

    # Step 4: scope concreteness — every concrete resource the skill will
    # touch must be named in the signed scope. Wildcard classes do not count.
    unbound = resolved.concrete_resources - manifest.signed_resources
    if unbound:
        raise ManifestConformanceError(
            f"{manifest.skill_id}: resolved resources {sorted(unbound)} not "
            f"covered by signed scope {sorted(manifest.signed_resources)}"
        )

    return resolved
