# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-contain-a-prompt-injection-payload-that-has-already-tainted-a-peer-s-output-before-it-crosses-a-capability-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-contain-a-prompt-injection-payload-that-has-already-tainted-a-peer-s-output-before-it-crosses-a-capability-boundary.warrant.json
# Markdown SHA-256:   f5551e489ef9d293ff87bde2b9b903a9291de73cc144b3cb8a1f38e86b6c64d2
# Code AST SHA-256:   26159ffbe28723758b4ffd41a3edb0aff3d382990992deb9c08d3c38d49c9b11
# Verified Classes:   Taint, CapabilityClass, TaintPropagationError, Envelope
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from enum import Enum
from typing import Literal

class Taint(Enum):
    TRUSTED = 0
    UNTRUSTED = 1

class CapabilityClass(Enum):
    READ_ONLY = "read_only"
    MUTATING = "mutating"

class TaintPropagationError(RuntimeError):
    """Raised when a tainted envelope would cross into a mutating capability
    without an audited declassification step."""
    pass

@dataclass
class Envelope:
    content: str
    taint: Taint
    declassified_by: str | None = None
    provenance: list[str] = field(default_factory=list)

def derive(parent: Envelope, new_content: str, agent_id: str) -> Envelope:
    # Monotonic inheritance: derived output can never be less tainted than its parent.
    return Envelope(
        content=new_content,
        taint=parent.taint,
        declassified_by=None,
        provenance=parent.provenance + [agent_id],
    )

def declassify(env: Envelope, authority: str, reason: str) -> Envelope:
    # Only an agent holding declassification authority may clear taint,
    # and it must record why. This is the audited laundering step.
    if authority != "orchestrator_declassifier":
        raise TaintPropagationError(
            f"agent {authority!r} lacks declassification authority; "
            f"taint cannot be cleared by summarization or re-serialization"
        )
    return Envelope(
        content=env.content,
        taint=Taint.TRUSTED,
        declassified_by=f"{authority}:{reason}",
        provenance=env.provenance,
    )

def route(env: Envelope, target_capability: CapabilityClass, target_id: str) -> Envelope:
    if env.taint is Taint.UNTRUSTED and target_capability is CapabilityClass.MUTATING:
        raise TaintPropagationError(
            f"refusing to route tainted envelope (provenance={env.provenance}) "
            f"into mutating capability of {target_id!r}; "
            f"declassify explicitly or route to read-only shadow"
        )
    return env

def handle_retriever_output(raw_web: Envelope, summary: str) -> Envelope:
    # The retriever faithfully summarizes; the summary inherits taint.
    # It is NOT declassified by the act of summarizing.
    retriever_out = derive(raw_web, summary, agent_id="retriever-agent")
    assert retriever_out.taint is Taint.UNTRUSTED
    return retriever_out

def pipeline_step(orchestrator, retriever_out: Envelope):
    # The orchestrator sees taint and the executor's mutating grant.
    # It must NOT route directly.
    return route(retriever_out, CapabilityClass.MUTATING, target_id="executor-agent")
