fix(query,serve): stop flagging fresh subfolder builds as pre-#1504
`graphify update sub` run from sub's parent stores source_file relative
to the build cwd (sub/pkg/mod.py, #2603) while ids are minted relative
to the scan root (pkg_mod). graph_has_legacy_ids derived the expected
stem from the stored path (sub_pkg_mod), so the real id matched the old
one-parent form and every query/MCP load of a freshly built graph told
the user to rebuild with `graphify extract --force`, which changes
nothing.
Read the cwd->scan-root offset from the relative .graphify_root marker
(new legacy_id_source_prefix) and strip it before deriving the stem.
Absolute, ".", and parent-escaping markers mean no offset, so behavior
for root-anchored graphs is unchanged and genuine legacy ids are still
reported.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TGb7YtZeBXGNQMw6JjuqZu
(cherry picked from commit f5ee99de8f)
This commit is contained in:
1 parent
67c139e403
commit
2394fd3f98
4 files changed
+109
-5
No files matched your search
+42
-3
@@ -1434,7 +1434,37 @@ def _has_global_id(node: dict) -> bool:
|
||||
return meta.get("mcp_kind") in _MCP_GLOBAL_ID_KINDS
|
||||
|
||||
|
||||
def graph_has_legacy_ids(nodes: list, root: str | Path | None = None, sample: int = 300) -> bool:
|
||||
def legacy_id_source_prefix(graph_path: str | Path) -> str | None:
|
||||
"""The scan-root prefix that a graph's stored ``source_file`` values carry.
|
||||
|
||||
A build scoped to a subfolder from its parent (``graphify update sub``)
|
||||
stores ``source_file`` relative to the build's cwd (``sub/pkg/mod.py``,
|
||||
#2603) while every id is minted relative to the scan root (``pkg_mod``).
|
||||
The relative ``.graphify_root`` marker next to the graph records exactly
|
||||
that offset (``sub``). Returns it in normalized POSIX form, or None when
|
||||
there is no offset: no marker, an absolute marker (the build anchored
|
||||
source_file at the scan root itself), ``.``, or a path leaving the cwd.
|
||||
"""
|
||||
try:
|
||||
text = (Path(graph_path).parent / ".graphify_root").read_text(
|
||||
encoding="utf-8-sig"
|
||||
).strip()
|
||||
except (OSError, UnicodeError, ValueError):
|
||||
return None
|
||||
if not text:
|
||||
return None
|
||||
text = text.replace("\\", "/")
|
||||
if _is_abs(text):
|
||||
return None
|
||||
import posixpath
|
||||
prefix = posixpath.normpath(text)
|
||||
if prefix in (".", "") or prefix == ".." or prefix.startswith("../"):
|
||||
return None
|
||||
return prefix
|
||||
|
||||
|
||||
def graph_has_legacy_ids(nodes: list, root: str | Path | None = None, sample: int = 300,
|
||||
source_prefix: str | None = None) -> bool:
|
||||
"""Whether a loaded graph still uses pre-#1504 node IDs (parent-dir / filename
|
||||
stem) rather than the full repo-relative path. Read-only consumers (query,
|
||||
serve) use this to nudge the user to rebuild, since they don't re-extract.
|
||||
@@ -1446,9 +1476,16 @@ def graph_has_legacy_ids(nodes: list, root: str | Path | None = None, sample: in
|
||||
file-stem form and would otherwise false-positive. Nodes whose ID is global by
|
||||
construction (see ``_MCP_GLOBAL_ID_KINDS``) are skipped for the same reason.
|
||||
Returns True as soon as one file node's ID matches an OLD stem form but not the
|
||||
canonical full-path form."""
|
||||
canonical full-path form.
|
||||
|
||||
``source_prefix`` (see :func:`legacy_id_source_prefix`) is the cwd->scan-root
|
||||
offset stored source_file values carry; it is stripped before the stem is
|
||||
derived, since ids never include it. Without it a fresh subfolder build reads
|
||||
as legacy: ``sub/pkg/mod.py`` wants ``sub_pkg_mod``, and the real ``pkg_mod``
|
||||
looks exactly like the old one-parent form."""
|
||||
from graphify.extractors.base import _file_stem
|
||||
_r = str(root) if root is not None else None
|
||||
_pfx = (source_prefix.replace("\\", "/").rstrip("/") + "/") if source_prefix else None
|
||||
checked = 0
|
||||
for node in nodes:
|
||||
if not isinstance(node, dict):
|
||||
@@ -1468,9 +1505,11 @@ def graph_has_legacy_ids(nodes: list, root: str | Path | None = None, sample: in
|
||||
if not nid or not isinstance(nid, str) or not sf:
|
||||
continue
|
||||
sf_norm = _norm_source_file(str(sf), _r) or str(sf)
|
||||
rel = Path(sf_norm)
|
||||
if _is_abs(sf_norm):
|
||||
continue
|
||||
if _pfx and sf_norm.startswith(_pfx):
|
||||
sf_norm = sf_norm[len(_pfx):]
|
||||
rel = Path(sf_norm)
|
||||
if not rel.name:
|
||||
continue # source_file == scan root -> Path('.'), no file stem (#1618)
|
||||
new_stem = make_id(_file_stem(rel))
|
||||
|
||||
+4
-1
@@ -2053,7 +2053,10 @@ def dispatch_command(cmd: str) -> None:
|
||||
# direction of a link persisted in flipped endpoint order.
|
||||
try:
|
||||
from graphify.build import graph_has_legacy_ids as _legacy
|
||||
_legacy_ids = _legacy(_raw.get("nodes", []))
|
||||
from graphify.build import legacy_id_source_prefix as _legacy_prefix
|
||||
_legacy_ids = _legacy(
|
||||
_raw.get("nodes", []), source_prefix=_legacy_prefix(gp)
|
||||
)
|
||||
except Exception:
|
||||
_legacy_ids = False
|
||||
_rows = consumed_graph_rows(dict(_raw, links=_raw.get("links", [])))
|
||||
|
||||
+2
-1
@@ -71,7 +71,8 @@ def _read_graph(graph_path: str) -> nx.Graph:
|
||||
data = {**data, "multigraph": True}
|
||||
try:
|
||||
from graphify.build import graph_has_legacy_ids as _legacy
|
||||
if _legacy(data.get("nodes", [])):
|
||||
from graphify.build import legacy_id_source_prefix as _legacy_prefix
|
||||
if _legacy(data.get("nodes", []), source_prefix=_legacy_prefix(safe)):
|
||||
print(
|
||||
"[graphify] note: this graph uses the pre-#1504 node-ID scheme; "
|
||||
"rebuild with `graphify extract --force` for path-qualified IDs.",
|
||||
|
||||
@@ -1528,6 +1528,67 @@ def test_graph_has_legacy_ids_detects_old_scheme():
|
||||
assert graph_has_legacy_ids(go_symbol, root=".") is False
|
||||
|
||||
|
||||
def test_graph_has_legacy_ids_honors_the_scan_root_prefix():
|
||||
"""A subfolder build run from its parent (`graphify update sub`) stores
|
||||
source_file cwd-relative (#2603) but mints ids scan-root-relative. With the
|
||||
marker's prefix the fresh graph is canonical; without it, `pkg_mod` reads as
|
||||
the old one-parent form of `sub/pkg/mod.py` and every query warned."""
|
||||
from graphify.build import graph_has_legacy_ids
|
||||
fresh = [{"id": "pkg_mod", "source_file": "sub/pkg/mod.py", "type": "code", "source_location": "L1"}]
|
||||
assert graph_has_legacy_ids(fresh, root=None) is True # the false positive
|
||||
assert graph_has_legacy_ids(fresh, root=None, source_prefix="sub") is False
|
||||
assert graph_has_legacy_ids(fresh, root=None, source_prefix="sub/") is False
|
||||
# A genuinely pre-#1504 id under the same prefix is still caught.
|
||||
old = [{"id": "v1_readme", "source_file": "sub/docs/v1/README.md", "type": "document", "source_location": "L1"}]
|
||||
assert graph_has_legacy_ids(old, root=None, source_prefix="sub") is True
|
||||
# A source_file outside the prefix is judged as stored.
|
||||
other = [{"id": "docs_v1_readme", "source_file": "docs/v1/README.md", "type": "document", "source_location": "L1"}]
|
||||
assert graph_has_legacy_ids(other, root=None, source_prefix="sub") is False
|
||||
|
||||
|
||||
@pytest.mark.parametrize("marker, expected", [
|
||||
("sub", "sub"),
|
||||
("./a/b/", "a/b"),
|
||||
("a\\b", "a/b"),
|
||||
(".", None),
|
||||
("", None),
|
||||
("../elsewhere", None),
|
||||
("/abs/scan/root", None),
|
||||
("C:\\scan\\root", None),
|
||||
])
|
||||
def test_legacy_id_source_prefix_reads_the_relative_marker(tmp_path, marker, expected):
|
||||
from graphify.build import legacy_id_source_prefix
|
||||
out = tmp_path / "graphify-out"
|
||||
out.mkdir()
|
||||
(out / ".graphify_root").write_text(marker, encoding="utf-8")
|
||||
assert legacy_id_source_prefix(out / "graph.json") == expected
|
||||
|
||||
|
||||
def test_legacy_id_source_prefix_without_marker(tmp_path):
|
||||
from graphify.build import legacy_id_source_prefix
|
||||
assert legacy_id_source_prefix(tmp_path / "graph.json") is None
|
||||
|
||||
|
||||
def test_subfolder_build_from_parent_is_not_flagged_legacy(tmp_path, monkeypatch):
|
||||
"""End to end: `graphify update proj` from proj's parent, then the same
|
||||
probe the query/serve consumers run, must not call the fresh graph legacy."""
|
||||
import json
|
||||
from graphify.build import graph_has_legacy_ids, legacy_id_source_prefix
|
||||
from graphify.watch import _rebuild_code
|
||||
proj = tmp_path / "proj"
|
||||
(proj / "pkg").mkdir(parents=True)
|
||||
(proj / "pkg" / "mod.py").write_text("def a():\n return 1\n", encoding="utf-8")
|
||||
(proj / "main.py").write_text("from pkg.mod import a\n", encoding="utf-8")
|
||||
monkeypatch.chdir(tmp_path)
|
||||
assert _rebuild_code(Path("proj"), no_cluster=True)
|
||||
gp = proj / "graphify-out" / "graph.json"
|
||||
nodes = json.loads(gp.read_text(encoding="utf-8"))["nodes"]
|
||||
files = {n["source_file"] for n in nodes if n.get("source_location") == "L1"}
|
||||
assert "proj/pkg/mod.py" in files # stored cwd-relative (#2603)
|
||||
assert legacy_id_source_prefix(gp) == "proj"
|
||||
assert graph_has_legacy_ids(nodes, source_prefix=legacy_id_source_prefix(gp)) is False
|
||||
|
||||
|
||||
# ── #2408: globally-scoped MCP node ids are not file-stem derived ──────────────
|
||||
|
||||
@pytest.mark.parametrize("mcp_kind, nid", [
|
||||
|
||||
Reference in new issue
Block a user