@hydranium/protocol 1.0.0-next.7 → 1.0.0-next.71

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 (104) hide show
  1. package/README.md +35 -1
  2. package/lib/client/data-connection.d.ts +103 -0
  3. package/lib/client/data-connection.d.ts.map +1 -0
  4. package/lib/client/data-connection.js +114 -0
  5. package/lib/client/data-connection.js.map +1 -0
  6. package/lib/client/data-events.d.ts +9 -1
  7. package/lib/client/data-events.d.ts.map +1 -1
  8. package/lib/client/data-events.js +14 -0
  9. package/lib/client/data-events.js.map +1 -1
  10. package/lib/client/data-port.d.ts +16 -21
  11. package/lib/client/data-port.d.ts.map +1 -1
  12. package/lib/client/data-session.d.ts +83 -78
  13. package/lib/client/data-session.d.ts.map +1 -1
  14. package/lib/client/data-session.js +81 -108
  15. package/lib/client/data-session.js.map +1 -1
  16. package/lib/client/index.d.ts +10 -7
  17. package/lib/client/index.d.ts.map +1 -1
  18. package/lib/client/index.js +10 -7
  19. package/lib/client/index.js.map +1 -1
  20. package/lib/client/message-relay.d.ts +8 -2
  21. package/lib/client/message-relay.d.ts.map +1 -1
  22. package/lib/client/message-relay.js +10 -4
  23. package/lib/client/message-relay.js.map +1 -1
  24. package/lib/client/rpc-connection.d.ts +139 -0
  25. package/lib/client/rpc-connection.d.ts.map +1 -0
  26. package/lib/client/rpc-connection.js +171 -0
  27. package/lib/client/rpc-connection.js.map +1 -0
  28. package/lib/client-ids.d.ts +41 -0
  29. package/lib/client-ids.d.ts.map +1 -0
  30. package/lib/client-ids.js +44 -0
  31. package/lib/client-ids.js.map +1 -0
  32. package/lib/data/data-protocol-methods.d.ts +2 -2
  33. package/lib/data/data-protocol-methods.d.ts.map +1 -1
  34. package/lib/data/data-protocol-methods.js +6 -1
  35. package/lib/data/data-protocol-methods.js.map +1 -1
  36. package/lib/data/data-server-protocol.d.ts +34 -1
  37. package/lib/data/data-server-protocol.d.ts.map +1 -1
  38. package/lib/data/events.d.ts +70 -3
  39. package/lib/data/events.d.ts.map +1 -1
  40. package/lib/errors.d.ts +25 -6
  41. package/lib/errors.d.ts.map +1 -1
  42. package/lib/errors.js +32 -12
  43. package/lib/errors.js.map +1 -1
  44. package/lib/index.d.ts +2 -0
  45. package/lib/index.d.ts.map +1 -1
  46. package/lib/index.js +5 -0
  47. package/lib/index.js.map +1 -1
  48. package/lib/messages/index.d.ts +28 -0
  49. package/lib/messages/index.d.ts.map +1 -0
  50. package/lib/messages/index.js +52 -0
  51. package/lib/messages/index.js.map +1 -0
  52. package/lib/messages/primitives.d.ts +141 -0
  53. package/lib/messages/primitives.d.ts.map +1 -0
  54. package/lib/messages/primitives.js +138 -0
  55. package/lib/messages/primitives.js.map +1 -0
  56. package/lib/model-server.d.ts +2 -2
  57. package/lib/model-server.d.ts.map +1 -1
  58. package/lib/rpc/bind-rpc-methods.d.ts +29 -3
  59. package/lib/rpc/bind-rpc-methods.d.ts.map +1 -1
  60. package/lib/rpc/bind-rpc-methods.js +22 -3
  61. package/lib/rpc/bind-rpc-methods.js.map +1 -1
  62. package/lib/rpc/create-rpc-proxy.d.ts +7 -0
  63. package/lib/rpc/create-rpc-proxy.d.ts.map +1 -1
  64. package/lib/rpc/create-rpc-proxy.js +6 -1
  65. package/lib/rpc/create-rpc-proxy.js.map +1 -1
  66. package/lib/testing/catalogue-audit.d.ts +80 -0
  67. package/lib/testing/catalogue-audit.d.ts.map +1 -0
  68. package/lib/testing/catalogue-audit.js +94 -0
  69. package/lib/testing/catalogue-audit.js.map +1 -0
  70. package/lib/testing/data-doubles.d.ts +11 -12
  71. package/lib/testing/data-doubles.d.ts.map +1 -1
  72. package/lib/testing/data-doubles.js +14 -5
  73. package/lib/testing/data-doubles.js.map +1 -1
  74. package/lib/testing/index.d.ts +1 -0
  75. package/lib/testing/index.d.ts.map +1 -1
  76. package/lib/testing/index.js +4 -1
  77. package/lib/testing/index.js.map +1 -1
  78. package/lib/transfer-diagnostic.d.ts +33 -0
  79. package/lib/transfer-diagnostic.d.ts.map +1 -1
  80. package/lib/transfer-diagnostic.js +23 -0
  81. package/lib/transfer-diagnostic.js.map +1 -1
  82. package/package.json +11 -2
  83. package/src/client/data-connection.ts +167 -0
  84. package/src/client/data-events.ts +24 -1
  85. package/src/client/data-port.ts +16 -22
  86. package/src/client/data-session.ts +138 -131
  87. package/src/client/index.ts +10 -7
  88. package/src/client/message-relay.ts +28 -6
  89. package/src/client/rpc-connection.ts +230 -0
  90. package/src/client-ids.ts +45 -0
  91. package/src/data/data-protocol-methods.ts +6 -3
  92. package/src/data/data-server-protocol.ts +45 -1
  93. package/src/data/events.ts +74 -3
  94. package/src/errors.ts +38 -14
  95. package/src/index.ts +5 -0
  96. package/src/messages/index.ts +35 -0
  97. package/src/messages/primitives.ts +215 -0
  98. package/src/model-server.ts +2 -2
  99. package/src/rpc/bind-rpc-methods.ts +49 -4
  100. package/src/rpc/create-rpc-proxy.ts +14 -1
  101. package/src/testing/catalogue-audit.ts +111 -0
  102. package/src/testing/data-doubles.ts +33 -17
  103. package/src/testing/index.ts +4 -1
  104. package/src/transfer-diagnostic.ts +40 -0
@@ -0,0 +1,230 @@
1
+ /********************************************************************************
2
+ * Copyright (c) 2026 CrossBreeze, EclipseSource and others.
3
+ *
4
+ * This program and the accompanying materials are made available under the
5
+ * terms of the MIT License which is available in the project root.
6
+ *
7
+ * SPDX-License-Identifier: MIT
8
+ ********************************************************************************/
9
+
10
+ import type { MessageConnection } from 'vscode-jsonrpc';
11
+ import { type ResolvedMessage, defineMessage, describeError, resolve } from '../messages/primitives';
12
+ import { type RpcProxy, createRpcProxy } from '../rpc';
13
+ import type { DataPort } from './data-port';
14
+
15
+ /**
16
+ * The transport never opened. A complete sentence rather than a fragment: a
17
+ * fragment is nested inside a sentence the framework does not own, so no
18
+ * translator controls the whole and the composition cannot be made to read
19
+ * correctly in every language.
20
+ */
21
+ export const DATA_SERVER_CONNECT_FAILED = defineMessage(
22
+ 'hydranium/protocol/data-server-connect-failed',
23
+ 'Could not connect to the data server: {detail}'
24
+ );
25
+
26
+ export const DATA_SERVER_NOT_READY = defineMessage(
27
+ 'hydranium/protocol/data-server-not-ready',
28
+ 'The data server did not become ready: {detail}'
29
+ );
30
+
31
+ /** The one method a connection needs of any server: its startup gate. */
32
+ export interface ReadyServer {
33
+ waitForReady(): Promise<void>;
34
+ }
35
+
36
+ /**
37
+ * Lifecycle reporting, for a host that raises warm-up UI around the two waits.
38
+ *
39
+ * Nothing fires until something asks for the connection — the generation is
40
+ * built on the first {@link RpcConnection.connected}. A host that wants the
41
+ * sequence at startup drives that call itself, or a workspace where nobody
42
+ * opens a document reports neither the connect nor the readiness its UI waits
43
+ * on.
44
+ */
45
+ export interface RpcConnectionLifecycle {
46
+ /** A generation is opening its transport, including on each reconnect. */
47
+ readonly onConnecting?: () => void;
48
+ /** The server's readiness gate has settled for a generation. */
49
+ readonly onReady?: () => void;
50
+ /**
51
+ * A generation failed to connect or to become ready. The failure is still
52
+ * reported through {@link DataPort.reportError} and still rejects the
53
+ * awaiting caller; this is for a host that also drives its own UI.
54
+ */
55
+ readonly onFailed?: (error: unknown) => void;
56
+ }
57
+
58
+ /** Everything {@link RpcConnection} needs once a subclass has resolved its defaults. */
59
+ export interface ResolvedRpcConnectionOptions<TClient extends object> {
60
+ readonly methodNamespace: string;
61
+ readonly clientMethods: readonly (keyof TClient & string)[];
62
+ readonly lifecycle: RpcConnectionLifecycle;
63
+ }
64
+
65
+ /** One connection generation: its connection, its proxy, and its readiness. */
66
+ interface Generation<TServer extends object> {
67
+ readonly connection: Promise<MessageConnection>;
68
+ readonly server: RpcProxy<TServer>;
69
+ /** Set on first use; the shared readiness gate for this generation. */
70
+ ready?: Promise<void>;
71
+ }
72
+
73
+ /**
74
+ * One JSON-RPC connection to a head, with the three jobs every host adapter
75
+ * would otherwise re-derive above {@link DataPort}:
76
+ *
77
+ * 1. **Build the typed proxy** over the port's connection, with the caller's
78
+ * wire prefix and client-method allowlist.
79
+ * 2. **Own the readiness gate** — `waitForReady` once per connection, shared
80
+ * across concurrent callers. A client can connect before the workspace walk
81
+ * finishes, and an early request is then answered correctly from an empty
82
+ * registry, which reads as a broken project tier rather than as a race.
83
+ * 3. **Own the reconnect policy**, by dropping its generation when the port
84
+ * disposes and building a fresh one on the next request.
85
+ *
86
+ * Bounded only by {@link ReadyServer}, so a head serving a slice of the data
87
+ * protocol — diagnostics alone, or one with methods excluded — is still a
88
+ * legal server here. `DataConnection` narrows the bound because its sessions
89
+ * call the document methods; nothing at this layer does.
90
+ */
91
+ export class RpcConnection<TServer extends ReadyServer, TClient extends object> {
92
+ protected readonly methodNamespace: string;
93
+ protected readonly clientMethods: readonly (keyof TClient & string)[];
94
+ protected readonly lifecycle: RpcConnectionLifecycle;
95
+ /** The current generation, or `undefined` before the first request / after a teardown. */
96
+ protected generation?: Generation<TServer>;
97
+ protected disposed = false;
98
+ protected readonly portDisposeListener: { dispose(): void };
99
+
100
+ constructor(
101
+ protected readonly port: DataPort,
102
+ protected readonly client: TClient,
103
+ options: ResolvedRpcConnectionOptions<TClient>
104
+ ) {
105
+ this.methodNamespace = options.methodNamespace;
106
+ this.clientMethods = options.clientMethods;
107
+ this.lifecycle = options.lifecycle;
108
+ this.portDisposeListener = this.port.onDispose(() => this.dropGeneration());
109
+ }
110
+
111
+ /**
112
+ * The connected, READY server proxy.
113
+ *
114
+ * Returns the proxy rather than `void` on purpose. A reconnect replaces the
115
+ * proxy, so a caller that cached one from an earlier call would go on
116
+ * addressing a dead connection with no error — handing it back per call
117
+ * makes the stale reference unrepresentable.
118
+ *
119
+ * Concurrent callers share one readiness promise, so `waitForReady` is
120
+ * awaited once per generation and not once per caller.
121
+ */
122
+ async connected(): Promise<RpcProxy<TServer>> {
123
+ this.assertLive();
124
+ const generation = this.currentGeneration();
125
+ if (!generation.ready) {
126
+ generation.ready = this.awaitReady(generation);
127
+ }
128
+ await generation.ready;
129
+ return generation.server;
130
+ }
131
+
132
+ /**
133
+ * The current generation's proxy WITHOUT awaiting readiness — calls queue
134
+ * against the connection promise.
135
+ *
136
+ * **Protected, and that is the point.** An early request is answered
137
+ * correctly from a registry the workspace walk has not filled yet, which
138
+ * reads as a broken project tier rather than as a race — so every caller has
139
+ * to interpose the gate, and every caller forgetting to is a silent bug.
140
+ * {@link connected} is the public route and returns this same proxy once the
141
+ * gate has settled, which leaves nothing to forget. The one caller that
142
+ * cannot use it is {@link awaitReady}, whose whole job is running the gate.
143
+ *
144
+ * Read per access, never cached: a reconnect replaces the generation, and a
145
+ * held reference would address the dead one.
146
+ */
147
+ protected get server(): RpcProxy<TServer> {
148
+ this.assertLive();
149
+ return this.currentGeneration().server;
150
+ }
151
+
152
+ /**
153
+ * Surface a failure the way this host does, through the port's sink.
154
+ *
155
+ * Here rather than only on the port so every participant sharing the
156
+ * connection reports through one route without being handed the transport.
157
+ */
158
+ reportError(error: unknown, reported: ResolvedMessage): void {
159
+ this.port.reportError(error, reported);
160
+ }
161
+
162
+ /** Tear down the current connection and stop tracking the port. Idempotent. */
163
+ dispose(): void {
164
+ if (this.disposed) {
165
+ return;
166
+ }
167
+ this.disposed = true;
168
+ this.portDisposeListener.dispose();
169
+ this.dropGeneration();
170
+ }
171
+
172
+ /** The live generation, building one if there is none. */
173
+ protected currentGeneration(): Generation<TServer> {
174
+ if (this.generation) {
175
+ return this.generation;
176
+ }
177
+ this.lifecycle.onConnecting?.();
178
+ const connection = this.port.connect();
179
+ // Rejection is reported here rather than left to float: an unhandled
180
+ // rejection on a connection promise is the failure mode that reads as
181
+ // "the model is empty" instead of "the transport never opened".
182
+ connection.catch((error: unknown) =>
183
+ this.port.reportError(error, resolve(DATA_SERVER_CONNECT_FAILED, { detail: describeError(error) }))
184
+ );
185
+ const server = createRpcProxy<TServer, TClient>(connection, {
186
+ methodNamespace: this.methodNamespace,
187
+ localTarget: this.client,
188
+ localMethods: this.clientMethods
189
+ });
190
+ this.generation = { connection, server };
191
+ return this.generation;
192
+ }
193
+
194
+ /** Await the connection and the server's startup gate for one generation. */
195
+ protected async awaitReady(generation: Generation<TServer>): Promise<void> {
196
+ try {
197
+ await generation.connection;
198
+ await generation.server.waitForReady();
199
+ this.lifecycle.onReady?.();
200
+ } catch (error: unknown) {
201
+ this.lifecycle.onFailed?.(error);
202
+ // Drop the generation so the next request retries rather than
203
+ // re-awaiting a settled rejection forever.
204
+ if (this.generation === generation) {
205
+ this.generation = undefined;
206
+ }
207
+ this.port.reportError(error, resolve(DATA_SERVER_NOT_READY, { detail: describeError(error) }));
208
+ throw error;
209
+ }
210
+ }
211
+
212
+ /**
213
+ * Discard the current generation, disposing its connection if it opened.
214
+ * The next {@link connected} builds a fresh one.
215
+ */
216
+ protected dropGeneration(): void {
217
+ const generation = this.generation;
218
+ this.generation = undefined;
219
+ if (!generation) {
220
+ return;
221
+ }
222
+ generation.connection.then(connection => connection.dispose()).catch(() => undefined);
223
+ }
224
+
225
+ protected assertLive(): void {
226
+ if (this.disposed) {
227
+ throw new Error(`${this.constructor.name} is disposed`);
228
+ }
229
+ }
230
+ }
@@ -0,0 +1,45 @@
1
+ /********************************************************************************
2
+ * Copyright (c) 2026 CrossBreeze, EclipseSource and others.
3
+ *
4
+ * This program and the accompanying materials are made available under the
5
+ * terms of the MIT License which is available in the project root.
6
+ *
7
+ * SPDX-License-Identifier: MIT
8
+ ********************************************************************************/
9
+
10
+ /**
11
+ * Well-known values of the wire's `clientId` / `sourceClientId` field.
12
+ *
13
+ * The framework tracks each document as belonging to one or more "clients" —
14
+ * typically an LSP-text client (Monaco, VS Code, …), a GLSP diagram client,
15
+ * and/or a structured-form client — and keys its holds and watches per
16
+ * `(uri, clientId)`. These three values are not real participants, so a client
17
+ * minting its own id must avoid them.
18
+ *
19
+ * **Here rather than in the server package, because the side that has to
20
+ * RECOGNISE them is the client.** An inbound `onDocumentUpdated` carries
21
+ * `sourceClientId`, and a consumer deciding what to do with it is asking which
22
+ * of these it is — so a frontend would otherwise have to depend on the server
23
+ * tier to name three strings, and in practice retypes the literal instead.
24
+ */
25
+
26
+ /** Identifier for an LSP-text client (Monaco, VS Code, …). */
27
+ export const LANGUAGE_CLIENT_ID = 'language-client';
28
+
29
+ /** Fallback identifier when a document's author is unknown (e.g. cold workspace-init load). */
30
+ export const UNKNOWN_CLIENT_ID = 'unknown';
31
+
32
+ /**
33
+ * Synthetic author id on the `onDocumentUpdated` broadcast a data-server
34
+ * emits after the LAST client closed a document and the framework rebuilt it
35
+ * from its disk content (discarding unsaved in-session edits). Not a real
36
+ * client: it lets consumers distinguish the revert broadcast from
37
+ * client-authored updates.
38
+ */
39
+ export const REVERT_ON_CLOSE_CLIENT_ID = 'revert-on-close';
40
+
41
+ /**
42
+ * Every id the framework reserves, for a client checking that the identity it
43
+ * is about to mint collides with none of them.
44
+ */
45
+ export const FRAMEWORK_CLIENT_IDS: readonly string[] = [LANGUAGE_CLIENT_ID, UNKNOWN_CLIENT_ID, REVERT_ON_CLOSE_CLIENT_ID];
@@ -59,9 +59,12 @@ export const REFERENCE_SERVER_PROTOCOL_METHODS = [
59
59
  ] as const satisfies ReadonlyArray<keyof ReferenceServerProtocol<TransferElement> & string>;
60
60
 
61
61
  /** Notification-method names on {@link DocumentClientProtocol}. */
62
- export const DOCUMENT_CLIENT_PROTOCOL_METHODS = ['onDocumentUpdated', 'onDocumentSaved'] as const satisfies ReadonlyArray<
63
- keyof DocumentClientProtocol<TransferElement> & string
64
- >;
62
+ export const DOCUMENT_CLIENT_PROTOCOL_METHODS = [
63
+ 'onDocumentUpdated',
64
+ 'onDocumentSaved',
65
+ 'onDocumentDeleted',
66
+ 'onDocumentsBuilt'
67
+ ] as const satisfies ReadonlyArray<keyof DocumentClientProtocol<TransferElement> & string>;
65
68
 
66
69
  /** Notification-method names on {@link ProjectClientProtocol}. */
67
70
  export const PROJECT_CLIENT_PROTOCOL_METHODS = ['onProjectsChanged'] as const satisfies ReadonlyArray<keyof ProjectClientProtocol & string>;
@@ -13,7 +13,13 @@ import type { Project } from '../project';
13
13
  import type { TransferDocument } from '../transfer-document';
14
14
  import type { CloseModelArgs, FindNextNameArgs, OpenModelArgs, ReferenceContext, ReferenceRequest } from '../model-server';
15
15
  import type { ReferenceCandidate, ReferenceTarget } from '../model-service/reference-candidate';
16
- import type { ProjectsChangedEvent, TransferDocumentSavedEvent, TransferDocumentUpdatedEvent } from './events';
16
+ import type {
17
+ ProjectsChangedEvent,
18
+ TransferDocumentDeletedEvent,
19
+ TransferDocumentSavedEvent,
20
+ TransferDocumentsBuiltEvent,
21
+ TransferDocumentUpdatedEvent
22
+ } from './events';
17
23
  import type {
18
24
  GetModelDocumentArgs,
19
25
  GetProjectForUriArgs,
@@ -194,6 +200,22 @@ export interface DataServerProtocol<
194
200
  >
195
201
  extends DocumentServerProtocol<TTransfer, TDiagnostic>, ProjectServerProtocol<TProject> {}
196
202
 
203
+ /**
204
+ * The diagnostic shape a server answers with, read back off its own
205
+ * declaration.
206
+ *
207
+ * This is what lets a client tier stay generic over the server alone and still
208
+ * describe what it hands back. Both parameters must be inferred: naming
209
+ * {@link TransferElement} for the transfer instead fails to match while that
210
+ * parameter is still generic, and a default declared against the constraint
211
+ * then stops satisfying it.
212
+ */
213
+ export type DiagnosticOf<TServer> =
214
+ TServer extends DocumentServerProtocol<infer _TTransfer, infer TDiagnostic> ? TDiagnostic : TransferDiagnostic;
215
+
216
+ /** The project shape a server answers with, read back off its own declaration. */
217
+ export type ProjectOf<TServer> = TServer extends ProjectServerProtocol<infer TProject> ? TProject : Project;
218
+
197
219
  /**
198
220
  * Cross-reference / naming slice of the server surface — scope-aware
199
221
  * queries that resolve against the language's reference services. NOT part
@@ -262,6 +284,28 @@ export interface DocumentClientProtocol<TTransfer extends TransferElement, TDiag
262
284
  * codepath, not a subscription codepath).
263
285
  */
264
286
  onDocumentSaved(event: TransferDocumentSavedEvent<TTransfer, TDiagnostic>): void;
287
+
288
+ /**
289
+ * Delivered when a document's backing file was removed. Separate from
290
+ * {@link onDocumentUpdated} for the same reason as {@link onDocumentSaved},
291
+ * and more strongly: there is no built state to deliver, and the build-phase
292
+ * path cannot report a deletion at all.
293
+ *
294
+ * Ungated, because on a browser host — where the workspace lives behind the
295
+ * head — no other source can observe a file disappearing. Recipients that
296
+ * only care about their own documents filter by URI.
297
+ */
298
+ onDocumentDeleted(event: TransferDocumentDeletedEvent): void;
299
+
300
+ /**
301
+ * Delivered once per build, naming the documents that reached the
302
+ * subscription phase and that
303
+ * nobody on this connection watches — chiefly the ones rebuilt as a cascade
304
+ * from a dependency's change, which no filesystem watcher can see because
305
+ * their own files did not change. The complement of
306
+ * {@link onDocumentUpdated}; carries URIs and no documents.
307
+ */
308
+ onDocumentsBuilt(event: TransferDocumentsBuiltEvent): void;
265
309
  }
266
310
 
267
311
  /**
@@ -30,10 +30,14 @@ import type { TransferDocument } from '../transfer-document';
30
30
  * from both `onDocumentUpdated` and `onDocumentSaved`. The framework's own
31
31
  * `dispatchPhaseEvent` does NOT emit `'saved'` — saves take the dedicated
32
32
  * `DataClientProtocol.onDocumentSaved` channel.
33
- * - `'deleted'` — the URI appeared in `DocumentBuilder.onUpdate`'s
34
- * `deleted` list. The backing file was removed.
33
+ *
34
+ * Deletion is deliberately NOT a member. An update event carries a built
35
+ * document, which a deleted one has none of, and the phase-driven path that
36
+ * produces these events never runs for a deleted URI — the builder drops the
37
+ * document before deriving the rebuild set. It travels as
38
+ * {@link TransferDocumentDeletedEvent} on its own channel instead.
35
39
  */
36
- export type TransferDocumentUpdateReason = 'changed' | 'rebuilt' | 'saved' | 'deleted';
40
+ export type TransferDocumentUpdateReason = 'changed' | 'rebuilt' | 'saved';
37
41
 
38
42
  /**
39
43
  * Delivered on the data-server when a document's content (or existence)
@@ -91,6 +95,73 @@ export type TransferDocumentSavedListener<
91
95
  TDiagnostic extends TransferDiagnostic = TransferDiagnostic
92
96
  > = (event: TransferDocumentSavedEvent<TTransfer, TDiagnostic>) => void;
93
97
 
98
+ /**
99
+ * Delivered on the data-server when a document's backing file was removed.
100
+ * Carries no document, and cannot: the state a
101
+ * {@link TransferDocumentUpdatedEvent} would have to carry no longer exists by
102
+ * the time anyone can be told. That is also why deletion is not a `reason` on
103
+ * the update stream — `DocumentBuilder.update` drops the document before
104
+ * deriving the rebuild set, so the phase-driven path that produces update
105
+ * events never runs for it.
106
+ *
107
+ * **Delivered for EVERY document, not only watched ones**, unlike
108
+ * `onDocumentUpdated` and `onDocumentSaved`. The test that decides which
109
+ * channels are gated is whether any OTHER source can observe the fact on the
110
+ * least capable host: a browser-hosted client's workspace lives behind the
111
+ * head, so nothing there can see a file disappear, and gating the notification
112
+ * would leave it blind. (A Theia frontend's filesystem watcher would cover it,
113
+ * which is why this is a host argument and not a structure-versus-content one.)
114
+ * A watcher is told about its own document's deletion here too, the update
115
+ * channel being silent for deletions by construction. Filter on {@link uri} if
116
+ * the receiver only cares about documents it opened.
117
+ *
118
+ * A watch survives the deletion, so a file that comes back resumes delivering
119
+ * `onDocumentUpdated` to the same subscribers with no re-subscription. A
120
+ * client that responds by closing its editor releases the watch through
121
+ * `closeModelDocument` as usual.
122
+ */
123
+ export interface TransferDocumentDeletedEvent {
124
+ /** Canonical URI of the removed document, keyed as the subscription is. */
125
+ readonly uri: string;
126
+ }
127
+
128
+ /** Callback shape for `DataClientProtocol.onDocumentDeleted`. */
129
+ export type TransferDocumentDeletedListener = (event: TransferDocumentDeletedEvent) => void;
130
+
131
+ /**
132
+ * Delivered on the data-server once per build, naming the documents that reached
133
+ * the configured subscription phase (`DataServerOptions.subscriptionPhase`,
134
+ * `Validated` by default) and that NO client on the connection is watching. Not
135
+ * the integrity-settled landmark, which is a different point and a different
136
+ * word in this framework.
137
+ *
138
+ * The complement of {@link TransferDocumentUpdatedEvent}, which is gated per
139
+ * URI: together the two cover every document a build touched. This one exists
140
+ * for the case no source outside the server can observe — a document rebuilt
141
+ * because something it DEPENDS ON changed. Its own file never changed, so a
142
+ * filesystem watcher cannot see it, and it has no subscriber, so the update
143
+ * channel does not report it. A consumer showing data derived from such a
144
+ * document (a tree label, a decorator) would otherwise hold a stale value with
145
+ * nothing to invalidate it.
146
+ *
147
+ * Carries URIs and no documents: a recipient re-reads what it displays, through
148
+ * `getModelDocument` or its own request. That keeps the bandwidth property the
149
+ * per-URI subscription exists for, without gating the message.
150
+ *
151
+ * Not sent when the set is empty, which is the normal case while editing — the
152
+ * document being edited is watched by its own editor and therefore excluded.
153
+ * Workspace initialisation sends nothing either: it does not build to the
154
+ * subscription phase. The largest message a workspace can produce is therefore
155
+ * a whole-workspace rebuild at that phase, which is one message of URIs.
156
+ */
157
+ export interface TransferDocumentsBuiltEvent {
158
+ /** Canonical URIs, keyed as subscriptions are. Never empty. */
159
+ readonly uris: readonly string[];
160
+ }
161
+
162
+ /** Callback shape for `DataClientProtocol.onDocumentsBuilt`. */
163
+ export type TransferDocumentsBuiltListener = (event: TransferDocumentsBuiltEvent) => void;
164
+
94
165
  /**
95
166
  * Why a project-change event fired. `added` — the project was newly
96
167
  * registered (descriptor discovered); `updated` — the project's
package/src/errors.ts CHANGED
@@ -8,6 +8,20 @@
8
8
  ********************************************************************************/
9
9
 
10
10
  import { ResponseError } from 'vscode-jsonrpc';
11
+ import { defineMessage, type HydraniumMessageData, messageData } from './messages/primitives';
12
+
13
+ /**
14
+ * The catalogue declaration behind {@link ConflictError}'s sentence.
15
+ *
16
+ * Its English must keep containing {@link CONFLICT_ERROR_MESSAGE_MARKER}: the
17
+ * marker is tier 3 of {@link isConflictError}'s ladder, and it matches on text.
18
+ * That tier only ever works untranslated, which is why it is the last resort
19
+ * behind the numeric code rather than the primary check.
20
+ */
21
+ export const STALE_BASED_UPDATE = defineMessage(
22
+ 'hydranium/protocol/stale-based-update',
23
+ 'Stale-based update for {uri}: expected v{expectedVersion}, server is at v{actualVersion}'
24
+ );
11
25
 
12
26
  /**
13
27
  * Application-specific JSON-RPC error code for {@link ConflictError}.
@@ -24,12 +38,12 @@ export const CONFLICT_ERROR_CODE = 1001;
24
38
  * Structured payload carried in {@link ConflictError.data}, and the only place
25
39
  * a post-RPC caller can read the version mismatch from.
26
40
  */
27
- export interface ConflictErrorData {
41
+ export interface ConflictErrorData extends HydraniumMessageData {
28
42
  readonly uri: string;
29
43
  /** The based-on version the caller authored against. */
30
- readonly expected: number;
44
+ readonly expectedVersion: number;
31
45
  /** The server's current text-document version at the time of the throw. */
32
- readonly actual: number;
46
+ readonly actualVersion: number;
33
47
  }
34
48
 
35
49
  /**
@@ -64,18 +78,19 @@ export interface ConflictErrorData {
64
78
  * No auto-retry or auto-merge ships by default.
65
79
  */
66
80
  export class ConflictError extends ResponseError<ConflictErrorData> {
67
- constructor(uri: string, expected: number, actual: number) {
68
- super(CONFLICT_ERROR_CODE, `Stale-based update for ${uri}: expected v${expected}, server is at v${actual}`, {
69
- uri,
70
- expected,
71
- actual
72
- });
81
+ constructor(uri: string, expectedVersion: number, actualVersion: number) {
82
+ const params = { uri, expectedVersion, actualVersion };
83
+ // The identity rides alongside the typed payload rather than replacing
84
+ // it: `isConflictError`'s name check is surface an adopter may bind, so
85
+ // adding the identity widens the payload rather than reshaping it.
86
+ super(CONFLICT_ERROR_CODE, STALE_BASED_UPDATE.format(params), { ...params, ...messageData(STALE_BASED_UPDATE, params) });
73
87
  this.name = 'ConflictError';
74
88
  // ResponseError's constructor calls `Object.setPrototypeOf(this,
75
89
  // ResponseError.prototype)` to keep its own prototype chain intact across
76
90
  // transpilation targets; that resets us to ResponseError, hiding the
77
91
  // ConflictError-specific getters. Restore the prototype here so
78
- // `err.uri` / `.expected` / `.actual` resolve through this class.
92
+ // `err.uri` / `.expectedVersion` / `.actualVersion` resolve through this
93
+ // class.
79
94
  Object.setPrototypeOf(this, ConflictError.prototype);
80
95
  }
81
96
 
@@ -83,12 +98,21 @@ export class ConflictError extends ResponseError<ConflictErrorData> {
83
98
  return this.data!.uri;
84
99
  }
85
100
 
86
- get expected(): number {
87
- return this.data!.expected;
101
+ /**
102
+ * The based-on version the caller authored against.
103
+ *
104
+ * Must not be renamed to `expected`, nor its sibling to `actual`: a test
105
+ * reporter reads an error carrying both as an assertion failure, and
106
+ * vitest's formatter then ASSIGNS to them, which throws on an accessor and
107
+ * replaces the real failure with a `TypeError`.
108
+ */
109
+ get expectedVersion(): number {
110
+ return this.data!.expectedVersion;
88
111
  }
89
112
 
90
- get actual(): number {
91
- return this.data!.actual;
113
+ /** The server's version at the time of the throw. Not `actual` — see {@link expectedVersion}. */
114
+ get actualVersion(): number {
115
+ return this.data!.actualVersion;
92
116
  }
93
117
  }
94
118
 
package/src/index.ts CHANGED
@@ -16,6 +16,7 @@
16
16
 
17
17
  export * from './abstract-logger';
18
18
  export * from './client';
19
+ export * from './client-ids';
19
20
  export * from './clock';
20
21
  export * from './data';
21
22
  export * from './browser-runtime';
@@ -24,6 +25,10 @@ export * from './errors';
24
25
  export * from './host-diagnostics';
25
26
  export * from './logger';
26
27
  export * from './latency-collector';
28
+ // The primitives only. The `./messages` subpath additionally enumerates this
29
+ // package's own declarations, which the root barrel already re-exports through
30
+ // the modules that raise them.
31
+ export * from './messages/primitives';
27
32
  export * from './patch-merge';
28
33
  export * from './noop-logger';
29
34
  export * from './observable-value';
@@ -0,0 +1,35 @@
1
+ /********************************************************************************
2
+ * Copyright (c) 2026 CrossBreeze, EclipseSource and others.
3
+ *
4
+ * This program and the accompanying materials are made available under the
5
+ * terms of the MIT License which is available in the project root.
6
+ *
7
+ * SPDX-License-Identifier: MIT
8
+ ********************************************************************************/
9
+
10
+ /**
11
+ * The message-externalization mechanism, plus every user-facing message
12
+ * `@hydranium/protocol` itself raises.
13
+ *
14
+ * Declarations stay beside their call sites and are re-exported here, so this is
15
+ * enumeration rather than centralization: a code's package segment has to name
16
+ * the package that raises it, and a shared module would make that segment a lie
17
+ * for every message in it. Adding a message therefore touches the file that
18
+ * raises it and this list, and nothing else.
19
+ *
20
+ * A barrel makes every code and English default public API, so renaming a code
21
+ * is a breaking change. That was already true — an adopter's catalogue keys on
22
+ * these codes either way — but it is now in the type system rather than implicit.
23
+ * The English is a fallback, not a contract; the code is the contract.
24
+ */
25
+
26
+ export * from './primitives';
27
+
28
+ export { STALE_BASED_UPDATE } from '../errors';
29
+ export { DATA_SERVER_CONNECT_FAILED, DATA_SERVER_NOT_READY } from '../client/rpc-connection';
30
+ export {
31
+ RELAY_REPLAY_FAILED,
32
+ RELAY_TRANSPORT_OPEN_FAILED,
33
+ RELAY_TRANSPORT_READ_FAILED,
34
+ RELAY_TRANSPORT_WRITE_FAILED
35
+ } from '../client/message-relay';