# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-quarantine-a-derived-memory-entry-after-a-tool-output-taint-label-is-laundered-through-a-legitimate-summarization-call.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-quarantine-a-derived-memory-entry-after-a-tool-output-taint-label-is-laundered-through-a-legitimate-summarization-call.warrant.json
# Markdown SHA-256:   b2830b306702617f9d671258468188e549e7527da5e14f97be8482b9bfe5aeb5
# Code AST SHA-256:   fe71c11b64439736b9d579037945ce988d493bafc3009e0b8f5051d7b8a8541d
# Verified Classes:   Label, TaintLaunderingError, ToolResult
# Defensive Guards:   1 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import FrozenSet

@dataclass(frozen=True)
class Label:
    sources: FrozenSet[str]
    untrusted: bool

    def join(self, other: "Label") -> "Label":
        return Label(
            sources=self.sources | other.sources,
            untrusted=self.untrusted or other.untrusted,
        )

TRUSTED = Label(frozenset({"local"}), untrusted=False)

class TaintLaunderingError(RuntimeError):
    """Raised when a sink would consume a value whose label is weaker than its derivation join."""

@dataclass
class ToolResult:
    value: str
    label: Label

def transformer(tool_name: str, inputs: list[ToolResult], output: str) -> ToolResult:
    # Correct rule: label is the join over tool identity AND every input.
    joined = Label(frozenset({tool_name}), untrusted=False)
    for i in inputs:
        joined = joined.join(i.label)
    return ToolResult(output, joined)

def memory_write(key: str, result: ToolResult) -> None:
    if result.label.untrusted:
        raise TaintLaunderingError(
            f"refusing to persist '{key}': derivation join is untrusted "
            f"(sources={sorted(result.label.sources)}); declassify explicitly first"
        )
    # ... persist ...

# Reproducing the observed failure against the naive producer-identity rule:
fetched = ToolResult("...policy reportedly disabled...", Label(frozenset({"web"}), untrusted=True))
summary = transformer("summarize", [fetched], "policy reportedly disabled")
try:
    memory_write("key_rotation_policy", summary)
except TaintLaunderingError as e:
    print("blocked:", e)
