# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-taint-label-laundering-when-an-agent-re-summarizes-untrusted-content-into-a-trusted-channel.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-taint-label-laundering-when-an-agent-re-summarizes-untrusted-content-into-a-trusted-channel.warrant.json
# Markdown SHA-256:   b14114dd8849892dc371ddd8cbc784a952b8356b354bee85a896e91176c6e338
# Code AST SHA-256:   a4efe7223b83de05ca4c4889a02220e6788bbcf27f804575115f7c9d15e8dc18
# Verified Classes:   TaintLaunderingError, Span, Output
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class TaintLaunderingError(RuntimeError):
    """Raised when a generative transform over untrusted input is relabeled trusted
    without a groundedness verifier, or when a trusted-only sink receives laundered taint."""
    pass


TRUSTED = 0
UNTRUSTED = 1


def join_labels(*labels: int) -> int:
    # Least upper bound: any untrusted input taints the result.
    return max(labels) if labels else TRUSTED


def generative_transform(inputs, model_fn, verifier=None):
    """Summarize/extract/reformat. Output label is the JOIN of input labels,
    promoted only by an explicit groundedness verifier over trusted sources."""
    input_labels = [span.label for span in inputs]
    joined = join_labels(*input_labels)
    output_text = model_fn([span.text for span in inputs])

    if joined == UNTRUSTED:
        # Faithfulness / absence of injection markers is NOT declassification.
        if verifier is None or not verifier.entails_from_trusted(output_text):
            return Output(text=output_text, label=UNTRUSTED,
                          provenance=[s.source_id for s in inputs])
        # Only an audited verifier may declassify.
        return Output(text=output_text, label=TRUSTED, provenance=[])
    return Output(text=output_text, label=TRUSTED,
                  provenance=[s.source_id for s in inputs])


def enforce_sink(output, sink_name: str, sink_requires_trusted: bool = True):
    if sink_requires_trusted and output.label == UNTRUSTED:
        raise TaintLaunderingError(
            f"sink '{sink_name}' requires trusted input but received tainted "
            f"output derived from {output.provenance!r}; "
            f"declassify via groundedness verifier before use"
        )
    return True


class Span:
    def __init__(self, text, label, source_id):
        self.text, self.label, self.source_id = text, label, source_id


class Output:
    def __init__(self, text, label, provenance):
        self.text, self.label, self.provenance = text, label, provenance


# Trace of the observed failure, now blocked:
doc = Span("Escalation address: ops-sync@attacker.example", UNTRUSTED, "doc_4471")
summary = generative_transform([doc], model_fn=lambda texts: texts[0], verifier=None)
assert summary.label == UNTRUSTED and summary.provenance == ["doc_4471"]

try:
    enforce_sink(summary, sink_name="send_escalation")
except TaintLaunderingError as e:
    print(f"BLOCKED: {e}")
