-
Notifications
You must be signed in to change notification settings - Fork 3k
refactor(cli): enforce utils leaf-layer dependency direction (#9146) #9737
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
a983ece
refactor(cli): enforce utils leaf-layer dependency direction (#9146)
yiliang114 7735a3b
fix: use Qwen Team 2026 license header on new files (#9146)
yiliang114 204b3c3
chore: refresh stale utils/ path references after leaf-layer move (#9…
yiliang114 21d21a0
Merge remote-tracking branch 'origin/main' into codex/9146-utils-leaf…
yiliang114 a81cbe7
docs: reconcile no-utils-upward-import header with the allowed type-o…
yiliang114 beab2dc
fix(cli): allowlist sandbox process.env accesses after leaf-layer mov…
yiliang114 1e48458
chore(ci): re-record qwen-autofix.yml size baseline after #9677 (#9146)
yiliang114 0566edc
fix(review): drop the stale utils/findings.ts digest root after the l…
yiliang114 0dca7b0
fix(review): colocate seatbelt profiles with the sandbox module (#9146)
yiliang114 25670ab
fix(review): exempt inline type-only specifiers from the utils upward…
yiliang114 21cfacf
fix(review): report upward inline type-specifier imports under verbat…
yiliang114 6980025
test(review): pin mixed-specifier and zero-specifier upward imports i…
yiliang114 bf6b9ba
test(review): anchor the nested-checkout utils rule fixture on the la…
yiliang114 8071456
test(review): pin that the utils/findings.ts digest root stays remove…
yiliang114 256abb1
Merge remote-tracking branch 'origin/main' into codex/9146-utils-leaf…
yiliang114 1b870e2
fix(review): reword stale-bundle SCOPE header to the post-move helper…
yiliang114 ab039d6
test(review): drop the pre-move utils/findings.ts from the skill-pari…
yiliang114 25687c4
test(serve): derive the seatbelt colocation tripwire from BUILTIN_SEA…
yiliang114 764d603
fix(architecture): fail closed on computed dynamic imports in the uti…
yiliang114 98f0134
fix(cli): point settings.test.ts at the post-move settingsUtils path …
yiliang114 e81731f
fix(cli): close utils boundary review gaps
yiliang114 85320da
Merge remote-tracking branch 'origin/main' into codex/pr-9737-closeou…
yiliang114 1445a76
test(cli): cover utils boundary allow paths
yiliang114 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,192 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2026 Qwen Team | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import path from 'node:path'; | ||
|
|
||
| /** | ||
| * `packages/cli/src/utils/` is the leaf layer that every other directory | ||
| * imports. It must not import back up into a domain directory (`config/`, | ||
| * `ui/`, `i18n/`, `nonInteractive/`, `commands/`, `serve/`, | ||
| * `acp-integration/`, ...): that is the dependency-direction invariant | ||
| * tracked in #9146. | ||
| * | ||
| * The only permitted "upward" references are the type-only constructs that | ||
| * are genuinely erased at compile time: statement-level `import type`, | ||
| * `export type ... from`, and TS `import('...').T` type queries. Inline type | ||
| * specifiers (`import { type X } from` / `export { type X } from`) are | ||
| * reported instead: under this repo's `verbatimModuleSyntax`, tsc keeps the | ||
| * declaration and emits `import {} from` / `export {} from`, a runtime edge | ||
| * that still evaluates the target module. Everything else (value imports, | ||
| * value re-exports, dynamic `import()`) is reported too: a literal or | ||
| * single-segment template source is checked against its resolved path. CLI | ||
| * baseUrl specifiers rooted at `src/` are resolved from `packages/cli/` and | ||
| * checked the same way. A | ||
| * computed source (a multi-segment template or a `+` concatenation) whose | ||
| * statically known prefix is local is reported fail-closed, because | ||
| * interpolation can contribute a `../` step no static check can rule out. A | ||
| * computed source with no statically known local prefix is dropped, the same | ||
| * boundary applied to package and builtin specifiers. The two remaining | ||
| * instances (`Settings` in `modelConfigUtils.ts`, `CommandContext` in | ||
| * `sessionPaths.ts`) are this irreducible type-level coupling. | ||
| */ | ||
|
|
||
| const CLI_PACKAGE_MARKER = 'packages/cli/'; | ||
| const CLI_UTILS_MARKER = `${CLI_PACKAGE_MARKER}src/utils/`; | ||
| const TEST_OR_FIXTURE_SEGMENTS = new Set(['__tests__', 'fixtures']); | ||
|
|
||
| function isCliUtilsProductionFile(filename) { | ||
| if (!filename || filename === '<input>' || filename === '<text>') { | ||
| return false; | ||
| } | ||
| const normalized = path.normalize(filename).replaceAll('\\', '/'); | ||
| const start = normalized.lastIndexOf(CLI_UTILS_MARKER); | ||
| if (start < 0) { | ||
| return false; | ||
| } | ||
| const relativePath = normalized.slice(start + CLI_UTILS_MARKER.length); | ||
| if (/\.(test|spec)\.[cm]?[jt]sx?$/.test(relativePath)) { | ||
| return false; | ||
| } | ||
| return !relativePath.split('/').some((s) => TEST_OR_FIXTURE_SEGMENTS.has(s)); | ||
| } | ||
|
|
||
| function escapesUtils(filename, importedPath) { | ||
| const normalized = path.normalize(filename).replaceAll('\\', '/'); | ||
| const markerStart = normalized.lastIndexOf(CLI_UTILS_MARKER); | ||
| const utilsRoot = normalized.slice(0, markerStart + CLI_UTILS_MARKER.length); | ||
| const cliRoot = normalized.slice(0, markerStart + CLI_PACKAGE_MARKER.length); | ||
| const resolved = path.resolve( | ||
| importedPath.startsWith('src/') ? cliRoot : path.dirname(filename), | ||
| importedPath, | ||
| ); | ||
| return path | ||
| .relative(utilsRoot, resolved) | ||
| .replaceAll('\\', '/') | ||
| .startsWith('..'); | ||
| } | ||
|
|
||
| /** | ||
| * The statically known leading characters of a computed dynamic-import | ||
| * source: the first quasi of a template literal, the string literal itself, | ||
| * or the leftmost operand of a `+` concatenation. Anything else (a bare | ||
| * identifier, a call, an empty first quasi) has no statically known prefix. | ||
| */ | ||
| function knownDynamicPrefix(node) { | ||
| if (node.type === 'Literal') { | ||
| return typeof node.value === 'string' ? node.value : null; | ||
| } | ||
| if (node.type === 'TemplateLiteral') { | ||
| return node.quasis[0].value.cooked; | ||
| } | ||
| if (node.type === 'BinaryExpression' && node.operator === '+') { | ||
| return knownDynamicPrefix(node.left); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /** Whether a known prefix spells a relative specifier (`./`, `../`, `.`, `..`). */ | ||
| function isRelativePrefix(prefix) { | ||
| return ( | ||
| prefix === '.' || | ||
| prefix === '..' || | ||
| prefix.startsWith('./') || | ||
| prefix.startsWith('../') | ||
| ); | ||
| } | ||
|
|
||
| function isCliBaseUrlPrefix(prefix) { | ||
| return prefix.startsWith('src/'); | ||
| } | ||
|
|
||
| export default { | ||
| meta: { | ||
| type: 'problem', | ||
| docs: { | ||
| description: | ||
| 'packages/cli/src/utils must not import outside utils/ (leaf-layer dependency direction).', | ||
| }, | ||
| messages: { | ||
| noUtilsUpwardImport: | ||
| 'packages/cli/src/utils must not import outside utils/. ' + | ||
| 'Invert the dependency (pass the value in) or move the module to the ' + | ||
| 'domain directory that owns it (#9146).', | ||
| noUtilsUnprovableDynamicImport: | ||
| 'packages/cli/src/utils cannot statically prove this computed ' + | ||
| 'dynamic import() stays inside utils/ — interpolation can ' + | ||
| 'contribute a `../` step. Resolve the target through a literal or ' + | ||
| 'single-segment template source, or pass the module in (#9146).', | ||
| }, | ||
| }, | ||
| create(context) { | ||
| const { filename } = context; | ||
| if (!isCliUtilsProductionFile(filename)) { | ||
| return {}; | ||
| } | ||
|
|
||
| const reportIfEscaping = (sourceNode, importedPath) => { | ||
| if ( | ||
| typeof importedPath === 'string' && | ||
| (importedPath.startsWith('.') || isCliBaseUrlPrefix(importedPath)) && | ||
| escapesUtils(filename, importedPath) | ||
| ) { | ||
|
yiliang114 marked this conversation as resolved.
|
||
| context.report({ node: sourceNode, messageId: 'noUtilsUpwardImport' }); | ||
| } | ||
| }; | ||
|
|
||
| const checkStatic = (node) => { | ||
| // Statement-level type-only imports (`import type`, `export type ... | ||
| // from`) are erased at compile time and cannot create a runtime cycle. | ||
| // Inline type specifiers (`import { type X } from ...`) are NOT exempt: | ||
| // under this repo's `verbatimModuleSyntax`, tsc keeps the declaration | ||
| // and emits `import {} from ...` / `export {} from ...`, a runtime edge | ||
| // that evaluates the target module — so they are reported like value | ||
| // imports. | ||
| if (node.importKind === 'type' || node.exportKind === 'type') { | ||
| return; | ||
| } | ||
|
yiliang114 marked this conversation as resolved.
|
||
| reportIfEscaping(node.source, node.source?.value); | ||
| }; | ||
|
|
||
| const checkDynamic = (node) => { | ||
| const { source } = node; | ||
| if (source.type === 'Literal') { | ||
| reportIfEscaping(source, source.value); | ||
| return; | ||
| } | ||
| if (source.type === 'TemplateLiteral' && source.quasis.length === 1) { | ||
| reportIfEscaping(source, source.quasis[0].value.cooked); | ||
| return; | ||
| } | ||
| // Computed sources — multi-segment templates and `+` concatenations — | ||
| // fail closed when their statically known prefix is relative: | ||
| // interpolation can contribute a `../` step, so no static check can | ||
| // prove the import stays inside utils/ (a leading `../` cannot be | ||
| // undone by interpolation at all). A computed source with no known | ||
| // local prefix — a bare identifier or a package-like prefix — is | ||
| // dropped, the same boundary applied to package and builtin static | ||
| // specifiers. CLI baseUrl sources rooted at `src/` are local too. | ||
| const prefix = knownDynamicPrefix(source); | ||
| if ( | ||
| typeof prefix === 'string' && | ||
| (isRelativePrefix(prefix) || isCliBaseUrlPrefix(prefix)) | ||
| ) { | ||
| context.report({ | ||
| node: source, | ||
| messageId: 'noUtilsUnprovableDynamicImport', | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| return { | ||
| ImportDeclaration: checkStatic, | ||
| ExportNamedDeclaration: checkStatic, | ||
| ExportAllDeclaration: checkStatic, | ||
| ImportExpression: checkDynamic, | ||
| // TSImportType (`import('../config/x').T`) is type-only by definition, so | ||
| // it is intentionally not reported. | ||
| }; | ||
| }, | ||
| }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.