# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-taint-propagation-as-irreversible-across-the-retrieval-to-generation-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-taint-propagation-as-irreversible-across-the-retrieval-to-generation-boundary.warrant.json
# Markdown SHA-256:   514bcb978be37c4fa132179da60652b976ab38487dff219b27fd6efe780741f3
# Code AST SHA-256:   996344da4ce35c07a8e873a55111fe8fc550438f4392c021dbc8bd7ca779809b
# Verified Classes:   TaintLaunderingError, ProvenanceNode, TaintGraph
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class TaintLaunderingError(RuntimeError):
    """Raised when a derived artifact is about to cross a privilege boundary
    without inheriting the taint of its contributing sources."""
    pass


class ProvenanceNode:
    def __init__(self, node_id, tainted, sources=None):
        self.node_id = node_id
        self.tainted = tainted
        self.sources = sources or []  # upstream node ids


class TaintGraph:
    def __init__(self):
        self.nodes = {}

    def add(self, node):
        self.nodes[node.node_id] = node

    def derive(self, new_id, input_ids, is_bounded_transformation=False):
        # Derivation inherits the union of input taints unless the
        # transformation has a pre-specified, model-independent output space.
        inherited = any(self.nodes[i].tainted for i in input_ids)
        tainted = inherited and not is_bounded_transformation
        node = ProvenanceNode(new_id, tainted, sources=list(input_ids))
        self.add(node)
        return node


def gate_privileged_action(graph, artifact_id, action_name, bounded_output_space):
    """Block a privileged action reached from a derived artifact unless taint
    was legitimately cleared by a bounded, verifiable transformation."""
    node = graph.nodes.get(artifact_id)
    if node is None:
        raise TaintLaunderingError(
            f"artifact {artifact_id!r} has no provenance node; refusing "
            f"action {action_name!r}"
        )
    if node.tainted and not bounded_output_space:
        raise TaintLaunderingError(
            f"action {action_name!r} reached from tainted artifact "
            f"{artifact_id!r} (sources={node.sources}); summarization does "
            f"not clear taint"
        )
    return True


# --- the observed incident, replayed ---
g = TaintGraph()
g.add(ProvenanceNode("ticket_88213", tainted=True))
# model summarizes the ticket -> free-form derivation, taint inherited
g.derive("summary_1", ["ticket_88213"], is_bounded_transformation=False)
# model extracts action items -> still free-form, taint still inherited
g.derive("action_items_1", ["summary_1"], is_bounded_transformation=False)

try:
    gate_privileged_action(g, "action_items_1", "process_refund",
                           bounded_output_space=False)
except TaintLaunderingError as e:
    # this is the correct outcome; the original runtime failed here
    print("BLOCKED:", e)
    raise
