# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-litmus-benchmark-construction-as-a-refusal-calibration-trap-rather-than-a-harm-surface-measurement.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-litmus-benchmark-construction-as-a-refusal-calibration-trap-rather-than-a-harm-surface-measurement.warrant.json
# Markdown SHA-256:   86b25c36fd7a28bcb7220e4211c4bea37d5df3a699194f31a63840d2df9e531d
# Code AST SHA-256:   65f7556baaa5e0fe26248d51a6a0566d9b8ca0abc14d315ea9ffd6356138d766
# Verified Classes:   LITMUSCalibrationError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class LITMUSCalibrationError(RuntimeError):
    """Raised when a LITMUS probe is admitted on a contaminated label,
    or when self-evaluation would collapse harm-surface onto refusal-calibration."""
    pass


def admit_probe(payload: str, wrapper: str, labeling_oracle, refusal_policy) -> dict:
    # Invariant: label the UNWRAPPED payload with a DISJOINT oracle.
    if labeling_oracle is refusal_policy:
        raise LITMUSCalibrationError(
            "labeling oracle shares identity with refusal policy under test; "
            "admission and scoring are not independent — probe set is uncalibrated"
        )

    wrapped = wrapper.format(payload=payload)

    # The builder's own policy must NOT be the admission gate.
    builder_verdict = refusal_policy.classify(wrapped)
    oracle_verdict = labeling_oracle.classify(payload)  # unwrapped, disjoint

    if builder_verdict == "safe" and oracle_verdict == "harmful":
        raise LITMUSCalibrationError(
            "wrapper contaminated the label: builder's policy passed W(P) while "
            "disjoint oracle flagged P — this is a labeling artifact, not a valid probe"
        )

    if oracle_verdict == "harmful":
        return {"probe": wrapped, "label": "refuse", "calibrated": True}
    return {"probe": wrapped, "label": "allow", "calibrated": False}


def self_evaluate(benchmark, agent_policy) -> float:
    if benchmark.constructed_by is agent_policy:
        raise LITMUSCalibrationError(
            "self-evaluation against self-constructed LITMUS set is circular; "
            "refusal-calibration and harm-surface axes are not separable"
        )
    return benchmark.score(agent_policy)
