# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-launders-tainted-arguments-through-a-second-mcp-tool-call-to-defeat-least-privilege-taint-gates.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-launders-tainted-arguments-through-a-second-mcp-tool-call-to-defeat-least-privilege-taint-gates.warrant.json
# Markdown SHA-256:   cb03de7f5db803d197ff48c3c9cc3941905f5ff89f6c67f951200f43f7b92a89
# Code AST SHA-256:   9c5ab4f23443abf8ceb6535b420de2983904ba032a931266f628772496ae78c2
# Verified Classes:   TaintLaunderingError, ToolRegistration
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class TaintLaunderingError(RuntimeError):
    """Raised when a tool response would launder tainted input into a clean value."""


class ToolRegistration:
    def __init__(self, name, accepts_untrusted, declares_clean_output, is_declassifying_by_construction):
        self.name = name
        self.accepts_untrusted = accepts_untrusted
        self.declares_clean_output = declares_clean_output
        self.is_declassifying_by_construction = is_declassifying_by_construction


def register_tool(tool: ToolRegistration) -> None:
    # A tool may only claim clean output if it is declassifying by construction.
    # Otherwise its output taint MUST be the union of input taints, and the
    # runtime must not trust any schema that says otherwise.
    if tool.declares_clean_output and not tool.is_declassifying_by_construction:
        raise TaintLaunderingError(
            f"tool {tool.name!r} declares clean output but is not declassifying "
            f"by construction; it accepts untrusted input and would launder taint "
            f"across the response boundary"
        )
    if tool.declares_clean_output and tool.accepts_untrusted and not tool.is_declassifying_by_construction:
        raise TaintLaunderingError(
            f"tool {tool.name!r} accepts UNTRUSTED input and declares clean output; "
            f"this is the laundering registration and must be rejected"
        )


def enforce_sink_call(tool_name: str, argument_lineage: list[str], trusted_sources: set[str]) -> None:
    # Invariant: every argument to a sink tool must have lineage terminating
    # in a trusted source or an audited declassification. Labels are not enough.
    for origin in argument_lineage:
        if origin not in trusted_sources and not origin.startswith("declassify:"):
            raise TaintLaunderingError(
                f"sink {tool_name!r} received argument with lineage to untrusted "
                f"origin {origin!r}; laundering chain detected"
            )
