# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-of-prompt-injection-taint-tracking-be-considered-broken-the-label-laundering-boundary-at-tool-result-re-entry.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-of-prompt-injection-taint-tracking-be-considered-broken-the-label-laundering-boundary-at-tool-result-re-entry.warrant.json
# Markdown SHA-256:   1d09d7d681bb6b5cada05bb40596b8a95b5bd174246c6712353e1bdec7e0c8dd
# Code AST SHA-256:   33db41026a154fdbdefc14c721aee67c3562d8498a97786854d296ecd7546e8d
# Verified Classes:   TaintLaunderingError
# Defensive Guards:   5 fail-closed invariants (zero external deps)

class TaintLaunderingError(RuntimeError):
    """Raised when a transformation hop would drop a taint label without a clearing certificate."""
    pass


# Privilege-ordered lattice: higher index dominates lower.
TAINT_ORDER = ["USER", "UNTRUSTED_WEB", "MODEL_OUTPUT", "TOOL"]


def join_labels(labels):
    """Return the least upper bound of a set of source labels."""
    if not labels:
        return frozenset()
    return frozenset(labels)


def dominates(a, b):
    """True if label a dominates (is at least as privileged-tainted as) label b."""
    return TAINT_ORDER.index(a) >= TAINT_ORDER.index(b)


def transform(in_labels, transformer, clearing_certificate=None):
    """
    Apply a transformation and return the output label set.

    Invariant: out_label must dominate every input label unless the
    transformer holds an explicit clearing certificate. A transformer that
    is merely 'trusted' does NOT clear taint.
    """
    in_join = join_labels(in_labels)

    if clearing_certificate is not None:
        # Sanctioned declassification: log it, then clear.
        return frozenset({f"via:{transformer}"}), clearing_certificate

    # No certificate: output must dominate the full input join.
    out = set(in_join)
    out.add(f"via:{transformer}")

    for src in in_join:
        if not dominates("MODEL_OUTPUT", src) and src in ("UNTRUSTED_WEB", "USER"):
            # A model-based transformer cannot sit below its inputs.
            raise TaintLaunderingError(
                f"transformer {transformer!r} would launder {src!r}: "
                f"MODEL_OUTPUT does not dominate {src!r} in the lattice"
            )

    return frozenset(out), None


def authorize_side_effect(tool_name, value_labels, forbidden_sources=("UNTRUSTED_WEB",)):
    """Gate a side-effecting tool call on the value's taint labels."""
    for src in forbidden_sources:
        if src in value_labels:
            raise TaintLaunderingError(
                f"side effect {tool_name!r} blocked: value carries {src!r} "
                f"via {sorted(value_labels)}"
            )
    return True


# Reproduce the observed failure and confirm the fix.
page_labels = frozenset({"UNTRUSTED_WEB"})
summary_labels, cert = transform(page_labels, transformer="summarize")

assert cert is None, "summarize must not silently clear taint"
assert "UNTRUSTED_WEB" in summary_labels, "join-completeness violated"

try:
    authorize_side_effect("transfer_funds", summary_labels)
except TaintLaunderingError as e:
    print(f"correctly blocked: {e}")
else:
    raise TaintLaunderingError("laundered value reached a side-effecting tool")
