Add support for TextDocument version on diagnostics push notifications
Problem
There's no reliable mechanism by which a generic LSP client can determine if it has the most recent diagnostics for a given TextDocument version because there's no direct association between TextDocument state and diagnostic state.
Requested change
typescript-language-server should support the version field of textDocument/publishDiagnostics that was introduced in the LSP spec version 3.15 (see diagnostics section in LSP spec v3.17 for details).
Why it matters
I'm attempting to use LSP for AI coding agents, and diagnostics are important feedback. When providing feedback to an AI Agent, it's important to provide that feedback in the context of the action the triggered the feedback. If diagnostics feedback is delayed, incomplete, or out of date, it confuses the agent, and this often sends it into a tailspin as it tries to diagnose and troubleshoot issues that are no longer relevant.
Other solutions
Pull diagnostics (as requested in #972) is arguably a better solution to this problem, especially since it handles grouping diagnostics (e.g. if an edit to one file breaks other files) from multiple files into a single diagnostics report, though I expect that this is probably a heavier lift than supporting the version field on push diagnostics.
I suppose this is doable but note that given how tsserver API works, you might get one set of diagnostics followed by a different (complete) set of diagnostics for the same document version. This is because tsserver can report different diagnostics from different sources at different times so we technically don't know if we have complete set of diagnostics and we send them as soon as we receive them.
@rchl Unfortunately that wouldn't be very useful to me, as my use case requires that I have the complete set of diagnostics for a given version (even if it's a null set).
Yeah, you will get a complete set eventually but we can't know if the one you've received is the complete already or if you'll get it a second later. LSP spec wise this is fine, I believe, so it can theoretically happen with any server.
I've raised https://github.com/microsoft/TypeScript/issues/62356 to account for the upstream changes that would need to be made to tsserver.
That said, there is something I thought about now.
The server could dynamically register pull diagnostics support twice, each time with a different identifier - see registration options
Then the client would still receive two sets of diagnostics but would be able to tell the source of each. And with server-specific logic, it could tell whether the response is complete from the fact that both were received (assuming that both are always sent which I haven't verified).