Repository navigation
pr_check: no new import cycle between Zig modules; inherited cycles warn - #4890
Conversation
|
The 16 structure-checker tests and 14 One current source form is missing from the import scan. Igor Lessio - AIFLOW LABS |
|
@AIFlowML thanks, good catch. Fixed in |
|
Confirmed on the clean trial merge of Igor Lessio - AIFLOW LABS |
|
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.
|
|
@ashhart thanks. Fixed in New tests in
Against the |
|
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.
|
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).
… the single-line one
…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.
7ad6923 to
3ed072d
Compare
|
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. |
Refs #4804. The cycle checker only, as asked in #4804 (comment): no cross-family check and no dependency cleanup here.
Head:
7ad6923cf1, four commits onef4dd750b4(891aebed70, then the review rules from #4804 (comment) ine53b826cf6, then the multiline@import(fix from #4890 (comment) in3b1dc7c0a1, then the scoped module-variable binding from #4890 (comment)).What it checks
tools/zig/structure_check.pyreads the@importgraph ofzig/srcfrom file text: no build, no index, no checkout of the base.tools/pr_check.pyruns it on the base text it already reads and on the working tree, the same way it runs lean.zig/src/<dir>,zig/src/families/<name>, or a file directly underzig/srcas its own module. An edge A -> B exists when a file of A imports a file of B.zig/srcis not a module; a missing path inside it is listed as unresolved.addImport("name", ...)inbuild.zigorzig/build/*.zigwhose argument names a"zig/src/....zig"root, directly or through aconst x = b.createModule(...)in the same file, or azig/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.stdandbuiltinare skipped. Whitespace, newlines and a trailing comma inside@import( ... )are read like the single-line spelling.//comments and\\string lines are not read.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.
new import) or an existing import now closes a cycle (existing import).main. A newcore -> hiperrors, even though hip is already in the inherited cycle and the first rule alone would miss it.import cycle edges H vs B, n new (k closing on their own), r removed.On
main(ef4dd750b4)python3 tools/pr_check.py --base origin/mainon this branch: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 likenemotron,lanes,engine_api,cuda_memoryandjson, and with them native's imports of seven families (native -> families/nemotronand 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.zigimporttp_settings.zig),core -> fabric,core -> cuda. The second cycle runs through tests andserver/json.zig:families/deepseek_v41/sampling_test.zigimportsjson, andserver/deepseek_fixture_test.zigimportsdsv41_chat. With the directory rule alone the gate would miss cycles closed through such names. 122 imports stay unresolved (structure_check.py --unresolvedlists them;kernel_sources,tensorfold,bf16_roundlead), 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.pyprints every cycle edge with one file import behind it.Controls on the real tree (not committed)
These use the checker's own
compareon blob text:pc.base_textof each revision, plus the planted files.origin/main(9a38b1f7b3) -> itselffamilies/zz_planted(imports core, cuda) imported fromnative/zz_planted.zig... joins the inherited cycle of 18 modulescore/planted_cycle.zigimporting../families/glm/affine.zigcore -> families/glm,families/glm -> core,families/glm -> families/qwen27,families/qwen27 -> core, eachcloses 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.core/planted_hip.zigimportinghipcore -> hip: core imports a new module (zig/src/core/planted_hip.zig imports hip)ef4dd750b4(this PR's base) ->origin/main, the Inkling 2 spanfamilies/inkling2 -> native(families/inkling2/metal/serving_test.zig imports ../../../native/inkling2.zig) andnative -> 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, beforeserving_test.zig)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:core -> hip), or a new module (core -> sys): ERROR; one more core file behind an existing edge: nothing;@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;mod(glm root, then core root):glm_apibinds tofamilies/glm, and a core file importing it iscore -> 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 resolvedmodto whichever helper came last; avarrebound 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 the3b1dc7c0a1checker the five binding tests fail.tools/test_pr_check.pygains one test that runs the current checker throughpr_check.import_cycleson both trees (new cycle: 2 ERRORs; removed: passes). Its two count fixtures and the patchedchecktest carry the new count keys.python3 -m unittestontools/test_pr_check.py,tools/zig/structure_check_test.py,tools/zig/lean_check_test.py: 47 passed on7ad6923cf1(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). One53b826cf6: the same set, all rc=0 (X86-ALL-GREEN, 70 min).3b1dc7c0a1and7ad6923cf1change 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 as3b1dc7c0a1: 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 onmain(dkindsv41_cuda.zig,minmuse.zig,kimiinmetal_targets.zig) resolve through the file's single declaration as before. On a trial merge intob92aebe9e9,python3 tools/pr_check.py --base b92aebe9e9gives 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 sinceef4dd750b4.On
mainthe only multiline@import(inzig/srciship/kernel_tests/nemotron/fused.zig(../../launches/norms.zig), insidehip: it adds no module edge and no unresolved name, so the counts above are unchanged;mainagainst 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.