Repository navigation
fix(dedup): keep distinct files that mint the same node id (#4281) - #4282
bitwizard25 wants to merge 2 commits into
Conversation
…Labs#4281) Two files that both derive the same id (x.py/x.html, or a bare id that is a trailing path slice of docs/x.md and docs/sub/x.md) were collapsed to one node with a warning, losing a real entity. The best-ranked file keeps the id; the others get a path- or extension-qualified id and their edges follow.
|
Thanks for the pull request, @bitwizard25. A maintainer will review it soon. Want to talk it through while it is in review? Come join us on our Discord server. For longer-form discussion there is also GitHub Discussions. A couple of things that speed up review: make sure the test suite passes on Python 3.10 and 3.13, and that the change keeps extraction deterministic. |
There was a problem hiding this comment.
Graphify reviewed this change.
Code graph out of date: Health delta baseline: last indexed commit 2cb81c6, 1 commit(s) behind this PR's base. Impact and hotspot numbers below may be wrong.
No verdict: the code graph is out of date. Static checks found no blocking issues, but they compared against an outdated graph. The next push after the repository re-indexes gets a full review.
Not checked: tests were not run; formal verification proved 0 of 1 changed function(s) (1 not verified).
Formal verification. PR-changed functions: 0/1 verified (0 proven, 0 may-equivalent, 0 distinguished) · 1 not verified (1 vacuous).
Not verified on this run: deduplicate\_entities (vacuous: never exercised).
Graphify review — findings
Fixes deduplicate_entities so two distinct files that mint the same node id (portal.py/portal.html, or a bare protocol from two protocol.md paths) each keep a node instead of one being dropped with a warning. _disambiguate_defining_collisions lets the best-ranked file keep the id and renames the others to a full-path id, or the full-path-plus-extension form if that's taken. Edges and hyperedges carrying the renamed file's source_file follow it, while unattributed references stay on the winner; ids no file derives from its own path, or with no free alternative, still collapse with the #1504 warning.
No blocking issues surfaced. 2 lower-confidence candidates did not survive cross-model review.
Analysis details — impact, health, verification
Impact & health
Graphify review
Impact — 800 functions depend on the 472 functions this change touches.
Health — this change adds coupling hotspots:
- worse:
deduplicate_entities()— 85 callers, 27 callees - worse:
_collision_rank()— 4 callers, 3 callees
Verification — 800 functions in the blast radius were not formally verified this run (proofs are advisory here).
Health delta baseline: last indexed commit 2cb81c6, 1 commit(s) behind this PR's base.
Gate & verification
graphify gate
PASS — objectively clean (no health regressions, tests not run — proofs not run this pass (advisory)). Grounded, not self-assessed.
Advisory (not blocking):
- verification_scope: 594 function(s) in the blast radius were not formally verified this run
Test selection
Test selection
361 of 361 test file(s) selected (100%) via static blast radius.
Escalated to a full run for safety — the selection is not trustworthy on its own (see below). CI should run the whole suite.
tests/test_affected_cli.py— full-run-safetytests/test_affected_member_seed.py— full-run-safetytests/test_agents_platform.py— full-run-safetytests/test_analyze.py— full-run-safetytests/test_anthropic_custom_endpoint.py— full-run-safetytests/test_antigravity_install.py— full-run-safetytests/test_apm_fallback_version.py— full-run-safetytests/test_architecture_doc.py— full-run-safetytests/test_astro_extraction.py— full-run-safetytests/test_astro_import_ids.py— full-run-safetytests/test_atomic_canvas_export.py— full-run-safetytests/test_atomic_version_stamp.py— full-run-safetytests/test_atomic_writes.py— full-run-safetytests/test_backend_env_isolation.py— full-run-safetytests/test_backend_extras.py— full-run-safetytests/test_benchmark.py— full-run-safetytests/test_benchmark_raw_graph.py— full-run-safetytests/test_blade_extractor.py— full-run-safetytests/test_build.py— impact, full-run-safetytests/test_build_located_semantic_identity.py— impact, full-run-safetytests/test_build_merge_dedup_scope.py— impact, full-run-safetytests/test_build_merge_hyperedges_and_prune.py— impact, full-run-safetytests/test_build_merge_shrink_guard.py— impact, full-run-safetytests/test_builtin_global_type_refs.py— full-run-safetytests/test_cache.py— full-run-safetytests/test_cache_stale_import_target.py— full-run-safetytests/test_callflow_html.py— full-run-safetytests/test_cargo_introspect.py— full-run-safetytests/test_cargo_missing_manifest.py— full-run-safetytests/test_carried_hyperedge_remap.py— impact, full-run-safetytests/test_case_sensitive_resolution.py— full-run-safetytests/test_charmap_encoding.py— full-run-safetytests/test_chunking.py— full-run-safetytests/test_cjs_module_extension.py— full-run-safetytests/test_claude_cli_backend.py— full-run-safetytests/test_claude_md.py— full-run-safetytests/test_cli_broken_pipe.py— full-run-safetytests/test_cli_export.py— full-run-safetytests/test_cli_help.py— full-run-safetytests/test_cloud_cta.py— full-run-safetytests/test_cluster.py— full-run-safetytests/test_cluster_ambiguous_scale.py— full-run-safetytests/test_cluster_exclude_hubs.py— full-run-safetytests/test_cobol_extractor.py— full-run-safetytests/test_codebuddy.py— full-run-safetytests/test_community_hub_labels.py— full-run-safetytests/test_community_labels_skill.py— full-run-safetytests/test_confidence.py— full-run-safetytests/test_corrupt_graph_json.py— impact, full-run-safetytests/test_cpp_method_declarations.py— full-run-safety- … and 311 more
non-code file(s) changed (
CHANGELOG.md) → running the full suite for safety (a code graph can't see config/fixture/data deps)
changed code file(s) with no mapped test (
CHANGELOG.md) — a coverage gap or a missing link — running the full suite rather than only the selected tests
Selection is safe under the controlled-regression assumption; always-run tests + a periodic full run are the backstops. Advisory — it never changes the check verdict.
Formal verification
Could not verify: Could not verify deduplicate\_entities.
The verifier did not have enough to check deduplicate\_entities, so it is saying so rather than guessing. No false assurance is the whole point.
Guarantee: No guarantee either way, this is an honest abstention, not a pass.
Note: Reason: no capturable inputs from the test suite; property tier: not verifiable: all 11 sampled inputs raised on both versions — the function never executed, so 'no divergence' would be vacuous (mostly TypeError — names the real obstacle, not a sampling gap)
· 2 more finding(s) on lines outside this diff (see the check run).
|
Thanks for picking this up so quickly. I ran an A/B test of this PR on the repository where I hit the warnings. It removes the warnings, but the restored nodes come back with no edges, so in practice little changes compared to before. Setup
Results
The only differences between baseline and PR are the two new nodes. The edge set is identical, no existing ID, label or Problem 1: edges don't follow the renamed node
In the cache, the HTML node has three The cause is a path-form mismatch. Semantic nodes and edges reach Problem 2: the doc case yields an orphan near-duplicate
Suggestion
|
… twins (Graphify-Labs#4281) Edges reach dedup with an absolute source_file while nodes are already root-relative, so the exact-path match never found them and the renamed node came back without edges. Compare root-relative keys instead. Also restrict the rename to files with the same full stem and a different extension (x.py / x.html). A bare id shared across different paths is a reference to another file's entity, so it stays on the collapse-and-warn path.
|
Thanks, One remaining edge case: The patch only relativizes absolute paths in diff --git a/graphify/dedup.py b/graphify/dedup.py
--- a/graphify/dedup.py
+++ b/graphify/dedup.py
@@ -518,6 +518,12 @@ def _full_stem(source_file: str) -> str:
return "_".join(s for s in (normalize_id(p) for p in stem.split("/")) if s)
+def _extension(source_file: str) -> str:
+ """The lowercased extension of ``source_file`` (``.html``), or ``""`` if it has none."""
+ m = _EXTENSION.search(source_file.replace("\\", "/"))
+ return m.group(0).lower() if m else ""
+
+
def _extension_qualified_id(node: dict) -> str | None:
"""``x.html``'s own spelling of an ID it shares with ``x.py`` (#4281).
@@ -554,7 +560,13 @@ def _disambiguate_defining_collisions(
collapse-and-warn path.
"""
def key(sf):
- return _rank_path(sf, root) if sf else ""
+ if not sf:
+ return ""
+ sf = sf.replace("\\", "/")
+ # Nodes are already root-relative here; only an absolute (semantic edge)
+ # path needs relativizing. Resolving a relative one would anchor it at
+ # the CWD, which differs from the root when run from a subdirectory.
+ return _rank_path(sf, root) if Path(sf).is_absolute() else sf
by_id: dict[str, list[dict]] = defaultdict(list)
for node in nodes:
@@ -576,7 +588,7 @@ def _disambiguate_defining_collisions(
continue
# The extension is what tells them apart; same-extension slug twins
# (pkg/service.py vs pkg_service.py) stay on the collapse-and-warn path.
- if _EXTENSION.search(d["source_file"]) and _EXTENSION.search(winner["source_file"]) and _EXTENSION.search(d["source_file"]).group(0).lower() == _EXTENSION.search(winner["source_file"]).group(0).lower():
+ if _extension(d["source_file"]) == _extension(winner["source_file"]):
continue
new_id = _extension_qualified_id(d)
if new_id and new_id not in taken:
@@ -600,7 +612,9 @@ def _disambiguate_defining_collisions(
item[end] = new_id
members = item.get("nodes")
if isinstance(members, list):
- item["nodes"] = [renames.get((m, k), m) if isinstance(m, str) else m for m in members]
+ item["nodes"] = [
+ renames.get((m, k), m) if isinstance(m, str) else m for m in members
+ ]
return out_nodes, edges
diff --git a/tests/test_dedup.py b/tests/test_dedup.py
--- a/tests/test_dedup.py
+++ b/tests/test_dedup.py
@@ -1622,3 +1622,33 @@ def test_build_with_absolute_edge_paths_moves_edges_to_renamed_node(tmp_path):
assert "firmware_lib_portal" in G and "firmware_lib_portal_html" in G
assert G.has_edge("firmware_lib_portal_html", "card_a")
assert not G.has_edge("firmware_lib_portal", "card_a")
+
+
+def test_build_moves_edges_to_renamed_node_when_cwd_is_inside_root(tmp_path, monkeypatch):
+ """A repo-relative node path must not be resolved against the CWD: run from a
+ subdirectory of the scan root, the renamed node still receives its edges (#4281)."""
+ from graphify.build import build
+
+ root = tmp_path
+ (root / "firmware").mkdir()
+ monkeypatch.chdir(root / "firmware")
+ py = str(root / "firmware" / "lib" / "portal.py")
+ html = str(root / "firmware" / "lib" / "portal.html")
+ ext = {
+ "nodes": [
+ {"id": "firmware_lib_portal", "label": "portal.py", "file_type": "code",
+ "source_file": py},
+ {"id": "firmware_lib_portal", "label": "Portal page", "file_type": "document",
+ "source_file": html},
+ {"id": "card_a", "label": "Card A", "file_type": "document", "source_file": html},
+ ],
+ "edges": [
+ {"source": "firmware_lib_portal", "target": "card_a", "relation": "references",
+ "confidence": "EXTRACTED", "source_file": html},
+ ],
+ }
+ G = build([ext], root=str(root))
+
+ assert "firmware_lib_portal" in G and "firmware_lib_portal_html" in G
+ assert G.has_edge("firmware_lib_portal_html", "card_a")
+ assert not G.has_edge("firmware_lib_portal", "card_a")
|
|
Landed in 0.9.85 (now on PyPI) via authorship-preserving cherry-pick, covering the extension-drop collision ( |
…phify-Labs#4281) Semantic extraction sometimes mints an id from the bare filename stem, so a/x.md and b/x.md both emit `x`. Dedup then collapsed them into one node and one file's node was lost. _semantic_id_remap in build.py knows how to rebuild the full-path id, but it runs after dedup and is keyed by id alone, so it cannot send one id to two files. Add _path_qualify_dropped_collisions next to the extension pass. When two or more files with different full path stems mint the same shorter-stem id, rename each node to its own full-path id (keyed by id and source file) and move edges by their source_file. References from other files cannot be attributed, so they are left unresolved with a warning. Boundaries: a group is skipped when the file the bare id names is in the input (a genuine reference); absolute source_files are never qualified, so checkout paths cannot leak into ids; AST-tier nodes are untouched. This reverses the collapse pinned in Graphify-Labs#1504/Graphify-Labs#4282 for path-dropped ids: the Graphify-Labs#1504 test now uses a non-shorter-stem id and the protocol test now expects both files to be path-qualified.
Semantic extraction sometimes mints an id from the bare filename stem, so a/x.md and b/x.md both emit `x`. Dedup then collapsed them into one node and one file's node was lost. _semantic_id_remap in build.py knows how to rebuild the full-path id, but it runs after dedup and is keyed by id alone, so it cannot send one id to two files. Add _path_qualify_dropped_collisions next to the extension pass. When two or more files with different full path stems mint the same shorter-stem id, rename each node to its own full-path id (keyed by id and source file) and move edges by their source_file. References from other files cannot be attributed, so they are left unresolved with a warning. Boundaries: a group is skipped when the file the bare id names is in the input (a genuine reference); absolute source_files are never qualified, so checkout paths cannot leak into ids; AST-tier nodes are untouched. This reverses the collapse pinned in #1504/#4282 for path-dropped ids: the #1504 test now uses a non-shorter-stem id and the protocol test now expects both files to be path-qualified. (cherry picked from commit 58f844e)
Fixes #4281
What does this PR do?
Two distinct files can derive the same node id (
portal.py/portal.html, or a bareprotocolfromdocs/protocol.mdanddocs/firmware/protocol.md). Dedup collapsed them to one node with a warning, losing a real entity on every rebuild. Now each file keeps its own node: the best-ranked file keeps the id, the others get a path- or extension-qualified id (firmware_lib_portal_html), and their edges follow. Ids no file derives from its own path still collapse with the existing warning.Type of change
How was this tested?