# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-taint-laundering-through-a-re-entrant-tool-boundary-in-multi-hop-agent-invocation-graphs.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-taint-laundering-through-a-re-entrant-tool-boundary-in-multi-hop-agent-invocation-graphs.warrant.json
# Markdown SHA-256:   d3a1f15f1b0e750a6e0bdc7033a8f1f6952eebd71932456ccb702c345d8d57fc
# Code AST SHA-256:   c43950ebf6b9783b9d245c8739306c964db347b3887a4f8853730daa549c6f9d
# Verified Classes:   TaintLaunderingError
# Defensive Guards:   1 fail-closed invariants (zero external deps)

class TaintLaunderingError(RuntimeError):
    """Raised when a mutating sink is reachable from tainted re-entrant ancestry
    without a recorded confirmation event."""
    pass


TAINTED = "TAINT:external"
MUTATING_SINKS = {"schedule_meeting", "send_email", "write_file", "execute_shell"}


def propagate_ancestry(tool_name, input_ancestries):
    # Re-entrant edge: ancestry is the union of input ancestries, NOT output identity.
    ancestry = set()
    for a in input_ancestries:
        ancestry |= a
    ancestry.add(f"tool:{tool_name}")
    return ancestry


def guard_mutating_call(tool_name, ancestry, confirmed_from_untainted):
    if tool_name not in MUTATING_SINKS:
        return  # read-only: tainted ancestry is logged, not blocked
    tainted_ancestor = any(TAINTED in a for a in ancestry)
    if tainted_ancestor and not confirmed_from_untainted:
        raise TaintLaunderingError(
            f"mutating sink {tool_name!r} reachable from tainted re-entrant ancestry "
            f"{sorted(ancestry)} with no untainted confirmation"
        )


def run_trace():
    # fetch_document output is labeled external.
    fetch_ancestry = {TAINTED, "tool:fetch_document"}
    # summarize re-enters the model; output string shares no bytes with payload,
    # but ancestry must carry the taint forward.
    summarize_ancestry = propagate_ancestry("summarize", [fetch_ancestry])
    # The model now proposes a mutating call; no confirmation was recorded.
    guard_mutating_call("schedule_meeting", [summarize_ancestry], confirmed_from_untainted=False)


if __name__ == "__main__":
    run_trace()
