# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-taints-a-tool-result-but-the-agent-s-planner-re-reads-the-same-bytes-through-a-different-retrieval-path.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-taints-a-tool-result-but-the-agent-s-planner-re-reads-the-same-bytes-through-a-different-retrieval-path.warrant.json
# Markdown SHA-256:   6260f2a809976eb8f6d9cd11e7e2160d4e6d1fc5ccb4db3c41682aac4a02e464
# Code AST SHA-256:   8bac6ac3531c16edf3dde6bfa57926d184a3827f7a48fbe11d17df51c0629222
# Verified Classes:   TaintLaunderingError, TrustedSink
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class TaintLaunderingError(RuntimeError):
    """Raised when a trusted sink returns content whose provenance
    includes previously-tainted input, violating the read-disjointness
    assertion made at sink registration."""


class TrustedSink:
    def __init__(self, name: str, read_disjoint_from_untrusted: bool, provenance_index):
        if not read_disjoint_from_untrusted:
            # Sinks that can read back untrusted writes must be taint-preserving;
            # this class only models the disjoint (taint-clearing) case.
            raise TaintLaunderingError(
                f"sink {name!r} cannot be registered as taint-clearing: "
                f"read_disjoint_from_untrusted=False"
            )
        self.name = name
        self.provenance_index = provenance_index  # set of content hashes written

    def read(self, content_hash: str, tainted_hashes: set[str]) -> bytes:
        if content_hash in tainted_hashes:
            raise TaintLaunderingError(
                f"sink {self.name!r} returned content {content_hash!r} whose "
                f"provenance includes a tainted input; taint label would be "
                f"stale at the tool-call boundary"
            )
        return self.provenance_index[content_hash]


def check_planner_action(sink: TrustedSink, content_hash: str, tainted_hashes: set[str]) -> None:
    # Call at every tool-call boundary before the policy engine evaluates taint.
    sink.read(content_hash, tainted_hashes)  # raises TaintLaunderingError on violation
