Fix global variable context resolution#8
Open
Huang-Xuwei wants to merge 1 commit intoTHU-WingTecher:mainfrom
Open
Fix global variable context resolution#8Huang-Xuwei wants to merge 1 commit intoTHU-WingTecher:mainfrom
Huang-Xuwei wants to merge 1 commit intoTHU-WingTecher:mainfrom
Conversation
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
This PR fixes a context resolution issue where global variables / constants were not correctly recognized during symbol analysis, which could lead to incomplete context being sent to the generator.
In particular, this patch addresses cases like a Python global constant being referenced inside a method (for example,
c = 2used insidecomplex_calculation(...)), where the existing logic failed to include that definition in the collected context.Root Cause
The issue came from two gaps in the current implementation:
There was also a workspace detection edge case in test/CLI-hosted environments: when
workspaceFolderswas empty, valid project files could be treated as outside the workspace and skipped.Changes
1. Add a fallback generation mode for broader context collection
Introduced a new generation mode:
cfg_fallbackThis mode bypasses the stricter CFG-only filtering path and allows context extraction from the full token set, making globally referenced symbols easier to capture.
Updated files:
src/config.tspackage.jsonsrc/tokenAnalyzer.tssrc/strategy/generators/cfgFallback.tssrc/strategy/generators/factory.ts2. Improve variable / constant context enrichment
Extended context loading so variable-like symbols such as constants, globals, and members can be processed through definition collection more reliably.
This includes:
Updated files:
src/agents/contextSelector.tssrc/lsp/utils.ts3. Fix workspace fallback in
isInWorkspace()Updated workspace detection so that when
vscode.workspace.workspaceFoldersis unavailable, the resolver can fall back to the configured workspace path.This avoids incorrectly classifying valid project definitions as external in test-host / CLI-like environments.
Updated file:
src/lsp/definition.tsTests
Added a regression fixture and test for the global constant case:
src/test/fixtures/python/global_constant.pysrc/test/suite/lsp/globalConstant.test.tsAlso updated related token test coverage:
src/test/suite/lsp/token.test.tsValidation
Validated locally with:
The regression test now passes and logs show that the global variable c is correctly recognized and its definition is successfully added to context.
Example Scenario Fixed
Before this patch, code like:
could miss c during context collection.
After this patch, c is detected as a relevant context term and its definition is included.
Notes
This change is implemented as an additive fix through a new generation mode, rather than changing the default behavior of the existing CFG-based strategy.