# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-skill-injection-sanitizer-as-a-semantic-boundary-rather-than-a-syntactic-one-and-refuse-to-admit-a-tool-who.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-skill-injection-sanitizer-as-a-semantic-boundary-rather-than-a-syntactic-one-and-refuse-to-admit-a-tool-who.warrant.json
# Markdown SHA-256:   5f17ed5090df51cf738f30519b034387ba46f879aaf11b7c2a96acfd5edea4da
# Code AST SHA-256:   e3cb0d1ffb0dd8279820f3098479d64343e8c50b36c360f1ce8468cdedd2c503
# Verified Classes:   EffectClass, SkillAdmissionError, SkillManifest
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from enum import IntEnum
from typing import Any


class EffectClass(IntEnum):
    READ = 0
    WRITE = 1
    DESTRUCTIVE = 2
    EXFILTRATE = 3
    UNKNOWN = 4  # most restrictive; open-ended surfaces land here


class SkillAdmissionError(RuntimeError):
    """Raised when a tool's parameter surface contradicts its declared effect class."""
    pass


# Heuristic parameter-shape -> minimum inferred effect class.
# Incomplete by construction; raises the cost of laundering, does not eliminate it.
_MUTATION_HINTS = ("warm", "flush", "commit", "sync", "persist", "write", "set", "update")
_TRAVERSAL_HINTS = ("follow", "recurse", "depth", "traverse", "expand")
_NETWORK_HINTS = ("url", "endpoint", "callback", "webhook", "host")
_EXEC_HINTS = ("raw", "exec", "eval", "script", "command")
_SECRET_HINTS = ("token", "secret", "credential", "api_key", "password")


def _infer_param_effect(param_name: str) -> EffectClass:
    n = param_name.lower()
    if any(h in n for h in _EXEC_HINTS):
        return EffectClass.UNKNOWN          # executable-shaped: never read
    if any(h in n for h in _SECRET_HINTS):
        return EffectClass.EXFILTRATE
    if any(h in n for h in _NETWORK_HINTS):
        return EffectClass.EXFILTRATE       # read tool with URL param = SSRF surface
    if any(h in n for h in _TRAVERSAL_HINTS):
        return EffectClass.WRITE            # unbounded fetch side effects
    if any(h in n for h in _MUTATION_HINTS):
        return EffectClass.WRITE
    return EffectClass.READ


@dataclass
class SkillManifest:
    name: str
    declared_effect: EffectClass
    description: str
    parameters: dict[str, Any] = field(default_factory=dict)
    open_ended: bool = False  # True if it accepts **kwargs / free-form blob


def admit_skill(manifest: SkillManifest) -> EffectClass:
    """Return the effective (never-lower-than-declared) effect class, or raise."""
    if manifest.open_ended:
        # Cannot bound authority from an open surface; must not be admitted as declared.
        if manifest.declared_effect < EffectClass.UNKNOWN:
            raise SkillAdmissionError(
                f"tool '{manifest.name}' has open-ended arguments but declares "
                f"{manifest.declared_effect.name}; authority is unbounded"
            )
        return EffectClass.UNKNOWN

    inferred = EffectClass.READ
    offending: list[str] = []
    for pname in manifest.parameters:
        pe = _infer_param_effect(pname)
        if pe > manifest.declared_effect:
            offending.append(f"{pname}->{pe.name}")
        inferred = max(inferred, pe)

    if inferred > manifest.declared_effect:
        # Boundary crossed: declaration is a lower bound violation.
        raise SkillAdmissionError(
            f"tool '{manifest.name}' declares {manifest.declared_effect.name} "
            f"but parameter surface implies {inferred.name} "
            f"(offending: {', '.join(offending)}); reclassify upward or reject"
        )
    return manifest.declared_effect


# Observed failing case:
try:
    admit_skill(SkillManifest(
        name="lookup_reference",
        declared_effect=EffectClass.READ,
        description="Read-only retrieval of reference entries.",
        parameters={"entry_id": "string", "cache_warm": "boolean", "follow_links": "integer"},
    ))
except SkillAdmissionError as e:
    print("REFUSED:", e)
