@hydranium/protocol 1.0.0-next.37 → 1.0.0-next.370

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 (294) hide show
  1. package/README.md +44 -83
  2. package/lib/abstract-logger.d.ts +11 -0
  3. package/lib/abstract-logger.d.ts.map +1 -1
  4. package/lib/abstract-logger.js +16 -0
  5. package/lib/abstract-logger.js.map +1 -1
  6. package/lib/client/data-connection.d.ts +303 -0
  7. package/lib/client/data-connection.d.ts.map +1 -0
  8. package/lib/client/data-connection.js +534 -0
  9. package/lib/client/data-connection.js.map +1 -0
  10. package/lib/client/data-events.d.ts +18 -1
  11. package/lib/client/data-events.d.ts.map +1 -1
  12. package/lib/client/data-events.js +26 -0
  13. package/lib/client/data-events.js.map +1 -1
  14. package/lib/client/data-port.d.ts +34 -24
  15. package/lib/client/data-port.d.ts.map +1 -1
  16. package/lib/client/data-session.d.ts +691 -89
  17. package/lib/client/data-session.d.ts.map +1 -1
  18. package/lib/client/data-session.js +1155 -115
  19. package/lib/client/data-session.js.map +1 -1
  20. package/lib/client/index.d.ts +15 -9
  21. package/lib/client/index.d.ts.map +1 -1
  22. package/lib/client/index.js +15 -9
  23. package/lib/client/index.js.map +1 -1
  24. package/lib/client/message-relay.d.ts +6 -4
  25. package/lib/client/message-relay.d.ts.map +1 -1
  26. package/lib/client/message-relay.js +4 -4
  27. package/lib/client/message-relay.js.map +1 -1
  28. package/lib/client/post-message-transport.d.ts +69 -3
  29. package/lib/client/post-message-transport.d.ts.map +1 -1
  30. package/lib/client/post-message-transport.js +175 -1
  31. package/lib/client/post-message-transport.js.map +1 -1
  32. package/lib/client/rpc-connection.d.ts +173 -0
  33. package/lib/client/rpc-connection.d.ts.map +1 -0
  34. package/lib/client/rpc-connection.js +225 -0
  35. package/lib/client/rpc-connection.js.map +1 -0
  36. package/lib/client/worker-handshake.d.ts +94 -0
  37. package/lib/client/worker-handshake.d.ts.map +1 -0
  38. package/lib/client/worker-handshake.js +51 -0
  39. package/lib/client/worker-handshake.js.map +1 -0
  40. package/lib/client-ids.d.ts +45 -0
  41. package/lib/client-ids.d.ts.map +1 -0
  42. package/lib/client-ids.js +48 -0
  43. package/lib/client-ids.js.map +1 -0
  44. package/lib/clock.d.ts +50 -10
  45. package/lib/clock.d.ts.map +1 -1
  46. package/lib/clock.js +36 -1
  47. package/lib/clock.js.map +1 -1
  48. package/lib/console-logger.d.ts +23 -0
  49. package/lib/console-logger.d.ts.map +1 -0
  50. package/lib/console-logger.js +39 -0
  51. package/lib/console-logger.js.map +1 -0
  52. package/lib/data/data-protocol-methods.d.ts +4 -4
  53. package/lib/data/data-protocol-methods.d.ts.map +1 -1
  54. package/lib/data/data-protocol-methods.js +13 -1
  55. package/lib/data/data-protocol-methods.js.map +1 -1
  56. package/lib/data/data-server-protocol.d.ts +171 -48
  57. package/lib/data/data-server-protocol.d.ts.map +1 -1
  58. package/lib/data/events.d.ts +144 -33
  59. package/lib/data/events.d.ts.map +1 -1
  60. package/lib/data/requests.d.ts +76 -11
  61. package/lib/data/requests.d.ts.map +1 -1
  62. package/lib/debouncer.d.ts +7 -4
  63. package/lib/debouncer.d.ts.map +1 -1
  64. package/lib/debouncer.js +10 -10
  65. package/lib/debouncer.js.map +1 -1
  66. package/lib/errors.d.ts +218 -41
  67. package/lib/errors.d.ts.map +1 -1
  68. package/lib/errors.js +364 -55
  69. package/lib/errors.js.map +1 -1
  70. package/lib/format-log-arg.d.ts +33 -0
  71. package/lib/format-log-arg.d.ts.map +1 -0
  72. package/lib/format-log-arg.js +158 -0
  73. package/lib/format-log-arg.js.map +1 -0
  74. package/lib/glsp-edits-settled-actions.d.ts +54 -0
  75. package/lib/glsp-edits-settled-actions.d.ts.map +1 -0
  76. package/lib/glsp-edits-settled-actions.js +28 -0
  77. package/lib/glsp-edits-settled-actions.js.map +1 -0
  78. package/lib/glsp-request-model-args.d.ts +16 -0
  79. package/lib/glsp-request-model-args.d.ts.map +1 -0
  80. package/lib/glsp-request-model-args.js +19 -0
  81. package/lib/glsp-request-model-args.js.map +1 -0
  82. package/lib/glsp-save-model-actions.d.ts +55 -0
  83. package/lib/glsp-save-model-actions.d.ts.map +1 -0
  84. package/lib/glsp-save-model-actions.js +28 -0
  85. package/lib/glsp-save-model-actions.js.map +1 -0
  86. package/lib/index.d.ts +11 -0
  87. package/lib/index.d.ts.map +1 -1
  88. package/lib/index.js +11 -0
  89. package/lib/index.js.map +1 -1
  90. package/lib/latency-collector.d.ts +10 -4
  91. package/lib/latency-collector.d.ts.map +1 -1
  92. package/lib/latency-collector.js +2 -0
  93. package/lib/latency-collector.js.map +1 -1
  94. package/lib/logger.d.ts +38 -7
  95. package/lib/logger.d.ts.map +1 -1
  96. package/lib/logger.js +42 -4
  97. package/lib/logger.js.map +1 -1
  98. package/lib/messages/index.d.ts +4 -2
  99. package/lib/messages/index.d.ts.map +1 -1
  100. package/lib/messages/index.js +16 -4
  101. package/lib/messages/index.js.map +1 -1
  102. package/lib/messages/primitives.d.ts +59 -4
  103. package/lib/messages/primitives.d.ts.map +1 -1
  104. package/lib/messages/primitives.js +97 -4
  105. package/lib/messages/primitives.js.map +1 -1
  106. package/lib/model-server.d.ts +60 -13
  107. package/lib/model-server.d.ts.map +1 -1
  108. package/lib/model-server.js +4 -2
  109. package/lib/model-server.js.map +1 -1
  110. package/lib/model-service/base-version.d.ts +67 -0
  111. package/lib/model-service/base-version.d.ts.map +1 -0
  112. package/lib/model-service/base-version.js +44 -0
  113. package/lib/model-service/base-version.js.map +1 -0
  114. package/lib/model-service/index.d.ts +1 -1
  115. package/lib/model-service/index.d.ts.map +1 -1
  116. package/lib/model-service/index.js +4 -5
  117. package/lib/model-service/index.js.map +1 -1
  118. package/lib/model-service/reference-candidate.d.ts +5 -3
  119. package/lib/model-service/reference-candidate.d.ts.map +1 -1
  120. package/lib/node/index.d.ts +10 -0
  121. package/lib/node/index.d.ts.map +1 -0
  122. package/lib/node/index.js +29 -0
  123. package/lib/node/index.js.map +1 -0
  124. package/lib/node/process-memory.d.ts +66 -0
  125. package/lib/node/process-memory.d.ts.map +1 -0
  126. package/lib/node/process-memory.js +291 -0
  127. package/lib/node/process-memory.js.map +1 -0
  128. package/lib/noop-logger.d.ts.map +1 -1
  129. package/lib/noop-logger.js.map +1 -1
  130. package/lib/observable-value.js.map +1 -1
  131. package/lib/patch-merge.d.ts +63 -35
  132. package/lib/patch-merge.d.ts.map +1 -1
  133. package/lib/patch-merge.js +171 -25
  134. package/lib/patch-merge.js.map +1 -1
  135. package/lib/pending-work-tracker.d.ts +30 -0
  136. package/lib/pending-work-tracker.d.ts.map +1 -0
  137. package/lib/pending-work-tracker.js +47 -0
  138. package/lib/pending-work-tracker.js.map +1 -0
  139. package/lib/profile-session.d.ts +24 -8
  140. package/lib/profile-session.d.ts.map +1 -1
  141. package/lib/profile-session.js +13 -4
  142. package/lib/profile-session.js.map +1 -1
  143. package/lib/profiling.d.ts +15 -2
  144. package/lib/profiling.d.ts.map +1 -1
  145. package/lib/profiling.js.map +1 -1
  146. package/lib/project.d.ts +4 -1
  147. package/lib/project.d.ts.map +1 -1
  148. package/lib/project.js.map +1 -1
  149. package/lib/random-uuid.d.ts +14 -0
  150. package/lib/random-uuid.d.ts.map +1 -0
  151. package/lib/random-uuid.js +24 -0
  152. package/lib/random-uuid.js.map +1 -0
  153. package/lib/reconcile-write.d.ts +70 -0
  154. package/lib/reconcile-write.d.ts.map +1 -0
  155. package/lib/reconcile-write.js +69 -0
  156. package/lib/reconcile-write.js.map +1 -0
  157. package/lib/rpc/bind-rpc-methods.d.ts +6 -2
  158. package/lib/rpc/bind-rpc-methods.d.ts.map +1 -1
  159. package/lib/rpc/bind-rpc-methods.js +17 -7
  160. package/lib/rpc/bind-rpc-methods.js.map +1 -1
  161. package/lib/rpc/create-rpc-proxy.d.ts +3 -0
  162. package/lib/rpc/create-rpc-proxy.d.ts.map +1 -1
  163. package/lib/rpc/create-rpc-proxy.js +6 -1
  164. package/lib/rpc/create-rpc-proxy.js.map +1 -1
  165. package/lib/rpc/index.d.ts +1 -0
  166. package/lib/rpc/index.d.ts.map +1 -1
  167. package/lib/rpc/index.js +1 -0
  168. package/lib/rpc/index.js.map +1 -1
  169. package/lib/rpc/send-by-method-name.d.ts +76 -0
  170. package/lib/rpc/send-by-method-name.d.ts.map +1 -0
  171. package/lib/rpc/send-by-method-name.js +120 -0
  172. package/lib/rpc/send-by-method-name.js.map +1 -0
  173. package/lib/rpc/wire-prefix.js.map +1 -1
  174. package/lib/save-text-document.d.ts +40 -0
  175. package/lib/save-text-document.d.ts.map +1 -0
  176. package/lib/save-text-document.js +34 -0
  177. package/lib/save-text-document.js.map +1 -0
  178. package/lib/testing/catalogue-audit.js.map +1 -1
  179. package/lib/testing/data-doubles.d.ts +41 -12
  180. package/lib/testing/data-doubles.d.ts.map +1 -1
  181. package/lib/testing/data-doubles.js +56 -8
  182. package/lib/testing/data-doubles.js.map +1 -1
  183. package/lib/testing/fake-clock.d.ts +17 -2
  184. package/lib/testing/fake-clock.d.ts.map +1 -1
  185. package/lib/testing/fake-clock.js +61 -46
  186. package/lib/testing/fake-clock.js.map +1 -1
  187. package/lib/testing/index.d.ts +1 -0
  188. package/lib/testing/index.d.ts.map +1 -1
  189. package/lib/testing/index.js +4 -3
  190. package/lib/testing/index.js.map +1 -1
  191. package/lib/testing/node/duplex-connection.d.ts.map +1 -1
  192. package/lib/testing/node/duplex-connection.js +3 -2
  193. package/lib/testing/node/duplex-connection.js.map +1 -1
  194. package/lib/testing/node/duplex-stream.js.map +1 -1
  195. package/lib/testing/node/index.d.ts +1 -0
  196. package/lib/testing/node/index.d.ts.map +1 -1
  197. package/lib/testing/node/index.js +2 -2
  198. package/lib/testing/node/index.js.map +1 -1
  199. package/lib/testing/node/message-port-pair.d.ts +25 -0
  200. package/lib/testing/node/message-port-pair.d.ts.map +1 -0
  201. package/lib/testing/node/message-port-pair.js +26 -0
  202. package/lib/testing/node/message-port-pair.js.map +1 -0
  203. package/lib/testing/transfer-node-builder.d.ts +81 -0
  204. package/lib/testing/transfer-node-builder.d.ts.map +1 -0
  205. package/lib/testing/transfer-node-builder.js +47 -0
  206. package/lib/testing/transfer-node-builder.js.map +1 -0
  207. package/lib/testing/wait-for.d.ts +9 -3
  208. package/lib/testing/wait-for.d.ts.map +1 -1
  209. package/lib/testing/wait-for.js +57 -11
  210. package/lib/testing/wait-for.js.map +1 -1
  211. package/lib/tracer.d.ts.map +1 -1
  212. package/lib/tracer.js +5 -9
  213. package/lib/tracer.js.map +1 -1
  214. package/lib/transfer-diagnostic.js.map +1 -1
  215. package/lib/transfer-document.d.ts +96 -32
  216. package/lib/transfer-document.d.ts.map +1 -1
  217. package/lib/transfer-document.js +17 -9
  218. package/lib/transfer-document.js.map +1 -1
  219. package/lib/transfer-element.d.ts +4 -4
  220. package/lib/types.d.ts +32 -0
  221. package/lib/types.d.ts.map +1 -0
  222. package/lib/{model-service/args.js → types.js} +2 -2
  223. package/lib/types.js.map +1 -0
  224. package/lib/uri.d.ts.map +1 -1
  225. package/lib/uri.js.map +1 -1
  226. package/lib/util.d.ts +8 -2
  227. package/lib/util.d.ts.map +1 -1
  228. package/lib/util.js +32 -4
  229. package/lib/util.js.map +1 -1
  230. package/package.json +24 -40
  231. package/src/abstract-logger.ts +18 -0
  232. package/src/client/data-connection.ts +654 -0
  233. package/src/client/data-events.ts +38 -1
  234. package/src/client/data-port.ts +36 -25
  235. package/src/client/data-session.ts +1476 -137
  236. package/src/client/index.ts +15 -9
  237. package/src/client/message-relay.ts +6 -4
  238. package/src/client/post-message-transport.ts +224 -4
  239. package/src/client/rpc-connection.ts +302 -0
  240. package/src/client/worker-handshake.ts +121 -0
  241. package/src/client-ids.ts +49 -0
  242. package/src/clock.ts +68 -10
  243. package/src/console-logger.ts +39 -0
  244. package/src/data/data-protocol-methods.ts +14 -4
  245. package/src/data/data-server-protocol.ts +198 -48
  246. package/src/data/events.ts +150 -33
  247. package/src/data/requests.ts +82 -11
  248. package/src/debouncer.ts +11 -11
  249. package/src/errors.ts +453 -65
  250. package/src/format-log-arg.ts +176 -0
  251. package/src/glsp-edits-settled-actions.ts +62 -0
  252. package/src/glsp-request-model-args.ts +16 -0
  253. package/src/glsp-save-model-actions.ts +64 -0
  254. package/src/index.ts +11 -0
  255. package/src/latency-collector.ts +10 -3
  256. package/src/logger.ts +44 -8
  257. package/src/messages/index.ts +11 -2
  258. package/src/messages/primitives.ts +129 -5
  259. package/src/model-server.ts +64 -19
  260. package/src/model-service/base-version.ts +75 -0
  261. package/src/model-service/index.ts +4 -5
  262. package/src/model-service/reference-candidate.ts +5 -3
  263. package/src/node/index.ts +14 -0
  264. package/src/node/process-memory.ts +299 -0
  265. package/src/patch-merge.ts +230 -45
  266. package/src/pending-work-tracker.ts +47 -0
  267. package/src/profile-session.ts +31 -11
  268. package/src/profiling.ts +15 -2
  269. package/src/project.ts +4 -1
  270. package/src/random-uuid.ts +21 -0
  271. package/src/reconcile-write.ts +131 -0
  272. package/src/rpc/README.md +6 -6
  273. package/src/rpc/bind-rpc-methods.ts +18 -8
  274. package/src/rpc/create-rpc-proxy.ts +6 -1
  275. package/src/rpc/index.ts +1 -0
  276. package/src/rpc/send-by-method-name.ts +140 -0
  277. package/src/save-text-document.ts +39 -0
  278. package/src/testing/data-doubles.ts +141 -19
  279. package/src/testing/fake-clock.ts +76 -48
  280. package/src/testing/index.ts +4 -3
  281. package/src/testing/node/duplex-connection.ts +3 -2
  282. package/src/testing/node/index.ts +2 -2
  283. package/src/testing/node/message-port-pair.ts +40 -0
  284. package/src/testing/transfer-node-builder.ts +107 -0
  285. package/src/testing/wait-for.ts +61 -13
  286. package/src/tracer.ts +5 -9
  287. package/src/transfer-document.ts +116 -34
  288. package/src/transfer-element.ts +4 -4
  289. package/src/types.ts +30 -0
  290. package/src/util.ts +33 -6
  291. package/lib/model-service/args.d.ts +0 -64
  292. package/lib/model-service/args.d.ts.map +0 -1
  293. package/lib/model-service/args.js.map +0 -1
  294. package/src/model-service/args.ts +0 -67
@@ -7,34 +7,43 @@
7
7
  * SPDX-License-Identifier: MIT
8
8
  ********************************************************************************/
9
9
 
10
- import { applyPatch, compare, deepClone, getValueByPointer, type Operation as JsonPatchOperation } from 'fast-json-patch';
10
+ import {
11
+ _areEquals,
12
+ applyPatch,
13
+ compare,
14
+ deepClone,
15
+ getValueByPointer,
16
+ type Operation as JsonPatchOperation,
17
+ unescapePathComponent
18
+ } from 'fast-json-patch';
11
19
 
12
20
  /**
13
- * Augment a user-intent JSON patch (computed `baseline → attempted`) with
21
+ * Augment a user-intent JSON patch (computed `base → ours`) with
14
22
  * `test` ops so a strict `applyPatch` against freshly-fetched server state
15
23
  * fails loudly when a foreign writer changed a path the user also changed.
16
24
  *
17
25
  * `fast-json-patch`'s `validateOperation: true` validates op structure and
18
26
  * path resolvability but NOT the pre-existing value — a plain `replace` /
19
27
  * `remove` silently overwrites whatever the foreign writer put there. For each
20
- * `replace` / `remove` op we prepend a `test` op carrying the baseline value at
28
+ * `replace` / `remove` op we prepend a `test` op carrying the base value at
21
29
  * that path, so a same-path divergence becomes a `TEST_OPERATION_FAILED` throw.
22
30
  * The caller treats that as a real field-level conflict (drop + refetch) rather
23
31
  * than silently clobbering the foreign edit.
24
32
  *
25
- * `add` ops are left unguarded: the path is new, so there is no baseline value
26
- * to test against, and add-vs-add overlaps are out of scope for this floor.
33
+ * `add` ops get no `test` op: the path is new, so there is no base value
34
+ * to test. {@link reconcileByPatchReplay} checks them against theirs
35
+ * instead; a caller applying this patch itself does not get that check.
27
36
  *
28
37
  * Used behind the framework's `ConflictError` contract by every reconcile
29
38
  * path — the GLSP recording command's undo/redo and forward-write, via
30
39
  * {@link ReconcilingConflictResolver}, and an adopter's form-widget save — so
31
40
  * they share one collision-detection rule.
32
41
  */
33
- export function augmentWithTestOps(baseline: object, userPatch: ReadonlyArray<JsonPatchOperation>): JsonPatchOperation[] {
42
+ export function augmentWithTestOps(base: object, userPatch: ReadonlyArray<JsonPatchOperation>): JsonPatchOperation[] {
34
43
  const augmented: JsonPatchOperation[] = [];
35
44
  for (const op of userPatch) {
36
45
  if (op.op === 'replace' || op.op === 'remove') {
37
- augmented.push({ op: 'test', path: op.path, value: getValueByPointer(baseline, op.path) });
46
+ augmented.push({ op: 'test', path: op.path, value: getValueByPointer(base, op.path) });
38
47
  }
39
48
  augmented.push(op);
40
49
  }
@@ -42,39 +51,60 @@ export function augmentWithTestOps(baseline: object, userPatch: ReadonlyArray<Js
42
51
  }
43
52
 
44
53
  /**
45
- * Outcome of {@link reconcileByPatchReplay}. The caller persists / re-baselines
46
- * on `merged`, drops + surfaces the `fresh` root on `conflict`, and decides its
47
- * own fallback (e.g. force-retry) on `no-op` / `unavailable`.
54
+ * Outcome of {@link reconcileByPatchReplay}. The caller persists `merged` and
55
+ * takes it as the new base, takes `theirs` as the new base on
56
+ * `already-applied`, drops + surfaces the `theirs` root on `conflict`,
57
+ * catches up with the server on `no-op`, and decides its own fallback (e.g.
58
+ * force-retry) on `unavailable`.
48
59
  */
49
60
  export type ReconcileOutcome<T> =
50
61
  | {
51
- /** The replay succeeded: the foreign writer touched no path the user did. */
62
+ /**
63
+ * The replay succeeded: the foreign writer touched no path the user
64
+ * did, or wrote the user's value at one that runs through no array.
65
+ */
52
66
  status: 'merged';
53
67
  /**
54
- * A fresh root built on the refetched server state, carrying both
55
- * intents. It is NOT the caller's `attempted` root — re-baseline on
56
- * this value, or the next write diffs against state the server never
57
- * had.
68
+ * A new root built on theirs, carrying both intents. It is NOT the
69
+ * caller's `ours` root — take this value as the new base, or the next
70
+ * write diffs against state the server never had.
58
71
  */
59
72
  merged: T;
60
73
  }
61
74
  | {
62
75
  /**
63
- * The user's root already equalled the baseline, so the version gate
64
- * fired on drift that changed nothing. No refetch was performed and
65
- * there is nothing to persist — retrying the same write reproduces it.
76
+ * The user's root equalled the base, so the gate fired on another writer's
77
+ * change and the caller is behind the server (`ConflictError.actualVersion`).
78
+ * Nothing was refetched. Catch up from the update at that version or a later
79
+ * one, or refetch; forcing the write overwrites the other writer's change.
66
80
  */
67
81
  status: 'no-op';
68
82
  }
69
83
  | {
70
- /** A guarded `test` op tripped: user and foreign writer touched one path. */
84
+ /** The refetch already holds every path the user changed at the user's value: nothing is left to write. */
85
+ status: 'already-applied';
86
+ /**
87
+ * The server's current root, refetched and unmodified. Take it as the
88
+ * new base, not `ours`: it can also hold another writer's edit to a
89
+ * path the user did not change.
90
+ */
91
+ theirs: T;
92
+ }
93
+ | {
94
+ /**
95
+ * User and foreign writer changed one path to different values, or
96
+ * both changed one inside an array the refetch holds otherwise than
97
+ * the user, even to the same value; both inserted the same array
98
+ * element; or the foreign writer removed or replaced what the user
99
+ * edited inside.
100
+ */
71
101
  status: 'conflict';
72
102
  /**
73
103
  * The server's current root, refetched and unmodified — the user's
74
104
  * intent was NOT applied to it. Surface it and drop the write; treating
75
105
  * it as a merge result silently discards what the user typed.
76
106
  */
77
- fresh: T;
107
+ theirs: T;
78
108
  }
79
109
  | {
80
110
  /**
@@ -87,39 +117,194 @@ export type ReconcileOutcome<T> =
87
117
 
88
118
  /**
89
119
  * Shared three-way reconcile for a `ConflictError`: diff the user's intent
90
- * (`baseline → attempted`), refetch the server's current root, and replay the
120
+ * (`base → ours`), refetch the server's current root (theirs), and replay the
91
121
  * intent on top under strict, {@link augmentWithTestOps}-guarded `applyPatch`.
92
122
  *
93
- * - `no-op` — the user's root equals the baseline, so the gate fired on a
94
- * benign version drift; nothing to replay (refetch is skipped).
123
+ * - `no-op` — the user's root equals the base: nothing to replay, and no
124
+ * refetch; the caller's document is behind the server's.
95
125
  * - `unavailable` — the refetch produced nothing; caller falls back.
96
- * - `merged` — the foreign writer touched only paths the user did not; the
97
- * merged root carries both intents.
98
- * - `conflict` — a same-path divergence tripped a `test` op; caller drops the
99
- * write and surfaces `fresh`.
126
+ * - `already-applied` — the refetch already holds the user's value at every
127
+ * path the user changed, and the user's whole array for a path through
128
+ * one; nothing is replayed.
129
+ * - `merged` — the foreign writer touched only paths the user did not, or
130
+ * made the user's change itself at a path through no array; the merged
131
+ * root carries both intents.
132
+ * - `conflict` — a same-path divergence tripped a `test` op, including the
133
+ * user's own value inside an array the refetch holds otherwise than the
134
+ * user; or an `add` collides with one the foreign writer made; or the
135
+ * foreign writer removed or replaced what the user edited inside. The
136
+ * caller drops the write and surfaces `theirs`.
100
137
  *
101
138
  * I/O is the caller's: `refetch` supplies the current root, and applying the
102
- * `merged` result (update vs save, re-baseline, UI refresh) stays at the call
139
+ * `merged` result (update vs save, new base, UI refresh) stays at the call
103
140
  * site so form and GLSP paths keep their own persistence semantics.
104
141
  */
105
142
  export async function reconcileByPatchReplay<T extends object>(
106
- baseline: T,
107
- attempted: T,
143
+ base: T,
144
+ ours: T,
108
145
  refetch: () => Promise<T | undefined>
109
146
  ): Promise<ReconcileOutcome<T>> {
110
- const userPatch = compare(baseline, attempted);
147
+ const userPatch = compare(base, ours);
111
148
  if (userPatch.length === 0) {
112
149
  return { status: 'no-op' };
113
150
  }
114
- const fresh = await refetch();
115
- if (!fresh) {
151
+ const theirs = await refetch();
152
+ if (!theirs) {
116
153
  return { status: 'unavailable' };
117
154
  }
155
+ const comparable = toJson(theirs);
156
+ const pending = withoutAgreed(base, ours, comparable, userPatch);
157
+ if (pending.length === 0) {
158
+ return { status: 'already-applied', theirs };
159
+ }
160
+ if (addsCollide(base, comparable, pending)) {
161
+ return { status: 'conflict', theirs };
162
+ }
118
163
  try {
119
- const merged = applyPatch(deepClone(fresh), augmentWithTestOps(baseline, userPatch), true).newDocument;
164
+ const merged = applyPatch(deepClone(theirs), augmentWithTestOps(base, pending), true).newDocument;
120
165
  return { status: 'merged', merged };
121
166
  } catch {
122
- return { status: 'conflict', fresh };
167
+ return { status: 'conflict', theirs };
168
+ }
169
+ }
170
+
171
+ /**
172
+ * `userPatch` without the ops `theirs` already agrees with: a `replace` or
173
+ * object-key `add` whose key the object at its parent path holds at the
174
+ * user's value, a `remove` whose key that object lacks. A parent that is
175
+ * absent or no object agrees with nothing: a foreign writer deleted or
176
+ * replaced what the user edited inside, or a refetch omitted a document it
177
+ * could not read.
178
+ *
179
+ * Ops address array elements by index, so an op through an array agrees only
180
+ * when that array, the outermost on its path, equals the user's: matching one
181
+ * index of an array that shifted would drop the user's edit to another
182
+ * element. An insert never agrees, as {@link addsCollide} keeps two equal
183
+ * additions apart, and neither does any op through its array, or replaying
184
+ * the insert alone would add its element a second time. Each array is
185
+ * compared once, however many ops run through it and however many arrays the
186
+ * patch inserts into, so a bulk edit stays linear.
187
+ *
188
+ * `theirs` is the refetch through {@link toJson}.
189
+ */
190
+ function withoutAgreed(base: object, ours: object, theirs: object, userPatch: readonly JsonPatchOperation[]): JsonPatchOperation[] {
191
+ const jsonOurs = toJson(ours);
192
+ // An insert is keyed by the outermost array on its path, as every op is looked up, so one set answers for every array.
193
+ const insertedArrays = new Set(
194
+ userPatch
195
+ .filter(op => op.op === 'add' && Array.isArray(valueAt(base, op.path.slice(0, op.path.lastIndexOf('/')))))
196
+ .map(op => outermostArrayOn(jsonOurs, op.path))
197
+ );
198
+ const arrayAgrees = new Map<string, boolean>();
199
+ return userPatch.filter(op => {
200
+ const array = outermostArrayOn(jsonOurs, op.path);
201
+ if (array !== undefined) {
202
+ let agrees = arrayAgrees.get(array);
203
+ if (agrees === undefined) {
204
+ agrees = !insertedArrays.has(array) && _areEquals(valueAt(theirs, array), valueAt(jsonOurs, array));
205
+ arrayAgrees.set(array, agrees);
206
+ }
207
+ return !agrees;
208
+ }
209
+ const cut = op.path.lastIndexOf('/');
210
+ const parent = valueAt(theirs, op.path.slice(0, cut));
211
+ if (typeof parent !== 'object' || parent === null || Array.isArray(parent)) {
212
+ return true;
213
+ }
214
+ const key = unescapePathComponent(op.path.slice(cut + 1));
215
+ if (op.op === 'remove') {
216
+ return Object.hasOwn(parent, key);
217
+ }
218
+ if (op.op !== 'replace' && op.op !== 'add') {
219
+ return true;
220
+ }
221
+ return !Object.hasOwn(parent, key) || !_areEquals(Reflect.get(parent, key), op.value);
222
+ });
223
+ }
224
+
225
+ /** The outermost array a step of `pointer` before its last runs through in `document`, as a pointer; `undefined` for none. */
226
+ function outermostArrayOn(document: object, pointer: string): string | undefined {
227
+ const steps = pointer.split('/');
228
+ for (let i = 1; i < steps.length; i++) {
229
+ const prefix = steps.slice(0, i).join('/');
230
+ if (Array.isArray(valueAt(document, prefix))) {
231
+ return prefix;
232
+ }
233
+ }
234
+ return undefined;
235
+ }
236
+
237
+ /**
238
+ * `value` through JSON, as `compare` clones the values it diffs: an
239
+ * undefined-valued key would otherwise make two equal values differ.
240
+ */
241
+ function toJson<T>(value: T): T {
242
+ return JSON.parse(JSON.stringify(value)) as T;
243
+ }
244
+
245
+ /**
246
+ * Whether an `add` of `userPatch` collides with one the foreign writer made:
247
+ * it inserts a value into an array that `theirs` holds more often than
248
+ * `base` did, or it adds an object key that `theirs` already holds, at
249
+ * another value once `withoutAgreed` has dropped the equal ones.
250
+ *
251
+ * Replayed, the first inserts the value a second time and the second replaces
252
+ * the foreign value. Identical array elements conflict rather than being
253
+ * skipped: skipping drops one of two insertions that merely happen to be
254
+ * equal. `theirs` is the refetch through {@link toJson}.
255
+ */
256
+ function addsCollide(base: object, theirs: object, userPatch: readonly JsonPatchOperation[]): boolean {
257
+ // Each array's elements are counted once, however many ops insert into it, so a bulk insert stays linear.
258
+ const tallies = new Map<string, { readonly before: ReadonlyMap<string, number>; readonly now: ReadonlyMap<string, number> }>();
259
+ return userPatch.some(op => {
260
+ if (op.op !== 'add') {
261
+ return false;
262
+ }
263
+ const cut = op.path.lastIndexOf('/');
264
+ const parent = op.path.slice(0, cut);
265
+ const before = valueAt(base, parent);
266
+ const now = valueAt(theirs, parent);
267
+ if (Array.isArray(before)) {
268
+ if (!Array.isArray(now)) {
269
+ return false;
270
+ }
271
+ let tally = tallies.get(parent);
272
+ if (!tally) {
273
+ tally = { before: tallyElements(before), now: tallyElements(now) };
274
+ tallies.set(parent, tally);
275
+ }
276
+ const key = canonicalJson(op.value);
277
+ return (tally.now.get(key) ?? 0) > (tally.before.get(key) ?? 0);
278
+ }
279
+ return typeof now === 'object' && now !== null && Object.hasOwn(now, unescapePathComponent(op.path.slice(cut + 1)));
280
+ });
281
+ }
282
+
283
+ /** How many elements of `array` there are of each value, keyed by {@link canonicalJson}. */
284
+ function tallyElements(array: readonly unknown[]): ReadonlyMap<string, number> {
285
+ const tally = new Map<string, number>();
286
+ for (const element of array) {
287
+ const key = canonicalJson(element);
288
+ tally.set(key, (tally.get(key) ?? 0) + 1);
289
+ }
290
+ return tally;
291
+ }
292
+
293
+ /** `value` as JSON with every object's keys sorted, so two deeply equal values give one string whatever their key order. */
294
+ function canonicalJson(value: unknown): string {
295
+ return JSON.stringify(value, (_key, nested: unknown) =>
296
+ typeof nested === 'object' && nested !== null && !Array.isArray(nested)
297
+ ? Object.fromEntries(Object.entries(nested).sort(([left], [right]) => (left < right ? -1 : left > right ? 1 : 0)))
298
+ : nested
299
+ );
300
+ }
301
+
302
+ /** The value at `pointer`, or `undefined` when a step of it is missing. */
303
+ function valueAt(document: object, pointer: string): unknown {
304
+ try {
305
+ return getValueByPointer(document, pointer);
306
+ } catch {
307
+ return undefined;
123
308
  }
124
309
  }
125
310
 
@@ -132,12 +317,12 @@ export async function reconcileByPatchReplay<T extends object>(
132
317
  */
133
318
  export interface ConflictResolver {
134
319
  /**
135
- * Reconcile the user's `baseline → attempted` intent against the current
136
- * server state (`refetch`), returning a {@link ReconcileOutcome} the caller
137
- * acts on (persist `merged`, drop on `conflict`, fall back on `no-op` /
138
- * `unavailable`).
320
+ * Reconcile the user's `base → ours` intent against the current
321
+ * server state (theirs, from `refetch`), returning a {@link ReconcileOutcome} the caller
322
+ * acts on (persist `merged`, write nothing on `already-applied`, drop on
323
+ * `conflict`, catch up on `no-op`, fall back on `unavailable`).
139
324
  */
140
- resolve<T extends object>(baseline: T, attempted: T, refetch: () => Promise<T | undefined>): Promise<ReconcileOutcome<T>>;
325
+ resolve<T extends object>(base: T, ours: T, refetch: () => Promise<T | undefined>): Promise<ReconcileOutcome<T>>;
141
326
  }
142
327
 
143
328
  /**
@@ -147,19 +332,19 @@ export interface ConflictResolver {
147
332
  * clobbered.
148
333
  */
149
334
  export class ReconcilingConflictResolver implements ConflictResolver {
150
- resolve<T extends object>(baseline: T, attempted: T, refetch: () => Promise<T | undefined>): Promise<ReconcileOutcome<T>> {
151
- return reconcileByPatchReplay(baseline, attempted, refetch);
335
+ resolve<T extends object>(base: T, ours: T, refetch: () => Promise<T | undefined>): Promise<ReconcileOutcome<T>> {
336
+ return reconcileByPatchReplay(base, ours, refetch);
152
337
  }
153
338
  }
154
339
 
155
340
  /**
156
- * Last-writer-wins {@link ConflictResolver}: always reports the `attempted`
341
+ * Last-writer-wins {@link ConflictResolver}: always reports the `ours`
157
342
  * state as merged, without refetching or guarding. Suitable for single-client
158
343
  * tools or always-regenerated artifacts where a concurrent foreign edit may be
159
344
  * overwritten. A foreign edit to any field is clobbered.
160
345
  */
161
346
  export class ForceConflictResolver implements ConflictResolver {
162
- async resolve<T extends object>(_baseline: T, attempted: T, _refetch: () => Promise<T | undefined>): Promise<ReconcileOutcome<T>> {
163
- return { status: 'merged', merged: attempted };
347
+ async resolve<T extends object>(_base: T, ours: T, _refetch: () => Promise<T | undefined>): Promise<ReconcileOutcome<T>> {
348
+ return { status: 'merged', merged: ours };
164
349
  }
165
350
  }
@@ -0,0 +1,47 @@
1
+ /********************************************************************************
2
+ * Copyright (c) 2026 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
+ * Watches work started elsewhere until it settles, so a caller can ask whether
12
+ * any is still pending and wait for what is pending now. It starts, orders and
13
+ * limits nothing, and keeps no clock: a caller that must not wait for good
14
+ * races {@link whenSettled} against its own bound.
15
+ */
16
+ export class PendingWorkTracker {
17
+ /** The tracked work. {@link track} removes each entry once it settles. */
18
+ protected readonly pending = new Set<Promise<unknown>>();
19
+
20
+ /** Watch `work` until it settles, and hand it back. */
21
+ track<T>(work: Promise<T>): Promise<T> {
22
+ this.pending.add(work);
23
+ const done = (): void => {
24
+ this.pending.delete(work);
25
+ };
26
+ work.then(done, done);
27
+ return work;
28
+ }
29
+
30
+ /** How much of the tracked work has not settled yet. */
31
+ get size(): number {
32
+ return this.pending.size;
33
+ }
34
+
35
+ /** Whether all tracked work has settled. Synchronous, for a check that must decide within the tick. */
36
+ get isIdle(): boolean {
37
+ return this.pending.size === 0;
38
+ }
39
+
40
+ /**
41
+ * Resolves once the work pending now has settled; work tracked later is not
42
+ * waited for. Never rejects, since failed work has settled too.
43
+ */
44
+ async whenSettled(): Promise<void> {
45
+ await Promise.allSettled([...this.pending]);
46
+ }
47
+ }
@@ -18,7 +18,11 @@ export interface ProfileRecord {
18
18
  count: number;
19
19
  /** Summed self-time across those runs (wall-clock minus nested scopes). */
20
20
  selfMs: number;
21
- /** Self-time as a percentage of the session's total wall-clock. */
21
+ /**
22
+ * Self-time as a percentage of the session's total wall-clock. Overlapping
23
+ * scopes each count their own wall time, so the percentages can add up to
24
+ * more than 100.
25
+ */
22
26
  selfPct: number;
23
27
  }
24
28
 
@@ -53,8 +57,9 @@ export interface ProfileSession {
53
57
  /**
54
58
  * Emit the aggregate through the bound {@link Logger}: one line per id,
55
59
  * sorted by self-time descending, plus an "unaccounted" remainder (the
56
- * session wall-clock not attributed to any top-level scope). No-op when
57
- * `logLevel` is suppressed by the global threshold (incl. `'off'`).
60
+ * session wall-clock not attributed to any top-level scope). The remainder
61
+ * is left out when overlapping scopes account for more than the wall-clock.
62
+ * No-op when `logLevel` is suppressed by the global threshold (incl. `'off'`).
58
63
  */
59
64
  report(logLevel?: LogThreshold): void;
60
65
 
@@ -62,7 +67,13 @@ export interface ProfileSession {
62
67
  records(): readonly ProfileRecord[];
63
68
  }
64
69
 
65
- interface ScopeFrame {
70
+ /**
71
+ * One in-flight {@link ProfileSession.scope} call. Held on the `protected`
72
+ * {@link DefaultProfileSession.stack} and named by
73
+ * {@link DefaultProfileSession.closeFrame}, so a subclass has to name it too.
74
+ */
75
+ export interface ProfileScopeFrame {
76
+ /** The {@link ProfileSession.scope} id, under which frames sharing it aggregate into one record. */
66
77
  readonly id: string;
67
78
  /** Session-stopwatch reading when this scope began. */
68
79
  readonly start: number;
@@ -78,7 +89,9 @@ interface ScopeFrame {
78
89
  export class DefaultProfileSession implements ProfileSession {
79
90
  /** Single monotonic timeline for the whole session; per-scope readings are deltas off it. */
80
91
  protected readonly sessionStopwatch: Stopwatch;
81
- protected readonly stack: ScopeFrame[] = [];
92
+ /** The scopes in flight, in the order they started. */
93
+ protected readonly stack: ProfileScopeFrame[] = [];
94
+ /** Self-time in milliseconds per id, one sample per closed scope, which {@link records} sums. */
82
95
  protected readonly entries = new Map<string, number[]>();
83
96
 
84
97
  constructor(
@@ -92,7 +105,7 @@ export class DefaultProfileSession implements ProfileSession {
92
105
  scope<T>(id: string, fn: () => Promise<T>): Promise<T>;
93
106
  scope<T>(id: string, fn: () => T): T;
94
107
  scope<T>(id: string, fn: () => T | Promise<T>): T | Promise<T> {
95
- const frame: ScopeFrame = { id, start: this.sessionStopwatch.elapsedMs, childMs: 0 };
108
+ const frame: ProfileScopeFrame = { id, start: this.sessionStopwatch.elapsedMs, childMs: 0 };
96
109
  this.stack.push(frame);
97
110
  let result: T | Promise<T>;
98
111
  try {
@@ -117,15 +130,22 @@ export class DefaultProfileSession implements ProfileSession {
117
130
  return result;
118
131
  }
119
132
 
120
- /** Pop `frame`, charge its full duration to the parent, and record its self-time. */
121
- protected closeFrame(frame: ScopeFrame): void {
122
- this.stack.pop();
133
+ /**
134
+ * Remove `frame`, charge its full duration to the frame below it, and record
135
+ * its self-time. Overlapping scopes can together outlast the one below
136
+ * them, whose self-time is then zero rather than negative.
137
+ */
138
+ protected closeFrame(frame: ProfileScopeFrame): void {
139
+ const index = this.stack.lastIndexOf(frame);
140
+ if (index >= 0) {
141
+ this.stack.splice(index, 1);
142
+ }
123
143
  const duration = this.sessionStopwatch.elapsedMs - frame.start;
124
- const parent = this.stack[this.stack.length - 1];
144
+ const parent = index > 0 ? this.stack[index - 1] : undefined;
125
145
  if (parent) {
126
146
  parent.childMs += duration;
127
147
  }
128
- const selfMs = duration - frame.childMs;
148
+ const selfMs = Math.max(0, duration - frame.childMs);
129
149
  const samples = this.entries.get(frame.id);
130
150
  if (samples) {
131
151
  samples.push(selfMs);
package/src/profiling.ts CHANGED
@@ -17,15 +17,23 @@
17
17
 
18
18
  /** Which dimensions to capture, and the two sampling knobs. All off by default. */
19
19
  export interface ProfileCaptureOptions {
20
+ /** Sample the CPU at {@link cpuIntervalMicros} into a `server-cpu` artefact. */
20
21
  cpu?: boolean;
22
+ /** Sample allocations at {@link allocationIntervalBytes} into a `server-alloc` artefact. */
21
23
  allocation?: boolean;
24
+ /** Record every GC pause, summed by kind in `server-summary` rather than as its own artefact. */
22
25
  gc?: boolean;
26
+ /**
27
+ * Sample event-loop delay, summarised as mean, p50, p99 and max in
28
+ * `server-summary` rather than as its own artefact. Absent there when the
29
+ * sampler recorded nothing.
30
+ */
23
31
  eventLoopDelay?: boolean;
24
32
  /** Also drop a retained `.heapsnapshot` at stop (for the memory analyzer). */
25
33
  heapSnapshot?: boolean;
26
- /** CPU sampling interval in µs (implementation default 1000). */
34
+ /** CPU sampling interval in µs (implementation default `DEFAULT_CPU_INTERVAL_MICROS`). */
27
35
  cpuIntervalMicros?: number;
28
- /** Allocation sampling interval in bytes (implementation default 32768). */
36
+ /** Allocation sampling interval in bytes (implementation default `DEFAULT_ALLOCATION_INTERVAL_BYTES`). */
29
37
  allocationIntervalBytes?: number;
30
38
  }
31
39
 
@@ -72,12 +80,17 @@ export interface ProfilingArtifact {
72
80
 
73
81
  /** What a skill reads to know what to expect from a run (browser entries only in `app` mode, etc.). */
74
82
  export interface ProfilingEnvironment {
83
+ /** `harness` for a headless run, `app` for one that drove the real app. */
75
84
  mode: 'harness' | 'app';
76
85
  /** Whether a cgroup memory controller is present (running in a pod) vs local. */
77
86
  container: boolean;
87
+ /** The commit the run reflects, which a finding's `file:line` is read against. Absent when the caller named none. */
78
88
  commit?: string;
89
+ /** The workspace under test as the caller named it. Absent when it named none. */
79
90
  workspace?: string;
91
+ /** The `process.version` of the process that wrote the manifest, which for an e2e run is the test runner, not the server. */
80
92
  node: string;
93
+ /** Captured windows in capture order, each with its duration in milliseconds. */
81
94
  windows: { label: string; ms: number }[];
82
95
  }
83
96
 
package/src/project.ts CHANGED
@@ -40,7 +40,10 @@
40
40
  * adopter-specific extension carried transparently through the typed
41
41
  * surface. The framework imposes no format on extension fields — they
42
42
  * ride on the JSON-RPC envelope as additional properties when crossing
43
- * the wire.
43
+ * the wire. A client receives a project's own enumerable fields as JSON: a
44
+ * getter or a method does not cross, and a class instance in a field arrives
45
+ * as a plain object, so the project type a client protocol declares holds the
46
+ * data fields alone.
44
47
  */
45
48
  export interface Project {
46
49
  /**
@@ -0,0 +1,21 @@
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
+ * A random v4 UUID. Not `crypto.randomUUID`: a browser offers it only in a
12
+ * secure context, and a client may be served over plain HTTP.
13
+ */
14
+ export function randomUuid(): string {
15
+ const bytes = globalThis.crypto.getRandomValues(new Uint8Array(16));
16
+ // Version 4, RFC 4122 variant.
17
+ bytes[6] = (bytes[6] & 0x0f) | 0x40;
18
+ bytes[8] = (bytes[8] & 0x3f) | 0x80;
19
+ const hex = Array.from(bytes, byte => byte.toString(16).padStart(2, '0')).join('');
20
+ return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
21
+ }