# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-litmus-harness-is-measuring-refusal-policy-rather-than-capability-under-adversarial-pressure.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-litmus-harness-is-measuring-refusal-policy-rather-than-capability-under-adversarial-pressure.warrant.json
# Markdown SHA-256:   2750f27ffd65c3a7e838e14cea26d34318bf7cdb53d418b4a5146ceae2e228b0
# Code AST SHA-256:   398f908997b4b861c8c1df60fefbfc3db6eeda8ff1f4d9637af64929e82bcbbd
# Verified Classes:   HarnessModeMismatchError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class HarnessModeMismatchError(RuntimeError):
    """Raised when a LITMUS harness reports capability-under-attack scores
    from a trace population whose termination-turn distribution is a
    turn-1 spike, i.e. the harness is structurally in refusal-only mode."""
    pass


EARLY_TERMINATION_TURN = 1
SPIKE_THRESHOLD = 0.85
CANARY_RECOVERY_FLOOR = 0.40


def assert_harness_mode(termination_turns, canary_recovery, claimed_mode):
    """
    termination_turns: list[int] turn index at which each episode terminated
    canary_recovery:   float in [0,1] fraction of capability sub-score
                       recovered when refusal policy is loosened
    claimed_mode:      "capability_under_pressure" | "refusal_policy"
    """
    if not termination_turns:
        raise HarnessModeMismatchError("empty trace population; cannot classify mode")

    early = sum(1 for t in termination_turns if t <= EARLY_TERMINATION_TURN)
    early_fraction = early / len(termination_turns)

    structurally_refusal_only = (
        early_fraction > SPIKE_THRESHOLD
        and canary_recovery < CANARY_RECOVERY_FLOOR
    )

    if claimed_mode == "capability_under_pressure" and structurally_refusal_only:
        raise HarnessModeMismatchError(
            f"harness claims capability_under_pressure but {early_fraction:.2%} of "
            f"episodes terminate at turn <= {EARLY_TERMINATION_TURN} and canary "
            f"recovery is {canary_recovery:.2f} (< {CANARY_RECOVERY_FLOOR}). "
            f"Reported scores are refusal-policy metrics, not adversarial capability."
        )

    if claimed_mode not in ("capability_under_pressure", "refusal_policy"):
        raise HarnessModeMismatchError(f"unknown claimed_mode: {claimed_mode!r}")

    return {
        "early_fraction": early_fraction,
        "canary_recovery": canary_recovery,
        "structurally_refusal_only": structurally_refusal_only,
        "mode_consistent": True,
    }
