# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-a-poisoned-mcp-tool-description-when-the-manifest-hash-is-valid-but-the-semantic-contract-has-been-silently-widened.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-a-poisoned-mcp-tool-description-when-the-manifest-hash-is-valid-but-the-semantic-contract-has-been-silently-widened.warrant.json
# Markdown SHA-256:   d35e69ecf7af748db33d32c85416bc31ba0791705b98175c68dd02ba3c2c1f71
# Code AST SHA-256:   985486d78e2071cec63a419a66981b4b3a3b6115101968fdbcbf5e8b9f19b0e1
# Verified Classes:   SemanticManifestDrift
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class SemanticManifestDrift(RuntimeError):
    """Raised when a tool description's authorization surface has widened
    without a corresponding change in the structured capability declaration."""
    pass


# Tokens that, when introduced into a description diff, indicate an
# authorization-surface widening rather than a clarity edit.
WIDENING_TOKENS = frozenset({
    "outside", "home", "any", "all", "resolve", "bypass",
    "override", "regardless", "unrestricted", "arbitrary",
})


def bind_tool(
    tool_name: str,
    current_description: str,
    previous_description: str | None,
    capability_declaration: dict,
    previous_capability: dict | None,
) -> dict:
    """
    Bind a tool only if its description has not widened the authorization
    surface relative to the structured capability declaration.

    Returns the binding record on success.
    Raises SemanticManifestDrift if the description introduces widening
    language without a matching capability change.
    """
    if previous_description is None:
        # First bind: no baseline to diff against. Require explicit scope.
        if "path_scope" not in capability_declaration:
            raise SemanticManifestDrift(
                f"tool {tool_name!r} has no baseline description and no "
                f"structured path_scope; refusing to derive preconditions "
                f"from prose"
            )
        return {"tool": tool_name, "scope": capability_declaration["path_scope"]}

    current_tokens = set(current_description.lower().split())
    previous_tokens = set(previous_description.lower().split())
    introduced = (current_tokens - previous_tokens) & WIDENING_TOKENS

    capability_changed = capability_declaration != previous_capability

    if introduced and not capability_changed:
        raise SemanticManifestDrift(
            f"tool {tool_name!r} description introduced widening tokens "
            f"{sorted(introduced)} without a structured capability change; "
            f"refusing to bind. Re-publish with an explicit path_scope grant "
            f"or confirm out-of-band."
        )

    return {
        "tool": tool_name,
        "scope": capability_declaration.get("path_scope", ["workspace"]),
        "widened": bool(introduced),
    }
