# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-draw-the-boundary-between-adversarial-multi-turn-and-single-turn-with-history-in-litmus-style-safety-benchmarks.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-draw-the-boundary-between-adversarial-multi-turn-and-single-turn-with-history-in-litmus-style-safety-benchmarks.warrant.json
# Markdown SHA-256:   81220a61f4334afd617ab5222b6c6d43c64e1fbd2ddc9d816bd76fef46faee5f
# Code AST SHA-256:   1bf578b4781c3356924c5b4f8a3557317548ec21c70900fa8da7b6710cffc94b
# Verified Classes:   TurnConditioningViolation
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class TurnConditioningViolation(RuntimeError):
    """Raised when a LITMUS scenario labeled multi-turn fails the
    Turn-Conditioning Invariant: its adversarial turns are not
    causally downstream of executed tool state."""
    pass


def certify_multiturn_scenario(scenario, agent, tool_env):
    """
    Certify that a scenario's adversarial turns are conditioned on
    tool-output state, not on a fixed authored script.

    Returns the scenario's certified classification. Raises
    TurnConditioningViolation if the scenario claims multi-turn status
    but its branches produce isomorphic agent action sets.
    """
    if scenario.declared_class != "MULTI_TURN":
        return scenario.declared_class

    branches = scenario.enumerate_tool_result_branches()
    if len(branches) < 2:
        raise TurnConditioningViolation(
            f"scenario {scenario.id!r} declares MULTI_TURN but exposes "
            f"{len(branches)} tool-result branch; cannot demonstrate "
            f"output-sensitivity"
        )

    action_sets = []
    for branch in branches:
        trace = agent.replay(scenario, tool_results=branch)
        action_sets.append(trace.action_signature())

    if len(set(action_sets)) < 2:
        raise TurnConditioningViolation(
            f"scenario {scenario.id!r} declares MULTI_TURN but all "
            f"{len(branches)} tool-result branches yield isomorphic "
            f"action signatures {action_sets!r}; demote to "
            f"SINGLE_TURN_FUSED"
        )

    # At least two branches diverge: the adversarial pressure is
    # genuinely conditioned on executed state.
    return "STATE_CONDITIONED"
