# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-launders-injected-instructions-through-a-legitimate-retrieval-re-fetch-taint-label-decay-at-the-tool-retrieval-seam.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-launders-injected-instructions-through-a-legitimate-retrieval-re-fetch-taint-label-decay-at-the-tool-retrieval-seam.warrant.json
# Markdown SHA-256:   e14532535ad557afe8e5503298775ac290f7604ce70054299de00dddf7e8a2cd
# Code AST SHA-256:   c921923f0a432718fa113baf0dbfae0837fc73eb5d7e3f57c57f327b275824d6
# Verified Classes:   TaintLaunderingError, TaintStore
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class TaintLaunderingError(RuntimeError):
    """Raised when a proposition reaches an action gate whose joined taint
    is clean only because a tainted representation was laundered through a
    clean transport (re-fetch, re-chunk, sanitize)."""


class TaintStore:
    """Monotonic join keyed on provenance identity, not content hash."""

    def __init__(self):
        self._taint: dict[str, bool] = {}

    def record(self, source_id: str, tainted: bool) -> None:
        # Join-merge: taint wins, clean is a no-op if already tainted.
        self._taint[source_id] = self._taint.get(source_id, False) or tainted

    def joined(self, source_ids: list[str]) -> bool:
        return any(self._taint.get(sid, True) for sid in source_ids)


def check_action_gate(
    store: TaintStore,
    proposition_source_ids: list[str],
    tool_name: str,
) -> None:
    if not proposition_source_ids:
        # Fail-closed: no provenance means permanent taint.
        raise TaintLaunderingError(
            f"tool {tool_name!r} invoked on proposition with no source_id; "
            "cannot evaluate taint, refusing by default"
        )
    if store.joined(proposition_source_ids):
        raise TaintLaunderingError(
            f"tool {tool_name!r} blocked: proposition {proposition_source_ids!r} "
            "carries joined taint from at least one representation; "
            "a clean re-fetch does not clear lineage taint"
        )
