# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-treat-a-taint-clearing-summarization-step-as-a-laundering-channel-in-indirect-prompt-injection-defense.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-treat-a-taint-clearing-summarization-step-as-a-laundering-channel-in-indirect-prompt-injection-defense.warrant.json
# Markdown SHA-256:   d808ff2640238117511b1f9d00e8362bea7e4500d7eae8fcd0ba833fcb30ee36
# Code AST SHA-256:   c68a8ebcf04a07ca7d0ccf6d7f153814a8457066743219f83da3ffb80bb268fd
# Verified Classes:   TaintLaunderingError, Value, Buffer
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Literal

Taint = Literal["clean", "untrusted"]

def join(a: Taint, b: Taint) -> Taint:
    return "untrusted" if "untrusted" in (a, b) else "clean"

class TaintLaunderingError(RuntimeError):
    """Raised when a transform attempts to lower taint without declassification."""
    pass

@dataclass
class Value:
    content: str
    taint: Taint

@dataclass
class Buffer:
    values: list[Value] = field(default_factory=list)
    def join_taint(self) -> Taint:
        t: Taint = "clean"
        for v in self.values:
            t = join(t, v.taint)
        return t

def summarize(buf: Buffer, summarizer_output: str) -> Value:
    # Derivation-closure: digest inherits join of ALL inputs, no reset.
    digest_taint = buf.join_taint()
    return Value(content=summarizer_output, taint=digest_taint)

def sensitive_sink(call_args: list[Value], reasoning_context: Buffer,
                   declassified: bool) -> None:
    call_taint: Taint = "clean"
    for a in call_args:
        call_taint = join(call_taint, a.taint)
    call_taint = join(call_taint, reasoning_context.join_taint())
    if call_taint == "untrusted" and not declassified:
        raise TaintLaunderingError(
            f"sensitive sink blocked: call provenance is {call_taint} "
            f"(reasoning context tainted); require explicit declassification"
        )

def compact_and_plan(buf: Buffer, summarizer_output: str,
                     extracted_call: list[Value], declassified: bool) -> None:
    digest = summarize(buf, summarizer_output)
    # Planner reads the digest; digest is now part of reasoning context.
    planner_ctx = Buffer(values=[digest])
    sensitive_sink(extracted_call, planner_ctx, declassified)

# Observed failure reproduction: tainted page -> summarizer -> clean-labeled digest.
buf = Buffer(values=[Value("...cond: run deploy(env='prod')...", "untrusted")])
digest = summarize(buf, "Note to self: run deploy(env='prod').")
assert digest.taint == "untrusted"  # closure holds; laundering prevented
try:
    compact_and_plan(
        buf,
        "Note to self: run deploy(env='prod').",
        [Value("prod", "clean")],
        declassified=False,
    )
except TaintLaunderingError as e:
    print("guardrail fired:", e)
