fix(cache): treat an AST hit whose import target is gone as a miss
An AST cache entry is keyed by its own file's content, so after
pkg/slice.py is renamed or deleted, the unchanged importer pkg/user.py
is a hit and replays its import edges resolved while slice.py existed:
a target id minted from the absolute path and a target_file stamp to
the old file. extract() maps a stamped target to a portable id only
when it exists, so the edge kept the absolute-path id and the #2873
stub was minted with it: graph.json held the checkout folder and OS
username, and a warm update differed from a cold build (pkg_slice).
save_cached now records the target_file stamps that name an existing
file when an AST entry is written (made portable like the entry's
ids). load_cached returns a miss when any recorded file is gone, so
the importer is extracted again exactly as on a cold build. Stamps
that did not exist at write time are not recorded and never cause a
miss; entries without the list load as before.
A no-change warm rebuild of graphify's own repo has the same 16 cache
misses as before, and its cold build is unchanged.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 48578d04f1)
This commit is contained in:
1 parent
7aab2ed7ac
commit
304d71592d
2 files changed
+150
No files matched your search
@@ -1105,10 +1105,49 @@ def load_cached(path: Path, root: Path = Path("."), kind: str = "ast",
|
||||
# id-remap cannot fix because they match none of its current-path
|
||||
# keys. Order is free — source_file never carries the marker.
|
||||
_absolutize_ids_in(result, path, root)
|
||||
targets = result.pop(_CACHED_TARGETS_KEY, None)
|
||||
if isinstance(targets, list) and not all(
|
||||
_target_file_present(t, root) for t in targets if isinstance(t, str)
|
||||
):
|
||||
return None
|
||||
return result
|
||||
return None
|
||||
|
||||
|
||||
# Cross-file edges (imports, re-exports, links) carry a ``target_file`` stamp
|
||||
# naming the file they resolved to. An AST entry is keyed by its own file's
|
||||
# content only, so a hit replays that resolution after the target is renamed or
|
||||
# deleted: the edge keeps the id minted from the target's absolute path, which
|
||||
# extract() maps to a portable id only for targets that exist, so the checkout
|
||||
# path reached graph.json and a warm build differed from a cold one. The entry
|
||||
# records the targets that existed when it was written; a hit whose recorded
|
||||
# target is gone is a miss and the file is extracted again.
|
||||
_CACHED_TARGETS_KEY = "_cached_target_files"
|
||||
|
||||
|
||||
def _target_file_present(target: str, root: Path) -> bool:
|
||||
candidate = Path(target)
|
||||
if not candidate.is_absolute():
|
||||
candidate = Path(root) / candidate
|
||||
try:
|
||||
return candidate.is_file()
|
||||
except (OSError, ValueError):
|
||||
return False
|
||||
|
||||
|
||||
def _present_target_files(result: dict, root: Path) -> list[str]:
|
||||
"""The ``target_file`` stamps in ``result`` that name an existing file."""
|
||||
present: set[str] = set()
|
||||
for edge in result.get("edges", []) or []:
|
||||
if not isinstance(edge, dict):
|
||||
continue
|
||||
target = edge.get("target_file")
|
||||
if isinstance(target, str) and target and target not in present:
|
||||
if _target_file_present(target, root):
|
||||
present.add(target)
|
||||
return sorted(present)
|
||||
|
||||
|
||||
def save_cached(path: Path, result: dict, root: Path = Path("."), kind: str = "ast",
|
||||
cache_root: Path | None = None, prompt: "str | Path | None" = None,
|
||||
prompt_file: "str | Path | None" = None) -> None:
|
||||
@@ -1155,6 +1194,10 @@ def save_cached(path: Path, result: dict, root: Path = Path("."), kind: str = "a
|
||||
if isinstance(result, dict):
|
||||
import copy as _copy
|
||||
on_disk = _copy.deepcopy(result)
|
||||
if kind == "ast":
|
||||
targets = _present_target_files(on_disk, root)
|
||||
if targets:
|
||||
on_disk[_CACHED_TARGETS_KEY] = targets
|
||||
_relativize_source_files_in(on_disk, root)
|
||||
# Then replace the absolute root inside the ids and remaining paths, so
|
||||
# the entry replays portably under any root (#2257). Strictly after the
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
"""An AST cache hit must not replay an import resolved to a file that is gone.
|
||||
|
||||
A cache entry is keyed by its own file's content, so an unchanged importer kept
|
||||
its import edge to a module after that module was renamed or deleted. The edge
|
||||
target was the id minted from the old file's absolute path, which extract() maps
|
||||
to a portable id only for targets that exist, so the checkout path reached
|
||||
graph.json and a warm update differed from a cold build of the same tree.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from graphify.cache import load_cached, save_cached
|
||||
|
||||
|
||||
def _write(path: Path, text: str) -> Path:
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_text(text, encoding="utf-8")
|
||||
return path
|
||||
|
||||
|
||||
def _import_result(importer: Path, target: Path) -> dict:
|
||||
return {
|
||||
"nodes": [{"id": "pkg_user", "source_file": str(importer)}],
|
||||
"edges": [{
|
||||
"source": "pkg_user", "target": "x", "relation": "imports_from",
|
||||
"source_file": str(importer), "target_file": str(target),
|
||||
}],
|
||||
}
|
||||
|
||||
|
||||
def test_hit_is_a_miss_once_a_recorded_import_target_is_gone(tmp_path: Path):
|
||||
importer = _write(tmp_path / "pkg" / "user.py", "from pkg.slice import cut\n")
|
||||
target = _write(tmp_path / "pkg" / "slice.py", "def cut(p):\n return p\n")
|
||||
save_cached(importer, _import_result(importer, target), root=tmp_path, kind="ast")
|
||||
|
||||
hit = load_cached(importer, root=tmp_path, kind="ast")
|
||||
assert hit is not None
|
||||
assert "_cached_target_files" not in hit, "bookkeeping must not reach extraction results"
|
||||
|
||||
target.rename(tmp_path / "pkg" / "slicing.py")
|
||||
assert load_cached(importer, root=tmp_path, kind="ast") is None
|
||||
|
||||
(tmp_path / "pkg" / "slicing.py").rename(target)
|
||||
assert load_cached(importer, root=tmp_path, kind="ast") is not None
|
||||
|
||||
|
||||
def test_target_missing_when_written_does_not_invalidate(tmp_path: Path):
|
||||
"""Extractors may stamp a speculative target that does not exist yet; such an
|
||||
entry stays a hit, as before."""
|
||||
importer = _write(tmp_path / "pkg" / "user.py", "from .later import f\n")
|
||||
save_cached(importer, _import_result(importer, tmp_path / "pkg" / "later.py"), root=tmp_path, kind="ast")
|
||||
assert load_cached(importer, root=tmp_path, kind="ast") is not None
|
||||
|
||||
|
||||
def test_recorded_targets_are_portable_across_roots(tmp_path: Path):
|
||||
a = tmp_path / "a"
|
||||
importer = _write(a / "pkg" / "user.py", "from pkg.slice import cut\n")
|
||||
target = _write(a / "pkg" / "slice.py", "def cut(p):\n return p\n")
|
||||
save_cached(importer, _import_result(importer, target), root=a, kind="ast", cache_root=tmp_path)
|
||||
|
||||
b = tmp_path / "b"
|
||||
shutil.copytree(a, b)
|
||||
assert load_cached(b / "pkg" / "user.py", root=b, kind="ast", cache_root=tmp_path) is not None
|
||||
(b / "pkg" / "slice.py").unlink()
|
||||
assert load_cached(b / "pkg" / "user.py", root=b, kind="ast", cache_root=tmp_path) is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize("change", ["rename", "delete"])
|
||||
def test_warm_update_after_removing_an_imported_module_matches_a_cold_build(tmp_path: Path, change: str):
|
||||
from graphify.watch import _rebuild_code
|
||||
|
||||
def corpus(root: Path) -> Path:
|
||||
_write(root / "pkg" / "__init__.py", "")
|
||||
_write(root / "pkg" / "slice.py", "def cut(p):\n return p\n")
|
||||
_write(root / "pkg" / "user.py", "from pkg.slice import cut\nimport pkg.slice\n\n\ndef go(p):\n return cut(p)\n")
|
||||
return root
|
||||
|
||||
def remove(root: Path) -> None:
|
||||
if change == "rename":
|
||||
(root / "pkg" / "slice.py").rename(root / "pkg" / "slicing.py")
|
||||
else:
|
||||
(root / "pkg" / "slice.py").unlink()
|
||||
|
||||
def graph(root: Path) -> tuple[set[str], set[tuple[str, str, str]]]:
|
||||
data = json.loads((root / "graphify-out" / "graph.json").read_text(encoding="utf-8"))
|
||||
return (
|
||||
{n["id"] for n in data["nodes"]},
|
||||
{(e["source"], e["target"], e["relation"]) for e in data["links"]},
|
||||
)
|
||||
|
||||
warm = corpus(tmp_path / "checkout_warm" / "proj")
|
||||
assert _rebuild_code(warm, no_cluster=True, acquire_lock=False) is True
|
||||
remove(warm)
|
||||
assert _rebuild_code(warm, no_cluster=True, force=True, acquire_lock=False) is True
|
||||
|
||||
cold = corpus(tmp_path / "checkout_cold" / "proj")
|
||||
remove(cold)
|
||||
assert _rebuild_code(cold, no_cluster=True, acquire_lock=False) is True
|
||||
|
||||
warm_ids, warm_edges = graph(warm)
|
||||
assert not [i for i in warm_ids if "checkout_warm" in i], "a node id embeds the checkout path"
|
||||
assert (warm_ids, warm_edges) == graph(cold)
|
||||
Reference in new issue
Block a user