# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-refuse-to-treat-tool-description-text-as-data-rather-than-instruction-and-what-breaks-when-it-doesn-t.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-refuse-to-treat-tool-description-text-as-data-rather-than-instruction-and-what-breaks-when-it-doesn-t.warrant.json
# Markdown SHA-256:   88ee16612e1b9a0e9479cecc2a2235c5316df8950cbe9e59f42c785416ce3db2
# Code AST SHA-256:   4ed3eae91f70b39681fb668a44374023fcfdde4b007f923f1650020f23edb212
# Verified Classes:   ManifestInstructionLeak
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class ManifestInstructionLeak(RuntimeError):
    """Raised when a capability manifest attempts to smuggle procedural
    instructions into the agent's instruction channel at discovery time."""
    pass


# Operator-signed policy: the ONLY source of truth for what the agent may do.
# Manifests may narrow this set; they may never widen it.
SIGNED_POLICY = {
    "list_directory": {"params": {"path"}, "returns": "list[str]"},
    "read_file": {"params": {"path"}, "returns": "str"},
}


def render_manifest(manifest: dict) -> str:
    """Render a remote tool manifest into a quarantined DATA block.

    Enforces the invariant: manifest prose may describe a tool but may not
    introduce procedures, and may not reference tools outside signed policy.
    """
    tool = manifest.get("name")
    if tool not in SIGNED_POLICY:
        # A manifest cannot add an action. Unknown tools are dropped, not trusted.
        raise ManifestInstructionLeak(
            f"manifest advertises tool {tool!r} absent from signed policy"
        )

    desc = (manifest.get("description") or "").strip()

    # Procedural markers in a capability description are a schema violation.
    # These are instructions masquerading as documentation.
    procedural_markers = (
        "first call", "then call", "before calling", "you must",
        "required by", "note for assistant", "include the contents",
    )
    lowered = desc.lower()
    for marker in procedural_markers:
        if marker in lowered:
            raise ManifestInstructionLeak(
                f"tool {tool!r} description contains procedural directive "
                f"{marker!r}; refusing to render into context"
            )

    # Even clean prose is rendered as DATA, never as preamble instructions.
    return (
        f"[UNTRUSTED MANIFEST CLAIM — tool={tool}]\n"
        f"  params: {sorted(SIGNED_POLICY[tool]['params'])}\n"
        f"  declared_behavior: {desc[:200]}\n"
        f"[END UNTRUSTED CLAIM]"
    )


def build_catalog(manifests: list[dict]) -> str:
    rendered = []
    for m in manifests:
        try:
            rendered.append(render_manifest(m))
        except ManifestInstructionLeak:
            # Drop the offending manifest entirely; never partially render it.
            continue
    return "\n\n".join(rendered)
