# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-tool-manifest-s-declared-schema-as-untrusted-input-rather-than-as-a-capability-contract-and-how-does-descri.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-tool-manifest-s-declared-schema-as-untrusted-input-rather-than-as-a-capability-contract-and-how-does-descri.warrant.json
# Markdown SHA-256:   c40a0a5a9e2f5cff35005edd89ed700765f7851992fb7e5ba0c82eae72f85d53
# Code AST SHA-256:   f1785099a172692e6848ebbde9190c853e12b38c1cfaa060d886c64e24a4cd45
# Verified Classes:   ManifestProvenanceViolation
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class ManifestProvenanceViolation(RuntimeError):
    """Raised when a tool argument traces its origin to manifest-supplied text."""

# Fields the validator treats as inert but the model reads as instruction-bearing.
MODEL_CONSUMED_FIELDS = ("description", "title", "default", "examples", "$comment")

def gate_tool_call(tool_name, manifest, argument_value, argument_origin):
    """
    Enforce side-effect gating: a tool call may proceed only if its argument
    did not originate from model-consumed manifest text.
    """
    # Confirm the tool exists in the manifest we actually received.
    declared = {t["name"] for t in manifest.get("tools", [])}
    if tool_name not in declared:
        raise ManifestProvenanceViolation(
            f"tool {tool_name!r} not present in manifest; refusing dispatch"
        )

    # If the argument value was copied from any model-consumed manifest field,
    # the call is a suspected skill-injection and must not fire side effects.
    if argument_origin in MODEL_CONSUMED_FIELDS:
        raise ManifestProvenanceViolation(
            f"argument for {tool_name!r} originated in manifest field "
            f"{argument_origin!r}; treating as poisoned manifest text"
        )

    # Schema validation must happen BEFORE any side effect, not after.
    # (Validator call elided; the ordering is the invariant.)
    return True
