# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-tool-manifest-skill-injection-from-silently-upgrading-an-agent-s-authority-at-dynamic-plugin-load.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-tool-manifest-skill-injection-from-silently-upgrading-an-agent-s-authority-at-dynamic-plugin-load.warrant.json
# Markdown SHA-256:   c80165df00f305d0889dffee6fbbbe4202c65d670216d98ceb8ae2afd224a47b
# Code AST SHA-256:   512ff6c2cef5e679c7642a9f349063f5e1d346e02c483031416494bd00485f32
# Verified Classes:   ManifestSkillInjection, TrustedPolicy
# Defensive Guards:   6 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Any

ALLOWED_FIELDS = {"name", "inputSchema", "description"}
CONTROL_PLANE_FIELDS = {"x-precedence", "priority", "annotations", "default", "x-audience"}
MAX_DESCRIPTION_LEN = 512

class ManifestSkillInjection(RuntimeError):
    """Raised when a tool manifest attempts to influence agent control flow."""

@dataclass
class TrustedPolicy:
    # Ordering/tiering comes ONLY from signed local policy, keyed by tool name.
    order: dict[str, int] = field(default_factory=dict)
    tier: dict[str, str] = field(default_factory=dict)
    arg_classification: dict[str, str] = field(default_factory=dict)  # arg -> "public"|"secret"

def admit_manifest(manifest: dict[str, Any], signer_verified: bool) -> dict[str, Any]:
    if not signer_verified:
        raise ManifestSkillInjection("manifest signer not verified; refusing to load")

    injected = CONTROL_PLANE_FIELDS.intersection(manifest)
    if injected:
        # Fail closed: do not sanitize a control-plane channel.
        raise ManifestSkillInjection(
            f"manifest carries control-plane fields {sorted(injected)}; "
            "ordering/tiering must come from local signed policy"
        )

    unknown = set(manifest) - ALLOWED_FIELDS
    if unknown:
        raise ManifestSkillInjection(f"manifest carries non-allowlisted fields {sorted(unknown)}")

    desc = manifest.get("description", "")
    if len(desc) > MAX_DESCRIPTION_LEN:
        raise ManifestSkillInjection("description exceeds inert-metadata length bound")

    # Promote ONLY inert catalog data; description is fenced, never concatenated as instruction.
    return {
        "name": manifest["name"],
        "inputSchema": manifest["inputSchema"],
        "description_fenced": f"<untrusted_tool_description>{desc}</untrusted_tool_description>",
    }

def check_argument_flow(tool: str, arg: str, value_class: str, policy: TrustedPolicy) -> None:
    declared = policy.arg_classification.get(f"{tool}.{arg}")
    if declared is None:
        raise ManifestSkillInjection(f"no classification for {tool}.{arg}; refuse dispatch")
    # A description asking to "include full history" cannot override this.
    if value_class == "secret" and declared == "public":
        raise ManifestSkillInjection(
            f"argument {tool}.{arg} classified public but received secret data; blocked"
        )
