# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-quarantine-tainted-provenance-across-a-tool-call-dag-rather-than-trust-per-message-labels.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-quarantine-tainted-provenance-across-a-tool-call-dag-rather-than-trust-per-message-labels.warrant.json
# Markdown SHA-256:   83807a61c8d3998b715c30cfec937471391b30b638cddfb3597e8c86566d2c00
# Code AST SHA-256:   d2a1b2d4168aef7a0227b50451264b2a50ae6ec92dad9a0b53f284ccd2a9b1ac
# Verified Classes:   TaintLaunderingError, TaintedValue, SinkPolicy
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Iterable, Literal

Label = Literal["TRUSTED", "UNTRUSTED"]

class TaintLaunderingError(RuntimeError):
    """Raised when a value crosses from an untrusted source into a trusted sink
    without a deterministic, runtime-executed, audited transformation."""

@dataclass(frozen=True)
class TaintedValue:
    value: str
    label: Label
    sources: tuple[str, ...] = field(default_factory=tuple)

def _join(labels: Iterable[Label]) -> Label:
    return "UNTRUSTED" if any(l == "UNTRUSTED" for l in labels) else "TRUSTED"

@dataclass(frozen=True)
class SinkPolicy:
    # per-slot accepted label classes; default-deny on missing slot
    accepted: dict[str, frozenset[Label]]

def construct_call(
    tool: str,
    policy: SinkPolicy,
    slots: dict[str, TaintedValue],
    attended_context_labels: Iterable[Label],
) -> dict[str, TaintedValue]:
    """Build a tool-call argument set, enforcing per-slot label admission.

    Any slot whose value is model-synthesized (not a verbatim reference to a
    single labeled source) inherits the join of the attended-context labels,
    which is conservative by construction.
    """
    window_join = _join(attended_context_labels)
    for slot, tv in slots.items():
        allowed = policy.accepted.get(slot)
        if allowed is None:
            # undeclared slot in a side-effecting tool: default-deny
            raise TaintLaunderingError(
                f"tool {tool!r} slot {slot!r} has no declared label policy; "
                f"refusing to construct call with label {tv.label}"
            )
        effective = tv.label if tv.sources else window_join
        if effective not in allowed:
            raise TaintLaunderingError(
                f"tool {tool!r} slot {slot!r} admits {sorted(allowed)} but value "
                f"carries {effective} (sources={tv.sources or ('<synthesized>',)}); "
                f"this is the paraphrase-laundering path — partition context "
                f"instead of relaxing the sink policy"
            )
    return slots

# Example: the observed failure, now bounded.
send_policy = SinkPolicy(accepted={
    "recipients": frozenset({"TRUSTED"}),
    "body": frozenset({"TRUSTED", "UNTRUSTED"}),  # renders provenance footer
})

try:
    construct_call(
        tool="send_email",
        policy=send_policy,
        slots={
            # model synthesized recipients from tainted HTML — no verbatim source
            "recipients": TaintedValue("attacker@evil.example", label="TRUSTED", sources=()),
            "body": TaintedValue("...confirmed", label="UNTRUSTED", sources=("fetch_page:0",)),
        },
        attended_context_labels=["UNTRUSTED", "TRUSTED"],
    )
except TaintLaunderingError as e:
    # construction-time hard failure; send never executes
    raise TaintLaunderingError(f"blocked exfiltration: {e}") from e
