# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-mcp-host-refuse-to-propagate-a-taint-label-across-a-tool-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-mcp-host-refuse-to-propagate-a-taint-label-across-a-tool-boundary.warrant.json
# Markdown SHA-256:   e6bee3b0250973e9e9def7387b4c2fed7c7fee5417ed8e394c4bd7d1587ace23
# Code AST SHA-256:   f79cefeaa3a50eadb38e99f3561ad52b94f4e69565ab5d8e519638265909346f
# Verified Classes:   TaintPropagationError
# Defensive Guards:   1 fail-closed invariants (zero external deps)

class TaintPropagationError(RuntimeError):
    """Raised when a privileged call would receive an argument whose taint
    label survived propagation across a non-sanitizing tool boundary."""
    def __init__(self, tool: str, arg: str, origin: str):
        self.tool, self.arg, self.origin = tool, arg, origin
        super().__init__(
            f"refusing privileged call to {tool!r}: argument {arg!r} "
            f"carries taint originating at {origin!r}"
        )


def admit_privileged_call(tool: str, args: dict, labels: dict) -> None:
    for name, label_set in labels.items():
        if label_set:  # non-empty taint label set survived propagation
            raise TaintPropagationError(tool, name, sorted(label_set)[0])


def propagate(tool_decl: dict, arg_labels: dict, output: dict) -> dict:
    """Return taint labels for a tool's output. Only a declared sanitizer
    whose postcondition verifies may clear taint; otherwise union propagates."""
    union = set().union(*arg_labels.values()) if arg_labels else set()
    if tool_decl.get("sanitizer") and tool_decl["postcondition"](output):
        return {k: set() for k in output}
    return {k: set(union) for k in output}
