# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-a-poisoned-skill-descriptor-before-the-registry-admits-it-into-the-live-tool-schema.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-a-poisoned-skill-descriptor-before-the-registry-admits-it-into-the-live-tool-schema.warrant.json
# Markdown SHA-256:   ab9928b2db9008cae1cd62b1a7e0f0566009c41059f47d1bd3552ea8e7607de7
# Code AST SHA-256:   a7b3fd863b3a1d7752c697b8a28fd7ef913184c2d29810cf686937ad4411a0d0
# Verified Classes:   SkillAdmissionError, Descriptor
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class SkillAdmissionError(RuntimeError):
    """Raised when a dynamically loaded skill descriptor must not enter the live schema."""
    pass


class Descriptor:
    def __init__(self, name, description, capabilities, signer, parameters):
        self.name = name
        self.description = description
        self.capabilities = set(capabilities)   # e.g. {"fs.read", "net.egress"}
        self.signer = signer                    # signing principal id
        self.parameters = parameters


def admit_descriptor(desc: Descriptor, task_grant: set, allowlisted_signers: set) -> Descriptor:
    # Structural validity is assumed already checked upstream; this gate is
    # provenance + capability, NOT content. Content is undecidable; provenance is not.
    if desc.signer not in allowlisted_signers:
        raise SkillAdmissionError(
            f"skill '{desc.name}' signer '{desc.signer}' not in task allowlist; quarantine"
        )
    escalation = desc.capabilities - task_grant
    if escalation:
        # A descriptor declaring capabilities beyond the task grant is the
        # strongest injection signal. Abort the load, do not silently drop.
        raise SkillAdmissionError(
            f"skill '{desc.name}' declares {sorted(escalation)} outside task grant; abort"
        )
    return desc


def load_skill_set(descriptors, task_grant, allowlisted_signers):
    admitted = []
    for d in descriptors:
        try:
            admitted.append(admit_descriptor(d, task_grant, allowlisted_signers))
        except SkillAdmissionError as e:
            # Quarantine path: log, continue with remaining set.
            # Escalation is fatal to the whole load by policy; re-raise here.
            raise
    return admitted
