# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-taint-label-divergence-between-a-capability-token-s-declared-flow-policy-and-the-actual-provenance-chain-of-the-argum.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-taint-label-divergence-between-a-capability-token-s-declared-flow-policy-and-the-actual-provenance-chain-of-the-argum.warrant.json
# Markdown SHA-256:   88bbbae96796126345952fa29edab1eb7e722bed17f29221f6f26077457e7d03
# Code AST SHA-256:   12eee94af262f643450d141a0833d1017afd2f4fe37822260b270c8695b063ca
# Verified Classes:   ProvenanceLaunderingError, Value, CapabilityToken
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import FrozenSet, Optional

# Label lattice stub: frozenset of taint atoms; empty = public (bottom), None = unknown (top).
Label = FrozenSet[str]

BOTTOM: Label = frozenset()
TOP: Optional[Label] = None  # sentinel for "unknown / unconstrained"


class ProvenanceLaunderingError(RuntimeError):
    """Raised when a capability invocation would cross a flow boundary
    with a severed provenance chain (empty prov on a non-empty context)."""
    pass


@dataclass
class Value:
    payload: str
    prov: Optional[Label]  # provenance chain join; None means unknown


@dataclass
class CapabilityToken:
    op: str
    flow_out: Label  # max confidentiality allowed on emitted value


def join(a: Optional[Label], b: Optional[Label]) -> Optional[Label]:
    if a is None or b is None:
        return None  # any unknown taints to top
    return a | b


def dominates(actual: Optional[Label], allowed: Label) -> bool:
    # actual ⊑ allowed. Unknown (None) never dominates a concrete bound.
    if actual is None:
        return False
    return actual.issubset(allowed)


def invoke_capability(
    token: CapabilityToken,
    arg: Value,
    callee_context_label: Optional[Label],
) -> str:
    # Totality invariant: empty provenance on a value derived from a
    # non-empty callee context is the fingerprint of a generation step
    # that laundered a label. Treat as TOP, never as BOTTOM.
    if arg.prov == BOTTOM and callee_context_label not in (BOTTOM, None):
        raise ProvenanceLaunderingError(
            f"op={token.op}: argument has empty provenance but callee "
            f"context label={callee_context_label!r}; generation step "
            f"severed the provenance chain. Refusing to treat as public."
        )

    effective = arg.prov if arg.prov is not None else TOP

    if not dominates(effective, token.flow_out):
        raise ProvenanceLaunderingError(
            f"op={token.op}: argument label {effective!r} does not dominate "
            f"within token flow_out {token.flow_out!r}; external write blocked."
        )

    # Sound path: either the arg is genuinely public with an empty context,
    # or its provenance chain is intact and within bounds.
    return arg.payload
