Skip to content

Commit 642e4b8

Browse files
SrijanSrivsafishamsi
authored andcommitted
fix(extract): keep edges between kept nodes when edge source_file is undispatched (#2276)
(cherry picked from commit cf78a6c)
1 parent edb9331 commit 642e4b8

3 files changed

Lines changed: 135 additions & 13 deletions

File tree

‎graphify/cache.py‎

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1838,17 +1838,18 @@ def scope_semantic_result(
18381838
still says unchanged, so no later incremental run re-dispatches it and the
18391839
loss is permanent until a full rebuild.
18401840
1841-
Items whose ``source_file`` resolves outside ``allowed_source_files`` are
1842-
dropped from ``result``'s ``nodes`` / ``edges`` / ``hyperedges`` lists
1843-
(mutated in place); items without a ``source_file`` pass through. An edge
1844-
or hyperedge that survives the scope filter but references a dropped node
1845-
id is dropped too (#1916 mirror), unless that id is also defined by a kept
1846-
node (duplicate attribution must not be over-pruned).
1841+
Nodes and hyperedges whose ``source_file`` resolves outside
1842+
``allowed_source_files`` are dropped from ``result`` (mutated in place);
1843+
items without a ``source_file`` pass through. An edge whose own
1844+
``source_file`` is outside the allowlist is kept when both endpoint ids are
1845+
kept nodes (#2276). An edge or hyperedge that survives
1846+
the scope filter but references a dropped node id is dropped too (#1916
1847+
mirror), unless that id is also defined by a kept node (duplicate
1848+
attribution must not be over-pruned).
18471849
18481850
Path matching shares :func:`_semantic_source_matcher` with
18491851
:func:`save_semantic_cache` (relative against ``root``, walked-path
1850-
identity), so an item this function keeps can never still hit the save's
1851-
out-of-scope skip, and vice versa.
1852+
identity). That kept edge can still be skipped by the cache write.
18521853
18531854
Returns ``(dropped_source_files, dropped_item_count)`` for logging;
18541855
``dropped_source_files`` holds the normalized ``source_file`` strings of
@@ -1885,6 +1886,20 @@ def _hashable(value) -> bool:
18851886
for item in result.get(bucket) or []:
18861887
display, ident = _item_identity(item)
18871888
if ident is not None and ident not in allowed_paths:
1889+
if bucket == "edges":
1890+
src = item.get("source")
1891+
tgt = item.get("target")
1892+
# Nodes are processed first, so kept_ids is complete.
1893+
# An edge to a dropped id that is not also kept still falls
1894+
# through and is counted below (#2276).
1895+
if (
1896+
_hashable(src)
1897+
and _hashable(tgt)
1898+
and src in kept_ids
1899+
and tgt in kept_ids
1900+
):
1901+
kept.append(item)
1902+
continue
18881903
dropped_files.add(display)
18891904
dropped_items += 1
18901905
if bucket == "nodes" and item.get("id") is not None:

‎graphify/llm.py‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2836,25 +2836,35 @@ def _out_of_scope(item: dict) -> bool:
28362836
dropped_ids: set = set()
28372837
dropped_files: set[str] = set()
28382838
kept_nodes: list[dict] = []
2839+
kept_ids: set = set()
28392840
for n in merged.get("nodes", []):
28402841
if _out_of_scope(n):
28412842
if n.get("id") is not None:
28422843
dropped_ids.add(n.get("id"))
28432844
dropped_files.add(str(n.get("source_file")))
28442845
continue
28452846
kept_nodes.append(n)
2847+
if n.get("id") is not None:
2848+
kept_ids.add(n.get("id"))
28462849
dropped_node_count = len(merged.get("nodes", [])) - len(kept_nodes)
28472850
merged["out_of_scope_dropped"] = dropped_node_count
28482851
if dropped_node_count:
28492852
merged["nodes"] = kept_nodes
2850-
# Keep the graph consistent: an edge or hyperedge referencing a
2851-
# dropped node's id (or itself attributed to an undispatched real
2852-
# file) must not survive its endpoint.
2853+
# Keep the graph consistent: drop an edge that references a dropped
2854+
# node's id. An edge attributed to an undispatched file is kept when
2855+
# both endpoints are kept nodes (#2276). Hyperedges stay on the
2856+
# out-of-scope predicate.
28532857
merged["edges"] = [
28542858
e for e in merged.get("edges", [])
2855-
if not _out_of_scope(e)
2856-
and e.get("source") not in dropped_ids
2859+
if e.get("source") not in dropped_ids
28572860
and e.get("target") not in dropped_ids
2861+
and (
2862+
not _out_of_scope(e)
2863+
or (
2864+
e.get("source") in kept_ids
2865+
and e.get("target") in kept_ids
2866+
)
2867+
)
28582868
]
28592869
merged["hyperedges"] = [
28602870
h for h in merged.get("hyperedges", [])

‎tests/test_chunking.py‎

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
492589
def 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

Comments
 (0)