# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-to-widen-a-taint-label-after-a-tool-returns-sanitized-output-the-laundering-through-summarization-boundary-in-taint-aware.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-to-widen-a-taint-label-after-a-tool-returns-sanitized-output-the-laundering-through-summarization-boundary-in-taint-aware.warrant.json
# Markdown SHA-256:   b0cd688809e4f70168b497a303eafa058413261469ace300b02268e756cc0431
# Code AST SHA-256:   737f1547a27ef65fd7fc7e60f80fc9b2c872277421f85f692639646e9b3a815d
# Verified Classes:   TaintNarrowingViolation, Transform
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class TaintNarrowingViolation(RuntimeError):
    """Raised when a transform attempts to drop a taint label without a valid declassification proof."""
    pass


class Transform:
    def __init__(self, name, deterministic, output_alphabet_fixed, residual_capacity_declared, model_mediated):
        self.name = name
        self.deterministic = deterministic
        self.output_alphabet_fixed = output_alphabet_fixed
        self.residual_capacity_declared = residual_capacity_declared
        self.model_mediated = model_mediated


def narrow_taint(taint_label, transform):
    """Return the output taint label after applying `transform` to a value labeled `taint_label`.

    A transform may narrow taint only if it is a valid declassification proof.
    Model-mediated transforms (summarizers, rewriters, chat completions) never qualify.
    """
    if taint_label == "clean":
        return "clean"

    is_proof = (
        transform.deterministic
        and transform.output_alphabet_fixed
        and transform.residual_capacity_declared
        and not transform.model_mediated
    )

    if not is_proof:
        raise TaintNarrowingViolation(
            f"Transform '{transform.name}' cannot narrow taint '{taint_label}': "
            f"deterministic={transform.deterministic}, "
            f"output_alphabet_fixed={transform.output_alphabet_fixed}, "
            f"residual_capacity_declared={transform.residual_capacity_declared}, "
            f"model_mediated={transform.model_mediated}. "
            "The output inherits the input taint label; the privileged gate must fail closed."
        )

    return "clean"


def gate_privileged_skill(skill_name, arg_taint, transform):
    """Gate a privileged skill on taint. Fails closed on any narrowing violation."""
    try:
        output_taint = narrow_taint(arg_taint, transform)
    except TaintNarrowingViolation as exc:
        # Fail closed: do not invoke the skill; surface the block for audit.
        raise TaintNarrowingViolation(
            f"Blocked '{skill_name}': {exc}"
        ) from exc

    if output_taint != "clean":
        raise TaintNarrowingViolation(
            f"Blocked '{skill_name}': argument remains tainted ('{output_taint}')."
        )
    return True


# Example: a summarizer is model-mediated and therefore never declassifies.
summarizer = Transform(
    name="summarize",
    deterministic=False,
    output_alphabet_fixed=False,
    residual_capacity_declared=False,
    model_mediated=True,
)

# This raises TaintNarrowingViolation and halts the send.
gate_privileged_skill("send_email", taint_label="untrusted_web_fetch", transform=summarizer)
