# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-resolves-a-skill-descriptor-whose-declared-capability-set-is-a-strict-superset-of-the-manifest-it-was-signed-against.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-resolves-a-skill-descriptor-whose-declared-capability-set-is-a-strict-superset-of-the-manifest-it-was-signed-against.warrant.json
# Markdown SHA-256:   7653818ec95d927354bc1f96dc721e37c7ddeb1a36cc68abb444ede8812adc07
# Code AST SHA-256:   763b87bdc9568eedea8dbcaf3db3dab41c807764877fba2210044d36aa40cc24
# Verified Classes:   SupersetGrant, Capability, SkillManifest, SkillDescriptor
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field


class SupersetGrant(RuntimeError):
    """Raised when a resolved skill descriptor requests capabilities outside
    the signed manifest envelope. This is a hostile condition, not a warning:
    the signature is valid but the grant would exceed what was authorized."""


@dataclass(frozen=True)
class Capability:
    kind: str          # e.g. "fs:read", "net:outbound", "exec"
    scope: str         # concrete path/host, or "*" for wildcard

    def __le__(self, other: "Capability") -> bool:
        # Lattice containment: same kind, and scope is equal or other is wildcard.
        return self.kind == other.kind and (other.scope == "*" or self.scope == other.scope)


@dataclass
class SkillManifest:
    skill_id: str
    version: str
    capabilities: list[Capability]
    signature_valid: bool


@dataclass
class SkillDescriptor:
    skill_id: str
    version: str
    requested_capabilities: list[Capability] = field(default_factory=list)


def _subset(requested: list[Capability], envelope: list[Capability]) -> bool:
    return all(any(req <= allowed for allowed in envelope) for req in requested)


def resolve_grant(manifest: SkillManifest, descriptor: SkillDescriptor) -> list[Capability]:
    if not manifest.signature_valid:
        raise SupersetGrant(f"manifest signature invalid for {manifest.skill_id}@{manifest.version}")

    if (manifest.skill_id, manifest.version) != (descriptor.skill_id, descriptor.version):
        raise SupersetGrant(
            f"descriptor identity mismatch: {descriptor.skill_id}@{descriptor.version} "
            f"resolved against {manifest.skill_id}@{manifest.version}"
        )

    if not _subset(descriptor.requested_capabilities, manifest.capabilities):
        over = [c for c in descriptor.requested_capabilities
                if not any(c <= a for a in manifest.capabilities)]
        raise SupersetGrant(
            f"descriptor for {manifest.skill_id}@{manifest.version} requests capabilities "
            f"outside signed envelope: {over}"
        )

    # Grant is the descriptor's set ONLY because containment was proven.
    return list(descriptor.requested_capabilities)


if __name__ == "__main__":
    manifest = SkillManifest(
        skill_id="summarize", version="1.4.2",
        capabilities=[Capability("fs:read", "/data/inbox"),
                      Capability("net:outbound", "summarizer.internal")],
        signature_valid=True,
    )
    poisoned = SkillDescriptor(
        skill_id="summarize", version="1.4.2",
        requested_capabilities=[Capability("fs:read", "*"),
                                Capability("net:outbound", "*"),
                                Capability("exec", "shell")],
    )
    resolve_grant(manifest, poisoned)  # raises SupersetGrant
