# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-confuses-data-taint-with-capability-scope-in-a-tool-sandbox.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-confuses-data-taint-with-capability-scope-in-a-tool-sandbox.warrant.json
# Markdown SHA-256:   07baa38fb6d6c179477f460e62a6055dd9e8a9391c389520956810a513437d51
# Code AST SHA-256:   ee89db471cef8ba051045e990059dcd9537e0a3113b394859045dc244f402b54
# Verified Classes:   TaintLaunderingError, Taint
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class TaintLaunderingError(RuntimeError):
    """Raised when a sink call is enabled by decision-tainted control flow,
    even though all of its arguments are syntactically clean."""
    pass


class Taint:
    CLEAN = 0
    TAINTED = 1


def gate_sink_call(tool_name: str,
                   arg_taints: list[int],
                   control_taint: int,
                   is_sink: bool) -> None:
    if not is_sink:
        return
    if any(t == Taint.TAINTED for t in arg_taints):
        raise TaintLaunderingError(
            f"sink {tool_name!r}: tainted argument")
    if control_taint == Taint.TAINTED:
        # The decision to invoke this sink was influenced by untrusted data,
        # e.g. a prompt-injected instruction selected this tool or its path.
        # A pure intermediary may have laundered the payload into a clean
        # literal (digest, length, formatted string); argument taint alone
        # is therefore insufficient.
        raise TaintLaunderingError(
            f"sink {tool_name!r}: decision-tainted control path; "
            "route through an explicit declassifier instead")
    return


def plan_and_execute(injected_page: str, planner_uses_page: bool) -> None:
    # Attacker-controlled text influences tool selection -> decision taint.
    control = Taint.TAINTED if planner_uses_page else Taint.CLEAN
    digest_arg = "deadbeef"  # appears clean after string-formatting
    gate_sink_call(
        tool_name="http_post",
        arg_taints=[Taint.CLEAN],   # argument looks clean
        control_taint=control,      # but the *choice* to call is tainted
        is_sink=True,
    )
    raise TaintLaunderingError("unreachable: gate should have fired")
