# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-inherits-taint-it-cannot-see-provenance-collapse-across-federated-tool-chains.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-inherits-taint-it-cannot-see-provenance-collapse-across-federated-tool-chains.warrant.json
# Markdown SHA-256:   57bc1fb0347ce18c6c73f15d1356775339b124f43dc33577fb605a48f6b89044
# Code AST SHA-256:   647a657b288b396f0f599f00695949d0f46a37ecfe5bed6657a210d30c2e954c
# Verified Classes:   Trust, TaintCollapseError, Envelope
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from enum import IntEnum
from typing import Optional


class Trust(IntEnum):
    UNTRUSTED = 0
    MEDIUM = 1
    TRUSTED = 2


class TaintCollapseError(RuntimeError):
    """Raised when a message's declared min_trust exceeds the trust of its sources."""
    pass


@dataclass
class Envelope:
    sender: str
    sender_trust: Trust
    sources: list[str] = field(default_factory=list)
    source_min_trust: Trust = Trust.TRUSTED
    payload: str = ""

    @property
    def effective_trust(self) -> Trust:
        # Lineage-keyed: meet of sender trust and declared source floor.
        return Trust(min(int(self.sender_trust), int(self.source_min_trust)))


def forward(upstream: Envelope, summarizer_trust: Trust, new_payload: str) -> Envelope:
    """An intermediate agent re-serializes upstream output. Taint must be monotone."""
    if summarizer_trust < upstream.effective_trust:
        # Summarizer is less trusted than its input; output cannot be more trusted.
        declared_floor = summarizer_trust
    else:
        declared_floor = upstream.effective_trust
    return Envelope(
        sender="summarizer",
        sender_trust=summarizer_trust,
        sources=upstream.sources + [upstream.sender],
        source_min_trust=declared_floor,
        payload=new_payload,
    )


def accept(env: Envelope, action_threshold: Trust) -> None:
    """Receiving agent decides whether to act on a message."""
    if env.effective_trust < action_threshold:
        raise TaintCollapseError(
            f"message from {env.sender} has effective_trust={env.effective_trust.name} "
            f"below action_threshold={action_threshold.name}; "
            f"sources={env.sources}; quarantine required"
        )


def demo() -> None:
    # Leaf fetch: untrusted external page.
    leaf = Envelope(
        sender="fetch_tool",
        sender_trust=Trust.UNTRUSTED,
        sources=["https://example.com/page"],
        source_min_trust=Trust.UNTRUSTED,
        payload="...directive-shaped content...",
    )
    # Summarizer is a medium-trust agent. Taint must propagate, not upgrade.
    summary = forward(leaf, summarizer_trust=Trust.MEDIUM, new_payload="summary text")
    assert summary.effective_trust == Trust.UNTRUSTED, "taint upgraded across hop"
    # Planner requires MEDIUM to act. This must raise, not silently proceed.
    accept(summary, action_threshold=Trust.MEDIUM)


if __name__ == "__main__":
    demo()
