# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-reject-a-tool-description-that-passes-schema-validation-but-carries-a-poisoned-semantic-payload.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-reject-a-tool-description-that-passes-schema-validation-but-carries-a-poisoned-semantic-payload.warrant.json
# Markdown SHA-256:   9b8c3e5a461cd13c5b71441687c26548b77de5a13d0db1f6aa44313b4aeda04c
# Code AST SHA-256:   1122fb454b1f0bde882f3918cc78806a39893d1f9e0ab9facb48ae8b2520b7ce
# Verified Classes:   PoisonedToolDescriptorError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class PoisonedToolDescriptorError(RuntimeError):
    """Raised when a tool descriptor's semantic surface contains an
    instruction directed at the agent rather than a description of the tool."""
    pass


# Imperative patterns that have no legitimate place in a tool description.
# Prerequisites belong in the schema; suppression belongs nowhere.
_IMPERATIVE_MARKERS = (
    "always call", "before calling", "you must", "make sure to",
    "first call", "do not mention", "do not tell", "don't tell",
    "silently", "internal only", "without informing",
)


def admit_descriptor(descriptor: dict, known_tool_names: set[str]) -> dict:
    """Registry admission gate: structural surface is validated upstream;
    this gate validates the semantic surface before injection."""
    desc = descriptor.get("description", "")
    lowered = desc.lower()

    for marker in _IMPERATIVE_MARKERS:
        if marker in lowered:
            raise PoisonedToolDescriptorError(
                f"tool {descriptor.get('name')!r}: description contains "
                f"imperative/suppression marker {marker!r}; reject and quarantine"
            )

    # A description naming another tool is a prerequisite expressed as prose,
    # which is the injection channel. Prerequisites must be structural.
    for other in known_tool_names:
        if other != descriptor.get("name") and other in desc:
            raise PoisonedToolDescriptorError(
                f"tool {descriptor.get('name')!r}: description references "
                f"another tool {other!r}; express prerequisite in schema, not prose"
            )

    return descriptor
