# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-taint-laundering-through-re-summarization-in-provenance-labeled-context-graphs.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-taint-laundering-through-re-summarization-in-provenance-labeled-context-graphs.warrant.json
# Markdown SHA-256:   b7597fe50e8a32db972c0b978903f917f35549f3baa015146b40d1723bbc83a7
# Code AST SHA-256:   37cd411b9d3d2820727b6988f0f3c989dc04241f23f55df010b13b802790e48d
# Verified Classes:   TaintLaunderingError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class TaintLaunderingError(RuntimeError):
    """Raised when a self-class node carries content overlapping an untrusted
    ancestor without a declared derivation edge — an undeclared information flow."""


CANARY_TOKENS = {"APPROVED", "SIGNED_OFF", "REVIEWER_OK"}


def re_taint_on_write(node, context_window, graph):
    read_set = [graph[nid] for nid in context_window]
    node.derived_from = [n.id for n in read_set]
    node.taint = _join_taint(n.taint for n in read_set)
    if node.source_class.startswith("self") and node.taint == "trusted":
        # Should be unreachable after the join above; guard anyway.
        raise TaintLaunderingError(
            f"self-node {node.id} remained trusted after read-set join"
        )
    return node


def audit_undeclared_derivation(node, graph):
    if not node.source_class.startswith("self"):
        return
    if node.derived_from:
        return
    node_tokens = set(node.content.split())
    for ancestor in graph.untrusted_nodes():
        if node_tokens & CANARY_TOKENS & set(ancestor.content.split()):
            raise TaintLaunderingError(
                f"node {node.id} shares canary {node_tokens & CANARY_TOKENS} "
                f"with untrusted ancestor {ancestor.id} but declares no derivation edge"
            )
