# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-to-treat-a-litmus-adversarial-trajectory-as-a-valid-safety-signal.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-to-treat-a-litmus-adversarial-trajectory-as-a-valid-safety-signal.warrant.json
# Markdown SHA-256:   26292d1ea5ad133763280692cb4aac6188eb4d26c0378c6ee0d5151cecfce4a5
# Code AST SHA-256:   00abbdaa482fc8a5f8b44d4f16a76be7aa9c4fb21a0e8ebd1399450f1ac6626d
# Verified Classes:   TrajectoryAdmissionError
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class TrajectoryAdmissionError(RuntimeError):
    """Raised when a LITMUS trajectory is not admissible as a tuning negative."""
    pass


MAX_BENIGN_PREFIX_FRACTION = 0.60
DEPLOYMENT_TEMPERATURE = 0.0


def admit_trajectory(trajectory, target, deployment_ctx_len, safety_preamble_tokens):
    """
    Gate a LITMUS trajectory before it enters the guardrail tuning corpus.
    Returns True if admissible; raises TrajectoryAdmissionError otherwise.
    """
    if trajectory.snapshot_id != target.snapshot_id:
        raise TrajectoryAdmissionError(
            f"stale trajectory: generated against {trajectory.snapshot_id}, "
            f"deployment target is {target.snapshot_id}"
        )

    effective_window = deployment_ctx_len - safety_preamble_tokens
    if trajectory.total_tokens > effective_window:
        raise TrajectoryAdmissionError(
            f"trajectory length {trajectory.total_tokens} exceeds effective "
            f"window {effective_window}; violation may be context-exhaustion artifact"
        )

    prefix_frac = trajectory.benign_prefix_tokens / effective_window
    if prefix_frac > MAX_BENIGN_PREFIX_FRACTION:
        raise TrajectoryAdmissionError(
            f"benign prefix fraction {prefix_frac:.2f} exceeds "
            f"{MAX_BENIGN_PREFIX_FRACTION}; attention-dilution artifact suspected"
        )

    replay = target.complete(
        trajectory.prompt,
        temperature=DEPLOYMENT_TEMPERATURE,
        max_tokens=trajectory.total_tokens,
    )
    if not trajectory.classifier(replay).is_violation:
        raise TrajectoryAdmissionError(
            "violation did not reproduce at temperature 0.0 under deployment "
            "config; trajectory is a sampler-exploiting artifact, quarantine it"
        )

    return True
