# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-enforce-taint-propagation-across-mcp-http-tool-calls-when-the-transport-strips-the-provenance-envelope.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-enforce-taint-propagation-across-mcp-http-tool-calls-when-the-transport-strips-the-provenance-envelope.warrant.json
# Markdown SHA-256:   1a8803286b1dca332f2d5893855b03afa06f3a3fd53be194d5c9c5dadac0f04c
# Code AST SHA-256:   53dea43db45ff14e76222491b05e507d45973af11301e0709b2d201b4ec6e5f6
# Verified Classes:   TaintLaunderingError, Span, ToolDecl, CallRequest
# Defensive Guards:   1 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Iterable

class TaintLaunderingError(RuntimeError):
    """Raised when a tainted value reaches a sink argument unlabeled."""

@dataclass
class Span:
    id: str
    text: str
    tainted: bool

@dataclass
class ToolDecl:
    name: str
    sinks: set[str]  # argument names that are sensitive sinks

@dataclass
class CallRequest:
    tool: str
    arguments: dict[str, str]
    origin_spans: dict[str, str] = field(default_factory=dict)

def is_tainted(value: str, spans: Iterable[Span]) -> list[str]:
    """Return IDs of tainted spans whose text overlaps the value."""
    hits = []
    for s in spans:
        if s.tainted and (s.text in value or value in s.text) and value:
            hits.append(s.id)
    return hits

def dispatch(call: CallRequest, decl: ToolDecl, spans: list[Span]) -> None:
    for arg_name, arg_val in call.arguments.items():
        if arg_name not in decl.sinks:
            continue
        sources = is_tainted(arg_val, spans)
        if sources:
            raise TaintLaunderingError(
                f"tool={decl.name} sink_arg={arg_name!r} carries taint from "
                f"spans={sources}; refuse or sanitize explicitly"
            )
    # safe to send over MCP HTTP transport
    return None
