# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-hierarchical-context-compaction-the-fidelity-budget-exhaustion-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-hierarchical-context-compaction-the-fidelity-budget-exhaustion-boundary.warrant.json
# Markdown SHA-256:   f33b5e962ea56d4959fc39fcbe45a6f607ac0507c95cbac0ea275bf0c714bb2a
# Code AST SHA-256:   04132a910bc008cd0409c6f441a76f0153af7de9446e9c9e45e9cb030dcf85cd
# Verified Classes:   FidelityBudgetExhausted
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class FidelityBudgetExhausted(RuntimeError):
    """Raised when committing a lossy child summary would leave the parent
    unable to meet its declared fact-coverage floor after siblings arrive."""
    pass


FACT_CLASS_MIN_WEIGHT = {
    "numeric_literal": 3.0,
    "negation": 2.0,
    "identifier": 2.0,
    "ordering": 1.5,
    "prose": 1.0,
}


def commit_child_summary(
    parent_budget: float,
    accepted_fact_weight: float,
    child_facts: dict,
    fidelity_known: bool,
    originals_retained: bool,
) -> float:
    if originals_retained:
        # Summary is a cache; loss is recoverable, budget is advisory.
        return accepted_fact_weight + sum(
            FACT_CLASS_MIN_WEIGHT.get(k, 1.0) * v for k, v in child_facts.items()
        )

    if not fidelity_known:
        raise FidelityBudgetExhausted(
            "summarizer cannot certify fact-class preservation; "
            "treating fidelity_unknown as refusal, not pass"
        )

    child_weight = sum(
        FACT_CLASS_MIN_WEIGHT.get(k, 1.0) * v for k, v in child_facts.items()
    )
    residual = parent_budget - accepted_fact_weight
    if residual < child_weight:
        raise FidelityBudgetExhausted(
            f"residual={residual:.1f} < required={child_weight:.1f}; "
            f"committing would drop decision-relevant facts irreversibly; "
            f"escalate to verbatim retention"
        )
    return accepted_fact_weight + child_weight
