Skip to content

Fix global variable context resolution#8

Open
Huang-Xuwei wants to merge 1 commit intoTHU-WingTecher:mainfrom
Huang-Xuwei:global-variable-context-fix-main-clean
Open

Fix global variable context resolution#8
Huang-Xuwei wants to merge 1 commit intoTHU-WingTecher:mainfrom
Huang-Xuwei:global-variable-context-fix-main-clean

Conversation

@Huang-Xuwei
Copy link
Contributor

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 = 2 used inside complex_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:

  1. Context term extraction was still constrained by CFG-focused token selection, so some globally referenced symbols could be missed.
  2. Variable-like symbols such as global variables / constants were not consistently enriched with definition context.

There was also a workspace detection edge case in test/CLI-hosted environments: when workspaceFolders was 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_fallback

This 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.ts
  • package.json
  • src/tokenAnalyzer.ts
  • src/strategy/generators/cfgFallback.ts
  • src/strategy/generators/factory.ts

2. 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:

  • recognizing more variable-like symbol kinds
  • extracting nearby source definitions when available
  • using hover information as a fallback when direct definition extraction is insufficient

Updated files:

  • src/agents/contextSelector.ts
  • src/lsp/utils.ts

3. Fix workspace fallback in isInWorkspace()

Updated workspace detection so that when vscode.workspace.workspaceFolders is 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.ts

Tests

Added a regression fixture and test for the global constant case:

  • src/test/fixtures/python/global_constant.py
  • src/test/suite/lsp/globalConstant.test.ts

Also updated related token test coverage:

  • src/test/suite/lsp/token.test.ts

Validation

Validated locally with:

npm run compile
DONT_PROMPT_WSL_INSTALL=1 npm_config_testfile=lsp/globalConstant node ./out/test/runTest.js

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:

c = 2

class Calculator:
    def complex_calculation(self, a, b):
        intermediate_result = self.add(a, b)
        intermediate_result = self.add(intermediate_result, c)
        return intermediate_result

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.

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.

1 participant