fix(watch): evict excluded linked-worktree source records

Refs #4912.

Co-Authored-By: OpenAI Codex <noreply@openai.com>
(cherry picked from commit 237840b578)
This commit is contained in:
azizur100389 authored and safishamsi committed 2026-11-02 18:02:08 +00:00
1 parent 7750ba8cef
commit 3d772b9778
3 files changed
+87

No files matched your search

+8
View File
@@ -2012,6 +2012,7 @@ def ignored_predicate(
if line:
patterns.append((root, line))
explicit_patterns.append((root, line))
linked_boundaries: dict[Path, bool] = {}
cache: dict[Path, bool] = {}
explicit_cache: dict[Path, bool] = {}
# root's own ignore file is the last entry of _load_graphifyignore's chain.
@@ -2032,6 +2033,13 @@ def ignored_predicate(
if _is_noise_dir(part, parent):
return True
parent = parent / part
if parent not in linked_boundaries:
main_root = _linked_worktree_main_root(parent)
linked_boundaries[parent] = (
main_root is not None and _resolves_under_root(main_root, root)
)
if linked_boundaries[parent]:
return True
# Load ignore files along this path's own lineage — detect()'s walk
# would have loaded exactly these before reaching the file (#1206).
ancestor = root
+17
View File
@@ -1254,6 +1254,23 @@ def _reconcile_existing_graph(
node_evicted_source_identities.add(identity)
edge_evicted_source_identities.add(identity)
hyperedge_evicted_source_identities.add(identity)
# Edges and hyperedges may own a source with no corresponding node.
# Positive exclusion evidence applies to these records too (#4912).
seen_record_sources: set[str] = set()
for item in existing.get("links", existing.get("edges", [])) + existing.get("hyperedges", []):
source_file = item.get("source_file")
if source_file in seen_record_sources:
continue
if source_file:
seen_record_sources.add(source_file)
if (not source_file or _is_remote_source(source_file)
or not source_paths.in_watch_root(source_file)):
continue
identity = source_paths.identity(source_file)
if identity and identity not in current_sources and Path(identity).exists() and _ignored_now(identity):
edge_evicted_source_identities.add(identity)
hyperedge_evicted_source_identities.add(identity)
newly_ignored_files.add(identity)
if newly_ignored_files:
print(
f"[graphify watch] pruned {newly_ignored_nodes} node(s) from "
+62
View File
@@ -0,0 +1,62 @@
"""Linked copies excluded by detection must not survive a full reconcile."""
import json
from pathlib import Path
import subprocess
import pytest
from graphify.detect import detect, ignored_predicate
from graphify.watch import _reconcile_existing_graph
def workspace(tmp_path):
main = tmp_path / "app"
main.mkdir()
source = main / "a.py"
source.write_text("def answer(): return 42\n", encoding="utf-8")
def git(*args):
subprocess.run(["git", "-C", str(main), *args], check=True, capture_output=True)
git("init", "-q")
git("add", ".")
git("-c", "user.name=Fixture", "-c", "user.email=fixture@example.com", "commit", "-qm", "fixture")
linked = tmp_path / "app-feature"
git("worktree", "add", "-q", "-b", "fixture-linked", str(linked))
return source, linked / "a.py", linked
def test_predicate_matches_scan_for_linked_worktree(tmp_path):
source, linked_source, linked = workspace(tmp_path)
files = {Path(p).resolve() for group in detect(tmp_path)["files"].values() for p in group}
assert source.resolve() in files
assert linked_source.resolve() not in files
assert ignored_predicate(tmp_path)(linked_source)
# Scanning the linked copy itself is intentional, and a main checkout
# outside the scan root must not suppress its only scanned copy.
assert not ignored_predicate(linked)(linked_source)
@pytest.mark.parametrize("linked_node", [False, True])
def test_reconcile_prunes_node_edge_and_hyperedge_owned_by_linked_copy(tmp_path, linked_node):
source, linked_source, _ = workspace(tmp_path)
control = tmp_path / "unexplained.txt"
control.write_text("still here", encoding="utf-8")
nodes = [{"id": n, "label": n, "source_file": str(source), "_origin": "ast"} for n in ["a", "b"]]
old = {"nodes": nodes + [{"id": "linked", "label": "linked", "source_file": str(linked_source), "_origin": "ast"},
{"id": "control", "label": "control", "source_file": str(control), "_origin": "semantic"}],
"links": [{"source": "a", "target": "b", "relation": "references", "source_file": str(linked_source), "_origin": "semantic"},
{"source": "b", "target": "a", "relation": "references", "source_file": str(control), "_origin": "semantic"}],
"hyperedges": [{"id": "linked-edge", "nodes": ["a", "b"], "source_file": str(linked_source)},
{"id": "control-edge", "nodes": ["a", "b"], "source_file": str(control)}]}
if not linked_node:
old["nodes"] = [n for n in old["nodes"] if n["id"] != "linked"]
out = tmp_path / "graphify-out"
out.mkdir()
graph = out / "graph.json"
graph.write_text(json.dumps(old), encoding="utf-8")
result, _ = _reconcile_existing_graph(graph, {"nodes": nodes, "edges": [], "hyperedges": []},
out=out, project_root=tmp_path, watch_root=tmp_path,
code_files=[source], extract_targets=[source], full_rebuild=True,
deleted_paths=set(), deleted_source_identities=set(),
is_ignored_full=ignored_predicate(tmp_path))
assert {n["id"] for n in result["nodes"]} == {"a", "b", "control"}
assert [e["source_file"] for e in result["edges"]] == [control.relative_to(tmp_path).as_posix()]
assert [h["id"] for h in result["hyperedges"]] == ["control-edge"]