# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-that-hierarchical-compaction-has-silently-promoted-a-hallucinated-summary-node-into-the-retrieval-index-as-ground-tru.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-that-hierarchical-compaction-has-silently-promoted-a-hallucinated-summary-node-into-the-retrieval-index-as-ground-tru.warrant.json
# Markdown SHA-256:   4a6d4a39970aef17816dca48ba1e9891c083aa2805ceadbf5c2ba70bf1ddac80
# Code AST SHA-256:   c438dc77412ec79a2c3971c60f380bd1c37053b7e05314f715670ab985aefd05
# Verified Classes:   ProvenanceLaunderingError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class ProvenanceLaunderingError(RuntimeError):
    """Raised when a derived summary node would be treated as primary evidence
    or re-summarized without lineage, enabling compounding loss."""
    pass


def retrieve_for_assertion(hit, query):
    depth = hit.get("depth")
    if depth is None:
        raise ProvenanceLaunderingError(
            f"node {hit['id']} has no provenance depth; cannot classify as evidence"
        )
    if depth == 0:
        return {"claim": hit["text"], "status": "observed", "source": hit["id"]}
    if depth == 1 and hit.get("leaves_retained"):
        return {"claim": hit["text"], "status": "pointer", "expand": hit["leaf_ids"]}
    if depth >= 2:
        raise ProvenanceLaunderingError(
            f"query {query!r} resolves only to depth-{depth} node {hit['id']}; "
            "fact is reconstruction, not observation -- refuse to assert"
        )
    return {"claim": hit["text"], "status": "believed_unverified", "source": hit["id"]}


def compact(nodes):
    for n in nodes:
        if n.get("depth", 0) >= 1 and not n.get("lineage_stamp"):
            raise ProvenanceLaunderingError(
                f"refusing to summarize derived node {n['id']} without lineage stamp"
            )
    new_depth = max((n.get("depth", 0) for n in nodes), default=0) + 1
    return {"id": "S_new", "depth": new_depth, "lineage_stamp": True,
            "text": summarize(nodes)}
