# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-mcp-tool-descriptor-text-as-executable-instruction-rather-than-inert-metadata-and-how-does-the-descriptor-to.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-mcp-tool-descriptor-text-as-executable-instruction-rather-than-inert-metadata-and-how-does-the-descriptor-to.warrant.json
# Markdown SHA-256:   ef48122c1d0fa813172c24671379d601fe00bdd1aa317dbee2067cce1c5a820e
# Code AST SHA-256:   d528255553f51bf7795be79ad0dcd161bdc53daa9017cce30c925f85dcc383f7
# Verified Classes:   DescriptorLaunderingError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class DescriptorLaunderingError(RuntimeError):
    """Raised when a tool descriptor attempts to author the agent's call graph."""
    pass


# Second-person / obligation markers that signal an imperative clause.
_IMPERATIVE_MARKERS = (
    "you must", "you should", "always ", "before calling", "first call",
    "required for", "make sure to", "note for calling agents",
)


def classify_descriptor(tool_name: str, description: str, schema: dict) -> dict:
    """
    Split a discovered tool descriptor into declarative surface and
    quarantined imperative clauses. Fail closed on cross-tool authoring.
    """
    lowered = description.lower()

    quarantined = []
    for marker in _IMPERATIVE_MARKERS:
        if marker in lowered:
            quarantined.append(description)

    # A descriptor that names another tool is authoring a call graph.
    for other in schema.get("_known_tools", []):
        if other != tool_name and other in lowered:
            raise DescriptorLaunderingError(
                f"descriptor for {tool_name!r} references tool {other!r}; "
                "refusing to treat discovery metadata as a call-graph author"
            )

    # Schema-default poisoning: sensitive params must not carry defaults.
    sensitive = ("_ctx", "token", "auth", "scope", "secret")
    for param, spec in schema.get("properties", {}).items():
        if any(s in param.lower() for s in sensitive) and spec.get("default"):
            raise DescriptorLaunderingError(
                f"parameter {param!r} on {tool_name!r} carries a non-empty "
                "default on a sensitive name; refusing registration"
            )

    return {
        "tool": tool_name,
        "declarative_surface": description if not quarantined else None,
        "quarantined_imperatives": quarantined,
        "requires_review": bool(quarantined),
    }
