# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-semantic-saturation-in-episodic-to-semantic-distillation.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-semantic-saturation-in-episodic-to-semantic-distillation.warrant.json
# Markdown SHA-256:   ed27322dbe7167990fbc98b396247535e632ca6ea5aa3a289923c50a343da691
# Code AST SHA-256:   b30542ad7c2bf0c82f253aff2a8485ee08603d50ddb2710bc68c3302873a8e4a
# Verified Classes:   SemanticSaturationError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class SemanticSaturationError(RuntimeError):
    """Raised when a consolidation cycle is eligible but no longer novel."""
    pass


def check_consolidation_health(
    aggregate_eligible_mass: float,
    novel_eligible_mass: float,
    consecutive_cycles: int,
    aggregate_floor: float = 0.4,
    novelty_floor: float = 0.05,
    required_cycles: int = 3,
) -> bool:
    if not (0.0 <= aggregate_eligible_mass <= 1.0):
        raise ValueError("aggregate_eligible_mass must be a fraction in [0,1]")
    if not (0.0 <= novel_eligible_mass <= aggregate_eligible_mass):
        raise ValueError("novel mass cannot exceed aggregate mass")

    saturated = (
        aggregate_eligible_mass > aggregate_floor
        and novel_eligible_mass < novelty_floor
        and consecutive_cycles >= required_cycles
    )
    if saturated:
        raise SemanticSaturationError(
            f"semantic saturation: aggregate={aggregate_eligible_mass:.3f} "
            f"> {aggregate_floor}, novel={novel_eligible_mass:.3f} "
            f"< {novelty_floor} for {consecutive_cycles} cycles; "
            "widen pairing window or apply novelty-conditioned importance floor"
        )
    return True
