# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-distinguish-compaction-lag-from-compaction-loss-when-a-lossy-summarizer-sits-between-the-raw-transcript-and-the-working-set.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-distinguish-compaction-lag-from-compaction-loss-when-a-lossy-summarizer-sits-between-the-raw-transcript-and-the-working-set.warrant.json
# Markdown SHA-256:   39f9bc4c5758919e29bbc5809a38bf3738fe90d2988a469910dde2a22f12b383
# Code AST SHA-256:   78811c4b5edf132d09f254935add6ce9b1e7af05033fc0796c14eff00ff3e4db
# Verified Classes:   CompactionLossError, CompactionLagError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class CompactionLossError(RuntimeError):
    """Raised when a required fact's span was compacted and its raw form evicted."""
    pass


class CompactionLagError(RuntimeError):
    """Raised when a required fact's span is unsummarized but still recoverable."""
    pass


def resolve_missing_fact(fact_span, watermark, raw_store, retention_horizon, now):
    compacted = watermark.covers(fact_span)
    retained = raw_store.has(fact_span) and (now - fact_span.end) < retention_horizon

    if not compacted:
        # Not yet folded in: lag, cheap to recover.
        raise CompactionLagError(
            f"span {fact_span} unsummarized; force compaction pass or read raw directly"
        )

    if retained:
        # Compacted but raw still available: suspect loss, re-derive before trusting summary.
        return raw_store.read(fact_span)

    # Compacted and raw evicted: confirmed loss. Do not reconstruct from the summary.
    raise CompactionLossError(
        f"span {fact_span} compacted at epoch {watermark.epoch_for(fact_span)} "
        f"and raw evicted at horizon {retention_horizon}; working set is not "
        f"authoritative for decisions depending on this span"
    )
