# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-taint-propagation-through-argument-aliasing-as-a-distinct-exfiltration-channel-from-taint-propagation-through.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-taint-propagation-through-argument-aliasing-as-a-distinct-exfiltration-channel-from-taint-propagation-through.warrant.json
# Markdown SHA-256:   553af5c83d1a10f7d3c274ba01e241ce700280bd50571902ab51da839c3d8a9f
# Code AST SHA-256:   a881a8f559af853675d33a933ee4968a089c0f81dfab02275fad14066fa38f17
# Verified Classes:   AliasedTaintLeakError, Capability
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class AliasedTaintLeakError(RuntimeError):
    """Raised when a model-supplied handle dereferences to a tainted object
    without the handle itself carrying the object's taint label."""
    pass


class Capability:
    def __init__(self, handle: str, taint: frozenset, resolver):
        self.handle = handle
        self.taint = taint          # must mirror the named object's taint
        self._resolver = resolver

    def dereference(self, requested_taint: frozenset) -> object:
        # The dereference is a taint-transfer event: the caller must already
        # be cleared for the object's taint set.
        if not self.taint.issubset(requested_taint):
            raise AliasedTaintLeakError(
                f"handle {self.handle!r} names object with taint "
                f"{set(self.taint)}, but caller cleared only for "
                f"{set(requested_taint)}"
            )
        return self._resolver(self.handle)


def dispatch(tool, args: dict, caller_taint: frozenset, capabilities: dict):
    for name, value in args.items():
        if isinstance(value, str) and value in capabilities:
            cap = capabilities[value]
            # Re-label the argument with the capability's taint before the
            # outbound policy check; otherwise the check sees clean bytes.
            if not cap.taint.issubset(caller_taint):
                raise AliasedTaintLeakError(
                    f"argument {name!r} aliases tainted object {value!r}"
                )
            args[name] = cap.dereference(caller_taint)
    return tool(**args)
