fix(build): exclude global MCP ids from legacy-id detection (#2408)

This commit is contained in:
Aryan
2026-08-13 14:17:42 +01:00
committed by safishamsi
parent a4e8446479
commit 48fbe30ebc
2 changed files with 87 additions and 2 deletions
+59
View File
@@ -1302,6 +1302,65 @@ def test_graph_has_legacy_ids_detects_old_scheme():
assert graph_has_legacy_ids(go_symbol, root=".") is False
# ── #2408: globally-scoped MCP node ids are not file-stem derived ──────────────
@pytest.mark.parametrize("mcp_kind, nid", [
("mcp_command", "mcp_command_npx"),
("mcp_package", "mcp_package_google_cloud_cloud_run_mcp"),
("env_var", "env_var_google_cloud_project"),
])
def test_graph_has_legacy_ids_ignores_global_mcp_ids(mcp_kind, nid):
"""MCP ingest stamps every node with L1 (JSON has no line info), so global ids
would otherwise be read as file-level. Under `sub/.mcp.json` the old bare stem
is `mcp`, which these ids legitimately start with (#2408)."""
from graphify.build import graph_has_legacy_ids
node = {
"id": nid,
"source_file": "sub/.mcp.json",
"source_location": "L1",
"metadata": {"mcp_kind": mcp_kind},
}
assert graph_has_legacy_ids([node], root=".") is False
def test_graph_has_legacy_ids_still_checks_file_scoped_mcp_nodes():
"""The exemption is narrow: file-derived MCP kinds stay under detection, and a
missing/malformed metadata blob doesn't exempt anything (or crash)."""
from graphify.build import graph_has_legacy_ids
for kind in ("mcp_config_file", "mcp_server"):
stale = {"id": "mcp_mcp_server_x", "source_file": "sub/.mcp.json",
"source_location": "L1", "metadata": {"mcp_kind": kind}}
assert graph_has_legacy_ids([stale], root=".") is True
for meta in (None, "not-a-dict", {}, {"mcp_kind": None}):
stale = {"id": "mcp_mcp_server_x", "source_file": "sub/.mcp.json",
"source_location": "L1", "metadata": meta}
assert graph_has_legacy_ids([stale], root=".") is True
@pytest.mark.parametrize("mcp_dir", ["", "sub"])
def test_fresh_mcp_graph_is_not_flagged_legacy(tmp_path, monkeypatch, mcp_dir):
"""End-to-end: a freshly extracted graph containing a .mcp.json — nested or at
the repo root — must not nudge the user to rebuild (#2408)."""
from graphify.build import graph_has_legacy_ids
from graphify.extract import extract
(tmp_path / "main.py").write_text("def main():\n return 1\n")
mcp_parent = tmp_path / mcp_dir if mcp_dir else tmp_path
mcp_parent.mkdir(parents=True, exist_ok=True)
(mcp_parent / ".mcp.json").write_text(json.dumps({"mcpServers": {"cloud-run": {
"command": "npx",
"args": ["-y", "@google-cloud/cloud-run-mcp"],
"env": {"GOOGLE_CLOUD_PROJECT": "x"},
}}}))
monkeypatch.chdir(tmp_path)
rel = Path(mcp_dir, ".mcp.json") if mcp_dir else Path(".mcp.json")
result = extract([Path("main.py"), rel], root=Path("."), parallel=False)
ids = {n["id"] for n in result["nodes"]}
assert "mcp_command_npx" in ids # guard: the ingest actually ran
assert graph_has_legacy_ids(result["nodes"], root=".") is False
def test_semantic_rekey_relative_vs_absolute_source_file():
"""Re-key contract: a relative source_file is migrated; an absolute one is left
untouched (it can't be relativized, so its on-disk path must not leak into IDs)."""