← Back to Pull requests

Reuse diagnostics checker for semantic tokens of checked files #65411

Open
hood opened this pull request with 181 lines diff across 5 files
View on GitHub
hood hood commented on Nov 09, 2026 20:10 UTC
Author

Fixes #65386

Semantic tokens used to run on a cold query checker and re-infer basically every variable, parameter and property type in the file, the same work diagnostics had just done on the diagnostics checker. With this PR, once a diagnostics request has fully checked a file and is done, semantic tokens for that file borrow the idle diagnostics checker instead.

The rules are kinda strict so the diagnostics checker's walk order doesn't change:

  • it's only lent out if it's idle (non-blocking diagSem), the file is fully checked (typeChecked + unusedChecked, not cancelled) and no diagnostics request for that file is still in flight (they acquire it three times: semantic, suggestion, declaration);

  • otherwise semantic tokens get a query checker exactly like today, and they never wait.
  • That ordering is what avoids stuff like #51661, where a query asked for a type before the normal walk and you'd get an order-dependent false error in the editor.

    VSCode pulls diagnostics right on change (diagnosticPullOptions.onChange) and only asks for semantic tokens ≥300 ms later (REQUEST_MIN_DELAY), so in practice the reuse path is the common one.

    | recharts src/cartesian/Line.tsx, 120 keystrokes, 3 runs | main | this PR |
    |---|---|---|
    | diagnostics then semantic tokens: semanticTokens/full median | 41.3–41.7 ms | 2.88–2.91 ms (−93%) |
    | same: textDocument/diagnostic median | 52.6–53.2 ms | 52.4–52.9 ms |
    | same: process CPU | 14.5–14.7 s | 8.6 s (−41%) |
    | both sent together: keystroke median / CPU | 53–55 ms / 14.7 s | 54 ms / 14.7 s (falls back, no change) |

    Tests:

  • new TestCheckerPoolCheckedFileQuery covers no reuse before the file is checked, no reuse while the diagnostics request is still in flight, reuse after, and fallback while the checker is lent out. It fails if the in-flight guard gets removed.

  • semantic tokens + diagnostics for Line.tsx and three other open files (TS2883 type printing, unused suggestions, the transient errors you get mid-typing) are byte-identical to main across 600 responses, both sequential and concurrent.

  • go test ./..., npx hereby lint and npx hereby check:format pass.
  • One thing I'm not 100% sure about: after the file is checked, classification still creates a few types/symbols on the diagnostics checker (~6 types and 32 symbols per request here, looks like throwaway re-checks of expressions). I didn't see any diagnostics change, but if keeping that checker totally free of query work is a hard requirement this approach doesn't fit, so happy to go another way.

    AI disclosure: I polished my solution, my breakdown of it and -most importantly- did most of the measurements with help from an AI agent. I've gone 2x through everything and I'll handle any follow-up myself ofc.

    Diff: 181 lines (+181 / -0) across 5 files Reviewer: hood Review duration: 36419m
    All Vouch review confidence checks passed
    Residual risk 0.13 = Change Risk (0.77) × (1 − Review Confidence (0.84)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.77 · 181 lines diff across 5 files
    0.77
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.90 · 5 substantive comment(s)
    0.90
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    83%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.311
    Self Review self_review
    0.140
    Sensitive Paths sensitive_paths
    0.077
    Real PR Conversation Timeline
    8 items
    Copilot Copilot commented on code on Nov 09, 2026 20:17 UTC
    Code Review
    tsc/internal/project/checkerpool.go:1
    @@ -216,10 +223,40 @@ func (p *checkerPool) getDiagnosticsChecker(ctx context.Context, requestID strin
     			p.requestAssociations[requestID] = diagIndex
     			p.registerRequestCleanup(ctx, requestID)
     		}
    +		if file != nil {
    +			p.diagnosticsRequests[requestID] = file

    getAllDiagnostics uses one request ID for the canonical file and its supplemental files, but the request-affinity path returns before this assignment, leaving subsequent files untracked. When declaration diagnostics are enabled, semantic tokens can therefore borrow the checker after a supplemental file's suggestion check but before its declaration diagnostics, bypassing the in-flight guard. Track a per-request set of files on both acquisition paths until request cleanup, and add a multi-projection regression test.

    Copilot Copilot commented on code on Nov 09, 2026 20:17 UTC
    Code Review
    tsc/internal/ls/semantictokens.go:1
    @@ -126,6 +126,7 @@ func SemanticTokensLegend(clientCapabilities lsproto.ResolvedSemanticTokensClien
     }
     
     func (l *LanguageService) ProvideSemanticTokens(ctx context.Context, documentURI lsproto.DocumentUri) (lsproto.SemanticTokensResponse, error) {
    +	ctx = core.WithCheckerLifetime(ctx, core.CheckerLifetimeCheckedFileQuery)

    The new pool test verifies checker selection but never runs semantic classification on the borrowed checker or checks diagnostics afterward. Add an integration regression that completes diagnostics, exercises full and range semantic tokens through the project checker pool, and verifies token output and unchanged subsequent diagnostics, including the order-sensitive assertion case from #51661. Explicitly verify that the test takes the reuse path so it cannot pass by falling back to a query checker.

    copilot-pull-request-reviewer[bot] copilot-pull-request-reviewer[bot] commented on Nov 09, 2026 20:17 UTC
    Reviewer

    <!-- ccr-overview-v2 -->

    🟡 Changes recommended

    A critical request-affinity tracking gap allows checker reuse before supplemental-file declaration diagnostics finish, and semantic-token reuse still lacks integration regression coverage.

    <details open>
    <summary><strong>2 open issues</strong></summary>

  • <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/high-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/high-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/high-v2-light.png" alt="High severity" width="62" height="18" align="texttop"></picture> Track supplemental files on the request-affinity acquisition path · New

  • <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/medium-v2-light.png" alt="Medium severity" width="62" height="18" align="texttop"></picture> Test pooled checker reuse through semantic tokens and diagnostics · New

  • </details>

    <details>
    <summary><strong>What changed in this PR</strong></summary>

    This PR addresses #65386 by letting semantic-token requests reuse an idle diagnostics checker for fully checked files, reducing repeated type inference while preserving query-checker fallback.

    Changes:

  • Adds a checked-file query lifetime for full and range semantic tokens.

  • Gates checker reuse on completed checks, availability, and in-flight diagnostics tracking.

  • Adds pool tests covering reuse and fallback behavior.
  • | File | Description |
    | ---- | ----------- |
    | tsc/​internal/​project/​checkerpool.go | Implements guarded diagnostics-checker lending and request tracking. |
    | tsc/​internal/​project/​checkerpool_test.go | Tests checker reuse eligibility and fallback. |
    | tsc/​internal/​ls/​semantictokens.go | Requests checked-file reuse for full and range tokens. |
    | tsc/​internal/​core/​context.go | Defines the checked-file query lifetime. |
    | tsc/​internal/​checker/​exports.go | Exposes whether a file has completed checking without cancellation. |
    </details>

    <sub>Review effort: Balanced</sub>

    jakebailey jakebailey commented on Nov 09, 2026 20:25 UTC
    Contributor

    This is generally unsafe due to ordering issues, hence diag checkers being separated.

    Somewhat what I'm trying to address once and for all in #65064

    hood hood commented on code on Nov 09, 2026 20:33 UTC
    Code Review
    tsc/internal/project/checkerpool.go:1
    @@ -216,10 +223,40 @@ func (p *checkerPool) getDiagnosticsChecker(ctx context.Context, requestID strin
     			p.requestAssociations[requestID] = diagIndex
     			p.registerRequestCleanup(ctx, requestID)
     		}
    +		if file != nil {
    +			p.diagnosticsRequests[requestID] = file

    Good catch clanker, the affinity path returned before recording anything, so only the canonical file was tracked. Instead of tracking files per request I made it stricter: the pool now keeps the set of in-flight diagnostics request IDs (cleared by the same ctx AfterFunc as requestAssociations) and won't lend the checker at all while any of them is in flight. That covers supplemental files for free and kinda simplifies things, also reverts the getDiagnosticsChecker signature change.

    TestCheckerPoolCheckedFileQuery now checks two files under one diagnostics request and asserts neither can borrow the checker until the request is done (fails if the guard is removed). Fixed in 9afed7b00.

    hood hood commented on code on Nov 09, 2026 20:33 UTC
    Code Review
    tsc/internal/ls/semantictokens.go:1
    @@ -126,6 +126,7 @@ func SemanticTokensLegend(clientCapabilities lsproto.ResolvedSemanticTokensClien
     }
     
     func (l *LanguageService) ProvideSemanticTokens(ctx context.Context, documentURI lsproto.DocumentUri) (lsproto.SemanticTokensResponse, error) {
    +	ctx = core.WithCheckerLifetime(ctx, core.CheckerLifetimeCheckedFileQuery)

    Added TestSemanticTokensReuseCheckedDiagnosticsChecker in 9afed7b00. It runs the #51661 repro through the real session + language service: semantic tokens on a query checker, then diagnostics, then semantic tokens again. It asserts the second request actually took the lending path (hooked on the pool's log, so it can't pass by falling back), that the tokens are identical to the query-checker ones, and that diagnostics are still empty and unchanged afterwards.

    Range tokens go through the exact same GetChecker path, so I didn't duplicate the test for them tbh, happy to add it if you prefer.

    hood hood commented on Nov 09, 2026 20:39 UTC
    Author

    Yeah that's fair, tbh I couldn't fully convince myself either: even with the guard, classification still creates a handful of types/symbols on the borrowed checker (~6/32 per request on recharts' Line.tsx), and those could run before another file's check. So it's only "safe" for the cases I diffed, not in general.

    Converting this to a draft and parking it behind #65064 (happy to just close it if you prefer). If order independence lands, the payoff is roughly: in VS Code's request order, semantic tokens go 41 → 3 ms per request and ~41% less CPU while typing, since today each edit type-checks the file twice.

    If it's useful, I've got a small LSP typing harness (real keystrokes on a real repo, pulls diagnostics/semantic tokens/completions per key, dumps responses for byte-identity diffs): https://gist.github.com/hood/506e87027c83bbec1b44c1f687273349. Happy to run it against #65064 to help shake out ordering differences.

    Reviewers
    Assignees
    hood hood
    Labels
    risk: low confidence: 83.6% reviewed sensitive_module
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 77.2% (HIGH) · Higher = larger blast radius
    77.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 83.6% · Lower = rushed or shallow review
    83.6%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 12.7% · Safe to merge
    12.7%