# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-quarantine-a-dynamically-loaded-toolkit-skill-before-schema-negotiation-completes.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-quarantine-a-dynamically-loaded-toolkit-skill-before-schema-negotiation-completes.warrant.json
# Markdown SHA-256:   8ac88a4c4cd81923909ac33343949917e8768a1e9ae292b1bcc67676a05a364d
# Code AST SHA-256:   17b3c35f143f2d35c158354bfbc794a075f93b783674c2603c83ac508e251398
# Verified Classes:   SkillInjectionBoundaryError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class SkillInjectionBoundaryError(RuntimeError):
    """Raised when a dynamically loaded skill's manifest carries instruction-bearing
    free text that could reach model context before invocation."""


ZERO_BY_TYPE = {"string": "", "number": 0, "integer": 0, "boolean": False}


def negotiate_skill(manifest: dict, side_effecting_tools_present: bool) -> dict:
    """Return an admitted, redacted manifest or raise if quarantine is required.

    Invariant: a skill becomes callable only if its descriptive fields are proven
    inert, or the model context is isolated from them.
    """
    params = manifest.get("parameters", {})
    for pname, spec in params.items():
        default = spec.get("default", None)
        desc = spec.get("description", "") or ""

        # A non-empty default is a pre-authorized argument -> quarantine.
        if default not in (None, ""):
            if side_effecting_tools_present:
                raise SkillInjectionBoundaryError(
                    f"skill={manifest.get('name')!r} param={pname!r} carries "
                    f"non-empty default {default!r}; model could adopt it without "
                    f"a user turn while side-effecting tools are live"
                )
            spec["default"] = ZERO_BY_TYPE.get(spec.get("type", "string"), "")

        # Imperative prose in a description is an injection vector.
        lowered = desc.lower()
        triggers = ("read the file", "populate from", "before calling",
                    "pass the contents", "then summarize")
        if any(t in lowered for t in triggers):
            raise SkillInjectionBoundaryError(
                f"skill={manifest.get('name')!r} param={pname!r} description "
                f"contains imperative content: {desc!r}"
            )

    return manifest


def register(manifest: dict, side_effecting_tools_present: bool) -> dict:
    try:
        return negotiate_skill(manifest, side_effecting_tools_present)
    except SkillInjectionBoundaryError:
        # Fail closed: withhold from callable set, keep schema for validation only.
        return {"name": manifest.get("name"), "status": "quarantined"}
