Fix: Support enum discriminators to prevent exponential validation freeze (#284)#320
Draft
Mayank-Maurya wants to merge 2 commits intomicrosoft:mainfrom
Conversation
Author
@microsoft-github-policy-service agree |
aeschli
approved these changes
Mar 23, 2026
Collaborator
|
@Mayank-Maurya Thanks a lot for the analysis and the fix. Very appreciated! |
…exponential-validation-freeze-(microsoft#284)
auto-merge was automatically disabled
March 23, 2026 10:00
Pull request was converted to draft
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.
Fixes #284 - JSON Language service freezes (CPU 100%) when validating Tree-sitter grammar
Description: This PR resolves a severe performance bottleneck where deeply nested, recursive JSON schemas (such as the Tree-sitter grammar.schema.json) cause the validation thread to freeze due to catastrophic backtracking in array combinations.
Root Cause: The language service employs
_tryDiscriminatorOptimization
to avoid exhaustively testing every anyOf/oneOf branch. However, it previously only mapped properties declared with
const
. Because Tree-sitter's schema differentiates its branches using enum identifiers (e.g. type: { enum: ["TOKEN", "IMMEDIATE_TOKEN"] }), the optimization function aborted. This forced the AST validator to fall back into a full
O(A^N)
combinatorial evaluation.
Changes:
Enhanced Discriminator Extraction: Expanded
_tryDiscriminatorOptimization
in
src/parser/jsonParser.ts
to correctly map schema.enum arrays alongside schema.const.
Updated Completion Tests: Refined outdated assertions in
src/test/completion.test.ts
. Before this fix, the parser failed to discriminate type: "1" properties declared via enum arrays, incorrectly offering autocomplete suggestions from multiple branches. It now accurately isolates exactly the correct branch and only returns relevant completions.
Regression Testing: Added a dedicated regression test to
src/test/parser.test.ts
using an enum-based recursive schema. The parsing test limits out to < 100ms (where without this patch, the same minimal depth takes over 5,000ms entirely bottlenecking Node).