# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-re-admit-tainted-data-after-a-successful-sanitization-pass-the-taint-laundering-boundary-in-tool-call-gating.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-re-admit-tainted-data-after-a-successful-sanitization-pass-the-taint-laundering-boundary-in-tool-call-gating.warrant.json
# Markdown SHA-256:   08e1bf4c6b46b79aba9cd20ad85ee322b816c206a17e0551dbfcb0cc0d7e7491
# Code AST SHA-256:   07718a6210bdb975632af5eedde3a5ac9c7f770a42b50fa94997e8b2002c72db
# Verified Classes:   TaintLaunderingError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class TaintLaunderingError(RuntimeError):
    """Raised when a sink argument semantically reproduces untrusted content
    whose span-level taint was laundered through a paraphrase/resolution step."""

def gate_sink_call(
    tool: str,
    argument: str,
    argument_spans: list[dict],
    session_taint_signatures: set[str],
    authorizations: set[str],
) -> None:
    # 1. Span-level check: any tainted span in the argument DAG blocks.
    for span in argument_spans:
        if span.get("taint") in ("HIGH", "CLEANED_REEMITTED"):
            raise TaintLaunderingError(
                f"sink {tool!r} reached by tainted span {span['id']}"
            )

    # 2. Content-level check: the assembled argument must not contain a
    #    semantic copy of any untrusted span seen this session, unless an
    #    explicit authorization names that exact content.
    normalized = " ".join(argument.lower().split())
    for sig in session_taint_signatures:
        if sig in normalized and sig not in authorizations:
            raise TaintLaunderingError(
                f"sink {tool!r} argument reproduces untrusted signature {sig!r} "
                f"without authorization; span graph reported clean (laundering suspected)"
            )

def summarize_and_emit(source_span: dict, summary_text: str) -> dict:
    # LLM-mediated transform is content-preserving under paraphrase:
    # join must be min-trust, never max.
    if source_span["taint"] != "NONE":
        new_taint = "CLEANED_REEMITTED"
    else:
        new_taint = "NONE"
    return {"id": f"summary_of_{source_span['id']}", "text": summary_text, "taint": new_taint}
