Skip to content

fix(analyze): preserve two-file import cycles in undirected graphs - #4852

Closed
legendteddy wants to merge 1 commit into
Graphify-Labs:v8from
legendteddy:fix/4839-import-cycle
Closed

legendteddy wants to merge 1 commit into
Graphify-Labs:v8from
legendteddy:fix/4839-import-cycle

Conversation

@legendteddy

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes #4839.

The default undirected build intentionally keeps only one same-relation edge for a node pair (#1061). For a bidirectional imports_from / re_exports pair, that meant the reverse runtime import disappeared before find_import_cycles() ran, so a real two-file cycle was reported as None detected.

This keeps the existing undirected graph representation and preserves only the missing fact needed for analysis:

  • when build_from_json() drops a reverse same-relation runtime imports_from / re_exports edge, it stamps the surviving edge with reverse_runtime_import: true;
  • find_import_cycles() restores that reverse runtime direction in its temporary directed file graph;
  • type-only and deferred reverse imports are not stamped as runtime dependencies;
  • a surviving type-only/deferred edge can still carry a real runtime import in the opposite direction without manufacturing a cycle.

No graph schema version or public graph type changes.

Verification & invariant

Invariant: if extraction contains runtime imports A -> B and B -> A, cycle analysis must retain enough information to report the two-file cycle even when the persisted NetworkX graph is undirected.

RED proof on untouched v8 (0a8cd4e1), with the regression test only:

tests/test_type_only_import_cycles.py::test_a_real_runtime_cycle_is_still_reported[False]
FAILED: assert 0 == 1

GREEN:

uv run --frozen pytest tests/test_type_only_import_cycles.py -q
24 passed

uv run --frozen pytest tests/test_build.py tests/test_analyze.py tests/test_type_only_import_cycles.py -q
188 passed

uv run --frozen ruff check graphify/build.py graphify/analyze.py tests/test_type_only_import_cycles.py
All checks passed!

Pyright on graphify/build.py graphify/analyze.py reports 16 existing errors on this branch and the same 16 errors on untouched v8; this change adds no new type errors.

git diff --check passes.

Compatibility / blast radius

  • Narrow: only reverse same-relation imports_from / re_exports edges in an undirected build gain one boolean attribute when the dropped leg is runtime.
  • Directed graphs are unchanged.
  • Type-only and deferred cycle filtering remains intact.
  • The canonical graph representation stays undirected; no migration is required.

AI assistance is disclosed in the commit metadata (Co-Authored-By: ChatGPT <[email protected]>).

When an undirected build collapses a reverse runtime imports_from or re_exports edge, retain a marker on the surviving edge and restore that direction during cycle analysis. This keeps the existing graph representation while making the default report able to detect two-file runtime cycles.\n\nFixes #4839\n\nCo-Authored-By: ChatGPT <[email protected]>
@github-actions

Copy link
Copy Markdown

Thanks for the pull request, @legendteddy. 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 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 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 2 changed function(s) (1 sampled, not proven, 1 not verified).

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

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


Graphify review — findings

Preserves two-file runtime import cycles in undirected builds, where nx.Graph stores only one edge per node pair and previously dropped the reverse leg. When build_from_json discards a reverse imports_from/re_exports edge that is neither type-only nor deferred, it now sets a reverse_runtime_import marker on the surviving edge, without moving the persisted graph to a directed or multigraph form. find_import_cycles reads that marker and adds the file edge in both directions. Deferred and type-only edges are still excluded from cycle detection unless they carry the marker, so a type-only import that shares a pair with a real runtime import in the other direction no longer hides the cycle. Tests now run the real-runtime-cycle case in both directed and undirected modes.

No blocking issues surfaced.

Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 2006 functions depend on the 181 functions this change touches.

Health — grade A; 10 existing hotspot(s) in the area this change touches (pre-existing, not introduced here):

  • _rebuild_code() — 205 callers, 65 callees (high)
  • build_from_json() — 272 callers, 20 callees (high)
  • deduplicate_entities() — 110 callers, 31 callees (high)
  • build_merge() — 101 callers, 16 callees (high)
  • to_json() — 85 callers, 11 callees (high)
  • dispatch_command() — 5 callers, 135 callees (high)
  • to_obsidian() — 43 callers, 14 callees (high)
  • build() — 83 callers, 6 callees (high)
  • …and 2 more

Verification — 2006 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: 1400 function(s) in the blast radius were not formally verified this run

Test selection

Test selection

137 of 469 test file(s) selected (29%) 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_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_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
  • tests/test_file_label_disambiguation.py — impact
  • tests/test_global_add_tag_inference.py — impact
  • tests/test_global_graph.py — impact
  • tests/test_go_qualified_resolution.py — impact
  • … and 87 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

safishamsi added a commit that referenced this pull request Oct 30, 2026
… js imports, extractors, serve/export/pdf)

13 PRs folded into the unreleased 0.9.98; dups #4869/#4852/#4871 to close
at release. One combined _AST_CACHE_SCHEMA bump (29->30) covers the
output-changing extractors and the file/declaration id change.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@safishamsi

Copy link
Copy Markdown
Member

The two-file undirected cycle fix shipped in v0.9.98 via #4841, which also handles the incremental/watch rebuild path (clearing the stamp so a dropped import does not leave a stale cycle). Closing as superseded; thanks @legendteddy.

@safishamsi safishamsi closed this Oct 30, 2026
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