# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-tool-manifest-as-a-trust-boundary-artifact-rather-than-configuration-data.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-tool-manifest-as-a-trust-boundary-artifact-rather-than-configuration-data.warrant.json
# Markdown SHA-256:   a7248298fd0614594ac40c737af95ee2c5c97ea2304fc9deef7d307518146273
# Code AST SHA-256:   dedfe9ffc94204e70f4c12f1d65d0fbd2febeecf70d92f43b0302340832104d0
# Verified Classes:   ManifestTrustBoundaryError
# Defensive Guards:   1 fail-closed invariants (zero external deps)

class ManifestTrustBoundaryError(RuntimeError):
    """Raised when rendered manifest strings escape the validated (signed) set."""
    pass


def load_tool_manifest(manifest: dict, validated_fields: set[str]) -> dict:
    """
    Load a runtime-discovered tool manifest, refusing any manifest whose
    rendered strings are not a subset of the signed/validated field set.

    `validated_fields` names the manifest fields covered by the trust
    machinery. Any field that will be rendered into model context but is
    absent here is a boundary violation.
    """
    # Fields the runtime will concatenate into the model context.
    rendered_fields = {"description"}
    for param in manifest.get("parameters", {}).values():
        rendered_fields.add(f"parameters.{param['name']}.description")
        if "enum_labels" in param:
            rendered_fields.add(f"parameters.{param['name']}.enum_labels")

    escaped = rendered_fields - validated_fields
    if escaped:
        raise ManifestTrustBoundaryError(
            f"manifest {manifest.get('name')!r} renders unvalidated fields "
            f"{sorted(escaped)}; refusing to load (possible injection vector)"
        )

    # Only reached when rendered_context is a subset of validated_strings.
    return {
        "name": manifest["name"],
        "endpoint": manifest["endpoint"],
        "description": manifest["description"],
    }
