# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-passes-every-litmus-safety-probe-but-violates-the-invariant-under-probe-composition.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-passes-every-litmus-safety-probe-but-violates-the-invariant-under-probe-composition.warrant.json
# Markdown SHA-256:   b4156ed745158d5b71ab80f13762ce1c9d5983f7c14df0dcec108e92171e3929
# Code AST SHA-256:   60bcf21bbedcee80fad071d128d90598c96bc48d38f7bc28b0db80a808883f8a
# Verified Classes:   LitmusCompositionError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class LitmusCompositionError(RuntimeError):
    """Raised when a LITMUS gate is asserted on per-probe scores despite session reuse."""
    pass


def assert_gate_valid(harness_config: dict, invariant_scope: str) -> None:
    session_reuse = harness_config.get("reuse_session_across_probes", False)
    composition_scored = harness_config.get("composition_aware_scoring", False)
    pinned_policy = harness_config.get("session_policy_pinned_in_gate", False)

    if invariant_scope not in ("local", "trajectorial"):
        raise LitmusCompositionError(
            f"unknown invariant scope {invariant_scope!r}; cannot validate gate"
        )

    if invariant_scope == "local" and not session_reuse:
        return

    if session_reuse and not composition_scored:
        raise LitmusCompositionError(
            "Harness reuses session/memory/tool state across probes but does not "
            "score probe compositions. Per-probe pass rate is not a valid gate for "
            "a trajectorial invariant; a 100% pass can mask an unsafe composition."
        )

    if not pinned_policy:
        raise LitmusCompositionError(
            "Session-reuse policy is not pinned as part of gate identity. A policy "
            "change can alter the reachable composition set while the aggregate "
            "score vector is unchanged; the gate cannot detect the regression."
        )
