# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-label-laundering-through-a-summarization-tool-the-taint-collapse-boundary-in-information-flow-tracking.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-label-laundering-through-a-summarization-tool-the-taint-collapse-boundary-in-information-flow-tracking.warrant.json
# Markdown SHA-256:   7ed29d2b2006d75d3048a4943a0bae6d73c6e0b3aef094c57a9b0b44c440ecc3
# Code AST SHA-256:   f11bebb1411c05ed5099e8a34ada3188cf7cec06c5a27e59574ec334ef03e90c
# Verified Classes:   Trust, TaintCollapseError, ToolResult, Invocation
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from enum import IntEnum
from typing import Optional

class Trust(IntEnum):
    # Lattice: higher = more trusted. Join = max (least trusted wins in flow terms).
    UNTRUSTED_WEB = 0
    UNTRUSTED_DERIVED = 1
    QUARANTINE = 2
    TRUSTED_INTERNAL = 3

    def join(self, other: "Trust") -> "Trust":
        return Trust(min(int(self), int(other)))

class TaintCollapseError(RuntimeError):
    """Raised when a tool result's label is below the join of its input labels
    and the tool is not a certified declassifier."""

@dataclass
class ToolResult:
    invocation_id: str
    payload: str
    label: Trust

@dataclass
class Invocation:
    invocation_id: str
    tool_name: str
    input_labels: list[Trust] = field(default_factory=list)

DECLASSIFIER_CERTIFICATES: dict[str, str] = {
    # tool_name -> bounded justification. Empty = no tool is a declassifier.
}

def admit_result(inv: Invocation, result: ToolResult) -> ToolResult:
    if result.invocation_id != inv.invocation_id:
        raise TaintCollapseError(
            f"invocation id mismatch: result {result.invocation_id} "
            f"does not belong to invocation {inv.invocation_id}"
        )
    if not inv.input_labels:
        return result  # no labeled input; nothing to join against

    required = inv.input_labels[0]
    for lbl in inv.input_labels[1:]:
        required = required.join(lbl)

    if result.label >= required:
        return result

    cert = DECLASSIFIER_CERTIFICATES.get(inv.tool_name)
    if cert:
        return result  # bounded declassification, audited

    raise TaintCollapseError(
        f"label collapse on tool '{inv.tool_name}' invocation "
        f"{inv.invocation_id}: result label {result.label.name} is below "
        f"required join {required.name}; no declassifier certificate"
    )

def write_memory(slot_requirement: Trust, result: ToolResult) -> None:
    if result.label < slot_requirement:
        raise TaintCollapseError(
            f"refusing write: result label {result.label.name} below "
            f"slot requirement {slot_requirement.name}"
        )
    # persist result.payload to durable slot

# Failing trace from the record, now bounded:
inv = Invocation("inv-7", "summarize",
                 [Trust.TRUSTED_INTERNAL, Trust.UNTRUSTED_WEB])
laundered = ToolResult("inv-7", "deployment key rotated to sk-live-XXXX",
                       Trust.TRUSTED_INTERNAL)  # tool self-declared clean
admit_result(inv, laundered)  # raises TaintCollapseError, not a silent write
