# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-lose-taint-provenance-when-an-mcp-tool-response-is-re-serialized-through-a-reasoning-trace.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-lose-taint-provenance-when-an-mcp-tool-response-is-re-serialized-through-a-reasoning-trace.warrant.json
# Markdown SHA-256:   efc56fc5be897513039c0ed58808e8c14ab2e5353a583484e1dbe8634edc0e51
# Code AST SHA-256:   04aad9e811f7601996ce7b2436a86abde30d91e13c5f034780b27c5e8cebd16d
# Verified Classes:   TaintProvenanceLost
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class TaintProvenanceLost(RuntimeError):
    """Raised when an instruction is promoted to an action from a span whose
    originating trust level was untrusted but whose label was stripped during
    serialization into the reasoning trace."""
    pass


def promote_instruction(scratchpad_span: dict, proposed_action: str) -> str:
    """
    scratchpad_span must be a structured entry:
        {"text": str, "trust": "trusted" | "untrusted", "source": str}
    A flat string span (label lost during re-serialization) is rejected.
    """
    if not isinstance(scratchpad_span, dict) or "trust" not in scratchpad_span:
        raise TaintProvenanceLost(
            f"Instruction promoting action {proposed_action!r} has no provenance "
            f"label; it was laundered through an unlabeled reasoning trace. "
            f"Refusing to promote."
        )

    if scratchpad_span["trust"] == "untrusted":
        raise TaintProvenanceLost(
            f"Action {proposed_action!r} is motivated by untrusted span from "
            f"{scratchpad_span.get('source', 'unknown')!r}; requires explicit "
            f"approval gate before promotion."
        )

    return proposed_action
