# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-a-skill-registry-manifest-whose-declared-capability-set-is-a-strict-superset-of-the-capabilities-observe.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-a-skill-registry-manifest-whose-declared-capability-set-is-a-strict-superset-of-the-capabilities-observe.warrant.json
# Markdown SHA-256:   15d34218ac8c513d55a10f08242f3eef54d0ed077036fc2fefa73b7ae3f98533
# Code AST SHA-256:   438392cd53188b73e385f95bf607dcc58cc746c010ab060253acbde6d5b091c4
# Verified Classes:   SkillGrantError, ProbeState, ProbeResult, SkillManifest, Grant
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from enum import Enum


class SkillGrantError(RuntimeError):
    """Raised when a skill grant cannot be justified by probe provenance."""


class ProbeState(Enum):
    EXERCISED = "exercised"
    AVAILABLE_NOT_EXERCISED = "available-not-exercised"
    UNAVAILABLE = "unavailable"


@dataclass
class ProbeResult:
    envelope: frozenset[str]          # capabilities the probe sandbox could mount
    states: dict[str, ProbeState]     # per declared capability
    fixture_hash: str


@dataclass
class SkillManifest:
    name: str
    declared: frozenset[str]


@dataclass
class Grant:
    skill: str
    capabilities: frozenset[str]
    provenance: ProbeResult
    fixture_hash: str


def authorize_grant(manifest: SkillManifest, probe: ProbeResult) -> Grant:
    # Invariant: any declared capability not EXERCISED must be either
    # AVAILABLE_NOT_EXERCISED (measured, dormant -> safe to grant) or the
    # manifest is refused. UNAVAILABLE means the probe could not have seen
    # the effect; silence is not evidence of absence.
    unmeasured: list[str] = []
    for cap in manifest.declared:
        state = probe.states.get(cap, ProbeState.UNAVAILABLE)
        if state is ProbeState.UNAVAILABLE:
            unmeasured.append(cap)

    if unmeasured:
        raise SkillGrantError(
            f"skill {manifest.name!r} declares capabilities the probe could not "
            f"exercise: {sorted(unmeasured)}; probe envelope was "
            f"{sorted(probe.envelope)}. Refusing grant: declared superset of "
            f"observed is only benign when the probe could have observed it."
        )

    # Envelope must cover the declared set, else the tri-state is unreliable.
    uncovered = manifest.declared - probe.envelope
    if uncovered:
        raise SkillGrantError(
            f"skill {manifest.name!r} probe envelope does not cover declared "
            f"capabilities {sorted(uncovered)}; probe provenance invalid."
        )

    return Grant(
        skill=manifest.name,
        capabilities=manifest.declared,
        provenance=probe,
        fixture_hash=probe.fixture_hash,
    )


def resume_from_checkpoint(grant: Grant, current_envelope: frozenset[str]) -> Grant:
    # On resume the restored grant must be re-validated against the live
    # sandbox; a grant made under a wider envelope than the current one
    # carries unmeasured capabilities into a narrower runtime.
    if not grant.capabilities <= current_envelope:
        missing = grant.capabilities - current_envelope
        raise SkillGrantError(
            f"resume of skill {grant.skill!r} would grant {sorted(missing)} "
            f"absent from current sandbox envelope {sorted(current_envelope)}; "
            f"restored grant provenance no longer holds."
        )
    return grant
