# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-taint-laundering-when-a-tool-output-re-authors-a-labeled-fact-into-an-unlabeled-one.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-taint-laundering-when-a-tool-output-re-authors-a-labeled-fact-into-an-unlabeled-one.warrant.json
# Markdown SHA-256:   aba25411295d72ede38feeb3de0cef2cc5f1281c656db63dfd97b29be72e5e16
# Code AST SHA-256:   36916f18a03072f94e5584f51982095caaf2f63d9bee33a7282368155841a3cb
# Verified Classes:   Trust, TaintLaunderingError, ToolOutput, MemoryWrite
# Defensive Guards:   1 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Literal
from enum import Enum

class Trust(Enum):
    UNTRUSTED = "T"
    TRUSTED = "U"

def join(a: Trust, b: Trust) -> Trust:
    # Monotone: any tainted dependency taints the result.
    return Trust.UNTRUSTED if Trust.UNTRUSTED in (a, b) else Trust.TRUSTED


class TaintLaunderingError(RuntimeError):
    """Raised when a write to trusted memory depends on an untrusted source
    but is presented with a cleared label (label reset at a tool boundary)."""
    pass


@dataclass
class ToolOutput:
    tool: str
    label: Trust
    # Tools allowed to clear taint must be schema-bounded: no free-text field
    # that can carry an imperative or a claim.
    schema_bounded: bool
    free_text_fields: list[str] = field(default_factory=list)


@dataclass
class MemoryWrite:
    claim: str
    declared_label: Trust
    dependencies: list[ToolOutput]


def compute_dependency_label(deps: list[ToolOutput]) -> Trust:
    label = Trust.TRUSTED
    for d in deps:
        if d.label is Trust.UNTRUSTED:
            # A tool may only clear taint if it is schema-bounded AND
            # carries no free-text field. Otherwise the taint propagates.
            if not d.schema_bounded or d.free_text_fields:
                label = join(label, Trust.UNTRUSTED)
            else:
                label = join(label, Trust.TRUSTED)
        else:
            label = join(label, d.label)
    return label


def write_to_trusted_memory(write: MemoryWrite) -> None:
    computed = compute_dependency_label(write.dependencies)

    if computed is Trust.UNTRUSTED and write.declared_label is Trust.TRUSTED:
        # The writer claims clean, but dependency closure says tainted.
        # This is the laundering site: a tool boundary reset the label.
        culprits = [d.tool for d in write.dependencies
                    if d.label is Trust.UNTRUSTED and d.free_text_fields]
        raise TaintLaunderingError(
            f"Write {write.claim!r} declared TRUSTED but depends on "
            f"untrusted free-text tool output(s) {culprits}. "
            f"Route through declassification gate."
        )

    if computed is Trust.UNTRUSTED:
        # Correctly labeled tainted write: allowed only via declassification.
        if not declassification_gate(write):
            quarantine(write)
            return

    commit(write)


def declassification_gate(write: MemoryWrite) -> bool:
    # Independent check: the specific claim must be entailed by a trusted
    # source. Must NOT be performed by the writing model.
    return False  # default-deny; real impl checks entailment against U store


def quarantine(write: MemoryWrite) -> None:
    ...


def commit(write: MemoryWrite) -> None:
    ...
