@hydranium/core 1.0.0-next.17 → 1.0.0-next.174

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (505) hide show
  1. package/README.md +5 -1
  2. package/lib/documents/ast-document-manager.d.ts +190 -29
  3. package/lib/documents/ast-document-manager.d.ts.map +1 -1
  4. package/lib/documents/ast-document-manager.js +235 -38
  5. package/lib/documents/ast-document-manager.js.map +1 -1
  6. package/lib/documents/client-ids.d.ts +5 -19
  7. package/lib/documents/client-ids.d.ts.map +1 -1
  8. package/lib/documents/client-ids.js +5 -19
  9. package/lib/documents/client-ids.js.map +1 -1
  10. package/lib/documents/client-session-errors.d.ts +15 -0
  11. package/lib/documents/client-session-errors.d.ts.map +1 -0
  12. package/lib/documents/client-session-errors.js +15 -0
  13. package/lib/documents/client-session-errors.js.map +1 -0
  14. package/lib/documents/client-session-registry.d.ts +138 -0
  15. package/lib/documents/client-session-registry.d.ts.map +1 -0
  16. package/lib/documents/client-session-registry.js +187 -0
  17. package/lib/documents/client-session-registry.js.map +1 -0
  18. package/lib/documents/file-system-task-queue.d.ts +49 -0
  19. package/lib/documents/file-system-task-queue.d.ts.map +1 -0
  20. package/lib/documents/file-system-task-queue.js +34 -0
  21. package/lib/documents/file-system-task-queue.js.map +1 -0
  22. package/lib/documents/hydranium-text-documents.d.ts +447 -82
  23. package/lib/documents/hydranium-text-documents.d.ts.map +1 -1
  24. package/lib/documents/hydranium-text-documents.js +780 -160
  25. package/lib/documents/hydranium-text-documents.js.map +1 -1
  26. package/lib/documents/index.d.ts +3 -0
  27. package/lib/documents/index.d.ts.map +1 -1
  28. package/lib/documents/index.js +3 -0
  29. package/lib/documents/index.js.map +1 -1
  30. package/lib/documents/language-client-text-shadow.d.ts +41 -9
  31. package/lib/documents/language-client-text-shadow.d.ts.map +1 -1
  32. package/lib/documents/language-client-text-shadow.js +56 -11
  33. package/lib/documents/language-client-text-shadow.js.map +1 -1
  34. package/lib/documents/self-save-registry.d.ts +37 -6
  35. package/lib/documents/self-save-registry.d.ts.map +1 -1
  36. package/lib/documents/self-save-registry.js +28 -15
  37. package/lib/documents/self-save-registry.js.map +1 -1
  38. package/lib/index.d.ts +3 -0
  39. package/lib/index.d.ts.map +1 -1
  40. package/lib/index.js +6 -0
  41. package/lib/index.js.map +1 -1
  42. package/lib/langium/ast-extension/ast-node-builder.d.ts +32 -9
  43. package/lib/langium/ast-extension/ast-node-builder.d.ts.map +1 -1
  44. package/lib/langium/ast-extension/ast-node-builder.js +60 -22
  45. package/lib/langium/ast-extension/ast-node-builder.js.map +1 -1
  46. package/lib/langium/bootstrap.d.ts.map +1 -1
  47. package/lib/langium/bootstrap.js +16 -9
  48. package/lib/langium/bootstrap.js.map +1 -1
  49. package/lib/langium/config/configuration-provider.d.ts +81 -0
  50. package/lib/langium/config/configuration-provider.d.ts.map +1 -0
  51. package/lib/langium/config/configuration-provider.js +112 -0
  52. package/lib/langium/config/configuration-provider.js.map +1 -0
  53. package/lib/langium/config/index.d.ts +1 -0
  54. package/lib/langium/config/index.d.ts.map +1 -1
  55. package/lib/langium/config/index.js +1 -0
  56. package/lib/langium/config/index.js.map +1 -1
  57. package/lib/langium/diagnostics/lsp-logger.d.ts +17 -0
  58. package/lib/langium/diagnostics/lsp-logger.d.ts.map +1 -1
  59. package/lib/langium/diagnostics/lsp-logger.js +40 -6
  60. package/lib/langium/diagnostics/lsp-logger.js.map +1 -1
  61. package/lib/langium/document-builder/build-pipeline-integration.d.ts +21 -1
  62. package/lib/langium/document-builder/build-pipeline-integration.d.ts.map +1 -1
  63. package/lib/langium/document-builder/build-pipeline-integration.js +32 -5
  64. package/lib/langium/document-builder/build-pipeline-integration.js.map +1 -1
  65. package/lib/langium/document-builder/build-session.d.ts +93 -0
  66. package/lib/langium/document-builder/build-session.d.ts.map +1 -0
  67. package/lib/langium/document-builder/build-session.js +72 -0
  68. package/lib/langium/document-builder/build-session.js.map +1 -0
  69. package/lib/langium/document-builder/document-builder.d.ts +368 -21
  70. package/lib/langium/document-builder/document-builder.d.ts.map +1 -1
  71. package/lib/langium/document-builder/document-builder.js +641 -47
  72. package/lib/langium/document-builder/document-builder.js.map +1 -1
  73. package/lib/langium/document-builder/index.d.ts +1 -0
  74. package/lib/langium/document-builder/index.d.ts.map +1 -1
  75. package/lib/langium/document-builder/index.js +1 -0
  76. package/lib/langium/document-builder/index.js.map +1 -1
  77. package/lib/langium/integration-services.d.ts +45 -1
  78. package/lib/langium/integration-services.d.ts.map +1 -1
  79. package/lib/langium/integration-services.js +10 -5
  80. package/lib/langium/integration-services.js.map +1 -1
  81. package/lib/langium/integrity/integrity-rule.d.ts +13 -5
  82. package/lib/langium/integrity/integrity-rule.d.ts.map +1 -1
  83. package/lib/langium/integrity/integrity-rule.js.map +1 -1
  84. package/lib/langium/integrity/integrity-service.d.ts +58 -13
  85. package/lib/langium/integrity/integrity-service.d.ts.map +1 -1
  86. package/lib/langium/integrity/integrity-service.js +213 -29
  87. package/lib/langium/integrity/integrity-service.js.map +1 -1
  88. package/lib/langium/keys/containment.d.ts +64 -0
  89. package/lib/langium/keys/containment.d.ts.map +1 -0
  90. package/lib/langium/keys/containment.js +53 -0
  91. package/lib/langium/keys/containment.js.map +1 -0
  92. package/lib/langium/keys/index.d.ts +1 -0
  93. package/lib/langium/keys/index.d.ts.map +1 -1
  94. package/lib/langium/keys/index.js +1 -0
  95. package/lib/langium/keys/index.js.map +1 -1
  96. package/lib/langium/keys/name-based-key-provider.d.ts +4 -0
  97. package/lib/langium/keys/name-based-key-provider.d.ts.map +1 -1
  98. package/lib/langium/keys/name-based-key-provider.js +4 -0
  99. package/lib/langium/keys/name-based-key-provider.js.map +1 -1
  100. package/lib/langium/language-module.d.ts +27 -2
  101. package/lib/langium/language-module.d.ts.map +1 -1
  102. package/lib/langium/language-module.js +16 -2
  103. package/lib/langium/language-module.js.map +1 -1
  104. package/lib/langium/model-service/client-session.d.ts +307 -0
  105. package/lib/langium/model-service/client-session.d.ts.map +1 -0
  106. package/lib/langium/model-service/client-session.js +395 -0
  107. package/lib/langium/model-service/client-session.js.map +1 -0
  108. package/lib/langium/model-service/index.d.ts +1 -0
  109. package/lib/langium/model-service/index.d.ts.map +1 -1
  110. package/lib/langium/model-service/index.js +1 -0
  111. package/lib/langium/model-service/index.js.map +1 -1
  112. package/lib/langium/model-service/model-service.d.ts +276 -226
  113. package/lib/langium/model-service/model-service.d.ts.map +1 -1
  114. package/lib/langium/model-service/model-service.js +251 -375
  115. package/lib/langium/model-service/model-service.js.map +1 -1
  116. package/lib/langium/module.d.ts +144 -35
  117. package/lib/langium/module.d.ts.map +1 -1
  118. package/lib/langium/module.js +46 -35
  119. package/lib/langium/module.js.map +1 -1
  120. package/lib/langium/naming/name-provider.d.ts +25 -4
  121. package/lib/langium/naming/name-provider.d.ts.map +1 -1
  122. package/lib/langium/naming/name-provider.js +2 -1
  123. package/lib/langium/naming/name-provider.js.map +1 -1
  124. package/lib/langium/naming/name-separator-validation.d.ts +23 -0
  125. package/lib/langium/naming/name-separator-validation.d.ts.map +1 -1
  126. package/lib/langium/naming/name-separator-validation.js +30 -1
  127. package/lib/langium/naming/name-separator-validation.js.map +1 -1
  128. package/lib/langium/project/abstract-project-manager.d.ts.map +1 -1
  129. package/lib/langium/project/abstract-project-manager.js +9 -2
  130. package/lib/langium/project/abstract-project-manager.js.map +1 -1
  131. package/lib/langium/project/project-change-event.d.ts +2 -2
  132. package/lib/langium/residency/cst-residency-service.d.ts +31 -11
  133. package/lib/langium/residency/cst-residency-service.d.ts.map +1 -1
  134. package/lib/langium/residency/cst-residency-service.js +13 -56
  135. package/lib/langium/residency/cst-residency-service.js.map +1 -1
  136. package/lib/langium/scope/hydranium-scope-provider.d.ts +48 -13
  137. package/lib/langium/scope/hydranium-scope-provider.d.ts.map +1 -1
  138. package/lib/langium/scope/hydranium-scope-provider.js +56 -20
  139. package/lib/langium/scope/hydranium-scope-provider.js.map +1 -1
  140. package/lib/langium/serialization/abstract-serializer.d.ts +11 -20
  141. package/lib/langium/serialization/abstract-serializer.d.ts.map +1 -1
  142. package/lib/langium/serialization/abstract-serializer.js +12 -21
  143. package/lib/langium/serialization/abstract-serializer.js.map +1 -1
  144. package/lib/langium/service-registry.d.ts +15 -1
  145. package/lib/langium/service-registry.d.ts.map +1 -1
  146. package/lib/langium/service-registry.js +16 -0
  147. package/lib/langium/service-registry.js.map +1 -1
  148. package/lib/langium/shared-services.d.ts +15 -2
  149. package/lib/langium/shared-services.d.ts.map +1 -1
  150. package/lib/langium/shared-services.js.map +1 -1
  151. package/lib/langium/transfer/transfer-encoder.d.ts +60 -17
  152. package/lib/langium/transfer/transfer-encoder.d.ts.map +1 -1
  153. package/lib/langium/transfer/transfer-encoder.js +38 -16
  154. package/lib/langium/transfer/transfer-encoder.js.map +1 -1
  155. package/lib/langium/trivia/comment-preserver.d.ts +423 -0
  156. package/lib/langium/trivia/comment-preserver.d.ts.map +1 -0
  157. package/lib/langium/trivia/comment-preserver.js +906 -0
  158. package/lib/langium/trivia/comment-preserver.js.map +1 -0
  159. package/lib/langium/trivia/document-ending-preserver.d.ts +43 -0
  160. package/lib/langium/trivia/document-ending-preserver.d.ts.map +1 -0
  161. package/lib/langium/trivia/document-ending-preserver.js +48 -0
  162. package/lib/langium/trivia/document-ending-preserver.js.map +1 -0
  163. package/lib/langium/trivia/index.d.ts +14 -0
  164. package/lib/langium/trivia/index.d.ts.map +1 -0
  165. package/lib/langium/trivia/index.js +14 -0
  166. package/lib/langium/trivia/index.js.map +1 -0
  167. package/lib/langium/trivia/trivia-contribution.d.ts +37 -0
  168. package/lib/langium/trivia/trivia-contribution.d.ts.map +1 -0
  169. package/lib/langium/trivia/trivia-contribution.js +10 -0
  170. package/lib/langium/trivia/trivia-contribution.js.map +1 -0
  171. package/lib/langium/trivia/trivia-preserver.d.ts +50 -0
  172. package/lib/langium/trivia/trivia-preserver.d.ts.map +1 -0
  173. package/lib/langium/trivia/trivia-preserver.js +10 -0
  174. package/lib/langium/trivia/trivia-preserver.js.map +1 -0
  175. package/lib/langium/trivia/trivia-service.d.ts +70 -0
  176. package/lib/langium/trivia/trivia-service.d.ts.map +1 -0
  177. package/lib/langium/trivia/trivia-service.js +56 -0
  178. package/lib/langium/trivia/trivia-service.js.map +1 -0
  179. package/lib/langium/update-rewrite/update-rewrite.d.ts +1 -1
  180. package/lib/langium/validation/document-validator.d.ts +294 -10
  181. package/lib/langium/validation/document-validator.d.ts.map +1 -1
  182. package/lib/langium/validation/document-validator.js +448 -5
  183. package/lib/langium/validation/document-validator.js.map +1 -1
  184. package/lib/langium/workspace/document-uri-policy.d.ts +17 -12
  185. package/lib/langium/workspace/document-uri-policy.d.ts.map +1 -1
  186. package/lib/langium/workspace/document-uri-policy.js +10 -9
  187. package/lib/langium/workspace/document-uri-policy.js.map +1 -1
  188. package/lib/langium/workspace/file-not-found.d.ts +17 -0
  189. package/lib/langium/workspace/file-not-found.d.ts.map +1 -0
  190. package/lib/langium/workspace/file-not-found.js +20 -0
  191. package/lib/langium/workspace/file-not-found.js.map +1 -0
  192. package/lib/langium/workspace/file-system-provider.d.ts +84 -8
  193. package/lib/langium/workspace/file-system-provider.d.ts.map +1 -1
  194. package/lib/langium/workspace/file-system-provider.js +93 -14
  195. package/lib/langium/workspace/file-system-provider.js.map +1 -1
  196. package/lib/langium/workspace/hydranium-langium-document-factory.d.ts +25 -0
  197. package/lib/langium/workspace/hydranium-langium-document-factory.d.ts.map +1 -1
  198. package/lib/langium/workspace/hydranium-langium-document-factory.js +25 -0
  199. package/lib/langium/workspace/hydranium-langium-document-factory.js.map +1 -1
  200. package/lib/langium/workspace/hydranium-workspace-lock.d.ts +43 -1
  201. package/lib/langium/workspace/hydranium-workspace-lock.d.ts.map +1 -1
  202. package/lib/langium/workspace/hydranium-workspace-lock.js +58 -1
  203. package/lib/langium/workspace/hydranium-workspace-lock.js.map +1 -1
  204. package/lib/langium/workspace/hydranium-workspace-manager.d.ts +81 -8
  205. package/lib/langium/workspace/hydranium-workspace-manager.d.ts.map +1 -1
  206. package/lib/langium/workspace/hydranium-workspace-manager.js +128 -10
  207. package/lib/langium/workspace/hydranium-workspace-manager.js.map +1 -1
  208. package/lib/langium/workspace/in-memory-file-system-provider.d.ts +25 -2
  209. package/lib/langium/workspace/in-memory-file-system-provider.d.ts.map +1 -1
  210. package/lib/langium/workspace/in-memory-file-system-provider.js +41 -15
  211. package/lib/langium/workspace/in-memory-file-system-provider.js.map +1 -1
  212. package/lib/langium/workspace/initialize-workspace.d.ts +31 -2
  213. package/lib/langium/workspace/initialize-workspace.d.ts.map +1 -1
  214. package/lib/langium/workspace/initialize-workspace.js +13 -5
  215. package/lib/langium/workspace/initialize-workspace.js.map +1 -1
  216. package/lib/langium/workspace/langium-documents.d.ts +79 -17
  217. package/lib/langium/workspace/langium-documents.d.ts.map +1 -1
  218. package/lib/langium/workspace/langium-documents.js +91 -29
  219. package/lib/langium/workspace/langium-documents.js.map +1 -1
  220. package/lib/langium/workspace/persistent-file-system-provider.d.ts +0 -7
  221. package/lib/langium/workspace/persistent-file-system-provider.d.ts.map +1 -1
  222. package/lib/langium/workspace/persistent-file-system-provider.js +0 -7
  223. package/lib/langium/workspace/persistent-file-system-provider.js.map +1 -1
  224. package/lib/langium/workspace/virtual-document.d.ts +48 -15
  225. package/lib/langium/workspace/virtual-document.d.ts.map +1 -1
  226. package/lib/langium/workspace/virtual-document.js +78 -18
  227. package/lib/langium/workspace/virtual-document.js.map +1 -1
  228. package/lib/langium/workspace/write-lock-scope.d.ts +20 -10
  229. package/lib/langium/workspace/write-lock-scope.d.ts.map +1 -1
  230. package/lib/langium/workspace/write-lock-scope.js +12 -4
  231. package/lib/langium/workspace/write-lock-scope.js.map +1 -1
  232. package/lib/locale/index.d.ts +10 -0
  233. package/lib/locale/index.d.ts.map +1 -0
  234. package/lib/locale/index.js +10 -0
  235. package/lib/locale/index.js.map +1 -0
  236. package/lib/locale/server-locale.d.ts +73 -0
  237. package/lib/locale/server-locale.d.ts.map +1 -0
  238. package/lib/locale/server-locale.js +61 -0
  239. package/lib/locale/server-locale.js.map +1 -0
  240. package/lib/lsp/completion/hydranium-completion-provider.d.ts +44 -2
  241. package/lib/lsp/completion/hydranium-completion-provider.d.ts.map +1 -1
  242. package/lib/lsp/completion/hydranium-completion-provider.js +68 -0
  243. package/lib/lsp/completion/hydranium-completion-provider.js.map +1 -1
  244. package/lib/lsp/connection-features.d.ts +23 -0
  245. package/lib/lsp/connection-features.d.ts.map +1 -0
  246. package/lib/lsp/connection-features.js +60 -0
  247. package/lib/lsp/connection-features.js.map +1 -0
  248. package/lib/lsp/diagnostics-connection.d.ts +27 -0
  249. package/lib/lsp/diagnostics-connection.d.ts.map +1 -0
  250. package/lib/lsp/diagnostics-connection.js +53 -0
  251. package/lib/lsp/diagnostics-connection.js.map +1 -0
  252. package/lib/lsp/hydranium-document-update-handler.d.ts +90 -79
  253. package/lib/lsp/hydranium-document-update-handler.d.ts.map +1 -1
  254. package/lib/lsp/hydranium-document-update-handler.js +152 -99
  255. package/lib/lsp/hydranium-document-update-handler.js.map +1 -1
  256. package/lib/lsp/index.d.ts +3 -1
  257. package/lib/lsp/index.d.ts.map +1 -1
  258. package/lib/lsp/index.js +3 -1
  259. package/lib/lsp/index.js.map +1 -1
  260. package/lib/lsp/lsp-latency.d.ts +39 -0
  261. package/lib/lsp/lsp-latency.d.ts.map +1 -0
  262. package/lib/lsp/lsp-latency.js +58 -0
  263. package/lib/lsp/lsp-latency.js.map +1 -0
  264. package/lib/lsp/semantic-token-provider.d.ts +34 -1
  265. package/lib/lsp/semantic-token-provider.d.ts.map +1 -1
  266. package/lib/lsp/semantic-token-provider.js +55 -8
  267. package/lib/lsp/semantic-token-provider.js.map +1 -1
  268. package/lib/lsp/shared-module.d.ts +4 -8
  269. package/lib/lsp/shared-module.d.ts.map +1 -1
  270. package/lib/lsp/shared-module.js.map +1 -1
  271. package/lib/lsp/start-language-server.d.ts +8 -7
  272. package/lib/lsp/start-language-server.d.ts.map +1 -1
  273. package/lib/lsp/start-language-server.js +13 -7
  274. package/lib/lsp/start-language-server.js.map +1 -1
  275. package/lib/messages/carriers.d.ts +54 -0
  276. package/lib/messages/carriers.d.ts.map +1 -0
  277. package/lib/messages/carriers.js +62 -0
  278. package/lib/messages/carriers.js.map +1 -0
  279. package/lib/messages/index.d.ts +25 -0
  280. package/lib/messages/index.d.ts.map +1 -0
  281. package/lib/messages/index.js +25 -0
  282. package/lib/messages/index.js.map +1 -0
  283. package/lib/messages/renderer.d.ts +142 -0
  284. package/lib/messages/renderer.d.ts.map +1 -0
  285. package/lib/messages/renderer.js +165 -0
  286. package/lib/messages/renderer.js.map +1 -0
  287. package/lib/node/heap-ceiling.d.ts +61 -0
  288. package/lib/node/heap-ceiling.d.ts.map +1 -0
  289. package/lib/node/heap-ceiling.js +72 -0
  290. package/lib/node/heap-ceiling.js.map +1 -0
  291. package/lib/node/index.d.ts +1 -0
  292. package/lib/node/index.d.ts.map +1 -1
  293. package/lib/node/index.js +1 -0
  294. package/lib/node/index.js.map +1 -1
  295. package/lib/node/latency-from-env.d.ts +1 -1
  296. package/lib/node/latency-from-env.js +1 -1
  297. package/lib/node/measure-memory.d.ts.map +1 -1
  298. package/lib/node/measure-memory.js +1 -0
  299. package/lib/node/measure-memory.js.map +1 -1
  300. package/lib/node/node-file-system-provider.d.ts +39 -4
  301. package/lib/node/node-file-system-provider.d.ts.map +1 -1
  302. package/lib/node/node-file-system-provider.js +109 -49
  303. package/lib/node/node-file-system-provider.js.map +1 -1
  304. package/lib/node/profile-capture.d.ts +8 -2
  305. package/lib/node/profile-capture.d.ts.map +1 -1
  306. package/lib/node/profile-capture.js.map +1 -1
  307. package/lib/node/profiling-run.d.ts +2 -2
  308. package/lib/node/profiling-run.js +1 -1
  309. package/lib/node/profiling-run.js.map +1 -1
  310. package/lib/node/rename-over-open-readers.d.ts +52 -0
  311. package/lib/node/rename-over-open-readers.d.ts.map +1 -0
  312. package/lib/node/rename-over-open-readers.js +66 -0
  313. package/lib/node/rename-over-open-readers.js.map +1 -0
  314. package/lib/node/socket-launcher.d.ts +16 -1
  315. package/lib/node/socket-launcher.d.ts.map +1 -1
  316. package/lib/node/socket-launcher.js +4 -3
  317. package/lib/node/socket-launcher.js.map +1 -1
  318. package/lib/node/stdio-launcher.d.ts +1 -1
  319. package/lib/node/stdio-launcher.d.ts.map +1 -1
  320. package/lib/node/stdio-launcher.js +1 -1
  321. package/lib/node/stdio-launcher.js.map +1 -1
  322. package/lib/testing/fake-document.d.ts +16 -4
  323. package/lib/testing/fake-document.d.ts.map +1 -1
  324. package/lib/testing/fake-document.js +18 -3
  325. package/lib/testing/fake-document.js.map +1 -1
  326. package/lib/testing/make-noop-shared-services.d.ts +14 -0
  327. package/lib/testing/make-noop-shared-services.d.ts.map +1 -1
  328. package/lib/testing/make-noop-shared-services.js +28 -2
  329. package/lib/testing/make-noop-shared-services.js.map +1 -1
  330. package/lib/testing/make-test-services.d.ts +62 -14
  331. package/lib/testing/make-test-services.d.ts.map +1 -1
  332. package/lib/testing/make-test-services.js +30 -9
  333. package/lib/testing/make-test-services.js.map +1 -1
  334. package/lib/testing/node/index.d.ts +1 -0
  335. package/lib/testing/node/index.d.ts.map +1 -1
  336. package/lib/testing/node/index.js +1 -0
  337. package/lib/testing/node/index.js.map +1 -1
  338. package/lib/testing/node/lsp-harness.d.ts.map +1 -1
  339. package/lib/testing/node/lsp-harness.js +2 -0
  340. package/lib/testing/node/lsp-harness.js.map +1 -1
  341. package/lib/testing/node/lsp-server-connection.d.ts +11 -3
  342. package/lib/testing/node/lsp-server-connection.d.ts.map +1 -1
  343. package/lib/testing/node/lsp-server-connection.js +42 -9
  344. package/lib/testing/node/lsp-server-connection.js.map +1 -1
  345. package/lib/testing/node/scratch-workspace.d.ts.map +1 -1
  346. package/lib/testing/node/scratch-workspace.js +10 -5
  347. package/lib/testing/node/scratch-workspace.js.map +1 -1
  348. package/lib/testing/node/spawned-server.d.ts +14 -0
  349. package/lib/testing/node/spawned-server.d.ts.map +1 -1
  350. package/lib/testing/node/spawned-server.js +33 -1
  351. package/lib/testing/node/spawned-server.js.map +1 -1
  352. package/lib/testing/node/unhandled-rejections.d.ts +20 -0
  353. package/lib/testing/node/unhandled-rejections.d.ts.map +1 -0
  354. package/lib/testing/node/unhandled-rejections.js +32 -0
  355. package/lib/testing/node/unhandled-rejections.js.map +1 -0
  356. package/lib/testing/playwright/flaky-network-proxy.d.ts +76 -0
  357. package/lib/testing/playwright/flaky-network-proxy.d.ts.map +1 -0
  358. package/lib/testing/playwright/flaky-network-proxy.js +164 -0
  359. package/lib/testing/playwright/flaky-network-proxy.js.map +1 -0
  360. package/lib/testing/playwright/index.d.ts +1 -0
  361. package/lib/testing/playwright/index.d.ts.map +1 -1
  362. package/lib/testing/playwright/index.js +1 -0
  363. package/lib/testing/playwright/index.js.map +1 -1
  364. package/lib/testing/playwright/server-log-capture.d.ts +14 -4
  365. package/lib/testing/playwright/server-log-capture.d.ts.map +1 -1
  366. package/lib/testing/playwright/server-log-capture.js +21 -4
  367. package/lib/testing/playwright/server-log-capture.js.map +1 -1
  368. package/lib/testing/playwright/server-log-rename-reporter.d.ts.map +1 -1
  369. package/lib/testing/playwright/server-log-rename-reporter.js +10 -1
  370. package/lib/testing/playwright/server-log-rename-reporter.js.map +1 -1
  371. package/lib/testing/run-update-pipeline.d.ts +4 -4
  372. package/lib/testing/run-update-pipeline.d.ts.map +1 -1
  373. package/lib/testing/stub-ast-document-manager.d.ts +10 -9
  374. package/lib/testing/stub-ast-document-manager.d.ts.map +1 -1
  375. package/lib/testing/stub-ast-document-manager.js +17 -9
  376. package/lib/testing/stub-ast-document-manager.js.map +1 -1
  377. package/lib/testing/stub-document-builder.d.ts +26 -11
  378. package/lib/testing/stub-document-builder.d.ts.map +1 -1
  379. package/lib/testing/stub-document-builder.js +57 -4
  380. package/lib/testing/stub-document-builder.js.map +1 -1
  381. package/lib/testing/stub-hydranium-text-documents.d.ts +22 -7
  382. package/lib/testing/stub-hydranium-text-documents.d.ts.map +1 -1
  383. package/lib/testing/stub-hydranium-text-documents.js +92 -15
  384. package/lib/testing/stub-hydranium-text-documents.js.map +1 -1
  385. package/lib/testing/stub-langium-documents.d.ts +3 -2
  386. package/lib/testing/stub-langium-documents.d.ts.map +1 -1
  387. package/lib/testing/stub-langium-documents.js.map +1 -1
  388. package/lib/testing/stub-model-service.d.ts +7 -6
  389. package/lib/testing/stub-model-service.d.ts.map +1 -1
  390. package/lib/testing/stub-model-service.js +4 -4
  391. package/lib/testing/stub-model-service.js.map +1 -1
  392. package/lib/util/connection-liveness.d.ts +7 -6
  393. package/lib/util/connection-liveness.d.ts.map +1 -1
  394. package/lib/util/connection-liveness.js +35 -14
  395. package/lib/util/connection-liveness.js.map +1 -1
  396. package/package.json +17 -8
  397. package/src/documents/ast-document-manager.ts +348 -64
  398. package/src/documents/client-ids.ts +5 -22
  399. package/src/documents/client-session-errors.ts +24 -0
  400. package/src/documents/client-session-registry.ts +240 -0
  401. package/src/documents/file-system-task-queue.ts +68 -0
  402. package/src/documents/hydranium-text-documents.ts +971 -202
  403. package/src/documents/index.ts +3 -0
  404. package/src/documents/language-client-text-shadow.ts +59 -11
  405. package/src/documents/self-save-registry.ts +52 -13
  406. package/src/index.ts +6 -0
  407. package/src/langium/ast-extension/ast-node-builder.ts +66 -22
  408. package/src/langium/bootstrap.ts +16 -9
  409. package/src/langium/config/configuration-provider.ts +119 -0
  410. package/src/langium/config/index.ts +1 -0
  411. package/src/langium/diagnostics/lsp-logger.ts +41 -6
  412. package/src/langium/document-builder/build-pipeline-integration.ts +50 -5
  413. package/src/langium/document-builder/build-session.ts +87 -0
  414. package/src/langium/document-builder/document-builder.ts +724 -46
  415. package/src/langium/document-builder/index.ts +1 -0
  416. package/src/langium/integration-services.ts +67 -6
  417. package/src/langium/integrity/integrity-rule.ts +13 -5
  418. package/src/langium/integrity/integrity-service.ts +229 -30
  419. package/src/langium/keys/containment.ts +87 -0
  420. package/src/langium/keys/index.ts +1 -0
  421. package/src/langium/keys/name-based-key-provider.ts +4 -0
  422. package/src/langium/language-module.ts +42 -4
  423. package/src/langium/model-service/client-session.ts +606 -0
  424. package/src/langium/model-service/index.ts +1 -0
  425. package/src/langium/model-service/model-service.ts +418 -399
  426. package/src/langium/module.ts +193 -64
  427. package/src/langium/naming/name-provider.ts +27 -5
  428. package/src/langium/naming/name-separator-validation.ts +34 -5
  429. package/src/langium/project/abstract-project-manager.ts +9 -1
  430. package/src/langium/project/project-change-event.ts +2 -2
  431. package/src/langium/residency/cst-residency-service.ts +36 -14
  432. package/src/langium/scope/hydranium-scope-provider.ts +60 -22
  433. package/src/langium/serialization/abstract-serializer.ts +13 -32
  434. package/src/langium/service-registry.ts +19 -2
  435. package/src/langium/shared-services.ts +19 -2
  436. package/src/langium/transfer/transfer-encoder.ts +85 -24
  437. package/src/langium/trivia/comment-preserver.ts +1100 -0
  438. package/src/langium/trivia/document-ending-preserver.ts +55 -0
  439. package/src/langium/trivia/index.ts +14 -0
  440. package/src/langium/trivia/trivia-contribution.ts +39 -0
  441. package/src/langium/trivia/trivia-preserver.ts +54 -0
  442. package/src/langium/trivia/trivia-service.ts +99 -0
  443. package/src/langium/update-rewrite/update-rewrite.ts +1 -1
  444. package/src/langium/validation/document-validator.ts +537 -11
  445. package/src/langium/workspace/document-uri-policy.ts +17 -12
  446. package/src/langium/workspace/file-not-found.ts +23 -0
  447. package/src/langium/workspace/file-system-provider.ts +153 -16
  448. package/src/langium/workspace/hydranium-langium-document-factory.ts +34 -0
  449. package/src/langium/workspace/hydranium-workspace-lock.ts +86 -2
  450. package/src/langium/workspace/hydranium-workspace-manager.ts +153 -13
  451. package/src/langium/workspace/in-memory-file-system-provider.ts +47 -16
  452. package/src/langium/workspace/initialize-workspace.ts +43 -5
  453. package/src/langium/workspace/langium-documents.ts +113 -33
  454. package/src/langium/workspace/persistent-file-system-provider.ts +0 -7
  455. package/src/langium/workspace/virtual-document.ts +96 -19
  456. package/src/langium/workspace/write-lock-scope.ts +24 -11
  457. package/src/locale/index.ts +10 -0
  458. package/src/locale/server-locale.ts +86 -0
  459. package/src/lsp/completion/hydranium-completion-provider.ts +82 -2
  460. package/src/lsp/connection-features.ts +67 -0
  461. package/src/lsp/diagnostics-connection.ts +56 -0
  462. package/src/lsp/hydranium-document-update-handler.ts +182 -106
  463. package/src/lsp/index.ts +3 -1
  464. package/src/lsp/lsp-latency.ts +62 -0
  465. package/src/lsp/semantic-token-provider.ts +74 -7
  466. package/src/lsp/shared-module.ts +4 -8
  467. package/src/lsp/start-language-server.ts +13 -7
  468. package/src/messages/carriers.ts +78 -0
  469. package/src/messages/index.ts +36 -0
  470. package/src/messages/renderer.ts +212 -0
  471. package/src/node/heap-ceiling.ts +108 -0
  472. package/src/node/index.ts +1 -0
  473. package/src/node/latency-from-env.ts +1 -1
  474. package/src/node/measure-memory.ts +1 -0
  475. package/src/node/node-file-system-provider.ts +122 -51
  476. package/src/node/profile-capture.ts +8 -2
  477. package/src/node/profiling-run.ts +3 -3
  478. package/src/node/rename-over-open-readers.ts +78 -0
  479. package/src/node/socket-launcher.ts +19 -4
  480. package/src/node/stdio-launcher.ts +1 -2
  481. package/src/testing/fake-document.ts +24 -5
  482. package/src/testing/make-noop-shared-services.ts +50 -2
  483. package/src/testing/make-test-services.ts +106 -30
  484. package/src/testing/node/index.ts +1 -0
  485. package/src/testing/node/lsp-harness.ts +2 -0
  486. package/src/testing/node/lsp-server-connection.ts +61 -11
  487. package/src/testing/node/scratch-workspace.ts +10 -5
  488. package/src/testing/node/spawned-server.ts +35 -1
  489. package/src/testing/node/unhandled-rejections.ts +31 -0
  490. package/src/testing/playwright/flaky-network-proxy.ts +240 -0
  491. package/src/testing/playwright/index.ts +1 -0
  492. package/src/testing/playwright/server-log-capture.ts +24 -5
  493. package/src/testing/playwright/server-log-rename-reporter.ts +12 -1
  494. package/src/testing/run-update-pipeline.ts +4 -4
  495. package/src/testing/stub-ast-document-manager.ts +28 -21
  496. package/src/testing/stub-document-builder.ts +83 -17
  497. package/src/testing/stub-hydranium-text-documents.ts +127 -22
  498. package/src/testing/stub-langium-documents.ts +3 -2
  499. package/src/testing/stub-model-service.ts +9 -8
  500. package/src/util/connection-liveness.ts +37 -14
  501. package/lib/lsp/instrument-connection.d.ts +0 -36
  502. package/lib/lsp/instrument-connection.d.ts.map +0 -1
  503. package/lib/lsp/instrument-connection.js +0 -62
  504. package/lib/lsp/instrument-connection.js.map +0 -1
  505. package/src/lsp/instrument-connection.ts +0 -66
@@ -20,7 +20,7 @@
20
20
  // on lsp-server, contradicting the peer architecture.
21
21
  // eslint-disable-next-line @typescript-eslint/no-restricted-imports
22
22
  import { NormalizedTextDocuments } from '@hydranium/langium/lsp';
23
- import { UriUtils } from '@hydranium/langium';
23
+ import { type URI, UriUtils } from '@hydranium/langium';
24
24
  import { type ServerSharedServices } from '../langium/module.js';
25
25
  import {
26
26
  type ApplyWorkspaceEditResult,
@@ -31,12 +31,13 @@ import {
31
31
  type DidOpenTextDocumentParams,
32
32
  type DidSaveTextDocumentParams,
33
33
  type Disposable,
34
- type Emitter,
34
+ Emitter,
35
35
  type Event,
36
36
  type HandlerResult,
37
37
  OptionalVersionedTextDocumentIdentifier,
38
38
  type RequestHandler,
39
39
  type TextDocumentChangeEvent,
40
+ TextDocumentContentChangeEvent as ContentChange,
40
41
  TextDocumentEdit,
41
42
  TextDocumentSyncKind,
42
43
  type TextDocumentWillSaveEvent,
@@ -45,9 +46,21 @@ import {
45
46
  type WillSaveTextDocumentParams
46
47
  } from 'vscode-languageserver';
47
48
  import { type DocumentUri, TextDocument, type TextDocumentContentChangeEvent } from 'vscode-languageserver-textdocument';
48
- import { type CanonicalUri, type LanguageClientUri, asLanguageClientUri, DisposableCollection, type Tracer } from '@hydranium/protocol';
49
+ import {
50
+ type CanonicalUri,
51
+ type LanguageClientUri,
52
+ asLanguageClientUri,
53
+ DisposableCollection,
54
+ type Stopwatch,
55
+ textHash,
56
+ type Tracer
57
+ } from '@hydranium/protocol';
49
58
  import { type LogNameOptions } from '../langium/diagnostics/logger.js';
59
+ import { HYDRANIUM_BUILD_REASONS } from '../langium/document-builder/document-builder.js';
60
+ import { isConnectionGoneError } from '../util/connection-liveness.js';
50
61
  import { LANGUAGE_CLIENT_ID } from './client-ids.js';
62
+ import { INTEGRITY_CLIENT_ID } from '../langium/integrity/integrity-rule.js';
63
+ import { type ClientSessionClosedEvent, ClientSessionRegistry, type SessionEndCause } from './client-session-registry.js';
51
64
  import { isFullReplace, LanguageClientTextShadow } from './language-client-text-shadow.js';
52
65
 
53
66
  /**
@@ -74,6 +87,62 @@ export interface HydraniumTextDocumentsOptions<T extends TextDocument = TextDocu
74
87
  * Adopters with a custom text-document type (rare) pass their factory.
75
88
  */
76
89
  readonly configuration?: TextDocumentsConfiguration<T>;
90
+ /**
91
+ * How long a document whose last open closed because its client's
92
+ * connection was lost keeps its text before it reverts to disk. An open by
93
+ * a client lost from the document, within this time of its own loss,
94
+ * cancels the revert, so a client that registers again under its id after
95
+ * a dropped connection finds its unsaved edits. Any other open releases the
96
+ * document first and then opens it as a first open does, from the text the
97
+ * opener supplies (an editor's own) or else from the file. Meanwhile the
98
+ * document counts as open for the integrity service, which therefore writes
99
+ * none of its unsaved text to disk. A close the client makes itself, or
100
+ * ending its session, reverts at once whatever this is.
101
+ *
102
+ * Defaults to 10 s. `0` reverts such a document at once as well, released
103
+ * in the close itself rather than on a timer.
104
+ */
105
+ readonly revertGraceMs?: number;
106
+ }
107
+
108
+ /**
109
+ * The default of {@link HydraniumTextDocumentsOptions.revertGraceMs}: long
110
+ * enough for a client whose connection dropped to register again and reopen
111
+ * its documents, which a data client does as soon as it has a connection.
112
+ */
113
+ const DEFAULT_REVERT_GRACE_MS = 10_000;
114
+
115
+ /** Delivered by {@link HydraniumTextDocuments.onDidSaveInLanguageClient}. */
116
+ export interface LanguageClientSavedEvent {
117
+ readonly uri: string;
118
+ }
119
+
120
+ /** Delivered by {@link HydraniumTextDocuments.onDidCloseLastOpen}. */
121
+ export interface LastOpenClosedEvent {
122
+ readonly uri: CanonicalUri;
123
+ }
124
+
125
+ /** Delivered by {@link HydraniumTextDocuments.onDidChangeDirty}. */
126
+ export interface DocumentDirtyChangedEvent {
127
+ readonly uri: CanonicalUri;
128
+ /** The new answer of {@link HydraniumTextDocuments.isDirty}. */
129
+ readonly dirty: boolean;
130
+ }
131
+
132
+ /**
133
+ * The language client's open of a document under one URI, from its didOpen to
134
+ * its didClose. Each URI is its own editor buffer with its own version counter,
135
+ * so a file reached through a symlink and its real path has one of these each.
136
+ */
137
+ export interface LanguageClientDocumentState {
138
+ /** The version the client last declared for this URI. */
139
+ declaredVersion: number;
140
+ /**
141
+ * The version an applied versioned push moved this URI to, ahead of its
142
+ * echo. Apart from `declaredVersion`, whose staleness guard would drop that
143
+ * echo and strand its pending push.
144
+ */
145
+ pushedVersion?: number;
77
146
  }
78
147
 
79
148
  /**
@@ -89,32 +158,60 @@ export interface HydraniumTextDocumentsOptions<T extends TextDocument = TextDocu
89
158
  * separately-tested diff/apply-verify abstraction that owns its own baseline
90
159
  * text; folding its storage here would couple a clean utility to this record
91
160
  * for no real gain.
161
+ *
162
+ * Returned by the `protected` {@link HydraniumTextDocuments.trackingFor}, so an
163
+ * override has to name it. Restating the shape structurally instead compiles
164
+ * until a field is added here, and then fails at the adopter rather than at the
165
+ * change that caused it.
92
166
  */
93
- interface DocumentTracking {
94
- /** Client ids currently holding this document open (multi-client membership). */
95
- readonly clients: Set<string>;
167
+ export interface DocumentTrackingRecord {
96
168
  /** Author of each version, sparse-indexed by the SHARED (server-assigned) version number. */
97
169
  readonly versionAuthors: string[];
98
170
  /**
99
171
  * Last version id each client declared for this document (didOpen baseline,
100
- * advanced by every accepted didChange). Client version ids are CLIENT-owned
101
- * per LSP (Monaco numbers its own buffer) and are used ONLY for this
102
- * per-client staleness guard — they never leak into the shared version
103
- * sequence, which the server assigns (see {@link HydraniumTextDocuments.__versionSequences}).
172
+ * advanced by every accepted didChange), for the per-client staleness guard.
173
+ * Client version ids are CLIENT-owned per LSP (Monaco numbers its own
174
+ * buffer) — they never leak into the shared version sequence, which the
175
+ * server assigns (see {@link HydraniumTextDocuments.__versionSequences}).
176
+ * The language client's entry is the latest any of its URIs declared; the
177
+ * store checks and addresses it per URI, through
178
+ * {@link DocumentTrackingRecord.languageClientDocuments}.
104
179
  */
105
180
  readonly clientVersions: Map<string, number>;
106
181
  /** Content staged by integrity rules for a closed document. Consumed on next open. */
107
182
  pendingContent?: string;
108
183
  /**
109
- * The URI(s) the LSP textual language client opened this (canonically-keyed)
110
- * document under. Usually one; a set because the same physical file can be
111
- * opened under more than one URI (a symlink path and its real path). This is
112
- * the egress address: `applyEditToLanguageClient`
113
- * targets these, since the document is *keyed* by its canonical identity but
114
- * Monaco holds it under the URI it opened. Lifecycle-bound — cleared with the
115
- * record on last close.
184
+ * Each URI the LSP textual language client opened this (canonically-keyed)
185
+ * document under, with that open's state. Usually one; more when the same
186
+ * file is opened under a symlink path and its real path. These are the
187
+ * egress addresses: the document is *keyed* by its canonical identity, but
188
+ * Monaco holds it under the URI it opened. The language client holds the
189
+ * document while any entry remains.
116
190
  */
117
- languageClientUris?: Set<LanguageClientUri>;
191
+ languageClientDocuments?: Map<LanguageClientUri, LanguageClientDocumentState>;
192
+ /**
193
+ * The clients whose close of this document was caused by a lost connection
194
+ * and that have not opened it again, each with a stopwatch started at its
195
+ * loss on the store's `Clock`. While the document waits out the revert grace, only an
196
+ * open by one of them within its own
197
+ * {@link HydraniumTextDocumentsOptions.revertGraceMs} of that loss cancels
198
+ * the revert. Every such client counts, not only the last to close: one
199
+ * connection's sessions all end lost together, and any of them may reopen
200
+ * first. Without the time limit, a client that returns late inherits the
201
+ * unsaved text of a holder lost after it. A stopwatch rather than a `now()`
202
+ * reading, because a wall-clock step would otherwise expire a claim early or
203
+ * revive one past its grace, against a grace timer that the step leaves
204
+ * alone. An entry past its grace counts as any other client's and is pruned
205
+ * at the next open of the document.
206
+ */
207
+ lostClients?: Map<string, Stopwatch>;
208
+ /**
209
+ * The text the server last knew the file to hold, `undefined` for no file.
210
+ * Set by the first open and moved by {@link HydraniumTextDocuments.updateDiskBaseline}.
211
+ */
212
+ diskBaseline?: string;
213
+ /** The last answer {@link HydraniumTextDocuments.onDidChangeDirty} announced. */
214
+ dirty?: boolean;
118
215
  }
119
216
 
120
217
  /**
@@ -128,16 +225,31 @@ export interface OpenDocument {
128
225
  readonly clients: readonly string[];
129
226
  }
130
227
 
228
+ /**
229
+ * What {@link HydraniumTextDocuments.commitRepair} did with an integrity repair.
230
+ *
231
+ * Three outcomes rather than a document-or-nothing, because the two ways of
232
+ * getting nothing call for opposite responses: `not-open` means there is no
233
+ * store document to correct and the caller carries on with the one it holds,
234
+ * while `stale` means there IS one and it has moved past the text the repair
235
+ * was computed against — so carrying on would persist a correction over an edit
236
+ * that superseded it. Collapsing them lets the second read as permission to
237
+ * proceed, which is the failure this shape exists to make unrepresentable.
238
+ */
239
+ export type RepairCommit<T extends TextDocument> =
240
+ { readonly status: 'committed'; readonly document: T } | { readonly status: 'stale' } | { readonly status: 'not-open' };
241
+
131
242
  /**
132
243
  * Where a URI's shared version sequence left off — written once at last-client
133
244
  * close, consulted at the next open so the sequence CONTINUES instead of
134
245
  * restarting at whatever version id the reopening client declares. One entry
135
- * of {@link HydraniumTextDocuments.__versionSequences}.
246
+ * of {@link HydraniumTextDocuments.__versionSequences} — a `protected` field, so
247
+ * a subclass reading the map has to name what it holds.
136
248
  */
137
- interface VersionSequence {
249
+ export interface VersionSequence {
138
250
  /** The shared version at last-client close. */
139
251
  readonly version: number;
140
- /** {@link contentHash} of the synced text at last-client close. */
252
+ /** {@link textHash} of the synced text at last-client close. */
141
253
  readonly contentHash: string;
142
254
  }
143
255
 
@@ -161,76 +273,80 @@ interface ConnectionWithTextDocumentSync {
161
273
  /**
162
274
  * One text pushed to the LSP textual language client by
163
275
  * {@link HydraniumTextDocuments.applyEditToLanguageClient} whose echo has not
164
- * come back yet. One entry of {@link HydraniumTextDocuments.__pendingPushes}.
276
+ * come back yet. One entry of {@link HydraniumTextDocuments.__pendingPushes} —
277
+ * a `protected` field, so a subclass reading the queue has to name what it holds.
165
278
  */
166
- interface PendingPush {
279
+ export interface PendingLanguageClientPush {
167
280
  /**
168
- * The text the client held BEFORE this push — the shadow baseline the
169
- * push's edits were diffed against, and therefore the text the client's
170
- * echo addresses with its ranges.
281
+ * The text the client held BEFORE this push, and therefore the text its
282
+ * echo addresses with its ranges. Wider than the baseline the push's edits
283
+ * were diffed against: a full replace is sent with no diff baseline and
284
+ * still lands on a buffer the echo is keyed to.
171
285
  *
172
- * `undefined` when the push was a full replace sent with no baseline (a
173
- * first sync, or the recovery push after a rejection). There is then no
174
- * knowing what the client held, so the echo is reconstructed against the
175
- * synced text instead — which is sound for a full replace, whose
176
- * application is position-independent.
286
+ * `undefined` only when that buffer is unknown — the client never declared
287
+ * one, or a rejection invalidated what was tracked. The echo is then
288
+ * reconstructed against the synced text, which is sound only for a
289
+ * position-independent (full-text) change.
177
290
  */
178
291
  readonly before: string | undefined;
179
- /** {@link contentHash} of the text this push moves the client to. */
292
+ /** {@link textHash} of the text this push moves the client to. */
180
293
  readonly afterHash: string;
181
294
  }
182
295
 
183
296
  /**
184
297
  * What an incoming language-client change turns out to be once reconstructed
185
- * against the pushes still in flight — the return of
186
- * {@link HydraniumTextDocuments.classifyLanguageClientChange}.
298
+ * against the buffer its ranges address — the return of
299
+ * {@link HydraniumTextDocuments.classifyLanguageClientChange}. That method is
300
+ * `protected`, so an override has to name every arm it can return.
187
301
  */
188
- type LanguageClientChangeOrigin =
302
+ export type LanguageClientChangeOrigin =
189
303
  /** The client is reporting a text we pushed it. The synced document is already there. */
190
304
  | { readonly kind: 'echo' }
191
305
  /**
192
- * The client's buffer has moved somewhere we did not push it — it coalesced
193
- * a genuine keystroke into the echo, or typed before the push arrived. The
194
- * reconstructed text is what it now holds, and is authoritative.
306
+ * The client's buffer holds a text we did not push it — a keystroke that
307
+ * raced a push, or an edit to a buffer the store has already been written
308
+ * past. The reconstructed text is what it now holds, and is authoritative.
195
309
  */
196
- | { readonly kind: 'divergent'; readonly text: string };
310
+ | { readonly kind: 'divergent'; readonly text: string }
311
+ /**
312
+ * The change carries ranges and no known text addresses them, so no
313
+ * reconstruction is offered. Adopting one anyway splices the document and
314
+ * stores an edit nobody made; dropping costs at most the one keystroke the
315
+ * client still holds and the next push contradicts.
316
+ */
317
+ | { readonly kind: 'unreconstructable' };
197
318
 
198
319
  /**
199
- * Cheap, stable, non-cryptographic content hash (cyrb53) for the version
200
- * sequence's "did the content change across close/reopen?" question. Only
201
- * needs to be collision-resistant enough that an accidental match across two
202
- * DIFFERENT revisions of the same file is practically impossible; 53 bits of
203
- * a well-mixed hash over full text + length gives that without pulling in
204
- * `node:crypto` (this module must stay runnable in browser hosts).
320
+ * Whether `err` reports that the file of `target` does not exist, as a Node
321
+ * file system and the framework's providers do: code `ENOENT`, with `target`'s
322
+ * path. A missing file of another document is a different failure.
205
323
  */
206
- function contentHash(text: string): string {
207
- let h1 = 0xdeadbeef;
208
- let h2 = 0x41c6ce57;
209
- for (let index = 0; index < text.length; index++) {
210
- const code = text.charCodeAt(index);
211
- h1 = Math.imul(h1 ^ code, 2654435761);
212
- h2 = Math.imul(h2 ^ code, 1597334677);
213
- }
214
- h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507);
215
- h1 ^= Math.imul(h2 ^ (h2 >>> 13), 3266489909);
216
- h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507);
217
- h2 ^= Math.imul(h1 ^ (h1 >>> 13), 3266489909);
218
- return `${(4294967296 * (2097151 & h2) + (h1 >>> 0)).toString(36)}:${text.length}`;
324
+ function isFileNotFound(err: unknown, target: URI): boolean {
325
+ if (typeof err !== 'object' || err === null || !('code' in err) || err.code !== 'ENOENT') {
326
+ return false;
327
+ }
328
+ return !('path' in err) || err.path === target.fsPath;
219
329
  }
220
330
 
221
331
  /**
222
332
  * Multi-client text-document tracking on top of Langium's `NormalizedTextDocuments`.
223
333
  *
224
334
  * Adds the framework features used by the integrity, model-server, and GLSP layers:
225
- * - Per-document client membership (multiple clients can attach to the same URI).
335
+ * - Per-document client membership (multiple clients can attach to the same
336
+ * URI) and the client-session table, both kept by {@link __sessions}.
226
337
  * - A SERVER-OWNED shared version sequence: per-URI, monotonic across
227
338
  * close/reopen cycles, advancing exactly when the synced content changes.
228
339
  * Client-declared version ids (Monaco's buffer numbering) feed only a
229
340
  * per-client staleness guard and never leak into the shared sequence —
230
341
  * the two are different things (an editor's edit-operation counter vs the
231
342
  * document's content-revision number), and splicing them lets versions drift
232
- * silently past `baseVersion` gate holders.
343
+ * silently past based-on gate holders.
233
344
  * - Version-author history so each edit is attributable to its originating client.
345
+ * - The revert to disk once no client has a document open, for every head
346
+ * ({@link revertToDisk}), deferred by
347
+ * {@link HydraniumTextDocumentsOptions.revertGraceMs} after a lost connection.
348
+ * - A disk baseline per open document, and whether its text differs from it
349
+ * ({@link isDirty}).
234
350
  * - Pending-content staging used by the integrity service to thread corrections
235
351
  * through `workspace/applyEdit` cycles for currently-closed documents.
236
352
  * - `didOpen` notifications arriving over the LSP connection wait on the
@@ -240,23 +356,28 @@ function contentHash(text: string): string {
240
356
  */
241
357
  export class HydraniumTextDocuments<T extends TextDocument = TextDocument> extends NormalizedTextDocuments<T> {
242
358
  /**
243
- * Per-URI client-facing tracking ({@link DocumentTracking}): client
244
- * membership, version-author history, and integrity-staged pending content,
245
- * keyed by normalized URI. One record per URI so the last-client close clears
246
- * every axis atomically. The language-client diff baseline stays in
247
- * {@link __shadow} (its own abstraction); the parsed-document store stays in
248
- * the inherited `__syncedDocuments`.
359
+ * Per-URI client-facing tracking ({@link DocumentTrackingRecord}), keyed by
360
+ * canonical URI. One record per URI, so dropping it clears every axis at
361
+ * once. Which client has the document open is kept apart, in
362
+ * {@link __sessions}.
363
+ */
364
+ protected __documents = new Map<CanonicalUri, DocumentTrackingRecord>();
365
+
366
+ /**
367
+ * Which client has which document open, and which client ids are registered
368
+ * sessions. Every open-state predicate on this class reads it, so an open
369
+ * recorded anywhere else is invisible to the last-close transition.
249
370
  */
250
- protected __documents = new Map<CanonicalUri, DocumentTracking>();
371
+ protected readonly __sessions = new ClientSessionRegistry();
251
372
 
252
373
  /**
253
374
  * Per-URI shared-version continuity across close/reopen cycles
254
375
  * ({@link VersionSequence}), written at last-client close and consulted by
255
- * the next first-client open. DELIBERATELY outside {@link DocumentTracking}:
376
+ * the next first-client open. DELIBERATELY outside {@link DocumentTrackingRecord}:
256
377
  * that record is deleted on last close, while the version sequence must
257
378
  * survive it — the shared version is a server-owned, monotonic,
258
379
  * advances-iff-content-changes counter that never resets while the server
259
- * lives. That invariant is what makes an optimistic `baseVersion` gate
380
+ * lives. That invariant is what makes an optimistic based-on gate
260
381
  * sound: "version unchanged ⇔ content unchanged", with no false conflicts
261
382
  * from close/reopen version resets and no false passes from a reopened
262
383
  * sequence coincidentally landing on a stale writer's number.
@@ -269,7 +390,7 @@ export class HydraniumTextDocuments<T extends TextDocument = TextDocument> exten
269
390
  /**
270
391
  * Texts pushed to the LSP textual language client via
271
392
  * {@link applyEditToLanguageClient} whose echoes have not come back yet
272
- * ({@link PendingPush}), keyed like the shadow by language-client URI.
393
+ * ({@link PendingLanguageClientPush}), keyed like the shadow by language-client URI.
273
394
  * Outbound pushes and inbound echoes are uncorrelated on the wire; this
274
395
  * FIFO is the explicit correlation, and it is what
275
396
  * {@link classifyLanguageClientChange} reconstructs against.
@@ -291,7 +412,7 @@ export class HydraniumTextDocuments<T extends TextDocument = TextDocument> exten
291
412
  * a pathological echo that never arrives; the memory cost until then is
292
413
  * one pre-push text per in-flight push, for milliseconds.
293
414
  */
294
- protected readonly __pendingPushes = new Map<LanguageClientUri, PendingPush[]>();
415
+ protected readonly __pendingPushes = new Map<LanguageClientUri, PendingLanguageClientPush[]>();
295
416
 
296
417
  /**
297
418
  * Tracked text content per URI for the LSP textual language client (Monaco / VS Code).
@@ -316,6 +437,11 @@ export class HydraniumTextDocuments<T extends TextDocument = TextDocument> exten
316
437
 
317
438
  protected readonly tracer: Tracer;
318
439
  protected readonly configuration: TextDocumentsConfiguration<T>;
440
+ /** See {@link HydraniumTextDocumentsOptions.revertGraceMs}. */
441
+ protected readonly revertGraceMs: number;
442
+ protected readonly lastOpenClosedEmitter = new Emitter<LastOpenClosedEvent>();
443
+ protected readonly languageClientSavedEmitter = new Emitter<LanguageClientSavedEvent>();
444
+ protected readonly dirtyChangedEmitter = new Emitter<DocumentDirtyChangedEvent>();
319
445
 
320
446
  constructor(
321
447
  protected services: ServerSharedServices,
@@ -329,6 +455,8 @@ export class HydraniumTextDocuments<T extends TextDocument = TextDocument> exten
329
455
  (uri, reason) => this.tracer.with(uri).warn(`Diff apply-verify fallback (${reason}) — using full-document replace`),
330
456
  this
331
457
  );
458
+ this.revertGraceMs = options.revertGraceMs ?? DEFAULT_REVERT_GRACE_MS;
459
+ this.onDidCloseLastOpen(event => void this.revertToDisk(event.uri));
332
460
  }
333
461
 
334
462
  // Re-exposed configuration factories — for framework-internal callers
@@ -403,7 +531,7 @@ export class HydraniumTextDocuments<T extends TextDocument = TextDocument> exten
403
531
  const disposables = new DisposableCollection();
404
532
  disposables.push(
405
533
  connection.onDidOpenTextDocument(async (event: DidOpenTextDocumentParams) => {
406
- await this.services.workspace.WorkspaceManager.workspaceInitialized;
534
+ await this.initialBuildFinished();
407
535
  this.notifyDidOpenTextDocument(event);
408
536
  })
409
537
  );
@@ -429,7 +557,7 @@ export class HydraniumTextDocuments<T extends TextDocument = TextDocument> exten
429
557
  );
430
558
  disposables.push(
431
559
  connection.onDidSaveTextDocument((event: DidSaveTextDocumentParams) => {
432
- this.notifyDidSaveTextDocument(event);
560
+ void this.notifyLanguageClientSave(event);
433
561
  })
434
562
  );
435
563
  return disposables;
@@ -458,28 +586,35 @@ export class HydraniumTextDocuments<T extends TextDocument = TextDocument> exten
458
586
  // client knowing). Gating on the shared version drops real edits in
459
587
  // exactly that lag window. A client with no baseline (never opened —
460
588
  // a protocol anomaly) falls back to the shared-version compare, the
461
- // conservative answer.
589
+ // conservative answer. The language client is checked per URI: each is
590
+ // its own buffer, and one URI's higher id would drop the other's edits.
462
591
  const record = this.trackingFor(uri);
463
- const lastSeen = record.clientVersions.get(clientId) ?? document.version;
592
+ const languageClientDocument =
593
+ clientId === LANGUAGE_CLIENT_ID ? record.languageClientDocuments?.get(this.toLanguageClientUri(td.uri)) : undefined;
594
+ const lastSeen = languageClientDocument?.declaredVersion ?? record.clientVersions.get(clientId) ?? document.version;
464
595
  if (lastSeen >= td.version) {
465
596
  // Distinguish "already at this version" (common: an echo from the client that triggered
466
597
  // the update) from "incoming version older than ours" (stale race).
467
598
  const reason =
468
599
  lastSeen === td.version ? `already at version ${lastSeen}` : `incoming version ${td.version} older than current ${lastSeen}`;
469
- this.logUri(uri, `Ignore update by ${clientId}: ${reason}`, 'debug');
600
+ this.logUri(uri, `Ignore update by ${this.formatClientId(clientId)}: ${reason}`, 'debug');
470
601
  return;
471
602
  }
472
603
  record.clientVersions.set(clientId, td.version);
604
+ if (languageClientDocument) {
605
+ languageClientDocument.declaredVersion = td.version;
606
+ }
473
607
 
474
- // A language-client change arriving while one of our own pushes is in
475
- // flight is keyed to the buffer the client held BEFORE that push, NOT
476
- // to the synced text — the authored write already advanced the latter.
608
+ // A language-client change is keyed to the buffer that client holds,
609
+ // which is the synced text only while the two agree — an authored
610
+ // write advances the synced text without the client knowing, and a
611
+ // push in flight moves the client without the store knowing.
477
612
  // Resolve which it is, and against what, before touching anything.
478
- const inFlight =
613
+ const origin =
479
614
  clientId === LANGUAGE_CLIENT_ID
480
615
  ? this.classifyLanguageClientChange(this.toLanguageClientUri(td.uri), document, changes)
481
616
  : undefined;
482
- if (inFlight?.kind === 'echo') {
617
+ if (origin?.kind === 'echo') {
483
618
  // The client is reporting a text we pushed it, so the synced
484
619
  // document is ALREADY there and the changes must not be applied a
485
620
  // second time. Nothing is minted and no rebuild fires. The
@@ -491,19 +626,23 @@ export class HydraniumTextDocuments<T extends TextDocument = TextDocument> exten
491
626
  this.logUri(uri, `Skip rebuild: echo of a server-authored push (client version ${td.version})`, 'debug');
492
627
  return;
493
628
  }
629
+ if (origin?.kind === 'unreconstructable') {
630
+ this.tracer.with(uri).warn(`Drop change: no known client buffer for its ranges (client version ${td.version})`);
631
+ return;
632
+ }
494
633
 
495
634
  // The SHARED version advances iff the content actually changes — the
496
- // invariant optimistic `baseVersion` gates rely on. The new text is
635
+ // invariant optimistic based-on gates rely on. The new text is
497
636
  // only known after applying the (possibly incremental) changes, so
498
637
  // apply at a tentative +1 and roll the version back on an identical
499
638
  // result (an empty-changes update only re-stamps the version).
500
639
  //
501
- // A divergent in-flight change is applied as its RECONSTRUCTED text
502
- // rather than as its own ranges: the ranges address the pre-push
503
- // baseline, so applying them here would splice the wrong lines.
640
+ // A divergent change is applied as its RECONSTRUCTED text rather than
641
+ // as its own ranges: those ranges address the client's own buffer, so
642
+ // applying them here would splice the wrong lines.
504
643
  const previousText = document.getText();
505
644
  const sharedVersion = document.version;
506
- document = this.configuration.update(document, inFlight === undefined ? changes : [{ text: inFlight.text }], sharedVersion + 1);
645
+ document = this.configuration.update(document, origin === undefined ? changes : [{ text: origin.text }], sharedVersion + 1);
507
646
  const changed = document.getText() !== previousText;
508
647
  if (!changed) {
509
648
  document = this.configuration.update(document, [], sharedVersion);
@@ -511,6 +650,7 @@ export class HydraniumTextDocuments<T extends TextDocument = TextDocument> exten
511
650
  this.__syncedDocuments.set(uri, document);
512
651
  if (changed) {
513
652
  this.setAuthor(uri, document.version, clientId);
653
+ this.refreshDirty(uri);
514
654
  }
515
655
  if (clientId === LANGUAGE_CLIENT_ID) {
516
656
  // Monaco just told us about its new content; record it so the next outbound
@@ -527,7 +667,10 @@ export class HydraniumTextDocuments<T extends TextDocument = TextDocument> exten
527
667
  return;
528
668
  }
529
669
  }
530
- this.log(document.uri, `Update to version ${document.version} by ${clientId}${changed ? '' : ' (content unchanged)'}`);
670
+ this.log(
671
+ document.uri,
672
+ `Update to version ${document.version} by ${this.formatClientId(clientId)}${changed ? '' : ' (content unchanged)'}`
673
+ );
531
674
  this.__onDidChangeContent.fire(Object.freeze({ document, clientId }));
532
675
  }
533
676
  }
@@ -544,7 +687,7 @@ export class HydraniumTextDocuments<T extends TextDocument = TextDocument> exten
544
687
  * A content-identical write still fires the change event (rebuild): the
545
688
  * authored write's server-side rebuild is correctness-bearing, it just
546
689
  * mints no new version — nothing observable changed, so watchers'
547
- * `baseVersion` pointers stay valid.
690
+ * based-on versions stay valid.
548
691
  *
549
692
  * Returns the resulting shared version. Throws when the document is not
550
693
  * open — callers (`AstDocumentManager.update`) open first.
@@ -560,57 +703,210 @@ export class HydraniumTextDocuments<T extends TextDocument = TextDocument> exten
560
703
  document = this.configuration.update(document, [{ text }], document.version + 1);
561
704
  this.__syncedDocuments.set(key, document);
562
705
  this.setAuthor(key, document.version, clientId);
706
+ this.refreshDirty(key);
563
707
  }
564
- this.log(document.uri, `Update to version ${document.version} by ${clientId}${changed ? '' : ' (content unchanged)'}`);
708
+ this.log(
709
+ document.uri,
710
+ `Update to version ${document.version} by ${this.formatClientId(clientId)}${changed ? '' : ' (content unchanged)'}`
711
+ );
565
712
  this.__onDidChangeContent.fire(Object.freeze({ document, clientId }));
566
713
  return document.version;
567
714
  }
568
715
 
569
- public notifyDidCloseTextDocument(event: DidCloseTextDocumentParams, clientId = LANGUAGE_CLIENT_ID): void {
716
+ /**
717
+ * Close `clientId`'s open of the document. When it was the last open, the
718
+ * document is released and reverts to disk, at once or, for a `'lost'`
719
+ * close, after {@link HydraniumTextDocumentsOptions.revertGraceMs}.
720
+ */
721
+ public notifyDidCloseTextDocument(
722
+ event: DidCloseTextDocumentParams,
723
+ clientId = LANGUAGE_CLIENT_ID,
724
+ cause: SessionEndCause = 'closed'
725
+ ): void {
570
726
  const uri = this.documentKey(event.textDocument.uri);
571
- if (!this.isOpenInClient(uri, clientId)) {
727
+ if (clientId === LANGUAGE_CLIENT_ID) {
728
+ const clientFacing = this.toLanguageClientUri(event.textDocument.uri);
729
+ const languageClientDocuments = this.__documents.get(uri)?.languageClientDocuments;
730
+ if (languageClientDocuments?.delete(clientFacing) && languageClientDocuments.size > 0) {
731
+ // Another URI still holds the document, so only this one's buffer goes.
732
+ this.__shadow.invalidate(clientFacing);
733
+ this.__pendingPushes.delete(clientFacing);
734
+ return;
735
+ }
736
+ }
737
+ if (!this.__sessions.removeOpen(uri, clientId)) {
572
738
  return;
573
739
  }
574
- const closingRecord = this.__documents.get(uri);
575
- closingRecord?.clients.delete(clientId);
576
- closingRecord?.clientVersions.delete(clientId);
740
+ const record = this.__documents.get(uri);
741
+ record?.clientVersions.delete(clientId);
742
+ if (record && cause === 'lost') {
743
+ (record.lostClients ??= new Map()).set(clientId, this.services.Clock.stopwatch());
744
+ }
577
745
  const syncedDocument = this.__syncedDocuments.get(uri);
578
746
  if (syncedDocument !== undefined) {
579
- this.log(syncedDocument.uri, `Closed synced document: ${syncedDocument.version} by ${clientId}`);
747
+ this.log(syncedDocument.uri, `Closed synced document: ${syncedDocument.version} by ${this.formatClientId(clientId)}`);
580
748
  this.__onDidClose.fire(Object.freeze({ document: syncedDocument, clientId }));
581
749
 
582
750
  if (clientId === LANGUAGE_CLIENT_ID) {
583
- // Monaco closed the document; drop the URI it held and the shadow
584
- // baselined under it. (If this was the last URI/client the whole record
585
- // is deleted below, clearing the set anyway.)
751
+ // Monaco closed the document; drop the shadow baselined under the URI
752
+ // it held. (If this was the last client the whole record is deleted on
753
+ // release.)
586
754
  const droppedUri = this.toLanguageClientUri(event.textDocument.uri);
587
- this.__documents.get(uri)?.languageClientUris?.delete(droppedUri);
588
755
  this.__shadow.invalidate(droppedUri);
589
756
  this.__pendingPushes.delete(droppedUri);
590
757
  }
591
- if (!this.__documents.get(uri)?.clients.size) {
592
- // Last client closed the document; delete sync state. The downstream
593
- // "rebuild from disk for file URIs / drop from index for ephemeral
594
- // URIs" decision lives in `HydraniumDocumentUpdateHandler.didCloseDocument`,
595
- // which subscribes to `onDidClose` and consults `isOpenInAnyClient`
596
- // to detect the last-close transition.
597
- this.log(syncedDocument.uri, `Remove synced document: ${syncedDocument.version} (no client left)`);
598
- // Persist where the shared version sequence left off (version +
599
- // content hash) so the next open CONTINUES the sequence instead of
600
- // restarting at the reopening client's declared id. Hashed once
601
- // here at close, not on every change.
602
- this.__versionSequences.set(uri, {
603
- version: syncedDocument.version,
604
- contentHash: contentHash(syncedDocument.getText())
605
- });
606
- this.__syncedDocuments.delete(uri);
607
- // One delete clears every per-URI axis (version history + any staged
608
- // pending content) so a future open with the same URI starts fresh.
609
- this.__documents.delete(uri);
758
+ if (!this.__sessions.isOpen(uri)) {
759
+ if (cause === 'lost' && this.revertGraceMs > 0) {
760
+ this.deferRelease(uri);
761
+ } else {
762
+ this.releaseDocument(uri);
763
+ }
764
+ }
765
+ }
766
+ }
767
+
768
+ /**
769
+ * Keep the document, text and all, for the revert grace, then release it.
770
+ * The document stays in the store meanwhile, so a lost client that opens it
771
+ * again within its own grace attaches to it and finds its unsaved text
772
+ * rather than reading disk; see {@link resolvePendingRevert} for any other
773
+ * open.
774
+ */
775
+ protected deferRelease(uri: CanonicalUri): void {
776
+ this.log(uri, `No client left; revert deferred for ${this.revertGraceMs} ms (connection lost)`);
777
+ const timer = this.services.Clock.setTimer(() => {
778
+ this.__sessions.cancelRevert(uri);
779
+ if (!this.__sessions.isOpen(uri)) {
780
+ this.releaseDocument(uri);
781
+ }
782
+ }, this.revertGraceMs);
783
+ this.__sessions.deferRevert(uri, timer);
784
+ }
785
+
786
+ /**
787
+ * Resolve the pending revert of `uri` for an open by `clientId`. An open by a
788
+ * client lost from the document within its grace cancels the revert, and
789
+ * the client finds its unsaved text. Any other open releases the document
790
+ * first, so it opens as a first open does: cancelling for every open hands
791
+ * a lost client's unsaved text to whoever opens next, a reloaded page or an
792
+ * editor, with nothing marking it unsaved.
793
+ */
794
+ protected resolvePendingRevert(uri: CanonicalUri, clientId: string): void {
795
+ const record = this.__documents.get(uri);
796
+ if (record) {
797
+ this.pruneLostClients(record);
798
+ }
799
+ const returning = record?.lostClients?.delete(clientId) ?? false;
800
+ if (!this.__sessions.isRevertPending(uri)) {
801
+ return;
802
+ }
803
+ this.__sessions.cancelRevert(uri);
804
+ if (!returning) {
805
+ this.releaseDocument(uri);
806
+ }
807
+ }
808
+
809
+ /** Drop the entries of {@link DocumentTrackingRecord.lostClients} whose grace has run out. */
810
+ protected pruneLostClients(record: DocumentTrackingRecord): void {
811
+ for (const [clientId, sinceLoss] of record.lostClients ?? []) {
812
+ if (sinceLoss.elapsedMs >= this.revertGraceMs) {
813
+ record.lostClients?.delete(clientId);
610
814
  }
611
815
  }
612
816
  }
613
817
 
818
+ /**
819
+ * Drop the document no client has open any more, then announce it on
820
+ * {@link onDidCloseLastOpen}, which is what reverts it to disk.
821
+ */
822
+ protected releaseDocument(uri: CanonicalUri): void {
823
+ const syncedDocument = this.__syncedDocuments.get(uri);
824
+ if (syncedDocument === undefined) {
825
+ return;
826
+ }
827
+ this.log(syncedDocument.uri, `Remove synced document: ${syncedDocument.version} (no client left)`);
828
+ // Persist where the shared version sequence left off (version +
829
+ // content hash) so the next open CONTINUES the sequence instead of
830
+ // restarting at the reopening client's declared id. Hashed once
831
+ // here at release, not on every change.
832
+ this.__versionSequences.set(uri, {
833
+ version: syncedDocument.version,
834
+ contentHash: textHash(syncedDocument.getText())
835
+ });
836
+ const wasDirty = this.__documents.get(uri)?.dirty === true;
837
+ this.__syncedDocuments.delete(uri);
838
+ // One delete clears every per-URI axis (version history + any staged
839
+ // pending content) so a future open with the same URI starts fresh.
840
+ this.__documents.delete(uri);
841
+ if (wasDirty) {
842
+ this.dirtyChangedEmitter.fire(Object.freeze({ uri, dirty: false }));
843
+ }
844
+ this.lastOpenClosedEmitter.fire(Object.freeze({ uri }));
845
+ }
846
+
847
+ /**
848
+ * Rebuild a released document from the file system provider, so the build
849
+ * stops carrying the unsaved text of its last client.
850
+ *
851
+ * The provider decides, for every scheme, by `exists`: a document it can
852
+ * serve is rebuilt from its text, and any other — an editor's `untitled:`
853
+ * buffer, a file never saved, or one deleted meanwhile — is removed
854
+ * from the workspace. A `virtual:` document survives, since the framework's
855
+ * provider for that scheme serves it from the index, whatever provider the
856
+ * host passes as `context.fileSystemProvider`; an edited one therefore keeps
857
+ * its last client's text, and keeping it read-only is the client's job.
858
+ *
859
+ * The answer is read in the document's disk queue, so the rebuild follows
860
+ * any save still queued rather than reverting past it; a file that goes
861
+ * after that read is removed when its rebuild finds none.
862
+ *
863
+ * Whether to revert at all is decided inside the write lock, as its holder:
864
+ * decided before waiting for the lock, a client that opens or re-creates the
865
+ * document meanwhile would have its text rebuilt over, or the document
866
+ * removed. A document some client has open again, or that waits out a new
867
+ * grace, is left to that client.
868
+ */
869
+ protected async revertToDisk(uri: CanonicalUri): Promise<void> {
870
+ const workspace = this.services.workspace;
871
+ const target = UriUtils.toUri(uri);
872
+ const reopened = (): boolean => this.isOpenInAnyClient(uri) || this.__syncedDocuments.has(uri);
873
+ try {
874
+ // Read before the lock: every build and read waits while the lock is
875
+ // held, and this read waits on the file's save I/O.
876
+ const onDisk = await workspace.FileSystemTaskQueue.enqueue(uri, () => workspace.FileSystemProvider.exists(target));
877
+ await workspace.WorkspaceManager?.ready;
878
+ await workspace.WorkspaceLock.write(async token => {
879
+ if (reopened()) {
880
+ return;
881
+ }
882
+ workspace.DocumentBuilder.markNextReason(HYDRANIUM_BUILD_REASONS.didClose);
883
+ try {
884
+ await (onDisk
885
+ ? workspace.DocumentBuilder.update([target], [], token)
886
+ : workspace.DocumentBuilder.update([], [target], token));
887
+ } catch (err: unknown) {
888
+ if (!onDisk || !isFileNotFound(err, target)) {
889
+ throw err;
890
+ }
891
+ // The file went after its existence was read.
892
+ this.tracer.with(uri).debug('Revert found no file; removing the document instead');
893
+ workspace.DocumentBuilder.markNextReason(HYDRANIUM_BUILD_REASONS.didClose);
894
+ await workspace.DocumentBuilder.update([], [target], token);
895
+ }
896
+ });
897
+ } catch (err: unknown) {
898
+ // A revert that finishes after the LSP peer went away fails its
899
+ // diagnostics publish, and one that runs after its workspace was torn
900
+ // down finds no file: teardown races, not failed reverts.
901
+ if (isConnectionGoneError(err) || isFileNotFound(err, target)) {
902
+ this.tracer.with(uri).debug(`Revert on last close skipped: ${err instanceof Error ? err.message : String(err)}`);
903
+ return;
904
+ }
905
+ const detail = err instanceof Error ? (err.stack ?? err.message) : String(err);
906
+ this.tracer.with(uri).error(`Revert on last close dropped. ${detail}`);
907
+ }
908
+ }
909
+
614
910
  public notifyWillSaveTextDocument(event: WillSaveTextDocumentParams): void {
615
911
  const syncedDocument = this.__syncedDocuments.get(this.documentKey(event.textDocument.uri));
616
912
  if (syncedDocument !== undefined) {
@@ -630,47 +926,111 @@ export class HydraniumTextDocuments<T extends TextDocument = TextDocument> exten
630
926
  }
631
927
  }
632
928
 
929
+ /**
930
+ * Handle a save the language client reports: fire
931
+ * {@link onDidSaveInLanguageClient}, then announce it on {@link onDidSave}
932
+ * only once the file read back from disk holds the store's current text.
933
+ */
934
+ protected async notifyLanguageClientSave(event: DidSaveTextDocumentParams): Promise<void> {
935
+ const syncedDocument = this.__syncedDocuments.get(this.documentKey(event.textDocument.uri));
936
+ if (syncedDocument === undefined) {
937
+ return;
938
+ }
939
+ const uri = syncedDocument.uri;
940
+ this.languageClientSavedEmitter.fire(Object.freeze({ uri }));
941
+ // `onDidSave` tells its listeners that the shared document is on disk.
942
+ // The editor wrote its own buffer, which lags the store while another
943
+ // client's edit is still on its way to it, so only the file says
944
+ // whether that holds. Read it rather than trust the editor: `didSave`
945
+ // carries text only for a client that honours `includeText`, and asking
946
+ // for it means rewriting Langium's initialize answer, which advertises
947
+ // `save` as a Boolean. The read goes through the disk queue and waits on
948
+ // nothing but the provider, so it follows a server save queued
949
+ // meanwhile. A file changed again between the editor's write and this
950
+ // read compares unequal, and the save goes unannounced.
951
+ let onDisk: string | undefined;
952
+ try {
953
+ onDisk = await this.services.workspace.FileSystemTaskQueue.enqueue(uri, () =>
954
+ this.services.workspace.FileSystemProvider.readFile(UriUtils.toUri(uri))
955
+ );
956
+ } catch (err: unknown) {
957
+ this.tracer
958
+ .with(uri)
959
+ .debug(`Editor save not announced: reading the file back failed. ${err instanceof Error ? err.message : String(err)}`);
960
+ return;
961
+ }
962
+ // What the file holds, not what the editor meant to write: an editor
963
+ // that saved older text leaves the document dirty.
964
+ this.updateDiskBaseline(uri, onDisk);
965
+ // An editor that saves and then closes releases the document while the
966
+ // read is under way; its save is still a save of the text it held.
967
+ const document = this.__syncedDocuments.get(this.documentKey(uri)) ?? syncedDocument;
968
+ if (onDisk !== document.getText()) {
969
+ this.tracer.with(uri).debug(`Editor save not announced: the file does not hold the text of version ${document.version}`);
970
+ return;
971
+ }
972
+ this.announceSave(document, LANGUAGE_CLIENT_ID);
973
+ }
974
+
975
+ /**
976
+ * Announce a save the server made. `event.text`, when given, is what the
977
+ * file now holds, written or found there, and becomes the disk baseline
978
+ * before the save is announced. It lags the store when an edit landed
979
+ * after the save took its text, and the document then stays dirty.
980
+ */
633
981
  public notifyDidSaveTextDocument(event: DidSaveTextDocumentParams, clientId = LANGUAGE_CLIENT_ID): void {
634
982
  const syncedDocument = this.__syncedDocuments.get(this.documentKey(event.textDocument.uri));
635
983
  if (syncedDocument !== undefined) {
636
- this.log(syncedDocument.uri, `Saved synced document: ${syncedDocument.version} by ${clientId}`);
637
- this.__onDidSave.fire(Object.freeze({ document: syncedDocument, clientId }));
984
+ if (event.text !== undefined) {
985
+ this.updateDiskBaseline(syncedDocument.uri, event.text);
986
+ }
987
+ this.announceSave(syncedDocument, clientId);
638
988
  }
639
989
  }
640
990
 
991
+ /** Fire {@link onDidSave} for `document`, which need not be synced any more. */
992
+ protected announceSave(document: T, clientId: string): void {
993
+ this.log(document.uri, `Saved synced document: ${document.version} by ${this.formatClientId(clientId)}`);
994
+ this.__onDidSave.fire(Object.freeze({ document, clientId }));
995
+ }
996
+
641
997
  public notifyDidOpenTextDocument(event: DidOpenTextDocumentParams, clientId = LANGUAGE_CLIENT_ID): void {
642
998
  const td = event.textDocument;
643
999
  const uri = this.documentKey(td.uri);
1000
+ this.resolvePendingRevert(uri, clientId);
644
1001
  if (this.isOpenInClient(uri, clientId)) {
645
1002
  // Already open for this client under this canonical identity. If this is a
646
1003
  // NEW client-facing URI for the same file (a second tab reached via a
647
- // divergent path, e.g. a symlink and its real path), record it and baseline
648
- // its shadow so outbound edits reach this tab too — but do NOT re-fire
649
- // open/rebuild; the document is already live.
1004
+ // divergent path, e.g. a symlink and its real path), record it so outbound
1005
+ // edits reach this tab too — but do NOT re-fire open/rebuild; the document
1006
+ // is already live.
650
1007
  if (clientId === LANGUAGE_CLIENT_ID) {
651
1008
  const clientFacing = this.toLanguageClientUri(td.uri);
652
1009
  const record = this.__documents.get(uri);
653
- if (record && !record.languageClientUris?.has(clientFacing)) {
654
- (record.languageClientUris ??= new Set<LanguageClientUri>()).add(clientFacing);
655
- const open = this.__syncedDocuments.get(uri);
656
- if (open) {
657
- this.__shadow.set(clientFacing, open.getText());
658
- }
1010
+ if (record && !record.languageClientDocuments?.has(clientFacing)) {
1011
+ (record.languageClientDocuments ??= new Map()).set(clientFacing, { declaredVersion: td.version });
1012
+ // This tab's own buffer, NOT the synced text: a second tab is a second
1013
+ // client model, read from disk, so a server-authored write already
1014
+ // applied to the first tab leaves it BEHIND the synced document. Keying
1015
+ // a diff to the synced text here addresses lines this tab does not have
1016
+ // and drops content it does.
1017
+ this.__shadow.setOpenedText(clientFacing, td.text);
659
1018
  }
660
1019
  }
661
1020
  return;
662
1021
  }
663
1022
  let document = this.__syncedDocuments.get(uri);
1023
+ const existingClients = this.__sessions.clientsOf(uri);
1024
+ this.__sessions.addOpen(uri, clientId);
664
1025
  const record = this.trackingFor(uri);
665
- const existingClients = [...record.clients];
666
- record.clients.add(clientId);
667
1026
  // Baseline the per-client staleness guard at the version id the client
668
1027
  // declared for its own buffer (client-owned per LSP).
669
1028
  record.clientVersions.set(clientId, td.version);
670
1029
  if (clientId === LANGUAGE_CLIENT_ID) {
671
1030
  // Remember the URI Monaco opened under (may differ from the canonical key)
672
1031
  // so outbound applyEditToLanguageClient can address the URI it actually holds.
673
- (record.languageClientUris ??= new Set<LanguageClientUri>()).add(this.toLanguageClientUri(td.uri));
1032
+ // A fresh state, since a reopened buffer numbers its versions afresh.
1033
+ (record.languageClientDocuments ??= new Map()).set(this.toLanguageClientUri(td.uri), { declaredVersion: td.version });
674
1034
  }
675
1035
  if (!document) {
676
1036
  // Use integrity-staged content if available, otherwise the client-provided (disk) text.
@@ -679,44 +1039,110 @@ export class HydraniumTextDocuments<T extends TextDocument = TextDocument> exten
679
1039
  const source = pendingText ? ', source=pending' : '';
680
1040
  // The SHARED version is server-assigned: continue the persisted
681
1041
  // sequence — same version when the content is unchanged since the
682
- // last close (so watchers' `baseVersion` pointers stay valid), one
1042
+ // last close (so watchers' based-on versions stay valid), one
683
1043
  // step when it changed (so no stale pointer can coincidentally pass
684
1044
  // the optimistic gate). Only a first-ever open adopts the client's
685
1045
  // declared id as the sequence seed.
686
1046
  const sequence = this.__versionSequences.get(uri);
687
1047
  const version =
688
- sequence === undefined ? td.version : sequence.contentHash === contentHash(text) ? sequence.version : sequence.version + 1;
689
- this.log(uri, `Open document: Version ${version} by ${clientId} [first client${source}]`);
1048
+ sequence === undefined ? td.version : sequence.contentHash === textHash(text) ? sequence.version : sequence.version + 1;
1049
+ this.log(uri, `Open document: Version ${version} by ${this.formatClientId(clientId)} [first client${source}]`);
690
1050
  document = this.configuration.create(uri, td.languageId, version, text);
691
1051
  this.__syncedDocuments.set(uri, document);
692
1052
  this.setAuthor(uri, version, clientId);
1053
+ // The opener's text, not the staged content: a session's open read
1054
+ // it from the file, and an editor opened its buffer from there. An
1055
+ // editor that opens a buffer it never saved is taken as clean.
1056
+ record.diskBaseline = td.text;
1057
+ this.refreshDirty(uri);
693
1058
  if (clientId === LANGUAGE_CLIENT_ID) {
694
1059
  // Baseline the shadow to what Monaco just opened so the next outbound
695
1060
  // applyEditToLanguageClient diffs against the right starting point. Keyed by the
696
1061
  // language-client URI (what Monaco holds), not the canonical document key.
697
- this.__shadow.set(this.toLanguageClientUri(td.uri), document.getText());
1062
+ //
1063
+ // The CLIENT's declared text, never the synced document: staged content
1064
+ // consumed above leaves the two different, and a baseline asserting the
1065
+ // client already holds the staged text suppresses the one sync that would
1066
+ // deliver it. Skipped entirely when a push is already outstanding for this
1067
+ // URI — an `applyEdit` to a closed file has the client open from disk and
1068
+ // apply afterwards, so that shadow records what it is about to hold and
1069
+ // disk text would key the next diff to a buffer nobody has.
1070
+ const clientFacing = this.toLanguageClientUri(td.uri);
1071
+ if (!this.__shadow.isTracked(clientFacing)) {
1072
+ this.__shadow.set(clientFacing, td.text);
1073
+ }
698
1074
  }
699
1075
  const toFire = Object.freeze({ document, clientId });
700
1076
  this.__onDidOpen.fire(toFire);
701
1077
  this.__onDidChangeContent.fire(toFire);
702
1078
  } else {
703
1079
  // An additional client attaches to a document already open by another client.
704
- this.log(
705
- uri,
706
- `Attach client: ${clientId} joined existing document (version ${document.version}, ` +
707
- `now open in: ${[...existingClients, clientId].join(', ')})`
708
- );
1080
+ this.logClientJoined(uri, clientId, document.version, existingClients);
1081
+ if (clientId === LANGUAGE_CLIENT_ID) {
1082
+ // Monaco's own buffer, which on this path is NOT the synced text — another
1083
+ // client opened the document and may already have changed it. Recorded as
1084
+ // an equality-only baseline (never a diff one, see `setOpenedText`) so the
1085
+ // refresh below does not push a full replace of content Monaco already
1086
+ // holds, which would dirty the file on open.
1087
+ this.__shadow.setOpenedText(this.toLanguageClientUri(td.uri), td.text);
1088
+ }
709
1089
  this.refreshContent(uri, clientId);
710
1090
  }
711
1091
  }
712
1092
 
1093
+ /**
1094
+ * Record `clientId` as an additional holder of a document another client
1095
+ * already has open.
1096
+ *
1097
+ * Unlike the attach branch of {@link notifyDidOpenTextDocument} this does
1098
+ * not refresh. That branch re-renders an attaching TEXTUAL view; a client
1099
+ * reaching a document through a non-textual route has nothing to re-render,
1100
+ * and refreshing per attach turns each into a Langium rebuild and dependent
1101
+ * relink cascade.
1102
+ *
1103
+ * The staleness guard baselines at the SYNCED version, not a declared buffer
1104
+ * version, because a client arriving this way holds no buffer of its own.
1105
+ * Unifying the two routes therefore rebaselines a textual client's guard to a
1106
+ * version it never declared.
1107
+ *
1108
+ * Returns whether a hold was added — `false` when `uri` is not open,
1109
+ * `clientId` already holds it, or the document was waiting out the revert
1110
+ * grace for other clients and was released instead (see
1111
+ * {@link resolvePendingRevert}); the caller then opens it anew.
1112
+ */
1113
+ attachClient(uri: DocumentUri, clientId: string): boolean {
1114
+ const key = this.documentKey(uri);
1115
+ if (!this.__syncedDocuments.has(key) || this.isOpenInClient(key, clientId)) {
1116
+ return false;
1117
+ }
1118
+ this.resolvePendingRevert(key, clientId);
1119
+ const document = this.__syncedDocuments.get(key);
1120
+ if (!document) {
1121
+ return false;
1122
+ }
1123
+ const existingClients = this.__sessions.clientsOf(key);
1124
+ this.__sessions.addOpen(key, clientId);
1125
+ const record = this.trackingFor(key);
1126
+ record.clientVersions.set(clientId, document.version);
1127
+ this.logClientJoined(key, clientId, document.version, existingClients);
1128
+ return true;
1129
+ }
1130
+
1131
+ protected logClientJoined(uri: DocumentUri, clientId: string, version: number, existingClients: readonly string[]): void {
1132
+ this.log(
1133
+ uri,
1134
+ `Attach client: ${this.formatClientId(clientId)} joined existing document (version ${version}, ` +
1135
+ `now open in: ${[...existingClients, clientId].map(id => this.formatClientId(id)).join(', ')})`
1136
+ );
1137
+ }
1138
+
713
1139
  refreshContent(uri: DocumentUri, clientId: string): void {
714
1140
  const syncedDocument = this.__syncedDocuments.get(this.documentKey(uri));
715
1141
  if (syncedDocument) {
716
1142
  // Trigger a (re-)build by firing a change event.
717
1143
  const timer = this.startTimerForUri(
718
1144
  syncedDocument.uri,
719
- `Refresh synced document: Version ${syncedDocument.version} by ${clientId}`
1145
+ `Refresh synced document: Version ${syncedDocument.version} by ${this.formatClientId(clientId)}`
720
1146
  );
721
1147
  this.__onDidChangeContent.fire(Object.freeze({ document: syncedDocument, clientId }));
722
1148
  timer.dispose();
@@ -730,7 +1156,7 @@ export class HydraniumTextDocuments<T extends TextDocument = TextDocument> exten
730
1156
  * two URIs for one physical file (a symlink path and its real path) into a
731
1157
  * single registration — dedup at the editor layer, not just in
732
1158
  * `LangiumDocuments`. The URI the client opened under is preserved separately
733
- * for egress addressing (see {@link DocumentTracking.languageClientUris}). The
1159
+ * for egress addressing (see {@link DocumentTrackingRecord.languageClientDocuments}). The
734
1160
  * policy is always bound (the framework defaults it to
735
1161
  * `DefaultDocumentUriPolicy`, where canonical ≡ syntactic normalize).
736
1162
  */
@@ -751,10 +1177,10 @@ export class HydraniumTextDocuments<T extends TextDocument = TextDocument> exten
751
1177
  }
752
1178
 
753
1179
  /** Get-or-create the per-URI tracking record. `uri` must already be a {@link documentKey}. */
754
- protected trackingFor(uri: CanonicalUri): DocumentTracking {
1180
+ protected trackingFor(uri: CanonicalUri): DocumentTrackingRecord {
755
1181
  let record = this.__documents.get(uri);
756
1182
  if (!record) {
757
- record = { clients: new Set(), versionAuthors: [], clientVersions: new Map() };
1183
+ record = { versionAuthors: [], clientVersions: new Map() };
758
1184
  this.__documents.set(uri, record);
759
1185
  }
760
1186
  return record;
@@ -790,7 +1216,7 @@ export class HydraniumTextDocuments<T extends TextDocument = TextDocument> exten
790
1216
  * `0` for a URI this store has never seen. The shared sequence is
791
1217
  * server-owned and monotonic across close/reopen cycles, and advances
792
1218
  * exactly when the synced content changes — which is what makes it a sound
793
- * optimistic-concurrency token (`baseVersion` gates): version unchanged ⇔
1219
+ * optimistic-concurrency token (based-on gates): version unchanged ⇔
794
1220
  * content unchanged.
795
1221
  */
796
1222
  version(uri: DocumentUri): number {
@@ -822,7 +1248,7 @@ export class HydraniumTextDocuments<T extends TextDocument = TextDocument> exten
822
1248
  if (sequence === undefined) {
823
1249
  return undefined;
824
1250
  }
825
- const hash = contentHash(text);
1251
+ const hash = textHash(text);
826
1252
  if (hash === sequence.contentHash) {
827
1253
  return sequence.version;
828
1254
  }
@@ -832,6 +1258,59 @@ export class HydraniumTextDocuments<T extends TextDocument = TextDocument> exten
832
1258
  return stepped.version;
833
1259
  }
834
1260
 
1261
+ /**
1262
+ * Commit an integrity repair into the OPEN document for `uri`, returning the
1263
+ * document that now holds it, or `undefined` when there was nothing here to
1264
+ * commit into.
1265
+ *
1266
+ * The one thing this exists to do is address the store BY URI. A repair
1267
+ * reaches the resync as a text-document object, and that object is the
1268
+ * store's own only on the LSP path; a document built for an already-open URI
1269
+ * through `LangiumDocumentFactory.fromString` carries its own, so writing the
1270
+ * repair into it corrects a copy the store does not know about and the editor
1271
+ * never sees.
1272
+ *
1273
+ * `parsedFrom` is the text the repaired AST was parsed from, and a mismatch
1274
+ * against the current content REFUSES the commit: the store has moved on,
1275
+ * which means an editor change landed after that parse, and committing would
1276
+ * overwrite a newer edit with a repair computed against text the user has
1277
+ * already replaced. The build that change provokes recomputes the repair.
1278
+ *
1279
+ * Deliberately NOT compared by version. A separately created document seeds
1280
+ * its own numbering, so requiring the two to agree would refuse every commit
1281
+ * on the path this exists for. Content is the thing both sides can be held to.
1282
+ *
1283
+ * A changed text is a new version authored by {@link INTEGRITY_CLIENT_ID}.
1284
+ * Kept at the old version, the repair is invisible to every reader keyed on
1285
+ * versions: a write based on the unrepaired version passes the based-on gate
1286
+ * and replaces the repair, and an echo filter credits the repair to the
1287
+ * client whose edit it corrected. No change event is fired: the repair
1288
+ * rides the build already under way, and an event would re-enter it.
1289
+ */
1290
+ commitRepair(uri: DocumentUri, parsedFrom: string, repaired: string): RepairCommit<T> {
1291
+ const key = this.documentKey(uri);
1292
+ const document = this.__syncedDocuments.get(key);
1293
+ if (document === undefined) {
1294
+ return { status: 'not-open' };
1295
+ }
1296
+ if (document.getText() !== parsedFrom) {
1297
+ this.logUri(key, 'Refuse repair commit: the open document moved on from the text the AST was parsed from', 'debug');
1298
+ return { status: 'stale' };
1299
+ }
1300
+ if (repaired === parsedFrom) {
1301
+ return { status: 'committed', document };
1302
+ }
1303
+ // Reassigned rather than mutated in place: the default configuration
1304
+ // updates and returns the SAME instance, but an adopter-supplied one may
1305
+ // return a new object, and the store must end up holding whichever it is.
1306
+ const updated = this.configuration.update(document, [{ text: repaired }], document.version + 1);
1307
+ this.__syncedDocuments.set(key, updated);
1308
+ this.setAuthor(key, updated.version, INTEGRITY_CLIENT_ID);
1309
+ this.refreshDirty(key);
1310
+ this.log(updated.uri, `Update to version ${updated.version} by ${this.formatClientId(INTEGRITY_CLIENT_ID)} (repair)`);
1311
+ return { status: 'committed', document: updated };
1312
+ }
1313
+
835
1314
  getAuthor(uri: DocumentUri, version?: number): string | undefined {
836
1315
  const history = this.__documents.get(this.documentKey(uri))?.versionAuthors;
837
1316
  // Either the requested version, or the latest. `version !== undefined` so we treat 0 correctly.
@@ -852,17 +1331,17 @@ export class HydraniumTextDocuments<T extends TextDocument = TextDocument> exten
852
1331
  * True iff any client still holds `uri` open. Distinct from {@link isOpen},
853
1332
  * which reads `__syncedDocuments` — that map is cleared only AFTER the
854
1333
  * `onDidClose` event fires for the last client, so `isOpen` returns `true`
855
- * during the close event itself. `isOpenInAnyClient` reads the tracking
856
- * record's `clients` set, which is updated BEFORE the fire, so an `onDidClose`
1334
+ * during the close event itself. `isOpenInAnyClient` reads the open table
1335
+ * ({@link __sessions}), which is updated BEFORE the fire, so an `onDidClose`
857
1336
  * subscriber that finds this `false` knows the last client just closed and a
858
1337
  * disk re-read / rebuild can proceed.
859
1338
  */
860
1339
  isOpenInAnyClient(uri: DocumentUri): boolean {
861
- return (this.__documents.get(this.documentKey(uri))?.clients.size ?? 0) > 0;
1340
+ return this.__sessions.isOpen(this.documentKey(uri));
862
1341
  }
863
1342
 
864
1343
  isOpenInClient(uri: DocumentUri, client: string): boolean {
865
- return !!this.__documents.get(this.documentKey(uri))?.clients.has(client);
1344
+ return this.__sessions.isOpenIn(this.documentKey(uri), client);
866
1345
  }
867
1346
 
868
1347
  isOpenInLanguageClient(uri: DocumentUri): boolean {
@@ -873,14 +1352,14 @@ export class HydraniumTextDocuments<T extends TextDocument = TextDocument> exten
873
1352
  }
874
1353
 
875
1354
  isOnlyOpenInClient(uri: DocumentUri, client: string): boolean {
876
- const key = this.documentKey(uri);
877
- return this.__documents.get(key)?.clients.size === 1 && this.isOpenInClient(key, client);
1355
+ const clients = this.__sessions.clientsOf(this.documentKey(uri));
1356
+ return clients.length === 1 && clients[0] === client;
878
1357
  }
879
1358
 
880
1359
  /**
881
1360
  * Every document currently held open by at least one client, with the client
882
- * ids holding it. Reads the same per-URI tracking records as
883
- * {@link isOpenInAnyClient}, so it reflects a last-client close immediately.
1361
+ * ids holding it. Reads the same open table as {@link isOpenInAnyClient}, so
1362
+ * it reflects a last-client close immediately.
884
1363
  *
885
1364
  * Diagnostics-oriented — the server-state snapshot lists these so an operator
886
1365
  * can see WHY a document is pinned: a document that lingers here after its
@@ -888,28 +1367,258 @@ export class HydraniumTextDocuments<T extends TextDocument = TextDocument> exten
888
1367
  * under a shedding policy, explains why its CST is never shed).
889
1368
  */
890
1369
  openDocuments(): OpenDocument[] {
891
- const result: OpenDocument[] = [];
892
- for (const [uri, tracking] of this.__documents) {
893
- if (tracking.clients.size > 0) {
894
- result.push({ uri, clients: [...tracking.clients] });
1370
+ return this.__sessions.openDocuments();
1371
+ }
1372
+
1373
+ /** Fires once a client session has ended, after every document it had open was closed. */
1374
+ get onDidCloseSession(): Event<ClientSessionClosedEvent> {
1375
+ return this.__sessions.onDidCloseSession;
1376
+ }
1377
+
1378
+ /**
1379
+ * Fires once a document no client has open is released: at its last close,
1380
+ * or, for a lost client's last close, when its revert grace runs out,
1381
+ * another client opens it, or it is deleted. The document then reverts to
1382
+ * disk.
1383
+ */
1384
+ get onDidCloseLastOpen(): Event<LastOpenClosedEvent> {
1385
+ return this.lastOpenClosedEmitter.event;
1386
+ }
1387
+
1388
+ /**
1389
+ * Fires for every save the language client reports of a document it has
1390
+ * open: the editor has written the file. {@link onDidSave} follows only
1391
+ * when the file holds the store's text.
1392
+ */
1393
+ get onDidSaveInLanguageClient(): Event<LanguageClientSavedEvent> {
1394
+ return this.languageClientSavedEmitter.event;
1395
+ }
1396
+
1397
+ /**
1398
+ * Whether `uri` is waiting out the revert grace: its last open closed with a
1399
+ * lost connection, and it still holds its unsaved text. Such a document is
1400
+ * open for no client, yet not closed either, so a caller that would persist
1401
+ * a closed document's text to disk treats it as open.
1402
+ */
1403
+ isRevertPending(uri: DocumentUri): boolean {
1404
+ return this.__sessions.isRevertPending(this.documentKey(uri));
1405
+ }
1406
+
1407
+ /**
1408
+ * Whether the store holds `uri` with text that differs from its disk
1409
+ * baseline: what the server last knew the file to hold. `false` for a URI
1410
+ * the store does not hold; a document waiting out the revert grace is still
1411
+ * held.
1412
+ *
1413
+ * The baseline is the text a first open brought, or what the server wrote,
1414
+ * or read back after an editor's save or a watched-file change, so it can
1415
+ * trail a change to the file that none of these has seen yet. A check that
1416
+ * must know the file reads it instead.
1417
+ */
1418
+ isDirty(uri: DocumentUri): boolean {
1419
+ return this.__documents.get(this.documentKey(uri))?.dirty ?? false;
1420
+ }
1421
+
1422
+ /** Fires each time the answer of {@link isDirty} changes, the release of a dirty document included. */
1423
+ get onDidChangeDirty(): Event<DocumentDirtyChangedEvent> {
1424
+ return this.dirtyChangedEmitter.event;
1425
+ }
1426
+
1427
+ /**
1428
+ * Record that the file behind `uri` holds `text`, or no file at all for
1429
+ * `undefined`. A no-op for a URI the store does not hold: the next first
1430
+ * open sets the baseline from its own text.
1431
+ */
1432
+ updateDiskBaseline(uri: DocumentUri, text: string | undefined): void {
1433
+ const key = this.documentKey(uri);
1434
+ const record = this.__documents.get(key);
1435
+ if (!record || !this.__syncedDocuments.has(key)) {
1436
+ return;
1437
+ }
1438
+ record.diskBaseline = text;
1439
+ this.refreshDirty(key);
1440
+ }
1441
+
1442
+ /**
1443
+ * Read the file behind `uri` through its disk queue and take it as the
1444
+ * baseline. A file that cannot be read counts as none, the side that
1445
+ * leaves the document dirty.
1446
+ */
1447
+ async reloadDiskBaseline(uri: DocumentUri): Promise<void> {
1448
+ const key = this.documentKey(uri);
1449
+ if (!this.__syncedDocuments.has(key)) {
1450
+ return;
1451
+ }
1452
+ let onDisk: string | undefined;
1453
+ try {
1454
+ onDisk = await this.services.workspace.FileSystemTaskQueue.enqueue(key, () =>
1455
+ this.services.workspace.FileSystemProvider.readFile(UriUtils.toUri(key))
1456
+ );
1457
+ } catch (err: unknown) {
1458
+ this.tracer.with(key).debug(`Disk baseline: the file cannot be read. ${err instanceof Error ? err.message : String(err)}`);
1459
+ }
1460
+ this.updateDiskBaseline(key, onDisk);
1461
+ }
1462
+
1463
+ /** Compare the held text of `uri` with its baseline, and announce a changed answer. */
1464
+ protected refreshDirty(uri: CanonicalUri): void {
1465
+ const record = this.__documents.get(uri);
1466
+ const document = this.__syncedDocuments.get(uri);
1467
+ if (!record || !document) {
1468
+ return;
1469
+ }
1470
+ const dirty = document.getText() !== record.diskBaseline;
1471
+ if (dirty !== (record.dirty ?? false)) {
1472
+ record.dirty = dirty;
1473
+ this.dirtyChangedEmitter.fire(Object.freeze({ uri, dirty }));
1474
+ }
1475
+ }
1476
+
1477
+ /**
1478
+ * Start a client session under `clientId`. Throws where
1479
+ * {@link ClientSessionRegistry.register} refuses the id.
1480
+ */
1481
+ registerSession(clientId: string): void {
1482
+ this.__sessions.register(clientId);
1483
+ this.tracer.info(`Session started: ${this.formatClientId(clientId)}`);
1484
+ this.tracer.trace(`Session started: ${clientId}`);
1485
+ }
1486
+
1487
+ /**
1488
+ * End the client session `clientId`: close every document it has open, then
1489
+ * free the id. Immediate — each close runs the ordinary close path before
1490
+ * this returns, with `cause` as its cause. A no-op for an id that is not a
1491
+ * registered session.
1492
+ */
1493
+ closeSession(clientId: string, cause: SessionEndCause = 'closed'): void {
1494
+ if (!this.__sessions.isRegistered(clientId)) {
1495
+ return;
1496
+ }
1497
+ try {
1498
+ for (const uri of this.__sessions.beginClose(clientId)) {
1499
+ this.notifyDidCloseTextDocument({ textDocument: { uri } }, clientId, cause);
1500
+ }
1501
+ } finally {
1502
+ this.__sessions.unregister(clientId);
1503
+ this.tracer.info(`Session closed: ${this.formatClientId(clientId)}`);
1504
+ this.tracer.trace(`Session closed: ${clientId}`);
1505
+ }
1506
+ }
1507
+
1508
+ /**
1509
+ * Close every document the language client has open, as a `didClose` for
1510
+ * each would, so each last close reverts. For a host whose editor connection
1511
+ * can end while the process lives on, such as a worker whose port's peer
1512
+ * closed: the language client is no session, so nothing else closes them.
1513
+ *
1514
+ * It first waits for the workspace initialization the open handler waits
1515
+ * for, so an open that arrived before the close is closed too; closing at
1516
+ * once would leave that open to land afterwards, held by a client that is
1517
+ * gone.
1518
+ */
1519
+ async closeLanguageClientDocuments(): Promise<void> {
1520
+ await this.initialBuildFinished();
1521
+ for (const uri of this.__sessions.opensOf(LANGUAGE_CLIENT_ID)) {
1522
+ this.untrackLanguageClientDocuments(uri);
1523
+ this.notifyDidCloseTextDocument({ textDocument: { uri } });
1524
+ }
1525
+ }
1526
+
1527
+ /**
1528
+ * Settles once the initial workspace build has finished, whether it
1529
+ * completed, was cancelled or failed. Awaiting `workspaceInitialized` itself
1530
+ * throws after a cancelled build, which any write during startup causes, and
1531
+ * every open and close gated on it is then dropped for the process lifetime.
1532
+ * The workspace manager logs a failed build.
1533
+ */
1534
+ protected async initialBuildFinished(): Promise<void> {
1535
+ await this.services.workspace.WorkspaceManager.workspaceInitialized.catch(() => undefined);
1536
+ }
1537
+
1538
+ /**
1539
+ * Stop tracking every URI the language client holds the document `key`
1540
+ * under, with its shadow and pending pushes. Call before a close by
1541
+ * canonical key that means all of them: a close for one URI while others
1542
+ * remain drops only that URI's and keeps the client's hold.
1543
+ */
1544
+ protected untrackLanguageClientDocuments(key: CanonicalUri): void {
1545
+ const languageClientDocuments = this.__documents.get(key)?.languageClientDocuments;
1546
+ for (const clientUri of languageClientDocuments?.keys() ?? []) {
1547
+ this.__shadow.invalidate(clientUri);
1548
+ this.__pendingPushes.delete(clientUri);
1549
+ }
1550
+ languageClientDocuments?.clear();
1551
+ }
1552
+
1553
+ /**
1554
+ * The file behind `uri` was deleted: close every open of it except the
1555
+ * language client's.
1556
+ *
1557
+ * The editor's open is left alone because the editor owns it: it keeps the
1558
+ * buffer of a deleted file and goes on sending changes for it, and a store
1559
+ * that had closed the document would drop every one of them until the editor
1560
+ * reopened.
1561
+ */
1562
+ notifyDocumentDeleted(uri: DocumentUri): void {
1563
+ const key = this.documentKey(uri);
1564
+ for (const clientId of this.__sessions.clientsOf(key)) {
1565
+ if (clientId !== LANGUAGE_CLIENT_ID) {
1566
+ this.notifyDidCloseTextDocument({ textDocument: { uri: key } }, clientId);
895
1567
  }
896
1568
  }
897
- return result;
898
1569
  }
899
1570
 
900
1571
  /**
901
- * Stages integrity-updated content for a closed document.
1572
+ * Remove the document for `uri`, closing every open of it first — the
1573
+ * editor's included, since the document it would keep editing is gone.
1574
+ *
1575
+ * The base removes the synced document without consulting the open table,
1576
+ * which leaves every open recorded against a document that no longer exists:
1577
+ * the store then answers that the URI is open, and the next open attaches to
1578
+ * nothing.
1579
+ */
1580
+ override delete(uri: string | URI | T): void {
1581
+ const key = this.documentKey((typeof uri === 'object' && 'uri' in uri ? uri.uri : uri).toString());
1582
+ this.untrackLanguageClientDocuments(key);
1583
+ for (const clientId of this.__sessions.clientsOf(key)) {
1584
+ this.notifyDidCloseTextDocument({ textDocument: { uri: key } }, clientId);
1585
+ }
1586
+ // A document waiting out the grace has no client left to close, and is
1587
+ // released now as its last close would have released it.
1588
+ if (this.__sessions.isRevertPending(key)) {
1589
+ this.__sessions.cancelRevert(key);
1590
+ this.releaseDocument(key);
1591
+ }
1592
+ super.delete(key);
1593
+ }
1594
+
1595
+ /**
1596
+ * A client id as log lines print it. An id containing `#` is cut eight
1597
+ * characters after its last `#`, which leaves a minted `label#uuid` as the
1598
+ * label and enough of the UUID to tell sessions apart; an id without `#`
1599
+ * prints whole. The full id goes out at trace level wherever a session
1600
+ * starts or ends.
1601
+ */
1602
+ protected formatClientId(clientId: string): string {
1603
+ const separator = clientId.lastIndexOf('#');
1604
+ return separator < 0 ? clientId : clientId.slice(0, separator + 9);
1605
+ }
1606
+
1607
+ /**
1608
+ * Stages integrity-updated content for a document no client holds.
902
1609
  *
903
1610
  * When `workspace/applyEdit` targets a closed file, the client opens it from
904
1611
  * disk (stale) and sends `didOpen` before applying the edit. This staged
905
1612
  * content is consumed by {@link notifyDidOpenTextDocument} to replace the
906
1613
  * stale disk text, preventing a brief revert of the integrity update.
907
1614
  *
908
- * Not cleared on close: pending content is only staged for already-closed
909
- * documents (the integrity service checks `isOpenInLanguageClient` first), so
910
- * a close → stage → open cycle cannot occur. The only scenario where an
911
- * entry lingers is if `workspace/applyEdit` fails and the file is never
912
- * opened — the memory cost is one serialised string per URI.
1615
+ * Only a FIRST open consumes it, so stage only for a URI no client holds
1616
+ * ({@link isOpenInAnyClient} is `false`). A URI held only through another
1617
+ * head is not closed: an editor attaching to it joins the existing entry and
1618
+ * never reads the stage, and the last close discards the stage with the
1619
+ * tracking record. An entry lingers only if `workspace/applyEdit` fails and
1620
+ * the file is never opened — the memory cost is one serialised string per
1621
+ * URI.
913
1622
  */
914
1623
  stagePendingContent(uri: DocumentUri, text: string): void {
915
1624
  this.trackingFor(this.documentKey(uri)).pendingContent = text;
@@ -942,8 +1651,9 @@ export class HydraniumTextDocuments<T extends TextDocument = TextDocument> exten
942
1651
  * and the client applying, the ranges address the wrong lines and splice the
943
1652
  * buffer (observed as a duplicated declaration, which the integrity tier then
944
1653
  * "repairs" into a suffixed name and persists). The edit is therefore
945
- * addressed at the language client's LAST DECLARED VERSION rather than at
946
- * `null` ("version intentionally unknown"), which is what lets the client
1654
+ * addressed at the language client's last known version for that URI (see
1655
+ * {@link languageClientVersion}) rather than at `null` ("version
1656
+ * intentionally unknown"), which is what lets the client
947
1657
  * reject a push its buffer has outrun. On rejection the shadow is invalidated,
948
1658
  * so the caller's retry is a full-range replace — position-independent, and
949
1659
  * safe to apply to whatever the client now holds.
@@ -963,14 +1673,15 @@ export class HydraniumTextDocuments<T extends TextDocument = TextDocument> exten
963
1673
  // when the same file was opened under a symlink and its real path. Falls back
964
1674
  // to the normalized URI when nothing is tracked. The shadow is keyed by each
965
1675
  // URI, so every diff is against the right baseline.
966
- const recorded = this.__documents.get(this.documentKey(uri))?.languageClientUris;
967
- const targets: Iterable<LanguageClientUri> = recorded && recorded.size > 0 ? recorded : [this.toLanguageClientUri(uri)];
1676
+ const recorded = this.__documents.get(this.documentKey(uri))?.languageClientDocuments;
1677
+ const targets: Iterable<LanguageClientUri> = recorded && recorded.size > 0 ? [...recorded.keys()] : [this.toLanguageClientUri(uri)];
968
1678
  let lastResult: ApplyWorkspaceEditResult | undefined;
969
1679
  for (const targetUri of targets) {
970
- // Read BEFORE computeEdits, which overwrites it. This is the text the
971
- // edits below are keyed to, and therefore the only text the client's
972
- // echo of them can be reconstructed against.
973
- const before = this.__shadow.get(targetUri);
1680
+ // Read BEFORE computeEdits, which overwrites the baseline and drops
1681
+ // the opened snapshot. This is the text the client holds, and
1682
+ // therefore the only text its echo of this push can be reconstructed
1683
+ // against — whether or not the push below is keyed to it.
1684
+ const before = this.__shadow.clientText(targetUri);
974
1685
  const edits = this.__shadow.computeEdits(targetUri, newText);
975
1686
  if (edits.length === 0) {
976
1687
  continue;
@@ -984,14 +1695,42 @@ export class HydraniumTextDocuments<T extends TextDocument = TextDocument> exten
984
1695
  // stale-by-one version into a refused update for no safety gain — and
985
1696
  // it is exactly what the caller retries with after a rejection, so
986
1697
  // gating it there would refuse the recovery too.
987
- const version = isFullReplace(edits) ? UNKNOWN_CLIENT_VERSION : this.languageClientVersion(uri);
1698
+ const version = isFullReplace(edits) ? UNKNOWN_CLIENT_VERSION : this.languageClientVersion(uri, targetUri);
1699
+ // Captured before the await: a reopen replaces the state, so a late
1700
+ // reply then writes to the old one instead of the new buffer's.
1701
+ const languageClientDocument = recorded?.get(targetUri);
1702
+ // A full `ApplyWorkspaceEditParams`, `edit` and all — NOT a bare
1703
+ // `WorkspaceEdit` with a `label` beside it. `applyEdit` takes
1704
+ // `ApplyWorkspaceEditParams | WorkspaceEdit` and discriminates on
1705
+ // `!!value.edit`, so `{ label, documentChanges }` is wrapped as
1706
+ // `{ edit: { label, documentChanges } }` — putting the label inside
1707
+ // the edit, where LSP defines no such field and no client reads it.
1708
+ // The union is also what hides it at compile time: excess-property
1709
+ // checking admits a property present in EITHER member, so an object
1710
+ // matching neither type-checks against the union.
988
1711
  const result = await connection.workspace.applyEdit({
989
1712
  label: options?.label,
990
- documentChanges: [TextDocumentEdit.create(OptionalVersionedTextDocumentIdentifier.create(targetUri, version), edits)]
1713
+ edit: {
1714
+ documentChanges: [TextDocumentEdit.create(OptionalVersionedTextDocumentIdentifier.create(targetUri, version), edits)]
1715
+ }
991
1716
  });
992
1717
  if (result && result.applied === false) {
993
1718
  this.__shadow.invalidate(targetUri);
994
1719
  this.__pendingPushes.delete(targetUri);
1720
+ if (version !== UNKNOWN_CLIENT_VERSION) {
1721
+ this.tracer
1722
+ .with(uri)
1723
+ .warn(
1724
+ `Language client refused applyEdit addressed at version ${version} (it last declared version ${languageClientDocument?.declaredVersion})`
1725
+ );
1726
+ }
1727
+ } else if (result?.applied && version !== UNKNOWN_CLIENT_VERSION && languageClientDocument) {
1728
+ // A client steps once per applied edit that changes its buffer. A
1729
+ // line edit does while the shadow is right; a full replace may be a
1730
+ // no-op the client drops. Left to the echo, a push sent first is
1731
+ // refused; advanced after a no-op, one could pass at a version a
1732
+ // keystroke reached.
1733
+ languageClientDocument.pushedVersion = version + 1;
995
1734
  }
996
1735
  lastResult = result;
997
1736
  } catch (err) {
@@ -1004,21 +1743,27 @@ export class HydraniumTextDocuments<T extends TextDocument = TextDocument> exten
1004
1743
  }
1005
1744
 
1006
1745
  /**
1007
- * The version the LSP textual language client last declared for `uri`, for
1008
- * addressing an outgoing `workspace/applyEdit`.
1746
+ * The version the LSP textual language client holds `uri` at under
1747
+ * `targetUri`, for addressing an outgoing `workspace/applyEdit`.
1009
1748
  *
1010
1749
  * Client version ids are CLIENT-owned per LSP, so this is the id the client
1011
- * itself stamped on its last `didOpen` / `didChange` — never the shared
1012
- * server version, which advances on authored writes the client knows nothing
1013
- * about and would therefore reject every push.
1750
+ * itself stamped on its last `didOpen` / `didChange` for `targetUri`, or the
1751
+ * one an applied push moved it to ahead of the push's echo
1752
+ * ({@link LanguageClientDocumentState}) — never the shared server version,
1753
+ * which advances on authored writes the client knows nothing about and would
1754
+ * therefore reject every push.
1014
1755
  *
1015
- * Falls back to {@link UNKNOWN_CLIENT_VERSION} when the client has never
1016
- * declared one, which is the honest answer for a document it has not opened.
1017
- * That is also the case in which there is no shadow, so the push is already a
1756
+ * Falls back to {@link UNKNOWN_CLIENT_VERSION} when the client has not opened
1757
+ * the document under `targetUri`, which is the honest answer. That is also
1758
+ * the case in which there is no shadow, so the push is already a
1018
1759
  * position-independent full replace and has nothing to gain from a gate.
1019
1760
  */
1020
- protected languageClientVersion(uri: DocumentUri): number {
1021
- return this.__documents.get(this.documentKey(uri))?.clientVersions.get(LANGUAGE_CLIENT_ID) ?? UNKNOWN_CLIENT_VERSION;
1761
+ protected languageClientVersion(uri: DocumentUri, targetUri: LanguageClientUri = this.toLanguageClientUri(uri)): number {
1762
+ const state = this.__documents.get(this.documentKey(uri))?.languageClientDocuments?.get(targetUri);
1763
+ if (state === undefined) {
1764
+ return UNKNOWN_CLIENT_VERSION;
1765
+ }
1766
+ return Math.max(state.declaredVersion, state.pushedVersion ?? state.declaredVersion);
1022
1767
  }
1023
1768
 
1024
1769
  /**
@@ -1034,7 +1779,7 @@ export class HydraniumTextDocuments<T extends TextDocument = TextDocument> exten
1034
1779
  pending = [];
1035
1780
  this.__pendingPushes.set(targetUri, pending);
1036
1781
  }
1037
- pending.push({ before, afterHash: contentHash(newText) });
1782
+ pending.push({ before, afterHash: textHash(newText) });
1038
1783
  if (pending.length > PENDING_ECHO_CAP) {
1039
1784
  pending.shift();
1040
1785
  this.logUri(targetUri, `Pending-echo queue exceeded ${PENDING_ECHO_CAP} entries; dropped the oldest`, 'debug');
@@ -1043,12 +1788,17 @@ export class HydraniumTextDocuments<T extends TextDocument = TextDocument> exten
1043
1788
 
1044
1789
  /**
1045
1790
  * Decide what an incoming language-client change actually is, by
1046
- * reconstructing the client's resulting buffer against the OLDEST push still
1047
- * in flight for `clientFacing`.
1791
+ * reconstructing the client's resulting buffer against the text its ranges
1792
+ * address — the pre-push buffer of the OLDEST push still in flight for
1793
+ * `clientFacing`, else whatever that client is believed to hold.
1048
1794
  *
1049
- * `undefined` when nothing of ours is in flight, which is the ordinary path:
1050
- * the client's ranges then address the synced text and the caller applies
1051
- * them to it directly.
1795
+ * `undefined` when the client's ranges address the synced text itself —
1796
+ * nothing in flight, and no evidence the client holds anything else. That
1797
+ * is the ordinary path, and the caller then applies the ranges directly.
1798
+ * The two texts part company without a push in flight whenever a client
1799
+ * attaches to a document another client has already written: it opened
1800
+ * from disk, so applying its ranges to the synced text splices lines they
1801
+ * never addressed.
1052
1802
  *
1053
1803
  * **Why the oldest, and why reconstruct at all.** The client applies our
1054
1804
  * pushes in order and echoes each against the buffer it held before that
@@ -1079,22 +1829,41 @@ export class HydraniumTextDocuments<T extends TextDocument = TextDocument> exten
1079
1829
  document: T,
1080
1830
  changes: TextDocumentContentChangeEvent[]
1081
1831
  ): LanguageClientChangeOrigin | undefined {
1082
- const pending = this.__pendingPushes.get(clientFacing);
1083
- if (pending === undefined || pending.length === 0) {
1832
+ const queued = this.__pendingPushes.get(clientFacing);
1833
+ const pending = queued?.length ? queued : undefined;
1834
+ // The text the client's ranges address: the buffer it held before the
1835
+ // oldest push still in flight, else the buffer it is believed to hold.
1836
+ const clientText = pending?.[0].before ?? this.__shadow.clientText(clientFacing);
1837
+ if (pending === undefined && (clientText === undefined || clientText === document.getText())) {
1084
1838
  return undefined;
1085
1839
  }
1840
+ if (pending !== undefined && pending[0].before === undefined && changes.some(change => ContentChange.isIncremental(change))) {
1841
+ // A push sent to a client whose buffer was unknown — a first sync, or
1842
+ // the recovery push after a rejection. The shadow now holds the text
1843
+ // that push MOVES the client to, which is the one text the echo's
1844
+ // ranges provably do not address, so the fallback above is a baseline
1845
+ // known to be wrong rather than merely unverified. Drop the queue and
1846
+ // the shadow: the next sync is then a full replace, which lands on
1847
+ // whatever the client holds. A full-text change is exempt because it
1848
+ // reconstructs identically against any baseline.
1849
+ pending.length = 0;
1850
+ this.__shadow.invalidate(clientFacing);
1851
+ return { kind: 'unreconstructable' };
1852
+ }
1086
1853
  // Through the configured factories, not `TextDocument` directly, so an
1087
1854
  // adopter's custom text-document type governs how the ranges are applied
1088
1855
  // here exactly as it does on the synced document.
1089
- const probe = this.create(clientFacing, document.languageId, 0, pending[0].before ?? document.getText());
1090
- const clientText = this.update(probe, changes, 0).getText();
1091
- const matchIndex = pending.findIndex(push => push.afterHash === contentHash(clientText));
1092
- if (matchIndex >= 0) {
1093
- pending.splice(0, matchIndex + 1);
1094
- return { kind: 'echo' };
1856
+ const probe = this.create(clientFacing, document.languageId, 0, clientText ?? document.getText());
1857
+ const reconstructed = this.update(probe, changes, 0).getText();
1858
+ if (pending !== undefined) {
1859
+ const matchIndex = pending.findIndex(push => push.afterHash === textHash(reconstructed));
1860
+ if (matchIndex >= 0) {
1861
+ pending.splice(0, matchIndex + 1);
1862
+ return { kind: 'echo' };
1863
+ }
1864
+ pending.length = 0;
1095
1865
  }
1096
- pending.length = 0;
1097
- return { kind: 'divergent', text: clientText };
1866
+ return { kind: 'divergent', text: reconstructed };
1098
1867
  }
1099
1868
 
1100
1869
  /**