# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-that-hierarchical-context-compaction-has-silently-promoted-a-lossy-summary-into-the-authoritative-state-record.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-that-hierarchical-context-compaction-has-silently-promoted-a-lossy-summary-into-the-authoritative-state-record.warrant.json
# Markdown SHA-256:   07f46eeca099217732f929f82ccb1309b974df9b9e011b8fb830f04f0288a6de
# Code AST SHA-256:   1fb47103b6ff4e6e4c4d337ad33db119572b9c04f637c46d9b17bff494824717
# Verified Classes:   CompactionAuthorityError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class CompactionAuthorityError(RuntimeError):
    """Raised when a summary node is read as authoritative state while its
    backing sources are evicted but the node was never marked terminal."""
    pass


def read_compacted_node(node, requested_claim_type):
    """
    node: dict with keys:
      summary, source_refs (list of {id, live: bool}), terminal (bool),
      summarizer_confidence (float), depth (int)
    requested_claim_type: 'qualitative' | 'quantitative'
    """
    live_refs = [r for r in node["source_refs"] if r["live"]]
    evicted_refs = [r for r in node["source_refs"] if not r["live"]]

    # Invariant: a node with any evicted refs and no terminal flag has silently
    # been promoted from lossy-preview to authoritative-record.
    if evicted_refs and not node["terminal"]:
        raise CompactionAuthorityError(
            f"Node at depth {node['depth']} has {len(evicted_refs)} evicted "
            f"source_refs but terminal=False. Summary has been silently "
            f"promoted to authoritative state; refusing to read as ground truth."
        )

    if node["terminal"]:
        # Authoritative but lossy: cap confidence at the weakest surviving
        # evidence, not the summarizer's fresh scalar.
        effective_confidence = min(
            [node["summarizer_confidence"]]
            + [r.get("confidence", 0.0) for r in evicted_refs]
        )
        if requested_claim_type == "quantitative" and effective_confidence < 0.8:
            raise CompactionAuthorityError(
                "Quantitative claim requested from terminal (irrecoverable) "
                f"node with capped confidence {effective_confidence:.2f}. "
                "Requires independent corroboration before use."
            )
        return {
            "summary": node["summary"],
            "authoritative": True,
            "lossy": True,
            "effective_confidence": effective_confidence,
        }

    # All refs live: summary is a recoverable preview.
    return {
        "summary": node["summary"],
        "authoritative": False,
        "lossy": True,
        "effective_confidence": node["summarizer_confidence"],
        "recoverable_via": [r["id"] for r in live_refs],
    }
