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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/README.md +57 -77
  2. package/lib/browser/index.d.ts +10 -0
  3. package/lib/browser/index.d.ts.map +1 -0
  4. package/lib/browser/index.js +15 -0
  5. package/lib/browser/index.js.map +1 -0
  6. package/lib/browser/start-data-server-in-worker.d.ts +23 -0
  7. package/lib/browser/start-data-server-in-worker.d.ts.map +1 -0
  8. package/lib/browser/start-data-server-in-worker.js +29 -0
  9. package/lib/browser/start-data-server-in-worker.js.map +1 -0
  10. package/lib/data-server.d.ts +407 -319
  11. package/lib/data-server.d.ts.map +1 -1
  12. package/lib/data-server.js +728 -530
  13. package/lib/data-server.js.map +1 -1
  14. package/lib/messages/index.d.ts +1 -1
  15. package/lib/messages/index.d.ts.map +1 -1
  16. package/lib/messages/index.js +1 -1
  17. package/lib/messages/index.js.map +1 -1
  18. package/lib/node/node-diagnostics-provider.d.ts.map +1 -1
  19. package/lib/node/node-diagnostics-provider.js +3 -2
  20. package/lib/node/node-diagnostics-provider.js.map +1 -1
  21. package/lib/testing/data-server-harness.d.ts +27 -14
  22. package/lib/testing/data-server-harness.d.ts.map +1 -1
  23. package/lib/testing/data-server-harness.js +5 -2
  24. package/lib/testing/data-server-harness.js.map +1 -1
  25. package/lib/testing/index.d.ts +1 -0
  26. package/lib/testing/index.d.ts.map +1 -1
  27. package/lib/testing/index.js.map +1 -1
  28. package/package.json +35 -43
  29. package/src/browser/index.ts +15 -0
  30. package/src/browser/start-data-server-in-worker.ts +35 -0
  31. package/src/data-server.ts +905 -580
  32. package/src/messages/index.ts +8 -1
  33. package/src/node/node-diagnostics-provider.ts +3 -9
  34. package/src/testing/data-server-harness.ts +44 -25
  35. package/src/testing/index.ts +5 -2
@@ -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
@@ -226,6 +273,22 @@ export interface DataServerOptions extends LogNameOptions {
226
273
  */
227
274
  readonly fingerprintStrategy?: FingerprintStrategy;
228
275
 
276
+ /**
277
+ * How long a reference question waits for the build to reach `Linked` before
278
+ * failing with {@link REFERENCE_SETTLE_TIMEOUT_CODE}. Bounded because the
279
+ * wait resolves off a build-phase notification, so a build cancelled below
280
+ * `Linked` with nothing after it leaves the request unanswered for the life
281
+ * of the server.
282
+ *
283
+ * Expiry rejects rather than reading the index anyway: the unsettled index
284
+ * is what the wait keeps the caller away from, so falling through to it on a
285
+ * slow build reinstates the fault — and on a name proposal persists it.
286
+ *
287
+ * Default {@link DataServer.DEFAULT_OPTIONS}, sized for a cold workspace
288
+ * still reaching `Linked` for the first time.
289
+ */
290
+ readonly referenceSettleTimeoutMs?: number;
291
+
229
292
  /**
230
293
  * Wire-method namespace for both inbound request handlers and the
231
294
  * outbound notification client proxy. Defaults to
@@ -283,15 +346,20 @@ export interface DataServerOptions extends LogNameOptions {
283
346
  * When supplied, every inbound data-server RPC is timed into this collector
284
347
  * (via the `createRpcProxy` binding) and exposed through
285
348
  * {@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).
349
+ * LSP connection can pass the SAME collector through `lspLatencyOptions` to
350
+ * that connection, so one report covers both heads. Absent by default (no
351
+ * timing overhead).
289
352
  */
290
353
  readonly latency?: LatencyCollector;
291
354
  }
292
355
 
293
- /** Fully-resolved variant — every field set, used internally after merging defaults. */
294
- interface ResolvedDataServerOptions {
356
+ /**
357
+ * Fully-resolved variant of {@link DataServerOptions} — every field set, after
358
+ * merging defaults. Returned by the `protected`
359
+ * {@link DataServer.resolveOptions} and held on {@link DataServer.options}, so
360
+ * an adopter changing how defaults resolve has to name it.
361
+ */
362
+ export interface ResolvedDataServerOptions {
295
363
  readonly subscriptionPhase: DocumentState;
296
364
  readonly fingerprintStrategy: FingerprintStrategy;
297
365
  readonly methodNamespace: string;
@@ -299,6 +367,41 @@ interface ResolvedDataServerOptions {
299
367
  readonly excludedMethods: readonly string[];
300
368
  /** Always resolved — to the caller's, or to the platform default. */
301
369
  readonly diagnostics: DataServerDiagnosticsProvider;
370
+ readonly referenceSettleTimeoutMs: number;
371
+ }
372
+
373
+ /**
374
+ * What a {@link DataServer} keeps per canonical URI, in the `protected`
375
+ * {@link DataServer.uriWatchRecords}. A record lives while it has a watcher
376
+ * or a release mark; {@link DataServer.pruneUriWatchRecord} enforces that on
377
+ * every path that removes either, so a record with no watcher has a release
378
+ * broadcast pending. Test the size of {@link DataServerUriWatchRecord.watchers}, not the
379
+ * record's presence, for "watched".
380
+ */
381
+ export interface DataServerUriWatchRecord {
382
+ /** The client ids watching the URI; save, dirty and phase events go out only while it is non-empty, a pending release mark aside. */
383
+ readonly watchers: Set<string>;
384
+ /** 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. */
385
+ fingerprint?: string;
386
+ /**
387
+ * The version {@link DataServerUriWatchRecord.fingerprint} was taken at; an
388
+ * event goes out when either moves. Not {@link DataServerUriWatchRecord.sentVersion}:
389
+ * a first watch sets this and sends nothing, so the next event at its
390
+ * version still carries the manager's attribution.
391
+ */
392
+ fingerprintVersion?: ModelVersion;
393
+ /** Set by {@link DataServer.subscribeToModelReleases} once a release happened: its broadcast, the next phase event, goes out even without a watcher. */
394
+ releaseBroadcastPending?: boolean;
395
+ /**
396
+ * The version of an open document the last event sent was built at. The
397
+ * version sent again goes out as `'rebuilt'` from no client: the manager
398
+ * counts a version delivered only once its `Validated` listeners ran, so a
399
+ * build cancelled after the subscription phase would send it as a change
400
+ * twice. Only while the store has the document open: without it, a
401
+ * document can keep one version through changes of its file, so its events
402
+ * keep the manager's attribution.
403
+ */
404
+ sentVersion?: number;
302
405
  }
303
406
 
304
407
  /**
@@ -322,15 +425,19 @@ interface ResolvedDataServerOptions {
322
425
  * adopters subclass ONLY when they need to decorate wire returns or
323
426
  * notifications. The usual adoption path is DI rebinds alone.
324
427
  *
428
+ * **`TTransfer` is not checked.** It declares the union of the transfer roots
429
+ * the workspace's languages produce. Which root a document has is known only
430
+ * at runtime, from its language, so a `TTransfer` missing a language compiles.
431
+ *
325
432
  * Lifecycle: the constructor registers framework request handlers (plus
326
433
  * any names supplied via {@link DataServerOptions.additionalMethods}) and
327
434
  * builds the {@link DataClientProtocol} notification proxy on the same
328
435
  * 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.
436
+ * `localTarget`) under the configured namespace, and follows documents
437
+ * through `ModelService`'s events: at the configured phase, `onModelUpdated`
438
+ * dispatches per-document `clientProxy.onDocumentUpdated` for watched URIs and
439
+ * `onModelsBuilt` a single `clientProxy.onDocumentsBuilt` naming the batch's
440
+ * unwatched documents.
334
441
  *
335
442
  * The data-server requires the full {@link ServerSharedServices}
336
443
  * shape — `HydraniumTextDocuments` for client-attributed updates,
@@ -358,89 +465,42 @@ export class DataServer<
358
465
  * Framework defaults, exposed so an adopter can spread them when
359
466
  * extending rather than restate a value the framework may change.
360
467
  */
361
- static readonly DEFAULT_OPTIONS: Required<Pick<DataServerOptions, 'subscriptionPhase'>> = {
362
- subscriptionPhase: DocumentState.Validated
468
+ static readonly DEFAULT_OPTIONS: Required<Pick<DataServerOptions, 'subscriptionPhase' | 'referenceSettleTimeoutMs'>> = {
469
+ subscriptionPhase: DocumentState.Validated,
470
+ referenceSettleTimeoutMs: 60_000
363
471
  };
364
472
 
365
473
  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>>();
474
+ /** This connection's {@link DataServerUriWatchRecord} per canonical URI; see there for when a record lives. */
475
+ protected readonly uriWatchRecords = new Map<string, DataServerUriWatchRecord>();
368
476
  /**
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.
477
+ * Per client id, the key each URI it watched resolved to when it watched.
478
+ * An unwatch looks the key up here: once a file is gone, a symlink to it
479
+ * no longer resolves to the key the watch is under.
378
480
  */
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();
481
+ protected readonly watchKeys = new Map<string, Map<string, string>>();
383
482
  /**
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.
483
+ * The position of the last update or deletion event sent, which each
484
+ * such event carries, and a watch's answer carries as of the moment it
485
+ * took whether its document exists: an event past it was sent after.
392
486
  */
393
- protected lastBuildUpdate?: { changed: readonly URI[]; deleted: readonly URI[] };
487
+ protected eventSequence = 0;
394
488
  /**
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.
489
+ * The live client sessions this connection registered, by client id. A
490
+ * document request carrying one of these ids acts as that session; one
491
+ * carrying any other id fails. A session leaves once it ends, so a
492
+ * connection that registers many over its life does not accumulate them.
428
493
  */
429
- protected readonly lastEmittedFingerprint = new Map<string, string>();
494
+ protected readonly clientSessions = new Map<string, ClientSession<AstNode, AstDiagnostic, TTransfer>>();
430
495
  /**
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.
496
+ * Typed client proxy — sends `data-server/on*` notifications back over the same wire.
497
+ * The proxy sends any `on[A-Z]*` name other than `onDidOpenConnection` and
498
+ * `onDidCloseConnection`, for which it returns its own events, so a subclass
499
+ * with notifications of its own narrows only the type:
500
+ * `declare protected readonly clientProxy: TheirClient`.
442
501
  */
443
- protected readonly pendingRevertBroadcasts = new Set<string>();
502
+ protected readonly clientProxy: DataClientProtocol<TTransfer, TDiagnostic, TProject>;
503
+ protected readonly disposables = new DisposableCollection();
444
504
  protected readonly tracer: Tracer;
445
505
  /** The in-flight interactive profile capture, held between {@link startProfiling} and {@link stopProfiling}. */
446
506
  protected activeProfile?: DataServerProfileCapture;
@@ -452,24 +512,20 @@ export class DataServer<
452
512
  protected readonly encoder: TransferEncoder<TDiagnostic>;
453
513
  /**
454
514
  * 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`.
515
+ * Its documents carry the AST-layer {@link AstDiagnostic}, never the wire `TDiagnostic`:
516
+ * {@link encoder} converts one into the other on the way out.
460
517
  */
461
- protected readonly modelService: ModelService<AstNode, unknown, TTransfer>;
518
+ protected readonly modelService: ModelService<AstNode, AstDiagnostic, TTransfer>;
462
519
 
463
520
  constructor(
464
521
  protected readonly connection: MessageConnection,
465
- protected readonly services: ServerSharedServices<TProject>,
522
+ protected readonly services: ServerSharedServices<TProject, TDiagnostic>,
466
523
  options: DataServerOptions = {}
467
524
  ) {
468
525
  this.options = this.resolveOptions(options);
469
526
  this.tracer = this.services.Tracer.for(options.logName ?? 'DataServer').trace('instantiated');
470
527
  // 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.
528
+ // `services.model.ModelService` with their own subclasses.
473
529
  const { TransferEncoder: encoder, ModelService: modelService } = this.services.model;
474
530
  if (!encoder || !modelService) {
475
531
  throw new Error(
@@ -477,8 +533,8 @@ export class DataServer<
477
533
  'Did you compose `createServerSharedModule(ctx)` into your shared module?'
478
534
  );
479
535
  }
480
- this.encoder = encoder as TransferEncoder<TDiagnostic>;
481
- this.modelService = modelService as ModelService<AstNode, unknown, TTransfer>;
536
+ this.encoder = encoder;
537
+ this.modelService = modelService;
482
538
  const excluded = new Set<string>(this.options.excludedMethods);
483
539
  const registeredMethods = [
484
540
  ...DATA_SERVER_PROTOCOL_METHODS,
@@ -500,37 +556,37 @@ export class DataServer<
500
556
  // framework's own rejections.
501
557
  renderErrorMessage: error => this.services.MessageRenderer.renderError(error)
502
558
  });
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();
559
+ this.disposables.push(this.modelService.onModelDeleted(event => this.dispatchDeleteEvent(event)));
560
+ this.subscribeToModelUpdates();
510
561
  this.subscribeToTextDocumentSaves();
511
- this.subscribeToTextDocumentCloses();
562
+ this.subscribeToDirtyChanges();
563
+ this.subscribeToModelReleases();
512
564
  this.subscribeToProjectManager();
513
565
  // Self-register teardown so an adopter that keeps no reference to the
514
566
  // server still releases per-connection state, with no lifecycle hook of
515
- // its own. See `dispose`.
516
- this.disposables.push(connection.onClose(() => this.dispose()));
567
+ // its own. The client did not close anything itself, so its sessions end
568
+ // as lost and each document it was the last to have open waits out the
569
+ // store's release grace. See `dispose`.
570
+ this.disposables.push(connection.onClose(() => this.dispose('lost')));
517
571
  }
518
572
 
519
573
  /**
520
574
  * 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.
575
+ * handlers and every listener — and clear {@link uriWatchRecords}, so a
576
+ * long-lived shared services bundle does not retain per-connection memory
577
+ * after the connection closes. Also ends every session this connection registered, which closes
578
+ * every document it has open: the SHARED store's state rather than this
579
+ * server's, which outlives the connection unless released here.
527
580
  *
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.
581
+ * Idempotent: subsequent calls are no-ops. Runs by itself when the
582
+ * connection closes, so adopters who don't hold a reference still get
583
+ * per-connection cleanup; adopters that DO hold a reference may call
584
+ * `dispose()` directly for early teardown. The sessions end with `cause`:
585
+ * `'closed'` releases every document whose last open they close at once,
586
+ * and `'lost'`, the connection's own close, lets each wait out the release
587
+ * grace.
532
588
  */
533
- dispose(): void {
589
+ dispose(cause: SessionEndCause = 'closed'): void {
534
590
  this.disposed = true;
535
591
  // Release an in-flight interactive capture so the process-wide inspector
536
592
  // singleton is not left active after the connection closes.
@@ -539,104 +595,185 @@ export class DataServer<
539
595
  this.activeProfile = undefined;
540
596
  void capture.stop({}).catch(() => undefined);
541
597
  }
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.
598
+ // AFTER `disposables.dispose()`, deliberately: ending a session releases
599
+ // each document it was the last to hold, and this server's own
600
+ // `onModelReleased` subscription would otherwise mark a release broadcast
601
+ // for a connection that is already gone.
545
602
  this.disposables.dispose();
546
- this.closeOpenDocuments();
547
- this.subscriptions.clear();
548
- this.lastEmittedFingerprint.clear();
549
- this.pendingRevertBroadcasts.clear();
603
+ for (const [clientId, session] of this.clientSessions) {
604
+ this.endSession(clientId, session, cause);
605
+ }
606
+ this.uriWatchRecords.clear();
607
+ this.watchKeys.clear();
550
608
  }
551
609
 
552
610
  // ============================================================
553
- // DataServerProtocol implementation — delegates to ModelService.
611
+ // DataServerProtocol implementation
554
612
  // ============================================================
555
613
  //
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);
614
+ // Reads go to `ModelService`; a request that opens or writes a document
615
+ // goes to the session this connection registered under its `clientId`.
616
+ // Each answer is encoded on the wire boundary via
617
+ // `encoder.astDocumentToTransferDocument`. To change how documents are
618
+ // read, override on the ModelService subclass; to change how they are
619
+ // written, on the session class the bound `ClientSessionFactory` builds.
620
+
621
+ async createSession(args: CreateSessionArgs): Promise<CreatedSession> {
622
+ // After teardown nothing would ever end a session registered now, and its
623
+ // id would stay taken for the life of the process.
624
+ if (this.disposed) {
625
+ throw new SessionClosedError(args.clientId);
575
626
  }
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) };
627
+ const session = this.modelService.createSession(args.label, args.clientId, { resumeToken: args.resumeToken });
628
+ this.clientSessions.set(args.clientId, session);
629
+ // A session also ends without closeSession, as when a client registers
630
+ // again on another connection, and its watches and entry here would
631
+ // otherwise outlive it.
632
+ session.onDidDispose(cause => {
633
+ // A session class may report its end late, once a replacement holds
634
+ // the id here; the replacement's entry and watches stay.
635
+ if (this.clientSessions.get(args.clientId) !== session) {
636
+ return;
637
+ }
638
+ this.dropWatches(args.clientId);
639
+ this.clientSessions.delete(args.clientId);
640
+ this.tracer.debug(`Forget session ${args.clientId}: it ended as ${cause}`);
641
+ });
642
+ return { epoch: this.epoch };
589
643
  }
590
644
 
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);
645
+ /**
646
+ * The epoch this server numbers versions in, which every registration and
647
+ * document names: the text store's, so every connection to the server
648
+ * answers the same one.
649
+ */
650
+ protected get epoch(): string {
651
+ return this.services.workspace.TextDocuments.epoch;
598
652
  }
599
653
 
600
654
  /**
601
- * Drop the recorded hold for `(uri, clientId)` so {@link dispose} does not
602
- * close it a second time. Idempotent.
655
+ * Refuse a write whose base version is numbered in another epoch, as a
656
+ * restarted server's predecessor numbered it. Versions start afresh with
657
+ * each epoch, so the session's gate would take the version for text the
658
+ * writer never saw. Checked once the session is found, before the open
659
+ * check and every other check of its documents.
603
660
  */
604
- protected forgetOpenDocument(uri: string, clientId: string): void {
605
- const holders = this.openedDocuments.get(uri);
606
- if (!holders) {
607
- return;
661
+ protected assertEpoch(
662
+ uri: string,
663
+ gate: BaseVersionGate,
664
+ message: typeof STALE_BASE_VERSION_UPDATE | typeof STALE_BASE_VERSION_SAVE = STALE_BASE_VERSION_UPDATE
665
+ ): void {
666
+ if (isModelVersion(gate.baseVersion) && gate.baseEpoch !== this.epoch) {
667
+ this.tracer.withUri(uri).debug(`Conflict: base v${gate.baseVersion} is numbered in epoch ${gate.baseEpoch}, not ${this.epoch}`);
668
+ throw new ConflictError(uri, gate.baseVersion, this.services.workspace.TextDocuments.version(uri), message);
608
669
  }
609
- holders.delete(clientId);
610
- if (holders.size === 0) {
611
- this.openedDocuments.delete(uri);
670
+ }
671
+
672
+ /** End a session this connection registered. */
673
+ async closeSession(args: CloseSessionArgs): Promise<void> {
674
+ const session = this.clientSessions.get(args.clientId);
675
+ if (session) {
676
+ this.endSession(args.clientId, session);
612
677
  }
613
678
  }
614
679
 
680
+ /** End `session`, registered here under `clientId`, with `cause`, and drop its watches on this connection. */
681
+ protected endSession(
682
+ clientId: string,
683
+ session: ClientSession<AstNode, AstDiagnostic, TTransfer>,
684
+ cause: SessionEndCause = 'closed'
685
+ ): void {
686
+ this.dropWatches(clientId);
687
+ session.dispose(cause);
688
+ }
689
+
690
+ protected dropWatches(clientId: string): void {
691
+ this.watchKeys.delete(clientId);
692
+ for (const [uri, record] of this.uriWatchRecords) {
693
+ if (record.watchers.delete(clientId)) {
694
+ this.pruneUriWatchRecord(uri);
695
+ }
696
+ }
697
+ }
698
+
699
+ async createModelDocument(args: CreateModelDocumentArgs): Promise<TransferCreatedDocument<TTransfer, TDiagnostic>> {
700
+ const session = this.requireSession(args.clientId);
701
+ const version = await session.create(args.uri, args.text);
702
+ const snapshot = await this.openedSnapshot(args.uri, () => session.close(args.uri));
703
+ return { ...snapshot, created: { version: asModelVersion(version) } };
704
+ }
705
+
706
+ async updateModelDocuments(args: TransferUpdateDocumentsArgs<TTransfer>): Promise<TransferWrittenDocument<TTransfer, TDiagnostic>[]> {
707
+ const session = this.requireSession(args.clientId);
708
+ // Every one before any applies, so a set stays all or none.
709
+ args.updates.forEach(update => this.assertEpoch(update.uri, update));
710
+ const astDocuments = await session.updateAll({
711
+ updates: args.updates.map(update => this.toSessionWrite(update)),
712
+ write: args.write
713
+ });
714
+ // A page that reads unsaved work from its editor sees the writes only once their text reaches it.
715
+ await Promise.all(args.updates.map(update => this.modelService.settleLanguageClient(update.uri)));
716
+ return astDocuments.map(astDocument => ({ ...this.encodeDocument(astDocument), written: astDocument.written }));
717
+ }
718
+
615
719
  /**
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.
720
+ * The session write a wire write request carries. Only the fields a session
721
+ * takes: a field an adopter adds to a request reaches the session only
722
+ * through an override of this.
629
723
  */
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
- }
724
+ protected toSessionWrite(request: Omit<TransferUpdateDocumentArgs<TTransfer>, 'clientId'>): ClientSessionWriteArgs<TTransfer> {
725
+ return { uri: request.uri, model: request.model, baseVersion: request.baseVersion, restore: request.restore, write: request.write };
726
+ }
727
+
728
+ /**
729
+ * The session this connection registered under `clientId`, which every
730
+ * document request acts as. An id that is not one fails with the
731
+ * closed-session code: opening or writing under it would leave an open that
732
+ * no session end ever closes.
733
+ */
734
+ protected requireSession(clientId: string): ClientSession<AstNode, AstDiagnostic, TTransfer> {
735
+ const session = this.clientSessions.get(clientId);
736
+ if (!session) {
737
+ throw new SessionClosedError(clientId, 'The client session is not registered on this connection.');
738
+ }
739
+ return session;
740
+ }
741
+
742
+ async openModelDocument(args: Pick<OpenModelArgs, 'uri' | 'clientId' | 'options'>): Promise<TransferDocument<TTransfer, TDiagnostic>> {
743
+ const session = this.requireSession(args.clientId);
744
+ const wasOpen = this.services.workspace.TextDocuments.isOpenInClient(args.uri, args.clientId);
745
+ await session.open(args.uri, args.options);
746
+ // A repeat open keeps the earlier one, which is still in use.
747
+ return this.openedSnapshot(args.uri, wasOpen ? undefined : () => session.close(args.uri));
748
+ }
749
+
750
+ /**
751
+ * The snapshot a session's open or create answers with. When the read
752
+ * fails, `rollback` undoes the open this call made before the failure is
753
+ * rethrown: the caller sees no open, so nothing on its side would ever close
754
+ * it.
755
+ */
756
+ protected async openedSnapshot(uri: string, rollback?: () => Promise<void>): Promise<TransferDocument<TTransfer, TDiagnostic>> {
757
+ try {
758
+ return await this.getModelDocument({ uri });
759
+ } catch (error: unknown) {
760
+ // The open's failure is the one the caller needs, not the cleanup's.
761
+ await rollback?.().catch(() => undefined);
762
+ throw error;
637
763
  }
638
764
  }
639
765
 
766
+ async closeModelDocument(args: CloseModelArgs): Promise<void> {
767
+ // Through the handle, so a close under an ended session fails as that
768
+ // session's calls do, and a session class's own close is honoured.
769
+ const session = this.requireSession(args.clientId);
770
+ // Closing also releases the watch for (uri, clientId) — a forgotten
771
+ // unwatch would otherwise leak phase-event dispatch until the connection
772
+ // closes. Idempotent: a close without a prior watch is a no-op.
773
+ await this.unwatchModelDocument({ uri: args.uri, clientId: args.clientId });
774
+ await session.close(args.uri);
775
+ }
776
+
640
777
  async getModelDocument(args: GetModelDocumentArgs): Promise<TransferDocument<TTransfer, TDiagnostic>> {
641
778
  // Smart dispatch: a warm document (already in LangiumDocuments) is
642
779
  // returned at its settle phase without a redundant build; a cold URI
@@ -649,77 +786,158 @@ export class DataServer<
649
786
  // unsubscribed caller that needs diagnostics inline passes
650
787
  // `includeDiagnostics: true` to settle at `Validated` instead.
651
788
  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>;
789
+ try {
790
+ return this.encodeDocument(await this.modelService.ensureDocumentState(args.uri, state));
791
+ } catch (error: unknown) {
792
+ // The protocol answers a read of a URI with no document with an
793
+ // envelope that has no model.
794
+ if (this.isMissingDocument(error, args.uri)) {
795
+ return this.envelope(UriUtils.toUri(args.uri));
796
+ }
797
+ throw error;
798
+ }
654
799
  }
655
800
 
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>;
801
+ async updateModelDocument(args: TransferUpdateDocumentArgs<TTransfer>): Promise<TransferWrittenDocument<TTransfer, TDiagnostic>> {
802
+ const session = this.requireSession(args.clientId);
803
+ this.assertEpoch(args.uri, args);
804
+ const astDocument = await session.update(this.toSessionWrite(args));
805
+ // A page that reads unsaved work from its editor sees the write only once the text reaches it.
806
+ await this.modelService.settleLanguageClient(args.uri);
807
+ return { ...this.encodeDocument(astDocument), written: astDocument.written };
659
808
  }
660
809
 
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>;
810
+ async saveModelDocument(args: TransferSaveDocumentArgs<TTransfer>): Promise<TransferSavedDocument<TTransfer, TDiagnostic>> {
811
+ const session = this.requireSession(args.clientId);
812
+ this.assertEpoch(args.uri, args, STALE_BASE_VERSION_SAVE);
813
+ const astDocument = await session.save(this.toSessionWrite(args));
814
+ await this.modelService.settleLanguageClient(args.uri);
815
+ return { ...this.encodeDocument(astDocument), persisted: astDocument.persisted };
816
+ }
817
+
818
+ async persistModelDocument(args: TransferPersistDocumentArgs): Promise<TransferSavedDocument<TTransfer, TDiagnostic>> {
819
+ const session = this.requireSession(args.clientId);
820
+ this.assertEpoch(args.uri, args, STALE_BASE_VERSION_SAVE);
821
+ const version = await session.persist({ uri: args.uri, baseVersion: args.baseVersion });
822
+ await this.modelService.settleLanguageClient(args.uri);
823
+ const astDocument = await this.modelService.ensureDocumentState(args.uri, this.services.workspace.DocumentBuilder.finalBuildState());
824
+ return { ...this.encodeDocument(astDocument), persisted: { version } };
664
825
  }
665
826
 
666
827
  /**
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.
828
+ * Delete the document through the session, then release the session's
829
+ * watch of it, as {@link closeModelDocument} does. Released only after the
830
+ * delete: one that fails leaves the session its open and its watch. Other
831
+ * sessions' watches survive, as for any deletion.
832
+ */
833
+ async deleteModelDocument(args: TransferDeleteDocumentArgs): Promise<void> {
834
+ const session = this.requireSession(args.clientId);
835
+ this.assertEpoch(args.uri, args);
836
+ // Keyed before the delete: once the file is gone, a symlink to it no
837
+ // longer resolves to the key the watch is under.
838
+ const uri = this.canonicalKey(args.uri);
839
+ await session.delete({ uri: args.uri, baseVersion: args.baseVersion });
840
+ await this.unwatchModelDocument({ uri, clientId: args.clientId });
841
+ }
842
+
843
+ /**
844
+ * Record a watch for `(uri, clientId)` in {@link uriWatchRecords}.
845
+ * Subsequent phase events on `uri` fan out to the wired
846
+ * `clientProxy.onDocumentUpdated`. The bidirectional pattern means the
847
+ * event channel is the client notification surface, not a returned
848
+ * handle.
849
+ *
850
+ * Also baselines the URI's emission fingerprint (see
851
+ * {@link dispatchPhaseEvent}) to the document's current state when the
852
+ * first watcher for the URI registers. This guarantees that any phase event firing immediately after
853
+ * the watch with no observable change is suppressed — watchers obtain
854
+ * initial state via `getModelDocument` (or {@link openModelDocument}) and
855
+ * do not need a redundant phase notification for that same state.
672
856
  *
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.
857
+ * Answers once the document is loaded as a read loads it, with the key
858
+ * events name it by and whether a document exists, taken after the watch
859
+ * is in place and the load, so an event sent while it loads is older.
680
860
  */
681
- async watchModelDocument(args: WatchModelDocumentArgs): Promise<void> {
861
+ async watchModelDocument(args: WatchModelDocumentArgs): Promise<TransferWatchedDocument> {
682
862
  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));
863
+ const keys = this.watchKeys.get(args.clientId) ?? new Map<string, string>();
864
+ this.watchKeys.set(args.clientId, keys.set(args.uri, uri));
865
+ const record = this.uriWatchRecords.get(uri) ?? { watchers: new Set<string>() };
866
+ this.uriWatchRecords.set(uri, record);
867
+ record.watchers.add(args.clientId);
868
+ if (record.fingerprint === undefined) {
869
+ const document = this.modelService.getDocument(args.uri);
870
+ if (document && !this.services.workspace.ModelLedger.isPlaceholder(document.parseResult.value)) {
871
+ record.fingerprint = this.computeDocumentFingerprint(document.parseResult.value, this.encoder.toTransferDocument(document));
872
+ record.fingerprintVersion = this.services.workspace.ModelLedger.versionOf(document.parseResult.value);
693
873
  }
694
874
  }
875
+ // As for a read, only a missing document counts as none. A file briefly
876
+ // unreadable leaves the builder's placeholder, and is there all the same.
877
+ // Whether it exists, the text held and the position of the last event
878
+ // sent are read in one synchronous run, so an event sent after is past
879
+ // the position and moves the text past the state answered.
880
+ const answer = (exists: boolean): TransferWatchedDocument => {
881
+ const text = exists ? this.serverTextState(args.uri) : undefined;
882
+ return { uri, epoch: this.epoch, exists, sequence: this.eventSequence, ...(text ? { text } : {}) };
883
+ };
884
+ return this.modelService.ensureDocumentState(args.uri).then(
885
+ () => answer(this.modelService.getDocument(args.uri) !== undefined),
886
+ (error: unknown) => answer(!this.isMissingDocument(error, args.uri))
887
+ );
695
888
  }
696
889
 
697
890
  /**
698
891
  * Remove a watch for `(uri, clientId)` previously created by
699
892
  * {@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.
893
+ * Dispatch for `uri` stops once no watchers remain, and the emission
894
+ * fingerprint goes with the last watcher (see {@link pruneUriWatchRecord}).
895
+ *
896
+ * A URI the client watched is unwatched under the key it resolved to then,
897
+ * from {@link watchKeys}, which still holds once its file is gone.
704
898
  */
705
899
  async unwatchModelDocument(args: WatchModelDocumentArgs): Promise<void> {
706
- const uri = this.canonicalKey(args.uri);
707
- const subscribers = this.subscriptions.get(uri);
708
- if (!subscribers) {
900
+ const keys = this.watchKeys.get(args.clientId);
901
+ const uri = keys?.get(args.uri) ?? this.canonicalKey(args.uri);
902
+ // Every spelling the client watched it under was this one watch.
903
+ keys?.forEach((key, spelling) => {
904
+ if (key === uri) {
905
+ keys.delete(spelling);
906
+ }
907
+ });
908
+ if (keys?.size === 0) {
909
+ this.watchKeys.delete(args.clientId);
910
+ }
911
+ if (this.uriWatchRecords.get(uri)?.watchers.delete(args.clientId)) {
912
+ this.pruneUriWatchRecord(uri);
913
+ }
914
+ }
915
+
916
+ /**
917
+ * Drop what `uri` no longer needs: the
918
+ * {@link DataServerUriWatchRecord.fingerprint} once no client watches it,
919
+ * and the whole record once it holds no
920
+ * {@link DataServerUriWatchRecord.releaseBroadcastPending} mark either.
921
+ * Every path that removes a watcher or consumes a mark ends here.
922
+ */
923
+ protected pruneUriWatchRecord(uri: string): void {
924
+ const record = this.uriWatchRecords.get(uri);
925
+ if (!record || record.watchers.size > 0) {
709
926
  return;
710
927
  }
711
- subscribers.delete(args.clientId);
712
- if (subscribers.size === 0) {
713
- this.subscriptions.delete(uri);
714
- this.lastEmittedFingerprint.delete(uri);
928
+ if (record.releaseBroadcastPending) {
929
+ record.fingerprint = undefined;
930
+ record.fingerprintVersion = undefined;
931
+ } else {
932
+ this.uriWatchRecords.delete(uri);
715
933
  }
716
934
  }
717
935
 
718
936
  /**
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
937
+ * Canonicalise a URI string for use as a {@link uriWatchRecords} key, via the
938
+ * shared `DocumentUriPolicy`. Callers may send non-canonical URIs
939
+ * (drive-letter casing, percent-encoding differences, or a symlink path)
940
+ * over the wire; the dispatch side keys by
723
941
  * `document.uri.toString()` from `LangiumDocuments`, so writer keys must
724
942
  * canonicalise to the same form or events silently fail to deliver.
725
943
  * Routing through the seam (rather than a bare `UriUtils.normalize`)
@@ -729,26 +947,24 @@ export class DataServer<
729
947
  * LSP head resolves.
730
948
  *
731
949
  * Adopters can still override for head-specific URI policy by subclassing.
950
+ * The result keys watches only; a document is looked up by its own URI.
732
951
  */
733
952
  protected canonicalKey(uri: string): string {
734
953
  return this.services.workspace.DocumentUriPolicy.canonicalUri(uri);
735
954
  }
736
955
 
737
956
  async getProjects(): Promise<readonly TProject[]> {
738
- // Pass-through: `ProjectManager<TProject>` already produces `TProject`
739
- // (the shared services are parameterised over the same generic).
740
957
  // The framework reads `id` for registry identity and `dependencies`
741
958
  // for the visibility closure; every other field rides on the
742
959
  // JSON-RPC envelope as adopter-defined wire metadata.
743
- return this.services.workspace.ProjectManager.getProjects();
960
+ return this.services.workspace.ProjectManager.getProjects().map(project => this.encodeProject(project));
744
961
  }
745
962
 
746
963
  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));
964
+ // Membership logic belongs to the adopter's `ProjectManager`; the
965
+ // data-server forwards the URI without interpretation.
966
+ const project = this.services.workspace.ProjectManager.getProject(UriUtils.toUri(args.uri));
967
+ return project && this.encodeProject(project);
752
968
  }
753
969
 
754
970
  /**
@@ -768,27 +984,60 @@ export class DataServer<
768
984
  // (see resolveReferenceServices) and delegate.
769
985
  // ============================================================
770
986
 
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;
987
+ /**
988
+ * Settle the build before a reference question is answered from the index.
989
+ *
990
+ * A query issued right after a model update otherwise races the rebuild and
991
+ * reads an index missing the symbols it asks about. The cost is not confined
992
+ * to this call: a scope built inside that window is cached, and the build's
993
+ * remaining documents link against it.
994
+ *
995
+ * Waits the source document specifically, but ONLY when one is loaded at
996
+ * that URI: a synthetic source can address a URI with no document (a
997
+ * directory URI, typically), where a per-URI `waitUntil` throws "No document
998
+ * found". Falls back to a global settle, matching the id-based
999
+ * `ElementSource` path.
1000
+ *
1001
+ * Bounded by {@link DataServerOptions.referenceSettleTimeoutMs}.
1002
+ */
1003
+ protected async awaitReferencesLinked(source: ReferenceSource): Promise<void> {
1004
+ const uri = isDocumentSource(source) || isSyntheticSource(source) ? UriUtils.toUri(source.uri) : undefined;
780
1005
  const waitUri = uri && this.services.workspace.LangiumDocuments.hasDocument(uri) ? uri : undefined;
781
- await this.services.workspace.DocumentBuilder.waitUntil(DocumentState.Linked, waitUri);
1006
+ const stopwatch = this.services.Clock.stopwatch();
1007
+ const linked = this.services.workspace.DocumentBuilder.waitUntil(DocumentState.Linked, waitUri);
1008
+ if ((await this.services.Clock.raceTimer(linked, this.options.referenceSettleTimeoutMs)) !== TIMED_OUT) {
1009
+ return;
1010
+ }
1011
+ const elapsedMs = Math.round(stopwatch.elapsedMs);
1012
+ // A per-URI wait resolves off a document-phase notification, so a
1013
+ // missed one strands a wait on a document that HAS reached the
1014
+ // phase. Re-read before failing; the workspace-wide wait has no
1015
+ // equivalent reading and can only reject.
1016
+ const document = waitUri ? this.services.workspace.LangiumDocuments.getDocument(waitUri) : undefined;
1017
+ if (waitUri && document && document.state >= DocumentState.Linked) {
1018
+ this.tracer
1019
+ .withUri(waitUri.toString())
1020
+ .warn(`Missed the 'Linked' notification after ${elapsedMs}ms; the document already reached it`);
1021
+ return;
1022
+ }
1023
+ throw referenceSettleTimeoutError(elapsedMs);
1024
+ }
1025
+
1026
+ async findReferenceCandidates(ctx: ReferenceContext): Promise<ReferenceCandidate[]> {
1027
+ await this.awaitReferencesLinked(ctx.source);
782
1028
  return this.resolveReferenceServices(ctx.source).CandidateProvider.find(ctx);
783
1029
  }
784
1030
 
785
- async resolveReference(ref: ReferenceRequest): Promise<ReferenceTarget<TTransfer> | undefined> {
1031
+ async resolveReference<TElement extends TransferElement = TransferElement>(
1032
+ ref: ReferenceRequest
1033
+ ): Promise<ReferenceTarget<TElement> | undefined> {
1034
+ await this.awaitReferencesLinked(ref.source);
786
1035
  const resolved = this.resolveReferenceServices(ref.source).CandidateProvider.resolveCandidate(ref);
787
1036
  if (!resolved) {
788
1037
  return undefined;
789
1038
  }
790
- const element = this.encoder.toTransfer(resolved.node) as unknown as TTransfer;
791
- return { ...resolved.candidate, element };
1039
+ // `TElement` is the caller's claim about the target's type; nothing checks it.
1040
+ return { ...resolved.candidate, element: this.encoder.toTransfer(resolved.node) as TElement };
792
1041
  }
793
1042
 
794
1043
  async findNextName(args: FindNextNameArgs): Promise<string> {
@@ -798,15 +1047,31 @@ export class DataServer<
798
1047
  // synthetic source — and the create-element flow driving this method is
799
1048
  // the one that produces directory URIs, on which a bare
800
1049
  // `getServices(uri)` throws while `findReferenceCandidates` succeeds.
801
- const nameProvider = this.resolveReferenceServices(ReferenceSource.synthetic(args.uri, args.type)).NameProvider;
1050
+ const source = ReferenceSource.synthetic(args.uri, args.type, args.language);
1051
+ // Mid-rebuild the names of documents not yet re-indexed are absent from
1052
+ // the taken set, and the caller PERSISTS what it is handed. Nothing
1053
+ // reserves the answer either, so a name is free of the collisions the
1054
+ // index knows about and of nothing else — two callers racing get the same
1055
+ // proposal, which only uniqueness at the write can catch.
1056
+ await this.awaitReferencesLinked(source);
1057
+ const nameProvider = this.resolveReferenceServices(source).NameProvider;
802
1058
  const tier = args.tier ?? 'project';
803
1059
  if (tier === 'public') {
804
1060
  return nameProvider.findNextProjectQualifiedName(args.type, args.proposal);
805
1061
  }
806
1062
  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;
1063
+ // Document-scoped uniqueness: the document root is the container. A
1064
+ // URI with no document has nothing to collide with, like a document
1065
+ // with no root.
1066
+ try {
1067
+ const document = await this.modelService.ensureDocumentState(args.uri);
1068
+ return document.root ? nameProvider.findNextName(args.type, args.proposal, document.root) : args.proposal;
1069
+ } catch (error: unknown) {
1070
+ if (this.isMissingDocument(error, args.uri)) {
1071
+ return args.proposal;
1072
+ }
1073
+ throw error;
1074
+ }
810
1075
  }
811
1076
  const project = this.services.workspace.ProjectManager.getProject(uri);
812
1077
  if (!project) {
@@ -843,67 +1108,32 @@ export class DataServer<
843
1108
  }
844
1109
 
845
1110
  /**
846
- * Pick the language that owns a reference source, in order:
1111
+ * Pick the language that owns a reference source: the registry's
1112
+ * `getLanguageFor` over the source's declared language, URI and AST type,
1113
+ * with an {@link isElementSource} routed by the document that holds the
1114
+ * element (see {@link findElementDocumentUri}) before its type, and
1115
+ * {@link fallbackReferenceLanguage}, adopter policy, last.
847
1116
  *
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.
1117
+ * An unregistered declared language falls through rather than failing: the
1118
+ * id crosses the wire, so a client built against a head with one more
1119
+ * grammar would otherwise lose queries the other signals answer.
863
1120
  */
864
1121
  protected resolveReferenceLanguage(source: ReferenceSource): HydraniumLanguageServices | undefined {
865
1122
  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
1123
  if (isElementSource(source)) {
879
- const documentUri = this.findElementDocumentUri(source);
880
- const byDocument = documentUri && registry.getServicesFor(documentUri);
881
- if (byDocument) {
882
- return byDocument;
883
- }
1124
+ // The index lookup runs only once the cheaper signals abstained.
1125
+ return (
1126
+ registry.getLanguageFor({}) ??
1127
+ registry.getLanguageFor({ target: this.findElementDocumentUri(source), type: source.type }) ??
1128
+ this.fallbackReferenceLanguage(source)
1129
+ );
884
1130
  }
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;
1131
+ const hint = isSyntheticSource(source)
1132
+ ? { languageId: source.language, target: source.uri, type: source.type }
1133
+ : isDocumentSource(source)
1134
+ ? { target: source.uri }
1135
+ : {};
1136
+ return registry.getLanguageFor(hint) ?? this.fallbackReferenceLanguage(source);
907
1137
  }
908
1138
 
909
1139
  /**
@@ -913,15 +1143,14 @@ export class DataServer<
913
1143
  * disambiguates names that repeat across types (honouring grammar
914
1144
  * subtyping through `AstReflection.isSubtype`).
915
1145
  *
916
- * Only reached on the multi-language, name-based path (step 3 of
917
- * {@link resolveReferenceLanguage}); single-language adopters return at
918
- * step 2.
1146
+ * Only reached in a multi-language workspace, once the declared language
1147
+ * and the URI have abstained.
919
1148
  *
920
1149
  * Abstains when the name matches elements in more than one DOCUMENT — the
921
1150
  * 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.
1151
+ * first match" would be file-watch order rather than an answer. Routing
1152
+ * then falls through to the element's type, which abstains on ties in the
1153
+ * same way, and finally to adopter policy.
925
1154
  */
926
1155
  protected findElementDocumentUri(source: ElementSource): URI | undefined {
927
1156
  const matches = this.services.workspace.IndexManager.getElementsByName(source.name, source.type);
@@ -956,58 +1185,132 @@ export class DataServer<
956
1185
  // Internal plumbing
957
1186
  // ============================================================
958
1187
 
1188
+ /**
1189
+ * Whether `error`, from waiting on `uri`'s document, means that `uri` has
1190
+ * no document: a URI with neither a file nor text builds nothing, and
1191
+ * Langium's wait after the build rejects for want of one, with a
1192
+ * `ServerCancelled` response error. Any other failure, such as a read
1193
+ * that fails before a document is registered, a cancelled wait or a
1194
+ * `ReentrantWriteLockError`, is the caller's to see.
1195
+ */
1196
+ protected isMissingDocument(error: unknown, uri: string): boolean {
1197
+ return isResponseError(error) && error.code === SERVER_CANCELLED && this.modelService.getDocument(uri) === undefined;
1198
+ }
1199
+
959
1200
  /**
960
1201
  * Build a {@link TransferDocument} envelope from the current document state,
961
1202
  * 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.
1203
+ * `TransferEncoder.toTransferDocument` for the walk). `fingerprint` is the
1204
+ * caller's {@link computeDocumentFingerprint} of that same state, which
1205
+ * spares hashing it twice.
970
1206
  */
971
- protected envelope(uri: URI): TransferDocument<TTransfer, TDiagnostic> {
1207
+ protected envelope(uri: URI, fingerprint?: string): TransferDocument<TTransfer, TDiagnostic> {
972
1208
  // Resolve through the model service's canonicalizing gateway rather than
973
1209
  // reaching into `LangiumDocuments` directly, so a divergent (symlink) URI
974
1210
  // still finds the document the build keys by its real path — and so the
975
1211
  // data-server never has to remember to canonicalize this lookup itself.
976
1212
  const document = this.modelService.getDocument(uri.toString());
977
- if (!document) {
1213
+ if (!document || this.services.workspace.ModelLedger.isPlaceholder(document.parseResult.value)) {
978
1214
  // 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
1215
+ // decides policy at its own layer; `model` is optional on the envelope
980
1216
  // 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
- };
1217
+ // override `envelope`. The builder's unparsed placeholder is no
1218
+ // document either: its root parses no text.
1219
+ return TransferDocument.absent<TTransfer, TDiagnostic>(uri.toString(), this.epoch);
988
1220
  }
989
- return this.encoder.toTransferDocument(document) as unknown as TransferDocument<TTransfer, TDiagnostic>;
1221
+ const encoded = this.encoder.toTransferDocument(document);
1222
+ return this.withServerState(encoded, fingerprint ?? this.computeDocumentFingerprint(document.parseResult.value, encoded));
990
1223
  }
991
1224
 
992
1225
  /**
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.
1226
+ * The transfer document a request answers with for `astDocument`, its model
1227
+ * hashed and the text stamped on it by {@link withServerState}. Every
1228
+ * document a request answers with goes through here, so none goes out
1229
+ * without them.
997
1230
  */
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
- );
1231
+ protected encodeDocument(astDocument: AstDocument<AstNode, AstDiagnostic>): TransferDocument<TTransfer, TDiagnostic> {
1232
+ const encoded = this.encoder.astDocumentToTransferDocument(astDocument);
1233
+ return this.withServerState(encoded, this.computeDocumentFingerprint(astDocument.root, encoded));
1234
+ }
1235
+
1236
+ /**
1237
+ * The project a client receives for `project`, unchanged by default. Every
1238
+ * project a request answers with or a change notification carries goes
1239
+ * through here, so a field derived for clients cannot miss one path. The
1240
+ * result crosses as JSON, so a value a client reads goes in a data field,
1241
+ * not a getter.
1242
+ */
1243
+ protected encodeProject(project: TProject): TProject {
1244
+ return project;
1245
+ }
1246
+
1247
+ /**
1248
+ * `encoded` with `hash` on its model and the text the server holds for it:
1249
+ * the store's while it holds the document, the build's otherwise. The text
1250
+ * is read when the document is sent rather than kept with the build: a save
1251
+ * changes `text.dirty` without a rebuild, and the store's text moves on before
1252
+ * the build that follows it.
1253
+ */
1254
+ protected withServerState(
1255
+ encoded: EncodedTransferDocument<TransferElement, TDiagnostic>,
1256
+ hash: string
1257
+ ): TransferDocument<TTransfer, TDiagnostic> {
1258
+ const text = this.serverTextState(encoded.uri);
1259
+ if (text && text.version > encoded.model.version) {
1260
+ this.tracer.withUri(encoded.uri).debug(`Send a model behind its text: model v${encoded.model.version} / text v${text.version}`);
1261
+ }
1262
+ return {
1263
+ uri: encoded.uri,
1264
+ epoch: this.epoch,
1265
+ heldWrites: this.heldWrites(encoded.uri, encoded.model.version),
1266
+ // The encoder returns the structural base; `TTransfer` is the adopter's
1267
+ // declaration of the roots its languages produce, and nothing checks it.
1268
+ model: { ...encoded.model, hash } as TransferModelSnapshot<TTransfer, TDiagnostic>,
1269
+ ...(text ? { text } : {})
1270
+ };
1271
+ }
1272
+
1273
+ /** Per session registered on this connection, the highest of its numbered writes of `uri` that `version` holds. */
1274
+ protected heldWrites(uri: string, version: number): Record<string, number> {
1275
+ const held: [string, number][] = [];
1276
+ for (const [clientId, session] of this.clientSessions) {
1277
+ const write = session.heldWrite(uri, version);
1278
+ if (write !== undefined) {
1279
+ held.push([clientId, write]);
1280
+ }
1281
+ }
1282
+ // Built from entries, so a session named `__proto__` is kept as a key.
1283
+ return Object.fromEntries(held);
1284
+ }
1285
+
1286
+ /**
1287
+ * The text the server holds for `uri`, as every envelope and a watch's
1288
+ * answer carry it: the store's while it holds the document, the build's
1289
+ * otherwise, and none for a document it has neither of.
1290
+ */
1291
+ protected serverTextState(uri: string): TextState | undefined {
1292
+ return this.services.workspace.TextDocuments.textState(uri) ?? this.builtTextState(uri);
1293
+ }
1294
+
1295
+ /**
1296
+ * The text a build read for a document the store never held, which is
1297
+ * clean: the store holds every text a client gave it.
1298
+ */
1299
+ protected builtTextState(uri: string): TextState | undefined {
1300
+ const textDocument = this.modelService.getDocument(uri)?.textDocument;
1301
+ return textDocument && { version: textDocument.version, hash: textHash(textDocument.getText()), dirty: false };
1302
+ }
1303
+
1304
+ /**
1305
+ * Follow every document at the configured
1306
+ * {@link DataServerOptions.subscriptionPhase}, once per connection rather
1307
+ * than once per watch, so the cost stays flat in the number of watches.
1308
+ */
1309
+ protected subscribeToModelUpdates(): void {
1310
+ const phase = this.options.subscriptionPhase;
1311
+ this.disposables.push(this.modelService.onModelUpdated(event => this.dispatchPhaseEvent(event), { phase }));
1312
+ // One message per build rather than one per document.
1313
+ this.disposables.push(this.modelService.onModelsBuilt(event => this.dispatchBuiltEvent(event.uris), { phase }));
1011
1314
  }
1012
1315
 
1013
1316
  /**
@@ -1030,7 +1333,7 @@ export class DataServer<
1030
1333
  *
1031
1334
  * Payload-free on purpose: URIs only, so a client re-reads what it displays
1032
1335
  * 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
1336
+ * property the watcher gate protects, and it is preserved here by
1034
1337
  * carrying no document rather than by gating the message.
1035
1338
  *
1036
1339
  * Quiet in the common case. Editing a document that an editor has open leaves
@@ -1038,15 +1341,9 @@ export class DataServer<
1038
1341
  * dependents produces nothing at all, and workspace initialisation produces
1039
1342
  * nothing because it does not build to this phase. The ceiling is a
1040
1343
  * 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
1344
  */
1048
- protected dispatchBuiltEvent(built: readonly LangiumDocument[]): void {
1049
- const uris = built.map(document => this.canonicalKey(document.uri.toString())).filter(uri => !this.subscriptions.has(uri));
1345
+ protected dispatchBuiltEvent(built: readonly CanonicalUri[]): void {
1346
+ const uris = built.map(uri => this.canonicalKey(uri)).filter(uri => !this.uriWatchRecords.get(uri)?.watchers.size);
1050
1347
  if (uris.length === 0) {
1051
1348
  return;
1052
1349
  }
@@ -1055,13 +1352,12 @@ export class DataServer<
1055
1352
  }
1056
1353
 
1057
1354
  /**
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`.
1355
+ * Follow every save so a single `onDocumentSaved` wire notification fires
1356
+ * for ANY save of a subscribed URI — regardless of whether the save
1357
+ * originated from the data-server's RPC `saveModelDocument`, the LSP head's
1358
+ * text-editor save, or any other client.
1063
1359
  *
1064
- * Architectural symmetry with {@link subscribeToDocumentBuilder}: every
1360
+ * Architectural symmetry with {@link subscribeToModelUpdates}: every
1065
1361
  * subscribed client sees every state change to documents they care about,
1066
1362
  * regardless of which client triggered it. Firing `onDocumentSaved` only
1067
1363
  * from the data-server's own RPC path is a bug, not an optimisation: an
@@ -1069,22 +1365,54 @@ export class DataServer<
1069
1365
  * clients, which never clear their dirty state.
1070
1366
  */
1071
1367
  protected subscribeToTextDocumentSaves(): void {
1072
- this.disposables.push(this.services.workspace.TextDocuments.onDidSave(event => this.dispatchSaveEvent(event)));
1368
+ this.disposables.push(this.modelService.onModelSaved(event => this.dispatchSaveEvent(event)));
1369
+ }
1370
+
1371
+ /** Relay each change of a document's dirty state; see {@link dispatchDirtyEvent}. */
1372
+ protected subscribeToDirtyChanges(): void {
1373
+ this.disposables.push(this.modelService.onDirtyChanged(event => this.dispatchDirtyEvent(event)));
1374
+ }
1375
+
1376
+ /**
1377
+ * Send a dirty flip to the connection, gated on the URI's watchers as
1378
+ * {@link dispatchSaveEvent} is: the answer at any one moment travels on
1379
+ * every document sent, so a client that watches nothing reads it there.
1380
+ */
1381
+ protected dispatchDirtyEvent(event: ModelDirtyChangedEvent): void {
1382
+ const uri = this.canonicalKey(event.uri);
1383
+ if (this.uriWatchRecords.get(uri)?.watchers.size) {
1384
+ this.clientProxy.onDocumentDirtyChanged({ uri, text: event.text });
1385
+ }
1073
1386
  }
1074
1387
 
1075
1388
  /**
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.
1389
+ * Mark each document the store releases, so {@link dispatchPhaseEvent}
1390
+ * broadcasts the build that follows even without a watcher. By default
1391
+ * that build reverts the document to its disk content, discarding unsaved
1392
+ * in-session edits, and the last close typically also
1393
+ * removed the last watcher: without the broadcast, a consumer that only
1394
+ * ever fetches via `getModelDocument` keeps showing the discarded state.
1395
+ * The broadcast is de-duplicated against the fingerprint while a watcher
1396
+ * holds one, so a close whose disk state equals the last emitted state
1397
+ * stays silent; with no watcher there is no fingerprint, and every release
1398
+ * build is sent. The first build after the release consumes the mark, and
1399
+ * goes out as the release unless it is stamped past the version a client
1400
+ * opened the document at again: a client's write or an integrity repair
1401
+ * that rides it keeps its author.
1402
+ *
1403
+ * A release, not the close itself: a document whose last client lost its
1404
+ * connection is released only once the release grace runs out, or when
1405
+ * another client opens it meanwhile, and not at all when a client lost
1406
+ * from it opens it again within its own grace.
1080
1407
  */
1081
- protected subscribeToTextDocumentCloses(): void {
1082
- const textDocuments = this.services.workspace.TextDocuments;
1408
+ protected subscribeToModelReleases(): void {
1083
1409
  this.disposables.push(
1084
- textDocuments.onDidClose(event => {
1085
- if (!textDocuments.isOpenInAnyClient(event.document.uri)) {
1086
- this.pendingRevertBroadcasts.add(this.canonicalKey(event.document.uri));
1087
- }
1410
+ this.modelService.onModelReleased(event => {
1411
+ const uri = this.canonicalKey(event.uri);
1412
+ const record = this.uriWatchRecords.get(uri) ?? { watchers: new Set<string>() };
1413
+ record.releaseBroadcastPending = true;
1414
+ this.uriWatchRecords.set(uri, record);
1415
+ this.tracer.withUri(uri).debug('Release marked: the first build after it is broadcast, watched or not');
1088
1416
  })
1089
1417
  );
1090
1418
  }
@@ -1095,7 +1423,7 @@ export class DataServer<
1095
1423
  *
1096
1424
  * **Deliberately ungated, where {@link dispatchPhaseEvent} and
1097
1425
  * {@link dispatchSaveEvent} are gated.** Those two carry a built document and
1098
- * fire on every build, so the subscription map is what keeps bandwidth
1426
+ * fire on every build, so the watcher gate is what keeps bandwidth
1099
1427
  * proportional to what a client asked for. A deletion is neither: it is one
1100
1428
  * URI, it is rare, and it reports the workspace's STRUCTURE rather than a
1101
1429
  * document's content. Gating it forces any consumer that displays the
@@ -1104,56 +1432,48 @@ export class DataServer<
1104
1432
  * no way to run: its workspace lives behind the head. A client that does not
1105
1433
  * care filters on the URI, which costs it a comparison.
1106
1434
  *
1107
- * The precedent is the last-close revert broadcast (see
1108
- * {@link pendingRevertBroadcasts}), where the same judgement was already made
1435
+ * The precedent is the release broadcast (see
1436
+ * {@link subscribeToModelReleases}), where the same judgement was already made
1109
1437
  * in the other direction: a transition that matters enough is delivered
1110
1438
  * without a subscription.
1111
1439
  *
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.
1440
+ * A deletion is announced before the build that follows it, so this goes
1441
+ * out ahead of the `'rebuilt'` events for the dependents whose references
1442
+ * the deletion just broke.
1121
1443
  *
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.
1444
+ * The URI's fingerprint and release mark are dropped: they describe a
1445
+ * document that no longer exists. A kept fingerprint is adopted as the
1446
+ * baseline and suppresses the first emit after the file returns with its
1447
+ * previous content.
1128
1448
  *
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.
1449
+ * The watchers SURVIVE: a recreated file resumes delivering to them with no
1450
+ * re-subscription, and a client that answers the deletion by closing
1451
+ * releases the watch through `closeModelDocument` anyway. A session that
1452
+ * deleted the document has its own released by {@link deleteModelDocument}.
1132
1453
  */
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 });
1454
+ protected dispatchDeleteEvent(event: ModelDeletedEvent): void {
1455
+ const uri = this.canonicalKey(event.uri);
1456
+ const record = this.uriWatchRecords.get(uri);
1457
+ if (record) {
1458
+ record.fingerprint = undefined;
1459
+ record.fingerprintVersion = undefined;
1460
+ record.releaseBroadcastPending = undefined;
1461
+ record.sentVersion = undefined;
1462
+ this.pruneUriWatchRecord(uri);
1139
1463
  }
1464
+ this.clientProxy.onDocumentDeleted({ uri, sourceClientId: event.sourceClientId, sequence: ++this.eventSequence });
1140
1465
  }
1141
1466
 
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).
1467
+ /** Fan out a save event for the document's URI, gated on the URI's watchers. */
1468
+ protected dispatchSaveEvent(event: AstDocumentSavedEvent<AstNode>): void {
1149
1469
  const uri = this.canonicalKey(event.document.uri);
1150
- if (!this.subscriptions.has(uri)) {
1470
+ if (!this.uriWatchRecords.get(uri)?.watchers.size) {
1151
1471
  return;
1152
1472
  }
1153
- const response = this.envelope(UriUtils.toUri(uri));
1473
+ const response = this.envelope(UriUtils.toUri(event.document.uri));
1154
1474
  const wireEvent: TransferDocumentSavedEvent<TTransfer, TDiagnostic> = {
1155
1475
  document: response,
1156
- sourceClientId: event.clientId
1476
+ sourceClientId: event.sourceClientId
1157
1477
  };
1158
1478
  this.clientProxy.onDocumentSaved(wireEvent);
1159
1479
  }
@@ -1167,109 +1487,115 @@ export class DataServer<
1167
1487
  * out to multiple local subscribers with an `Emitter<T>` — the
1168
1488
  * framework deliberately does NOT promise multi-listener semantics.
1169
1489
  *
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.
1490
+ * An event for a URI no client watches is NOT sent over the wire, so
1491
+ * bandwidth scales with watched URIs rather than with phase events; a
1492
+ * pending release mark is the exception (see
1493
+ * {@link subscribeToModelReleases}).
1494
+ *
1495
+ * An event for a rebuild with no observable change since the last emit is
1496
+ * suppressed against the URI's fingerprint and the version it was taken at;
1497
+ * a new version with the same fingerprint goes out with the same
1498
+ * `model.hash`, so a watcher still learns the version. Such rebuilds are routine: a
1499
+ * second client attaching to an open document makes
1500
+ * `HydraniumTextDocuments.refreshContent` fire `onDidChangeContent` purely
1501
+ * to re-trigger the build, and Langium rebuilds a document when a
1502
+ * dependency changes even if its diagnostics come out the same. Sent at a
1503
+ * version this head has not sent, they reach watchers as `'changed'`, and a
1504
+ * watcher that reads `'changed'` as another client's write and resets its
1505
+ * root to the server view loses the user's edits. The first {@link watchModelDocument} baselines the
1506
+ * fingerprint, so the first event after a watch is de-duplicated against
1507
+ * the state the watcher just fetched; it goes with the last watcher, so the
1508
+ * next first watch re-baselines rather than adopting a stale digest. A
1509
+ * digest keeps its memory constant in document size; see
1510
+ * {@link computeDocumentFingerprint} for the inputs.
1177
1511
  */
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.
1512
+ protected dispatchPhaseEvent(event: ModelUpdatedEvent<AstNode>): void {
1513
+ const uri = this.canonicalKey(event.document.uri);
1514
+ const record = this.uriWatchRecords.get(uri);
1515
+ const afterRelease = record?.releaseBroadcastPending === true;
1516
+ if (!record || (record.watchers.size === 0 && !afterRelease)) {
1185
1517
  return;
1186
1518
  }
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) {
1519
+ if (afterRelease) {
1520
+ // Consumed even when watchers exist — the regular dispatch below
1521
+ // serves them, and the mark's attribution is more precise than the
1522
+ // post-close unknown client the manager's attribution would yield.
1523
+ // With no watcher the record goes now, so the fingerprint written
1524
+ // below lands on a detached record and the next first watch
1525
+ // re-baselines instead of adopting it.
1526
+ record.releaseBroadcastPending = undefined;
1527
+ this.pruneUriWatchRecord(uri);
1528
+ }
1529
+ // The encoder reads the built document, which the event's holds for this
1530
+ // synchronous run.
1531
+ const document = this.modelService.getDocument(event.document.uri);
1532
+ if (!document) {
1194
1533
  return;
1195
1534
  }
1196
- const fingerprint = this.computeDocumentFingerprint(document);
1197
- if (this.lastEmittedFingerprint.get(uri) === fingerprint) {
1535
+ const fingerprint = this.computeDocumentFingerprint(document.parseResult.value, this.encoder.toTransferDocument(document));
1536
+ const stamped = this.services.workspace.ModelLedger.versionOf(document.parseResult.value);
1537
+ if (record.fingerprint === fingerprint && record.fingerprintVersion === stamped) {
1198
1538
  // A rebuild that produced no observable change since the last emit —
1199
1539
  // suppressed so RPC subscribers don't see a no-op broadcast. Logged at
1200
1540
  // debug so a *needed* re-broadcast wrongly suppressed by this dedup
1201
1541
  // (the failure mode the fingerprint strategy must avoid) is visible.
1202
- this.tracer.withUri(uri).debug(`Suppress onDocumentUpdated v${document.textDocument.version}: fingerprint unchanged`);
1542
+ this.tracer.withUri(uri).debug(`Suppress onDocumentUpdated v${stamped}: fingerprint and version unchanged`);
1203
1543
  return;
1204
1544
  }
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)
1545
+ record.fingerprint = fingerprint;
1546
+ record.fingerprintVersion = stamped;
1547
+ // The event's attribution, so this head names the same client as the
1548
+ // in-process heads do for one build, except for a version this head
1549
+ // already sent; see `DataServerUriWatchRecord.sentVersion`.
1550
+ const version = this.modelService.isOpen(event.document.uri) ? document.textDocument.version : undefined;
1551
+ const { reason, sourceClientId } =
1552
+ version !== undefined && record.sentVersion === version
1553
+ ? { reason: 'rebuilt' as const, sourceClientId: UNKNOWN_CLIENT_ID }
1554
+ : event;
1555
+ record.sentVersion = version;
1556
+ // The stamp, not the live version: a write after the build's parse moves
1557
+ // only the latter. At the opened version too, which a reopen with the
1558
+ // released text keeps.
1559
+ const openedVersion = afterRelease ? this.modelService.openedVersion(event.document.uri) : undefined;
1560
+ const releaseBuild = afterRelease && (openedVersion === undefined || stamped <= openedVersion);
1561
+ const wireEvent: TransferDocumentUpdatedEvent<TTransfer, TDiagnostic> = {
1562
+ document: this.envelope(document.uri, fingerprint),
1563
+ sourceClientId: releaseBuild ? DOCUMENT_RELEASE_CLIENT_ID : sourceClientId,
1564
+ reason,
1565
+ sequence: ++this.eventSequence
1210
1566
  };
1567
+ const mark = afterRelease ? `, release mark: opened v${openedVersion ?? '-'}, attributed ${sourceClientId}` : '';
1211
1568
  this.tracer
1212
1569
  .withUri(uri)
1213
- .debug(`Emit onDocumentUpdated v${event.document.version} (reason=${event.reason}, sourceClientId=${event.sourceClientId})`);
1214
- this.clientProxy.onDocumentUpdated(event);
1570
+ .debug(`Emit onDocumentUpdated v${stamped} (reason=${wireEvent.reason}, sourceClientId=${wireEvent.sourceClientId}${mark})`);
1571
+ this.clientProxy.onDocumentUpdated(wireEvent);
1215
1572
  }
1216
1573
 
1217
1574
  /**
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.
1575
+ * Fingerprint of the snapshot `root` and its encoding `encoded`, used to
1576
+ * de-dup `onDocumentUpdated` emissions and sent as every document's
1577
+ * `model.hash`, so it must not depend on the version or on live state. The
1578
+ * {@link FingerprintStrategy} option selects what is hashed (default
1579
+ * `'transfer-document'` — see the type).
1222
1580
  *
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.
1581
+ * {@link TransferEncoder.toTransferDocument} is cached per build, so within
1582
+ * one phase event the fingerprint and the subsequently-emitted `envelope`
1583
+ * share a single encode walk.
1227
1584
  *
1228
1585
  * Wrapped in {@link Tracer.time} against {@link FINGERPRINT_LOG_AFTER_MS}.
1229
1586
  */
1230
- protected computeDocumentFingerprint(document: LangiumDocument): string {
1231
- return this.tracer.withUri(document.uri.toString()).time(
1232
- 'Compute fingerprint',
1233
- () => {
1234
- const base =
1587
+ protected computeDocumentFingerprint(root: AstNode, encoded: EncodedTransferDocument<TransferElement, TDiagnostic>): string {
1588
+ return this.tracer
1589
+ .withUri(encoded.uri)
1590
+ .time(
1591
+ 'Compute fingerprint',
1592
+ () =>
1235
1593
  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';
1594
+ ? textDiagnosticsFingerprint(this.services.workspace.ModelLedger.textOf(root), encoded.model)
1595
+ : transferDocumentFingerprint(encoded.model),
1596
+ 'debug',
1597
+ { logAfterMs: FINGERPRINT_LOG_AFTER_MS }
1598
+ );
1273
1599
  }
1274
1600
 
1275
1601
  /**
@@ -1295,40 +1621,38 @@ export class DataServer<
1295
1621
  /**
1296
1622
  * Fan one internal {@link ProjectChangeEvent} out to per-project wire
1297
1623
  * notifications. Each `removed` entry pairs the id with the pre-removal
1298
- * snapshot needed for the `'removed'` wire payload.
1624
+ * snapshot needed for the `'removed'` wire payload. A project whose
1625
+ * {@link encodeProject} throws is logged and skipped, so the rest of the
1626
+ * batch still reaches the client.
1299
1627
  */
1300
1628
  protected dispatchProjectChangeEvent(event: ProjectChangeEvent<TProject>): void {
1629
+ const send = (project: TProject, reason: ProjectChangeReason): void => {
1630
+ let encoded: TProject;
1631
+ try {
1632
+ encoded = this.encodeProject(project);
1633
+ } catch (error: unknown) {
1634
+ this.tracer.error(`Project ${project.id} not sent as ${reason}: encodeProject threw: ${describeErrorWithStack(error)}`);
1635
+ return;
1636
+ }
1637
+ this.clientProxy.onProjectsChanged({ project: encoded, reason });
1638
+ };
1301
1639
  for (const id of event.added) {
1302
1640
  const project = this.services.workspace.ProjectManager.getProjectById(id);
1303
1641
  if (project) {
1304
- this.clientProxy.onProjectsChanged({ project, reason: 'added' });
1642
+ send(project, 'added');
1305
1643
  }
1306
1644
  }
1307
1645
  for (const id of event.updated) {
1308
1646
  const project = this.services.workspace.ProjectManager.getProjectById(id);
1309
1647
  if (project) {
1310
- this.clientProxy.onProjectsChanged({ project, reason: 'updated' });
1648
+ send(project, 'updated');
1311
1649
  }
1312
1650
  }
1313
1651
  for (const entry of event.removed) {
1314
- this.clientProxy.onProjectsChanged({ project: entry.snapshot, reason: 'removed' });
1652
+ send(entry.snapshot, 'removed');
1315
1653
  }
1316
1654
  }
1317
1655
 
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
1656
  // --- Diagnostics (DataServerDiagnosticsProtocol) ---------------------------
1333
1657
  // Computed in THIS process: the data-server child holds the model store, so
1334
1658
  // these snapshots reflect the heap that actually carries the workspace
@@ -1390,11 +1714,11 @@ export class DataServer<
1390
1714
  /** Resolve a partial options object into a fully-defaulted form. */
1391
1715
  protected resolveOptions(partial: DataServerOptions): ResolvedDataServerOptions {
1392
1716
  const additionalMethods = partial.additionalMethods ?? [];
1393
- const builtIn = [...DATA_SERVER_PROTOCOL_METHODS, ...DATA_SERVER_DIAGNOSTICS_METHODS] as readonly string[];
1717
+ const builtIn: readonly string[] = [...DATA_SERVER_PROTOCOL_METHODS, ...DATA_SERVER_DIAGNOSTICS_METHODS];
1394
1718
  const overlap = additionalMethods.filter(name => builtIn.includes(name));
1395
1719
  if (overlap.length > 0) {
1396
1720
  throw new Error(
1397
- `DataServer.additionalMethods overlaps with built-in data-server methods: ${overlap.join(', ')}. ` +
1721
+ `DataServerOptions.additionalMethods overlaps with built-in data-server methods: ${overlap.join(', ')}. ` +
1398
1722
  'Adopter protocols must not redeclare framework method names; rename the adopter method or remove it from additionalMethods.'
1399
1723
  );
1400
1724
  }
@@ -1404,7 +1728,8 @@ export class DataServer<
1404
1728
  methodNamespace: partial.methodNamespace ?? DATA_SERVER_WIRE_PREFIX,
1405
1729
  additionalMethods,
1406
1730
  excludedMethods: partial.excludedMethods ?? [],
1407
- diagnostics: partial.diagnostics ?? defaultDataServerDiagnostics()
1731
+ diagnostics: partial.diagnostics ?? defaultDataServerDiagnostics(),
1732
+ referenceSettleTimeoutMs: partial.referenceSettleTimeoutMs ?? DataServer.DEFAULT_OPTIONS.referenceSettleTimeoutMs
1408
1733
  };
1409
1734
  }
1410
1735
  }