fix(dedup): keep an AST definer's id when another file's node also defines it (#4859)

When a doc's own path prefixes a code entity's id (pkg.md beside
pkg/test_x.py, id pkg_test_x), both colliding nodes pass the definer key
of _collision_rank and the survivor fell to label length. A terse doc
label took the id, the node carried pkg.md as its source_file, and the
next re-extract of pkg.md deleted it although the code file never
changed.

Rank a non-AST definer right behind the other definers when an AST node
from a file with a different full stem also defines the id and the
non-AST node's own file has no AST definer. The rule applies to any such
non-AST definer, not only docs. The pairs are collected once per dedup
call (_outranked_by_ast), so the rank stays a total order independent of
arrival (#1851). The check is strict on _origin == "ast", like the
ghost pass in build.py, because fresh semantic chunks arrive unstamped.

Unchanged: collisions with at most one definer; a file's AST node and
its same-file semantic enrichment (#2091), even when another file also
has an AST definer; and same-stem twins (x.py/x.html, or slug twins such
as pkg/service.py and pkg_service.py), where an AST twin never outranks
the other. A twin can still be outranked by an AST node from a third
file with a different stem, like any other definer.

Fixes #4859

Co-Authored-By: Claude <noreply@anthropic.com>
(cherry picked from commit da897c27be)
This commit is contained in:
Tanmay Shah authored and safishamsi committed 2026-10-30 22:42:36 +00:00
1 parent fc2c354919
commit e677fb8093
2 files changed
+233 -6

No files matched your search

+53 -6
View File
@@ -446,12 +446,14 @@ def _rank_path(source_file: str, root: Path | None) -> str:
def _collision_rank(node: dict, root: Path | None = None,
mentions: "frozenset | set" = frozenset()) -> tuple:
mentions: "frozenset | set" = frozenset(),
outranked: "frozenset | set" = frozenset()) -> tuple:
"""A total order for choosing the survivor of an ID collision, independent of
the order the colliding nodes arrive in.
The winner is the node with the SMALLEST rank. A node whose ``source_file``
defines the ID always outranks a mere reference; among equally-(non-)defining
defines the ID always outranks a mere reference, and a definer listed in
``outranked`` ranks behind the other definers (#4859); among equally-(non-)defining
nodes an active/in-progress path outranks an archived/done one (#2532); then
it prefers the shorter, more canonical label over a longer qualified variant,
then breaks any remaining tie lexically on label and finally on the REVERSED
@@ -470,6 +472,14 @@ def _collision_rank(node: dict, root: Path | None = None,
level up (#3775/#3786). It changes no collision between two sourced
nodes, since both sides of that key agree there.
``outranked`` (see ``_outranked_by_ast``) holds the ``(id, file)`` of non-AST
definers that another file's AST node also defines. A doc whose own path
prefixes a code entity's id (``pkg.md`` beside ``pkg/test_x.py``) defines it
too, and used to take it on a shorter label, so its next re-extract deleted the
code node (#4859). Such a definer now ranks right behind the other definers;
same-file pairs (#2091), same-stem twins, and collisions with one definer
rank as before.
``mentions`` (see ``_mentions``) holds the ``(id, file)`` of nodes read as a
mention of a document rather than the document itself. Such a node ranks as a
reference even though its id is a path slice, so a real document outranks it
@@ -481,8 +491,10 @@ def _collision_rank(node: dict, root: Path | None = None,
"""
label = node.get("label") or ""
rank_path = _rank_path(node.get("source_file") or "", root)
key = (node.get("id"), rank_path)
return (
not _defines_id(node) or (node.get("id"), rank_path) in mentions,
not _defines_id(node) or key in mentions,
key in outranked and node.get("_origin") != "ast", # (#4859)
not node.get("source_file"), # a located node beats a source-less one (#4492)
_lifecycle_penalty(rank_path), # active paths beat archived ones (#2532)
len(label), # shorter, more canonical label first
@@ -491,6 +503,40 @@ def _collision_rank(node: dict, root: Path | None = None,
)
def _outranked_by_ast(nodes: list[dict], root: Path | None,
mentions: "frozenset | set") -> set[tuple[str, str]]:
"""``(id, root-relative file)`` of non-AST definers that an AST node from
another file also defines, while their own file has no AST definer (#4859).
The AST node is extracted fact and the other is an inference about it, so the
AST node keeps the id. The check is strict on ``_origin == "ast"``, as in the
build ghost pass: fresh semantic chunks arrive unstamped. A file that has its
own AST definer (#2091 enrichment) is never listed, and an AST node with the
same full stem (an ``x.py``/``x.html`` twin, or a slug twin such as
``pkg/service.py`` and ``pkg_service.py``) never counts against it.
"""
ast_defs: dict[str, list[tuple[str, str]]] = defaultdict(list)
others: list[tuple[str, str, str]] = []
for node in nodes:
nid = node.get("id") or ""
source_file = node.get("source_file") or ""
rank_path = _rank_path(source_file, root)
if not nid or not _defines_id(node) or (nid, rank_path) in mentions:
continue
entry = (rank_path, _full_stem(source_file))
if node.get("_origin") == "ast":
ast_defs[nid].append(entry)
else:
others.append((nid, *entry))
outranked = set()
for nid, rank_path, stem in others:
defs = ast_defs.get(nid, [])
if (defs and rank_path not in {rp for rp, _ in defs}
and any(ast_stem != stem for _, ast_stem in defs)):
outranked.add((nid, rank_path))
return outranked
def _mentions(nodes: list[dict], edges: list[dict],
root: Path | None) -> set[tuple[str, str]]:
"""``(id, root-relative file)`` of nodes read as a MENTION of a document (#4281).
@@ -1104,6 +1150,7 @@ def deduplicate_entities(
nodes, edges, hyperedges, root_resolved,
)
mentions = _mentions(nodes, edges, root_resolved) | _slice_references(nodes, root_resolved)
outranked = _outranked_by_ast(nodes, root_resolved, mentions)
seen_ids: dict[str, dict] = {}
dropped: dict[str, list[dict]] = defaultdict(list)
for node in nodes:
@@ -1113,8 +1160,8 @@ def deduplicate_entities(
incumbent = seen_ids.get(nid)
if incumbent is None:
seen_ids[nid] = node
elif (_collision_rank(node, root_resolved, mentions)
< _collision_rank(incumbent, root_resolved, mentions)):
elif (_collision_rank(node, root_resolved, mentions, outranked)
< _collision_rank(incumbent, root_resolved, mentions, outranked)):
# Smallest-ranked node wins; the min over a total order is independent
# of the order nodes arrive in, so the survivor no longer depends on
# chunk ordering (#1851).
@@ -1132,7 +1179,7 @@ def deduplicate_entities(
survivor = seen_ids[nid]
same_source = sorted(
(l for l in losers if _same_source_entity(survivor, l)),
key=lambda l: _collision_rank(l, root_resolved, mentions),
key=lambda l: _collision_rank(l, root_resolved, mentions, outranked),
)
for loser in same_source:
survivor = _merge_missing_attributes(survivor, loser)
+180
View File
@@ -890,6 +890,186 @@ def test_sourced_node_survives_a_source_less_namesake_order_independent():
assert out[0] == sourced
# #4859: pkg.md sits beside pkg/test_x.py. The AST id for the file is
# `pkg_test_x`, and `pkg_` is also a slice of pkg.md's own path, so a doc node
# reusing that id defines it too and both pass the first `_collision_rank` key.
_AST_FILE = {"id": "pkg_test_x", "label": "test_x.py", "file_type": "code",
"source_file": "pkg/test_x.py", "source_location": "L1",
"_origin": "ast"}
# Fresh semantic chunks arrive without `_origin`; a loaded graph backfills it.
_DOC_COPY = {"id": "pkg_test_x", "label": "Tests", "file_type": "code",
"source_file": "pkg.md"}
@pytest.mark.parametrize("origin", [None, "semantic"], ids=["unstamped", "stamped"])
@pytest.mark.parametrize("doc_label", ["Tests", "test_x.py (tests)"])
def test_ast_definer_beats_semantic_definer_order_independent(doc_label, origin):
"""#4859: when both colliding nodes define the id, the AST node keeps it.
A doc label shorter than the code label used to win on label length; the
longer label is the control that already passed."""
import itertools
doc = dict(_DOC_COPY, label=doc_label)
if origin:
doc["_origin"] = origin
assert _defines_id(_AST_FILE) and _defines_id(doc)
for perm in itertools.permutations([_AST_FILE, doc]):
out, _ = deduplicate_entities([dict(n) for n in perm], [], communities={})
assert len(out) == 1
assert out[0]["source_file"] == "pkg/test_x.py"
assert out[0]["label"] == "test_x.py"
def test_semantic_definer_still_beats_ast_reference():
"""#4859 boundary: the AST preference only separates two definers. A semantic
node that defines the id still outranks an AST node that merely references it."""
definer = {"id": "pkg_overview", "label": "Overview", "file_type": "document",
"source_file": "pkg.md", "_origin": "semantic"}
reference = {"id": "pkg_overview", "label": "overview", "file_type": "code",
"source_file": "other/mod.py", "source_location": "L4",
"_origin": "ast"}
import itertools
for perm in itertools.permutations([definer, reference]):
out, _ = deduplicate_entities([dict(n) for n in perm], [], communities={})
assert len(out) == 1
assert out[0]["source_file"] == "pkg.md"
def test_doc_reextract_keeps_ast_node_with_shared_id(tmp_path):
"""#4859 end to end: an --update that re-extracts only pkg.md, whose node reuses
the AST id of pkg/test_x.py, must not take the code node over, and a later
re-extract of pkg.md without that node must not delete it."""
import json
from networkx.readwrite import json_graph
from graphify.build import build_merge
other = {"id": "pkg_test_x_run", "label": "run()", "file_type": "code",
"source_file": "pkg/test_x.py", "source_location": "L3",
"_origin": "ast"}
graph = {"directed": False, "multigraph": False, "graph": {},
"hyperedges": [], "nodes": [dict(_AST_FILE), other],
"links": [{"source": "pkg_test_x", "target": "pkg_test_x_run",
"relation": "contains", "confidence": "EXTRACTED",
"source_file": "pkg/test_x.py", "_origin": "ast"}]}
(tmp_path / "graph.json").write_text(json.dumps(graph))
overview = {"id": "pkg_overview", "label": "Overview", "file_type": "document",
"source_file": "pkg.md"}
doc = {"nodes": [dict(_DOC_COPY), dict(overview)],
"edges": [{"source": "pkg_overview", "target": "pkg_test_x",
"relation": "describes", "confidence": "EXTRACTED",
"source_file": "pkg.md"}],
"hyperedges": []}
G = build_merge([doc], graph_path=tmp_path / "graph.json")
assert G.nodes["pkg_test_x"]["source_file"] == "pkg/test_x.py"
assert G.has_edge("pkg_overview", "pkg_test_x")
(tmp_path / "graph2.json").write_text(
json.dumps(json_graph.node_link_data(G, edges="links")))
doc2 = {"nodes": [dict(overview)], "edges": [], "hyperedges": []}
G2 = build_merge([doc2], graph_path=tmp_path / "graph2.json")
assert "pkg_test_x" in G2
assert G2.nodes["pkg_test_x"]["source_file"] == "pkg/test_x.py"
def test_extension_twin_ids_ignore_the_ast_preference():
"""#4859 boundary: x.py and x.html are distinct entities, so which twin keeps
the bare id must not change because one is AST. A shorter html label keeps it,
as before."""
import itertools
py = {"id": "firmware_lib_portal", "label": "portal.py helper module",
"file_type": "code", "source_file": "firmware/lib/portal.py",
"source_location": "L1", "_origin": "ast"}
html = {"id": "firmware_lib_portal", "label": "portal", "file_type": "document",
"source_file": "firmware/lib/portal.html"}
for perm in itertools.permutations([py, html]):
out, _ = deduplicate_entities([dict(n) for n in perm], [], communities={})
ids = {n["source_file"]: n["id"] for n in out}
assert ids == {"firmware/lib/portal.html": "firmware_lib_portal",
"firmware/lib/portal.py": "firmware_lib_portal_py"}
def test_same_file_ast_and_semantic_definers_rank_as_before():
"""#4859 boundary: the AST preference is only for ids defined from two or more
files. An AST node and its semantic enrichment from the same file (#2091) keep
the shorter label as before."""
import itertools
ast = {"id": "m_run", "label": "run()", "file_type": "code",
"source_file": "m.py", "source_location": "L3", "_origin": "ast"}
sem = {"id": "m_run", "label": "run", "file_type": "code", "source_file": "m.py",
"summary": "Runs the job."}
for perm in itertools.permutations([ast, sem]):
out, _ = deduplicate_entities([dict(n) for n in perm], [], communities={})
assert len(out) == 1
assert out[0]["label"] == "run"
def test_cross_file_doc_loses_but_same_file_pair_keeps_its_label():
"""#4859: a doc from another file loses to the AST node (it won on v8 with an
equally short label), while the AST node and its same-file semantic
enrichment (#2091) still rank on label length between themselves."""
import itertools
ast = {"id": "m_run", "label": "run()", "file_type": "code",
"source_file": "m.py", "source_location": "L3", "_origin": "ast"}
sem = {"id": "m_run", "label": "run", "file_type": "code", "source_file": "m.py"}
doc = {"id": "m_run", "label": "Run", "file_type": "document",
"source_file": "docs/m.md"}
for perm in itertools.permutations([ast, sem, doc]):
out, _ = deduplicate_entities([dict(n) for n in perm], [], communities={})
assert len(out) == 1
assert out[0]["source_file"] == "m.py"
assert out[0]["label"] == "run"
def test_unsplit_extension_twins_rank_as_before():
"""#4859 boundary: when x.py cannot take its extension-qualified id because
another node already holds it, the twins collapse onto one id, and the
survivor is chosen exactly as on v8 (here the shorter html label)."""
import itertools
py = {"id": "firmware_lib_portal", "label": "portal.py helper module",
"file_type": "code", "source_file": "firmware/lib/portal.py",
"source_location": "L1", "_origin": "ast"}
html = {"id": "firmware_lib_portal", "label": "portal", "file_type": "document",
"source_file": "firmware/lib/portal.html"}
taken = {"id": "firmware_lib_portal_py", "label": "portal_py notes",
"file_type": "document", "source_file": "notes.md"}
for perm in itertools.permutations([py, html, taken]):
out, _ = deduplicate_entities([dict(n) for n in perm], [], communities={})
bare = [n for n in out if n["id"] == "firmware_lib_portal"]
assert len(bare) == 1
assert bare[0]["source_file"] == "firmware/lib/portal.html"
def test_slug_twin_semantic_definer_ranks_as_before():
"""#4859 boundary: pkg/service.py and pkg_service.py share a full stem, so an
AST node from one does not outrank a semantic definer from the other."""
import itertools
ast = {"id": "pkg_service_run", "label": "run()", "file_type": "code",
"source_file": "pkg/service.py", "source_location": "L2", "_origin": "ast"}
sem = {"id": "pkg_service_run", "label": "run", "file_type": "code",
"source_file": "pkg_service.py"}
for perm in itertools.permutations([ast, sem]):
out, _ = deduplicate_entities([dict(n) for n in perm], [], communities={})
assert len(out) == 1
assert out[0]["source_file"] == "pkg_service.py"
def test_semantic_enrichment_is_not_outranked_by_another_files_ast():
"""#4859 boundary: a file with its own AST definer keeps its #2091 pairing even
when a slug twin from another file also has an AST definer for the id."""
import itertools
a = {"id": "pkg_service_run", "label": "run()", "file_type": "code",
"source_file": "pkg/service.py", "source_location": "L2", "_origin": "ast"}
b = {"id": "pkg_service_run", "label": "run()", "file_type": "code",
"source_file": "pkg_service.py", "source_location": "L2", "_origin": "ast"}
sem = {"id": "pkg_service_run", "label": "run", "file_type": "code",
"source_file": "pkg/service.py"}
survivors = set()
for perm in itertools.permutations([a, b, sem]):
out, _ = deduplicate_entities([dict(n) for n in perm], [], communities={})
assert len(out) == 1
survivors.add((out[0]["source_file"], out[0]["label"]))
assert survivors == {("pkg/service.py", "run")}
def test_checkout_dir_lifecycle_name_does_not_mark_paths(tmp_path):
"""Gap in #2540: markers must be matched on ROOT-RELATIVE segments only. An
absolute source_file stored under a checkout directory named `wip` must not