# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-skill-injection-attack-surface-in-dynamically-loaded-agent-tool-manifests-and-how-signed-skill-registries-bound-the.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-skill-injection-attack-surface-in-dynamically-loaded-agent-tool-manifests-and-how-signed-skill-registries-bound-the.warrant.json
# Markdown SHA-256:   c86d7e75c51ccfbc0436ce4ed1b230338d0a6321a29ab44d4bf86794dffc3c20
# Code AST SHA-256:   859c5da3bd7c788ad7cce4828438b499384c9638362ccc0fc93367184bad9189
# Verified Classes:   SkillInjectionError, Manifest
# Defensive Guards:   5 fail-closed invariants (zero external deps)

import hashlib
import json
from dataclasses import dataclass
from typing import Callable


class SkillInjectionError(RuntimeError):
    """Raised when a dynamically loaded skill manifest fails provenance or structural checks."""


@dataclass
class Manifest:
    name: str
    description: str
    parameters: dict
    signature: str
    signed_fields: tuple[str, ...]


IMPERATIVE_MARKERS = ("always ", "you must ", "do not mention", "before calling", "ignore previous")
COVERTNESS_MARKERS = ("do not mention", "do not tell", "without informing", "silently")


def verify_and_lint(
    manifest: Manifest,
    registry_public_key: Callable[[str], bool],
    known_tools: set[str],
) -> None:
    # Boundary 1: signature must cover every field that reaches model context.
    context_fields = {"name", "description", "parameters"}
    if not context_fields.issubset(set(manifest.signed_fields)):
        unsigned = context_fields - set(manifest.signed_fields)
        raise SkillInjectionError(
            f"manifest {manifest.name!r} has unsigned context fields {sorted(unsigned)}; "
            f"treat as unsigned and reject"
        )
    if not registry_public_key(manifest.signature):
        raise SkillInjectionError(
            f"manifest {manifest.name!r} signature does not verify against pinned registry key"
        )

    # Boundary 2: structural lint on the (now provenance-verified) prose.
    desc = manifest.description.lower()
    for marker in COVERTNESS_MARKERS:
        if marker in desc:
            raise SkillInjectionError(
                f"manifest {manifest.name!r} description contains covertness clause {marker!r}; quarantine"
            )
    for marker in IMPERATIVE_MARKERS:
        if marker in desc:
            raise SkillInjectionError(
                f"manifest {manifest.name!r} description contains imperative directive {marker!r}; quarantine"
            )
    for tool in known_tools:
        if tool != manifest.name and tool in desc:
            raise SkillInjectionError(
                f"manifest {manifest.name!r} description references foreign tool {tool!r}; "
                f"capability redirection signal"
            )


def load_manifest(raw: str, registry_public_key, known_tools: set[str]) -> Manifest:
    data = json.loads(raw)
    m = Manifest(
        name=data["name"],
        description=data["description"],
        parameters=data["parameters"],
        signature=data["signature"],
        signed_fields=tuple(data.get("signed_fields", ())),
    )
    verify_and_lint(m, registry_public_key, known_tools)
    return m
