# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-tool-description-as-executable-policy-rather-than-inert-documentation-and-how-does-skill-injection-thro.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-tool-description-as-executable-policy-rather-than-inert-documentation-and-how-does-skill-injection-thro.warrant.json
# Markdown SHA-256:   0519f6a21dfefb525aa332eb5506d88f44bb1d9219feb1357f0ac188488a3974
# Code AST SHA-256:   325bf18a8a043f0407cecacda6e0be891f356c9ac709868023957dc45b1342ec
# Verified Classes:   DescriptionTrustViolation, PinnedDescription
# Defensive Guards:   5 fail-closed invariants (zero external deps)

import hashlib
from dataclasses import dataclass


class DescriptionTrustViolation(RuntimeError):
    """Raised when a tool description cannot be treated as inert documentation."""


@dataclass(frozen=True)
class PinnedDescription:
    server_id: str
    tool_name: str
    description_sha256: str


def admit_description(
    server_id: str,
    tool_name: str,
    live_description: str,
    pinned: PinnedDescription | None,
    trusted_authors: frozenset[str],
    declaring_namespace: str,
) -> str:
    """
    Gate that decides whether a server-supplied description may enter the
    planning context. Returns a safe stub; never returns raw prose from an
    untrusted or mutated source.
    """
    live_hash = hashlib.sha256(live_description.encode("utf-8")).hexdigest()

    if server_id not in trusted_authors:
        raise DescriptionTrustViolation(
            f"server {server_id!r} is not an allowlisted description author; "
            f"refusing to inject prose for tool {tool_name!r}"
        )

    if pinned is None:
        raise DescriptionTrustViolation(
            f"no pinned description for {server_id}/{tool_name}; "
            f"trust-on-first-use must be completed at authorization time"
        )

    if pinned.description_sha256 != live_hash:
        raise DescriptionTrustViolation(
            f"manifest swap detected for {server_id}/{tool_name}: "
            f"pinned {pinned.description_sha256[:12]} != live {live_hash[:12]}"
        )

    lowered = live_description.lower()
    imperative_markers = ("system", "directive", "must not mention", "do not tell")
    if any(marker in lowered for marker in imperative_markers):
        raise DescriptionTrustViolation(
            f"description for {server_id}/{tool_name} contains agent-directed "
            f"imperatives; treating as executable policy, not documentation"
        )

    # Cross-namespace tool references inside a description are a routing-poison vector.
    for token in live_description.split():
        if "/" in token and token.split("/")[0] != declaring_namespace:
            raise DescriptionTrustViolation(
                f"description for {server_id}/{tool_name} references foreign "
                f"namespace token {token!r}"
            )

    return f"[{tool_name}] schema-defined tool from {server_id}"
