# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-launders-a-tainted-label-through-a-tool-s-return-value-the-declassification-gap-in-prompt-injection-taint-tracking.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-launders-a-tainted-label-through-a-tool-s-return-value-the-declassification-gap-in-prompt-injection-taint-tracking.warrant.json
# Markdown SHA-256:   4766719582cf66b9ab01091fad040d3ee028378ae41ca8151bd601929d03d278
# Code AST SHA-256:   02ddedeef98cc12c9feb86938bc6c24cbf39d4f24a23f581933d735a98aed526
# Verified Classes:   TaintLaunderingError, ToolSpec, Value
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Literal

Label = Literal["TRUSTED", "UNTRUSTED"]

def join(a: Label, b: Label) -> Label:
    return "UNTRUSTED" if "UNTRUSTED" in (a, b) else "TRUSTED"

class TaintLaunderingError(RuntimeError):
    """Raised when a tool boundary would silently declassify tainted input."""
    pass

@dataclass
class ToolSpec:
    name: str
    kind: Literal["PURE", "MODEL_BACKED", "ACTION"]
    declared_output: Label
    accepts_untrusted_input: bool

@dataclass
class Value:
    payload: object
    label: Label

def register_tool(spec: ToolSpec) -> ToolSpec:
    # Schema-time invariant: a content-sensitive tool may not declare TRUSTED output
    # while accepting untrusted input, because it is an unauthorized declassifier.
    if (
        spec.kind == "MODEL_BACKED"
        and spec.declared_output == "TRUSTED"
        and spec.accepts_untrusted_input
    ):
        raise TaintLaunderingError(
            f"Tool {spec.name!r} declares TRUSTED output but is MODEL_BACKED and "
            f"accepts UNTRUSTED input; output must inherit the join of input labels."
        )
    return spec

def invoke(spec: ToolSpec, args: list[Value], raw_result: object) -> Value:
    input_label: Label = "TRUSTED"
    for a in args:
        input_label = join(input_label, a.label)

    if spec.kind == "PURE":
        out_label: Label = "TRUSTED"  # sound: output independent of input content
    elif spec.kind == "MODEL_BACKED":
        out_label = input_label        # no declassification; inherit join
    else:  # ACTION
        if input_label == "UNTRUSTED":
            raise TaintLaunderingError(
                f"Action {spec.name!r} invoked with UNTRUSTED argument expression."
            )
        out_label = "TRUSTED"

    return Value(payload=raw_result, label=out_label)

# --- Reproduction of the observed failure ---

summarize = ToolSpec(
    name="summarize",
    kind="MODEL_BACKED",
    declared_output="TRUSTED",   # the bug: content-sensitive tool claims clean output
    accepts_untrusted_input=True,
)
register_tool(summarize)  # raises TaintLaunderingError before any call executes
