# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-draw-the-trust-boundary-between-tool-registry-metadata-and-executable-plugin-code-a-study-of-skill-injection-via-manifest-decla.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-draw-the-trust-boundary-between-tool-registry-metadata-and-executable-plugin-code-a-study-of-skill-injection-via-manifest-decla.warrant.json
# Markdown SHA-256:   e6c1a7706df50095918f656bcf8cc5d8bd7790e22cea2546f9dc2998224cbfd6
# Code AST SHA-256:   006e9a16e2d05d7cd24a531d62ddc0bc3f2046d8ef8468b8109ebd75145f79c0
# Verified Classes:   ManifestTrustViolation
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class ManifestTrustViolation(RuntimeError):
    """Raised when a registry manifest field would influence control flow
    or authorization without host-side attestation."""
    pass


# Parameters whose omission must never be silently filled from schema defaults.
SECURITY_RELEVANT = {"path", "url", "recipient", "amount", "target", "command"}

# Annotations a host must never trust from the registry body itself.
HOST_ASSERTED_ANNOTATIONS = {"readOnly", "destructive", "idempotent", "scope"}


def validate_manifest_for_execution(manifest: dict, host_attestation: dict) -> dict:
    """
    Gate a registry manifest before it is exposed to the model or used to
    authorize a call. Returns a sanitized view. Raises on any boundary breach.
    """
    name = manifest.get("name", "<unnamed>")
    schema = manifest.get("input_schema", {})
    annotations = manifest.get("annotations", {})

    # 1. Defaults on security-relevant params = argument injection channel.
    for param, spec in schema.items():
        if param in SECURITY_RELEVANT and "default" in spec:
            raise ManifestTrustViolation(
                f"tool '{name}': security-relevant param '{param}' declares a "
                f"default ({spec['default']!r}); omitted args must fail, not fill."
            )

    # 2. Self-declared side-effect annotations must be corroborated by host.
    for ann in HOST_ASSERTED_ANNOTATIONS & annotations.keys():
        attested = host_attestation.get(name, {}).get(ann)
        if attested is None:
            raise ManifestTrustViolation(
                f"tool '{name}': annotation '{ann}' is self-declared by the "
                f"registry with no host attestation; refusing to honor it."
            )
        if attested != annotations[ann]:
            raise ManifestTrustViolation(
                f"tool '{name}': registry claims {ann}={annotations[ann]!r} "
                f"but host attests {ann}={attested!r}; manifest is lying."
            )

    # 3. Free-text must be rendered as untrusted data, never as directives.
    description = manifest.get("description", "")
    sanitized = dict(manifest)
    sanitized["description"] = (
        "<untrusted_manifest_data>\n" + description + "\n</untrusted_manifest_data>"
    )
    sanitized["_trust"] = "untrusted_metadata"
    return sanitized


def authorize_call(manifest: dict, args: dict, host_attestation: dict) -> dict:
    """Authorize an actual invocation against the sanitized manifest view."""
    validate_manifest_for_execution(manifest, host_attestation)

    schema = manifest.get("input_schema", {})
    for param in schema:
        if param in SECURITY_RELEVANT and param not in args:
            raise ManifestTrustViolation(
                f"tool '{manifest.get('name')}': required security-relevant "
                f"argument '{param}' was omitted; refusing default-fill."
            )
    return args
