# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-draw-the-compaction-floor-hierarchical-summary-triggers-that-fire-below-the-recovery-horizon.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-draw-the-compaction-floor-hierarchical-summary-triggers-that-fire-below-the-recovery-horizon.warrant.json
# Markdown SHA-256:   407cf84b832873c5cf4e4009bb0b2600b4725071e68209af02842c22cf947348
# Code AST SHA-256:   b3504b7e5dec87b2eb58297fe4f455b34bb86f38f32391339b7e108da0faf679
# Verified Classes:   UnrecoverableCompactionError
# Defensive Guards:   1 fail-closed invariants (zero external deps)

class UnrecoverableCompactionError(RuntimeError):
    """Raised when a compaction trigger would evict a verified fact that is
    neither pointered to a live source nor explicitly marked as elided."""
    pass


def compact_band(band_facts, summary, live_pointers, occupancy, budget):
    """
    band_facts: list of dicts with keys 'key', 'value', 'verified' (bool)
    summary: dict mapping fact key -> {'text': str, 'elided': bool}
    live_pointers: set of fact keys that can be re-fetched externally
    """
    for fact in band_facts:
        if not fact["verified"]:
            continue  # unverified facts may be summarized freely
        key = fact["key"]
        pointerable = key in live_pointers
        entry = summary.get(key)
        marked_elided = entry is not None and entry.get("elided", False)
        captured_lossless = (
            entry is not None
            and not entry.get("elided", False)
            and fact["value"] in entry.get("text", "")
        )

        if not (pointerable or marked_elided or captured_lossless):
            # Occupancy is over budget but the fact is below the recovery
            # horizon: halt compaction and spill to external memory instead.
            raise UnrecoverableCompactionError(
                f"Cannot compact fact {key!r}={fact['value']!r}: no live "
                f"pointer, not marked elided, not captured losslessly. "
                f"Occupancy {occupancy}/{budget}. Spill to external memory "
                f"and record a pointer before retrying compaction."
            )

    return summary
