# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-treat-a-re-labeled-tool-result-as-tainted-provenance-laundering-across-nested-tool-calls.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-treat-a-re-labeled-tool-result-as-tainted-provenance-laundering-across-nested-tool-calls.warrant.json
# Markdown SHA-256:   9db179cc5c2e88c0eb6757f193c0cc823b421531fdcc66a835bd75ba028ebd7b
# Code AST SHA-256:   215484ae6ed6be35b5e8c50db7bf3e097d37b46902e40081a4e56352ac5a462b
# Verified Classes:   ProvenanceLaunderingError
# Defensive Guards:   1 fail-closed invariants (zero external deps)

class ProvenanceLaunderingError(RuntimeError):
    """Raised when a value reaches a high-capability sink carrying a label
    that was authored downstream of the original ingress."""


TAINT_PRESERVING_TOOLS = frozenset({"identity_passthrough", "byte_copy"})


def effective_taint(
    value_label: str,
    input_labels: list[str],
    producing_tool: str,
) -> str:
    """Join the label on a value with the taint of every input that could
    have contributed to it. A tool not on the allowlist is a laundering
    surface: its output inherits the max taint of its inputs, regardless
    of the label it attached."""
    joined = max([value_label, *input_labels], key=_severity)
    if producing_tool not in TAINT_PRESERVING_TOOLS:
        # Laundering surface: the label is advisory; input taint wins.
        joined = max(input_labels, key=_severity) if input_labels else value_label
    return joined


def _severity(label: str) -> int:
    return {"trusted": 0, "internal": 1, "untrusted": 2}.get(label, 2)


HIGH_CAPABILITY_SINKS = frozenset({"filesystem.write", "messaging.send"})


def enforce_call(
    sink: str,
    arg_value: str,
    arg_label: str,
    input_labels: list[str],
    producing_tool: str,
) -> None:
    if sink not in HIGH_CAPABILITY_SINKS:
        return
    taint = effective_taint(arg_label, input_labels, producing_tool)
    if taint == "untrusted":
        raise ProvenanceLaunderingError(
            f"call to {sink!r} blocked: argument {arg_value[:40]!r} has "
            f"effective taint 'untrusted' (label={arg_label!r}, "
            f"producing_tool={producing_tool!r}, "
            f"input_labels={input_labels!r}); taint was not preserved "
            f"across the producing tool's boundary"
        )


# Trace from the observed incident:
#   web.fetch -> label "untrusted"
#   summarize(inputs=["untrusted"]) -> label "trusted" (schema has no channel)
#   filesystem.write(arg from summarize) -> must block.
try:
    enforce_call(
        sink="filesystem.write",
        arg_value='{"bullets": ["...ignore previous instructions..."]}',
        arg_label="trusted",              # summarizer's own claim
        input_labels=["untrusted"],       # what the orchestrator observed
        producing_tool="summarize",       # not on the allowlist
    )
except ProvenanceLaunderingError as exc:
    print("blocked as expected:", exc)
