Skip to content

Changed-files incremental rebuild drops cross-file dependency edges (hook install / watch affected) #3570

Open

Description

@WallaceScott240

Summary

The changed-files incremental rebuild (watch._rebuild_code(changed_paths=[...])) is not edge-equivalent to a full rebuild. It drops cross-file dependency edges — including edges belonging to files that were never touched — so dependency, dependent and impact queries under-report after an incremental refresh.

This is reachable from two shipped features, not just the internal API:

  • graphify hook install — the post-commit hook calls _rebuild_code(_root, changed_paths=changed, force=_force) (hooks.py:141)
  • graphify watch <path> — calls _rebuild_code(..., changed_paths=merged, ...) (watch.py:1067)

So a repository using the git hooks or the watcher accumulates edge loss on every commit or save, silently. The query most affected is graphify affected "X", since it is reverse traversal over exactly the relations being lost.

graphify update is unaffected: it always calls _rebuild_code(watch_path, force=..., no_cluster=..., block_on_lock=True) with changed_paths left at None (cli.py:2151), i.e. a full corpus rebuild. That is likely why this has gone unnoticed — the CLI never exercises the incremental path.

Version: 0.9.47 (graphifyy 0.9.47), macOS, Python 3.14

How it was measured

The comparison that matters is incremental vs a full rebuild of the same working tree, not incremental vs the pre-edit graph. Comparing against the pre-edit graph only shows that the edited file changed, which is expected and correct.

Procedure, on a copy of a 491-file TypeScript/JavaScript repository (346 files indexed):

  1. _rebuild_code(root, changed_paths=None) → baseline
  2. append one exported function to a single indexed file
  3. _rebuild_code(root, changed_paths=[that_file]) → incremental result
  4. _rebuild_code(root, changed_paths=None, force=True) → correct result for the same tree
  5. diff the edge sets of (3) and (4)

Results

Touched file Edges missing vs full …originating from untouched files
web/components/Workspace.tsx 286 259
web/app/layout.tsx 262 261
web/emails/templates.tsx 263 263

Relation breakdown for the Workspace.tsx case (286 total):

imports_from     258
imports           24
references         3
dynamic_import     1

Two details that should help localise the fix:

  • Zero edges were invented. The incremental result is a strict subset of the full result. That points at a missing re-resolution pass rather than corruption, and suggests the fix is additive.
  • The loss is dominated by edges whose source_file is not the file that changed. Cross-file import resolution looks like a whole-corpus pass that is not re-run for preserved nodes, so their outbound import edges are lost when the graph is reassembled.

Node counts stay almost exactly right (2307 → 2307 in one case, with the new symbol correctly present), which is why the problem is easy to miss: the graph looks healthy by node count and the edited file's own symbols are updated correctly.

Why this matters

The loss is one-directional — it only ever removes dependency edges. So affected and any dependency/dependent query answers "nothing depends on this" about code that does have dependents. For an impact-analysis tool that is the dangerous direction: a change author is told it is safe to change something that is not.

Performance context

The incremental path is genuinely valuable, which is why this seems worth fixing rather than removing:

Duration
Full rebuild 3.93 s
Incremental, no files changed 0.08 s (~49×, byte-identical graph, zero delta)
Incremental, one file changed 1.34–1.46 s (~3×)

The no-change case is exactly correct, so the preservation machinery itself works; it is specifically cross-file relationship re-resolution that is missing.

Expected

Either of:

  1. A changed-files rebuild that re-resolves cross-file relationships for preserved nodes, so the result is edge-equivalent to a full rebuild; or
  2. Documentation that changed_paths is not correctness-preserving for dependency analysis — in which case hook install and watch should probably warn, since they use it by default.

Additionally, it would be useful to expose a correctness-preserving changed-files update through the CLI (e.g. graphify update --changed <paths>), so downstream tools can use the fast path without depending on a private function.

Reproduction

Self-contained script, run with the installed venv's interpreter. It performs the four steps above and exits non-zero while the fast path is lossy:

graphify_incremental_probe.py
import collections, json, pathlib, sys, time


def _graph(out):
    payload = json.loads(out.read_text(encoding="utf-8"))
    nodes = {n["id"]: n for n in payload.get("nodes", [])}
    links = {
        (l["source"], l["target"], l.get("relation")): l
        for l in payload.get("links", payload.get("edges", []))
    }
    return nodes, links


def main():
    if len(sys.argv) != 3:
        print("usage: probe.py <repo-copy> <relative/file.ts>")
        return 2
    from graphify.watch import _rebuild_code

    root = pathlib.Path(sys.argv[1]).resolve()
    target = root / sys.argv[2]
    out = root / "graphify-out" / "graph.json"

    t = time.perf_counter()
    _rebuild_code(root, changed_paths=None, no_cluster=True)
    baseline = time.perf_counter() - t
    bn, bl = _graph(out)
    print(f"1. full baseline   {baseline:6.2f}s nodes={len(bn)} edges={len(bl)}")

    original = target.read_text(encoding="utf-8")
    try:
        target.write_text(original + "\n\nexport function __probe() { return 42; }\n",
                          encoding="utf-8")
        t = time.perf_counter()
        _rebuild_code(root, changed_paths=[target], no_cluster=True)
        inc = time.perf_counter() - t
        in_n, in_l = _graph(out)
        print(f"2. changed-files   {inc:6.2f}s nodes={len(in_n)} edges={len(in_l)}")

        t = time.perf_counter()
        _rebuild_code(root, changed_paths=None, no_cluster=True, force=True)
        full = time.perf_counter() - t
        fu_n, fu_l = _graph(out)
        print(f"3. full, same tree {full:6.2f}s nodes={len(fu_n)} edges={len(fu_l)}")
    finally:
        target.write_text(original, encoding="utf-8")

    lost = {k: v for k, v in fu_l.items() if k not in in_l}
    invented = {k for k in in_l if k not in fu_l}
    touched = str(target.relative_to(root)).replace("\\", "/")
    from_touched = sum(1 for v in lost.values() if v.get("source_file") == touched)

    print(f"\nspeedup {full / inc:.1f}x")
    print(f"edges LOST by the fast path : {len(lost)}")
    print(f"  from the touched file     : {from_touched}")
    print(f"  from untouched files      : {len(lost) - from_touched}")
    print(f"edges INVENTED              : {len(invented)}")
    print("  by relation:",
          collections.Counter(v.get("relation") for v in lost.values()).most_common(6))
    return 1 if lost else 0


if __name__ == "__main__":
    raise SystemExit(main())

Run as:

<venv>/bin/python graphify_incremental_probe.py /path/to/repo-copy web/components/Workspace.tsx

Use a copy — it rewrites the named file and rebuilds graphify-out/ in place.

Sample output:

1. full baseline     4.24s nodes=2307 edges=5072
2. changed-files     1.46s nodes=2307 edges=4787
3. full, same tree   4.55s nodes=2308 edges=5073

speedup 3.1x
edges LOST by the fast path : 286
  from the touched file     : 27
  from untouched files      : 259
edges INVENTED              : 0
  by relation: [('imports_from', 258), ('imports', 24), ('references', 3), ('dynamic_import', 1)]

Happy to test a patch against the same repository if that would help.

Activity

  1. L4XB commented on Sep 15, 2026

    @L4XB
    Contributor

    I could not reproduce this at main (0.9.61), and I think the reason is that it was already fixed between your version and now. Posting the measurements rather than a fix, since the useful next step is probably yours.

    I ran your probe script unmodified against two synthetic corpora, both built to have the shape the numbers point at: cross-file imports_from edges whose source_file is not the file being edited.

    One, a plain import chain. c.ts exports, b.ts imports from it, a.ts imports from both, d.ts imports from a and b. Edited c.ts, the file with the most dependents.

    1. full baseline     0.27s nodes=8  edges=19
    2. changed-files     0.03s nodes=9  edges=20
    3. full, same tree   0.03s nodes=9  edges=20
    edges LOST by the fast path : 0
    edges INVENTED              : 0
    

    Two, the barrel shape a Next.js app has. lib/core.ts exports two symbols, lib/index.ts re-exports them, and 40 app/page*.tsx files import through the barrel. Edited lib/core.ts, so the loss would have to travel core -> barrel -> 40 importers.

    1. full baseline     0.54s nodes=84 edges=246
    2. changed-files     0.07s nodes=85 edges=247
    3. full, same tree   0.29s nodes=85 edges=247
    speedup 4.1x
    edges LOST by the fast path : 0
    edges INVENTED              : 0
    

    Step 3 prints No code-graph changes detected; outputs left untouched in both runs, which is the tool saying the full rebuild produced the same graph the incremental one had already written. The relation mix is right in both: the first corpus carries imports_from, imports, calls and contains, so the probe was looking at the relations your breakdown names.

    What I think happened. 32b209a, "Fix incremental merge of cross-file project stubs", touches graphify/build.py with 270 lines of new tests in tests/test_build.py, and git tag --contains puts it first in v0.9.57. You reported against 0.9.47, so it would not have been in your build. 8a89253 ("Scope deduplication during incremental merges", v0.9.59) is in the same area.

    So this may be closeable. What would settle it:

    • re-run your probe on 0.9.61 against the same 491-file repository, since a synthetic corpus cannot rule out something corpus-shaped that I did not build, or
    • if it still loses edges there, one source_file and one relation from the lost set plus the two files at its ends, which is enough to build a fixture that fails.

    The two suggestions at the end of your report stand either way, and neither depends on the bug still being live: hook install and watch reach _rebuild_code(changed_paths=...) while graphify update does not, so the incremental path ships without a CLI surface that exercises it, and an edge-equivalence test between the two would have caught this class of regression in CI rather than in a user's graph.

  2. ayushcodes10 commented on Sep 16, 2026

    @ayushcodes10
    Contributor

    Confirming @L4XB's analysis independently on current v8 HEAD (post 0.9.63).

    Reproduced the barrel-import shape from this issue directly against _rebuild_code (fresh 12-file tree: lib/core.ts exporting two symbols, lib/index.ts re-exporting them, 10 app/pageN.tsx importers through the barrel):

    baseline edges: 66
    incremental (changed_paths=[lib/core.ts]) edges: 66
    full rebuild, same tree: 66
    edges LOST by the fast path: 0
    edges INVENTED: 0
    

    Both commits L4XB identified as the likely fix are present in v8 history: 32b209a ("Fix incremental merge of cross-file project stubs", touches build.py) and 8a89253 ("Scope deduplication during incremental merges"). No edges from untouched importers were dropped in this repro, matching their synthetic-corpus result.

    Since zero edges were lost in either reproduction and the fix commits predate current v8, I believe this is resolved on current code and safe to close. If it is still reproducible against a real corpus (not a synthetic one), the most useful next step per L4XB's comment stands: one source_file + relation pair from the lost set, plus the two files at its ends, to build a failing fixture.

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