Skip to content

fix(build): report two-file import cycles in the default undirected build - #4841

Closed
Mpasha17 wants to merge 1 commit into
Graphify-Labs:v8from
Mpasha17:fix/two-file-import-cycles-4839
Closed

Mpasha17 wants to merge 1 commit into
Graphify-Labs:v8from
Mpasha17:fix/two-file-import-cycles-4839

Conversation

@Mpasha17

Copy link
Copy Markdown
Contributor

Fixes #4839

In the default undirected build, build_from_json drops the reverse-direction duplicate of a same-relation pair (#1061). For imports_from / re_exports that dropped edge is the second leg of a two-file cycle, so find_import_cycles never saw it and the report said "None detected".

What changed:

  • build.py: when the v0.8.22 regression of #760: fresh /graphify build round-trips correct AST output into reversed calls edges (build_from_json -> to_json undirected round-trip) #1061 branch drops a reverse imports_from/re_exports that isn't type_only or deferred, it sets reverse_import: true on the edge it keeps. Same edges as before, first-seen direction still wins. Directed builds never hit this branch.
  • analyze.py: find_import_cycles adds the reverse file leg for a stamped edge. The forward leg still skips type_only/deferred like before.
  • watch.py: the stamp is really the target file's import, so the incremental reconcile treats it that way. If the target file gets re-extracted or deleted, the stamp is cleared (the rebuild adds it back if the import is still there). If only the kept edge's own file is re-extracted, the stamp moves to the fresh edge. Without this, a hook rebuild after b.py stops importing a.py kept reporting the cycle, and editing a.py alone made a real cycle disappear. I also pulled the existing eviction condition into a small _edge_evicted helper so both checks use the same rule.

Tests: new tests/test_undirected_two_file_import_cycle.py covers a TS and a Python two-file cycle in the undirected build, a one-way import, a 3-file ring next to a 2-file cycle, import type / TYPE_CHECKING reverse legs (no cycle), other relations / deferred / type-only reverse edges not stamping, the directed build being unchanged, a graph.json round trip, and the two incremental cases. The existing type-only cycle tests pass unchanged.

Ran the full suite on 3.10, 3.12, 3.13 and 3.14 against unmodified v8. Failures are the same on both trees (yaml extraction on 3.10, safe_fetch network tests, etc.), no new ones. I also ran the real CLI (graphify update . and then graphify cluster-only . from the saved graph.json) on small Python, TS and JS two-file cycle projects. Before: "None detected". After: - 2-file cycle: a.py -> b.py -> a.py (and the same for TS and JS). The one-way, 3-file ring and TYPE_CHECKING projects give the same output as before.

One thing I left alone: an imports_from one way plus a re_exports back is a different-relation collision. The later edge already overwrites the pair there, on v8 too, so that shape still isn't reported. That's separate from this issue. Also, a graph.json written before this change doesn't have the stamp, so cluster-only on it still says "None detected" until the next graphify update.

Written with help from Grok Bot; I reviewed and tested it.

…cted build (Graphify-Labs#4839)

The undirected build drops the reverse-direction duplicate of a same-relation
pair (Graphify-Labs#1061). For imports_from/re_exports that dropped edge is the second leg
of a two-file import cycle, so find_import_cycles never saw it and
GRAPH_REPORT.md said "None detected".

Stamp the kept edge with reverse_import when the dropped leg is a runtime
import, and have find_import_cycles add the reverse file leg for stamped
edges. The incremental reconcile treats the stamp as the target file's import,
so it is cleared or carried along with that file.

Co-Authored-By: Grok Bot
@Mpasha17
Mpasha17 requested a review from safishamsi as a code owner October 28, 2026 15:16
@github-actions

Copy link
Copy Markdown

Thanks for the pull request, @Mpasha17. 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.10 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 Oct 28, 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 reviewed this change.

No issues found by static checks — no coupling regressions or blocking issues in the code graph. That is not the same as safe to merge: see what was not checked.

Not checked: tests were not run; formal verification proved 0 of 3 changed function(s) (1 sampled, not proven, 2 not verified).

Formal verification. PR-changed functions: 1/3 verified (0 proven, 1 may-equivalent, 0 distinguished) · 2 not verified (2 vacuous).

Not verified on this run: build\_from\_json (vacuous: never exercised), \_reconcile\_existing\_graph (vacuous: never exercised).


Graphify review — findings

Fixes two-file import cycles going unreported in the default (undirected) build, where the reverse-direction import was dropped as a duplicate edge. The build now stamps reverse_import on the surviving imports_from/re_exports edge, and find_import_cycles reads it back as an extra file-level edge. Type-only and deferred reverse imports are not stamped, and directed builds are unchanged. Incremental rebuilds in _reconcile_existing_graph treat the stamp as belonging to the target file. It is cleared when that file is re-extracted or evicted, and carried onto the fresh edge when only the edge's own file is re-extracted, so a removed import doesn't leave a stale cycle.

No blocking issues surfaced. 2 lower-confidence candidates did not survive cross-model review.

Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 2140 functions depend on the 274 functions this change touches.

Health — this change adds coupling hotspots:

  • worse: _rebuild_code() — 207 callers, 65 callees
  • worse: build_from_json() — 277 callers, 20 callees
  • worse: to_json() — 87 callers, 11 callees
  • worse: _reconcile_existing_graph() — 1 callers, 13 callees

Verification — 2140 functions in the blast radius were not formally verified this run (proofs are advisory here).

Gate & verification

graphify gate

PASS — objectively clean (no health regressions, tests not run — proofs not run this pass (advisory)). Grounded, not self-assessed.

Advisory (not blocking):

  • verification_scope: 1558 function(s) in the blast radius were not formally verified this run

Test selection

Test selection

157 of 470 test file(s) selected (33%) via static blast radius.

  • tests/test_affected_cli.py — impact
  • tests/test_affected_path_file_node.py — impact
  • tests/test_alias_folding.py — impact
  • tests/test_analyze.py — impact
  • tests/test_atomic_canvas_export.py — impact
  • tests/test_atomic_writes.py — impact
  • tests/test_backend_opt_in.py — impact
  • tests/test_benchmark.py — impact
  • tests/test_benchmark_raw_graph.py — impact
  • tests/test_benchmark_real_query.py — impact
  • tests/test_build.py — impact
  • tests/test_build_located_semantic_identity.py — impact
  • tests/test_build_merge_dedup_scope.py — impact
  • tests/test_build_merge_hyperedges_and_prune.py — impact
  • tests/test_build_merge_shrink_guard.py — impact
  • tests/test_c_include_resolution.py — impact
  • tests/test_cache_stale_import_target.py — impact
  • tests/test_carried_hyperedge_remap.py — impact
  • tests/test_charmap_encoding.py — impact
  • tests/test_chunking.py — impact
  • tests/test_claude_cli_backend.py — impact
  • tests/test_cli_export.py — impact
  • tests/test_cluster.py — impact
  • tests/test_community_labels_skill.py — impact
  • tests/test_compact_graph_json.py — impact
  • tests/test_confidence.py — impact
  • tests/test_corrupt_graph_json.py — impact
  • tests/test_cpp_objc_cross_file_calls.py — impact
  • tests/test_cross_extension_reexport_self_cycle.py — impact
  • tests/test_cross_repo_external_call_guards.py — impact
  • tests/test_csharp_object_creation.py — impact
  • tests/test_dedup.py — impact
  • tests/test_dedup_remaps_hyperedges.py — impact
  • tests/test_dedup_shrink_refuses_force_write.py — impact
  • tests/test_dedup_survivor_richness.py — impact
  • tests/test_definition_file_portability.py — impact
  • tests/test_duplicate_annotation_edges.py — impact
  • tests/test_elixir_import_resolution.py — impact
  • tests/test_elixir_qualified_calls.py — impact
  • tests/test_elixir_unqualified_call_scope.py — impact
  • tests/test_evidence_binding.py — impact
  • tests/test_export.py — impact
  • tests/test_export_control_characters.py — impact
  • tests/test_export_direction.py — impact
  • tests/test_export_idempotent_writes.py — impact
  • tests/test_export_path_length.py — impact
  • tests/test_ext_tagged_id_clash.py — impact
  • tests/test_external_stub_endpoints.py — impact
  • tests/test_extract.py — impact
  • tests/test_falkordb_integration.py — impact
  • … and 107 more

Selection is safe under the controlled-regression assumption; always-run tests + a periodic full run are the backstops. Advisory — it never changes the check verdict.

Formal verification

No difference found (not proven): No behavior difference found in find\_import\_cycles (not a proof).

The verifier ran both versions of find\_import\_cycles on many inputs and saw identical behavior every time. Strong evidence the change is safe, but evidence, not a proof.

Guarantee: Empirical: differential testing (both versions run on many generated inputs). A divergence on an untested input remains possible, so this is 'no counterexample found', not 'proven equivalent'.

Note: An input the sampler did not try could still differ.

Could not verify: Could not verify build\_from\_json.

The verifier did not have enough to check build\_from\_json, so it is saying so rather than guessing. No false assurance is the whole point.

Guarantee: No guarantee either way, this is an honest abstention, not a pass.

Note: Reason: no capturable inputs from the test suite; property tier: not verifiable: all 11 sampled inputs raised on both versions — the function never executed, so 'no divergence' would be vacuous

Could not verify: Could not verify \_reconcile\_existing\_graph.

The verifier did not have enough to check \_reconcile\_existing\_graph, so it is saying so rather than guessing. No false assurance is the whole point.

Guarantee: No guarantee either way, this is an honest abstention, not a pass.

Note: Reason: no capturable inputs from the test suite; property tier: not verifiable: all 24 sampled inputs raised on both versions — the function never executed, so 'no divergence' would be vacuous (mostly TypeError — names the real obstacle, not a sampling gap)

· 4 more finding(s) on lines outside this diff (see the check run).

@safishamsi

Copy link
Copy Markdown
Member

Landed in v0.9.98, now on PyPI (pip install graphifyy==0.9.98). A two-file import cycle is reported in the default undirected build, with the stamp cleared correctly on incremental rebuilds. Thanks for the fix, @Mpasha17.

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

Development

Successfully merging this pull request may close these issues.

Import Cycles reports "None detected" for every two-file import cycle in the default (undirected) build

2 participants