Skip to content

Commit 5d8d611

Browse files
ousamabenyounessafishamsi
authored andcommitted
fix(cache): surface corrupt semantic cache entries instead of silently missing (#2683)
1 parent a30b56e commit 5d8d611

2 files changed

Lines changed: 59 additions & 2 deletions

File tree

‎graphify/cache.py‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ def _cleanup_stale_ast_entries(ast_base: Path, current_dir: Path) -> None:
8080
# check_semantic_cache can report N to the user (#1939).
8181
_legacy_semantic_hits = 0
8282

83+
# Count of cache entries that failed to parse as JSON this process. A corrupt
84+
# entry is not a miss: left in place it fails on every future run, silently
85+
# re-extracting (and, for semantic kinds, re-billing) the file forever. The
86+
# counter lets check_semantic_cache surface one aggregate warning (#2405).
87+
_corrupt_cache_entries = 0
88+
8389
# Prompt-file fingerprints already computed, keyed by (path, size, mtime_ns) —
8490
# the same stat signature the hash index uses. check_semantic_cache resolves the
8591
# prompt once per FILE in the corpus, so without this a 500-doc run re-reads and
@@ -896,7 +902,7 @@ def load_cached(path: Path, root: Path = Path("."), kind: str = "ast",
896902
``merge_existing``) pass allow_legacy=False.
897903
Returns None if no cache entry or file has changed.
898904
"""
899-
global _legacy_semantic_hits
905+
global _legacy_semantic_hits, _corrupt_cache_entries
900906
location = cache_root if cache_root is not None else root
901907
try:
902908
h = file_hash(path, root, cache_root=cache_root)
@@ -912,7 +918,14 @@ def load_cached(path: Path, root: Path = Path("."), kind: str = "ast",
912918
if entry.exists():
913919
try:
914920
result = json.loads(entry.read_text(encoding="utf-8"))
915-
except (json.JSONDecodeError, OSError):
921+
except json.JSONDecodeError:
922+
# Corrupt entry, not a miss: a truncated write or a bad producer
923+
# (e.g. unescaped Windows backslashes in source_file) leaves JSON
924+
# that fails to parse on every future run, so the file is silently
925+
# re-extracted forever. Count it so the run can report it (#2405).
926+
_corrupt_cache_entries += 1
927+
return None
928+
except OSError:
916929
return None
917930
# A ``partial`` entry was produced from a truncated LLM response and
918931
# covers only part of the file's symbols. Serving it as authoritative
@@ -1154,6 +1167,7 @@ def check_semantic_cache(
11541167
cached_hyperedges: list[dict] = []
11551168
uncached: list[str] = []
11561169
legacy_before = _legacy_semantic_hits
1170+
corrupt_before = _corrupt_cache_entries
11571171

11581172
for fpath in files:
11591173
p = Path(fpath)
@@ -1180,6 +1194,18 @@ def check_semantic_cache(
11801194
stacklevel=2,
11811195
)
11821196

1197+
corrupt = _corrupt_cache_entries - corrupt_before
1198+
if corrupt:
1199+
warnings.warn(
1200+
f"{corrupt} semantic cache entr{'y' if corrupt == 1 else 'ies'} could "
1201+
"not be parsed as JSON and were treated as misses, so those files were "
1202+
"re-extracted. A corrupt entry stays on disk and fails again every run; "
1203+
"run with --force (or GRAPHIFY_FORCE=1) to rewrite them, or clear the "
1204+
"cache to stop paying for the re-extraction (#2405).",
1205+
RuntimeWarning,
1206+
stacklevel=2,
1207+
)
1208+
11831209
return cached_nodes, cached_edges, cached_hyperedges, uncached
11841210

11851211

‎tests/test_cache.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,3 +1455,34 @@ def test_file_hash_fastpath_still_serves_a_settled_file(tmp_path, monkeypatch):
14551455
second = file_hash(f, tmp_path)
14561456
assert second == first
14571457
assert reads == [], "settled file was re-read; the stat fastpath is dead"
1458+
1459+
1460+
def test_corrupt_semantic_entry_warns_and_is_a_miss(tmp_path):
1461+
"""A corrupt (invalid-JSON) cache entry must not be silently swallowed
1462+
(#2405). Left unreported it fails to parse on every future run, re-billing
1463+
the semantic extraction forever with no diagnostic. check_semantic_cache
1464+
treats it as a miss (uncached) AND emits one aggregate warning naming the
1465+
count, mirroring the pre-fingerprint legacy-hit warning."""
1466+
from graphify.cache import (
1467+
check_semantic_cache,
1468+
save_semantic_cache,
1469+
cache_dir,
1470+
)
1471+
1472+
f = tmp_path / "doc.md"
1473+
f.write_text("# Doc\n\nBody.\n")
1474+
save_semantic_cache([{"id": "n", "source_file": "doc.md"}], [], root=tmp_path)
1475+
1476+
# Corrupt the on-disk entry (e.g. an old producer wrote unescaped
1477+
# backslashes, or a partial write left truncated JSON).
1478+
h = file_hash(f, tmp_path)
1479+
entry = cache_dir(tmp_path, "semantic") / f"{h}.json"
1480+
assert entry.exists()
1481+
entry.write_text('{"nodes": [ this is not valid json')
1482+
1483+
with pytest.warns(RuntimeWarning, match="corrupt"):
1484+
nodes, _, _, uncached = check_semantic_cache([str(f)], root=tmp_path)
1485+
1486+
# The corrupt entry is a miss, so the file is re-dispatched for extraction.
1487+
assert nodes == []
1488+
assert uncached == [str(f)]

0 commit comments

Comments
 (0)