@@ -444,11 +444,13 @@ def test_dedup_still_merges_crossfile_true_duplicates():
444444def test_cross_chunk_id_collision_emits_warning (capsys ):
445445 """When two nodes share the same ID but come from different source files
446446 (a cross-chunk LLM ID collision), a WARNING must be printed to stderr
447- and only the first node survives (#1504)."""
447+ and only the first node survives (#1504). The id is not a shorter-stem mint
448+ of either path, so this covers a collision the path-qualify pass cannot
449+ attribute (#4281)."""
448450 nodes = [
449- {"id" : "readme_booking_service " , "label" : "Booking Service" ,
451+ {"id" : "booking_service " , "label" : "Booking Service" ,
450452 "file_type" : "concept" , "source_file" : "module-a/README.md" },
451- {"id" : "readme_booking_service " , "label" : "Booking Service" ,
453+ {"id" : "booking_service " , "label" : "Booking Service" ,
452454 "file_type" : "concept" , "source_file" : "module-b/README.md" },
453455 ]
454456 result_nodes , _ = deduplicate_entities (nodes , [], communities = {})
@@ -458,7 +460,7 @@ def test_cross_chunk_id_collision_emits_warning(capsys):
458460
459461 captured = capsys .readouterr ()
460462 assert "WARNING" in captured .err
461- assert "readme_booking_service " in captured .err
463+ assert "booking_service " in captured .err
462464 assert "module-b/README.md" in captured .err
463465 assert "module-a/README.md" in captured .err
464466
@@ -1582,17 +1584,34 @@ def test_extension_rename_is_order_independent():
15821584 assert {(n ["source_file" ], n ["id" ]) for n in n1 } == {(n ["source_file" ], n ["id" ]) for n in n2 }
15831585
15841586
1585- def test_bare_id_across_different_paths_still_collapses_with_warning (capsys ):
1586- """A bare `protocol` shared by docs/protocol.md and docs/firmware/protocol.md is
1587- a reference to another file's entity, not a second entity: it is not renamed."""
1587+ def test_bare_id_across_different_paths_is_path_qualified (capsys ):
1588+ """A bare `protocol` shared by docs/protocol.md and docs/firmware/protocol.md,
1589+ with no protocol.md anywhere: neither file is named by the bare id, so both are
1590+ definers and each gets its own path-qualified id (#4281 remaining case)."""
15881591 nodes = [
15891592 {"id" : "protocol" , "label" : "protocol" , "file_type" : "document" ,
15901593 "source_file" : "docs/protocol.md" },
15911594 {"id" : "protocol" , "label" : "protocol (top-level)" , "file_type" : "document" ,
15921595 "source_file" : "docs/firmware/protocol.md" },
15931596 ]
15941597 result_nodes , _ = deduplicate_entities (nodes , [], communities = {})
1595- assert len (result_nodes ) == 1
1598+ assert {n ["id" ] for n in result_nodes } == {"docs_protocol" , "docs_firmware_protocol" }
1599+ assert "minted by two different files" not in capsys .readouterr ().err
1600+
1601+
1602+ def test_bare_id_naming_an_existing_file_is_not_renamed (capsys ):
1603+ """`protocol.md` exists, so a bare `protocol` may be a genuine reference to it:
1604+ the group is not path-qualified and takes the definer-wins collapse path."""
1605+ nodes = [
1606+ {"id" : "protocol" , "label" : "protocol" , "file_type" : "document" ,
1607+ "source_file" : "protocol.md" },
1608+ {"id" : "protocol" , "label" : "protocol (docs)" , "file_type" : "document" ,
1609+ "source_file" : "docs/protocol.md" },
1610+ {"id" : "protocol" , "label" : "protocol (firmware)" , "file_type" : "document" ,
1611+ "source_file" : "docs/firmware/protocol.md" },
1612+ ]
1613+ result_nodes , _ = deduplicate_entities (nodes , [], communities = {})
1614+ assert [(n ["id" ], n ["source_file" ]) for n in result_nodes ] == [("protocol" , "protocol.md" )]
15961615 assert "WARNING" in capsys .readouterr ().err
15971616
15981617
@@ -1693,3 +1712,179 @@ def test_build_from_subdirectory_moves_edges_to_renamed_node(tmp_path, monkeypat
16931712 assert "firmware_lib_portal" in G and "firmware_lib_portal_html" in G
16941713 assert G .has_edge ("firmware_lib_portal_html" , "card_a" )
16951714 assert not G .has_edge ("firmware_lib_portal" , "card_a" )
1715+
1716+
1717+ # ── #4281: path-dropped doc ids (a/x.md vs b/x.md both mint `x`) ──────────────
1718+
1719+ def _path_dropped_input ():
1720+ """Two docs whose semantic ids dropped the directory: both mint `x`/`x_intro`."""
1721+ nodes = [
1722+ {"id" : "x" , "label" : "X A" , "file_type" : "document" , "source_file" : "a/x.md" },
1723+ {"id" : "x_intro" , "label" : "Intro A" , "file_type" : "document" ,
1724+ "source_file" : "a/x.md" },
1725+ {"id" : "x" , "label" : "X B" , "file_type" : "document" , "source_file" : "b/x.md" },
1726+ {"id" : "x_intro" , "label" : "Intro B" , "file_type" : "document" ,
1727+ "source_file" : "b/x.md" },
1728+ ]
1729+ edges = [
1730+ {"source" : "x" , "target" : "x_intro" , "relation" : "references" ,
1731+ "confidence" : "EXTRACTED" , "source_file" : "a/x.md" },
1732+ {"source" : "x" , "target" : "x_intro" , "relation" : "references" ,
1733+ "confidence" : "EXTRACTED" , "source_file" : "b/x.md" },
1734+ ]
1735+ return nodes , edges
1736+
1737+
1738+ def test_path_dropped_doc_id_keeps_both_files (capsys ):
1739+ """a/x.md and b/x.md both mint `x`; neither may be dropped, and each file's
1740+ edge stays inside its own file (#4281)."""
1741+ nodes , edges = _path_dropped_input ()
1742+ result_nodes , result_edges = deduplicate_entities (nodes , edges , communities = {})
1743+
1744+ assert len (result_nodes ) == 4
1745+ assert {n ["id" ] for n in result_nodes } == {"a_x" , "a_x_intro" , "b_x" , "b_x_intro" }
1746+ by_file = {e ["source_file" ]: (e ["source" ], e ["target" ]) for e in result_edges }
1747+ assert by_file == {"a/x.md" : ("a_x" , "a_x_intro" ), "b/x.md" : ("b_x" , "b_x_intro" )}
1748+ assert "minted by two different files" not in capsys .readouterr ().err
1749+
1750+
1751+ def test_path_dropped_rename_is_order_independent ():
1752+ """Guards determinism of the new pass: reversed input must give the same result.
1753+ Expected to pass pre-fix too, since the old collapse is deterministic as well."""
1754+ nodes , edges = _path_dropped_input ()
1755+ n1 , e1 = deduplicate_entities ([dict (n ) for n in nodes ], [dict (e ) for e in edges ],
1756+ communities = {})
1757+ n2 , e2 = deduplicate_entities ([dict (n ) for n in reversed (nodes )],
1758+ [dict (e ) for e in reversed (edges )], communities = {})
1759+ assert {(n ["source_file" ], n ["id" ]) for n in n1 } == {(n ["source_file" ], n ["id" ]) for n in n2 }
1760+ assert {(e ["source_file" ], e ["source" ], e ["target" ]) for e in e1 } == \
1761+ {(e ["source_file" ], e ["source" ], e ["target" ]) for e in e2 }
1762+
1763+
1764+ def test_path_dropped_foreign_reference_is_not_guessed (capsys ):
1765+ """c.md points at the bare `x`. It cannot be attributed to a/x.md or b/x.md, so
1766+ it is left unresolved with one warning rather than guessed (#4281)."""
1767+ nodes , edges = _path_dropped_input ()
1768+ nodes .append ({"id" : "c_note" , "label" : "Note C" , "file_type" : "document" ,
1769+ "source_file" : "c.md" })
1770+ edges .append ({"source" : "c_note" , "target" : "x" , "relation" : "references" ,
1771+ "confidence" : "INFERRED" , "source_file" : "c.md" })
1772+ result_nodes , result_edges = deduplicate_entities (nodes , edges , communities = {})
1773+
1774+ assert {n ["id" ] for n in result_nodes } == {
1775+ "a_x" , "a_x_intro" , "b_x" , "b_x_intro" , "c_note" }
1776+ foreign = next (e for e in result_edges if e ["source_file" ] == "c.md" )
1777+ assert foreign ["target" ] == "x"
1778+ warning = ("[graphify] WARNING: node 'x' was minted by several files and renamed "
1779+ "per file; 1 reference(s) from other files could not be attributed "
1780+ "and were left unresolved." )
1781+ assert capsys .readouterr ().err .count (warning ) == 1
1782+
1783+
1784+ @pytest .mark .parametrize ("a, b, leaked" , [
1785+ ("/home/u/a/x.md" , "/home/u/b/x.md" , "home_u" ),
1786+ ("C:\\ r\\ a\\ x.md" , "C:\\ r\\ b\\ x.md" , "c_r" ),
1787+ ])
1788+ def test_path_dropped_absolute_paths_are_not_qualified (a , b , leaked , capsys ):
1789+ """With no root to relativize against, a stored absolute path (POSIX or
1790+ Windows) must not be baked into an id: the checkout location would leak into
1791+ persisted ids. Such files stay on the collapse-and-warn path (#4281)."""
1792+ nodes = [
1793+ {"id" : "x" , "label" : "X A" , "file_type" : "document" , "source_file" : a },
1794+ {"id" : "x" , "label" : "X B" , "file_type" : "document" , "source_file" : b },
1795+ ]
1796+ result_nodes , _ = deduplicate_entities (nodes , [], communities = {})
1797+ assert not any (leaked in n ["id" ] for n in result_nodes )
1798+ assert "minted by two different files" in capsys .readouterr ().err
1799+
1800+
1801+ def test_single_path_dropped_id_left_alone ():
1802+ """A lone path-dropped id has no collision; build_from_json's remap handles it."""
1803+ nodes = [
1804+ {"id" : "x" , "label" : "X A" , "file_type" : "document" , "source_file" : "a/x.md" },
1805+ {"id" : "other" , "label" : "Other" , "file_type" : "document" , "source_file" : "a/y.md" },
1806+ ]
1807+ result_nodes , _ = deduplicate_entities (nodes , [], communities = {})
1808+ assert {n ["id" ] for n in result_nodes } == {"x" , "other" }
1809+
1810+
1811+ def test_path_dropped_through_build (tmp_path ):
1812+ """The full build path (dedup runs before build_from_json) keeps both files'
1813+ nodes and edges, with absolute semantic paths as real extractions carry (#4281)."""
1814+ from graphify .build import build
1815+
1816+ root = tmp_path
1817+ a = str (root / "a" / "x.md" )
1818+ b = str (root / "b" / "x.md" )
1819+ ext = {
1820+ "nodes" : [
1821+ {"id" : "x" , "label" : "X A" , "file_type" : "document" , "source_file" : a },
1822+ {"id" : "x_intro" , "label" : "Intro A" , "file_type" : "document" , "source_file" : a },
1823+ {"id" : "x" , "label" : "X B" , "file_type" : "document" , "source_file" : b },
1824+ {"id" : "x_intro" , "label" : "Intro B" , "file_type" : "document" , "source_file" : b },
1825+ ],
1826+ "edges" : [
1827+ {"source" : "x" , "target" : "x_intro" , "relation" : "references" ,
1828+ "confidence" : "EXTRACTED" , "source_file" : a },
1829+ {"source" : "x" , "target" : "x_intro" , "relation" : "references" ,
1830+ "confidence" : "EXTRACTED" , "source_file" : b },
1831+ ],
1832+ }
1833+ G = build ([ext ], root = str (root ))
1834+
1835+ assert set (G .nodes ) == {"a_x" , "a_x_intro" , "b_x" , "b_x_intro" }
1836+ assert G .has_edge ("a_x" , "a_x_intro" )
1837+ assert G .has_edge ("b_x" , "b_x_intro" )
1838+ assert not G .has_edge ("a_x" , "b_x_intro" )
1839+ assert not G .has_edge ("b_x" , "a_x_intro" )
1840+
1841+
1842+ def test_path_dropped_rebuild_is_idempotent ():
1843+ """Round-tripping a built graph through build() again must not keep renaming
1844+ the fix's own output (#4281)."""
1845+ import copy
1846+ from networkx .readwrite import json_graph
1847+ from graphify .build import build
1848+
1849+ nodes , edges = _path_dropped_input ()
1850+ G1 = build ([copy .deepcopy ({"nodes" : nodes , "edges" : edges })])
1851+ G2 = build ([json_graph .node_link_data (G1 , edges = "edges" )])
1852+
1853+ expected = ["a_x" , "a_x_intro" , "b_x" , "b_x_intro" ]
1854+ assert sorted (G1 .nodes ) == sorted (G2 .nodes ) == expected
1855+ assert {frozenset (e ) for e in G2 .edges } == {
1856+ frozenset (("a_x" , "a_x_intro" )), frozenset (("b_x" , "b_x_intro" ))}
1857+
1858+
1859+ def test_path_dropped_id_stays_qualified_after_collision_disappears ():
1860+ """Once b/x.md leaves the corpus there is no collision, but a/x.md must keep
1861+ `a_x` rather than flip back to the bare `x` (#4281)."""
1862+ from graphify .build import build
1863+
1864+ nodes , edges = _path_dropped_input ()
1865+ only_a = {"nodes" : [n for n in nodes if n ["source_file" ] == "a/x.md" ],
1866+ "edges" : [e for e in edges if e ["source_file" ] == "a/x.md" ]}
1867+ G = build ([only_a ])
1868+
1869+ assert sorted (G .nodes ) == ["a_x" , "a_x_intro" ]
1870+ assert G .has_edge ("a_x" , "a_x_intro" )
1871+
1872+
1873+ def test_path_dropped_foreign_reference_edge_is_dropped_by_build (capsys ):
1874+ """c.md's reference to the bare `x` cannot be attributed, so build() ends up
1875+ with no node `x` and drops the edge, and the warning says so (#4281)."""
1876+ from graphify .build import build
1877+
1878+ nodes , edges = _path_dropped_input ()
1879+ nodes .append ({"id" : "c_note" , "label" : "Note C" , "file_type" : "document" ,
1880+ "source_file" : "c.md" })
1881+ edges .append ({"source" : "c_note" , "target" : "x" , "relation" : "references" ,
1882+ "confidence" : "INFERRED" , "source_file" : "c.md" })
1883+ G = build ([{"nodes" : nodes , "edges" : edges }])
1884+
1885+ assert sorted (G .nodes ) == ["a_x" , "a_x_intro" , "b_x" , "b_x_intro" , "c_note" ]
1886+ assert G .degree ("c_note" ) == 0
1887+ assert not G .has_edge ("c_note" , "a_x" )
1888+ assert not G .has_edge ("c_note" , "b_x" )
1889+ assert ("node 'x' was minted by several files and renamed per file; "
1890+ "1 reference(s)" ) in capsys .readouterr ().err
0 commit comments