@hydranium/data-server 1.0.0-next.42 → 1.0.0-next.421

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