# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-harm-taxonomy-drift-when-a-litmus-harness-re-elicits-capability-across-a-mutated-harm-rubric.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-harm-taxonomy-drift-when-a-litmus-harness-re-elicits-capability-across-a-mutated-harm-rubric.warrant.json
# Markdown SHA-256:   25fd528803045762d3d3bc99a23270469b26c0f48a884295ca0a47aeaf33c593
# Code AST SHA-256:   5b885fe3e54f0fe64b476060022624f62f8be46249fb2ee0ba17a6f6339d4797
# Verified Classes:   RubricDriftError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class RubricDriftError(RuntimeError):
    """Raised when a rubric mutation is non-conservative on the calibration set,
    making a score delta uninterpretable as a behavioral signal."""
    pass


def attribute_score_delta(
    frozen_trajectory,
    rubric_v1,
    rubric_v2,
    calibration_set,
    grader,
):
    # Re-score the SAME frozen trajectory under both rubrics, grader context
    # restricted to category labels/definitions (no rubric prose, no changelog).
    score_v1 = grader.score(frozen_trajectory, rubric_v1, labels_only=True)
    score_v2 = grader.score(frozen_trajectory, rubric_v2, labels_only=True)

    # Check projection stability on the calibration set.
    unstable = []
    for traj, adjudicated_hits in calibration_set:
        hits_v1 = grader.hits(traj, rubric_v1, labels_only=True)
        hits_v2 = grader.hits(traj, rubric_v2, labels_only=True)
        mapped_v1 = rubric_v2.project_from(rubric_v1, hits_v1)
        if mapped_v1 != hits_v2:
            unstable.append(traj.id)

    if unstable:
        raise RubricDriftError(
            f"Rubric mutation non-conservative on {len(unstable)} calibration "
            f"trajectories (e.g. {unstable[:3]}); score delta withheld. "
            f"v1={score_v1} v2={score_v2} is a projection artifact, not behavior."
        )

    if score_v1 != score_v2:
        # Same frozen trajectory, same labels-only grader, different score:
        # the taxonomy structure itself changed the projection.
        raise RubricDriftError(
            f"Frozen-trajectory rescore diverged (v1={score_v1}, v2={score_v2}) "
            f"despite stable calibration labels; investigate weight mapping."
        )

    # Delta is now attributable to the model, not the rubric.
    return grader.score(frozen_trajectory, rubric_v2, labels_only=False) - score_v1
