# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-taint-clear-on-write-back-but-persist-injected-state-across-sessions-the-write-back-barrier-invariant.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-taint-clear-on-write-back-but-persist-injected-state-across-sessions-the-write-back-barrier-invariant.warrant.json
# Markdown SHA-256:   c2164111691a11834edb9ae0b4c14c8f02450cd31a8959a29fd3e0637ec2c4c2
# Code AST SHA-256:   4658ea64d1715f47ced5cff1739f9f91eca53b626870d57d82e55de2a027206b
# Verified Classes:   TaintLaunderingError, TaintedValue
# Defensive Guards:   1 fail-closed invariants (zero external deps)

class TaintLaunderingError(RuntimeError):
    """Raised when a clean-identity write would carry tainted provenance into a clean region."""
    pass


class TaintedValue:
    __slots__ = ("payload", "taint")

    def __init__(self, payload, taint: bool):
        self.payload = payload
        self.taint = taint


def consolidate(dest_region_taint: bool, writer_taint: bool, sources: list[TaintedValue]) -> TaintedValue:
    """
    Copy values into a destination region. The destination's resulting taint is the
    join of: its prior taint, the writer identity's taint, and every source value's taint.
    A clean-identity writer copying a tainted value into a clean region is a violation.
    """
    joined = dest_region_taint or writer_taint or any(s.taint for s in sources)

    if not writer_taint and not dest_region_taint and any(s.taint for s in sources):
        raise TaintLaunderingError(
            "clean-identity write of tainted value into clean region: "
            "taint must join over value provenance, not just writer identity"
        )

    return TaintedValue([s.payload for s in sources], taint=joined)
