# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-to-route-a-tainted-tool-call-to-a-privileged-sink.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-to-route-a-tainted-tool-call-to-a-privileged-sink.warrant.json
# Markdown SHA-256:   d3f1ffb3008649f69dbb527202d96ec06765ed8d2c2eb26b7f73b128152b863f
# Code AST SHA-256:   d101920ada03276f65e329ae181d70be83bc85a0101f714084f06156b4b11337
# Verified Classes:   TaintedSinkError
# Defensive Guards:   1 fail-closed invariants (zero external deps)

class TaintedSinkError(RuntimeError):
    """Raised when a tainted value reaches a privileged sink parameter."""


PRIVILEGED_SINKS = {"write_file", "shell.exec", "http.post", "credential.use"}


def route_tool_call(tool: str, resolved_args: dict, provenance: dict) -> dict:
    """
    resolved_args: fully resolved arguments (defaults and interpolation already applied).
    provenance:    maps argument name -> set of origin labels for that resolved value.
    """
    if tool not in PRIVILEGED_SINKS:
        return {"tool": tool, "args": resolved_args, "gated": False}

    for name, value in resolved_args.items():
        labels = provenance.get(name, set())
        # A value with no provenance record is treated as untrusted, not clean.
        if not labels or "UNTRUSTED" in labels:
            raise TaintedSinkError(
                f"refusing privileged sink {tool!r}: argument {name!r} "
                f"carries labels {sorted(labels) or ['<unrecorded>']} "
                f"with no independent declassification record"
            )
    return {"tool": tool, "args": resolved_args, "gated": True}
