# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-reject-a-dynamically-loaded-tool-manifest-whose-skill-description-is-semantically-valid-but-instructionally-load-bearing.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-reject-a-dynamically-loaded-tool-manifest-whose-skill-description-is-semantically-valid-but-instructionally-load-bearing.warrant.json
# Markdown SHA-256:   7372d8a52ae4c94744e1aa1ebb6332fdb67af653fb417beec842f218983508a6
# Code AST SHA-256:   7116c283d0571f3b9f5f204421451dfcb44eacf73f52e176a9b4db5d00b1059b
# Verified Classes:   LoadBearingManifestError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class LoadBearingManifestError(RuntimeError):
    """Raised when a manifest description directs the agent to an action the schema does not admit."""
    pass


def validate_manifest_injection(manifest: dict, in_context_literals: set[str]) -> None:
    schema_enum = set(manifest["schema"].get("enum", []))
    description = manifest.get("description", "")

    # Enum closure: every literal named in the description must be schema-admitted.
    for literal in extract_literals(description):
        if literal not in schema_enum:
            raise LoadBearingManifestError(
                f"tool={manifest['name']!r} description names {literal!r} "
                f"which is not in schema enum {sorted(schema_enum)}; "
                f"description is load-bearing, not descriptive"
            )

    # Policy-channel separation: caller-state conditionals belong in constraints, not prose.
    if contains_caller_state_conditional(description):
        raise LoadBearingManifestError(
            f"tool={manifest['name']!r} description contains a caller-state conditional; "
            f"move it to the 'constraints' field with a declared grammar"
        )

    # Re-fetch delta gating: mid-task description edits must not silently enter context.
    new_literals = extract_literals(description) - in_context_literals
    if new_literals and manifest.get("refresh", False):
        raise LoadBearingManifestError(
            f"tool={manifest['name']!r} refresh introduces new literals {sorted(new_literals)} "
            f"mid-task; gate the refresh and rebuild scratch state before injecting"
        )
