Skip to content

Fix: Support enum discriminators to prevent exponential validation freeze (#284)#320

Draft
Mayank-Maurya wants to merge 2 commits intomicrosoft:mainfrom
Mayank-Maurya:Fix--Support-enum-discriminators-to-prevent-exponential-validation-freeze-(#284)
Draft

Fix: Support enum discriminators to prevent exponential validation freeze (#284)#320
Mayank-Maurya wants to merge 2 commits intomicrosoft:mainfrom
Mayank-Maurya:Fix--Support-enum-discriminators-to-prevent-exponential-validation-freeze-(#284)

Conversation

@Mayank-Maurya
Copy link

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).

@Mayank-Maurya
Copy link
Author

@Mayank-Maurya please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.

@microsoft-github-policy-service agree [company="{your company}"]

Options:

  • (default - no company specified) I have sole ownership of intellectual property rights to my Submissions and I am not making Submissions in the course of work for my employer.
@microsoft-github-policy-service agree
  • (when company given) I am making Submissions in the course of work for my employer (or my employer has intellectual property rights in my Submissions by contract or applicable law). I have permission from my employer to make Submissions and enter into this Agreement on behalf of my employer. By signing below, the defined term “You” includes me and my employer.
@microsoft-github-policy-service agree company="Microsoft"

Contributor License Agreement

@microsoft-github-policy-service agree

@aeschli aeschli enabled auto-merge (squash) March 23, 2026 08:13
@aeschli
Copy link
Collaborator

aeschli commented Mar 23, 2026

@Mayank-Maurya Thanks a lot for the analysis and the fix. Very appreciated!

@joaomoreno joaomoreno marked this pull request as draft March 23, 2026 10:00
auto-merge was automatically disabled March 23, 2026 10:00

Pull request was converted to draft

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.

JSON language features broken. TreeSitter grammar.json. no error

2 participants