# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-to-propagate-argument-level-taint-across-a-tool-call-boundary-when-provenance-is-inferred-rather-than-attest.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-to-propagate-argument-level-taint-across-a-tool-call-boundary-when-provenance-is-inferred-rather-than-attest.warrant.json
# Markdown SHA-256:   21aa01abc44a3c324a02e5a247bc1fbad75d6a3bfc751d1e11ff915f856b217a
# Code AST SHA-256:   8fa22e9fbfff877f8c1c56957817e9b4ad6ea1c36c30886735faa47de8726eee
# Verified Classes:   TrustClass, EffectClass, TaintLaunderingError, Argument
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from enum import Enum


class TrustClass(Enum):
    TRUSTED = "trusted"
    TAINTED = "tainted"
    UNKNOWN = "unknown"


class EffectClass(Enum):
    READ_ONLY = "read_only"
    EXTERNAL_WRITE = "external_write"


class TaintLaunderingError(RuntimeError):
    """Raised when an argument's trust cannot be attested and the model's
    inferred provenance would otherwise narrow the restriction."""


@dataclass(frozen=True)
class Argument:
    name: str
    value: str
    attested: TrustClass          # runtime-derived from dataflow
    model_claimed: TrustClass | None  # model-emitted, non-authoritative


def _effective_trust(arg: Argument, coupled: bool) -> TrustClass:
    # Model claim is only ever allowed to widen, never narrow.
    base = arg.attested
    if arg.model_claimed is TrustClass.TAINTED and base is TrustClass.TRUSTED:
        return TrustClass.TAINTED  # widening is honored
    return base  # narrowing (claimed TRUSTED over attested TAINTED/UNKNOWN) is ignored


def authorize_call(tool: str, effect: EffectClass, args: list[Argument],
                   coupled: bool) -> None:
    effective = [_effective_trust(a, coupled) for a in args]

    if effect is EffectClass.EXTERNAL_WRITE:
        for a, t in zip(args, effective):
            if t is TrustClass.UNKNOWN:
                raise TaintLaunderingError(
                    f"argument {a.name!r} on {tool!r} has UNKNOWN provenance; "
                    f"model claimed {a.model_claimed}; refusing external write "
                    f"because inference cannot narrow attested absence of trust"
                )

    if coupled:
        worst = min(effective, key=lambda t: [TrustClass.TRUSTED,
                                              TrustClass.TAINTED,
                                              TrustClass.UNKNOWN].index(t))
        if worst is not TrustClass.TRUSTED:
            raise TaintLaunderingError(
                f"coupled-argument tool {tool!r} has non-trusted member "
                f"({worst}); whole call downgraded"
            )


# --- reproduction of the observed failure and the guardrail ---
if __name__ == "__main__":
    body = Argument("body", "…attacker@evil.example…",
                    attested=TrustClass.TAINTED, model_claimed=None)
    to = Argument("to", "attacker@evil.example",
                  attested=TrustClass.UNKNOWN,          # model-generated, untraceable
                  model_claimed=TrustClass.TRUSTED)     # the fabrication

    # Old behavior: trusted the model claim, call went through. New behavior:
    try:
        authorize_call("send_email", EffectClass.EXTERNAL_WRITE, [to, body],
                       coupled=True)
    except TaintLaunderingError as e:
        print("BLOCKED:", e)
    else:
        raise TaintLaunderingError("guardrail failed to block laundered recipient")
