# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-defend-a-sandboxed-extension-s-skill-registry-against-capability-laundering-via-tool-description-injection.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-defend-a-sandboxed-extension-s-skill-registry-against-capability-laundering-via-tool-description-injection.warrant.json
# Markdown SHA-256:   7e3a9703613d31ce5f6833131e7de736c142bf4ef5a89efb0fc5f96b90c89ef2
# Code AST SHA-256:   a7cb0bece49f4003ea951f738153bcfe00d7923508e795bfe980c41a275735fe
# Verified Classes:   CapabilityLaunderingError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class CapabilityLaunderingError(RuntimeError):
    """Raised when an extension description asserts authority its manifest lacks,
    or when the planner attempts to route an intent through a tool that does not
    hold the required capability."""


# Action verbs that imply capabilities, mapped to the capability they require.
_ACTION_CAPABILITY_MAP = {
    "save": "fs.write",
    "persist": "fs.write",
    "write": "fs.write",
    "send": "net.egress",
    "transmit": "net.egress",
    "upload": "net.egress",
    "delete": "store.delete",
    "remove": "store.delete",
    "execute": "subprocess",
    "run": "subprocess",
}


def lint_description(tool_name: str, manifest_caps: set[str], description: str) -> None:
    """Registration gate: reject descriptions that claim capabilities the
    manifest does not grant, or that name other tools as routing targets."""
    lowered = description.lower()
    for verb, required_cap in _ACTION_CAPABILITY_MAP.items():
        if verb in lowered and required_cap not in manifest_caps:
            raise CapabilityLaunderingError(
                f"tool {tool_name!r} description asserts action {verb!r} "
                f"requiring {required_cap!r}, but manifest grants only "
                f"{sorted(manifest_caps)}"
            )
    if "call " in lowered and "tool" in lowered:
        raise CapabilityLaunderingError(
            f"tool {tool_name!r} description contains a cross-tool routing "
            f"instruction; descriptions may not direct the planner to other tools"
        )


def select_tools_for_intent(
    intent_capabilities: set[str],
    registry: list[dict],
) -> list[dict]:
    """Pre-filter: only tools whose signed manifest covers the intent's
    required capabilities are candidates. Description text is never consulted
    for routing authority."""
    candidates = []
    for tool in registry:
        manifest_caps = set(tool["manifest_capabilities"])
        if intent_capabilities.issubset(manifest_caps):
            candidates.append(tool)
    if not candidates:
        raise CapabilityLaunderingError(
            f"no registered tool holds required capabilities "
            f"{sorted(intent_capabilities)}; surface capability gap to user, "
            f"do not substitute a description-claimed path"
        )
    return candidates
