@hydranium/protocol 1.0.0-next.16 → 1.0.0-next.168

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 (177) hide show
  1. package/README.md +35 -1
  2. package/lib/client/data-connection.d.ts +157 -0
  3. package/lib/client/data-connection.d.ts.map +1 -0
  4. package/lib/client/data-connection.js +236 -0
  5. package/lib/client/data-connection.js.map +1 -0
  6. package/lib/client/data-events.d.ts +13 -1
  7. package/lib/client/data-events.d.ts.map +1 -1
  8. package/lib/client/data-events.js +21 -0
  9. package/lib/client/data-events.js.map +1 -1
  10. package/lib/client/data-port.d.ts +25 -23
  11. package/lib/client/data-port.d.ts.map +1 -1
  12. package/lib/client/data-session.d.ts +440 -78
  13. package/lib/client/data-session.d.ts.map +1 -1
  14. package/lib/client/data-session.js +697 -106
  15. package/lib/client/data-session.js.map +1 -1
  16. package/lib/client/index.d.ts +13 -8
  17. package/lib/client/index.d.ts.map +1 -1
  18. package/lib/client/index.js +13 -8
  19. package/lib/client/index.js.map +1 -1
  20. package/lib/client/message-relay.d.ts +9 -3
  21. package/lib/client/message-relay.d.ts.map +1 -1
  22. package/lib/client/message-relay.js +11 -5
  23. package/lib/client/message-relay.js.map +1 -1
  24. package/lib/client/post-message-transport.d.ts +64 -3
  25. package/lib/client/post-message-transport.d.ts.map +1 -1
  26. package/lib/client/post-message-transport.js +175 -1
  27. package/lib/client/post-message-transport.js.map +1 -1
  28. package/lib/client/rpc-connection.d.ts +149 -0
  29. package/lib/client/rpc-connection.d.ts.map +1 -0
  30. package/lib/client/rpc-connection.js +188 -0
  31. package/lib/client/rpc-connection.js.map +1 -0
  32. package/lib/client-ids.d.ts +45 -0
  33. package/lib/client-ids.d.ts.map +1 -0
  34. package/lib/client-ids.js +48 -0
  35. package/lib/client-ids.js.map +1 -0
  36. package/lib/clock.d.ts +38 -0
  37. package/lib/clock.d.ts.map +1 -1
  38. package/lib/clock.js +36 -1
  39. package/lib/clock.js.map +1 -1
  40. package/lib/data/data-protocol-methods.d.ts +4 -4
  41. package/lib/data/data-protocol-methods.d.ts.map +1 -1
  42. package/lib/data/data-protocol-methods.js +11 -1
  43. package/lib/data/data-protocol-methods.js.map +1 -1
  44. package/lib/data/data-server-protocol.d.ts +111 -31
  45. package/lib/data/data-server-protocol.d.ts.map +1 -1
  46. package/lib/data/events.d.ts +103 -14
  47. package/lib/data/events.d.ts.map +1 -1
  48. package/lib/data/requests.d.ts +67 -11
  49. package/lib/data/requests.d.ts.map +1 -1
  50. package/lib/errors.d.ts +163 -17
  51. package/lib/errors.d.ts.map +1 -1
  52. package/lib/errors.js +219 -22
  53. package/lib/errors.js.map +1 -1
  54. package/lib/glsp-save-model-actions.d.ts +50 -0
  55. package/lib/glsp-save-model-actions.d.ts.map +1 -0
  56. package/lib/glsp-save-model-actions.js +28 -0
  57. package/lib/glsp-save-model-actions.js.map +1 -0
  58. package/lib/index.d.ts +3 -0
  59. package/lib/index.d.ts.map +1 -1
  60. package/lib/index.js +6 -0
  61. package/lib/index.js.map +1 -1
  62. package/lib/latency-collector.d.ts +8 -4
  63. package/lib/latency-collector.d.ts.map +1 -1
  64. package/lib/latency-collector.js.map +1 -1
  65. package/lib/messages/index.d.ts +29 -0
  66. package/lib/messages/index.d.ts.map +1 -0
  67. package/lib/messages/index.js +58 -0
  68. package/lib/messages/index.js.map +1 -0
  69. package/lib/messages/primitives.d.ts +188 -0
  70. package/lib/messages/primitives.d.ts.map +1 -0
  71. package/lib/messages/primitives.js +161 -0
  72. package/lib/messages/primitives.js.map +1 -0
  73. package/lib/model-server.d.ts +60 -13
  74. package/lib/model-server.d.ts.map +1 -1
  75. package/lib/model-server.js +4 -2
  76. package/lib/model-server.js.map +1 -1
  77. package/lib/model-service/based-on.d.ts +57 -0
  78. package/lib/model-service/based-on.d.ts.map +1 -0
  79. package/lib/model-service/based-on.js +37 -0
  80. package/lib/model-service/based-on.js.map +1 -0
  81. package/lib/model-service/index.d.ts +1 -1
  82. package/lib/model-service/index.d.ts.map +1 -1
  83. package/lib/model-service/index.js +4 -5
  84. package/lib/model-service/index.js.map +1 -1
  85. package/lib/patch-merge.d.ts +28 -27
  86. package/lib/patch-merge.d.ts.map +1 -1
  87. package/lib/patch-merge.js +66 -22
  88. package/lib/patch-merge.js.map +1 -1
  89. package/lib/profile-session.d.ts +8 -4
  90. package/lib/profile-session.d.ts.map +1 -1
  91. package/lib/profile-session.js.map +1 -1
  92. package/lib/rpc/bind-rpc-methods.d.ts +29 -3
  93. package/lib/rpc/bind-rpc-methods.d.ts.map +1 -1
  94. package/lib/rpc/bind-rpc-methods.js +22 -3
  95. package/lib/rpc/bind-rpc-methods.js.map +1 -1
  96. package/lib/rpc/create-rpc-proxy.d.ts +7 -0
  97. package/lib/rpc/create-rpc-proxy.d.ts.map +1 -1
  98. package/lib/rpc/create-rpc-proxy.js +6 -1
  99. package/lib/rpc/create-rpc-proxy.js.map +1 -1
  100. package/lib/testing/catalogue-audit.d.ts +80 -0
  101. package/lib/testing/catalogue-audit.d.ts.map +1 -0
  102. package/lib/testing/catalogue-audit.js +94 -0
  103. package/lib/testing/catalogue-audit.js.map +1 -0
  104. package/lib/testing/data-doubles.d.ts +13 -12
  105. package/lib/testing/data-doubles.d.ts.map +1 -1
  106. package/lib/testing/data-doubles.js +19 -5
  107. package/lib/testing/data-doubles.js.map +1 -1
  108. package/lib/testing/fake-clock.d.ts +9 -1
  109. package/lib/testing/fake-clock.d.ts.map +1 -1
  110. package/lib/testing/fake-clock.js +54 -45
  111. package/lib/testing/fake-clock.js.map +1 -1
  112. package/lib/testing/index.d.ts +1 -0
  113. package/lib/testing/index.d.ts.map +1 -1
  114. package/lib/testing/index.js +4 -1
  115. package/lib/testing/index.js.map +1 -1
  116. package/lib/testing/node/index.d.ts +1 -0
  117. package/lib/testing/node/index.d.ts.map +1 -1
  118. package/lib/testing/node/index.js +2 -2
  119. package/lib/testing/node/index.js.map +1 -1
  120. package/lib/testing/node/message-port-pair.d.ts +25 -0
  121. package/lib/testing/node/message-port-pair.d.ts.map +1 -0
  122. package/lib/testing/node/message-port-pair.js +26 -0
  123. package/lib/testing/node/message-port-pair.js.map +1 -0
  124. package/lib/transfer-diagnostic.d.ts +33 -0
  125. package/lib/transfer-diagnostic.d.ts.map +1 -1
  126. package/lib/transfer-diagnostic.js +23 -0
  127. package/lib/transfer-diagnostic.js.map +1 -1
  128. package/lib/transfer-document.d.ts +34 -5
  129. package/lib/transfer-document.d.ts.map +1 -1
  130. package/lib/transfer-document.js +14 -1
  131. package/lib/transfer-document.js.map +1 -1
  132. package/lib/util.d.ts +8 -0
  133. package/lib/util.d.ts.map +1 -1
  134. package/lib/util.js +32 -0
  135. package/lib/util.js.map +1 -1
  136. package/package.json +11 -2
  137. package/src/client/data-connection.ts +314 -0
  138. package/src/client/data-events.ts +33 -1
  139. package/src/client/data-port.ts +26 -24
  140. package/src/client/data-session.ts +880 -123
  141. package/src/client/index.ts +13 -8
  142. package/src/client/message-relay.ts +29 -7
  143. package/src/client/post-message-transport.ts +219 -4
  144. package/src/client/rpc-connection.ts +254 -0
  145. package/src/client-ids.ts +49 -0
  146. package/src/clock.ts +56 -0
  147. package/src/data/data-protocol-methods.ts +11 -3
  148. package/src/data/data-server-protocol.ts +132 -31
  149. package/src/data/events.ts +109 -14
  150. package/src/data/requests.ts +71 -11
  151. package/src/errors.ts +259 -24
  152. package/src/glsp-save-model-actions.ts +59 -0
  153. package/src/index.ts +6 -0
  154. package/src/latency-collector.ts +8 -3
  155. package/src/messages/index.ts +36 -0
  156. package/src/messages/primitives.ts +271 -0
  157. package/src/model-server.ts +63 -18
  158. package/src/model-service/based-on.ts +63 -0
  159. package/src/model-service/index.ts +4 -5
  160. package/src/patch-merge.ts +90 -37
  161. package/src/profile-session.ts +9 -4
  162. package/src/rpc/bind-rpc-methods.ts +49 -4
  163. package/src/rpc/create-rpc-proxy.ts +14 -1
  164. package/src/testing/catalogue-audit.ts +111 -0
  165. package/src/testing/data-doubles.ts +42 -17
  166. package/src/testing/fake-clock.ts +62 -47
  167. package/src/testing/index.ts +4 -1
  168. package/src/testing/node/index.ts +2 -2
  169. package/src/testing/node/message-port-pair.ts +40 -0
  170. package/src/transfer-diagnostic.ts +40 -0
  171. package/src/transfer-document.ts +41 -6
  172. package/src/util.ts +33 -0
  173. package/lib/model-service/args.d.ts +0 -64
  174. package/lib/model-service/args.d.ts.map +0 -1
  175. package/lib/model-service/args.js +0 -11
  176. package/lib/model-service/args.js.map +0 -1
  177. package/src/model-service/args.ts +0 -67
package/src/errors.ts CHANGED
@@ -8,32 +8,81 @@
8
8
  ********************************************************************************/
9
9
 
10
10
  import { ResponseError } from 'vscode-jsonrpc';
11
+ import { defineMessage, type HydraniumMessageData, messageData } from './messages/primitives';
12
+
13
+ /*
14
+ * Every error here is a typed error: a caller reacts to it, so it has a class,
15
+ * a JSON-RPC code and an `is*` guard, and an English message. A typed error
16
+ * also carries a message identity where the framework can write an end-user
17
+ * sentence that needs no context. A URI may stay in that sentence; a client id
18
+ * or a raw version stays out of it, worded or left to `data`.
19
+ *
20
+ * An error nothing reacts to does not belong here: it stays a plain `Error`, or
21
+ * a named subclass without a code, at its throw site, since a code and a guard
22
+ * are public API that no caller would use.
23
+ */
24
+
25
+ /**
26
+ * The catalogue declaration behind {@link ConflictError}'s sentence. It words
27
+ * the version mismatch rather than stating it: the two numbers mean nothing to
28
+ * an end user, and they stay in {@link ConflictErrorData}.
29
+ *
30
+ * Its English must keep containing {@link CONFLICT_ERROR_MESSAGE_MARKER}: the
31
+ * marker is tier 3 of {@link isConflictError}'s ladder, and it matches on text.
32
+ * That tier only ever works untranslated, which is why it is the last resort
33
+ * behind the numeric code rather than the primary check.
34
+ */
35
+ export const STALE_BASED_UPDATE = defineMessage(
36
+ 'hydranium/protocol/stale-based-update',
37
+ 'The edit to {uri} was not applied: it was made to an older version of the document.'
38
+ );
39
+
40
+ /**
41
+ * Every JSON-RPC error code the framework raises, from any package, keyed by
42
+ * the error it identifies.
43
+ *
44
+ * The framework reserves the block 42000 to 42999 for these, and an adopter's
45
+ * own codes stay outside it: after an RPC a guard matches on the code alone, so
46
+ * an adopter's error carrying one of these reads as the framework's. A new code
47
+ * takes the next unused number in the block, whichever package raises it, and
48
+ * is declared here, where tests hold every code distinct and inside the block.
49
+ * The block sits clear of the range JSON-RPC 2.0 reserves for itself, -32768
50
+ * to -32000.
51
+ */
52
+ export const HYDRANIUM_ERROR_CODES = {
53
+ conflict: 42001,
54
+ noActiveProfile: 42002,
55
+ referenceSettleTimeout: 42003,
56
+ sessionClosed: 42004,
57
+ documentNotOpen: 42005,
58
+ duplicateClientId: 42006,
59
+ reservedClientId: 42007
60
+ } as const;
11
61
 
12
62
  /**
13
- * Application-specific JSON-RPC error code for {@link ConflictError}.
14
- * Outside the reserved range (-32768 .. -32000) per JSON-RPC 2.0.
63
+ * JSON-RPC error code for {@link ConflictError}.
15
64
  *
16
65
  * The code is the load-bearing identifier across realm boundaries —
17
66
  * `code` is a first-class field on the JSON-RPC error envelope and
18
67
  * survives wire reconstruction; the custom `Error` subclass name does
19
68
  * not.
20
69
  */
21
- export const CONFLICT_ERROR_CODE = 1001;
70
+ export const CONFLICT_ERROR_CODE = HYDRANIUM_ERROR_CODES.conflict;
22
71
 
23
72
  /**
24
73
  * Structured payload carried in {@link ConflictError.data}, and the only place
25
74
  * a post-RPC caller can read the version mismatch from.
26
75
  */
27
- export interface ConflictErrorData {
76
+ export interface ConflictErrorData extends HydraniumMessageData {
28
77
  readonly uri: string;
29
78
  /** The based-on version the caller authored against. */
30
- readonly expected: number;
79
+ readonly expectedVersion: number;
31
80
  /** The server's current text-document version at the time of the throw. */
32
- readonly actual: number;
81
+ readonly actualVersion: number;
33
82
  }
34
83
 
35
84
  /**
36
- * Thrown by `ModelService.update` / `ModelService.save` when the caller-
85
+ * Thrown by a client session's `update` / `save` when the caller-
37
86
  * supplied based-on version no longer matches the server's current text-
38
87
  * document version for the same URI — i.e. the snapshot the caller
39
88
  * authored against has been superseded by an intervening edit.
@@ -46,11 +95,9 @@ export interface ConflictErrorData {
46
95
  * `err.data` (the instance is reconstructed as a generic
47
96
  * `ResponseError`, so subclass getters / fields do not survive).
48
97
  *
49
- * Detection is opt-in via the optional `baseVersion` field on
50
- * `TransferUpdateArgs` / `TransferSaveArgs`; callers that omit the field get
51
- * no gating. This mirrors LSP's `OptionalVersionedTextDocumentIdentifier`
52
- * posture, so headless / CLI / batch tooling with no meaningful based-on
53
- * version can opt out explicitly.
98
+ * Detection is driven by the required `basedOn` field of every write
99
+ * request; a caller with no meaningful based-on version passes
100
+ * `'anything'` and gets no gating.
54
101
  *
55
102
  * Three reasonable adopter recovery strategies:
56
103
  *
@@ -64,18 +111,23 @@ export interface ConflictErrorData {
64
111
  * No auto-retry or auto-merge ships by default.
65
112
  */
66
113
  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}`, {
114
+ constructor(uri: string, expectedVersion: number, actualVersion: number) {
115
+ // The identity rides alongside the typed payload rather than replacing
116
+ // it: `isConflictError`'s name check is surface an adopter may bind, so
117
+ // adding the identity widens the payload rather than reshaping it.
118
+ super(CONFLICT_ERROR_CODE, STALE_BASED_UPDATE.format({ uri }), {
69
119
  uri,
70
- expected,
71
- actual
120
+ expectedVersion,
121
+ actualVersion,
122
+ ...messageData(STALE_BASED_UPDATE, { uri })
72
123
  });
73
124
  this.name = 'ConflictError';
74
125
  // ResponseError's constructor calls `Object.setPrototypeOf(this,
75
126
  // ResponseError.prototype)` to keep its own prototype chain intact across
76
127
  // transpilation targets; that resets us to ResponseError, hiding the
77
128
  // ConflictError-specific getters. Restore the prototype here so
78
- // `err.uri` / `.expected` / `.actual` resolve through this class.
129
+ // `err.uri` / `.expectedVersion` / `.actualVersion` resolve through this
130
+ // class.
79
131
  Object.setPrototypeOf(this, ConflictError.prototype);
80
132
  }
81
133
 
@@ -83,19 +135,201 @@ export class ConflictError extends ResponseError<ConflictErrorData> {
83
135
  return this.data!.uri;
84
136
  }
85
137
 
86
- get expected(): number {
87
- return this.data!.expected;
138
+ /**
139
+ * The based-on version the caller authored against.
140
+ *
141
+ * Must not be renamed to `expected`, nor its sibling to `actual`: a test
142
+ * reporter reads an error carrying both as an assertion failure, and
143
+ * vitest's formatter then ASSIGNS to them, which throws on an accessor and
144
+ * replaces the real failure with a `TypeError`.
145
+ */
146
+ get expectedVersion(): number {
147
+ return this.data!.expectedVersion;
148
+ }
149
+
150
+ /** The server's version at the time of the throw. Not `actual` — see {@link expectedVersion}. */
151
+ get actualVersion(): number {
152
+ return this.data!.actualVersion;
153
+ }
154
+ }
155
+
156
+ /**
157
+ * JSON-RPC code for {@link SessionClosedError}, beside {@link CONFLICT_ERROR_CODE}
158
+ * and for the same reason: the code survives reconstruction, the class does not.
159
+ */
160
+ export const SESSION_CLOSED_ERROR_CODE = HYDRANIUM_ERROR_CODES.sessionClosed;
161
+ /** JSON-RPC code for {@link DocumentNotOpenError}. */
162
+ export const DOCUMENT_NOT_OPEN_ERROR_CODE = HYDRANIUM_ERROR_CODES.documentNotOpen;
163
+ /** JSON-RPC code for {@link DuplicateClientIdError}. */
164
+ export const DUPLICATE_CLIENT_ID_ERROR_CODE = HYDRANIUM_ERROR_CODES.duplicateClientId;
165
+ /** JSON-RPC code for {@link ReservedClientIdError}. */
166
+ export const RESERVED_CLIENT_ID_ERROR_CODE = HYDRANIUM_ERROR_CODES.reservedClientId;
167
+
168
+ /**
169
+ * The catalogue declaration behind {@link SessionClosedError}'s default
170
+ * sentence. It names no client id: the sentence can reach an end user, and the
171
+ * id stays in {@link SessionClosedErrorData.clientId} for whoever needs it.
172
+ */
173
+ export const SESSION_CLOSED = defineMessage('hydranium/protocol/session-closed', 'The editing session has ended.');
174
+
175
+ /** Structured payload carried in {@link SessionClosedError.data}. */
176
+ export interface SessionClosedErrorData extends HydraniumMessageData {
177
+ readonly clientId: string;
178
+ }
179
+
180
+ /**
181
+ * Thrown by every call on a client session after it ended, and by an open that
182
+ * was still in flight when its session ended: by the server for its sessions,
183
+ * and by a client-side `DataSession` once disposed, so a caller handles both
184
+ * the same way. A caller recognises it with {@link isSessionClosedError}, never
185
+ * by its sentence.
186
+ */
187
+ export class SessionClosedError extends ResponseError<SessionClosedErrorData> {
188
+ /**
189
+ * `message` replaces the default English, for a caller that knows more than
190
+ * that the session is gone. The identity stays {@link SESSION_CLOSED}'s, so a
191
+ * translating renderer renders the catalogue sentence in its place.
192
+ */
193
+ constructor(clientId: string, message = SESSION_CLOSED.format()) {
194
+ super(SESSION_CLOSED_ERROR_CODE, message, { clientId, ...messageData(SESSION_CLOSED) });
195
+ this.name = 'SessionClosedError';
196
+ // `ResponseError` resets the prototype to its own; see `ConflictError`.
197
+ Object.setPrototypeOf(this, SessionClosedError.prototype);
198
+ }
199
+
200
+ get clientId(): string {
201
+ return this.data!.clientId;
88
202
  }
203
+ }
204
+
205
+ /**
206
+ * The catalogue declaration behind {@link DocumentNotOpenError}'s sentence. The
207
+ * client id stays in {@link DocumentNotOpenErrorData.clientId}.
208
+ */
209
+ export const DOCUMENT_NOT_OPEN = defineMessage(
210
+ 'hydranium/protocol/document-not-open',
211
+ 'The document {uri} is not open in this editing session.'
212
+ );
213
+
214
+ /** Structured payload carried in {@link DocumentNotOpenError.data}. */
215
+ export interface DocumentNotOpenErrorData extends HydraniumMessageData {
216
+ readonly uri: string;
217
+ readonly clientId: string;
218
+ }
89
219
 
90
- get actual(): number {
91
- return this.data!.actual;
220
+ /**
221
+ * Thrown when a client session writes a document it does not have open.
222
+ *
223
+ * A session writes only what it has open, so this is the answer both to a write
224
+ * that never opened and to one whose open was closed underneath it — by the
225
+ * session itself, or by the document being deleted.
226
+ */
227
+ export class DocumentNotOpenError extends ResponseError<DocumentNotOpenErrorData> {
228
+ constructor(uri: string, clientId: string) {
229
+ super(DOCUMENT_NOT_OPEN_ERROR_CODE, DOCUMENT_NOT_OPEN.format({ uri }), {
230
+ uri,
231
+ clientId,
232
+ ...messageData(DOCUMENT_NOT_OPEN, { uri })
233
+ });
234
+ this.name = 'DocumentNotOpenError';
235
+ Object.setPrototypeOf(this, DocumentNotOpenError.prototype);
92
236
  }
237
+
238
+ get uri(): string {
239
+ return this.data!.uri;
240
+ }
241
+
242
+ get clientId(): string {
243
+ return this.data!.clientId;
244
+ }
245
+ }
246
+
247
+ /**
248
+ * The catalogue declaration behind {@link DuplicateClientIdError}'s sentence.
249
+ * The client id stays in {@link DuplicateClientIdErrorData.clientId}.
250
+ */
251
+ export const DUPLICATE_CLIENT_ID = defineMessage(
252
+ 'hydranium/protocol/duplicate-client-id',
253
+ 'Could not start an editing session: its identifier is still in use by another editor.'
254
+ );
255
+
256
+ /** Structured payload carried in {@link DuplicateClientIdError.data}. */
257
+ export interface DuplicateClientIdErrorData extends HydraniumMessageData {
258
+ readonly clientId: string;
259
+ }
260
+
261
+ /**
262
+ * Thrown when a client session is started under an id that is already live in
263
+ * the process. The id frees up once its holder ends or closes its last
264
+ * document, so a caller may retry.
265
+ *
266
+ * Ids are unique process-wide because the id is also the author label on every
267
+ * version and the key a client recognises its own echoes by; two participants
268
+ * sharing one would each take the other's writes for their own.
269
+ */
270
+ export class DuplicateClientIdError extends ResponseError<DuplicateClientIdErrorData> {
271
+ constructor(clientId: string) {
272
+ super(DUPLICATE_CLIENT_ID_ERROR_CODE, DUPLICATE_CLIENT_ID.format(), { clientId, ...messageData(DUPLICATE_CLIENT_ID) });
273
+ this.name = 'DuplicateClientIdError';
274
+ Object.setPrototypeOf(this, DuplicateClientIdError.prototype);
275
+ }
276
+
277
+ get clientId(): string {
278
+ return this.data!.clientId;
279
+ }
280
+ }
281
+
282
+ /**
283
+ * Thrown when a client session is started under an id the framework reserves
284
+ * for one of its own participants. A reserved id never frees up, so a caller
285
+ * that retries on {@link DuplicateClientIdError} stops on this one.
286
+ *
287
+ * It carries no message identity: a host that picks a reserved id has a bug,
288
+ * and no sentence addressed to an end user is true for it.
289
+ */
290
+ export class ReservedClientIdError extends ResponseError<{ readonly clientId: string }> {
291
+ constructor(clientId: string) {
292
+ super(RESERVED_CLIENT_ID_ERROR_CODE, `Client id ${clientId} is reserved for a framework participant`, { clientId });
293
+ this.name = 'ReservedClientIdError';
294
+ Object.setPrototypeOf(this, ReservedClientIdError.prototype);
295
+ }
296
+
297
+ get clientId(): string {
298
+ return this.data!.clientId;
299
+ }
300
+ }
301
+
302
+ /**
303
+ * Whether `error` is a {@link SessionClosedError}: by name in-process, by code
304
+ * after an RPC, where it arrives as a plain `ResponseError`.
305
+ */
306
+ export function isSessionClosedError(error: unknown): error is SessionClosedError {
307
+ return hasErrorIdentity(error, 'SessionClosedError', SESSION_CLOSED_ERROR_CODE);
308
+ }
309
+
310
+ /** Whether `error` is a {@link DocumentNotOpenError}; see {@link isSessionClosedError}. */
311
+ export function isDocumentNotOpenError(error: unknown): error is DocumentNotOpenError {
312
+ return hasErrorIdentity(error, 'DocumentNotOpenError', DOCUMENT_NOT_OPEN_ERROR_CODE);
313
+ }
314
+
315
+ /** Whether `error` is a {@link DuplicateClientIdError}; see {@link isSessionClosedError}. */
316
+ export function isDuplicateClientIdError(error: unknown): error is DuplicateClientIdError {
317
+ return hasErrorIdentity(error, 'DuplicateClientIdError', DUPLICATE_CLIENT_ID_ERROR_CODE);
318
+ }
319
+
320
+ /** Whether `error` is a {@link ReservedClientIdError}; see {@link isSessionClosedError}. */
321
+ export function isReservedClientIdError(error: unknown): error is ReservedClientIdError {
322
+ return hasErrorIdentity(error, 'ReservedClientIdError', RESERVED_CLIENT_ID_ERROR_CODE);
323
+ }
324
+
325
+ function hasErrorIdentity(error: unknown, name: string, code: number): boolean {
326
+ return error instanceof Error && (error.name === name || (error as Partial<ResponseError<unknown>>).code === code);
93
327
  }
94
328
 
95
329
  /** Marker substring present in every {@link ConflictError} message, used by
96
330
  * {@link isConflictError} as a fallback when a transport re-wraps the error
97
331
  * and drops the JSON-RPC code. */
98
- const CONFLICT_ERROR_MESSAGE_MARKER = 'Stale-based update for ';
332
+ const CONFLICT_ERROR_MESSAGE_MARKER = ': it was made to an older version of the document';
99
333
 
100
334
  /**
101
335
  * Type guard for {@link ConflictError}. Detection ladder:
@@ -106,8 +340,9 @@ const CONFLICT_ERROR_MESSAGE_MARKER = 'Stale-based update for ';
106
340
  * canonical wire-side check; the JSON-RPC `code` field is preserved
107
341
  * across reconstruction, so any adopter catching after an RPC call
108
342
  * hits this branch.
109
- * 3. `error.message.includes('Stale-based update for ')` — fallback
110
- * for transports that re-wrap the message and drop the code (rare).
343
+ * 3. `error.message` contains the marker {@link STALE_BASED_UPDATE}'s
344
+ * English carries — fallback for transports that re-wrap the message
345
+ * and drop the code (rare).
111
346
  *
112
347
  * `instanceof ConflictError` alone would silently return `false` on the
113
348
  * reconstructed shape, so callers do not use it.
@@ -0,0 +1,59 @@
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
+ // Declared field by field rather than extending GLSP's action types: this
11
+ // package carries no GLSP dependency, and both GLSP heads already depend on it.
12
+ // The fields are GLSP's own, so both actions are assignable to GLSP's
13
+ // `RequestAction` and `ResponseAction`.
14
+
15
+ /**
16
+ * A save of a diagram's model that the server answers, with a
17
+ * {@link ModelSavedAction} once the save has finished or a GLSP
18
+ * `RejectAction` when it fails.
19
+ *
20
+ * It carries what GLSP's `SaveModelAction` carries, and the server saves it the
21
+ * same way. GLSP's `SaveModelAction` is not answered: the server's only
22
+ * reply is a dirty-state change, which the client drops when the dirty flag
23
+ * does not change, so a client cannot tell which save an answer belongs to or
24
+ * whether a save failed.
25
+ */
26
+ export interface RequestSaveModelAction {
27
+ kind: typeof RequestSaveModelAction.KIND;
28
+ /** The id the response echoes as its `responseId`. Empty until the dispatcher assigns one. */
29
+ requestId: string;
30
+ /** How long the receiver waits for its own handler, in milliseconds. */
31
+ timeout?: number;
32
+ /** The file to save to, as in GLSP's `SaveModelAction`; absent saves to the diagram's source. */
33
+ fileUri?: string;
34
+ /** GLSP's typing marker for the response. Never set. */
35
+ readonly _?: ModelSavedAction;
36
+ }
37
+
38
+ export namespace RequestSaveModelAction {
39
+ export const KIND = 'hydraniumRequestSaveModel';
40
+
41
+ export function create(options: { fileUri?: string; requestId?: string } = {}): RequestSaveModelAction {
42
+ return { kind: KIND, requestId: '', ...options };
43
+ }
44
+ }
45
+
46
+ /** The server's answer to a {@link RequestSaveModelAction} that saved. */
47
+ export interface ModelSavedAction {
48
+ kind: typeof ModelSavedAction.KIND;
49
+ /** The `requestId` of the save this answers. The server's dispatcher sets it. */
50
+ responseId: string;
51
+ }
52
+
53
+ export namespace ModelSavedAction {
54
+ export const KIND = 'hydraniumModelSaved';
55
+
56
+ export function create(options: { responseId?: string } = {}): ModelSavedAction {
57
+ return { kind: KIND, responseId: '', ...options };
58
+ }
59
+ }
package/src/index.ts CHANGED
@@ -16,14 +16,20 @@
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';
22
23
  export * from './debouncer';
24
+ export * from './glsp-save-model-actions';
23
25
  export * from './errors';
24
26
  export * from './host-diagnostics';
25
27
  export * from './logger';
26
28
  export * from './latency-collector';
29
+ // The primitives only. The `./messages` subpath additionally enumerates this
30
+ // package's own declarations, which the root barrel already re-exports through
31
+ // the modules that raise them.
32
+ export * from './messages/primitives';
27
33
  export * from './patch-merge';
28
34
  export * from './noop-logger';
29
35
  export * from './observable-value';
@@ -87,8 +87,13 @@ export interface LatencyReport {
87
87
  */
88
88
  export type LatencyRetention = { readonly kind: 'keep-all' } | { readonly kind: 'ring-buffer'; readonly maxSamplesPerMethod: number };
89
89
 
90
- /** Per-method state: retained samples (bounded in ring-buffer mode) plus lifetime totals. */
91
- interface MethodAccumulator {
90
+ /**
91
+ * Per-method state: retained samples (bounded in ring-buffer mode) plus lifetime
92
+ * totals. The mutable tally behind {@link MethodLatency}, which is the derived
93
+ * report row. Held by the `protected` {@link LatencyCollector.durations}, so a
94
+ * subclass recording its own samples has to name it.
95
+ */
96
+ export interface MethodLatencyTally {
92
97
  /** Retained durations for percentile estimation; capped in ring-buffer mode. Order is irrelevant (report sorts). */
93
98
  readonly samples: number[];
94
99
  /** Next slot to overwrite once the ring buffer is full (ring-buffer mode only). */
@@ -119,7 +124,7 @@ function percentile(sortedAscending: readonly number[], percent: number): number
119
124
  * deterministic on a fake clock.
120
125
  */
121
126
  export class LatencyCollector {
122
- protected readonly durations = new Map<string, MethodAccumulator>();
127
+ protected readonly durations = new Map<string, MethodLatencyTally>();
123
128
  protected window: Stopwatch;
124
129
 
125
130
  constructor(
@@ -0,0 +1,36 @@
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 { DOCUMENT_NOT_OPEN, DUPLICATE_CLIENT_ID, SESSION_CLOSED, STALE_BASED_UPDATE } from '../errors';
29
+ export { DATA_SERVER_CONNECT_FAILED, DATA_SERVER_NOT_READY } from '../client/rpc-connection';
30
+ export { DATA_SESSION_RESTORE_FAILED, DATA_SESSION_UNSAVED_LOST } from '../client/data-session';
31
+ export {
32
+ RELAY_REPLAY_FAILED,
33
+ RELAY_TRANSPORT_OPEN_FAILED,
34
+ RELAY_TRANSPORT_READ_FAILED,
35
+ RELAY_TRANSPORT_WRITE_FAILED
36
+ } from '../client/message-relay';