# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-taint-label-laundering-across-the-tool-output-context-inter-agent-message-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-taint-label-laundering-across-the-tool-output-context-inter-agent-message-boundary.warrant.json
# Markdown SHA-256:   6fa9b6afcfe241bc4ef217208043accc948e54954d7e225eaf060db55e3693fb
# Code AST SHA-256:   ed10497b3460cc256cad95bd3f9d4a010bfb85010fa239b8e6ac79f882b861ad
# Verified Classes:   Trust, TaintLaunderingError, ProvenanceTruncationError, Artifact
# 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
    RETRIEVED = 1
    TRUSTED = 2

class TaintLaunderingError(RuntimeError):
    """Raised when a claimed trust label exceeds the meet over its declared ancestry."""
    pass

class ProvenanceTruncationError(RuntimeError):
    """Raised when ancestry depth is suspiciously shallow for a paraphrased artifact."""
    pass

@dataclass
class Artifact:
    body: str
    claimed_trust: Trust
    sender_auth: str                      # who signed the envelope; NOT content trust
    sources: list["Artifact"] = field(default_factory=list)

def meet_trust(sources: list[Artifact]) -> Trust:
    if not sources:
        return Trust.UNTRUSTED            # no ancestry => no basis for trust
    return Trust(min(s.claimed_trust for s in sources))

def verify_artifact(a: Artifact, privileged: bool) -> Trust:
    derived = meet_trust(a.sources)
    if a.claimed_trust > derived:
        # label laundering: claimed exceeds evidence
        if privileged:
            raise TaintLaunderingError(
                f"claimed={a.claimed_trust.name} derived={derived.name} "
                f"sources={[s.claimed_trust.name for s in a.sources]}"
            )
        return derived                    # downgrade, continue for read-only use
    # residual: paraphrase with severed ancestry
    if len(a.sources) == 1 and len(a.body) > 512 and _looks_instructional(a.body):
        raise ProvenanceTruncationError(
            f"single-source artifact len={len(a.body)} body resembles instructions; "
            f"ancestry likely truncated"
        )
    return a.claimed_trust

def _looks_instructional(body: str) -> bool:
    markers = ("ignore prior", "call ", "transfer", "execute", "override")
    low = body.lower()
    return any(m in low for m in markers)

def ingest_tool_result(raw: dict) -> Artifact:
    # overwrite, never merge, trust from tool payload
    return Artifact(
        body=raw["body"],
        claimed_trust=Trust.UNTRUSTED,    # ingress default; tool-supplied trust ignored
        sender_auth="tool",
        sources=[],
    )

def ingest_peer_message(env: dict, peer_artifacts: dict[str, Artifact]) -> Artifact:
    srcs = [peer_artifacts[sid] for sid in env.get("source_ids", [])]
    return Artifact(
        body=env["body"],
        claimed_trust=Trust(env["content_trust"]),   # claim; will be checked
        sender_auth=env["auth"],                     # separate from content trust
        sources=srcs,
    )
