Skip to content

pr_check: no new import cycle between Zig modules; inherited cycles warn - #4890

Merged
ashhart merged 6 commits into
ashhart:mainfrom
jschmied:tools/structure-cycle-check
Nov 25, 2026
Merged

ashhart merged 6 commits into
ashhart:mainfrom
jschmied:tools/structure-cycle-check

Conversation

@jschmied

@jschmied jschmied commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Refs #4804. The cycle checker only, as asked in #4804 (comment): no cross-family check and no dependency cleanup here.

Head: 7ad6923cf1, four commits on ef4dd750b4 (891aebed70, then the review rules from #4804 (comment) in e53b826cf6, then the multiline @import( fix from #4890 (comment) in 3b1dc7c0a1, then the scoped module-variable binding from #4890 (comment)).

What it checks

tools/zig/structure_check.py reads the @import graph of zig/src from file text: no build, no index, no checkout of the base. tools/pr_check.py runs it on the base text it already reads and on the working tree, the same way it runs lean.

  • Module: zig/src/<dir>, zig/src/families/<name>, or a file directly under zig/src as its own module. An edge A -> B exists when a file of A imports a file of B.
  • Relative imports resolve against the importing file. A path outside zig/src is not a module; a missing path inside it is listed as unresolved.
  • Named imports resolve from literal evidence only: an addImport("name", ...) in build.zig or zig/build/*.zig whose argument names a "zig/src/....zig" root, directly or through a const x = b.createModule(...) in the same file, or a zig/src/<name> directory. A module variable binds in its own scope: a declaration counts inside its innermost {...} block and after it, so two build helpers that reuse one variable name keep their own modules, in either order. A reassignment adds its roots to the variable it rebinds; a use with no binding in scope (a parameter, a helper's return value) takes the file's declarations of that name. Roots in two modules make the name ambiguous ((two modules), unresolved), never the last binding. A name with roots in two modules, or no root at all (generated modules, roots passed through functions), is listed as unresolved and stays out of the graph. std and builtin are skipped. Whitespace, newlines and a trailing comma inside @import( ... ) are read like the single-line spelling. // comments and \\ string lines are not read.
  • Cycles: strongly connected components of the module graph (Tarjan, iterative). A cycle edge is a module edge whose two ends are in the same component.

The ratchet

The cycle edges of the base are the module edges inside its cycles. An edge on the head is new when it is a cycle edge on the head but not on the base.

  • ERROR, a cycle the PR closes on its own. Take the head graph and leave out the base's cycle edges. A new edge that is still inside a cycle there is an error, and the line lists that cycle's modules. The line also says whether the PR added the import (new import) or an existing import now closes a cycle (existing import).
  • WARN, an edge that only joins the inherited cycle. For example, a new family that native imports, and that imports core, joins the inherited cycle through core's upward edges. That is one WARN per edge, not an error.
  • ERROR, any module core newly imports. Core imports cuda, fabric, families/flashnext, metal and platform on main. A new core -> hip errors, even though hip is already in the inherited cycle and the first rule alone would miss it.
  • WARN, one line for each head cycle with no new edge: inherited, visible, not blocking.
  • Removed cycle edges pass and are counted: import cycle edges H vs B, n new (k closing on their own), r removed.
  • An unresolved import the base lacks is a WARN, so a gap in the graph shows instead of being guessed.
  • One more file behind an existing edge is not new: the unit is the module edge.

On main (ef4dd750b4)

python3 tools/pr_check.py --base origin/main on this branch:

WARN: inherited import cycle of 16 modules (53 edges): core, cuda, fabric, families/drama3, families/flashnext, families/gemma4, families/kimi_k3, families/moss_image, families/muse_spark, families/nemo_audio, families/nemotron, families/qwen3_5, hip, hip_serve, native, xpu
WARN: inherited import cycle of 2 modules (2 edges): families/deepseek_v41, server
merge base ef4dd750b4e0c6366d281a6b4432f8460f54dc6f; 4 changed files; lean 165 vs 165, 0 new; import cycle edges 55 vs 55, 0 new (0 closing on their own), 0 removed
0 error(s), 2 warning(s); warnings are advisory

The RFC counted 8 modules. It mapped a named import only to a zig/src/<name> directory. Reading the literal build roots also resolves names like nemotron, lanes, engine_api, cuda_memory and json, and with them native's imports of seven families (native -> families/nemotron and others) join the same cycle. The edges that close it from core are still the RFC's three: core -> families/flashnext (pair.zig, pair_rows.zig import tp_settings.zig), core -> fabric, core -> cuda. The second cycle runs through tests and server/json.zig: families/deepseek_v41/sampling_test.zig imports json, and server/deepseek_fixture_test.zig imports dsv41_chat. With the directory rule alone the gate would miss cycles closed through such names. 122 imports stay unresolved (structure_check.py --unresolved lists them; kernel_sources, tensorfold, bf16_round lead), so the graph is a lower bound and a cycle closed only through one of them is not seen. The full run takes about 3 s per tree.

python3 tools/zig/structure_check.py prints every cycle edge with one file import behind it.

Controls on the real tree (not committed)

These use the checker's own compare on blob text: pc.base_text of each revision, plus the planted files.

base -> head result
origin/main (9a38b1f7b3) -> itself 0 ERROR; 60 vs 60 cycle edges (inkling2 is now in the inherited 17-module cycle)
planted family families/zz_planted (imports core, cuda) imported from native/zz_planted.zig 0 ERROR, passes; 3 WARN ... joins the inherited cycle of 18 modules
planted core/planted_cycle.zig importing ../families/glm/affine.zig 4 ERROR: core -> families/glm, families/glm -> core, families/glm -> families/qwen27, families/qwen27 -> core, each closes a cycle without the inherited one ...; cycle of 3 modules: core, families/glm, families/qwen27. glm's other 4 edges only join the cycle and WARN.
planted core/planted_hip.zig importing hip 1 ERROR: core -> hip: core imports a new module (zig/src/core/planted_hip.zig imports hip)
ef4dd750b4 (this PR's base) -> origin/main, the Inkling 2 span 2 ERROR: families/inkling2 -> native (families/inkling2/metal/serving_test.zig imports ../../../native/inkling2.zig) and native -> families/inkling2 (native/inkling2.zig imports ../families/inkling2/metal/backend.zig), cycle of 2 modules: families/inkling2, native. The family's imports of core, cuda and hip are 3 WARNs.
ef4dd750b4 -> 056128272d (Mini native serving, before serving_test.zig) 0 ERROR; 4 WARN (joins)

With the first commit's rule, the planted family drew ERRORs and the span drew five.

Tests

tools/zig/structure_check_test.py (26 tests), on small planted trees:

  • no cycle: no output;
  • inherited cycle: one WARN, no ERROR;
  • a new import closing a cycle: an ERROR on each edge now in it;
  • a module that only joins the inherited cycle (a family imported from native that imports core): WARN only, passes;
  • a two-module cycle closed beside the inherited one: ERROR on its two edges, while the family's core import only WARNs;
  • a new edge inside an inherited cycle: an ERROR only where it closes without the base's cycle edges;
  • core newly importing a module already in the inherited cycle (core -> hip), or a new module (core -> sys): ERROR; one more core file behind an existing edge: nothing;
  • a second file behind an inherited edge: nothing;
  • a removed cycle: passes, counted as removed;
  • module naming, relative and missing paths, named imports from literal roots, a name with two roots and a name with none listed as unresolved, commented and quoted imports, a new unresolved name;
  • two separate components, and a 5,000-module ring with no recursion;
  • the checkout parses;
  • a single-line and a multiline @import(\n "...",\n) give the same edge and the same unresolved diagnostic (unknown name and missing path), and a control shows the old pattern finds nothing in the multiline spelling;
  • two build helpers reusing one local mod (glm root, then core root): glm_api binds to families/glm, and a core file importing it is core -> families/glm: core imports a new module; order-change control: the helpers swapped give the same names and graph; a control shows the file-wide dictionary resolved mod to whichever helper came last; a var rebound to a second module, and a parameter whose name two helpers declare in different modules, are (two modules); a nested block sees its helper's variable, and .root_module = b.createModule(...) is not a binding. Against the 3b1dc7c0a1 checker the five binding tests fail.

tools/test_pr_check.py gains one test that runs the current checker through pr_check.import_cycles on both trees (new cycle: 2 ERRORs; removed: passes). Its two count fixtures and the patched check test carry the new count keys.

python3 -m unittest on tools/test_pr_check.py, tools/zig/structure_check_test.py, tools/zig/lean_check_test.py: 47 passed on 7ad6923cf1 (26 + 14 + 7, run per directory). Lean: 165 vs 165, 0 new.

Full x86 CI set on 891aebed70 (first commit) (dispatcher x86 lane): zig build -Dfixed-cpu test-host test test-golden test-kernel-contract test-server-security test-pull-security fuzz-cortheon check-release-paths, check-native-cuda-host compile-cuda-tests, pr_check, zig fmt --check, diff-check and lean all rc=0 (X86-ALL-GREEN, 74 min). On e53b826cf6: the same set, all rc=0 (X86-ALL-GREEN, 70 min). 3b1dc7c0a1 and 7ad6923cf1 change only the checker's Python files, so the Zig set was not rerun; the Python tests and pr_check ran on them.

On current main (b92aebe9e9), the scoped binding gives the same graph as 3b1dc7c0a1: 336 module edges, 131 unresolved names, the same 3 cycles (8 modules: flashnext, gemma4, inkling2, nemotron, qwen3_5, hip, hip_serve, native; core, cuda, fabric; deepseek_v41, server), edge for edge. The three names with no binding in scope on main (dk in dsv41_cuda.zig, m in muse.zig, kimi in metal_targets.zig) resolve through the file's single declaration as before. On a trial merge into b92aebe9e9, python3 tools/pr_check.py --base b92aebe9e9 gives 0 errors, the 3 inherited cycle WARNs, lean 164 vs 164, import cycle edges 24 vs 24, 0 new. Main merged the 16-module cycle above apart since ef4dd750b4.

On main the only multiline @import( in zig/src is hip/kernel_tests/nemotron/fused.zig (../../launches/norms.zig), inside hip: it adds no module edge and no unresolved name, so the counts above are unchanged; main against itself gives 0 errors.

Not applicable: no Zig, kernel, served path, env flag or refusal changes, so the build output is unchanged against main. No reply, timing or concurrency rows apply to a Python checker. The controls are the planted cycle and the removal above.

Proposed CHANGELOG line (Unreleased): - pr_check: an import cycle a PR closes between Zig modules, or a new module core imports, is an error; inherited cycles warn (tools/zig/structure_check.py) (#4890).

Not in this PR: the allow list (qwen3 -> llama is its first entry) comes with the cross-family check.

@AIFlowML

Copy link
Copy Markdown
Contributor

The 16 structure-checker tests and 14 pr_check tests passed at e53b826cf67b24d2ead6fdcf23c3b3cbffe17c90 on Apple silicon. python3 tools/pr_check.py --base upstream/main gave zero errors and two inherited cycle warnings. The head also test-merges cleanly into main 4c79cfe346d3c239ff7dfd7aa694df799755bafa.

One current source form is missing from the import scan. zig/src/hip/kernel_tests/nemotron/fused.zig already has a multiline @import with a trailing comma. In a small graph() fixture, single-line and multiline spellings of the same relative import returned different graphs. The multiline form returned no edge and no unresolved import. Allowing whitespace and an optional trailing comma made the multiline fixture return the same edge and diagnostic as the single-line form. Could the unit tests pin those two spellings to the same result?

Igor Lessio - AIFLOW LABS

@jschmied

Copy link
Copy Markdown
Contributor Author

@AIFlowML thanks, good catch. Fixed in 3b1dc7c0a1: the scan now reads whitespace, newlines and a trailing comma inside @import( ... ). New tests pin a single-line and a multiline spelling to the same edge and the same unresolved diagnostic (unknown name and missing path), with a control that the old pattern finds nothing in the multiline one. On main, fused.zig is the only multiline import and stays inside hip, so no new edge appears and main against itself still gives 0 errors.

@AIFlowML

Copy link
Copy Markdown
Contributor

Confirmed on the clean trial merge of 3b1dc7c0a1 into 4c79cfe346. All 19 structure-checker tests and 14 PR-check tests passed, including the multiline import and trailing-comma cases. python3 tools/pr_check.py --base 4c79cfe346 reported zero errors and the two inherited cycle warnings, with the same 59 edges on both sides. This closes my earlier parser finding.

Igor Lessio - AIFLOW LABS

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thanks, @jschmied. The multiline import fix and its edge/diagnostic controls close the earlier parser finding.

One binding gap remains: two build helpers using the same local module variable overwrite each other in the whole-file dictionary. The first alias can resolve to the second helper's module, hiding a new core dependency. Please preserve scope or report ambiguous roots, with a two-helper regression and an order-change control.

  • Synthena

@jschmied

Copy link
Copy Markdown
Contributor Author

@ashhart thanks. Fixed in 7ad6923cf1: a module variable now binds in its own scope (innermost {...} block, after the declaration), so two helpers reusing mod keep their own modules. A reassignment adds its roots to the variable; a use with no binding in scope (a parameter) takes the file's declarations of that name. Roots in two modules report the name as (two modules), never the last binding.

New tests in structure_check_test.py (HelperScope):

  • test_two_helpers_keep_their_own_module_variable and test_the_new_core_dependency_behind_the_first_helper_errors: the glm helper first, then the core helper; the core file's import is core -> families/glm: core imports a new module.
  • test_control_helper_order_does_not_change_the_result: helpers swapped, same names and graph.
  • test_control_one_file_wide_dictionary_would_resolve_to_the_last_helper: the old binding follows the last helper in both orders.
  • Ambiguous roots: a rebound var and a parameter declared in two helpers give (two modules).

Against the 3b1dc7c0a1 checker the five binding tests fail. 26 checker tests, 14 PR-check tests and 7 lean tests pass. On main b92aebe9e9 the graph is unchanged edge for edge (336 edges, 131 unresolved, 3 inherited cycles). A trial-merge pr_check gives 0 errors and 24 vs 24 cycle edges.

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thank you, @jschmied. The scoped binding closes the last ask: each helper keeps its module, both helper orders are covered, and ambiguous roots stay unresolved. The graph and cycle rules are unchanged, and the head merges cleanly.

Ready to land after the usual final checks.

  • Synthena

ashhart and others added 6 commits November 25, 2026 20:33
tools/zig/structure_check.py builds the module import graph of zig/src from file text
(a module is zig/src/<dir>, zig/src/families/<name>, or a top-level file), finds its
strongly connected components and compares base with head. An edge inside a cycle that
the base did not have inside a cycle is an ERROR; an inherited cycle is one WARN line;
removed cycle edges are counted in the summary. Named imports resolve from literal build
roots or a zig/src/<name> directory; the rest is listed as unresolved, not guessed.

Refs ashhart#4804
… a new module core imports

A new cycle edge is an ERROR when its cycle still closes with the base's cycle edges left
out; an edge that only joins the inherited cycle is a WARN. Any module core newly imports
is an ERROR, also inside the inherited cycle. Asked on ashhart#4804 (comment 7132779637).
…port two roots by name

Two build helpers that reuse a local module variable name no longer overwrite each other in one
file-wide dictionary: a declaration counts inside its own block and after it. A reassignment adds
its roots to the variable, and a use with no binding in scope takes the file's declarations of the
name; roots in two modules read as an ambiguous name, never as the last one.
@ashhart
ashhart force-pushed the tools/structure-cycle-check branch from 7ad6923 to 3ed072d Compare November 25, 2026 20:33
@ashhart
ashhart merged commit df95d08 into ashhart:main Nov 25, 2026
2 of 12 checks passed
@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thank you @jschmied. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes.

ashhart added a commit to Bizuayeu/TensorFold that referenced this pull request Nov 25, 2026
ashhart added a commit that referenced this pull request Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants