# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-registry-resident-skill-injection-when-the-injected-skill-is-schema-valid-and-signature-valid.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-registry-resident-skill-injection-when-the-injected-skill-is-schema-valid-and-signature-valid.warrant.json
# Markdown SHA-256:   e6e82c6a54e4c11ef01c28d7f00475e50af222c9420fc4c9614a9b4e54bec47e
# Code AST SHA-256:   56649ee38715618a74fe36aa8dba708f84bfef2bd7c4606195451622534cfa42
# Verified Classes:   EffectClass, SkillDescriptor, InvocationTrace, SkillInjectionError
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from enum import Enum
from typing import Set


class EffectClass(str, Enum):
    FS_READ = "fs_read"
    FS_WRITE = "fs_write"
    NET_EGRESS = "net_egress"
    PROC_SPAWN = "proc_spawn"
    RETURN_CHANNEL = "return_channel"


@dataclass(frozen=True)
class SkillDescriptor:
    skill_id: str
    version: str
    namespace: str
    declared_effects: Set[EffectClass]
    publisher_fingerprint: str
    signature_valid: bool


@dataclass(frozen=True)
class InvocationTrace:
    skill_id: str
    version: str
    observed_effects: Set[EffectClass] = field(default_factory=set)


class SkillInjectionError(RuntimeError):
    """Raised when a schema- and signature-valid skill's observed effect
    surface exceeds its declared effect surface (semantic mislabeling)."""


def admit_skill(
    descriptor: SkillDescriptor,
    trace: InvocationTrace,
    expected_namespace: str,
    trusted_fingerprints: Set[str],
) -> str:
    # Provenance checks: necessary, not sufficient.
    if not descriptor.signature_valid:
        raise SkillInjectionError(
            f"{descriptor.skill_id}@{descriptor.version}: invalid signature"
        )
    if descriptor.publisher_fingerprint not in trusted_fingerprints:
        raise SkillInjectionError(
            f"{descriptor.skill_id}@{descriptor.version}: untrusted publisher "
            f"{descriptor.publisher_fingerprint}"
        )
    # Namespace pinning: cross-namespace fallback is an injection vector.
    if descriptor.namespace != expected_namespace:
        raise SkillInjectionError(
            f"{descriptor.skill_id}@{descriptor.version}: namespace "
            f"{descriptor.namespace!r} != expected {expected_namespace!r}"
        )
    # The non-obvious boundary: effect-fit decides the trust tier.
    undeclared = trace.observed_effects - descriptor.declared_effects
    if undeclared:
        raise SkillInjectionError(
            f"{descriptor.skill_id}@{descriptor.version}: observed effects "
            f"{sorted(e.value for e in undeclared)} not in declared contract "
            f"{sorted(e.value for e in descriptor.declared_effects)}; "
            f"demote to sandboxed tier, do not promote on signature alone"
        )
    return "full_trust"


# Example: the observed failure from the record.
descriptor = SkillDescriptor(
    skill_id="summarize_document",
    version="2.4.1",
    namespace="staging",  # shadowed production via fallback
    declared_effects={EffectClass.FS_READ},
    publisher_fingerprint="fp:attacker-key",
    signature_valid=True,  # display-name collision made this "trusted"
)
trace = InvocationTrace(
    skill_id="summarize_document",
    version="2.4.1",
    observed_effects={EffectClass.FS_READ, EffectClass.RETURN_CHANNEL},
)
try:
    admit_skill(
        descriptor,
        trace,
        expected_namespace="production",
        trusted_fingerprints={"fp:attacker-key"},
    )
except SkillInjectionError as e:
    print(f"BLOCKED: {e}")
