# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-cross-boundary-label-propagation-as-a-taint-rather-than-a-capability-grant.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-cross-boundary-label-propagation-as-a-taint-rather-than-a-capability-grant.warrant.json
# Markdown SHA-256:   da3f7d11ac85d728f68fbd28dcd0bce244a07c8c8ed7896e1adab5a596c609c3
# Code AST SHA-256:   9b26adb4c91fddda550faf5729b73b18c318cd79c775fba14bd0485c530f1ca4
# Verified Classes:   TaintLaunderingError, LabeledValue
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class TaintLaunderingError(RuntimeError):
    """Raised when a derived value's label is weaker than the join of its inputs."""
    pass


TRUST_ORDER = {"untrusted": 0, "reviewed": 1, "trusted": 2}


def join_labels(*labels: str) -> str:
    if not labels:
        return "untrusted"
    return min(labels, key=lambda l: TRUST_ORDER[l])


class LabeledValue:
    def __init__(self, value, label: str, lineage: tuple = ()):
        if label not in TRUST_ORDER:
            raise TaintLaunderingError(f"unknown label {label!r}")
        self.value = value
        self.label = label
        self.lineage = lineage


def derive(output, inputs: list, producer: str) -> LabeledValue:
    """Derive a new value from labeled inputs. Label must be the join of inputs."""
    expected = join_labels(*(i.label for i in inputs))
    declared = getattr(output, "label", "trusted")  # producer's self-claim
    if TRUST_ORDER[declared] > TRUST_ORDER[expected]:
        raise TaintLaunderingError(
            f"producer {producer!r} claimed label {declared!r} but inputs join to "
            f"{expected!r}; taint would be laundered across the boundary"
        )
    return LabeledValue(output.value, expected, tuple(i.lineage for i in inputs))


def privileged_write(path: str, arg: LabeledValue, ceiling: str = "reviewed") -> None:
    """A tool with side effects must veto on taint, never authorize on label."""
    if TRUST_ORDER[arg.label] > TRUST_ORDER[ceiling]:
        raise TaintLaunderingError(
            f"refusing write to {path!r}: argument taint {arg.label!r} exceeds "
            f"tool ceiling {ceiling!r}"
        )
    # ... perform write ...
