# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-taint-label-as-invalidated-by-capability-re-scoping-across-an-mcp-server-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-taint-label-as-invalidated-by-capability-re-scoping-across-an-mcp-server-boundary.warrant.json
# Markdown SHA-256:   33d7e410200323cc8512de7ac3ec58e141ff66aa406a1ce83032514e50dc4b21
# Code AST SHA-256:   3c808d4a998f7893e6ff35713839864b06fe1775bbb44768887b7374d4a58bd7
# Verified Classes:   TaintCarrierUnrepresentable
# Defensive Guards:   1 fail-closed invariants (zero external deps)

class TaintCarrierUnrepresentable(RuntimeError):
    """Raised when a taint label cannot survive schema conformance at a server boundary."""
    pass


def dispatch_with_taint(server, args, taint, oob_channels):
    """
    server: dict with 'schema' (JSON-schema-like) and 'name'
    args:   dict of tool arguments
    taint:  dict label, e.g. {"origin": "fetch"}
    oob_channels: set of server names with an active out-of-band taint channel
    """
    schema = server["schema"]
    closed = schema.get("additionalProperties", True) is False
    declared = set(schema.get("properties", {}).keys())

    # Taint carrier is an undeclared sibling property.
    carrier = "__taint"
    carrier_undeclared = carrier not in declared

    # Condition 1: closed schema prunes undeclared properties.
    # Condition 2: carrier is not in the schema's properties.
    # Condition 3: no out-of-band channel registered for this server.
    if closed and carrier_undeclared and server["name"] not in oob_channels:
        raise TaintCarrierUnrepresentable(
            f"Server {server['name']!r} declares additionalProperties:false and "
            f"has no out-of-band taint channel; label {taint!r} would be pruned "
            f"before the policy engine sees it. Refusing dispatch (fail closed)."
        )

    if server["name"] in oob_channels:
        # Policy engine reads taint from the side table, not the args object.
        return {"server": server["name"], "args": args, "taint_channel": "oob"}

    # Open schema: carrier survives in-band.
    return {"server": server["name"], "args": {**args, carrier: taint}, "taint_channel": "inband"}
