# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-quarantine-retrieved-content-at-the-tool-sandbox-boundary-and-when-does-taint-propagation-silently-fail.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-quarantine-retrieved-content-at-the-tool-sandbox-boundary-and-when-does-taint-propagation-silently-fail.warrant.json
# Markdown SHA-256:   0dbebb55c57ba2053b21930f4a11150e1ba9315b75b155fbe85a48e968fbecdd
# Code AST SHA-256:   00d02854a4011c6573ec5aeb6acaa07954636f5561af8c385d9d316506ff54d1
# Verified Classes:   TaintLaunderingError, Artifact
# Defensive Guards:   5 fail-closed invariants (zero external deps)

class TaintLaunderingError(RuntimeError):
    """Raised when a sandbox transform drops taint without a declassification warrant."""
    pass


class Artifact:
    def __init__(self, payload, taint: frozenset, provenance: tuple):
        self.payload = payload
        self.taint = taint            # set of source ids that influenced this artifact
        self.provenance = provenance  # chain of transform ids


# Transforms that may legitimately declassify (bounded expressive capacity).
DECLASSIFYING_TRANSFORMS = {"schema_extract_typed", "hash", "length"}


def sandbox_transform(name: str, inputs: list[Artifact], fn) -> Artifact:
    if not inputs:
        raise TaintLaunderingError(f"transform {name} invoked with no inputs")
    joined_taint = frozenset().union(*(a.taint for a in inputs))
    out_payload = fn(*[a.payload for a in inputs])

    if name in DECLASSIFYING_TRANSFORMS:
        # Bounded transform: taint may be dropped, but provenance is retained.
        out_taint = frozenset()
    else:
        # Freeform transform: output inherits the join of input taint.
        # An LLM summarizer MUST land here; it cannot declassify.
        out_taint = joined_taint

    if joined_taint and not out_taint and name not in DECLASSIFYING_TRANSFORMS:
        raise TaintLaunderingError(
            f"transform {name} dropped taint from {sorted(joined_taint)} "
            f"without a declassification warrant"
        )

    return Artifact(out_payload, out_taint, tuple(a.provenance for a in inputs) + (name,))


def admit_to_context(art: Artifact, carrier_scanner) -> None:
    if carrier_scanner(art.payload):
        # Instruction-smuggling carrier detected: neutralize before admission.
        raise TaintLaunderingError(
            f"artifact from {art.provenance} carries executable-looking tokens; "
            f"neutralize before context admission"
        )
    if art.taint:
        raise TaintLaunderingError(
            f"tainted artifact from {sorted(art.taint)} reached admission; "
            f"re-quarantine required"
        )
    # ... admit ...


def dispatch_tool_call(call, dependency_graph) -> None:
    origins = dependency_graph.roots_of(call)
    tainted = [o for o in origins if o.taint]
    if tainted:
        raise TaintLaunderingError(
            f"tool call {call.name} traces to tainted retrieval "
            f"{[sorted(o.taint) for o in tainted]}; authorization denied"
        )
    # ... execute ...
