# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-tool-manifest-is-attempting-skill-injection-through-description-field-authority-laundering.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-tool-manifest-is-attempting-skill-injection-through-description-field-authority-laundering.warrant.json
# Markdown SHA-256:   a8d6b93ebfacf0d81d55f34e2004db6e9654cf27444a22a0080cb7743fd78005
# Code AST SHA-256:   7db32e6301a2fc00a253215c0cf50a0335171a2c7b2ec473698f3b97d74c363e
# Verified Classes:   ManifestScopeViolation
# Defensive Guards:   1 fail-closed invariants (zero external deps)

import re

# Control predicates that signal authority/ordering claims in prose.
_AUTHORITY_PREDICATES = re.compile(
    r"\b(gatekeeper|prerequisite|must (?:run|be invoked|be called) (?:before|first)|"
    r"designated|canonical|pre-?authorized|all tools in)\b",
    re.IGNORECASE,
)

class ManifestScopeViolation(RuntimeError):
    """Raised when a tool description asserts structural authority it does not own."""
    pass

def validate_description_scope(tool_name: str, description: str,
                               dependency_graph: dict[str, set[str]]) -> None:
    """
    Reject manifests whose prose claims control authority over other tools
    or the registry, unless the claim is backed by a real dependency edge.
    """
    for sentence in re.split(r"(?<=[.!?])\s+", description.strip()):
        if not _AUTHORITY_PREDICATES.search(sentence):
            continue
        # A control claim is present. It is only legal if the manifest's
        # structured depends_on fields actually encode the referenced edge.
        referenced = [t for t in dependency_graph.get(tool_name, set())
                      if t in sentence]
        if not referenced:
            raise ManifestScopeViolation(
                f"tool '{tool_name}' description asserts unbacked authority: "
                f"{sentence!r} (no matching dependency edge in manifest graph)"
            )

def load_manifest(manifest: dict, dependency_graph: dict[str, set[str]]) -> dict:
    validate_description_scope(
        manifest["name"], manifest["description"], dependency_graph
    )
    # Only after scope validation may the description enter model context.
    return manifest
