Fix stale pull diagnostics after document edits#4074
Conversation
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds a stale-snapshot guard for document diagnostics requests so that responses computed against an out-of-date snapshot are rejected with ContentModified, allowing the client to re-request fresh diagnostics.
Changes:
- Introduces
Session.IsSnapshotCurrentForDocumentto compare a request-time snapshot with the session's current snapshot for a document. - Adds a dedicated
registerDocumentDiagnosticHandlerthat captures the snapshot at request time and returnsContentModifiedif it becomes stale. - Adds a test verifying that stale diagnostic responses are rejected after the underlying file changes.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| internal/project/session.go | Adds IsSnapshotCurrentForDocument and sameFileHandle helpers to detect snapshot staleness. |
| internal/lsp/server.go | Replaces generic diagnostic handler registration with a custom one that checks snapshot freshness before sending the result. |
| internal/lsp/server_diagnostics_test.go | New test verifying diagnostic requests against stale snapshots return ContentModified. |
|
Hi @jakebailey, would you be the right person to take a look at this? This follows up on microsoft/vscode#317522 and touches LSP pull diagnostics / snapshot freshness. Happy to adjust the approach if there’s a better place to handle it. |
|
I'm not sure why this is required? Pull diags are requested at a specific point in time, and the answer at that point is valid. If there's a stale bug here, surely it's an LSP client bug. |
|
The comments on the linked issue also mentions the client so I'm not sure where this comes from |
Addresses microsoft/vscode#317522
Summary
This prevents stale
textDocument/diagnosticresponses from being applied after a document has changed.Diagnostic requests now keep the request-time snapshot, compute diagnostics against it, and then verify that the snapshot still matches the session's current document/default-project state before sending the response. If the document or project has moved on, the server returns
ContentModifiedso the client can retry with fresh state instead of showing outdated errors.Changes
Validation
go test ./internal/lsp -run TestDocumentDiagnosticCancelsStaleSnapshot -v -count=1 -timeout=45sgo test ./internal/lsp -count=1 -timeout=5mgo test ./internal/project -run TestSession -count=1 -timeout=2mnpx hereby check:formatnpx hereby lintnpx hereby buildnpx hereby testNote:
hereby testpassed with the repo warning that the TypeScript submodule is not cloned, so submodule-dependent tests may be skipped.Disclosure
AI assistance was used to prepare this patch; I reviewed, tested, and take responsibility for the changes.