# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-reject-a-tool-manifest-whose-declared-capability-set-is-a-strict-subset-of-the-capabilities-the-skill-actually.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-reject-a-tool-manifest-whose-declared-capability-set-is-a-strict-subset-of-the-capabilities-the-skill-actually.warrant.json
# Markdown SHA-256:   5be86dc6ae65168151c35635f2dccf97bee0c82e579a18b714c8c1a2b685aaeb
# Code AST SHA-256:   ecba583e825e377a694f2d1cbb3f526e21047e5401690a0ff7e26a3ed066c654
# Verified Classes:   SkillCapabilityUnderdeclaredError, SkillManifest
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import FrozenSet

class SkillCapabilityUnderdeclaredError(RuntimeError):
    """Raised when a skill's exercised capability set is not a subset of its
    signed manifest's declared capability set. A signed under-declaration is
    treated as a forgery, not a warning."""
    pass

TOP = "*"  # sentinel for the top element of the capability lattice

@dataclass(frozen=True)
class SkillManifest:
    name: str
    version: str
    payload_hash: str
    declared_capabilities: FrozenSet[str]
    signature: bytes

def verify_manifest_signature(m: SkillManifest, root_pubkey: bytes) -> bool:
    # Signature MUST cover (name, version, payload_hash, declared_capabilities).
    # A signature over payload_hash alone is insufficient: the sidecar could be swapped.
    signed_tuple = (m.name, m.version, m.payload_hash, tuple(sorted(m.declared_capabilities)))
    return _ed25519_verify(root_pubkey, _canonical(signed_tuple), m.signature)

def admit_skill(m: SkillManifest, exercised: FrozenSet[str], root_pubkey: bytes) -> None:
    if not verify_manifest_signature(m, root_pubkey):
        raise SkillCapabilityUnderdeclaredError(
            f"{m.name}@{m.version}: signature invalid over (payload, capabilities)"
        )
    if TOP in exercised and TOP not in m.declared_capabilities:
        raise SkillCapabilityUnderdeclaredError(
            f"{m.name}@{m.version}: exercised set is top (dynamic resolution); "
            f"declared set {sorted(m.declared_capabilities)} cannot bound it"
        )
    under = exercised - m.declared_capabilities
    if under:
        raise SkillCapabilityUnderdeclaredError(
            f"{m.name}@{m.version}: exercised capabilities {sorted(under)} "
            f"not in declared set {sorted(m.declared_capabilities)}"
        )
    # admitted: exercised ⊆ declared under closed resolution
