Skip to content

Commit 4ac5f07

Browse files
xor-xesafishamsi
authored andcommitted
fix(watch): preserve semantic edges of re-extracted sources (#1865)
A full graphify update evicted every edge whose source_file was a re-extracted source, including LLM semantic edges (e.g. semantically_similar_to) from Markdown docs that also have an AST extractor. The AST pass cannot regenerate those edges, so they were silently lost while their concept nodes survived — node eviction is provenance-aware via _origin (#1116), edge eviction was not. Tag AST-extracted edges with the same _origin=ast marker nodes already carry, and scope rebuild-driven edge eviction to that tier: re-extraction replaces a source's AST edges, while its semantic edges survive until a semantic re-extraction supersedes them. Deletion-driven eviction stays provenance-blind, so edges of deleted or excluded sources are still purged regardless of tier. Edges from graphs built before this change lack the marker; a stale AST edge from a file changed exactly once between the old and new version can linger until its source is deleted — the same migration trade-off the #1116 node marker made.
1 parent 1aca158 commit 4ac5f07

3 files changed

Lines changed: 85 additions & 6 deletions

File tree

‎graphify/extract.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4874,9 +4874,14 @@ def _has_import_evidence(candidate_id: str) -> bool:
48744874
# Tag AST provenance so the incremental watch rebuild can distinguish
48754875
# AST-extracted nodes from semantic/LLM nodes. On a full re-extraction
48764876
# the watcher drops any AST-marked node missing from the fresh output
4877-
# even when its source file still exists (#1116).
4877+
# even when its source file still exists (#1116). Edges carry the same
4878+
# marker so edge eviction can be tier-scoped: re-extracting a source
4879+
# replaces its AST edges without evicting the semantic edges the AST
4880+
# pass cannot regenerate (#1865).
48784881
for n in all_nodes:
48794882
n["_origin"] = "ast"
4883+
for e in all_edges:
4884+
e["_origin"] = "ast"
48804885

48814886
return {
48824887
"nodes": all_nodes,

‎graphify/watch.py‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -408,11 +408,11 @@ def _reconcile_existing_graph(
408408
}
409409
node_evicted_source_identities = set(deleted_source_identities)
410410
hyperedge_evicted_source_identities = set(deleted_source_identities)
411+
# Deletion evicts edges regardless of tier; re-extraction only owns a
412+
# source's AST-tier edges (checked per-edge below, #1865).
413+
edge_evicted_source_identities = set(deleted_source_identities)
411414
if not full_rebuild:
412415
node_evicted_source_identities.update(rebuilt_source_identities)
413-
edge_evicted_source_identities = (
414-
node_evicted_source_identities | rebuilt_source_identities
415-
)
416416

417417
# Reconcile every rebuild against the current watched corpus. Hook change
418418
# lists can contain only a rename destination, so explicit paths alone
@@ -485,14 +485,22 @@ def _reconcile_existing_graph(
485485
]
486486
all_ids = new_ast_ids | {node["id"] for node in preserved_nodes}
487487

488-
# Edges are owned by source_file. Re-extraction must replace an owner's
489-
# previous edges, while edges from unchanged or semantic sources survive.
488+
# Edges are owned by source_file, but ownership is tier-scoped: the AST
489+
# pass replaces a re-extracted source's AST edges, while that source's
490+
# semantic/LLM edges — which the AST pass cannot regenerate — survive
491+
# until a semantic re-extraction supersedes them. Same provenance rule
492+
# the node reconciliation above applies via _origin (#1865). Deletion
493+
# eviction stays provenance-blind.
490494
preserved_edges = [
491495
edge
492496
for edge in existing.get("links", existing.get("edges", []))
493497
if edge.get("source") in all_ids
494498
and edge.get("target") in all_ids
495499
and not source_paths.is_evicted(edge, edge_evicted_source_identities)
500+
and not (
501+
edge.get("_origin") == "ast"
502+
and source_paths.is_evicted(edge, rebuilt_source_identities)
503+
)
496504
]
497505

498506
new_hyperedge_ids = {

‎tests/test_watch.py‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,72 @@ def test_rebuild_code_preserves_hyperedges_for_rebuilt_surviving_source(
386386
}]
387387

388388

389+
@pytest.mark.parametrize(
390+
"changed_paths",
391+
[None, [Path("auth.md")]],
392+
ids=["full-update", "incremental-doc-update"],
393+
)
394+
def test_rebuild_code_preserves_semantic_edges_from_reextracted_doc(
395+
tmp_path, changed_paths
396+
):
397+
"""#1865: AST-only updates must not evict semantic edges whose source_file
398+
is a re-extracted document; only that source's AST-tier edges are replaced."""
399+
from graphify.watch import _rebuild_code
400+
401+
corpus = tmp_path / "corpus"
402+
corpus.mkdir()
403+
(corpus / "auth.md").write_text(
404+
"# Token Validation\n\nVerifies bearer tokens.\n", encoding="utf-8"
405+
)
406+
(corpus / "login.md").write_text(
407+
"# Session Verification\n\nVerifies login sessions.\n", encoding="utf-8"
408+
)
409+
410+
assert _rebuild_code(corpus, no_cluster=True, acquire_lock=False) is True
411+
graph_path = corpus / "graphify-out" / "graph.json"
412+
data = json.loads(graph_path.read_text(encoding="utf-8"))
413+
node_ids = {n["id"] for n in data["nodes"]}
414+
assert {"auth_token_validation", "login_session_verification"} <= node_ids
415+
416+
data["links"].extend([
417+
{
418+
"source": "auth_token_validation",
419+
"target": "login_session_verification",
420+
"relation": "semantically_similar_to",
421+
"confidence": "INFERRED",
422+
"source_file": "auth.md",
423+
},
424+
# A stale AST-tier edge of the same source must still be evicted.
425+
{
426+
"source": "auth_token_validation",
427+
"target": "login_session_verification",
428+
"relation": "references",
429+
"_origin": "ast",
430+
"source_file": "auth.md",
431+
},
432+
])
433+
graph_path.write_text(json.dumps(data), encoding="utf-8")
434+
435+
assert _rebuild_code(
436+
corpus,
437+
changed_paths=changed_paths,
438+
no_cluster=True,
439+
acquire_lock=False,
440+
) is True
441+
442+
after = json.loads(graph_path.read_text(encoding="utf-8"))
443+
relations = {
444+
(e.get("source"), e.get("target"), e.get("relation"))
445+
for e in after["links"]
446+
}
447+
assert (
448+
"auth_token_validation", "login_session_verification", "semantically_similar_to"
449+
) in relations, "semantic edge from a re-extracted doc must survive an AST-only update"
450+
assert (
451+
"auth_token_validation", "login_session_verification", "references"
452+
) not in relations, "stale AST-tier edge of a re-extracted source must be evicted"
453+
454+
389455
@pytest.mark.parametrize(
390456
"changed_paths",
391457
[None, [Path("only.py")]],

0 commit comments

Comments
 (0)