Skip to content

fix(watch): re-extract unchanged importers of an added or deleted file - #5635

Closed
0xkaushik-ai wants to merge 1 commit into
Graphify-Labs:v8from
0xkaushik-ai:fix/incremental-added-import-target
Closed

0xkaushik-ai wants to merge 1 commit into
Graphify-Labs:v8from
0xkaushik-ai:fix/incremental-added-import-target

Conversation

@0xkaushik-ai

@0xkaushik-ai 0xkaushik-ai commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #5631. Fixes #5633.

An incremental rebuild re-resolves only the files it re-extracts, and an unchanged importer keeps the edges it got last time. So:

Change (graphify/watch.py)

Such importers are now re-extracted with the change, next to the existing id-collision partners:

  • _import_referrer_partners:
    • deletion: files with an AST edge into a node of the deleted file;
    • addition: files with an AST edge into a sourceless placeholder whose id contains the new file's stem or a parent directory name as whole _ tokens.
  • _with_incremental_partners runs _id_collision_partners and then this pass. _rebuild_code calls the helper, which keeps it within pyright's analysis limit (pyright output on watch.py is unchanged vs v8).

Placeholders don't say whether they stand for an external package or a missing project file, so the name match over-approximates: at worst a few extra files are re-extracted (mostly from the AST cache), never a wrong edge. Deleting a widely imported file re-extracts all its importers, which a full-build-equivalent result needs.

Invariant: an incremental update gives the same nodes and edges as a full build of the same tree.

Tests

tests/test_incremental_import_referrers.py compares an update with a full build for add and delete in Python, TypeScript and Go, plus a TS re-export delete, and checks that an unrelated added file re-extracts nothing extra.

  • On v8 all 8 cases fail; with the fix all 8 pass.
  • Incremental / watch / update suites: 388 passed, 3 skipped.
  • Full suite: 12464 passed. 3 timing tests failed under -n auto (test_css_extractor::test_megabyte_of_css_is_fast, test_graph_context_cache_reload::test_concurrent_first_load_of_new_path_builds_once, test_watch::test_watch_handler_honors_graphifyignore) and pass when run alone.
  • ruff check clean.

Not covered: an existing file gaining a symbol an unchanged importer already asks for by name (from a import f while a.py has no f, then f is added). That needs the importers of every edited file re-resolved, which is the expensive path, so it is left for a separate change.

Overlap: #5634 (fixes #5630) touches different files. #5636 (fixes #5632) also wraps the _id_collision_partners call in _rebuild_code, so whichever lands second needs a small rebase: one wrapper calling both passes. I've checked that combination locally: all three branches merged give 12477 passed, 0 failed.

🤖 Generated with Claude Code

An incremental rebuild re-resolves only the files it re-extracts, and an
unchanged importer keeps the edges it was given when it was last extracted.
Two shapes left the graph different from a full build of the same tree:

* Deleting a file an unchanged file imports: the importer's edges into the
  deleted file's nodes dangled and were dropped. A full build gives it an
  unresolved-import placeholder (`a`, `ref_m`, `go_pkg_ex_p`) and the edge to it.
* Adding a file an unchanged file already imports: the importer stayed on the
  placeholder a full build no longer mints, and never gained the imports and
  calls edges into the new file. Seen for Python, TypeScript and Go.

Re-extract such importers together with the change, next to the existing
id-collision partners. For a deletion they are the files with an AST edge into
a node of the deleted file. For an addition they are the files with an AST edge
into a sourceless placeholder whose id carries the new file's stem or a parent
directory name as whole tokens. Placeholders do not record whether they stand
for an external package or a missing project file, so the name match
over-approximates: it can re-extract a few extra files, never draw a wrong edge.

The two partner passes move into one helper so _rebuild_code stays within
pyright's analysis limit.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

Copy link
Copy Markdown

Thanks for the pull request, @0xkaushik-ai. A maintainer will review it soon.

Want to talk it through while it is in review? Come join us on our Discord server. For longer-form discussion there is also GitHub Discussions.

A couple of things that speed up review: make sure the test suite passes on Python 3.11 and 3.13, and that the change keeps extraction deterministic.

@github-actions github-actions Bot added the contributor Pull request from a returning community contributor label Nov 29, 2026

@graphify-labs graphify-labs Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Graphify review View in Graphify

Nothing blocking · 3 possible issues to check · Merge confidence 5/5

3 possible issues to check · High impact

The update path now widens incremental extraction to include unchanged importers that point to deleted-file nodes or matching unresolved-import placeholders.

3 possible issues we couldn't confirm
  • Stored nodes with a source file outside the project are treated as sourceless placeholders when pulling in importers of an added file · graphify/watch.py:394
  • Parent directory names of an added file match very generic placeholder ids, pulling many unchanged importers into each incremental run · graphify/watch.py:402
  • Bounded-widening test checks a private helper's return value, not that the unrelated importer was left alone · tests/test_incremental_import_referrers.py:152

They don't change the score.

What this review covered
  • Some lookups asked for files or lines that do not exist; the review was told and carried on.
  • 2 of 2 changed files inspected. This is scope, not a correctness guarantee.
Code graph · 849 functions depend on this change
  • 55 of 659 test files are affected by this change.
  • Gate, test list and every flagged function: in the Graphify check.

Reviewed 5dd7117

@safishamsi

Copy link
Copy Markdown
Member

Thanks for this. The deleted-file-importer half here overlaps #5638, which we landed in 0.9.124 — that one covers the same cases plus transitive barrel/template re-extraction and fixes both the extract and update paths (this PR patches only the watch/update path). The unique and valuable part here is the add-importer handling. The blocker on landing it as-is: the ADD matcher builds keys from every parent-directory component (src, lib, core, components...) and matches any placeholder id containing the token, so a common directory name can widen re-extraction to a large number of unrelated files. If you'd like to resubmit just the ADD case — tightened to match on the file stem or the resolved import path, and wired into both call sites (watch.py and the cli.py incremental block) — we'd happily take it. Appreciate the work!

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

Labels

contributor Pull request from a returning community contributor

Projects

None yet

2 participants