fix(build): spare hyperedge members and pre-prune semantic isolates in orphan sweep (#4399)

Review follow-up on #4429:
- The merge-time sweep now skips nodes referenced by a hyperedge in
  G.graph; degree 0 does not mean unreferenced for them, and sweeping one
  left a dangling member that to_json writes verbatim.
- On the prune path a source-less non-code, non-stub node is removed only
  when the prune itself isolated it (v8's post-rebind _isolated_before), so
  semantic nodes stranded by re-extraction survive as before.

Co-Authored-By: Grok Bot <grokbot@users.noreply.github.com>
(cherry picked from commit 685f7f47bb)
This commit is contained in:
Mpasha17 authored and safishamsi committed 2026-10-13 22:53:44 +01:00
1 parent 9cbb70a41c
commit aa18a71c30
2 files changed
+53 -5

No files matched your search

+22 -5
View File
@@ -2518,22 +2518,39 @@ def build_merge(
# references/inherits edge, never as a source), so removing one can
# never drop another to degree 0. A future extractor emitting a
# stub->stub edge would require iterating this to a fixpoint.
# Hyperedge members are referenced out of band (G.graph, not G.edges),
# so degree 0 does not mean "connects nothing" for them; sweeping one
# would leave the hyperedge with a dangling member.
_he_members = {
m for he in (G.graph.get("hyperedges") or []) if isinstance(he, dict)
for m in (he.get("nodes") or []) if _hashable(m)
}
orphaned = [
n for n, d in G.nodes(data=True)
if not d.get("source_file")
and G.degree(n) == 0
and n not in _spare_isolated
# Off the prune path only machine-minted placeholders are collected
# (AST code stubs and #2873 external import stubs); a source-less
# semantic/curated node is never second-guessed by a plain merge.
and (prune_sources or d.get("file_type") == "code"
or _is_external_stub(d))
and n not in _he_members
# Only machine-minted placeholders (AST code stubs and #2873
# external import stubs) are collected when re-extraction strands
# them. A source-less semantic/curated node goes only when THIS
# prune isolated it, as before #4399 (_isolated_before is empty
# off the prune path).
and (d.get("file_type") == "code" or _is_external_stub(d)
or (prune_sources and n not in _isolated_before))
]
G.remove_nodes_from(orphaned)
return orphaned
# Prune nodes and edges from deleted source files
_isolated_before: set = set()
if prune_sources:
# Source-less nodes already isolated before this prune (post-rebind).
# Non-placeholder nodes among them are not this prune's doing.
_isolated_before = {
n for n, d in G.nodes(data=True)
if not d.get("source_file") and G.degree(n) == 0
}
to_remove = [
n for n, d in G.nodes(data=True)
if _prune_match(d.get("source_file"))
@@ -107,3 +107,34 @@ def test_a_sourceless_semantic_node_is_untouched(tmp_path):
), tmp_path)
G = build_merge([_x([_n("a", "A.cs")], [])], graph_path=gp, root=".")
assert "concept" in G and "doc_idea" in G
def _save_he(extraction, tmp_path) -> str:
gp = Path(_save(extraction, tmp_path))
data = json.loads(gp.read_text(encoding="utf-8"))
data["hyperedges"] = extraction["hyperedges"]
gp.write_text(json.dumps(data), encoding="utf-8")
return str(gp)
def test_hyperedge_member_is_not_swept_as_an_orphan(tmp_path):
"""A degree-0 placeholder still referenced by a carried hyperedge stays."""
he = {"id": "h1", "label": "grp", "nodes": ["c", "list"],
"relation": "participate_in", "source_file": "C.md"}
gp = _save_he({"nodes": [_n("a", "A.cs"), _n("c", "C.md", "document"), _n("list")],
"edges": [_e("a", "list", "A.cs")], "hyperedges": [he]}, tmp_path)
G = build_merge([_x([_n("a", "A.cs")], [])], graph_path=gp, root=".")
assert "list" in G
for h in G.graph.get("hyperedges", []):
assert all(m in G for m in h["nodes"])
def test_prune_spares_semantic_node_stranded_by_reextraction(tmp_path):
"""Under prune, a source-less non-code node goes only if the prune isolated it."""
gp = _save(_x(
[_n("a", "A.md", "document"), _n("concept", None, "document"), _n("d", "D.cs")],
[_e("a", "concept", "A.md")],
), tmp_path)
G = build_merge([_x([_n("a", "A.md", "document")], [])],
graph_path=gp, root=".", prune_sources=["D.cs"])
assert "concept" in G and "d" not in G