# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-quarantine-a-skill-s-declared-capability-manifest-from-its-observed-tool-call-behavior-before-granting-execution-ri.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-quarantine-a-skill-s-declared-capability-manifest-from-its-observed-tool-call-behavior-before-granting-execution-ri.warrant.json
# Markdown SHA-256:   aa56e61e2e023c122fe59cd85d9a354441d0531deb9fb2de59192b239793815b
# Code AST SHA-256:   789c98f617aa9c473dc667d6ac91f43d2b643923886ecda4d78904324eb68af1
# Verified Classes:   SkillAdmissionError, Transport, SkillManifest, AdmissionDecision
# Defensive Guards:   3 fail-closed invariants (zero external deps)

import json
from dataclasses import dataclass, field
from enum import Enum


class SkillAdmissionError(RuntimeError):
    """Raised when a skill's manifest is inconsistent with its transport or schema."""
    pass


class Transport(Enum):
    IN_PROCESS = "in_process"      # pure function, no ambient capability
    SUBPROCESS = "subprocess"      # local exec, filesystem + env reachable
    REMOTE_MCP = "remote_mcp"      # network inherently reachable
    HTTP = "http"                  # network inherently reachable


# Transport floor: the minimum capability a transport grants regardless of manifest.
TRANSPORT_FLOOR = {
    Transport.IN_PROCESS: {"read"},
    Transport.SUBPROCESS: {"read", "write", "exec"},
    Transport.REMOTE_MCP: {"read", "network"},
    Transport.HTTP: {"read", "network"},
}


@dataclass
class SkillManifest:
    name: str
    transport: Transport
    declared_side_effects: set
    parameters: dict            # JSON-schema-like properties
    free_form_string_params: bool = field(init=False)

    def __post_init__(self):
        props = self.parameters.get("properties", {})
        # A free-form string is any string param with no enum/format constraint.
        self.free_form_string_params = any(
            p.get("type") == "string" and "enum" not in p and "format" not in p
            for p in props.values()
        )


class AdmissionDecision(Enum):
    ADMIT = "admit"
    QUARANTINE = "quarantine"
    REJECT = "reject"


def evaluate(manifest: SkillManifest) -> AdmissionDecision:
    floor = TRANSPORT_FLOOR[manifest.transport]

    # Rule 1: transport floor must not exceed declared ceiling.
    # A remote skill declaring no network is self-contradictory.
    if not floor.issubset(manifest.declared_side_effects):
        missing = floor - manifest.declared_side_effects
        raise SkillAdmissionError(
            f"skill {manifest.name!r}: transport {manifest.transport.value} "
            f"grants {sorted(missing)} but manifest declares "
            f"{sorted(manifest.declared_side_effects)}"
        )

    # Rule 2: free-form string over a capability-bearing transport
    # is an unmodeled input channel -> quarantine, do not admit.
    if manifest.free_form_string_params and manifest.transport is not Transport.IN_PROCESS:
        return AdmissionDecision.QUARANTINE

    return AdmissionDecision.ADMIT


def bind_skill(manifest: SkillManifest, sandbox_available: bool) -> AdmissionDecision:
    decision = evaluate(manifest)
    if decision is AdmissionDecision.QUARANTINE and not sandbox_available:
        # Cannot quarantine safely -> must reject rather than admit with ambient rights.
        raise SkillAdmissionError(
            f"skill {manifest.name!r} requires quarantine "
            f"(free-form string over {manifest.transport.value}) "
            f"but no sandbox is available to bound it"
        )
    return decision


# Reproduction of the observed failure: the summarize_url skill.
if __name__ == "__main__":
    bad = SkillManifest(
        name="summarize_url",
        transport=Transport.REMOTE_MCP,
        declared_side_effects={"read"},          # lies: omits network
        parameters={"properties": {"text": {"type": "string"}}},
    )
    try:
        bind_skill(bad, sandbox_available=True)
    except SkillAdmissionError as e:
        print(f"blocked at admission: {e}")

    hidden_channel = SkillManifest(
        name="summarize_url_v2",
        transport=Transport.REMOTE_MCP,
        declared_side_effects={"read", "network"},   # honest about network
        parameters={"properties": {"text": {"type": "string"}}},
    )
    # Passes rule 1, but free-form string over remote transport -> quarantine.
    assert bind_skill(hidden_channel, sandbox_available=True) is AdmissionDecision.QUARANTINE
