# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-quarantine-tainted-provenance-across-a-multi-hop-tool-call-dataflow-graph-when-taint-labels-are-lost-at-the-summarization-bound.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-quarantine-tainted-provenance-across-a-multi-hop-tool-call-dataflow-graph-when-taint-labels-are-lost-at-the-summarization-bound.warrant.json
# Markdown SHA-256:   cc1fc5c51f37260cde010ab3c2e8132a8e98148a3c4dbd2af5869b07aeaadc13
# Code AST SHA-256:   891a38b21d7b3424ca3657cccdc83ab2afb8ad96e9835f5585f626bc899c0141
# Verified Classes:   TaintLabel, TaintLaunderingError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class TaintLabel:
    TRUSTED = 0
    UNTRUSTED = 1

    @staticmethod
    def join(a: int, b: int) -> int:
        # Least upper bound: any untrusted input taints the result.
        return max(a, b)


class TaintLaunderingError(RuntimeError):
    """Raised when a tool returns text without a provenance label,
    or when a privileged sink is handed an argument whose taint
    exceeds its privilege. This is a contract violation, not a
    content-filter miss: the label was destroyed, not the payload."""


def propagate_through_tool(
    tool_name: str,
    input_labels: list[int],
    output_label: int | None,
    sink_privilege: int | None = None,
) -> int:
    """Compute the output label of a tool call as the join of its
    input labels, and verify the tool did not launder provenance.

    output_label is what the tool *claims* its return value carries.
    None means the tool's contract does not expose provenance at all.
    """
    if output_label is None:
        # Lossy hop with no provenance field: fail closed. Do not
        # assume trusted; do not infer from content.
        raise TaintLaunderingError(
            f"tool {tool_name!r} returned unlabeled text; "
            f"cannot compute join of {input_labels} — refusing to "
            f"promote to TRUSTED by default"
        )

    expected = TaintLabel.TRUSTED
    for lbl in input_labels:
        expected = TaintLabel.join(expected, lbl)

    if output_label < expected:
        # Tool claimed a lower taint than its inputs permit. This is
        # the summarizer that paraphrased an injection into planner
        # voice and dropped the label.
        raise TaintLaunderingError(
            f"tool {tool_name!r} laundered provenance: inputs imply "
            f"label {expected}, tool reported {output_label}"
        )

    if sink_privilege is not None and output_label > sink_privilege:
        raise TaintLaunderingError(
            f"sink for {tool_name!r} has privilege {sink_privilege} "
            f"but argument carries label {output_label}; route through "
            f"an out-of-band declassification gate, not a content filter"
        )

    return output_label


def run_pipeline() -> None:
    # Hop 1: fetch returns UNTRUSTED body, correctly labeled.
    body_label = propagate_through_tool(
        "fetch_url", input_labels=[], output_label=TaintLabel.UNTRUSTED
    )
    # Hop 2: summarizer is lossy and its contract omits provenance.
    # This must raise, not silently pass.
    summary_label = propagate_through_tool(
        "summarize",
        input_labels=[body_label],
        output_label=None,  # schema has no provenance field
    )
    # Hop 3 would be execute; unreachable because hop 2 failed closed.
    propagate_through_tool(
        "execute",
        input_labels=[summary_label],
        output_label=summary_label,
        sink_privilege=TaintLabel.TRUSTED,
    )


if __name__ == "__main__":
    try:
        run_pipeline()
    except TaintLaunderingError as exc:
        print(f"pipeline halted at taint boundary: {exc}")
