# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-taints-a-tool-output-at-the-span-boundary-and-the-parent-generation-span-never-re-reads-it.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-taints-a-tool-output-at-the-span-boundary-and-the-parent-generation-span-never-re-reads-it.warrant.json
# Markdown SHA-256:   658c3b5e9dc4481df17e7fe56e822fcdb7e01d4a701bbfb5cbb120ee13878034
# Code AST SHA-256:   43293ee6d19cdc6414c3225b51bb87fc38f0d82bce3289c0c2b93808ac972534
# Verified Classes:   TaintPropagationError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class TaintPropagationError(RuntimeError):
    """Raised when a generation span consumed a tool output whose taint
    state was not propagated across the span boundary."""


def assert_generation_taint_propagated(
    generation_span_attrs: dict,
    child_tool_spans: list[dict],
) -> None:
    tainted_children = [
        s for s in child_tool_spans if s.get("taint") == "untrusted:external"
    ]
    if not tainted_children:
        return

    gen_taint = generation_span_attrs.get("taint")
    if gen_taint is None:
        raise TaintPropagationError(
            f"generation span has {len(tainted_children)} tainted child tool "
            f"span(s) but no taint attribute; state is indeterminate, not clean"
        )
    if gen_taint == "clean":
        raise TaintPropagationError(
            "generation span marked clean while consuming tainted tool output; "
            "context snapshot predates child attribute write"
        )
