# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-inherits-a-taint-label-from-a-tool-result-that-was-laundered-through-a-caching-layer.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-inherits-a-taint-label-from-a-tool-result-that-was-laundered-through-a-caching-layer.warrant.json
# Markdown SHA-256:   b110f1c5f7756e132900038b5d9e273d79afa5504abac087113fe7e60e9d1f93
# Code AST SHA-256:   bdc07556d722c282985aca54ccde29e4af58bbe6b2279cdc9ceae35cad10adb4
# Verified Classes:   TaintLaunderingError, TaintAwareCache
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class TaintLaunderingError(RuntimeError):
    """Raised when a cache hit yields a value whose taint provenance
    cannot be reconciled with the current call's taint state."""
    pass


TAINTED = "EXTERNAL_UNTRUSTED"
CLEAN = "CLEAN"


class TaintAwareCache:
    def __init__(self):
        # key -> (payload, taint_label)
        self._store = {}

    def put(self, key, payload, taint_label):
        self._store[key] = (payload, taint_label)

    def get(self, key, caller_taint):
        entry = self._store.get(key)
        if entry is None:
            return None
        payload, entry_taint = entry
        # A cache hit is a fresh provenance event. If the entry is tainted
        # but the caller is clean, the caller is about to consume laundered
        # bytes -- refuse rather than silently downgrade.
        if entry_taint == TAINTED and caller_taint == CLEAN:
            raise TaintLaunderingError(
                f"cache hit for key={key!r} carries taint {entry_taint!r} "
                f"into a clean caller; refusing to launder provenance"
            )
        return payload


def guarded_summarize(cache, key, caller_taint):
    cached = cache.get(key, caller_taint)
    if cached is not None:
        return cached
    # ... perform the actual summarize and store with caller taint ...
    raise TaintLaunderingError("cache miss path not exercised in this record")


def send_email(cache, key, recipient, caller_taint):
    body = guarded_summarize(cache, key, caller_taint)
    if body is None:
        raise TaintLaunderingError(f"no body for key={key!r}")
    # sink gate: only reached if body is clean w.r.t. caller
    return {"to": recipient, "body": body}
