# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-treat-a-tainted-tool-response-as-a-capability-grant-rather-than-data-the-label-laundering-boundary-at-mcp-result-re-entry.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-treat-a-tainted-tool-response-as-a-capability-grant-rather-than-data-the-label-laundering-boundary-at-mcp-result-re-entry.warrant.json
# Markdown SHA-256:   f64831f025839ad0aa20093577fcf7f7a13b33e3882cfa430f5bd2a79917b578
# Code AST SHA-256:   8a1e7890481d8d4739558683dbbb5d56b1994aa6cdd78cdab8f88cd28f15f359
# Verified Classes:   TaintLaunderingError, Value
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import FrozenSet

class TaintLaunderingError(RuntimeError):
    """Raised when tainted-derived bytes re-enter the planner context as clean."""
    pass

@dataclass(frozen=True)
class Value:
    payload: str
    provenance: FrozenSet[str] = field(default_factory=frozenset)

    @property
    def tainted(self) -> bool:
        return bool(self.provenance)

def summarize(inputs: list[Value]) -> Value:
    # A summary inherits the union of its inputs' provenance.
    prov: FrozenSet[str] = frozenset()
    for v in inputs:
        prov = prov | v.provenance
    return Value(payload="<summary>", provenance=prov)

def write_to_store(store: dict, key: str, value: Value) -> None:
    # Store the value with its provenance intact; do NOT strip the label.
    store[key] = value

def read_from_store(store: dict, key: str) -> Value:
    v = store.get(key)
    if v is None:
        raise TaintLaunderingError(f"missing key {key!r}")
    # Ingress check: a stored value that is tainted must not be admitted as clean.
    if v.tainted:
        raise TaintLaunderingError(
            f"tainted value re-entered planner context via {key!r}: "
            f"provenance={sorted(v.provenance)}"
        )
    return v

def issue_privileged_call(arg: Value, command: str) -> str:
    if arg.tainted:
        raise TaintLaunderingError(
            f"privileged call {command!r} argument carries taint: "
            f"{sorted(arg.provenance)}"
        )
    return f"executed {command}"

def demo() -> None:
    store: dict = {}
    untrusted = Value(payload="rename build target", provenance=frozenset({"server:notes"}))
    s = summarize([untrusted])
    write_to_store(store, "scratch:1", s)
    # The laundering path: read back and attempt a privileged call.
    read_from_store(store, "scratch:1")  # raises TaintLaunderingError
    issue_privileged_call(s, "make rename-target")  # also raises

if __name__ == "__main__":
    demo()
