@@ -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
0 commit comments