Skip to content

graphify update on a cloned checkout re-extracts every file and rebuilds the whole graph (3 to 7 min, ~6 GB) even with zero changes #4740

Closed

Description

@lavaxun

Use case. One large monorepo, many git worktrees (one per branch / agent). The map is built once on a canonical checkout of main; each worktree gets a copy of graphify-out/graph.json (and manifest.json, and in one run cache/). We wanted each worktree to run graphify update so its map reflects the branch's changes. Correctness was fine: a new file and its function appeared. Cost was the blocker.

Environment. graphifyy 0.9.92 (pipx, Python 3.14.5), macOS, Apple M5 Max, 18 cores, 48 GB. Graph: 184,314 nodes, 463,239 edges, 4,610 communities, graph.json 258 MB, 19,397 code files. AST only (no LLM key set). Command each time: GRAPHIFY_OUT=<worktree>/graphify-out graphify update <worktree>.

run change since the copy wall peak RSS log
1 none 6m 12s 6.3 GB rebuilt, same counts
2 49 files (42 modified/deleted, 1 new) 7m 2s 5.7 GB rebuilt, correct
3 none 3m 12s 6.1 GB AST extraction: 19397/19397 uncached files (100%) then No code-graph topology changes detected; outputs left untouched.
4 1 file, canonical cache/ (5,108 AST entries, 94 MB) copied in with the copy 5m 58s 6.2 GB 19300/19397 uncached files (99%), full rebuild

What looks wrong.

  1. Run 3 and 4: the AST cache misses almost every file on a copied checkout. cache.file_hash documents keys as content + root-relative path for portability across checkout directories (stat-index.json keyed by absolute path and never pruned: 0% cache hits for any root that didn't write it (11,614 entries for 1,130 files across 4 roots) #2199), so a copied cache should hit for unchanged files. Possibly the stat-index fast path misses (different inode/mtime on the copy) and something downstream counts the fallback as "uncached"; we have not traced further.
  2. Run 1 and 4: with no or one changed file, update still re-clusters and rewrites graph.json, graph.html and GRAPH_REPORT.md in full. Run 3 shows a "no topology changes" short-circuit exists; it would help if it also applied before re-extraction, and if a small change patched the graph instead of rebuilding it.

Ask. An incremental update whose cost scales with the changed files: re-extract only files whose content changed, patch their nodes and edges into the existing graph, skip re-clustering and the HTML/report when topology changes are local (or behind flags like --no-html --no-report). Related: #2406 (cross-file edges lost on incremental re-extract), #3763 (AST-only update drops semantic edges).

Happy to run a patched build against this corpus and report timings.

Activity

  1. github-actions commented on Oct 24, 2026

    @github-actions

    Thanks for opening this issue, @lavaxun. A maintainer will take a look soon.

    If you would like to discuss it in real time, come say hi on our Discord server. For longer-form questions and ideas there is also GitHub Discussions.

    To help us triage, please make sure the report includes what you expected, what actually happened, and the steps (and a small sample) to reproduce it.

  2. Deepjyoti-Sarmah commented on Oct 24, 2026

    @Deepjyoti-Sarmah
    Contributor

    I dug into this on 0.9.95 (report is 0.9.92, same behavior). Two separate problems are bundled here, and they have different causes.

    The bigger one is not the copy at all. TS/JS files never touch the AST cache. graphify/extractors/models.py:11 defines _JS_CACHE_BYPASS_SUFFIXES (.js .jsx .mjs .cjs .ts .tsx .mts .cts .vue .svelte .ejs), and extract.py:9420-9425 plus 9022-9043 skip both load_cached and save_cached for them. They get re-parsed on every run, copied cache or not. The bypass is a workaround for a portability bug. JS/TS resolution stamps a resolved target_file on edges (resolution.py:1392), but cache.py:700 only relativizes source_file and definition_file, so a JS/TS entry would replay a foreign absolute path. tests/test_cross_extension_reexport_self_cycle.py:293 documents the workaround. For a TS monorepo that is most of the corpus, which is why uncached_work lands on the total file count.

    The copied cache itself does hit. I don't think the stat-index is the problem: file_hash (cache.py:512-614) salts on the root-relative path, and a stat miss only re-reads the bytes and reproduces the same digest. On a 20 .py + 20 .ts corpus:

    scenario bypass cacheable hits uncached_work
    cold 20 20 0 40
    warm, same dir 20 20 20 20
    cp -r, new mtimes 20 20 20 20
    cp -a, preserved mtimes 20 20 20 20
    copy, renamed namespace 20 20 0 40

    Every cacheable file hit after both copies.

    What zeroes a copied cache is _cleanup_stale_ast_entries (cache.py:86-111), which rmtree's sibling v*/ dirs under cache/ast/. The namespace is v{version}-s{schema}-g{grammar} (cache.py:1040), so a cache copied in from a different graphify version is deleted on first use. That fits run 4: 5,108 entries copied in, yet all 19,397 files read as uncached, which means the cacheable ones missed too.

    Two smaller things. The (100%) in the log is a progress counter over the uncached set. extract.py:9181 prints done_count/len(uncached_work), so the final line is always N/N (100%) and the real total is never shown. And even with a perfect cache the run stays expensive: watch.py goes detect -> extract -> build_from_json -> topology compare at :2362, and only then can short-circuit at :2396. _rebuild_code does support changed_paths (watch.py:1915-1971), but cli.py:2838 never passes it, so update is always full corpus. legacy_layout (watch.py:2208) forces the full path when the copied graph.json is multi-line, which is probably run 1.

    The incremental ask and the cache-hit ask are two different fixes. Happy to help on either.

  3. Deepjyoti-Sarmah commented on Oct 24, 2026

    @Deepjyoti-Sarmah
    Contributor

    Here's how I'll fix this. The report bundles two problems and each needs its own change, so this lands as several PRs rather than one.

    1. Log the real cache hit rate (small, first)
      The (100%) in the output counts progress through the uncached set, not cache
      hits, so the last line always reads N/N ... (100%). The PR prints
      cache_hits/total instead. No behavior change.

    2. Skip the work when nothing changed
      Today update runs detect -> extract -> build -> cluster and only then checks
      for topology changes, so a no-change run still costs 3-6 min. I'll move that
      check before extraction: compare the detected file set, per-file content
      hashes (the manifest already stores them), and the non-code inputs that affect
      resolution (tsconfig, package.json, lockfiles), then exit early when nothing
      moved. Covers runs 1 and 3.

    3. Make update incremental
      _rebuild_code already accepts changed_paths, but update never passes them.
      Wiring that through lets a small edit re-extract only what changed. Bigger,
      lands after 2.

    4. Make the TS/JS cache real
      TS/JS files skip the AST cache because their edges carry a resolved absolute
      target_file, and the cache only relativizes source_file and definition_file, so
      a cached entry would replay a foreign path. Relativizing it is not enough on
      its own: a JS/TS file's result also depends on which siblings exist and what
      tsconfig/package.json say, which a content hash can't see, so a naive cache hit
      goes stale. The sound fix is to cache only the parse output (pure in file
      bytes) and keep resolution in the post-pass that already runs. This one is a
      refactor, so it lands last.

    5. Stop deleting a copied cache without a word
      _cleanup_stale_ast_entries deletes AST entries from other versions and grammar
      sets. That's correct, since a different extractor can't be trusted, but it
      happens silently, which is why run 4 looks like a total miss. The PR logs how
      many entries were discarded and why.

      Each PR runs the full test suite.

  4. safishamsi commented on Oct 26, 2026

    @safishamsi
    Member

    Fixed in v0.9.96 via #4748, now on PyPI (pip install graphifyy==0.9.96). graphify update skips extraction when no relevant inputs changed, so an unchanged cloned checkout no longer rebuilds the whole graph. Thanks for the report, @lavaxun.

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