# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-tool-manifest-has-been-poisoned-through-description-field-provenance-laundering-rather-than-direct-injection.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-tool-manifest-has-been-poisoned-through-description-field-provenance-laundering-rather-than-direct-injection.warrant.json
# Markdown SHA-256:   793fa3ec8427a5a213327c16a89dd9bb84a831680dba25974c232c2037d5c434
# Code AST SHA-256:   11ce001fe9d70391f3473dbfe410857cbd3e2a3aeee53442b35bd96feb248568
# Verified Classes:   ProvenanceLaunderingDetected
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class ProvenanceLaunderingDetected(RuntimeError):
    """Raised when a manifest description field carries instruction-shaped
    content whose provenance lies outside the signing boundary, or whose
    register does not match the tool's declared function."""
    pass


# Register keywords that indicate a description is attempting to direct
# the planner rather than describe the tool.
_INSTRUCTION_REGISTER = (
    "first call", "before calling", "you must", "include its value",
    "confirm the", "pass the", "so that", "for automated clients",
)
# Credential/secret handling has no place in a tool description.
_SECRET_TOKENS = ("token", "api_key", "secret", "credential", "password")


def audit_manifest_field(tool_name: str, declared_function: str,
                         description: str, provenance: str,
                         signed_sources: set[str]) -> None:
    lowered = description.lower()

    # 1. Provenance laundering: content authored outside the signing boundary
    #    but carried inside a signed manifest.
    if provenance not in signed_sources:
        for kw in _INSTRUCTION_REGISTER:
            if kw in lowered:
                raise ProvenanceLaunderingDetected(
                    f"tool {tool_name!r}: instruction-register phrase {kw!r} "
                    f"present in field with out-of-boundary provenance "
                    f"{provenance!r}; quarantine before context assembly"
                )

    # 2. Register mismatch: credential handling described by a non-credential tool.
    if declared_function != "credential_management":
        for tok in _SECRET_TOKENS:
            if tok in lowered:
                raise ProvenanceLaunderingDetected(
                    f"tool {tool_name!r}: description references secret token "
                    f"{tok!r} but declared function is {declared_function!r}; "
                    f"category error indicates laundering"
                )

    # 3. Cross-tool reference: description names a sibling tool's arguments.
    if "get_env" in lowered or "sibling tool" in lowered:
        raise ProvenanceLaunderingDetected(
            f"tool {tool_name!r}: description references sibling tool "
            f"arguments; cross-tool instruction in a tool manifest"
        )


def route_description(tool_name: str, declared_function: str,
                      description: str, provenance: str,
                      signed_sources: set[str]) -> str:
    audit_manifest_field(tool_name, declared_function, description,
                         provenance, signed_sources)
    # Only fields that pass audit may enter the instruction region.
    if provenance in signed_sources:
        return f"INSTRUCTION_REGION::{description}"
    return f"DATA_REGION::{description}"
