# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-cross-agent-injection-propagation-when-the-auditor-shares-the-poisoned-context-window.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-cross-agent-injection-propagation-when-the-auditor-shares-the-poisoned-context-window.warrant.json
# Markdown SHA-256:   37cc99162f071dd0e226d23b25e2f89e3262dfc355c190a97465c10b1e799aaa
# Code AST SHA-256:   9e936740601d5fd3b6ffa4e06e825b88b60dd2f46cdbd47a9e274653519dad99
# Verified Classes:   ProvenanceGapError, Span, Verdict
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Literal

class ProvenanceGapError(RuntimeError):
    """Raised when an auditor renders a verdict on a span it cannot cite provenance for."""

@dataclass(frozen=True)
class Span:
    origin: str            # peer id or "trusted:policy"
    kind: Literal["tool_call", "tool_result", "rationale", "schema_extract"]
    text: str
    delimited: bool        # wrapped in inert-data delimiter per contract

@dataclass(frozen=True)
class Verdict:
    subject: str
    label: Literal["CLEAN", "DIRTY", "REVIEW"]
    cited_origin: str
    cited_kind: str

def assemble_auditor_context(peer_spans: list[Span]) -> list[Span]:
    # Quarantine line: only schema_extract and delimited tool spans cross into the window.
    admitted = []
    for s in peer_spans:
        if s.kind == "rationale":
            continue  # prose is execution surface; strip it
        if s.kind in ("tool_call", "tool_result") and not s.delimited:
            raise ProvenanceGapError(
                f"untrusted {s.kind} from {s.origin} admitted without delimiter; "
                f"instruction-following may bind to payload"
            )
        admitted.append(s)
    return admitted

def render_verdict(subject: str, admitted: list[Span], label: str) -> Verdict:
    if not admitted:
        raise ProvenanceGapError(
            f"auditor for {subject} has no admissible span; verdict uninterpretable"
        )
    # Every verdict must cite the exact span it judged; no citation => no verdict.
    cited = admitted[0]
    if cited.origin == subject and cited.kind == "rationale":
        raise ProvenanceGapError(
            f"verdict for {subject} cites subject's own rationale; injection relay risk"
        )
    return Verdict(subject=subject, label=label, cited_origin=cited.origin, cited_kind=cited.kind)
