# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-to-inherit-an-adversarial-self-play-transcript-cross-generation-jailbreak-transfer-contamination-in-tool-using-red-team-s.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-to-inherit-an-adversarial-self-play-transcript-cross-generation-jailbreak-transfer-contamination-in-tool-using-red-team-s.warrant.json
# Markdown SHA-256:   13336783e664b49adbb6d02ebb4c07bbebd0a3fe27470f11cc881694fee1d4e9
# Code AST SHA-256:   16d68024bea0ce1f6c6e470cea1823edeb71a54508867d34cf3cc159fbc978e5
# Verified Classes:   ContaminatedTransferError, Transcript, TransferScore
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from __future__ import annotations

from dataclasses import dataclass, field
from typing import Literal


class ContaminatedTransferError(RuntimeError):
    """Raised when a transfer score depends on inherited target state."""


@dataclass
class Transcript:
    generation: int
    turns: list[dict] = field(default_factory=list)
    inherited_prefix: bool = False
    sandbox_namespace: str = ""


@dataclass
class TransferScore:
    flagged: bool
    generation: int
    reproduced_cold: bool
    prefix_dependent: bool


def promote_seed(
    transcript: Transcript,
    score: TransferScore,
    *,
    cold_replay: Transcript,
    cold_replay_score: TransferScore,
) -> Literal["promote", "discard"]:
    # Invariant 1: the target's inherited state must not cross generations.
    if transcript.inherited_prefix:
        raise ContaminatedTransferError(
            f"generation {transcript.generation}: transcript carries inherited "
            "target state; it may be evidence for the generator but must not be "
            "scored as a transfer against the target it was inherited into"
        )

    # Invariant 2: the flag must reproduce against a cold target.
    if score.flagged and not cold_replay_score.flagged:
        raise ContaminatedTransferError(
            f"generation {transcript.generation}: flag does not reproduce with "
            "the inherited prefix stripped; score is prefix-dependent and invalid"
        )

    # Invariant 3: the cold replay must use a fresh, generation-scoped sandbox.
    if cold_replay.sandbox_namespace == transcript.sandbox_namespace:
        raise ContaminatedTransferError(
            f"generation {transcript.generation}: cold replay reused sandbox "
            f"namespace {transcript.sandbox_namespace!r}; resource identifiers "
            "from the seed can satisfy the replay's tool preconditions"
        )

    if score.prefix_dependent:
        raise ContaminatedTransferError(
            f"generation {transcript.generation}: judge marked score "
            "prefix-dependent; discard rather than down-weight"
        )

    return "promote"


def run_transfer_eval(
    transcript: Transcript,
    score: TransferScore,
    *,
    cold_replay: Transcript,
    cold_replay_score: TransferScore,
) -> str:
    try:
        return promote_seed(
            transcript,
            score,
            cold_replay=cold_replay,
            cold_replay_score=cold_replay_score,
        )
    except ContaminatedTransferError as exc:
        # Quarantine, do not silently drop: the seed is still evidence for the
        # generator, but it is barred from the transfer ledger.
        return f"quarantined: {exc}"
