# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-launders-taint-through-a-summarization-tool-provenance-label-collapse-at-the-tool-return-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-launders-taint-through-a-summarization-tool-provenance-label-collapse-at-the-tool-return-boundary.warrant.json
# Markdown SHA-256:   4aee87570fbf90597d8069fdddae4a8ea7a693214449e526b31109467ee7578e
# Code AST SHA-256:   ed91997bdbdf89b7114d49993caa10c034857569106f68c45e47d1b84230becd
# Verified Classes:   TaintLaunderingError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class TaintLaunderingError(RuntimeError):
    """Raised when a derivation would lower taint below the join of its declared sources."""
    pass


def propagate_taint(tool_name: str, declared_data_inputs: list[dict], default_trust: bool = False) -> str:
    """
    Compute a tool output's taint from its declared data-flow inputs.
    Each input is {"segment_id": str, "taint": str} with taint in {"low", "high"}.
    Raises TaintLaunderingError if the schema declares no data-flow inputs
    (a laundering channel by construction) or if a lowering is attempted
    without a registered sanitizer capability.
    """
    if not declared_data_inputs:
        raise TaintLaunderingError(
            f"tool {tool_name!r} declares no data-flow inputs; "
            f"output is a laundering channel and must be quarantined, not default-trusted"
        )
    joined = "high" if any(i["taint"] == "high" for i in declared_data_inputs) else "low"
    if default_trust and joined == "high":
        # The exact defect observed: tool identity overriding lineage.
        raise TaintLaunderingError(
            f"tool {tool_name!r} attempted to default-trust a high-taint output; "
            f"taint must be computed from lineage, not tool identity"
        )
    return joined


def admit_to_memory(segment: dict, store_threshold: str = "low") -> dict:
    """Admit a segment only if its taint is at or below the store threshold."""
    taint = segment["taint"]
    if taint == "high" and store_threshold == "low":
        raise TaintLaunderingError(
            f"segment {segment['segment_id']!r} carries taint=high and cannot be "
            f"admitted to a taint=low store without explicit declassification"
        )
    return {"admitted": True, "segment_id": segment["segment_id"], "taint": taint}
