Skip to content

Semantic cache never hits: save_semantic_cache silently drops entries when source_file is corpus-root-relative #2314

Open

Description

@MesmericDigital

Summary

save_semantic_cache() (graphify/cache.py) silently fails to save almost all entries when semantic extraction results carry source_file paths that are relative to the scanned corpus root rather than the process cwd. The result: the semantic cache never hits, and every rebuild re-pays the LLM for the same unchanged documents.

Observed on graphifyy 0.8.14 (macOS, Python 3.13, Gemini backend): after three extraction runs covering 126 markdown docs, graphify-out/cache/semantic/ contained 2 entries, and check_semantic_cache() reported 0 hits on every subsequent run.

Root cause

save_semantic_cache() groups results by each item's source_file and then resolves relative paths against root (default Path(".") = cwd):

p = Path(fpath)
if not p.is_absolute():
    p = Path(root) / p
if p.is_file():          # <- False for corpus-root-relative paths
    save_cached(...)     #    -> entry silently skipped

The LLM extraction prompt asks the model for "source_file": "relative/path", and the emitted paths are relative to the scanned folder (e.g. docs/spec.md for a corpus at some/nested/plugin/). When the scanned folder is not the cwd (the common case for graphify extract <path> pipelines or any monorepo layout), Path(cwd)/docs/spec.md does not exist, p.is_file() is False, and the entry is dropped with no warning. check_semantic_cache() then hashes the real absolute paths, finds no entries, and returns everything as uncached.

Second, related gap

Files that are extracted but yield zero nodes/edges never get a cache entry at all (there is no source_file to group by), so they are re-sent to the LLM on every run forever. In our corpus that was 39 of 57 docs — the majority.

Suggested fixes

  1. In save_semantic_cache(), resolve source_file against the corpus root (pass it through from the extraction call) as well as cwd, and log a warning when an entry cannot be resolved instead of skipping silently.
  2. Accept the list of extracted files as a parameter and write an explicit empty {"nodes": [], "edges": [], "hyperedges": []} entry for any extracted file that produced no output, so check_semantic_cache() can skip it next run.
  3. Optionally: have extract_corpus_parallel() normalize source_file on results to corpus-root-relative before returning, so cache keys and graph provenance agree regardless of what the model emitted.

We worked around it downstream with exactly (1) + (2) and the cache went from 0/57 to 56/56 hits, so the fix shape is confirmed to work. Happy to provide more detail if useful — and thanks for graphify, it's been genuinely great for repo mapping.

Activity

  1. ayushcodes10 commented on Sep 16, 2026

    @ayushcodes10
    Contributor

    The main defect here (root cause #1: source_file resolved against cwd instead of the corpus root) is already fixed on current v8. Reproduced the issue's own scenario directly:

    root = Path(td) / "some" / "nested" / "plugin"
    (root / "docs" / "spec.md").write_text("# Spec\n")
    nodes = [{"id": "n1", "label": "Spec", "source_file": "docs/spec.md"}]
    saved = save_semantic_cache(nodes, [], root=root)
    # saved == 1
    cached = load_cached(root / "docs" / "spec.md", root=root, kind="semantic")
    # entry reads back correctly

    This works because save_semantic_cache/load_cached now take an explicit root= that resolves source_file against the corpus root rather than cwd, which the real CLI/skill pipeline threads through (this area was also touched by an adjacent same-day fix I opened, #2973, for a different gap in the same function).

    On suggested fix #2 (cache an explicit empty entry for a file that yields zero nodes/edges): current code deliberately does NOT do this, and I don't think it should — save_semantic_cache's own docstring cites #2927: an empty/edge-only result is treated as an extraction MISS on purpose, specifically so a file that failed to extract (LLM returned nothing due to an error) gets retried on the next run instead of being cached as "correctly extracted, nothing here" forever. That's a considered design choice, not the bug this issue is about, so leaving it as-is rather than proposing a change there.

    Closing out the main report; happy to hear if you still see the corpus-root-relative drop reproduce on current code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions