# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-invalidate-inherited-tool-warrants-after-a-prompt-injection-taint-crosses-a-tool-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-invalidate-inherited-tool-warrants-after-a-prompt-injection-taint-crosses-a-tool-boundary.warrant.json
# Markdown SHA-256:   d6a2a77cc8f6cd3b4c91634d136a02bb7f3c388067b19c957fc525fa40bc0575
# Code AST SHA-256:   e64a2bcd6adb2f117aa4c943d877456f1d9c15250eada986cdba165fb1303df3
# Verified Classes:   TaintLaunderingError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class TaintLaunderingError(RuntimeError):
    """Raised when a privileged warrant would be minted across a provenance gap."""
    pass


def mint_warrant(provenance_chain, action_scope, privileged_params):
    """
    provenance_chain: list of Hop(source_id, schema_has_taint_field, taint)
    Refuse to mint if any hop lacks a taint field AND the tainted span
    reaches a privileged parameter.
    """
    for hop in provenance_chain:
        if not hop.schema_has_taint_field:
            # Schema gap: cannot attest cleanliness past this hop.
            if _reaches_privileged(hop, privileged_params):
                raise TaintLaunderingError(
                    f"Provenance gap at source={hop.source_id!r}: "
                    f"tool schema omits taint field, yet tainted span "
                    f"reaches privileged params {privileged_params!r} "
                    f"for scope {action_scope!r}. Mint refused (fail-closed)."
                )
        if hop.taint and _reaches_privileged(hop, privileged_params):
            raise TaintLaunderingError(
                f"Tainted hop {hop.source_id!r} reaches privileged params "
                f"{privileged_params!r}; warrant for {action_scope!r} refused."
            )
    return {"scope": action_scope, "provenance": "clean", "chain": provenance_chain}


def _reaches_privileged(hop, privileged_params):
    return bool(set(hop.influenced_params) & set(privileged_params))
