Repository navigation
fix(analyze): preserve two-file import cycles in undirected graphs - #4852
legendteddy wants to merge 1 commit into
Conversation
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]>
|
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. |
There was a problem hiding this comment.
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— impacttests/test_affected_path_file_node.py— impacttests/test_alias_folding.py— impacttests/test_analyze.py— impacttests/test_atomic_canvas_export.py— impacttests/test_atomic_writes.py— impacttests/test_backend_opt_in.py— impacttests/test_benchmark.py— impacttests/test_benchmark_raw_graph.py— impacttests/test_benchmark_real_query.py— impacttests/test_build.py— impacttests/test_build_located_semantic_identity.py— impacttests/test_build_merge_dedup_scope.py— impacttests/test_build_merge_hyperedges_and_prune.py— impacttests/test_build_merge_shrink_guard.py— impacttests/test_c_include_resolution.py— impacttests/test_carried_hyperedge_remap.py— impacttests/test_charmap_encoding.py— impacttests/test_chunking.py— impacttests/test_claude_cli_backend.py— impacttests/test_cli_export.py— impacttests/test_cluster.py— impacttests/test_community_labels_skill.py— impacttests/test_compact_graph_json.py— impacttests/test_confidence.py— impacttests/test_corrupt_graph_json.py— impacttests/test_cpp_objc_cross_file_calls.py— impacttests/test_cross_extension_reexport_self_cycle.py— impacttests/test_cross_repo_external_call_guards.py— impacttests/test_csharp_object_creation.py— impacttests/test_dedup.py— impacttests/test_dedup_remaps_hyperedges.py— impacttests/test_dedup_shrink_refuses_force_write.py— impacttests/test_dedup_survivor_richness.py— impacttests/test_definition_file_portability.py— impacttests/test_duplicate_annotation_edges.py— impacttests/test_evidence_binding.py— impacttests/test_export.py— impacttests/test_export_control_characters.py— impacttests/test_export_direction.py— impacttests/test_export_idempotent_writes.py— impacttests/test_export_path_length.py— impacttests/test_ext_tagged_id_clash.py— impacttests/test_external_stub_endpoints.py— impacttests/test_extract.py— impacttests/test_falkordb_integration.py— impacttests/test_file_label_disambiguation.py— impacttests/test_global_add_tag_inference.py— impacttests/test_global_graph.py— impacttests/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
… 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]>
|
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. |
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_exportspair, that meant the reverse runtime import disappeared beforefind_import_cycles()ran, so a real two-file cycle was reported asNone detected.This keeps the existing undirected graph representation and preserves only the missing fact needed for analysis:
build_from_json()drops a reverse same-relation runtimeimports_from/re_exportsedge, it stamps the surviving edge withreverse_runtime_import: true;find_import_cycles()restores that reverse runtime direction in its temporary directed file graph;No graph schema version or public graph type changes.
Verification & invariant
Invariant: if extraction contains runtime imports
A -> BandB -> 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:GREEN:
Pyright on
graphify/build.py graphify/analyze.pyreports 16 existing errors on this branch and the same 16 errors on untouchedv8; this change adds no new type errors.git diff --checkpasses.Compatibility / blast radius
imports_from/re_exportsedges in an undirected build gain one boolean attribute when the dropped leg is runtime.AI assistance is disclosed in the commit metadata (
Co-Authored-By: ChatGPT <[email protected]>).