# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-a-sink-call-when-taint-provenance-is-lost-across-a-summarization-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-a-sink-call-when-taint-provenance-is-lost-across-a-summarization-boundary.warrant.json
# Markdown SHA-256:   620ae1c5cf20b5ea8f3bfbf57672e4d35b1a542373c5631fa9cf7ffff189983e
# Code AST SHA-256:   433fe4a4d8947e59e6ff7fcc1839a1c24b2b72a3d317796bd7eab5bb160f182c
# Verified Classes:   TaintLaunderingError
# Defensive Guards:   1 fail-closed invariants (zero external deps)

class TaintLaunderingError(RuntimeError):
    """Raised when a sink call's payload derives from tainted content
    through a taint-preserving transform chain that was never declassified."""
    pass


# Transform classification: True means the transform can clear taint
# (provably destroys instruction-bearing capability). False means it
# preserves taint. Unregistered transforms are treated as preserving.
DECLASSIFYING_TRANSFORMS = {
    "extract_closed_enum": True,
    "extract_integer_score": True,
    "reformat_json_keys": False,
    "summarize": False,
    "rephrase": False,
    "extract_fields_freeform": False,
}


def derivation_is_tainted(derivation_chain, tainted_sources):
    """Walk a derivation chain from source to sink.

    derivation_chain: list of (transform_name, input_tainted) tuples in
    application order. input_tainted is the taint state entering the transform.
    """
    tainted = False
    for transform_name, input_tainted in derivation_chain:
        if input_tainted or tainted:
            # A transform applied to tainted input preserves taint unless
            # it is explicitly classified as declassifying.
            if not DECLASSIFYING_TRANSFORMS.get(transform_name, False):
                tainted = True
            else:
                # Declassifying transform clears taint for this link only;
                # the chain remains tainted if a later link preserves.
                tainted = False
        else:
            tainted = False
    return tainted


def authorize_sink_call(sink_name, payload_derivation, tainted_sources):
    """Enforce sink policy against the transitive derivation closure.

    payload_derivation is the full chain of transforms from retrieval to
    the value being passed to the sink. The policy denies if any link
    in the chain is taint-preserving over a tainted input.
    """
    if sink_name not in {"send_webhook", "post_external", "write_public"}:
        return True  # internal sinks not subject to this policy

    if derivation_is_tainted(payload_derivation, tainted_sources):
        raise TaintLaunderingError(
            f"sink '{sink_name}' denied: payload derivation includes a "
            f"taint-preserving transform over untrusted content; "
            f"chain={[t for t, _ in payload_derivation]!r}. "
            f"Either add an explicit declassification rule, constrain the "
            f"sink payload to a closed schema, or route through an "
            f"extractive-only transform."
        )
    return True


# Reproduction of the observed failure:
# fetch_doc -> summarize -> send_webhook. summarize is not declassifying,
# so the chain is tainted and the call must be denied.
chain = [
    ("fetch_doc", True),      # retrieval produces tainted value
    ("summarize", True),      # summarizer preserves taint
]
try:
    authorize_sink_call("send_webhook", chain, tainted_sources={"memo_17"})
except TaintLaunderingError as e:
    print(f"DENIED as required: {e}")
