@hydranium/data-server 1.0.0-next.40 → 1.0.0-next.400

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 (35) hide show
  1. package/README.md +57 -77
  2. package/lib/browser/index.d.ts +10 -0
  3. package/lib/browser/index.d.ts.map +1 -0
  4. package/lib/browser/index.js +15 -0
  5. package/lib/browser/index.js.map +1 -0
  6. package/lib/browser/start-data-server-in-worker.d.ts +23 -0
  7. package/lib/browser/start-data-server-in-worker.d.ts.map +1 -0
  8. package/lib/browser/start-data-server-in-worker.js +29 -0
  9. package/lib/browser/start-data-server-in-worker.js.map +1 -0
  10. package/lib/data-server.d.ts +407 -319
  11. package/lib/data-server.d.ts.map +1 -1
  12. package/lib/data-server.js +728 -530
  13. package/lib/data-server.js.map +1 -1
  14. package/lib/messages/index.d.ts +1 -1
  15. package/lib/messages/index.d.ts.map +1 -1
  16. package/lib/messages/index.js +1 -1
  17. package/lib/messages/index.js.map +1 -1
  18. package/lib/node/node-diagnostics-provider.d.ts.map +1 -1
  19. package/lib/node/node-diagnostics-provider.js +3 -2
  20. package/lib/node/node-diagnostics-provider.js.map +1 -1
  21. package/lib/testing/data-server-harness.d.ts +27 -14
  22. package/lib/testing/data-server-harness.d.ts.map +1 -1
  23. package/lib/testing/data-server-harness.js +5 -2
  24. package/lib/testing/data-server-harness.js.map +1 -1
  25. package/lib/testing/index.d.ts +1 -0
  26. package/lib/testing/index.d.ts.map +1 -1
  27. package/lib/testing/index.js.map +1 -1
  28. package/package.json +35 -43
  29. package/src/browser/index.ts +15 -0
  30. package/src/browser/start-data-server-in-worker.ts +35 -0
  31. package/src/data-server.ts +905 -580
  32. package/src/messages/index.ts +8 -1
  33. package/src/node/node-diagnostics-provider.ts +3 -9
  34. package/src/testing/data-server-harness.ts +44 -25
  35. package/src/testing/index.ts +5 -2
@@ -6,9 +6,9 @@
6
6
  *
7
7
  * SPDX-License-Identifier: MIT
8
8
  ********************************************************************************/
9
- import { createRpcProxy, defineMessage, DisposableCollection, isDocumentSource, isElementSource, isSyntheticSource, messageError, ReferenceSource } from '@hydranium/protocol';
9
+ import { asModelVersion, createRpcProxy, defineMessage, describeErrorWithStack, DisposableCollection, HYDRANIUM_ERROR_CODES, isDocumentSource, isElementSource, isResponseError, ConflictError, isModelVersion, isSyntheticSource, messageData, messageError, ReferenceSource, SessionClosedError, TIMED_OUT, TransferDocument, UNKNOWN_CLIENT_ID, STALE_BASE_VERSION_SAVE, STALE_BASE_VERSION_UPDATE, textHash } from '@hydranium/protocol';
10
10
  import { DATA_SERVER_DIAGNOSTICS_METHODS, DATA_SERVER_PROTOCOL_METHODS, DATA_SERVER_WIRE_PREFIX } from '@hydranium/protocol/data';
11
- import { REVERT_ON_CLOSE_CLIENT_ID, UNKNOWN_CLIENT_ID } from '@hydranium/core';
11
+ import { DOCUMENT_RELEASE_CLIENT_ID } from '@hydranium/core';
12
12
  import { defaultDataServerDiagnostics } from './default-diagnostics.js';
13
13
  /**
14
14
  * Raised when a profiling command arrives with no capture running.
@@ -23,9 +23,26 @@ export const NO_ACTIVE_PROFILE = defineMessage('hydranium/data-server/no-active-
23
23
  * The JSON-RPC code, unrelated to the catalogue code above: this one is numeric,
24
24
  * survives reconstruction and is what a caller switches on.
25
25
  */
26
- export const NO_ACTIVE_PROFILE_CODE = 1002;
26
+ export const NO_ACTIVE_PROFILE_CODE = HYDRANIUM_ERROR_CODES.noActiveProfile;
27
27
  export const noActiveProfileError = () => messageError(NO_ACTIVE_PROFILE_CODE, NO_ACTIVE_PROFILE);
28
+ /**
29
+ * Raised when a reference question's wait for the build exceeds
30
+ * {@link DataServerOptions.referenceSettleTimeoutMs}.
31
+ *
32
+ * A `ResponseError` because a class name does not cross RPC: without the code a
33
+ * wire client cannot tell a wedged build from a reference that resolves to
34
+ * nothing, which is the distinction the wait exists to keep.
35
+ */
36
+ export const REFERENCE_SETTLE_TIMEOUT = defineMessage('hydranium/data-server/reference-settle-timeout', 'The model is still being processed; try again in a moment.');
37
+ /** See {@link NO_ACTIVE_PROFILE_CODE} for why this is separate from the catalogue code. */
38
+ export const REFERENCE_SETTLE_TIMEOUT_CODE = HYDRANIUM_ERROR_CODES.referenceSettleTimeout;
39
+ /** The wait's length rides in `data` as `elapsedMs`, for the code that reacts rather than the reader. */
40
+ export const referenceSettleTimeoutError = (elapsedMs) => new ResponseError(REFERENCE_SETTLE_TIMEOUT_CODE, REFERENCE_SETTLE_TIMEOUT.format(), {
41
+ elapsedMs,
42
+ ...messageData(REFERENCE_SETTLE_TIMEOUT)
43
+ });
28
44
  import { DocumentState, UriUtils } from '@hydranium/langium';
45
+ import { ResponseError } from 'vscode-jsonrpc';
29
46
  /**
30
47
  * Domain separator between text and diagnostics inputs of
31
48
  * {@link DataServer.computeDocumentFingerprint}'s hash. A single NUL byte is
@@ -34,47 +51,30 @@ import { DocumentState, UriUtils } from '@hydranium/langium';
34
51
  */
35
52
  const FINGERPRINT_SEPARATOR = '\0';
36
53
  /**
37
- * Portable, non-cryptographic 64-bit fingerprint hash (cyrb53), returning a
38
- * 16-char hex digest.
39
- *
40
- * **A cryptographic hash via `node:crypto` is the wrong trade**: the
41
- * fingerprint only needs a stable signal that `(text, diagnostics)` changed
42
- * between emissions, and a `node:*` import would cost
43
- * `@hydranium/data-server` its browser-portability. Parts are fed
44
- * incrementally, char by char, so multi-MB document text is never
45
- * concatenated into one string.
54
+ * `LSPErrorCodes.ServerCancelled`, with which Langium's document wait rejects
55
+ * for a URI that has no document; `vscode-languageserver-protocol` is not a
56
+ * dependency of this package.
46
57
  */
47
- function fingerprintHash(parts) {
48
- let h1 = 0xdeadbeef;
49
- let h2 = 0x41c6ce57;
50
- for (const part of parts) {
51
- for (let i = 0; i < part.length; i++) {
52
- const ch = part.charCodeAt(i);
53
- h1 = Math.imul(h1 ^ ch, 2654435761);
54
- h2 = Math.imul(h2 ^ ch, 1597334677);
55
- }
56
- }
57
- h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507);
58
- h1 ^= Math.imul(h2 ^ (h2 >>> 13), 3266489909);
59
- h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507);
60
- h2 ^= Math.imul(h1 ^ (h1 >>> 13), 3266489909);
61
- const high = (h2 >>> 0).toString(16).padStart(8, '0');
62
- const low = (h1 >>> 0).toString(16).padStart(8, '0');
63
- return high + low;
64
- }
65
- /** Hash a document's raw text + diagnostics — the `'text-diagnostics'` strategy. */
66
- function textDiagnosticsFingerprint(document) {
67
- return fingerprintHash([document.textDocument.getText(), FINGERPRINT_SEPARATOR, JSON.stringify(document.diagnostics ?? [])]);
58
+ const SERVER_CANCELLED = -32802;
59
+ /**
60
+ * Hash `text`, the text the model was parsed from, + `model`'s diagnostics — the `'text-diagnostics'` strategy.
61
+ * The text comes from the root's ledger record or CST, as the document's own may have moved on,
62
+ * and a repair in place leaves the CST on the unrepaired text;
63
+ * a root built without a CST hashes as `'transfer-document'`.
64
+ * Absent diagnostics must hash apart from `[]`, as in {@link transferDocumentFingerprint}.
65
+ */
66
+ function textDiagnosticsFingerprint(text, model) {
67
+ return text === undefined
68
+ ? transferDocumentFingerprint(model)
69
+ : textHash([text, FINGERPRINT_SEPARATOR, JSON.stringify(model.diagnostics ?? null)]);
68
70
  }
69
- /** Hash an encoded transfer document's root + diagnostics — the `'transfer-document'` strategy. */
70
- function transferDocumentFingerprint(transferDocument) {
71
- // null, not undefined: `JSON.stringify(undefined)` yields undefined rather
72
- // than a string, putting a non-string into the hash inputs.
73
- return fingerprintHash([
74
- JSON.stringify(transferDocument.root ?? null),
75
- FINGERPRINT_SEPARATOR,
76
- JSON.stringify(transferDocument.diagnostics)
77
- ]);
71
+ /**
72
+ * Hash an encoded model's root + diagnostics — the `'transfer-document'` strategy.
73
+ * Absent diagnostics must hash apart from `[]`: a client skipping a render on an
74
+ * equal hash would otherwise miss a validation that found nothing.
75
+ */
76
+ function transferDocumentFingerprint(model) {
77
+ return textHash([JSON.stringify(model.root), FINGERPRINT_SEPARATOR, JSON.stringify(model.diagnostics ?? null)]);
78
78
  }
79
79
  /**
80
80
  * `logAfterMs` threshold passed to {@link Tracer.time} around
@@ -105,15 +105,19 @@ const FINGERPRINT_LOG_AFTER_MS = 5;
105
105
  * adopters subclass ONLY when they need to decorate wire returns or
106
106
  * notifications. The usual adoption path is DI rebinds alone.
107
107
  *
108
+ * **`TTransfer` is not checked.** It declares the union of the transfer roots
109
+ * the workspace's languages produce. Which root a document has is known only
110
+ * at runtime, from its language, so a `TTransfer` missing a language compiles.
111
+ *
108
112
  * Lifecycle: the constructor registers framework request handlers (plus
109
113
  * any names supplied via {@link DataServerOptions.additionalMethods}) and
110
114
  * builds the {@link DataClientProtocol} notification proxy on the same
111
115
  * connection in one {@link createRpcProxy} call (binding `this` as its
112
- * `localTarget`) under the configured namespace, and subscribes two listeners
113
- * at the configured phase: a `DocumentBuilder.onDocumentPhase` one dispatching
114
- * per-document `clientProxy.onDocumentUpdated` for watched URIs, and a
115
- * `DocumentBuilder.onBuildPhase` one dispatching a single
116
- * `clientProxy.onDocumentsBuilt` naming the batch's unwatched documents.
116
+ * `localTarget`) under the configured namespace, and follows documents
117
+ * through `ModelService`'s events: at the configured phase, `onModelUpdated`
118
+ * dispatches per-document `clientProxy.onDocumentUpdated` for watched URIs and
119
+ * `onModelsBuilt` a single `clientProxy.onDocumentsBuilt` naming the batch's
120
+ * unwatched documents.
117
121
  *
118
122
  * The data-server requires the full {@link ServerSharedServices}
119
123
  * shape — `HydraniumTextDocuments` for client-attributed updates,
@@ -138,87 +142,40 @@ export class DataServer {
138
142
  * extending rather than restate a value the framework may change.
139
143
  */
140
144
  static DEFAULT_OPTIONS = {
141
- subscriptionPhase: DocumentState.Validated
145
+ subscriptionPhase: DocumentState.Validated,
146
+ referenceSettleTimeoutMs: 60_000
142
147
  };
143
148
  options;
144
- /** Subscription bookkeeping: URI → set of clientIds that subscribed for that URI. */
145
- subscriptions = new Map();
149
+ /** This connection's {@link DataServerUriWatchRecord} per canonical URI; see there for when a record lives. */
150
+ uriWatchRecords = new Map();
146
151
  /**
147
- * Open-document bookkeeping: URI → set of clientIds that opened it over THIS
148
- * connection.
149
- *
150
- * Kept because the document store releases a client's hold only from an
151
- * explicit close, and a client that dies without closing would otherwise keep
152
- * the document open forever — resident, and with its last-close revert
153
- * suppressed. The store has no notion of which connection a clientId reached
154
- * it over, so the connection-scoped set has to live here; teaching it a client
155
- * identity is the more robust and much larger alternative.
152
+ * Per client id, the key each URI it watched resolved to when it watched.
153
+ * An unwatch looks the key up here: once a file is gone, a symlink to it
154
+ * no longer resolves to the key the watch is under.
156
155
  */
157
- openedDocuments = new Map();
158
- /** Typed client proxy — sends `data-server/on*` notifications back over the same wire. */
159
- clientProxy;
160
- disposables = new DisposableCollection();
156
+ watchKeys = new Map();
161
157
  /**
162
- * Snapshot of the most recent `DocumentBuilder.onUpdate` event. Drives
163
- * `reason` discrimination on outbound `onDocumentUpdated` notifications:
164
- * a URI in the `changed` list emits `'changed'`, otherwise `'rebuilt'`
165
- * (cascade rebuild from a dependent URI's change). The same mechanism
166
- * `AstDocumentManager.onUpdate` uses on the LSP side, so reason fidelity
167
- * stays consistent between heads. The `deleted` half is read by
168
- * {@link dispatchDeleteEvents} rather than by the reason discrimination,
169
- * a deletion travelling on its own channel.
158
+ * The position of the last update or deletion event sent, which each
159
+ * such event carries, and a watch's answer carries as of the moment it
160
+ * took whether its document exists: an event past it was sent after.
170
161
  */
171
- lastBuildUpdate;
162
+ eventSequence = 0;
172
163
  /**
173
- * Per-URI hash of the last emitted (text + diagnostics) state,
174
- * used by {@link dispatchPhaseEvent} to suppress duplicate
175
- * `onDocumentUpdated` notifications for rebuilds that produce no
176
- * observable change since the last emit.
177
- *
178
- * Two scenarios routinely produce such rebuilds:
179
- * 1. **Refresh-triggered rebuilds.** When a second client attaches to a
180
- * document already open in another client,
181
- * `HydraniumTextDocuments.refreshContent` fires `onDidChangeContent`
182
- * purely to re-trigger the build pipeline — the text and diagnostics
183
- * are identical to the prior emit.
184
- * 2. **Cascade rebuilds with no diagnostic delta.** Langium rebuilds a
185
- * document when a dependency changes; if the rebuild produces the
186
- * same diagnostics, there is nothing new to communicate to subscribers.
187
- *
188
- * Without this filter, both scenarios reach subscribers as wire-side
189
- * `'changed'` events. A subscriber that interprets `'changed'` as
190
- * "another client wrote new content" and resets its in-memory root to
191
- * the server view then misreads the spurious event as a concurrent
192
- * third-party write and loses the user's edits.
193
- *
194
- * Initialised on {@link watchModelDocument} to the current
195
- * document fingerprint so the FIRST phase event after a fresh
196
- * subscription is also de-duplicated against the state subscribers
197
- * obtained via `getModelDocument` — they do not need a redundant phase
198
- * notification for the state they just fetched.
199
- *
200
- * Cleared in {@link unwatchModelDocument} when the last
201
- * subscriber for a URI leaves so memory does not accumulate.
202
- *
203
- * Stored as a cyrb53 hex digest (16 chars per URI) rather than the raw
204
- * text + diagnostics serialisation so the memory cost is constant in
205
- * document size. See {@link computeDocumentFingerprint} for the inputs.
164
+ * The live client sessions this connection registered, by client id. A
165
+ * document request carrying one of these ids acts as that session; one
166
+ * carrying any other id fails. A session leaves once it ends, so a
167
+ * connection that registers many over its life does not accumulate them.
206
168
  */
207
- lastEmittedFingerprint = new Map();
169
+ clientSessions = new Map();
208
170
  /**
209
- * URIs whose LAST client just closed. The update handler rebuilds such a
210
- * document from its disk content (discarding unsaved in-session edits),
211
- * but {@link dispatchPhaseEvent} gates on the subscription map — and the
212
- * last close typically also removed the last watcher, so consumers that
213
- * only ever fetch via `getModelDocument` would keep showing the
214
- * discarded state forever. Marked on the last-close transition (see
215
- * {@link subscribeToTextDocumentCloses}) and consumed by
216
- * {@link dispatchPhaseEvent}, which broadcasts the following rebuild's
217
- * phase event even without a subscription — de-duplicated against
218
- * {@link lastEmittedFingerprint} where an entry survives, so a close
219
- * whose disk state equals the last emitted state stays silent.
171
+ * Typed client proxy — sends `data-server/on*` notifications back over the same wire.
172
+ * The proxy sends any `on[A-Z]*` name other than `onDidOpenConnection` and
173
+ * `onDidCloseConnection`, for which it returns its own events, so a subclass
174
+ * with notifications of its own narrows only the type:
175
+ * `declare protected readonly clientProxy: TheirClient`.
220
176
  */
221
- pendingRevertBroadcasts = new Set();
177
+ clientProxy;
178
+ disposables = new DisposableCollection();
222
179
  tracer;
223
180
  /** The in-flight interactive profile capture, held between {@link startProfiling} and {@link stopProfiling}. */
224
181
  activeProfile;
@@ -230,11 +187,8 @@ export class DataServer {
230
187
  encoder;
231
188
  /**
232
189
  * In-process workspace facade — the lifecycle delegate `get` / `update` / `save` go through.
233
- * The facade's AstDocument diagnostic shape is intentionally typed `unknown` here: adopters
234
- * carry LSP-shape diagnostics in their AstDocument (e.g. an adopter's LSP-shape diagnostic type)
235
- * while the wire shape stays `TDiagnostic extends TransferDiagnostic`. The encoder's
236
- * `astDocumentToTransferDocument` accepts both shapes (wire-shape or LSP-shape) and projects
237
- * to wire shape on the return — see `TransferEncoder.astDocumentToTransferDocument`.
190
+ * Its documents carry the AST-layer {@link AstDiagnostic}, never the wire `TDiagnostic`:
191
+ * {@link encoder} converts one into the other on the way out.
238
192
  */
239
193
  modelService;
240
194
  constructor(connection, services, options = {}) {
@@ -243,8 +197,7 @@ export class DataServer {
243
197
  this.options = this.resolveOptions(options);
244
198
  this.tracer = this.services.Tracer.for(options.logName ?? 'DataServer').trace('instantiated');
245
199
  // DI-bound: adopters rebind `services.model.TransferEncoder` /
246
- // `services.model.ModelService` with their own subclasses. Slot types use the
247
- // framework upper bounds; the casts below narrow to this instance's generics.
200
+ // `services.model.ModelService` with their own subclasses.
248
201
  const { TransferEncoder: encoder, ModelService: modelService } = this.services.model;
249
202
  if (!encoder || !modelService) {
250
203
  throw new Error('DataServer requires `services.model.TransferEncoder` and `services.model.ModelService` to be bound. ' +
@@ -273,34 +226,36 @@ export class DataServer {
273
226
  // framework's own rejections.
274
227
  renderErrorMessage: error => this.services.MessageRenderer.renderError(error)
275
228
  });
276
- this.disposables.push(this.services.workspace.DocumentBuilder.onUpdate((changed, deleted) => {
277
- this.lastBuildUpdate = { changed, deleted };
278
- this.dispatchDeleteEvents(deleted);
279
- }));
280
- this.subscribeToDocumentBuilder();
229
+ this.disposables.push(this.modelService.onModelDeleted(event => this.dispatchDeleteEvent(event)));
230
+ this.subscribeToModelUpdates();
281
231
  this.subscribeToTextDocumentSaves();
282
- this.subscribeToTextDocumentCloses();
232
+ this.subscribeToDirtyChanges();
233
+ this.subscribeToModelReleases();
283
234
  this.subscribeToProjectManager();
284
235
  // Self-register teardown so an adopter that keeps no reference to the
285
236
  // server still releases per-connection state, with no lifecycle hook of
286
- // its own. See `dispose`.
287
- this.disposables.push(connection.onClose(() => this.dispose()));
237
+ // its own. The client did not close anything itself, so its sessions end
238
+ // as lost and each document it was the last to have open waits out the
239
+ // store's release grace. See `dispose`.
240
+ this.disposables.push(connection.onClose(() => this.dispose('lost')));
288
241
  }
289
242
  /**
290
243
  * Release everything the constructor wired up — the bound protocol
291
- * handlers and every listener — and clear the subscription map and the
292
- * per-URI emission-fingerprint cache, so a long-lived shared services
293
- * bundle does not retain per-connection memory after the connection
294
- * closes. Also closes every document still open over this connection (see
295
- * {@link closeOpenDocuments}), which is the SHARED store's state rather than
296
- * this server's and so outlives the connection unless released here.
244
+ * handlers and every listener — and clear {@link uriWatchRecords}, so a
245
+ * long-lived shared services bundle does not retain per-connection memory
246
+ * after the connection closes. Also ends every session this connection registered, which closes
247
+ * every document it has open: the SHARED store's state rather than this
248
+ * server's, which outlives the connection unless released here.
297
249
  *
298
- * Idempotent: subsequent calls are no-ops. Self-fires on
299
- * `connection.onClose` so adopters who don't hold a reference still
300
- * get per-connection cleanup; adopters that DO hold a reference may
301
- * call `dispose()` directly for early teardown.
250
+ * Idempotent: subsequent calls are no-ops. Runs by itself when the
251
+ * connection closes, so adopters who don't hold a reference still get
252
+ * per-connection cleanup; adopters that DO hold a reference may call
253
+ * `dispose()` directly for early teardown. The sessions end with `cause`:
254
+ * `'closed'` releases every document whose last open they close at once,
255
+ * and `'lost'`, the connection's own close, lets each wait out the release
256
+ * grace.
302
257
  */
303
- dispose() {
258
+ dispose(cause = 'closed') {
304
259
  this.disposed = true;
305
260
  // Release an in-flight interactive capture so the process-wide inspector
306
261
  // singleton is not left active after the connection closes.
@@ -309,97 +264,162 @@ export class DataServer {
309
264
  this.activeProfile = undefined;
310
265
  void capture.stop({}).catch(() => undefined);
311
266
  }
312
- // AFTER `disposables.dispose()`, deliberately: closing a document fires
313
- // `onDidClose`, and this server's own close listener would otherwise mark a
314
- // revert broadcast for a connection that is already gone.
267
+ // AFTER `disposables.dispose()`, deliberately: ending a session releases
268
+ // each document it was the last to hold, and this server's own
269
+ // `onModelReleased` subscription would otherwise mark a release broadcast
270
+ // for a connection that is already gone.
315
271
  this.disposables.dispose();
316
- this.closeOpenDocuments();
317
- this.subscriptions.clear();
318
- this.lastEmittedFingerprint.clear();
319
- this.pendingRevertBroadcasts.clear();
272
+ for (const [clientId, session] of this.clientSessions) {
273
+ this.endSession(clientId, session, cause);
274
+ }
275
+ this.uriWatchRecords.clear();
276
+ this.watchKeys.clear();
320
277
  }
321
278
  // ============================================================
322
- // DataServerProtocol implementation — delegates to ModelService.
279
+ // DataServerProtocol implementation
323
280
  // ============================================================
324
281
  //
325
- // The lifecycle methods (`get` / `update` / `save` / `ready`) forward
326
- // straight to `ModelService` and encode the returned `AstDocument` on
327
- // the wire boundary via `encoder.astDocumentToTransferDocument`.
328
- // Adopters customising lifecycle behaviour (normalisation, supersession,
329
- // settled-phase choice, etc.) override on the ModelService subclass —
330
- // no override on DataServer is needed.
331
- async openModelDocument(args) {
332
- // Register the editor session (idempotent — `ModelService.open` refreshes
333
- // an already-open document rather than re-opening), then return the built
334
- // state at the configured target phase. The one-shot snapshot; subsequent
335
- // build-phase events arrive via `watchModelDocument`.
336
- await this.modelService.open(args);
337
- // Record the hold so `dispose` can release it for a client that never
338
- // closes. Keyed by the URI as given, because that is what `close` takes.
339
- let holders = this.openedDocuments.get(args.uri);
340
- if (!holders) {
341
- holders = new Set();
342
- this.openedDocuments.set(args.uri, holders);
282
+ // Reads go to `ModelService`; a request that opens or writes a document
283
+ // goes to the session this connection registered under its `clientId`.
284
+ // Each answer is encoded on the wire boundary via
285
+ // `encoder.astDocumentToTransferDocument`. To change how documents are
286
+ // read, override on the ModelService subclass; to change how they are
287
+ // written, on the session class the bound `ClientSessionFactory` builds.
288
+ async createSession(args) {
289
+ // After teardown nothing would ever end a session registered now, and its
290
+ // id would stay taken for the life of the process.
291
+ if (this.disposed) {
292
+ throw new SessionClosedError(args.clientId);
343
293
  }
344
- holders.add(args.clientId);
345
- const document = await this.getModelDocument({ uri: args.uri });
346
- // Project the authoritative client-facing version onto the open snapshot.
347
- // `getModelDocument` encodes the freshly-built AST snapshot, whose version
348
- // is the `LangiumDocument`'s own `textDocument.version`. That lags the
349
- // multi-client synced version whenever the open seeded the synced document
350
- // with a caller-supplied version id — and the synced version is what
351
- // `ModelService.update`'s optimistic-concurrency gate compares
352
- // `baseVersion` against, so reporting the snapshot's would make the
353
- // caller's first tagged write self-conflict. A genuine concurrent edit
354
- // still trips the gate and is reconciled by the caller's replay rather
355
- // than predicted here.
356
- return { ...document, version: this.services.workspace.TextDocuments.version(args.uri) };
294
+ const session = this.modelService.createSession(args.label, args.clientId, { resumeToken: args.resumeToken });
295
+ this.clientSessions.set(args.clientId, session);
296
+ // A session also ends without closeSession, as when a client registers
297
+ // again on another connection, and its watches and entry here would
298
+ // otherwise outlive it.
299
+ session.onDidDispose(cause => {
300
+ // A session class may report its end late, once a replacement holds
301
+ // the id here; the replacement's entry and watches stay.
302
+ if (this.clientSessions.get(args.clientId) !== session) {
303
+ return;
304
+ }
305
+ this.dropWatches(args.clientId);
306
+ this.clientSessions.delete(args.clientId);
307
+ this.tracer.debug(`Forget session ${args.clientId}: it ended as ${cause}`);
308
+ });
309
+ return { epoch: this.epoch };
357
310
  }
358
- async closeModelDocument(args) {
359
- // Closing a session also releases its watch for (uri, clientId) — a
360
- // forgotten unwatch would otherwise leak phase-event dispatch until the
361
- // connection closes. Idempotent: a close without a prior watch is a no-op.
362
- await this.unwatchModelDocument({ uri: args.uri, clientId: args.clientId });
363
- this.forgetOpenDocument(args.uri, args.clientId);
364
- await this.modelService.close(args);
311
+ /**
312
+ * The epoch this server numbers versions in, which every registration and
313
+ * document names: the text store's, so every connection to the server
314
+ * answers the same one.
315
+ */
316
+ get epoch() {
317
+ return this.services.workspace.TextDocuments.epoch;
365
318
  }
366
319
  /**
367
- * Drop the recorded hold for `(uri, clientId)` so {@link dispose} does not
368
- * close it a second time. Idempotent.
320
+ * Refuse a write whose base version is numbered in another epoch, as a
321
+ * restarted server's predecessor numbered it. Versions start afresh with
322
+ * each epoch, so the session's gate would take the version for text the
323
+ * writer never saw. Checked once the session is found, before the open
324
+ * check and every other check of its documents.
369
325
  */
370
- forgetOpenDocument(uri, clientId) {
371
- const holders = this.openedDocuments.get(uri);
372
- if (!holders) {
373
- return;
326
+ assertEpoch(uri, gate, message = STALE_BASE_VERSION_UPDATE) {
327
+ if (isModelVersion(gate.baseVersion) && gate.baseEpoch !== this.epoch) {
328
+ this.tracer.withUri(uri).debug(`Conflict: base v${gate.baseVersion} is numbered in epoch ${gate.baseEpoch}, not ${this.epoch}`);
329
+ throw new ConflictError(uri, gate.baseVersion, this.services.workspace.TextDocuments.version(uri), message);
374
330
  }
375
- holders.delete(clientId);
376
- if (holders.size === 0) {
377
- this.openedDocuments.delete(uri);
331
+ }
332
+ /** End a session this connection registered. */
333
+ async closeSession(args) {
334
+ const session = this.clientSessions.get(args.clientId);
335
+ if (session) {
336
+ this.endSession(args.clientId, session);
378
337
  }
379
338
  }
339
+ /** End `session`, registered here under `clientId`, with `cause`, and drop its watches on this connection. */
340
+ endSession(clientId, session, cause = 'closed') {
341
+ this.dropWatches(clientId);
342
+ session.dispose(cause);
343
+ }
344
+ dropWatches(clientId) {
345
+ this.watchKeys.delete(clientId);
346
+ for (const [uri, record] of this.uriWatchRecords) {
347
+ if (record.watchers.delete(clientId)) {
348
+ this.pruneUriWatchRecord(uri);
349
+ }
350
+ }
351
+ }
352
+ async createModelDocument(args) {
353
+ const session = this.requireSession(args.clientId);
354
+ const version = await session.create(args.uri, args.text);
355
+ const snapshot = await this.openedSnapshot(args.uri, () => session.close(args.uri));
356
+ return { ...snapshot, created: { version: asModelVersion(version) } };
357
+ }
358
+ async updateModelDocuments(args) {
359
+ const session = this.requireSession(args.clientId);
360
+ // Every one before any applies, so a set stays all or none.
361
+ args.updates.forEach(update => this.assertEpoch(update.uri, update));
362
+ const astDocuments = await session.updateAll({
363
+ updates: args.updates.map(update => this.toSessionWrite(update)),
364
+ write: args.write
365
+ });
366
+ // A page that reads unsaved work from its editor sees the writes only once their text reaches it.
367
+ await Promise.all(args.updates.map(update => this.modelService.settleLanguageClient(update.uri)));
368
+ return astDocuments.map(astDocument => ({ ...this.encodeDocument(astDocument), written: astDocument.written }));
369
+ }
380
370
  /**
381
- * Close every document still open over this connection, for the clients that
382
- * opened it here.
383
- *
384
- * The document store releases a per-URI hold only from an explicit close, so
385
- * without this a client that dies mid-session keeps its documents open for the
386
- * lifetime of the process: `isOpenInAnyClient` stays true, the document stays
387
- * resident, and the last-close revert never runs. A long-lived multi-client
388
- * head is the configuration where a dead client is normal rather than
389
- * exceptional, so the leak accumulates there.
390
- *
391
- * Runs from {@link dispose}, which is synchronous, so each close is fired and
392
- * its failure swallowed — a teardown must not reject, and a URI whose close
393
- * fails is no worse off than it was before this drain existed.
371
+ * The session write a wire write request carries. Only the fields a session
372
+ * takes: a field an adopter adds to a request reaches the session only
373
+ * through an override of this.
394
374
  */
395
- closeOpenDocuments() {
396
- const held = [...this.openedDocuments];
397
- this.openedDocuments.clear();
398
- for (const [uri, clientIds] of held) {
399
- for (const clientId of clientIds) {
400
- void Promise.resolve(this.modelService.close({ uri, clientId })).catch(() => undefined);
401
- }
375
+ toSessionWrite(request) {
376
+ return { uri: request.uri, model: request.model, baseVersion: request.baseVersion, restore: request.restore, write: request.write };
377
+ }
378
+ /**
379
+ * The session this connection registered under `clientId`, which every
380
+ * document request acts as. An id that is not one fails with the
381
+ * closed-session code: opening or writing under it would leave an open that
382
+ * no session end ever closes.
383
+ */
384
+ requireSession(clientId) {
385
+ const session = this.clientSessions.get(clientId);
386
+ if (!session) {
387
+ throw new SessionClosedError(clientId, 'The client session is not registered on this connection.');
402
388
  }
389
+ return session;
390
+ }
391
+ async openModelDocument(args) {
392
+ const session = this.requireSession(args.clientId);
393
+ const wasOpen = this.services.workspace.TextDocuments.isOpenInClient(args.uri, args.clientId);
394
+ await session.open(args.uri, args.options);
395
+ // A repeat open keeps the earlier one, which is still in use.
396
+ return this.openedSnapshot(args.uri, wasOpen ? undefined : () => session.close(args.uri));
397
+ }
398
+ /**
399
+ * The snapshot a session's open or create answers with. When the read
400
+ * fails, `rollback` undoes the open this call made before the failure is
401
+ * rethrown: the caller sees no open, so nothing on its side would ever close
402
+ * it.
403
+ */
404
+ async openedSnapshot(uri, rollback) {
405
+ try {
406
+ return await this.getModelDocument({ uri });
407
+ }
408
+ catch (error) {
409
+ // The open's failure is the one the caller needs, not the cleanup's.
410
+ await rollback?.().catch(() => undefined);
411
+ throw error;
412
+ }
413
+ }
414
+ async closeModelDocument(args) {
415
+ // Through the handle, so a close under an ended session fails as that
416
+ // session's calls do, and a session class's own close is honoured.
417
+ const session = this.requireSession(args.clientId);
418
+ // Closing also releases the watch for (uri, clientId) — a forgotten
419
+ // unwatch would otherwise leak phase-event dispatch until the connection
420
+ // closes. Idempotent: a close without a prior watch is a no-op.
421
+ await this.unwatchModelDocument({ uri: args.uri, clientId: args.clientId });
422
+ await session.close(args.uri);
403
423
  }
404
424
  async getModelDocument(args) {
405
425
  // Smart dispatch: a warm document (already in LangiumDocuments) is
@@ -413,72 +433,149 @@ export class DataServer {
413
433
  // unsubscribed caller that needs diagnostics inline passes
414
434
  // `includeDiagnostics: true` to settle at `Validated` instead.
415
435
  const state = args.includeDiagnostics ? DocumentState.Validated : undefined;
416
- const astDocument = await this.modelService.ensureDocumentState(args.uri, state);
417
- return this.encoder.astDocumentToTransferDocument(astDocument);
436
+ try {
437
+ return this.encodeDocument(await this.modelService.ensureDocumentState(args.uri, state));
438
+ }
439
+ catch (error) {
440
+ // The protocol answers a read of a URI with no document with an
441
+ // envelope that has no model.
442
+ if (this.isMissingDocument(error, args.uri)) {
443
+ return this.envelope(UriUtils.toUri(args.uri));
444
+ }
445
+ throw error;
446
+ }
418
447
  }
419
448
  async updateModelDocument(args) {
420
- const astDocument = await this.modelService.update(args);
421
- return this.encoder.astDocumentToTransferDocument(astDocument);
449
+ const session = this.requireSession(args.clientId);
450
+ this.assertEpoch(args.uri, args);
451
+ const astDocument = await session.update(this.toSessionWrite(args));
452
+ // A page that reads unsaved work from its editor sees the write only once the text reaches it.
453
+ await this.modelService.settleLanguageClient(args.uri);
454
+ return { ...this.encodeDocument(astDocument), written: astDocument.written };
422
455
  }
423
456
  async saveModelDocument(args) {
424
- const astDocument = await this.modelService.save(args);
425
- return this.encoder.astDocumentToTransferDocument(astDocument);
457
+ const session = this.requireSession(args.clientId);
458
+ this.assertEpoch(args.uri, args, STALE_BASE_VERSION_SAVE);
459
+ const astDocument = await session.save(this.toSessionWrite(args));
460
+ await this.modelService.settleLanguageClient(args.uri);
461
+ return { ...this.encodeDocument(astDocument), persisted: astDocument.persisted };
462
+ }
463
+ async persistModelDocument(args) {
464
+ const session = this.requireSession(args.clientId);
465
+ this.assertEpoch(args.uri, args, STALE_BASE_VERSION_SAVE);
466
+ const version = await session.persist({ uri: args.uri, baseVersion: args.baseVersion });
467
+ await this.modelService.settleLanguageClient(args.uri);
468
+ const astDocument = await this.modelService.ensureDocumentState(args.uri, this.services.workspace.DocumentBuilder.finalBuildState());
469
+ return { ...this.encodeDocument(astDocument), persisted: { version } };
470
+ }
471
+ /**
472
+ * Delete the document through the session, then release the session's
473
+ * watch of it, as {@link closeModelDocument} does. Released only after the
474
+ * delete: one that fails leaves the session its open and its watch. Other
475
+ * sessions' watches survive, as for any deletion.
476
+ */
477
+ async deleteModelDocument(args) {
478
+ const session = this.requireSession(args.clientId);
479
+ this.assertEpoch(args.uri, args);
480
+ // Keyed before the delete: once the file is gone, a symlink to it no
481
+ // longer resolves to the key the watch is under.
482
+ const uri = this.canonicalKey(args.uri);
483
+ await session.delete({ uri: args.uri, baseVersion: args.baseVersion });
484
+ await this.unwatchModelDocument({ uri, clientId: args.clientId });
426
485
  }
427
486
  /**
428
- * Record a watch for `(uri, clientId)`. Subsequent phase events on `uri`
429
- * fan out to the wired `clientProxy.onDocumentUpdated`. The
430
- * bidirectional pattern means the event channel is the client
431
- * notification surface, not a returned handle — this method only
432
- * registers the URI in the dispatch table.
487
+ * Record a watch for `(uri, clientId)` in {@link uriWatchRecords}.
488
+ * Subsequent phase events on `uri` fan out to the wired
489
+ * `clientProxy.onDocumentUpdated`. The bidirectional pattern means the
490
+ * event channel is the client notification surface, not a returned
491
+ * handle.
492
+ *
493
+ * Also baselines the URI's emission fingerprint (see
494
+ * {@link dispatchPhaseEvent}) to the document's current state when the
495
+ * first watcher for the URI registers. This guarantees that any phase event firing immediately after
496
+ * the watch with no observable change is suppressed — watchers obtain
497
+ * initial state via `getModelDocument` (or {@link openModelDocument}) and
498
+ * do not need a redundant phase notification for that same state.
433
499
  *
434
- * Also baselines the per-URI emission fingerprint (see
435
- * {@link lastEmittedFingerprint}) to the document's current state when
436
- * the first watcher for the URI registers. This guarantees that any
437
- * phase event firing immediately after the watch with no observable
438
- * change is suppressed — watchers obtain initial state via
439
- * `getModelDocument` (or {@link openModelDocument}) and do not need a
440
- * redundant phase notification for that same state.
500
+ * Answers once the document is loaded as a read loads it, with the key
501
+ * events name it by and whether a document exists, taken after the watch
502
+ * is in place and the load, so an event sent while it loads is older.
441
503
  */
442
504
  async watchModelDocument(args) {
443
505
  const uri = this.canonicalKey(args.uri);
444
- let subscribers = this.subscriptions.get(uri);
445
- if (!subscribers) {
446
- subscribers = new Set();
447
- this.subscriptions.set(uri, subscribers);
448
- }
449
- subscribers.add(args.clientId);
450
- if (!this.lastEmittedFingerprint.has(uri)) {
451
- const document = this.services.workspace.LangiumDocuments.getDocument(UriUtils.toUri(uri));
452
- if (document) {
453
- this.lastEmittedFingerprint.set(uri, this.computeDocumentFingerprint(document));
506
+ const keys = this.watchKeys.get(args.clientId) ?? new Map();
507
+ this.watchKeys.set(args.clientId, keys.set(args.uri, uri));
508
+ const record = this.uriWatchRecords.get(uri) ?? { watchers: new Set() };
509
+ this.uriWatchRecords.set(uri, record);
510
+ record.watchers.add(args.clientId);
511
+ if (record.fingerprint === undefined) {
512
+ const document = this.modelService.getDocument(args.uri);
513
+ if (document && !this.services.workspace.ModelLedger.isPlaceholder(document.parseResult.value)) {
514
+ record.fingerprint = this.computeDocumentFingerprint(document.parseResult.value, this.encoder.toTransferDocument(document));
515
+ record.fingerprintVersion = this.services.workspace.ModelLedger.versionOf(document.parseResult.value);
454
516
  }
455
517
  }
518
+ // As for a read, only a missing document counts as none. A file briefly
519
+ // unreadable leaves the builder's placeholder, and is there all the same.
520
+ // Whether it exists, the text held and the position of the last event
521
+ // sent are read in one synchronous run, so an event sent after is past
522
+ // the position and moves the text past the state answered.
523
+ const answer = (exists) => {
524
+ const text = exists ? this.serverTextState(args.uri) : undefined;
525
+ return { uri, epoch: this.epoch, exists, sequence: this.eventSequence, ...(text ? { text } : {}) };
526
+ };
527
+ return this.modelService.ensureDocumentState(args.uri).then(() => answer(this.modelService.getDocument(args.uri) !== undefined), (error) => answer(!this.isMissingDocument(error, args.uri)));
456
528
  }
457
529
  /**
458
530
  * Remove a watch for `(uri, clientId)` previously created by
459
531
  * {@link watchModelDocument}. Idempotent — unwatching twice is a no-op.
460
- * Dispatch for `uri` stops once no watchers remain, at which point the
461
- * per-URI emission fingerprint is also cleared so the next first-watch
462
- * re-baselines against the then-current document state rather than a
463
- * stale snapshot from the previous watch.
532
+ * Dispatch for `uri` stops once no watchers remain, and the emission
533
+ * fingerprint goes with the last watcher (see {@link pruneUriWatchRecord}).
534
+ *
535
+ * A URI the client watched is unwatched under the key it resolved to then,
536
+ * from {@link watchKeys}, which still holds once its file is gone.
464
537
  */
465
538
  async unwatchModelDocument(args) {
466
- const uri = this.canonicalKey(args.uri);
467
- const subscribers = this.subscriptions.get(uri);
468
- if (!subscribers) {
539
+ const keys = this.watchKeys.get(args.clientId);
540
+ const uri = keys?.get(args.uri) ?? this.canonicalKey(args.uri);
541
+ // Every spelling the client watched it under was this one watch.
542
+ keys?.forEach((key, spelling) => {
543
+ if (key === uri) {
544
+ keys.delete(spelling);
545
+ }
546
+ });
547
+ if (keys?.size === 0) {
548
+ this.watchKeys.delete(args.clientId);
549
+ }
550
+ if (this.uriWatchRecords.get(uri)?.watchers.delete(args.clientId)) {
551
+ this.pruneUriWatchRecord(uri);
552
+ }
553
+ }
554
+ /**
555
+ * Drop what `uri` no longer needs: the
556
+ * {@link DataServerUriWatchRecord.fingerprint} once no client watches it,
557
+ * and the whole record once it holds no
558
+ * {@link DataServerUriWatchRecord.releaseBroadcastPending} mark either.
559
+ * Every path that removes a watcher or consumes a mark ends here.
560
+ */
561
+ pruneUriWatchRecord(uri) {
562
+ const record = this.uriWatchRecords.get(uri);
563
+ if (!record || record.watchers.size > 0) {
469
564
  return;
470
565
  }
471
- subscribers.delete(args.clientId);
472
- if (subscribers.size === 0) {
473
- this.subscriptions.delete(uri);
474
- this.lastEmittedFingerprint.delete(uri);
566
+ if (record.releaseBroadcastPending) {
567
+ record.fingerprint = undefined;
568
+ record.fingerprintVersion = undefined;
569
+ }
570
+ else {
571
+ this.uriWatchRecords.delete(uri);
475
572
  }
476
573
  }
477
574
  /**
478
- * Canonicalise a URI string for use as a subscription / fingerprint
479
- * map key, via the shared `DocumentUriPolicy`. Callers may send
480
- * non-canonical URIs (drive-letter casing, percent-encoding differences,
481
- * or a symlink path) over the wire; the dispatch side keys by
575
+ * Canonicalise a URI string for use as a {@link uriWatchRecords} key, via the
576
+ * shared `DocumentUriPolicy`. Callers may send non-canonical URIs
577
+ * (drive-letter casing, percent-encoding differences, or a symlink path)
578
+ * over the wire; the dispatch side keys by
482
579
  * `document.uri.toString()` from `LangiumDocuments`, so writer keys must
483
580
  * canonicalise to the same form or events silently fail to deliver.
484
581
  * Routing through the seam (rather than a bare `UriUtils.normalize`)
@@ -488,24 +585,22 @@ export class DataServer {
488
585
  * LSP head resolves.
489
586
  *
490
587
  * Adopters can still override for head-specific URI policy by subclassing.
588
+ * The result keys watches only; a document is looked up by its own URI.
491
589
  */
492
590
  canonicalKey(uri) {
493
591
  return this.services.workspace.DocumentUriPolicy.canonicalUri(uri);
494
592
  }
495
593
  async getProjects() {
496
- // Pass-through: `ProjectManager<TProject>` already produces `TProject`
497
- // (the shared services are parameterised over the same generic).
498
594
  // The framework reads `id` for registry identity and `dependencies`
499
595
  // for the visibility closure; every other field rides on the
500
596
  // JSON-RPC envelope as adopter-defined wire metadata.
501
- return this.services.workspace.ProjectManager.getProjects();
597
+ return this.services.workspace.ProjectManager.getProjects().map(project => this.encodeProject(project));
502
598
  }
503
599
  async getProjectForUri(args) {
504
- // Pass-through: `ProjectManager.getProject(uri)` already returns the
505
- // adopter's `TProject` shape. Membership logic belongs to the adopter's
506
- // `ProjectManager`; the data-server forwards the URI without
507
- // interpretation.
508
- return this.services.workspace.ProjectManager.getProject(UriUtils.toUri(args.uri));
600
+ // Membership logic belongs to the adopter's `ProjectManager`; the
601
+ // data-server forwards the URI without interpretation.
602
+ const project = this.services.workspace.ProjectManager.getProject(UriUtils.toUri(args.uri));
603
+ return project && this.encodeProject(project);
509
604
  }
510
605
  /**
511
606
  * Resolve once the data-server is ready to serve requests. Delegates
@@ -522,26 +617,56 @@ export class DataServer {
522
617
  // Resolve the language's reference services from each request's source
523
618
  // (see resolveReferenceServices) and delegate.
524
619
  // ============================================================
525
- async findReferenceCandidates(ctx) {
526
- // Candidates are derived from resolved cross-references, so the relevant document(s) must
527
- // have finished the Linked phase — otherwise a query issued right after a model update races
528
- // the asynchronous rebuild and returns stale candidates. Wait the source document
529
- // specifically, but ONLY when a document is actually loaded at that URI: a synthetic source
530
- // can address a URI with no document (a directory URI, typically), and a per-URI `waitUntil`
531
- // there throws "No document found". Fall back to a global Linked settle, matching the
532
- // id-based ElementSource (no URI) path.
533
- const uri = isDocumentSource(ctx.source) || isSyntheticSource(ctx.source) ? UriUtils.toUri(ctx.source.uri) : undefined;
620
+ /**
621
+ * Settle the build before a reference question is answered from the index.
622
+ *
623
+ * A query issued right after a model update otherwise races the rebuild and
624
+ * reads an index missing the symbols it asks about. The cost is not confined
625
+ * to this call: a scope built inside that window is cached, and the build's
626
+ * remaining documents link against it.
627
+ *
628
+ * Waits the source document specifically, but ONLY when one is loaded at
629
+ * that URI: a synthetic source can address a URI with no document (a
630
+ * directory URI, typically), where a per-URI `waitUntil` throws "No document
631
+ * found". Falls back to a global settle, matching the id-based
632
+ * `ElementSource` path.
633
+ *
634
+ * Bounded by {@link DataServerOptions.referenceSettleTimeoutMs}.
635
+ */
636
+ async awaitReferencesLinked(source) {
637
+ const uri = isDocumentSource(source) || isSyntheticSource(source) ? UriUtils.toUri(source.uri) : undefined;
534
638
  const waitUri = uri && this.services.workspace.LangiumDocuments.hasDocument(uri) ? uri : undefined;
535
- await this.services.workspace.DocumentBuilder.waitUntil(DocumentState.Linked, waitUri);
639
+ const stopwatch = this.services.Clock.stopwatch();
640
+ const linked = this.services.workspace.DocumentBuilder.waitUntil(DocumentState.Linked, waitUri);
641
+ if ((await this.services.Clock.raceTimer(linked, this.options.referenceSettleTimeoutMs)) !== TIMED_OUT) {
642
+ return;
643
+ }
644
+ const elapsedMs = Math.round(stopwatch.elapsedMs);
645
+ // A per-URI wait resolves off a document-phase notification, so a
646
+ // missed one strands a wait on a document that HAS reached the
647
+ // phase. Re-read before failing; the workspace-wide wait has no
648
+ // equivalent reading and can only reject.
649
+ const document = waitUri ? this.services.workspace.LangiumDocuments.getDocument(waitUri) : undefined;
650
+ if (waitUri && document && document.state >= DocumentState.Linked) {
651
+ this.tracer
652
+ .withUri(waitUri.toString())
653
+ .warn(`Missed the 'Linked' notification after ${elapsedMs}ms; the document already reached it`);
654
+ return;
655
+ }
656
+ throw referenceSettleTimeoutError(elapsedMs);
657
+ }
658
+ async findReferenceCandidates(ctx) {
659
+ await this.awaitReferencesLinked(ctx.source);
536
660
  return this.resolveReferenceServices(ctx.source).CandidateProvider.find(ctx);
537
661
  }
538
662
  async resolveReference(ref) {
663
+ await this.awaitReferencesLinked(ref.source);
539
664
  const resolved = this.resolveReferenceServices(ref.source).CandidateProvider.resolveCandidate(ref);
540
665
  if (!resolved) {
541
666
  return undefined;
542
667
  }
543
- const element = this.encoder.toTransfer(resolved.node);
544
- return { ...resolved.candidate, element };
668
+ // `TElement` is the caller's claim about the target's type; nothing checks it.
669
+ return { ...resolved.candidate, element: this.encoder.toTransfer(resolved.node) };
545
670
  }
546
671
  async findNextName(args) {
547
672
  const uri = UriUtils.toUri(args.uri);
@@ -550,15 +675,32 @@ export class DataServer {
550
675
  // synthetic source — and the create-element flow driving this method is
551
676
  // the one that produces directory URIs, on which a bare
552
677
  // `getServices(uri)` throws while `findReferenceCandidates` succeeds.
553
- const nameProvider = this.resolveReferenceServices(ReferenceSource.synthetic(args.uri, args.type)).NameProvider;
678
+ const source = ReferenceSource.synthetic(args.uri, args.type, args.language);
679
+ // Mid-rebuild the names of documents not yet re-indexed are absent from
680
+ // the taken set, and the caller PERSISTS what it is handed. Nothing
681
+ // reserves the answer either, so a name is free of the collisions the
682
+ // index knows about and of nothing else — two callers racing get the same
683
+ // proposal, which only uniqueness at the write can catch.
684
+ await this.awaitReferencesLinked(source);
685
+ const nameProvider = this.resolveReferenceServices(source).NameProvider;
554
686
  const tier = args.tier ?? 'project';
555
687
  if (tier === 'public') {
556
688
  return nameProvider.findNextProjectQualifiedName(args.type, args.proposal);
557
689
  }
558
690
  if (tier === 'local') {
559
- // Document-scoped uniqueness: the document root is the container.
560
- const document = await this.modelService.ensureDocumentState(args.uri);
561
- return document.root ? nameProvider.findNextName(args.type, args.proposal, document.root) : args.proposal;
691
+ // Document-scoped uniqueness: the document root is the container. A
692
+ // URI with no document has nothing to collide with, like a document
693
+ // with no root.
694
+ try {
695
+ const document = await this.modelService.ensureDocumentState(args.uri);
696
+ return document.root ? nameProvider.findNextName(args.type, args.proposal, document.root) : args.proposal;
697
+ }
698
+ catch (error) {
699
+ if (this.isMissingDocument(error, args.uri)) {
700
+ return args.proposal;
701
+ }
702
+ throw error;
703
+ }
562
704
  }
563
705
  const project = this.services.workspace.ProjectManager.getProject(uri);
564
706
  if (!project) {
@@ -591,66 +733,30 @@ export class DataServer {
591
733
  return language.references;
592
734
  }
593
735
  /**
594
- * Pick the language that owns a reference source, in order:
736
+ * Pick the language that owns a reference source: the registry's
737
+ * `getLanguageFor` over the source's declared language, URI and AST type,
738
+ * with an {@link isElementSource} routed by the document that holds the
739
+ * element (see {@link findElementDocumentUri}) before its type, and
740
+ * {@link fallbackReferenceLanguage}, adopter policy, last.
595
741
  *
596
- * 1. A URI-bearing source ({@link isDocumentSource}/{@link isSyntheticSource})
597
- * whose URI resolves to a registered language routes by URI.
598
- * 2. A single-language workspace always routes to that one language — so
599
- * single-language adopters never reach the steps below, and never pay
600
- * for them.
601
- * 3. An {@link isElementSource} source carries no URI, so in a
602
- * multi-language workspace it is routed via the document that holds the
603
- * element (see {@link findElementDocumentUri}).
604
- * 4. A source carrying an AST type is routed by that type when exactly one
605
- * registered grammar can produce it (see
606
- * {@link resolveReferenceLanguageByType}, over the registry's own type
607
- * index). This is what resolves a create-element flow's synthetic source
608
- * on a bare directory URI without asking the adopter.
609
- * 5. Anything still unresolved falls to {@link fallbackReferenceLanguage},
610
- * which is adopter policy.
742
+ * An unregistered declared language falls through rather than failing: the
743
+ * id crosses the wire, so a client built against a head with one more
744
+ * grammar would otherwise lose queries the other signals answer.
611
745
  */
612
746
  resolveReferenceLanguage(source) {
613
747
  const registry = this.services.ServiceRegistry;
614
- // `getServicesFor` (non-throwing, one ladder walk) gates the URI lookup:
615
- // an extensionless / unregistered URI falls through to the steps below
616
- // rather than throwing "no services for the extension ''".
617
- const uri = isDocumentSource(source) || isSyntheticSource(source) ? UriUtils.toUri(source.uri) : undefined;
618
- const byUri = uri && registry.getServicesFor(uri);
619
- if (byUri) {
620
- return byUri;
621
- }
622
- const all = registry.all;
623
- if (all.length === 1) {
624
- return all[0];
625
- }
626
748
  if (isElementSource(source)) {
627
- const documentUri = this.findElementDocumentUri(source);
628
- const byDocument = documentUri && registry.getServicesFor(documentUri);
629
- if (byDocument) {
630
- return byDocument;
631
- }
749
+ // The index lookup runs only once the cheaper signals abstained.
750
+ return (registry.getLanguageFor({}) ??
751
+ registry.getLanguageFor({ target: this.findElementDocumentUri(source), type: source.type }) ??
752
+ this.fallbackReferenceLanguage(source));
632
753
  }
633
- const byType = this.resolveReferenceLanguageByType(source);
634
- return byType ?? this.fallbackReferenceLanguage(source);
635
- }
636
- /**
637
- * Route a reference source by the AST type it carries — a
638
- * {@link isSyntheticSource}'s `type` (the transient node being created) or an
639
- * {@link isElementSource}'s optional narrowing `type`.
640
- *
641
- * Answers only when EXACTLY ONE registered grammar can produce the type.
642
- * Several can when the type comes from a grammar both import, and then the
643
- * type genuinely does not identify a language — that is a fall-through to
644
- * adopter policy, not a coin toss. Note this asks which grammar can *produce*
645
- * the type, not which mentions it: a grammar that merely cross-references a
646
- * type can never hold a node of it.
647
- */
648
- resolveReferenceLanguageByType(source) {
649
- const type = isSyntheticSource(source) ? source.type : isElementSource(source) ? source.type : undefined;
650
- // The registry owns the type index — it is the one place that knows when
651
- // the registered set changed, so unlike a private memo here it cannot go
652
- // stale on a language registered after the first lookup.
653
- return type ? this.services.ServiceRegistry.soleServicesByType(type) : undefined;
754
+ const hint = isSyntheticSource(source)
755
+ ? { languageId: source.language, target: source.uri, type: source.type }
756
+ : isDocumentSource(source)
757
+ ? { target: source.uri }
758
+ : {};
759
+ return registry.getLanguageFor(hint) ?? this.fallbackReferenceLanguage(source);
654
760
  }
655
761
  /**
656
762
  * Locate the document that holds the element an {@link ElementSource}
@@ -659,15 +765,14 @@ export class DataServer {
659
765
  * disambiguates names that repeat across types (honouring grammar
660
766
  * subtyping through `AstReflection.isSubtype`).
661
767
  *
662
- * Only reached on the multi-language, name-based path (step 3 of
663
- * {@link resolveReferenceLanguage}); single-language adopters return at
664
- * step 2.
768
+ * Only reached in a multi-language workspace, once the declared language
769
+ * and the URI have abstained.
665
770
  *
666
771
  * Abstains when the name matches elements in more than one DOCUMENT — the
667
772
  * index spans every language and is filled in build order, so "the
668
- * first match" would be file-watch order rather than an answer. Step 3
669
- * then falls through to the type-based step 4, which abstains on ties
670
- * in the same way, and finally to adopter policy.
773
+ * first match" would be file-watch order rather than an answer. Routing
774
+ * then falls through to the element's type, which abstains on ties in the
775
+ * same way, and finally to adopter policy.
671
776
  */
672
777
  findElementDocumentUri(source) {
673
778
  const matches = this.services.workspace.IndexManager.getElementsByName(source.name, source.type);
@@ -699,51 +804,121 @@ export class DataServer {
699
804
  // ============================================================
700
805
  // Internal plumbing
701
806
  // ============================================================
807
+ /**
808
+ * Whether `error`, from waiting on `uri`'s document, means that `uri` has
809
+ * no document: a URI with neither a file nor text builds nothing, and
810
+ * Langium's wait after the build rejects for want of one, with a
811
+ * `ServerCancelled` response error. Any other failure, such as a read
812
+ * that fails before a document is registered, a cancelled wait or a
813
+ * `ReentrantWriteLockError`, is the caller's to see.
814
+ */
815
+ isMissingDocument(error, uri) {
816
+ return isResponseError(error) && error.code === SERVER_CANCELLED && this.modelService.getDocument(uri) === undefined;
817
+ }
702
818
  /**
703
819
  * Build a {@link TransferDocument} envelope from the current document state,
704
820
  * delegating root + diagnostic encoding to {@link encoder} (see
705
- * `TransferEncoder.toTransferDocument` for the walk).
706
- *
707
- * The encoder field's generic-map binding is widened to
708
- * `Record<string, TransferElement>` at the framework-default level, and an
709
- * adopter supplying a typed-overlay encoder narrows the runtime shape to
710
- * its wire types. The cast on the return is where that invariant — the
711
- * adopter's `TTransfer` matches its encoder's overlay — is asserted, at a
712
- * single boundary point rather than spread across the callers.
821
+ * `TransferEncoder.toTransferDocument` for the walk). `fingerprint` is the
822
+ * caller's {@link computeDocumentFingerprint} of that same state, which
823
+ * spares hashing it twice.
713
824
  */
714
- envelope(uri) {
825
+ envelope(uri, fingerprint) {
715
826
  // Resolve through the model service's canonicalizing gateway rather than
716
827
  // reaching into `LangiumDocuments` directly, so a divergent (symlink) URI
717
828
  // still finds the document the build keys by its real path — and so the
718
829
  // data-server never has to remember to canonicalize this lookup itself.
719
830
  const document = this.modelService.getDocument(uri.toString());
720
- if (!document) {
831
+ if (!document || this.services.workspace.ModelLedger.isPlaceholder(document.parseResult.value)) {
721
832
  // No document — a shaped envelope rather than a throw, so the caller
722
- // decides policy at its own layer; `root` is optional on the envelope
833
+ // decides policy at its own layer; `model` is optional on the envelope
723
834
  // so the compiler forces that decision. Adopters preferring to throw
724
- // override `envelope`.
725
- return {
726
- uri: uri.toString(),
727
- version: 0,
728
- root: undefined,
729
- diagnostics: []
730
- };
835
+ // override `envelope`. The builder's unparsed placeholder is no
836
+ // document either: its root parses no text.
837
+ return TransferDocument.absent(uri.toString(), this.epoch);
731
838
  }
732
- return this.encoder.toTransferDocument(document);
839
+ const encoded = this.encoder.toTransferDocument(document);
840
+ return this.withServerState(encoded, fingerprint ?? this.computeDocumentFingerprint(document.parseResult.value, encoded));
733
841
  }
734
842
  /**
735
- * Subscribe one listener at the configured {@link DataServerOptions.subscriptionPhase}.
736
- * The listener dispatches subscription events for every matching URI. A
737
- * single listener (rather than one per subscription) keeps the cost flat
738
- * regardless of subscriber count.
843
+ * The transfer document a request answers with for `astDocument`, its model
844
+ * hashed and the text stamped on it by {@link withServerState}. Every
845
+ * document a request answers with goes through here, so none goes out
846
+ * without them.
739
847
  */
740
- subscribeToDocumentBuilder() {
741
- this.disposables.push(this.services.workspace.DocumentBuilder.onDocumentPhase(this.options.subscriptionPhase, (document, cancelToken) => this.dispatchPhaseEvent(document, cancelToken)));
742
- // The BUILD-phase hook, not the per-document one: this notification is one
743
- // message per build rather than one per document, and Langium hands the
744
- // whole batch over here. It also does not fire for a cancelled build,
745
- // which is what the per-document dispatch has to check by hand.
746
- this.disposables.push(this.services.workspace.DocumentBuilder.onBuildPhase(this.options.subscriptionPhase, built => this.dispatchBuiltEvent(built)));
848
+ encodeDocument(astDocument) {
849
+ const encoded = this.encoder.astDocumentToTransferDocument(astDocument);
850
+ return this.withServerState(encoded, this.computeDocumentFingerprint(astDocument.root, encoded));
851
+ }
852
+ /**
853
+ * The project a client receives for `project`, unchanged by default. Every
854
+ * project a request answers with or a change notification carries goes
855
+ * through here, so a field derived for clients cannot miss one path. The
856
+ * result crosses as JSON, so a value a client reads goes in a data field,
857
+ * not a getter.
858
+ */
859
+ encodeProject(project) {
860
+ return project;
861
+ }
862
+ /**
863
+ * `encoded` with `hash` on its model and the text the server holds for it:
864
+ * the store's while it holds the document, the build's otherwise. The text
865
+ * is read when the document is sent rather than kept with the build: a save
866
+ * changes `text.dirty` without a rebuild, and the store's text moves on before
867
+ * the build that follows it.
868
+ */
869
+ withServerState(encoded, hash) {
870
+ const text = this.serverTextState(encoded.uri);
871
+ if (text && text.version > encoded.model.version) {
872
+ this.tracer.withUri(encoded.uri).debug(`Send a model behind its text: model v${encoded.model.version} / text v${text.version}`);
873
+ }
874
+ return {
875
+ uri: encoded.uri,
876
+ epoch: this.epoch,
877
+ heldWrites: this.heldWrites(encoded.uri, encoded.model.version),
878
+ // The encoder returns the structural base; `TTransfer` is the adopter's
879
+ // declaration of the roots its languages produce, and nothing checks it.
880
+ model: { ...encoded.model, hash },
881
+ ...(text ? { text } : {})
882
+ };
883
+ }
884
+ /** Per session registered on this connection, the highest of its numbered writes of `uri` that `version` holds. */
885
+ heldWrites(uri, version) {
886
+ const held = [];
887
+ for (const [clientId, session] of this.clientSessions) {
888
+ const write = session.heldWrite(uri, version);
889
+ if (write !== undefined) {
890
+ held.push([clientId, write]);
891
+ }
892
+ }
893
+ // Built from entries, so a session named `__proto__` is kept as a key.
894
+ return Object.fromEntries(held);
895
+ }
896
+ /**
897
+ * The text the server holds for `uri`, as every envelope and a watch's
898
+ * answer carry it: the store's while it holds the document, the build's
899
+ * otherwise, and none for a document it has neither of.
900
+ */
901
+ serverTextState(uri) {
902
+ return this.services.workspace.TextDocuments.textState(uri) ?? this.builtTextState(uri);
903
+ }
904
+ /**
905
+ * The text a build read for a document the store never held, which is
906
+ * clean: the store holds every text a client gave it.
907
+ */
908
+ builtTextState(uri) {
909
+ const textDocument = this.modelService.getDocument(uri)?.textDocument;
910
+ return textDocument && { version: textDocument.version, hash: textHash(textDocument.getText()), dirty: false };
911
+ }
912
+ /**
913
+ * Follow every document at the configured
914
+ * {@link DataServerOptions.subscriptionPhase}, once per connection rather
915
+ * than once per watch, so the cost stays flat in the number of watches.
916
+ */
917
+ subscribeToModelUpdates() {
918
+ const phase = this.options.subscriptionPhase;
919
+ this.disposables.push(this.modelService.onModelUpdated(event => this.dispatchPhaseEvent(event), { phase }));
920
+ // One message per build rather than one per document.
921
+ this.disposables.push(this.modelService.onModelsBuilt(event => this.dispatchBuiltEvent(event.uris), { phase }));
747
922
  }
748
923
  /**
749
924
  * Report the documents that reached {@link DataServerOptions.subscriptionPhase}
@@ -765,7 +940,7 @@ export class DataServer {
765
940
  *
766
941
  * Payload-free on purpose: URIs only, so a client re-reads what it displays
767
942
  * rather than being handed transfer documents it did not ask for. That is the
768
- * property the subscription map protects, and it is preserved here by
943
+ * property the watcher gate protects, and it is preserved here by
769
944
  * carrying no document rather than by gating the message.
770
945
  *
771
946
  * Quiet in the common case. Editing a document that an editor has open leaves
@@ -773,15 +948,9 @@ export class DataServer {
773
948
  * dependents produces nothing at all, and workspace initialisation produces
774
949
  * nothing because it does not build to this phase. The ceiling is a
775
950
  * whole-workspace rebuild at the subscription phase: one message, URIs only.
776
- *
777
- * The URI is canonicalised for the same reason the subscription map is keyed
778
- * that way, and is untested for the same reason as its twin in
779
- * {@link dispatchDeleteEvents}: the builder reports URIs out of its own
780
- * store, so a non-canonical one cannot be produced without a fixture
781
- * asserting a shape the real system never emits.
782
951
  */
783
952
  dispatchBuiltEvent(built) {
784
- const uris = built.map(document => this.canonicalKey(document.uri.toString())).filter(uri => !this.subscriptions.has(uri));
953
+ const uris = built.map(uri => this.canonicalKey(uri)).filter(uri => !this.uriWatchRecords.get(uri)?.watchers.size);
785
954
  if (uris.length === 0) {
786
955
  return;
787
956
  }
@@ -789,13 +958,12 @@ export class DataServer {
789
958
  this.clientProxy.onDocumentsBuilt({ uris });
790
959
  }
791
960
  /**
792
- * Subscribe to the universal save event on `HydraniumTextDocuments` so a
793
- * single `onDocumentSaved` wire notification fires for ANY save of a
794
- * subscribed URI — regardless of whether the save originated from the
795
- * data-server's RPC `saveModelDocument`, the LSP head's text-editor save,
796
- * or any other client writing through `notifyDidSaveTextDocument`.
961
+ * Follow every save so a single `onDocumentSaved` wire notification fires
962
+ * for ANY save of a subscribed URI — regardless of whether the save
963
+ * originated from the data-server's RPC `saveModelDocument`, the LSP head's
964
+ * text-editor save, or any other client.
797
965
  *
798
- * Architectural symmetry with {@link subscribeToDocumentBuilder}: every
966
+ * Architectural symmetry with {@link subscribeToModelUpdates}: every
799
967
  * subscribed client sees every state change to documents they care about,
800
968
  * regardless of which client triggered it. Firing `onDocumentSaved` only
801
969
  * from the data-server's own RPC path is a bug, not an optimisation: an
@@ -803,20 +971,50 @@ export class DataServer {
803
971
  * clients, which never clear their dirty state.
804
972
  */
805
973
  subscribeToTextDocumentSaves() {
806
- this.disposables.push(this.services.workspace.TextDocuments.onDidSave(event => this.dispatchSaveEvent(event)));
974
+ this.disposables.push(this.modelService.onModelSaved(event => this.dispatchSaveEvent(event)));
975
+ }
976
+ /** Relay each change of a document's dirty state; see {@link dispatchDirtyEvent}. */
977
+ subscribeToDirtyChanges() {
978
+ this.disposables.push(this.modelService.onDirtyChanged(event => this.dispatchDirtyEvent(event)));
807
979
  }
808
980
  /**
809
- * Mark the last-close transition per URI (see
810
- * {@link pendingRevertBroadcasts}). The listener consults
811
- * `isOpenInAnyClient` AFTER the store decremented the closing client's
812
- * hold, so a `false` answer means this close was the last one.
981
+ * Send a dirty flip to the connection, gated on the URI's watchers as
982
+ * {@link dispatchSaveEvent} is: the answer at any one moment travels on
983
+ * every document sent, so a client that watches nothing reads it there.
813
984
  */
814
- subscribeToTextDocumentCloses() {
815
- const textDocuments = this.services.workspace.TextDocuments;
816
- this.disposables.push(textDocuments.onDidClose(event => {
817
- if (!textDocuments.isOpenInAnyClient(event.document.uri)) {
818
- this.pendingRevertBroadcasts.add(this.canonicalKey(event.document.uri));
819
- }
985
+ dispatchDirtyEvent(event) {
986
+ const uri = this.canonicalKey(event.uri);
987
+ if (this.uriWatchRecords.get(uri)?.watchers.size) {
988
+ this.clientProxy.onDocumentDirtyChanged({ uri, text: event.text });
989
+ }
990
+ }
991
+ /**
992
+ * Mark each document the store releases, so {@link dispatchPhaseEvent}
993
+ * broadcasts the build that follows even without a watcher. By default
994
+ * that build reverts the document to its disk content, discarding unsaved
995
+ * in-session edits, and the last close typically also
996
+ * removed the last watcher: without the broadcast, a consumer that only
997
+ * ever fetches via `getModelDocument` keeps showing the discarded state.
998
+ * The broadcast is de-duplicated against the fingerprint while a watcher
999
+ * holds one, so a close whose disk state equals the last emitted state
1000
+ * stays silent; with no watcher there is no fingerprint, and every release
1001
+ * build is sent. The first build after the release consumes the mark, and
1002
+ * goes out as the release unless it is stamped past the version a client
1003
+ * opened the document at again: a client's write or an integrity repair
1004
+ * that rides it keeps its author.
1005
+ *
1006
+ * A release, not the close itself: a document whose last client lost its
1007
+ * connection is released only once the release grace runs out, or when
1008
+ * another client opens it meanwhile, and not at all when a client lost
1009
+ * from it opens it again within its own grace.
1010
+ */
1011
+ subscribeToModelReleases() {
1012
+ this.disposables.push(this.modelService.onModelReleased(event => {
1013
+ const uri = this.canonicalKey(event.uri);
1014
+ const record = this.uriWatchRecords.get(uri) ?? { watchers: new Set() };
1015
+ record.releaseBroadcastPending = true;
1016
+ this.uriWatchRecords.set(uri, record);
1017
+ this.tracer.withUri(uri).debug('Release marked: the first build after it is broadcast, watched or not');
820
1018
  }));
821
1019
  }
822
1020
  /**
@@ -825,7 +1023,7 @@ export class DataServer {
825
1023
  *
826
1024
  * **Deliberately ungated, where {@link dispatchPhaseEvent} and
827
1025
  * {@link dispatchSaveEvent} are gated.** Those two carry a built document and
828
- * fire on every build, so the subscription map is what keeps bandwidth
1026
+ * fire on every build, so the watcher gate is what keeps bandwidth
829
1027
  * proportional to what a client asked for. A deletion is neither: it is one
830
1028
  * URI, it is rare, and it reports the workspace's STRUCTURE rather than a
831
1029
  * document's content. Gating it forces any consumer that displays the
@@ -834,55 +1032,47 @@ export class DataServer {
834
1032
  * no way to run: its workspace lives behind the head. A client that does not
835
1033
  * care filters on the URI, which costs it a comparison.
836
1034
  *
837
- * The precedent is the last-close revert broadcast (see
838
- * {@link pendingRevertBroadcasts}), where the same judgement was already made
1035
+ * The precedent is the release broadcast (see
1036
+ * {@link subscribeToModelReleases}), where the same judgement was already made
839
1037
  * in the other direction: a transition that matters enough is delivered
840
1038
  * without a subscription.
841
1039
  *
842
- * Runs from the `DocumentBuilder.onUpdate` listener rather than from a phase
843
- * listener, which is the only place a deletion is observable: `update`
844
- * removes the document before deriving the rebuild set, so it is never built.
845
- * That ordering also puts this notification ahead of the `'rebuilt'` events
846
- * for the dependents whose references the deletion just broke.
1040
+ * A deletion is announced before the build that follows it, so this goes
1041
+ * out ahead of the `'rebuilt'` events for the dependents whose references
1042
+ * the deletion just broke.
847
1043
  *
848
- * The `deleted` list arrives already expanded to concrete document URIs —
849
- * `deleteDocuments` resolves a directory URI to the documents beneath it — so
850
- * no caller has to handle a directory here.
1044
+ * The URI's fingerprint and release mark are dropped: they describe a
1045
+ * document that no longer exists. A kept fingerprint is adopted as the
1046
+ * baseline and suppresses the first emit after the file returns with its
1047
+ * previous content.
851
1048
  *
852
- * Per-URI derived state is dropped for the same reason it is dropped
853
- * anywhere: it describes a document that no longer exists.
854
- * {@link lastEmittedFingerprint} in particular can outlive its subscriptions
855
- * through the last-close revert path — left behind, it is adopted as the
856
- * baseline by the next {@link watchModelDocument} and suppresses the first
857
- * emit after the file returns with its previous content.
858
- *
859
- * The subscription itself SURVIVES: a recreated file resumes delivering to
860
- * the same watchers with no re-subscription, and a client that answers the
861
- * deletion by closing releases the watch through `closeModelDocument` anyway.
1049
+ * The watchers SURVIVE: a recreated file resumes delivering to them with no
1050
+ * re-subscription, and a client that answers the deletion by closing
1051
+ * releases the watch through `closeModelDocument` anyway. A session that
1052
+ * deleted the document has its own released by {@link deleteModelDocument}.
862
1053
  */
863
- dispatchDeleteEvents(deleted) {
864
- for (const removed of deleted) {
865
- const uri = this.canonicalKey(removed.toString());
866
- this.lastEmittedFingerprint.delete(uri);
867
- this.pendingRevertBroadcasts.delete(uri);
868
- this.clientProxy.onDocumentDeleted({ uri });
1054
+ dispatchDeleteEvent(event) {
1055
+ const uri = this.canonicalKey(event.uri);
1056
+ const record = this.uriWatchRecords.get(uri);
1057
+ if (record) {
1058
+ record.fingerprint = undefined;
1059
+ record.fingerprintVersion = undefined;
1060
+ record.releaseBroadcastPending = undefined;
1061
+ record.sentVersion = undefined;
1062
+ this.pruneUriWatchRecord(uri);
869
1063
  }
1064
+ this.clientProxy.onDocumentDeleted({ uri, sourceClientId: event.sourceClientId, sequence: ++this.eventSequence });
870
1065
  }
871
- /** Fan out a save event for the document's URI, gated by the subscription map. */
1066
+ /** Fan out a save event for the document's URI, gated on the URI's watchers. */
872
1067
  dispatchSaveEvent(event) {
873
- // The save event arrives under the CLIENT URI the text store keys by (e.g. a
874
- // symlink path S); the subscription map and `dispatchPhaseEvent` key by the
875
- // CANONICAL identity R. Canonicalize before both the gate and the envelope so
876
- // a save of a symlinked file isn't silently dropped (and the envelope resolves
877
- // the R-keyed document rather than missing into an empty one).
878
1068
  const uri = this.canonicalKey(event.document.uri);
879
- if (!this.subscriptions.has(uri)) {
1069
+ if (!this.uriWatchRecords.get(uri)?.watchers.size) {
880
1070
  return;
881
1071
  }
882
- const response = this.envelope(UriUtils.toUri(uri));
1072
+ const response = this.envelope(UriUtils.toUri(event.document.uri));
883
1073
  const wireEvent = {
884
1074
  document: response,
885
- sourceClientId: event.clientId
1075
+ sourceClientId: event.sourceClientId
886
1076
  };
887
1077
  this.clientProxy.onDocumentSaved(wireEvent);
888
1078
  }
@@ -895,100 +1085,107 @@ export class DataServer {
895
1085
  * out to multiple local subscribers with an `Emitter<T>` — the
896
1086
  * framework deliberately does NOT promise multi-listener semantics.
897
1087
  *
898
- * The dispatch is guarded by two filters:
899
- * 1. **Subscription map**: events for URIs no subscriber registered for
900
- * are NOT sent over the wire (bandwidth scales with subscribed URIs,
901
- * not phase events).
902
- * 2. **Emission fingerprint**: events for rebuilds that produce no
903
- * observable change since the last emit are suppressed. See
904
- * {@link lastEmittedFingerprint} for the rationale.
1088
+ * An event for a URI no client watches is NOT sent over the wire, so
1089
+ * bandwidth scales with watched URIs rather than with phase events; a
1090
+ * pending release mark is the exception (see
1091
+ * {@link subscribeToModelReleases}).
1092
+ *
1093
+ * An event for a rebuild with no observable change since the last emit is
1094
+ * suppressed against the URI's fingerprint and the version it was taken at;
1095
+ * a new version with the same fingerprint goes out with the same
1096
+ * `model.hash`, so a watcher still learns the version. Such rebuilds are routine: a
1097
+ * second client attaching to an open document makes
1098
+ * `HydraniumTextDocuments.refreshContent` fire `onDidChangeContent` purely
1099
+ * to re-trigger the build, and Langium rebuilds a document when a
1100
+ * dependency changes even if its diagnostics come out the same. Sent at a
1101
+ * version this head has not sent, they reach watchers as `'changed'`, and a
1102
+ * watcher that reads `'changed'` as another client's write and resets its
1103
+ * root to the server view loses the user's edits. The first {@link watchModelDocument} baselines the
1104
+ * fingerprint, so the first event after a watch is de-duplicated against
1105
+ * the state the watcher just fetched; it goes with the last watcher, so the
1106
+ * next first watch re-baselines rather than adopting a stale digest. A
1107
+ * digest keeps its memory constant in document size; see
1108
+ * {@link computeDocumentFingerprint} for the inputs.
905
1109
  */
906
- dispatchPhaseEvent(document, cancelToken) {
907
- if (cancelToken.isCancellationRequested) {
908
- // Build preempted by a concurrent write lock (or other cancel source) —
909
- // the subscription event is stale by the time it would fire. Skip
910
- // emission so RPC subscribers don't surface intermediate states.
911
- // A pending revert mark is deliberately NOT consumed here: the
912
- // follow-up build re-fires this phase and broadcasts then.
1110
+ dispatchPhaseEvent(event) {
1111
+ const uri = this.canonicalKey(event.document.uri);
1112
+ const record = this.uriWatchRecords.get(uri);
1113
+ const afterRelease = record?.releaseBroadcastPending === true;
1114
+ if (!record || (record.watchers.size === 0 && !afterRelease)) {
913
1115
  return;
914
1116
  }
915
- const uri = document.uri.toString();
916
- // Consume the last-close mark even when subscriptions exist — the
917
- // regular dispatch below serves those watchers, and the mark's author
918
- // attribution is more precise than the post-close `UNKNOWN_CLIENT_ID`
919
- // the author lookup would yield.
920
- const revertedOnClose = this.pendingRevertBroadcasts.delete(uri);
921
- if (!this.subscriptions.has(uri) && !revertedOnClose) {
1117
+ if (afterRelease) {
1118
+ // Consumed even when watchers exist — the regular dispatch below
1119
+ // serves them, and the mark's attribution is more precise than the
1120
+ // post-close unknown client the manager's attribution would yield.
1121
+ // With no watcher the record goes now, so the fingerprint written
1122
+ // below lands on a detached record and the next first watch
1123
+ // re-baselines instead of adopting it.
1124
+ record.releaseBroadcastPending = undefined;
1125
+ this.pruneUriWatchRecord(uri);
1126
+ }
1127
+ // The encoder reads the built document, which the event's holds for this
1128
+ // synchronous run.
1129
+ const document = this.modelService.getDocument(event.document.uri);
1130
+ if (!document) {
922
1131
  return;
923
1132
  }
924
- const fingerprint = this.computeDocumentFingerprint(document);
925
- if (this.lastEmittedFingerprint.get(uri) === fingerprint) {
1133
+ const fingerprint = this.computeDocumentFingerprint(document.parseResult.value, this.encoder.toTransferDocument(document));
1134
+ const stamped = this.services.workspace.ModelLedger.versionOf(document.parseResult.value);
1135
+ if (record.fingerprint === fingerprint && record.fingerprintVersion === stamped) {
926
1136
  // A rebuild that produced no observable change since the last emit —
927
1137
  // suppressed so RPC subscribers don't see a no-op broadcast. Logged at
928
1138
  // debug so a *needed* re-broadcast wrongly suppressed by this dedup
929
1139
  // (the failure mode the fingerprint strategy must avoid) is visible.
930
- this.tracer.withUri(uri).debug(`Suppress onDocumentUpdated v${document.textDocument.version}: fingerprint unchanged`);
1140
+ this.tracer.withUri(uri).debug(`Suppress onDocumentUpdated v${stamped}: fingerprint and version unchanged`);
931
1141
  return;
932
1142
  }
933
- this.lastEmittedFingerprint.set(uri, fingerprint);
934
- const event = {
935
- document: this.envelope(document.uri),
936
- sourceClientId: revertedOnClose ? REVERT_ON_CLOSE_CLIENT_ID : this.resolveSourceClientId(document),
937
- reason: this.resolveUpdateReason(document.uri)
1143
+ record.fingerprint = fingerprint;
1144
+ record.fingerprintVersion = stamped;
1145
+ // The event's attribution, so this head names the same client as the
1146
+ // in-process heads do for one build, except for a version this head
1147
+ // already sent; see `DataServerUriWatchRecord.sentVersion`.
1148
+ const version = this.modelService.isOpen(event.document.uri) ? document.textDocument.version : undefined;
1149
+ const { reason, sourceClientId } = version !== undefined && record.sentVersion === version
1150
+ ? { reason: 'rebuilt', sourceClientId: UNKNOWN_CLIENT_ID }
1151
+ : event;
1152
+ record.sentVersion = version;
1153
+ // The stamp, not the live version: a write after the build's parse moves
1154
+ // only the latter. At the opened version too, which a reopen with the
1155
+ // released text keeps.
1156
+ const openedVersion = afterRelease ? this.modelService.openedVersion(event.document.uri) : undefined;
1157
+ const releaseBuild = afterRelease && (openedVersion === undefined || stamped <= openedVersion);
1158
+ const wireEvent = {
1159
+ document: this.envelope(document.uri, fingerprint),
1160
+ sourceClientId: releaseBuild ? DOCUMENT_RELEASE_CLIENT_ID : sourceClientId,
1161
+ reason,
1162
+ sequence: ++this.eventSequence
938
1163
  };
1164
+ const mark = afterRelease ? `, release mark: opened v${openedVersion ?? '-'}, attributed ${sourceClientId}` : '';
939
1165
  this.tracer
940
1166
  .withUri(uri)
941
- .debug(`Emit onDocumentUpdated v${event.document.version} (reason=${event.reason}, sourceClientId=${event.sourceClientId})`);
942
- this.clientProxy.onDocumentUpdated(event);
1167
+ .debug(`Emit onDocumentUpdated v${stamped} (reason=${wireEvent.reason}, sourceClientId=${wireEvent.sourceClientId}${mark})`);
1168
+ this.clientProxy.onDocumentUpdated(wireEvent);
943
1169
  }
944
1170
  /**
945
- * Fingerprint of the document's observable state, used to de-dup
946
- * `onDocumentUpdated` emissions. The {@link FingerprintStrategy} option
947
- * selects what is hashed (default `'transfer-document'` — see the type), and
948
- * {@link additionalFingerprintInputs} folds in any extra adopter signal.
1171
+ * Fingerprint of the snapshot `root` and its encoding `encoded`, used to
1172
+ * de-dup `onDocumentUpdated` emissions and sent as every document's
1173
+ * `model.hash`, so it must not depend on the version or on live state. The
1174
+ * {@link FingerprintStrategy} option selects what is hashed (default
1175
+ * `'transfer-document'` — see the type).
949
1176
  *
950
- * The default `'transfer-document'` strategy goes through the encoder's
951
- * {@link TransferEncoder.toTransferDocument}, which is cached per build —
952
- * so within one phase event the fingerprint and the subsequently-emitted
953
- * `envelope` share a single encode walk.
1177
+ * {@link TransferEncoder.toTransferDocument} is cached per build, so within
1178
+ * one phase event the fingerprint and the subsequently-emitted `envelope`
1179
+ * share a single encode walk.
954
1180
  *
955
1181
  * Wrapped in {@link Tracer.time} against {@link FINGERPRINT_LOG_AFTER_MS}.
956
1182
  */
957
- computeDocumentFingerprint(document) {
958
- return this.tracer.withUri(document.uri.toString()).time('Compute fingerprint', () => {
959
- const base = this.options.fingerprintStrategy === 'text-diagnostics'
960
- ? textDiagnosticsFingerprint(document)
961
- : transferDocumentFingerprint(this.encoder.toTransferDocument(document));
962
- const extra = this.additionalFingerprintInputs(document);
963
- return extra.length === 0 ? base : fingerprintHash([base, FINGERPRINT_SEPARATOR, ...extra.map(value => JSON.stringify(value))]);
964
- }, 'debug', { logAfterMs: FINGERPRINT_LOG_AFTER_MS });
965
- }
966
- /**
967
- * Extra inputs folded into {@link computeDocumentFingerprint} alongside the
968
- * chosen {@link FingerprintStrategy}. Default: none. Override to contribute a
969
- * signal that lives outside the document's text / root / diagnostics — each
970
- * entry need only be stable across equivalent emissions and
971
- * JSON-serialisable.
972
- */
973
- additionalFingerprintInputs(_document) {
974
- return [];
975
- }
976
- /**
977
- * Discriminate the reason for a phase-event-driven update notification.
978
- * Uses the most recent `DocumentBuilder.onUpdate` snapshot:
979
- * - URI in the `changed` list → `'changed'` (the URI was passed to
980
- * `documentBuilder.update(changed, deleted)`, which spans `didChange`
981
- * text-document events and programmatic `update([uri], [])` calls).
982
- * - Otherwise → `'rebuilt'` (cascade re-derivation: this URI was rebuilt
983
- * because something it depends on changed; its own text wasn't flagged).
984
- *
985
- * `'saved'` is NOT emitted here — saves take the dedicated
986
- * `DataClientProtocol.onDocumentSaved` channel, and adopters wanting a
987
- * unified stream synthesise it in their bridge layer. A deletion is not a
988
- * reason at all; see {@link dispatchDeleteEvents}.
989
- */
990
- resolveUpdateReason(uri) {
991
- return this.lastBuildUpdate?.changed.some(changed => UriUtils.equals(changed, uri)) ? 'changed' : 'rebuilt';
1183
+ computeDocumentFingerprint(root, encoded) {
1184
+ return this.tracer
1185
+ .withUri(encoded.uri)
1186
+ .time('Compute fingerprint', () => this.options.fingerprintStrategy === 'text-diagnostics'
1187
+ ? textDiagnosticsFingerprint(this.services.workspace.ModelLedger.textOf(root), encoded.model)
1188
+ : transferDocumentFingerprint(encoded.model), 'debug', { logAfterMs: FINGERPRINT_LOG_AFTER_MS });
992
1189
  }
993
1190
  /**
994
1191
  * Subscribe to the project tier's change channel and re-fan registry
@@ -1012,38 +1209,38 @@ export class DataServer {
1012
1209
  /**
1013
1210
  * Fan one internal {@link ProjectChangeEvent} out to per-project wire
1014
1211
  * notifications. Each `removed` entry pairs the id with the pre-removal
1015
- * snapshot needed for the `'removed'` wire payload.
1212
+ * snapshot needed for the `'removed'` wire payload. A project whose
1213
+ * {@link encodeProject} throws is logged and skipped, so the rest of the
1214
+ * batch still reaches the client.
1016
1215
  */
1017
1216
  dispatchProjectChangeEvent(event) {
1217
+ const send = (project, reason) => {
1218
+ let encoded;
1219
+ try {
1220
+ encoded = this.encodeProject(project);
1221
+ }
1222
+ catch (error) {
1223
+ this.tracer.error(`Project ${project.id} not sent as ${reason}: encodeProject threw: ${describeErrorWithStack(error)}`);
1224
+ return;
1225
+ }
1226
+ this.clientProxy.onProjectsChanged({ project: encoded, reason });
1227
+ };
1018
1228
  for (const id of event.added) {
1019
1229
  const project = this.services.workspace.ProjectManager.getProjectById(id);
1020
1230
  if (project) {
1021
- this.clientProxy.onProjectsChanged({ project, reason: 'added' });
1231
+ send(project, 'added');
1022
1232
  }
1023
1233
  }
1024
1234
  for (const id of event.updated) {
1025
1235
  const project = this.services.workspace.ProjectManager.getProjectById(id);
1026
1236
  if (project) {
1027
- this.clientProxy.onProjectsChanged({ project, reason: 'updated' });
1237
+ send(project, 'updated');
1028
1238
  }
1029
1239
  }
1030
1240
  for (const entry of event.removed) {
1031
- this.clientProxy.onProjectsChanged({ project: entry.snapshot, reason: 'removed' });
1241
+ send(entry.snapshot, 'removed');
1032
1242
  }
1033
1243
  }
1034
- /**
1035
- * Resolve the wire-level `sourceClientId` for `document`'s events — the client
1036
- * that authored its current version, from the version-author history on
1037
- * `HydraniumTextDocuments`. The protocol-level counterpart of the internal
1038
- * `AstDocumentManager.getAuthor`: a framework-internal rebuild has no author,
1039
- * so this surfaces the {@link UNKNOWN_CLIENT_ID} presentation default. Adopters
1040
- * that rebuild through non-text-document channels override to derive a
1041
- * source id of their own.
1042
- */
1043
- resolveSourceClientId(document) {
1044
- const author = this.services.workspace.TextDocuments.getAuthor(document.textDocument.uri, document.textDocument.version);
1045
- return author ?? UNKNOWN_CLIENT_ID;
1046
- }
1047
1244
  // --- Diagnostics (DataServerDiagnosticsProtocol) ---------------------------
1048
1245
  // Computed in THIS process: the data-server child holds the model store, so
1049
1246
  // these snapshots reflect the heap that actually carries the workspace
@@ -1101,7 +1298,7 @@ export class DataServer {
1101
1298
  const builtIn = [...DATA_SERVER_PROTOCOL_METHODS, ...DATA_SERVER_DIAGNOSTICS_METHODS];
1102
1299
  const overlap = additionalMethods.filter(name => builtIn.includes(name));
1103
1300
  if (overlap.length > 0) {
1104
- throw new Error(`DataServer.additionalMethods overlaps with built-in data-server methods: ${overlap.join(', ')}. ` +
1301
+ throw new Error(`DataServerOptions.additionalMethods overlaps with built-in data-server methods: ${overlap.join(', ')}. ` +
1105
1302
  'Adopter protocols must not redeclare framework method names; rename the adopter method or remove it from additionalMethods.');
1106
1303
  }
1107
1304
  return {
@@ -1110,7 +1307,8 @@ export class DataServer {
1110
1307
  methodNamespace: partial.methodNamespace ?? DATA_SERVER_WIRE_PREFIX,
1111
1308
  additionalMethods,
1112
1309
  excludedMethods: partial.excludedMethods ?? [],
1113
- diagnostics: partial.diagnostics ?? defaultDataServerDiagnostics()
1310
+ diagnostics: partial.diagnostics ?? defaultDataServerDiagnostics(),
1311
+ referenceSettleTimeoutMs: partial.referenceSettleTimeoutMs ?? DataServer.DEFAULT_OPTIONS.referenceSettleTimeoutMs
1114
1312
  };
1115
1313
  }
1116
1314
  }