Remove orphaned semantic cache entries, returning the count pruned. The semantic cache is content-hash-keyed (``{file_hash}.json`` under ``cache/semantic/``) and deliberately UNVERSIONED — entries are produced by the LLM from file contents, so invalidating them on every release would
(root: Path, live_hashes: set[str])
| 1904 | |
| 1905 | |
| 1906 | def prune_semantic_cache(root: Path, live_hashes: set[str]) -> int: |
| 1907 | """Remove orphaned semantic cache entries, returning the count pruned. |
| 1908 | |
| 1909 | The semantic cache is content-hash-keyed (``{file_hash}.json`` under |
| 1910 | ``cache/semantic/``) and deliberately UNVERSIONED — entries are produced by |
| 1911 | the LLM from file contents, so invalidating them on every release would |
| 1912 | re-bill extraction. Because it is unversioned it is also never swept by the |
| 1913 | AST version-cleanup, so every content change or file deletion leaves a |
| 1914 | permanent orphan entry that accumulates unbounded. |
| 1915 | |
| 1916 | This sweeps ``cache/semantic/**/*.json`` AND ``cache/semantic-deep/**/*.json`` |
| 1917 | (the ``--mode deep`` namespace, #1894) and deletes any entry whose stem |
| 1918 | (the content hash) is not in ``live_hashes`` — the hashes of the current |
| 1919 | live document set. Both namespaces are pruned against the SAME live set: |
| 1920 | liveness is content-based and mode-independent, so a hash that is live for |
| 1921 | one namespace is live for both. Skipping the deep namespace would re-grow |
| 1922 | the unbounded-orphan problem this function fixed (#1527). ``*.tmp`` |
| 1923 | atomic-write temporaries are skipped, and only these directories are |
| 1924 | touched (never ``cache/ast/**`` or anything else). The unversioned design |
| 1925 | is preserved: we prune by liveness, not by version. |
| 1926 | |
| 1927 | The sweep recurses into the per-prompt-fingerprint subdirs (#1939) for the |
| 1928 | same reason it covers the deep namespace: a glob that stopped at the top |
| 1929 | level would leave every fingerprinted entry permanently unprunable. Entries |
| 1930 | under a fingerprint other than the current one are pruned by liveness only, |
| 1931 | never swept wholesale the way :func:`_cleanup_stale_ast_entries` sweeps old |
| 1932 | AST versions — two hosts with different prompts (verbose vs compact |
| 1933 | extraction-spec) can share one graphify-out/, and a wholesale sweep would |
| 1934 | have each run delete the other's entries and re-bill extraction on every |
| 1935 | alternation. Liveness keeps the total bounded by live docs × prompts seen. |
| 1936 | |
| 1937 | Best-effort, mirroring :func:`_cleanup_stale_ast_entries`: each unlink is |
| 1938 | wrapped in ``try/except OSError`` and a failure is ignored. The worst-case |
| 1939 | failure mode is benign — a surviving orphan costs only one re-extraction of |
| 1940 | one doc on a future run, never incorrect output. |
| 1941 | """ |
| 1942 | _out = Path(_GRAPHIFY_OUT) |
| 1943 | base = _out if _out.is_absolute() else Path(root).resolve() / _out |
| 1944 | pruned = 0 |
| 1945 | for kind in ("semantic", "semantic-deep"): |
| 1946 | semantic_dir = base / "cache" / kind |
| 1947 | if not semantic_dir.is_dir(): |
| 1948 | continue |
| 1949 | for entry in semantic_dir.glob("**/*.json"): |
| 1950 | if entry.stem in live_hashes: |
| 1951 | continue |
| 1952 | try: |
| 1953 | entry.unlink() |
| 1954 | pruned += 1 |
| 1955 | except OSError: |
| 1956 | pass |
| 1957 | return pruned |
| 1958 | |
| 1959 | |
| 1960 | def check_semantic_cache( |