# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-launders-tainted-provenance-across-a-summarizer-sub-agent-and-re-admits-it-as-trusted-context.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-launders-tainted-provenance-across-a-summarizer-sub-agent-and-re-admits-it-as-trusted-context.warrant.json
# Markdown SHA-256:   a3a3636ddcad9cbd9026c1947571a2a5743332f00afb1fb1da9c4144ff3aeb64
# Code AST SHA-256:   64e80a0a0052db6acdab591ebeb68b3e676a52059d2b7ef66b702f4311274da2
# Verified Classes:   TaintLaunderingError, Span, ProvenanceGraph
# Defensive Guards:   1 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Dict, Set, List

class TaintLaunderingError(RuntimeError):
    """Raised when a privileged call's ancestor closure contains tainted data
    that was laundered through a sub-agent boundary."""
    pass

@dataclass
class Span:
    span_id: str
    label: str                      # "TRUSTED" | "UNTRUSTED_WEB" | ...
    derived_from: Set[str] = field(default_factory=set)

class ProvenanceGraph:
    def __init__(self) -> None:
        self.spans: Dict[str, Span] = {}

    def add(self, span: Span) -> None:
        self.spans[span.span_id] = span

    def ancestor_closure(self, span_id: str) -> Set[str]:
        seen: Set[str] = set()
        stack: List[str] = [span_id]
        while stack:
            cur = stack.pop()
            if cur in seen:
                continue
            seen.add(cur)
            node = self.spans.get(cur)
            if node is not None:
                stack.extend(node.derived_from)
        return seen

    def is_admissible(self, arg_span_ids: List[str]) -> bool:
        tainted = {sid for sid, s in self.spans.items() if s.label != "TRUSTED"}
        for arg in arg_span_ids:
            if self.ancestor_closure(arg) & tainted:
                return False
        return True

def privileged_call(graph: ProvenanceGraph, tool: str,
                    arg_span_ids: List[str]) -> None:
    # The sub-agent output S2 is labeled TRUSTED by the naive labeler, but its
    # derived_from still points at the tainted source S1. Ancestor closure
    # catches what the immediate label misses.
    if not graph.is_admissible(arg_span_ids):
        raise TaintLaunderingError(
            f"privileged tool {tool!r} authorized against laundered taint: "
            f"args {arg_span_ids} descend from UNTRUSTED spans"
        )
    # ... execute tool ...

# Reproduction of the observed trace:
g = ProvenanceGraph()
g.add(Span("S1", "UNTRUSTED_WEB"))                       # fetch_url result
g.add(Span("S2", "TRUSTED", derived_from={"S1"}))        # summarize(S1) output
privileged_call(g, "send_email", ["S2"])                 # raises
