@@ -489,6 +489,103 @@ def stray(chunk, **kwargs):
489489 assert result ["uncovered_files" ] == []
490490
491491
492+ def test_edge_between_kept_nodes_survives_when_source_file_was_not_dispatched (tmp_path ):
493+ """#2276: a references edge between two re-extracted pages is kept when the
494+ model stamps it with a real file this run did not dispatch, and a node
495+ attributed to another undispatched file is still dropped."""
496+ from graphify .build import build , build_merge
497+ from graphify .cache import scope_semantic_result
498+ from graphify .export import to_json
499+ from graphify .llm import extract_corpus_parallel
500+
501+ page_a = tmp_path / "page_a.md"
502+ page_b = tmp_path / "page_b.md"
503+ log = tmp_path / "log.md"
504+ other = tmp_path / "other.md"
505+ for path in (page_a , page_b , log , other ):
506+ path .write_text ("# page\n " , encoding = "utf-8" )
507+
508+ def _node (nid : str , source_file : str ) -> dict :
509+ return {
510+ "id" : nid ,
511+ "label" : nid ,
512+ "file_type" : "document" ,
513+ "source_file" : source_file ,
514+ "source_location" : None ,
515+ "_origin" : "semantic" ,
516+ }
517+
518+ def _edge (source : str , target : str , source_file : str ) -> dict :
519+ return {
520+ "source" : source ,
521+ "target" : target ,
522+ "relation" : "references" ,
523+ "source_location" : None ,
524+ "_origin" : "semantic" ,
525+ "confidence" : "INFERRED" ,
526+ "weight" : 1.0 ,
527+ "source_file" : source_file ,
528+ "file_type" : "document" ,
529+ }
530+
531+ prior = {
532+ "nodes" : [
533+ _node ("page_a" , "page_a.md" ),
534+ _node ("page_b" , "page_b.md" ),
535+ _node ("log" , "log.md" ),
536+ _node ("leaf" , "log.md" ),
537+ ],
538+ "edges" : [
539+ _edge ("log" , "leaf" , "log.md" ),
540+ _edge ("page_a" , "page_b" , "page_a.md" ),
541+ ],
542+ "hyperedges" : [],
543+ }
544+ graph_path = tmp_path / "graph.json"
545+ to_json (build ([prior ], root = tmp_path ), {}, str (graph_path ))
546+
547+ def fresh (chunk , ** kwargs ):
548+ return {
549+ "nodes" : [
550+ _node ("page_a" , "page_a.md" ),
551+ _node ("page_b" , "page_b.md" ),
552+ _node ("stray" , "other.md" ),
553+ ],
554+ "edges" : [
555+ _edge ("page_a" , "page_b" , "log.md" ),
556+ _edge ("page_a" , "stray" , "page_a.md" ),
557+ ],
558+ "hyperedges" : [],
559+ "input_tokens" : 1 ,
560+ "output_tokens" : 1 ,
561+ }
562+
563+ with patch ("graphify.llm.extract_files_direct" , side_effect = fresh ):
564+ result = extract_corpus_parallel (
565+ [page_a , page_b ], backend = "kimi" , root = tmp_path ,
566+ token_budget = None , chunk_size = 2 , max_concurrency = 1 ,
567+ )
568+
569+ assert any (
570+ e .get ("source" ) == "page_a" and e .get ("target" ) == "page_b"
571+ for e in result ["edges" ]
572+ ), f"page_a→page_b missing after extract: { result ['edges' ]} "
573+ assert all (n .get ("id" ) != "stray" for n in result ["nodes" ])
574+
575+ scope_semantic_result (result , tmp_path , [page_a , page_b ])
576+ assert any (
577+ e .get ("source" ) == "page_a" and e .get ("target" ) == "page_b"
578+ for e in result ["edges" ]
579+ ), f"page_a→page_b missing after scope: { result ['edges' ]} "
580+
581+ G = build_merge ([result ], graph_path , root = tmp_path )
582+ assert G .has_edge ("page_a" , "page_b" )
583+ assert G .has_edge ("log" , "leaf" )
584+ assert "stray" not in G
585+ assert not G .has_edge ("page_a" , "stray" )
586+ assert "page_a" in G and "page_b" in G
587+
588+
492589def test_out_of_scope_drop_count_is_zero_when_all_in_scope (tmp_path , capsys ):
493590 """Counter-test: a clean run records out_of_scope_dropped == 0 and no warning."""
494591 from graphify .llm import extract_corpus_parallel
0 commit comments