@@ -851,13 +851,18 @@ def test_crossfile_one_char_typo_concepts_still_merge():
851851
852852
853853@pytest .mark .parametrize ("a,b" , [
854- ({"id" : "d1" , "label" : "Getting Started Installation Guide" ,
854+ # #296 narrowed the document/rationale rows here to their boundary case: an
855+ # identical label on two nodes that are provably NOT their files' own nodes
856+ # now merges (that is the bug the concept-only gate was causing). What must
857+ # still never merge is a file's OWN node, so these two rows now carry the
858+ # ids the file nodes themselves would be minted with.
859+ ({"id" : "docs_a" , "label" : "Getting Started Installation Guide" ,
855860 "file_type" : "document" , "source_file" : "docs/a.md" },
856- {"id" : "d2 " , "label" : "Getting Started Installation Guide" ,
861+ {"id" : "docs_b " , "label" : "Getting Started Installation Guide" ,
857862 "file_type" : "document" , "source_file" : "docs/b.md" }),
858- ({"id" : "r1 " , "label" : _RATIONALE_BOILER ,
863+ ({"id" : "apps_platform_cards_apps " , "label" : _RATIONALE_BOILER ,
859864 "file_type" : "rationale" , "source_file" : "apps/platform/cards/apps.py" },
860- {"id" : "r2 " , "label" : _RATIONALE_BOILER ,
865+ {"id" : "apps_platform_cores_apps " , "label" : _RATIONALE_BOILER ,
861866 "file_type" : "rationale" , "source_file" : "apps/platform/cores/apps.py" }),
862867 ({"id" : "backend_a_render_frame" , "label" : "render_frame" ,
863868 "file_type" : "code" , "source_file" : "backend_a.py" },
@@ -879,12 +884,15 @@ def test_crossfile_one_char_typo_concepts_still_merge():
879884 "file_type" : "concept" , "source_file" : "doc1.md" },
880885 {"id" : "api_b" , "label" : "API" ,
881886 "file_type" : "concept" , "source_file" : "doc2.md" }),
882- ], ids = ["document" , "rationale" , "code" , "image-basename" , "concept-image-mixed" ,
883- "empty-source-file" , "low-entropy-concept" ])
887+ ], ids = ["document-own-file-node" , "rationale-own-file-node" , "code" ,
888+ "image-basename" , "concept-image-mixed" , "empty-source-file" ,
889+ "low-entropy-concept" ])
884890def test_crossfile_identical_labels_stay_distinct_for_guarded_types (a , b ):
885- """The #2182 fix is gated to high-entropy `concept` nodes with provenance
886- on BOTH sides. Identical labels must NOT merge for: file-anchored types
887- (document/rationale, #1284), code (#1205), images sharing a basename in
891+ """The #2182 cross-file merge requires provenance and high entropy on BOTH
892+ sides, and #296 widened its type gate only as far as nodes provably not
893+ their file's own node. Identical labels must still NOT merge for: a
894+ document/rationale node that IS its file's own node (#296, the boundary of
895+ #1284's file-anchored guard), code (#1205), images sharing a basename in
888896 different dirs, mixed concept+image pairs, provenance-less nodes (#1178),
889897 and low-entropy generic labels."""
890898 result_nodes , _ = deduplicate_entities ([dict (a ), dict (b )], [], communities = {})
@@ -1196,3 +1204,167 @@ def test_same_word_variant_helper():
11961204 # Accepted trade: a length-differing 5-char spelling variant reads as two
11971205 # words and stays unmerged, per the never-merge-distinct-entities bar.
11981206 assert not _same_word_variant ("colour" , "color" )
1207+
1208+
1209+ # -- #296: cross-file merge for entity nodes typed by their file's extension --
1210+ #
1211+ # Reported shape: per-file extraction over a note vault mints one node per
1212+ # mention of the same person/project, and they never merge. Normalization was
1213+ # never the problem -- `_norm` already buckets the variants together -- the
1214+ # Pass 1 cross-file union was gated to `file_type == "concept"`, and an entity
1215+ # pulled out of a `.md` note inherits `document` from the file's extension, not
1216+ # from anything about the entity. The @cyrilXBT variant family below is the
1217+ # reported corpus's shape, reduced to the spellings that occur in it.
1218+
1219+ _CYRIL_VARIANTS = ["@cyrilXBT" , "@cyrilxbt" , "cyrilXBT" ]
1220+
1221+
1222+ def test_provably_not_file_structure_helper ():
1223+ """The predicate proves entity-ness, and answers False whenever it cannot
1224+ (#296): a file's own node, a structural `page`/`heading` node, and any node
1225+ missing an id or provenance all stay treated as file-anchored."""
1226+ from graphify .dedup import _provably_not_file_structure
1227+ # An entity extracted from a note: `<path>_<entity>` never equals the path.
1228+ assert _provably_not_file_structure (
1229+ {"id" : "journal_2024_03_01_cyrilxbt" , "label" : "@cyrilXBT" ,
1230+ "source_file" : "journal/2024-03-01.md" })
1231+ # The file's own node, in each spelling a stored source_file may take.
1232+ assert not _provably_not_file_structure (
1233+ {"id" : "journal_2024_03_01" , "source_file" : "journal/2024-03-01.md" })
1234+ assert not _provably_not_file_structure (
1235+ {"id" : "2024_03_01" , "source_file" : "journal/2024-03-01.md" }) # pre-#1504 bare stem
1236+ assert not _provably_not_file_structure (
1237+ {"id" : "vault_journal_2024_03_01" , "source_file" : 'C:\\ vault\\ journal\\ 2024-03-01.md' })
1238+ # The markdown extractor states it outright, for the file and its sections.
1239+ assert not _provably_not_file_structure (
1240+ {"id" : "anything" , "node_kind" : "page" , "source_file" : "docs/a.md" })
1241+ assert not _provably_not_file_structure (
1242+ {"id" : "docs_a_decisions" , "node_kind" : "heading" ,
1243+ "label" : "Decisions" , "source_file" : "docs/a.md" })
1244+ # Unprovable -- no id, or no provenance.
1245+ assert not _provably_not_file_structure ({"id" : "" , "source_file" : "docs/a.md" })
1246+ assert not _provably_not_file_structure ({"id" : "docs_a_thing" , "source_file" : "" })
1247+
1248+
1249+ def test_dedup_merges_crossfile_document_entity_variants ():
1250+ """The reported bug (#296): case/prefix variants of one entity, extracted
1251+ from three different notes and typed `document` by extension, must collapse
1252+ to a single node."""
1253+ nodes = [
1254+ {"id" : "journal_2024_03_0%d_cyrilxbt" % i , "label" : variant ,
1255+ "file_type" : "document" , "source_file" : "journal/2024-03-0%d.md" % i }
1256+ for i , variant in enumerate (_CYRIL_VARIANTS , start = 1 )
1257+ ]
1258+ result_nodes , _ = deduplicate_entities (nodes , [], communities = {})
1259+ assert len (result_nodes ) == 1 , (
1260+ "cross-file `document` entity variants did not merge -- the #296 gate "
1261+ "widening is not reaching Pass 1's cross-file residue"
1262+ )
1263+
1264+
1265+ def test_dedup_merges_crossfile_rationale_entity_variants ():
1266+ """`rationale` rides the same gate as `document` (#296): entity nodes of
1267+ that type, provably not their files' own nodes, merge on an exact label."""
1268+ nodes = [
1269+ {"id" : "svc_alpha_py_retention_window" , "label" : "Retention Window" ,
1270+ "file_type" : "rationale" , "source_file" : "svc/alpha.py" },
1271+ {"id" : "svc_beta_py_retention_window" , "label" : "retention window" ,
1272+ "file_type" : "rationale" , "source_file" : "svc/beta.py" },
1273+ ]
1274+ result_nodes , _ = deduplicate_entities (nodes , [], communities = {})
1275+ assert len (result_nodes ) == 1
1276+
1277+
1278+ def test_dedup_never_merges_a_files_own_node_away ():
1279+ """The boundary the widened gate is defined against (#296): a curated note
1280+ whose OWN node carries the entity's label must survive as its own node, even
1281+ when a same-label entity node exists in another file."""
1282+ nodes = [
1283+ # The curated page: its id is exactly the slugified source path.
1284+ {"id" : "people_cyrilxbt" , "label" : "@cyrilXBT" ,
1285+ "file_type" : "document" , "source_file" : "people/@cyrilXBT.md" },
1286+ # A mention of the same entity, extracted from a journal note.
1287+ {"id" : "journal_2024_03_01_cyrilxbt" , "label" : "cyrilXBT" ,
1288+ "file_type" : "document" , "source_file" : "journal/2024-03-01.md" },
1289+ ]
1290+ result_nodes , _ = deduplicate_entities (nodes , [], communities = {})
1291+ assert len (result_nodes ) == 2 , (
1292+ "a file's own node merged away -- the #296 widening must never reach it"
1293+ )
1294+ assert {n ["id" ] for n in result_nodes } == {
1295+ "people_cyrilxbt" , "journal_2024_03_01_cyrilxbt" }
1296+
1297+
1298+ def test_dedup_never_merges_two_files_own_nodes ():
1299+ """Two README.md in different folders are genuinely two documents (#1284's
1300+ original reasoning), and stay two under #296."""
1301+ nodes = [
1302+ {"id" : "web_readme" , "label" : "README.md" , "file_type" : "document" ,
1303+ "node_kind" : "page" , "source_file" : "web/README.md" },
1304+ {"id" : "api_readme" , "label" : "README.md" , "file_type" : "document" ,
1305+ "node_kind" : "page" , "source_file" : "api/README.md" },
1306+ ]
1307+ result_nodes , _ = deduplicate_entities (nodes , [], communities = {})
1308+ assert len (result_nodes ) == 2
1309+
1310+
1311+ def test_dedup_crossfile_entity_merge_keeps_the_entropy_gate ():
1312+ """Entropy guard untouched (#296): a short generic label stays distinct for
1313+ `document` entity nodes exactly as it does for `concept`."""
1314+ nodes = [
1315+ {"id" : "docs_a_api" , "label" : "API" , "file_type" : "document" ,
1316+ "source_file" : "docs/a.md" },
1317+ {"id" : "docs_b_api" , "label" : "API" , "file_type" : "document" ,
1318+ "source_file" : "docs/b.md" },
1319+ ]
1320+ result_nodes , _ = deduplicate_entities (nodes , [], communities = {})
1321+ assert len (result_nodes ) == 2
1322+
1323+
1324+ def test_dedup_crossfile_entity_merge_keeps_the_provenance_gate ():
1325+ """Provenance guard untouched (#296, #1178): without a source_file the node
1326+ cannot be proven to be an entity, so it stays out of the merge."""
1327+ nodes = [
1328+ {"id" : "orphan_cyrilxbt" , "label" : "@cyrilXBT" ,
1329+ "file_type" : "document" , "source_file" : "" },
1330+ {"id" : "journal_2024_03_01_cyrilxbt" , "label" : "cyrilXBT" ,
1331+ "file_type" : "document" , "source_file" : "journal/2024-03-01.md" },
1332+ ]
1333+ result_nodes , _ = deduplicate_entities (nodes , [], communities = {})
1334+ assert len (result_nodes ) == 2
1335+
1336+
1337+ def test_dedup_crossfile_fuzzy_fileanchored_block_is_untouched ():
1338+ """#296 widens only the exact-normalization pass. Pass 2's fuzzy
1339+ `_crossfile_fileanchored_blocked` is unchanged, so near-identical (not
1340+ identical) document labels in different files still stay distinct -- the
1341+ #1284 guard keeps doing its job on entity nodes too."""
1342+ nodes = [
1343+ {"id" : "docs_a_guide" , "label" : "Getting Started Installation Guide" ,
1344+ "file_type" : "document" , "source_file" : "docs/a.md" },
1345+ {"id" : "docs_b_setup" , "label" : "Getting Started Installation Setup" ,
1346+ "file_type" : "document" , "source_file" : "docs/b.md" },
1347+ ]
1348+ result_nodes , _ = deduplicate_entities (nodes , [], communities = {})
1349+ assert len (result_nodes ) == 2
1350+
1351+
1352+ def test_dedup_never_merges_repeated_headings_across_files ():
1353+ """#3094's verification case must keep holding under #296: sibling documents
1354+ that each carry the same `## Decisions` / `## Next steps` sections keep one
1355+ heading node per file, attributed to that file."""
1356+ nodes = [
1357+ {"id" : "docs_%s_%s" % (doc , slug ), "label" : heading ,
1358+ "file_type" : "document" , "node_kind" : "heading" ,
1359+ "source_file" : "docs/%s.md" % doc }
1360+ for doc in ("a" , "b" , "c" )
1361+ for slug , heading in (("decisions" , "Decisions" ),
1362+ ("next_steps" , "Next steps" ))
1363+ ]
1364+ result_nodes , _ = deduplicate_entities (nodes , [], communities = {})
1365+ assert len (result_nodes ) == 6 , (
1366+ "repeated headings merged across files -- #296 must not reach section "
1367+ "nodes (#1284, #3094)"
1368+ )
1369+ assert {n ["source_file" ] for n in result_nodes } == {
1370+ "docs/a.md" , "docs/b.md" , "docs/c.md" }
0 commit comments