# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-taint-laundering-through-agent-authored-tool-arguments-during-retrieved-content-execution.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-taint-laundering-through-agent-authored-tool-arguments-during-retrieved-content-execution.warrant.json
# Markdown SHA-256:   e8fc3c4daed0aa273322689fcbfe14af242504a28dfddcf7b7b96b98ad6622e3
# Code AST SHA-256:   cdea6b8ea4333ca55f55af8aa662b8d4472ba2ca99a33db172a91bd5cb83fb22
# Verified Classes:   TaintLaunderingError
# Defensive Guards:   7 fail-closed invariants (zero external deps)

class TaintLaunderingError(RuntimeError):
    """Raised when a tool argument cannot be proven to derive from a cited,
    trusted span, indicating taint was laundered through model re-emission."""


def enforce_taint_policy(
    tool_name: str,
    arguments: dict,
    sink_policy: dict,
    cited_spans: dict,
) -> None:
    """Validate tool arguments against a taint sink policy.

    sink_policy[tool_name] declares the argument grammar:
      - "handle_only": every arg must be a handle present in cited_spans
      - "provenance_reconstruct": string args must be substrings of the
        cited span, and the span handle must be supplied alongside.
    """
    policy = sink_policy.get(tool_name)
    if policy is None:
        raise TaintLaunderingError(
            f"tool {tool_name!r} has no declared taint policy; refusing call"
        )

    if policy["mode"] == "handle_only":
        for arg_name, value in arguments.items():
            if not isinstance(value, str):
                raise TaintLaunderingError(
                    f"{tool_name}.{arg_name} must be a handle string"
                )
            if value not in cited_spans:
                raise TaintLaunderingError(
                    f"{tool_name}.{arg_name}={value!r} is not a cited handle; "
                    "possible taint laundering via model re-emission"
                )
        return

    if policy["mode"] == "provenance_reconstruct":
        for arg_name, value in arguments.items():
            if not isinstance(value, str):
                continue
            span_handle = arguments.get(f"{arg_name}__span")
            if span_handle is None:
                raise TaintLaunderingError(
                    f"{tool_name}.{arg_name} supplied without cited span handle"
                )
            span_text = cited_spans.get(span_handle)
            if span_text is None:
                raise TaintLaunderingError(
                    f"cited span {span_handle!r} not found in retrieval set"
                )
            if value not in span_text:
                raise TaintLaunderingError(
                    f"{tool_name}.{arg_name}={value!r} is not a substring of "
                    f"cited span {span_handle!r}; argument was composed, not copied"
                )
        return

    raise TaintLaunderingError(f"unknown policy mode {policy['mode']!r}")
