# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-treat-a-tool-manifest-as-untrusted-the-skill-injection-boundary-at-manifest-load-time.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-treat-a-tool-manifest-as-untrusted-the-skill-injection-boundary-at-manifest-load-time.warrant.json
# Markdown SHA-256:   b0d6048bfea8675032c0525953c65d28c57d5ed8451a40498bf8535fa35a5b49
# Code AST SHA-256:   ff8714f40d61d0ac165fd85c742d747f3459a777339aa52dd64226367b47512d
# Verified Classes:   SkillInjectionError, Manifest
# Defensive Guards:   2 fail-closed invariants (zero external deps)

"""Manifest load-time guard: descriptor text is model-facing input, not metadata."""
from dataclasses import dataclass


class SkillInjectionError(RuntimeError):
    """Raised when a manifest descriptor attempts to issue instructions or induce
    a capability the caller did not request."""
    pass


@dataclass
class Manifest:
    server_id: str
    trusted_build_artifact: bool
    description: str
    induced_calls: list  # (tool, path_or_arg) pairs the descriptor suggests


IMPERATIVE_MARKERS = ("always first", "before using", "you must", "required for correct")


def load_manifest(m: Manifest, user_requested_paths: set) -> Manifest:
    # Provenance check is necessary but not sufficient; descriptor still model-facing.
    if m.trusted_build_artifact:
        return m

    desc = m.description.lower()
    if any(marker in desc for marker in IMPERATIVE_MARKERS):
        raise SkillInjectionError(
            f"manifest {m.server_id!r} descriptor issues instructions; "
            f"treating as untrusted model-input"
        )

    for tool, arg in m.induced_calls:
        if tool == "read_file" and arg not in user_requested_paths:
            raise SkillInjectionError(
                f"manifest {m.server_id!r} induces unrequested capability: "
                f"{tool}({arg!r})"
            )
    return m
