@openchamber/web 1.24.0 → 1.24.2

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 (227) hide show
  1. package/bin/cli.test.js +29 -6
  2. package/bin/lib/cli-http.js +5 -1
  3. package/bin/lib/commands-status.js +5 -0
  4. package/dist/assets/{CodeMirrorCommentWidgets-C9kV-n7r.js → CodeMirrorCommentWidgets-B_vBPQYm.js} +1 -1
  5. package/dist/assets/DiffView-Ce0oBXXQ.js +4 -0
  6. package/dist/assets/{FilesView-vjJx7VaM.js → FilesView-DtXueAWP.js} +6 -6
  7. package/dist/assets/GitView-k0hUF8KB.js +6 -0
  8. package/dist/assets/GuestAttachDialog-DIf2U85Z.js +1 -0
  9. package/dist/assets/{JsonTreeView-D_4G5X6-.js → JsonTreeView-4AnJ04Pw.js} +1 -1
  10. package/dist/assets/KaTeX_AMS-Regular-BQhdFMY1.woff2 +0 -0
  11. package/dist/assets/KaTeX_AMS-Regular-DMm9YOAa.woff +0 -0
  12. package/dist/assets/KaTeX_AMS-Regular-DRggAlZN.ttf +0 -0
  13. package/dist/assets/KaTeX_Caligraphic-Bold-ATXxdsX0.ttf +0 -0
  14. package/dist/assets/KaTeX_Caligraphic-Bold-BEiXGLvX.woff +0 -0
  15. package/dist/assets/KaTeX_Caligraphic-Bold-Dq_IR9rO.woff2 +0 -0
  16. package/dist/assets/KaTeX_Caligraphic-Regular-CTRA-rTL.woff +0 -0
  17. package/dist/assets/KaTeX_Caligraphic-Regular-Di6jR-x-.woff2 +0 -0
  18. package/dist/assets/KaTeX_Caligraphic-Regular-wX97UBjC.ttf +0 -0
  19. package/dist/assets/KaTeX_Fraktur-Bold-BdnERNNW.ttf +0 -0
  20. package/dist/assets/KaTeX_Fraktur-Bold-BsDP51OF.woff +0 -0
  21. package/dist/assets/KaTeX_Fraktur-Bold-CL6g_b3V.woff2 +0 -0
  22. package/dist/assets/KaTeX_Fraktur-Regular-CB_wures.ttf +0 -0
  23. package/dist/assets/KaTeX_Fraktur-Regular-CTYiF6lA.woff2 +0 -0
  24. package/dist/assets/KaTeX_Fraktur-Regular-Dxdc4cR9.woff +0 -0
  25. package/dist/assets/KaTeX_Main-Bold-Cx986IdX.woff2 +0 -0
  26. package/dist/assets/KaTeX_Main-Bold-Jm3AIy58.woff +0 -0
  27. package/dist/assets/KaTeX_Main-Bold-waoOVXN0.ttf +0 -0
  28. package/dist/assets/KaTeX_Main-BoldItalic-DxDJ3AOS.woff2 +0 -0
  29. package/dist/assets/KaTeX_Main-BoldItalic-DzxPMmG6.ttf +0 -0
  30. package/dist/assets/KaTeX_Main-BoldItalic-SpSLRI95.woff +0 -0
  31. package/dist/assets/KaTeX_Main-Italic-3WenGoN9.ttf +0 -0
  32. package/dist/assets/KaTeX_Main-Italic-BMLOBm91.woff +0 -0
  33. package/dist/assets/KaTeX_Main-Italic-NWA7e6Wa.woff2 +0 -0
  34. package/dist/assets/KaTeX_Main-Regular-B22Nviop.woff2 +0 -0
  35. package/dist/assets/KaTeX_Main-Regular-Dr94JaBh.woff +0 -0
  36. package/dist/assets/KaTeX_Main-Regular-ypZvNtVU.ttf +0 -0
  37. package/dist/assets/KaTeX_Math-BoldItalic-B3XSjfu4.ttf +0 -0
  38. package/dist/assets/KaTeX_Math-BoldItalic-CZnvNsCZ.woff2 +0 -0
  39. package/dist/assets/KaTeX_Math-BoldItalic-iY-2wyZ7.woff +0 -0
  40. package/dist/assets/KaTeX_Math-Italic-DA0__PXp.woff +0 -0
  41. package/dist/assets/KaTeX_Math-Italic-flOr_0UB.ttf +0 -0
  42. package/dist/assets/KaTeX_Math-Italic-t53AETM-.woff2 +0 -0
  43. package/dist/assets/KaTeX_SansSerif-Bold-CFMepnvq.ttf +0 -0
  44. package/dist/assets/KaTeX_SansSerif-Bold-D1sUS0GD.woff2 +0 -0
  45. package/dist/assets/KaTeX_SansSerif-Bold-DbIhKOiC.woff +0 -0
  46. package/dist/assets/KaTeX_SansSerif-Italic-C3H0VqGB.woff2 +0 -0
  47. package/dist/assets/KaTeX_SansSerif-Italic-DN2j7dab.woff +0 -0
  48. package/dist/assets/KaTeX_SansSerif-Italic-YYjJ1zSn.ttf +0 -0
  49. package/dist/assets/KaTeX_SansSerif-Regular-BNo7hRIc.ttf +0 -0
  50. package/dist/assets/KaTeX_SansSerif-Regular-CS6fqUqJ.woff +0 -0
  51. package/dist/assets/KaTeX_SansSerif-Regular-DDBCnlJ7.woff2 +0 -0
  52. package/dist/assets/KaTeX_Script-Regular-C5JkGWo-.ttf +0 -0
  53. package/dist/assets/KaTeX_Script-Regular-D3wIWfF6.woff2 +0 -0
  54. package/dist/assets/KaTeX_Script-Regular-D5yQViql.woff +0 -0
  55. package/dist/assets/KaTeX_Size1-Regular-C195tn64.woff +0 -0
  56. package/dist/assets/KaTeX_Size1-Regular-Dbsnue_I.ttf +0 -0
  57. package/dist/assets/KaTeX_Size1-Regular-mCD8mA8B.woff2 +0 -0
  58. package/dist/assets/KaTeX_Size2-Regular-B7gKUWhC.ttf +0 -0
  59. package/dist/assets/KaTeX_Size2-Regular-Dy4dx90m.woff2 +0 -0
  60. package/dist/assets/KaTeX_Size2-Regular-oD1tc_U0.woff +0 -0
  61. package/dist/assets/KaTeX_Size3-Regular-CTq5MqoE.woff +0 -0
  62. package/dist/assets/KaTeX_Size3-Regular-DgpXs0kz.ttf +0 -0
  63. package/dist/assets/KaTeX_Size4-Regular-BF-4gkZK.woff +0 -0
  64. package/dist/assets/KaTeX_Size4-Regular-DWFBv043.ttf +0 -0
  65. package/dist/assets/KaTeX_Size4-Regular-Dl5lxZxV.woff2 +0 -0
  66. package/dist/assets/KaTeX_Typewriter-Regular-C0xS9mPB.woff +0 -0
  67. package/dist/assets/KaTeX_Typewriter-Regular-CO6r4hn1.woff2 +0 -0
  68. package/dist/assets/KaTeX_Typewriter-Regular-D3Ib7_Hf.ttf +0 -0
  69. package/dist/assets/{LinearIssuesView-1uKhePfH.js → LinearIssuesView-z_9p2GuO.js} +1 -1
  70. package/dist/assets/{MarkdownImageGallery-Ix7N_2wp.js → MarkdownImageGallery-BC-BNN-s.js} +1 -1
  71. package/dist/assets/{MarkdownRendererImpl-Deel_y5S.js → MarkdownRendererImpl-DyaBTUbH.js} +6 -6
  72. package/dist/assets/{OnboardingScreen-ToIakxb_.js → OnboardingScreen-DKU_yTb6.js} +1 -1
  73. package/dist/assets/{PatchDiff-BjMgezr1.js → PatchDiff-DpGmHRnV.js} +1 -1
  74. package/dist/assets/PierreDiffViewer-CgLl_tp7.js +210 -0
  75. package/dist/assets/{PlanView-BGAzxDwD.js → PlanView-CrfB4qqY.js} +4 -4
  76. package/dist/assets/PluginPane-B5cn-jPy.js +3 -0
  77. package/dist/assets/SettingsView-DMKKwezx.js +29 -0
  78. package/dist/assets/SettingsWindow-DwbnVzOC.js +1 -0
  79. package/dist/assets/{SubmoduleDiffSummary--YFqK6Ih.js → SubmoduleDiffSummary-hkGcvs0E.js} +1 -1
  80. package/dist/assets/{ToolOutputDialog-DRWBvdaJ.js → ToolOutputDialog-DuoIOc0j.js} +6 -6
  81. package/dist/assets/{ToolPartDiffPreview-BxZJCU28.js → ToolPartDiffPreview-t1Bvq_2Y.js} +2 -2
  82. package/dist/assets/{VirtualizedFileDiff-DCXjBGRf.js → VirtualizedFileDiff-BK4aU5qJ.js} +1 -1
  83. package/dist/assets/{WalkthroughView-CfIPvWWa.js → WalkthroughView-Bk6TPNFw.js} +2 -2
  84. package/dist/assets/{appThemeRegistry-DDROzcUO.js → appThemeRegistry-DnOVbJjF.js} +1 -1
  85. package/dist/assets/commitAndPush-DLWNZkkY.js +1 -0
  86. package/dist/assets/{computeVirtualFileMetrics-DHgQIkUq.js → computeVirtualFileMetrics-BVTujWSX.js} +1 -1
  87. package/dist/assets/{de-CX_f-T5U.js → de-zEHaAIi3.js} +4 -4
  88. package/dist/assets/{es-BERmWNn_.js → es-CBDgEzgk.js} +4 -4
  89. package/dist/assets/{fr-BjYYE_-z.js → fr-DjiTptT_.js} +4 -4
  90. package/dist/assets/index-AgOdanig.css +1 -0
  91. package/dist/assets/index-B-2_X4DD.js +40 -0
  92. package/dist/assets/{index-WjufPkIV.js → index-CkA4p2IK.js} +1 -1
  93. package/dist/assets/{isDiffPlainText-D9faP70y.js → isDiffPlainText-D3yX_vx4.js} +1 -1
  94. package/dist/assets/{isFilePlainText-6qOcz5Ky.js → isFilePlainText-CZR9TcHV.js} +1 -1
  95. package/dist/assets/ja-CerMgj3r.js +16 -0
  96. package/dist/assets/{ko-Dty1aUod.js → ko-DHYDAze5.js} +4 -4
  97. package/dist/assets/main-BDka4W43.js +896 -0
  98. package/dist/assets/main-Dl7Dyge_.js +2 -0
  99. package/dist/assets/markdownCore-DFmr0rUR.js +5 -0
  100. package/dist/assets/miniChat-CLKQPPpO.js +2 -0
  101. package/dist/assets/mobile--giD2Q1L.js +2 -0
  102. package/dist/assets/{patchFileDiff-BvU6KOQC.js → patchFileDiff-DRh-AQvd.js} +1 -1
  103. package/dist/assets/{pl-CX-_ROkr.js → pl-BTB_93bq.js} +6 -6
  104. package/dist/assets/{pt-BR-DhRR1arx.js → pt-BR-BwA3oyCK.js} +4 -4
  105. package/dist/assets/renderElectronMiniChatApp-DmB7zpVK.js +2 -0
  106. package/dist/assets/renderMobileApp-pMfOuQfl.js +25 -0
  107. package/dist/assets/runtimeEndpointReset-Gqhmkm3U.js +25 -0
  108. package/dist/assets/{scheduledTasksApi-DW3xqMQ8.js → scheduledTasksApi-CeRAIUmW.js} +1 -1
  109. package/dist/assets/{shikiHighlight-DsxD9QEa.js → shikiHighlight-BfjuB3CD.js} +1 -1
  110. package/dist/assets/{textMateThemeFromAppTheme-Da7ed8qM.js → textMateThemeFromAppTheme-BP60t9SX.js} +1 -1
  111. package/dist/assets/tr-CcPsJoNM.js +7 -0
  112. package/dist/assets/{uk-CD21CXFV.js → uk-B68WibXi.js} +4 -4
  113. package/dist/assets/{useAppFontEffects-hfv2s6mZ.js → useAppFontEffects-DcdrFv2Z.js} +147 -147
  114. package/dist/assets/{useInlineCommentController-BmtfVbo8.js → useInlineCommentController-Cq4SUjsA.js} +2 -2
  115. package/dist/assets/useRootScrollLock-CSdR6Wnm.js +2 -0
  116. package/dist/assets/{vendor-codemirror-language-data-BPMJpOpT.js → vendor-codemirror-language-data-BFm1Ju2w.js} +2 -2
  117. package/dist/assets/vendor-katex-Cp579UoJ.css +1 -0
  118. package/dist/assets/{vendor-tanstack-react-virtual-g05Cx3z9.js → vendor-tanstack-react-virtual-ClYN8CzB.js} +1 -1
  119. package/dist/assets/{vendor-tanstack-virtual-core-Dubg10g5.js → vendor-tanstack-virtual-core-BewVQryB.js} +1 -1
  120. package/dist/assets/{vendor-zod-fZppILAM.js → vendor-zod-CgAdXWZO.js} +4 -4
  121. package/dist/assets/{web-components-U-hWbgJo.js → web-components-2vo9YsZc.js} +1 -1
  122. package/dist/assets/zh-CN-DbFHYXwP.js +16 -0
  123. package/dist/assets/{zh-TW-CRp5dJU2.js → zh-TW-DmXAwdty.js} +4 -4
  124. package/dist/index.html +6 -4
  125. package/dist/mini-chat.html +6 -4
  126. package/dist/mobile.html +6 -4
  127. package/package.json +6 -5
  128. package/server/index.js +29 -7
  129. package/server/lib/agent-tool/DOCUMENTATION.md +17 -4
  130. package/server/lib/agent-tool/runtime.js +62 -15
  131. package/server/lib/agent-tool/runtime.test.js +88 -2
  132. package/server/lib/changelog/update-notes.js +2 -1
  133. package/server/lib/changelog/update-notes.test.js +19 -0
  134. package/server/lib/event-stream/DOCUMENTATION.md +23 -0
  135. package/server/lib/event-stream/delta-coalescer.js +0 -0
  136. package/server/lib/event-stream/delta-coalescer.test.js +292 -0
  137. package/server/lib/event-stream/global-hub.js +53 -23
  138. package/server/lib/event-stream/global-hub.test.js +160 -0
  139. package/server/lib/event-stream/global-ws-bridge.js +1 -0
  140. package/server/lib/event-stream/index.js +4 -0
  141. package/server/lib/fs/DOCUMENTATION.md +2 -1
  142. package/server/lib/fs/routes.js +56 -4
  143. package/server/lib/fs/routes.test.js +116 -0
  144. package/server/lib/git/DOCUMENTATION.md +18 -5
  145. package/server/lib/git/routes.js +1 -1
  146. package/server/lib/git/serial-refresh.js +93 -0
  147. package/server/lib/git/serial-refresh.test.js +112 -0
  148. package/server/lib/git/service.js +325 -70
  149. package/server/lib/git/service.test.js +291 -0
  150. package/server/lib/github/pr-status.js +5 -5
  151. package/server/lib/github/pr-status.test.js +1 -1
  152. package/server/lib/github/routes.js +4 -4
  153. package/server/lib/guests/DOCUMENTATION.md +5 -3
  154. package/server/lib/guests/background.test.js +83 -0
  155. package/server/lib/guests/catalog.js +20 -9
  156. package/server/lib/guests/catalog.test.js +5 -5
  157. package/server/lib/guests/routes.js +2 -2
  158. package/server/lib/message-queue/DOCUMENTATION.md +10 -1
  159. package/server/lib/message-queue/runtime.js +21 -1
  160. package/server/lib/message-queue/runtime.test.js +52 -5
  161. package/server/lib/notifications/routes.js +5 -0
  162. package/server/lib/openchamber-sessions/routes.js +18 -18
  163. package/server/lib/openchamber-sessions/routes.test.js +49 -0
  164. package/server/lib/opencode/DOCUMENTATION.md +1 -1
  165. package/server/lib/opencode/bootstrap-runtime.js +1 -0
  166. package/server/lib/opencode/feature-routes-runtime.js +5 -0
  167. package/server/lib/opencode/session-runtime.js +68 -0
  168. package/server/lib/opencode/session-runtime.test.js +43 -0
  169. package/server/lib/opencode/settings-helpers.js +3 -0
  170. package/server/lib/opencode/settings-registry.json +8 -0
  171. package/server/lib/permission-auto-accept/DOCUMENTATION.md +8 -0
  172. package/server/lib/permission-auto-accept/runtime.js +13 -0
  173. package/server/lib/permission-auto-accept/runtime.test.js +37 -1
  174. package/server/lib/projects/project-config.js +2 -2
  175. package/server/lib/realtime-proxy-terminal.test.js +150 -0
  176. package/server/lib/realtime-proxy.js +2 -0
  177. package/server/lib/routing/DOCUMENTATION.md +83 -0
  178. package/server/lib/routing/defaults.js +98 -0
  179. package/server/lib/routing/feature-flag.js +11 -0
  180. package/server/lib/routing/history.js +34 -0
  181. package/server/lib/routing/jev.js +98 -0
  182. package/server/lib/routing/routes.http.test.js +100 -0
  183. package/server/lib/routing/routes.js +81 -0
  184. package/server/lib/routing/runtime.js +217 -0
  185. package/server/lib/routing/runtime.test.js +188 -0
  186. package/server/lib/routing/store.js +249 -0
  187. package/server/lib/routing/store.test.js +96 -0
  188. package/server/lib/scheduled-tasks/runtime.js +2 -2
  189. package/server/lib/scheduled-tasks/runtime.test.js +14 -0
  190. package/server/lib/security/request-security.js +9 -5
  191. package/server/lib/security/request-security.test.js +37 -0
  192. package/server/lib/small-model/DOCUMENTATION.md +14 -2
  193. package/server/lib/small-model/call.js +37 -6
  194. package/server/lib/small-model/call.test.js +182 -0
  195. package/server/lib/terminal/DOCUMENTATION.md +21 -2
  196. package/server/lib/terminal/runtime.js +10 -3
  197. package/server/lib/terminal/runtime.test.js +22 -0
  198. package/server/lib/terminal/shutdown.js +106 -0
  199. package/server/lib/terminal/shutdown.test.js +84 -0
  200. package/server/lib/tts/service.test.js +78 -0
  201. package/server/lib/ui-auth/DOCUMENTATION.md +8 -0
  202. package/server/lib/ui-auth/session-cookie.js +51 -0
  203. package/server/lib/ui-auth/session-cookie.test.js +46 -0
  204. package/server/lib/ui-auth/ui-auth.js +13 -9
  205. package/server/lib/ui-auth/ui-auth.test.js +56 -0
  206. package/dist/assets/DiffView-B7YkbY-u.js +0 -4
  207. package/dist/assets/DirtyBranchSwitchDialog-CU0A93Zx.js +0 -1
  208. package/dist/assets/GitView-BLhlMYEY.js +0 -6
  209. package/dist/assets/GuestAttachDialog-B-fwE8r3.js +0 -3
  210. package/dist/assets/PierreDiffViewer-53Ghfnlr.js +0 -161
  211. package/dist/assets/SettingsView-n2X-jfBB.js +0 -29
  212. package/dist/assets/SettingsWindow-Bi8LE9XI.js +0 -1
  213. package/dist/assets/index-DpzTz2UH.css +0 -1
  214. package/dist/assets/index-Dy8xgawx.js +0 -40
  215. package/dist/assets/ja-DxqeAk9N.js +0 -16
  216. package/dist/assets/main-BOYSXfYn.js +0 -896
  217. package/dist/assets/main-Bjcre_GE.js +0 -2
  218. package/dist/assets/markdownCore-DPrwM1rU.js +0 -5
  219. package/dist/assets/miniChat-Dz_bPgMc.js +0 -2
  220. package/dist/assets/mobile-BbZPBJrK.js +0 -2
  221. package/dist/assets/renderElectronMiniChatApp-uNEUkeAI.js +0 -2
  222. package/dist/assets/renderMobileApp-BK1kFlcJ.js +0 -25
  223. package/dist/assets/runtimeEndpointReset-BULHvOwb.js +0 -25
  224. package/dist/assets/tr-cIQtYBpg.js +0 -7
  225. package/dist/assets/useRootScrollLock-Cwc4kSl2.js +0 -1
  226. package/dist/assets/zh-CN-BWvlPHY-.js +0 -16
  227. /package/dist/assets/{vendor-katex-k3Ppq1Ha.js → vendor-katex-CHD9BdDO.js} +0 -0
@@ -0,0 +1,292 @@
1
+ import { describe, expect, it } from 'vitest';
2
+
3
+ import { createDeltaCoalescer, DELTA_COALESCE_WINDOW_MS, resolveDeltaCoalesceWindowMs } from './delta-coalescer.js';
4
+
5
+ let nextId = 0;
6
+
7
+ const delta = (partID, text, { messageID = 'msg_1', field = 'text', directory = '/repo', sessionID = 'ses_1' } = {}) => {
8
+ nextId += 1;
9
+ const id = String(nextId).padStart(6, '0');
10
+ const payload = { id, type: 'message.part.delta', properties: { sessionID, messageID, partID, field, delta: text } };
11
+ return { envelope: { eventId: id, directory, payload }, payload };
12
+ };
13
+
14
+ const other = (type, properties = {}) => {
15
+ nextId += 1;
16
+ const id = String(nextId).padStart(6, '0');
17
+ const payload = { id, type, properties };
18
+ return { envelope: { eventId: id, directory: '/repo', payload }, payload };
19
+ };
20
+
21
+ // A clock and timer queue the test advances by hand, so every assertion about
22
+ // the window is exact instead of racing a real timeout.
23
+ const createHarness = (options = {}) => {
24
+ let time = 1_000_000;
25
+ let timers = [];
26
+ let timerSeq = 0;
27
+ const emitted = [];
28
+ const coalescer = createDeltaCoalescer({
29
+ emit: (event) => emitted.push(event),
30
+ now: () => time,
31
+ setTimer: (callback, delay) => {
32
+ timerSeq += 1;
33
+ timers.push({ id: timerSeq, at: time + delay, callback });
34
+ return timerSeq;
35
+ },
36
+ clearTimer: (id) => {
37
+ timers = timers.filter((timer) => timer.id !== id);
38
+ },
39
+ ...options,
40
+ });
41
+ const advance = (ms) => {
42
+ const target = time + ms;
43
+ for (;;) {
44
+ const due = timers.filter((timer) => timer.at <= target).sort((left, right) => left.at - right.at)[0];
45
+ if (!due) break;
46
+ timers = timers.filter((timer) => timer !== due);
47
+ time = due.at;
48
+ due.callback();
49
+ }
50
+ time = target;
51
+ };
52
+ return { coalescer, emitted, advance, pendingTimers: () => timers.length };
53
+ };
54
+
55
+ const isDelta = (event) => event.payload.type === 'message.part.delta';
56
+ const keyOf = (event) => `${event.payload.properties.messageID}:${event.payload.properties.partID}:${event.payload.properties.field}`;
57
+
58
+ const textByKey = (events) => {
59
+ const text = new Map();
60
+ for (const event of events) {
61
+ if (!isDelta(event)) continue;
62
+ text.set(keyOf(event), (text.get(keyOf(event)) ?? '') + event.payload.properties.delta);
63
+ }
64
+ return text;
65
+ };
66
+
67
+ describe('delta coalescer', () => {
68
+ it('emits the first delta after a quiet spell at once and unchanged', () => {
69
+ const { coalescer, emitted } = createHarness();
70
+ const first = delta('prt_a', 'He');
71
+
72
+ coalescer.push(first);
73
+
74
+ expect(emitted).toEqual([first]);
75
+ expect(emitted[0]).toBe(first);
76
+ });
77
+
78
+ it('merges following deltas for one part field and carries the last fragment id', () => {
79
+ const { coalescer, emitted, advance, pendingTimers } = createHarness();
80
+ const fragments = [delta('prt_a', 'He'), delta('prt_a', 'll'), delta('prt_a', 'o,'), delta('prt_a', ' wor'), delta('prt_a', 'ld')];
81
+
82
+ for (const fragment of fragments) coalescer.push(fragment);
83
+ expect(emitted).toHaveLength(1);
84
+ advance(50);
85
+
86
+ expect(emitted).toHaveLength(2);
87
+ expect(emitted[1].payload.properties).toEqual({ sessionID: 'ses_1', messageID: 'msg_1', partID: 'prt_a', field: 'text', delta: 'llo, world' });
88
+ expect(emitted[1].envelope.eventId).toBe(fragments[4].envelope.eventId);
89
+ expect(emitted[1].payload.id).toBe(fragments[4].payload.id);
90
+ expect(emitted[1].envelope.payload).toBe(emitted[1].payload);
91
+ expect(pendingTimers()).toBe(0);
92
+ // Upstream objects are never mutated: other hub subscribers may hold them.
93
+ expect(fragments.map((fragment) => fragment.payload.properties.delta)).toEqual(['He', 'll', 'o,', ' wor', 'ld']);
94
+ });
95
+
96
+ it('never holds text longer than the window', () => {
97
+ const { coalescer, emitted, advance } = createHarness();
98
+ coalescer.push(delta('prt_a', 'a'));
99
+ coalescer.push(delta('prt_a', 'b'));
100
+
101
+ advance(49);
102
+ expect(emitted).toHaveLength(1);
103
+ advance(1);
104
+ expect(emitted).toHaveLength(2);
105
+ });
106
+
107
+ it('treats every other event as a barrier, so a delta never crosses a part snapshot', () => {
108
+ const { coalescer, emitted, advance } = createHarness();
109
+ const snapshot = other('message.part.updated', { part: { id: 'prt_a', messageID: 'msg_1', text: 'abc' } });
110
+
111
+ coalescer.push(delta('prt_a', 'a'));
112
+ coalescer.push(delta('prt_a', 'b'));
113
+ coalescer.push(delta('prt_a', 'c'));
114
+ coalescer.push(snapshot);
115
+ coalescer.push(delta('prt_a', 'd'));
116
+ coalescer.push(delta('prt_a', 'e'));
117
+ advance(50);
118
+
119
+ expect(emitted.map((event) => (isDelta(event) ? event.payload.properties.delta : event.payload.type)))
120
+ .toEqual(['a', 'bc', 'message.part.updated', 'de']);
121
+ expect(emitted[2]).toBe(snapshot);
122
+ });
123
+
124
+ it('keeps several part fields pending at once and emits them in upstream id order', () => {
125
+ const { coalescer, emitted, advance } = createHarness();
126
+ coalescer.push(delta('prt_lead', '.'));
127
+
128
+ coalescer.push(delta('prt_a', 'a1'));
129
+ coalescer.push(delta('prt_b', 'b1', { messageID: 'msg_2', sessionID: 'ses_2' }));
130
+ coalescer.push(delta('prt_a', 'a2'));
131
+ const lastB = delta('prt_b', 'b2', { messageID: 'msg_2', sessionID: 'ses_2' });
132
+ coalescer.push(lastB);
133
+ advance(50);
134
+
135
+ const merged = emitted.slice(1);
136
+ expect(merged.map((event) => event.payload.properties.delta)).toEqual(['a1a2', 'b1b2']);
137
+ expect(merged[1].envelope.eventId).toBe(lastB.envelope.eventId);
138
+ const ids = emitted.map((event) => event.envelope.eventId);
139
+ expect(ids).toEqual([...ids].sort());
140
+ });
141
+
142
+ it('does not merge the same part across directories, sessions, messages, or fields', () => {
143
+ const { coalescer, emitted, advance } = createHarness();
144
+ coalescer.push(delta('prt_lead', '.'));
145
+
146
+ coalescer.push(delta('prt_a', '1'));
147
+ coalescer.push(delta('prt_a', '2', { directory: '/other' }));
148
+ coalescer.push(delta('prt_a', '3', { messageID: 'msg_2' }));
149
+ coalescer.push(delta('prt_a', '4', { field: 'reasoning' }));
150
+ coalescer.push(delta('prt_a', '5', { sessionID: 'ses_2' }));
151
+ advance(50);
152
+
153
+ expect(emitted.slice(1).map((event) => event.payload.properties.delta)).toEqual(['1', '2', '3', '4', '5']);
154
+ });
155
+
156
+ it('bounds pending text by characters and by part fields, emitting early instead of dropping', () => {
157
+ const { coalescer, emitted } = createHarness({ maxPendingChars: 10, maxPendingKeys: 3 });
158
+ coalescer.push(delta('prt_lead', '.'));
159
+
160
+ let peakChars = 0;
161
+ for (let index = 0; index < 40; index += 1) {
162
+ coalescer.push(delta('prt_a', 'xyz'));
163
+ peakChars = Math.max(peakChars, coalescer.pendingSize().chars);
164
+ }
165
+ expect(peakChars).toBeLessThan(10 + 3);
166
+ expect(textByKey(emitted).get('msg_1:prt_a:text')).toBe('xyz'.repeat(40));
167
+
168
+ let peakKeys = 0;
169
+ for (let index = 0; index < 20; index += 1) {
170
+ coalescer.push(delta(`prt_k${index}`, 'k'));
171
+ peakKeys = Math.max(peakKeys, coalescer.pendingSize().keys);
172
+ }
173
+ expect(peakKeys).toBeLessThanOrEqual(3);
174
+ coalescer.flush();
175
+ expect(emitted.filter((event) => event.payload.properties.delta === 'k')).toHaveLength(20);
176
+ });
177
+
178
+ it('flushes on demand and leaves nothing pending', () => {
179
+ const { coalescer, emitted, pendingTimers } = createHarness();
180
+ coalescer.push(delta('prt_a', 'a'));
181
+ coalescer.push(delta('prt_a', 'b'));
182
+
183
+ coalescer.flush();
184
+
185
+ expect(emitted.map((event) => event.payload.properties.delta)).toEqual(['a', 'b']);
186
+ expect(coalescer.pendingSize()).toEqual({ keys: 0, chars: 0 });
187
+ expect(pendingTimers()).toBe(0);
188
+ });
189
+
190
+ it('passes a malformed delta through as a barrier instead of guessing', () => {
191
+ const { coalescer, emitted } = createHarness();
192
+ const malformed = other('message.part.delta', { messageID: 'msg_1', partID: 'prt_a', field: 'text', delta: 42 });
193
+
194
+ coalescer.push(delta('prt_a', 'a'));
195
+ coalescer.push(delta('prt_a', 'b'));
196
+ coalescer.push(malformed);
197
+
198
+ expect(emitted.map((event) => event.payload.properties.delta)).toEqual(['a', 'b', 42]);
199
+ expect(emitted[2]).toBe(malformed);
200
+ });
201
+
202
+ it('is a pass-through when the window is zero', () => {
203
+ const { coalescer, emitted } = createHarness({ windowMs: 0 });
204
+ const events = [delta('prt_a', 'a'), delta('prt_a', 'b'), delta('prt_a', 'c')];
205
+
206
+ for (const event of events) coalescer.push(event);
207
+
208
+ expect(emitted).toEqual(events);
209
+ });
210
+
211
+ // The properties that make coalescing safe, checked over arbitrary streams
212
+ // rather than the handful of shapes someone thought of.
213
+ it('loses, duplicates, and reorders nothing across randomized streams', () => {
214
+ let seed = 0x5eed;
215
+ const random = () => {
216
+ seed = (seed * 1664525 + 1013904223) % 4294967296;
217
+ return seed / 4294967296;
218
+ };
219
+
220
+ for (let round = 0; round < 40; round += 1) {
221
+ const { coalescer, emitted, advance } = createHarness({ maxPendingChars: 200, maxPendingKeys: 4 });
222
+ const raw = [];
223
+ const partCount = 1 + Math.floor(random() * 6);
224
+ for (let index = 0; index < 600; index += 1) {
225
+ const roll = random();
226
+ let event;
227
+ if (roll < 0.9) {
228
+ const part = Math.floor(random() * partCount);
229
+ event = delta(`prt_${part}`, `${index}|`.repeat(1 + Math.floor(random() * 3)), { messageID: `msg_${part % 2}` });
230
+ } else if (roll < 0.95) {
231
+ event = other('message.part.updated', { part: { id: `prt_${Math.floor(random() * partCount)}`, messageID: 'msg_0' } });
232
+ } else {
233
+ event = other('session.status', { sessionID: 'ses_1', status: { type: 'busy' } });
234
+ }
235
+ raw.push(event);
236
+ coalescer.push(event);
237
+ if (random() < 0.3) advance(Math.floor(random() * 80));
238
+ }
239
+ advance(1_000);
240
+
241
+ // Nothing lost or duplicated: every part field ends with the same text.
242
+ expect(textByKey(emitted)).toEqual(textByKey(raw));
243
+
244
+ // Every other event is delivered once, as the same object, in order.
245
+ expect(emitted.filter((event) => !isDelta(event))).toEqual(raw.filter((event) => !isDelta(event)));
246
+
247
+ // Barrier: the text delivered before each barrier is exactly the text
248
+ // that arrived before it.
249
+ for (const barrier of raw.filter((event) => !isDelta(event))) {
250
+ expect(textByKey(emitted.slice(0, emitted.indexOf(barrier)))).toEqual(textByKey(raw.slice(0, raw.indexOf(barrier))));
251
+ }
252
+
253
+ // Replay cursors: ids stay unique and in upstream order.
254
+ const ids = emitted.map((event) => event.envelope.eventId);
255
+ expect(new Set(ids).size).toBe(ids.length);
256
+ expect(ids).toEqual([...ids].sort());
257
+
258
+ expect(coalescer.pendingSize()).toEqual({ keys: 0, chars: 0 });
259
+ }
260
+ });
261
+
262
+ it('collapses a dense single-part stream to a few frames per window', () => {
263
+ const { coalescer, emitted, advance } = createHarness();
264
+
265
+ // 150 fragments a second for two seconds, the rate measured from OpenCode.
266
+ for (let index = 0; index < 300; index += 1) {
267
+ coalescer.push(delta('prt_a', 'ab'));
268
+ advance(index % 3 === 2 ? 8 : 6);
269
+ }
270
+ advance(50);
271
+
272
+ expect(textByKey(emitted).get('msg_1:prt_a:text')).toBe('ab'.repeat(300));
273
+ expect(emitted.length).toBeLessThanOrEqual(45);
274
+ });
275
+ });
276
+
277
+ describe('resolveDeltaCoalesceWindowMs', () => {
278
+ it('uses the default when unset and accepts zero as off', () => {
279
+ expect(resolveDeltaCoalesceWindowMs({})).toBe(DELTA_COALESCE_WINDOW_MS);
280
+ expect(resolveDeltaCoalesceWindowMs({ OPENCHAMBER_EVENT_DELTA_COALESCE_MS: ' ' })).toBe(DELTA_COALESCE_WINDOW_MS);
281
+ expect(resolveDeltaCoalesceWindowMs({ OPENCHAMBER_EVENT_DELTA_COALESCE_MS: '0' })).toBe(0);
282
+ expect(resolveDeltaCoalesceWindowMs({ OPENCHAMBER_EVENT_DELTA_COALESCE_MS: '25' })).toBe(25);
283
+ });
284
+
285
+ it('keeps the default and says so for anything that is not 0-1000 whole milliseconds', () => {
286
+ for (const value of ['-1', '1001', '12.5', 'fast', 'NaN', '1e9']) {
287
+ const warnings = [];
288
+ expect(resolveDeltaCoalesceWindowMs({ OPENCHAMBER_EVENT_DELTA_COALESCE_MS: value }, (message) => warnings.push(message))).toBe(DELTA_COALESCE_WINDOW_MS);
289
+ expect(warnings).toHaveLength(1);
290
+ }
291
+ });
292
+ });
@@ -1,5 +1,8 @@
1
+ import { randomUUID } from 'node:crypto';
2
+
1
3
  import { createUpstreamSseReader } from './upstream-reader.js';
2
4
  import { serializeMessageStreamWsEvent } from './protocol.js';
5
+ import { createDeltaCoalescer, DELTA_COALESCE_WINDOW_MS } from './delta-coalescer.js';
3
6
 
4
7
  // Raised from 512 → 2048 to improve recovery after brief disconnects during
5
8
  // long-running agent sessions where many events accumulate quickly.
@@ -14,6 +17,7 @@ export function createGlobalMessageStreamHub({
14
17
  upstreamReconnectDelayMs,
15
18
  replayLimit = MESSAGE_STREAM_GLOBAL_REPLAY_LIMIT,
16
19
  replayByteLimit = MESSAGE_STREAM_GLOBAL_REPLAY_BYTES,
20
+ deltaCoalesceWindowMs = DELTA_COALESCE_WINDOW_MS,
17
21
  }) {
18
22
  if (!Number.isSafeInteger(replayLimit) || replayLimit < 0 || !Number.isSafeInteger(replayByteLimit) || replayByteLimit < 0) {
19
23
  throw new RangeError('Replay limits must be nonnegative safe integers');
@@ -23,6 +27,15 @@ export function createGlobalMessageStreamHub({
23
27
  const replay = [];
24
28
  let replayBytes = 0;
25
29
  let latestEventId;
30
+ // OpenCode's event stream carries no SSE ids (verified on 1.18.30: not one
31
+ // frame in a full response), and an event without an id never entered the
32
+ // replay buffer, so a reconnecting browser had no cursor and every event in
33
+ // the gap was gone. The replay log is this hub's own, so the hub numbers
34
+ // what upstream leaves unnumbered. The per-process prefix makes a cursor
35
+ // from before a restart miss instead of matching an unrelated sequence
36
+ // number, which reports `replayReset` and sends the client to repair.
37
+ const replayIdPrefix = `oc-${randomUUID().slice(0, 8)}-`;
38
+ let replaySequence = 0;
26
39
 
27
40
  let controller = null;
28
41
  let reader = null;
@@ -52,7 +65,9 @@ export function createGlobalMessageStreamHub({
52
65
  const normalizeEvent = ({ envelope, payload }) => {
53
66
  const directory =
54
67
  typeof envelope?.directory === 'string' && envelope.directory.length > 0 ? envelope.directory : 'global';
55
- const eventId = typeof envelope?.eventId === 'string' && envelope.eventId.length > 0 ? envelope.eventId : undefined;
68
+ const eventId = typeof envelope?.eventId === 'string' && envelope.eventId.length > 0
69
+ ? envelope.eventId
70
+ : `${replayIdPrefix}${String(++replaySequence).padStart(12, '0')}`;
56
71
  let serializedFrame;
57
72
  return {
58
73
  envelope,
@@ -66,6 +81,34 @@ export function createGlobalMessageStreamHub({
66
81
  };
67
82
  };
68
83
 
84
+ // Replay and fan-out see the same committed sequence: an event enters the
85
+ // replay buffer in the same step that delivers it, so a client's cursor
86
+ // always names a frame the buffer can find.
87
+ const commitEvent = (event) => {
88
+ const normalized = normalizeEvent(event);
89
+ latestEventId = normalized.eventId;
90
+ const serializedFrame = normalized.serialize();
91
+ const bytes = Buffer.byteLength(serializedFrame);
92
+ if (bytes > replayByteLimit) {
93
+ // An oversized live event creates a hole: retain only a contiguous
94
+ // suffix after it, never replay an older prefix across the gap.
95
+ replay.length = 0;
96
+ replayBytes = 0;
97
+ } else {
98
+ replay.push({ eventId: normalized.eventId, serializedFrame, bytes });
99
+ replayBytes += bytes;
100
+ while (replay.length > replayLimit || replayBytes > replayByteLimit) {
101
+ replayBytes -= replay.shift().bytes;
102
+ }
103
+ }
104
+
105
+ for (const subscriber of Array.from(eventSubscribers)) {
106
+ notifySubscriber('event', subscriber, normalized);
107
+ }
108
+ };
109
+
110
+ const coalescer = createDeltaCoalescer({ emit: commitEvent, windowMs: deltaCoalesceWindowMs });
111
+
69
112
  const start = () => {
70
113
  if (reader) {
71
114
  return;
@@ -98,28 +141,7 @@ export function createGlobalMessageStreamHub({
98
141
  notifyStatus({ type: 'disconnect', reason });
99
142
  },
100
143
  onEvent(event) {
101
- const normalized = normalizeEvent(event);
102
- if (normalized.eventId) {
103
- latestEventId = normalized.eventId;
104
- const serializedFrame = normalized.serialize();
105
- const bytes = Buffer.byteLength(serializedFrame);
106
- if (bytes > replayByteLimit) {
107
- // An oversized live event creates a hole: retain only a contiguous
108
- // suffix after it, never replay an older prefix across the gap.
109
- replay.length = 0;
110
- replayBytes = 0;
111
- } else {
112
- replay.push({ eventId: normalized.eventId, serializedFrame, bytes });
113
- replayBytes += bytes;
114
- while (replay.length > replayLimit || replayBytes > replayByteLimit) {
115
- replayBytes -= replay.shift().bytes;
116
- }
117
- }
118
- }
119
-
120
- for (const subscriber of Array.from(eventSubscribers)) {
121
- notifySubscriber('event', subscriber, normalized);
122
- }
144
+ coalescer.push(event);
123
145
  },
124
146
  onError(error) {
125
147
  if (controller?.signal.aborted) {
@@ -139,6 +161,8 @@ export function createGlobalMessageStreamHub({
139
161
 
140
162
  const stop = () => {
141
163
  connected = false;
164
+ // Text that already arrived belongs in the retained replay suffix.
165
+ coalescer.flush();
142
166
  reader?.stop();
143
167
  if (controller && !controller.signal.aborted) {
144
168
  controller.abort();
@@ -170,6 +194,12 @@ export function createGlobalMessageStreamHub({
170
194
  statusSubscribers.delete(subscriber);
171
195
  };
172
196
  },
197
+ // A client that becomes ready must not receive text from before it was
198
+ // ready merged into its first live delta, so the bridge commits pending
199
+ // deltas before it reads the replay tail.
200
+ flushPending() {
201
+ coalescer.flush();
202
+ },
173
203
  replayAfter(eventId) {
174
204
  if (!eventId) {
175
205
  return [];
@@ -62,6 +62,166 @@ async function waitForAssertion(assertion) {
62
62
  throw lastError;
63
63
  }
64
64
 
65
+ const deltaBlock = (id, text, partID = 'prt_a') => `id: ${id}\ndata: ${JSON.stringify({
66
+ id, type: 'message.part.delta',
67
+ properties: { sessionID: 'ses_1', messageID: 'msg_1', partID, field: 'text', delta: text },
68
+ })}\n\n`;
69
+
70
+ const createDeltaHub = ({ blocks, deltaCoalesceWindowMs }) => createGlobalMessageStreamHub({
71
+ buildOpenCodeUrl: (pathname) => `http://127.0.0.1:4096${pathname}`,
72
+ getOpenCodeAuthHeaders: () => ({}),
73
+ upstreamReconnectDelayMs: 60_000,
74
+ deltaCoalesceWindowMs,
75
+ fetchImpl: async () => createSseResponse({ blocks }),
76
+ });
77
+
78
+ // What a browser holds after applying frames in order: text per part, and the
79
+ // text each part had when a snapshot barrier passed.
80
+ const applyFrames = (frames) => {
81
+ const text = {};
82
+ const barriers = [];
83
+ for (const frame of frames) {
84
+ const payload = frame.payload;
85
+ if (payload.type === 'message.part.delta') {
86
+ text[payload.properties.partID] = (text[payload.properties.partID] ?? '') + payload.properties.delta;
87
+ } else {
88
+ barriers.push({ id: payload.id, seen: { ...text } });
89
+ }
90
+ }
91
+ return { text, barriers };
92
+ };
93
+
94
+ describe('delta coalescing in the global hub', () => {
95
+ it('delivers the same text in far fewer frames', async () => {
96
+ const words = Array.from({ length: 120 }, (_, index) => `w${index} `);
97
+ const hub = createDeltaHub({ blocks: words.map((word, index) => deltaBlock(`e${String(index).padStart(4, '0')}`, word)) });
98
+ const received = [];
99
+ hub.subscribeEvent((event) => received.push(event));
100
+ try {
101
+ hub.start();
102
+ await waitForAssertion(() => expect(applyFrames(received).text.prt_a).toBe(words.join('')));
103
+ expect(received.length).toBeLessThanOrEqual(3);
104
+ expect(received.at(-1).eventId).toBe('e0119');
105
+ } finally { hub.stop(); }
106
+ });
107
+
108
+ it('resumes from any cursor without losing or repeating text', async () => {
109
+ const blocks = [];
110
+ let id = 0;
111
+ const nextId = () => `e${String(id++).padStart(4, '0')}`;
112
+ for (let index = 0; index < 40; index += 1) blocks.push(deltaBlock(nextId(), `a${index}.`, index % 3 === 0 ? 'prt_b' : 'prt_a'));
113
+ const snapshotId = nextId();
114
+ blocks.push(`id: ${snapshotId}\ndata: ${JSON.stringify({ id: snapshotId, type: 'message.part.updated', properties: { part: { id: 'prt_a', messageID: 'msg_1' } } })}\n\n`);
115
+ for (let index = 0; index < 40; index += 1) blocks.push(deltaBlock(nextId(), `b${index}.`));
116
+
117
+ const hub = createDeltaHub({ blocks });
118
+ const received = [];
119
+ hub.subscribeEvent((event) => received.push(event));
120
+ try {
121
+ hub.start();
122
+ const expectedA = [...Array(40).keys()].filter((index) => index % 3 !== 0).map((index) => `a${index}.`).join('')
123
+ + [...Array(40).keys()].map((index) => `b${index}.`).join('');
124
+ await waitForAssertion(() => expect(applyFrames(received).text.prt_a).toBe(expectedA));
125
+ expect(received.length).toBeLessThan(blocks.length / 4);
126
+
127
+ const complete = applyFrames(received);
128
+ // The snapshot barrier saw exactly the text that arrived before it.
129
+ expect(complete.barriers).toHaveLength(1);
130
+ expect(complete.barriers[0].seen.prt_a).toBe([...Array(40).keys()].filter((index) => index % 3 !== 0).map((index) => `a${index}.`).join(''));
131
+
132
+ // A socket that drops after any frame reconnects with that frame's id.
133
+ for (let cut = 0; cut < received.length; cut += 1) {
134
+ const tail = hub.replayAfter(received[cut].eventId);
135
+ expect(tail).not.toBeNull();
136
+ const replayed = tail.map((entry) => JSON.parse(entry.serializedFrame));
137
+ expect(applyFrames([...received.slice(0, cut + 1), ...replayed])).toEqual(complete);
138
+ }
139
+ } finally { hub.stop(); }
140
+ });
141
+
142
+ // OpenCode 1.18 sends no SSE ids at all. Before the hub numbered such events
143
+ // itself the replay buffer stayed empty and every reconnect lost its gap.
144
+ it('numbers id-less upstream events so a reconnect resumes from any cursor', async () => {
145
+ const idless = (type, properties) => `data: ${JSON.stringify({ type, properties })}\n\n`;
146
+ const blocks = [];
147
+ for (let index = 0; index < 30; index += 1) {
148
+ blocks.push(idless('message.part.delta', { sessionID: 'ses_1', messageID: 'msg_1', partID: 'prt_a', field: 'text', delta: `a${index}.` }));
149
+ }
150
+ blocks.push(idless('message.part.updated', { part: { id: 'prt_a', messageID: 'msg_1' } }));
151
+ for (let index = 0; index < 30; index += 1) {
152
+ blocks.push(idless('message.part.delta', { sessionID: 'ses_1', messageID: 'msg_1', partID: 'prt_a', field: 'text', delta: `b${index}.` }));
153
+ }
154
+
155
+ const hub = createDeltaHub({ blocks });
156
+ const received = [];
157
+ hub.subscribeEvent((event) => received.push(event));
158
+ try {
159
+ hub.start();
160
+ const expected = [...Array(30).keys()].map((index) => `a${index}.`).join('') + [...Array(30).keys()].map((index) => `b${index}.`).join('');
161
+ await waitForAssertion(() => expect(applyFrames(received).text.prt_a).toBe(expected));
162
+
163
+ const ids = received.map((event) => event.eventId);
164
+ expect(ids.every((eventId) => typeof eventId === 'string' && eventId.length > 0)).toBe(true);
165
+ expect(new Set(ids).size).toBe(ids.length);
166
+ expect(ids).toEqual([...ids].sort());
167
+
168
+ const complete = applyFrames(received);
169
+ for (let cut = 0; cut < received.length; cut += 1) {
170
+ const tail = hub.replayAfter(received[cut].eventId);
171
+ expect(tail).not.toBeNull();
172
+ const replayed = tail.map((entry) => JSON.parse(entry.serializedFrame));
173
+ expect(replayed.every((frame) => typeof frame.eventId === 'string')).toBe(true);
174
+ expect(applyFrames([...received.slice(0, cut + 1), ...replayed])).toEqual(complete);
175
+ }
176
+
177
+ // A cursor minted by another server process must miss, never match by
178
+ // sequence number: the bridge then reports replayReset and the client
179
+ // repairs from HTTP instead of trusting a wrong tail.
180
+ const foreign = ids[0].replace(/^oc-[^-]+-/, 'oc-00000000-');
181
+ expect(foreign).not.toBe(ids[0]);
182
+ expect(hub.replayAfter(foreign)).toBeNull();
183
+ } finally { hub.stop(); }
184
+ });
185
+
186
+ it('keeps pending text for replay when the hub stops', async () => {
187
+ const hub = createDeltaHub({
188
+ deltaCoalesceWindowMs: 60_000,
189
+ blocks: ['one ', 'two ', 'three'].map((text, index) => deltaBlock(`e${index}`, text)),
190
+ });
191
+ const received = [];
192
+ hub.subscribeEvent((event) => received.push(event));
193
+ hub.start();
194
+ // The window is a minute, so only the leading delta has been delivered.
195
+ await waitForAssertion(() => expect(received).toHaveLength(1));
196
+ await new Promise((resolve) => setTimeout(resolve, 20));
197
+
198
+ hub.stop();
199
+
200
+ const tail = hub.replayAfter('e0').map((entry) => JSON.parse(entry.serializedFrame));
201
+ expect(applyFrames(tail).text.prt_a).toBe('two three');
202
+ expect(tail.at(-1).eventId).toBe('e2');
203
+ });
204
+
205
+ it('commits pending text on demand, so a client readied later starts after it', async () => {
206
+ const hub = createDeltaHub({
207
+ deltaCoalesceWindowMs: 60_000,
208
+ blocks: ['one ', 'two ', 'three'].map((text, index) => deltaBlock(`e${index}`, text)),
209
+ });
210
+ const received = [];
211
+ hub.subscribeEvent((event) => received.push(event));
212
+ try {
213
+ hub.start();
214
+ await waitForAssertion(() => expect(received).toHaveLength(1));
215
+ await new Promise((resolve) => setTimeout(resolve, 20));
216
+
217
+ hub.flushPending();
218
+
219
+ expect(applyFrames(received).text.prt_a).toBe('one two three');
220
+ expect(hub.replayAfter('e2')).toEqual([]);
221
+ } finally { hub.stop(); }
222
+ });
223
+ });
224
+
65
225
  describe('createGlobalMessageStreamHub', () => {
66
226
  it('continues fanout when an event subscriber throws', async () => {
67
227
  const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
@@ -46,6 +46,7 @@ export function createGlobalMessageStreamWsBridge({
46
46
  return;
47
47
  }
48
48
 
49
+ globalHub.flushPending();
49
50
  const replay = globalHub.replayAfter(requestedLastEventId);
50
51
  const ready = { type: 'ready', scope: 'global' };
51
52
  if (replay === null) ready.replayReset = true;
@@ -7,6 +7,10 @@ export {
7
7
  createGlobalMessageStreamHub,
8
8
  } from './global-hub.js';
9
9
 
10
+ export {
11
+ resolveDeltaCoalesceWindowMs,
12
+ } from './delta-coalescer.js';
13
+
10
14
  export {
11
15
  DEFAULT_UPSTREAM_STALL_TIMEOUT_MS,
12
16
  UPSTREAM_STALL_TIMEOUT_CONCURRENT_MS,
@@ -51,7 +51,8 @@ twelve children.
51
51
  ## Notes for contributors
52
52
  - Keep filesystem policy (workspace root checks, error mapping, exec timeout behavior) inside this module, not in the composition root.
53
53
  - Workspace checks accept, besides the active workspace and its worktrees, the **managed roots**: the OpenChamber config root and the managed chats root (`managedChatsRoot` dependency; `OPENCHAMBER_CHATS_DIR` upstream, default `<config root>/chats`). Chat worktrees may legitimately live outside every project workspace.
54
- - `GET /api/fs/home` answers `{ home, chatsRoot }`. `chatsRoot` is the server-resolved managed chats root; clients must use it instead of joining `home` + the well-known segment (a relocated root does not contain that segment).
54
+ - `GET /api/fs/home` preserves `{ home, chatsRoot }` and adds `canonicalChatsRoot` and `canonicalLegacyChatsRoot`, resolved by the server's filesystem. If a root does not exist yet, it resolves the nearest existing ancestor and appends the missing segments, without creating directories. Errors resolving the configured root fail the request. A failed legacy lookup warns and omits only that optional alias, retaining exact legacy matching without blocking an accessible relocated root. Clients use confirmed aliases for exact membership while keeping the original roots as folder/scope identities. `chatsRoot` is the server-resolved managed chats root; clients must use it instead of joining `home` + the well-known segment (a relocated root does not contain that segment).
55
+ - Workspace authorization accepts both the configured managed paths and their filesystem-confirmed canonical roots. Canonical lookup runs only when lexical workspace/managed-root checks fail. One failed managed-root lookup cannot reject an independently authorized root or worktree. Path comparisons remain case-sensitive on POSIX: distinct Linux directories named `chats` and `Chats` never become aliases through string folding. Shared UI keeps exact root membership for classification and directory cleanup.
55
56
  - Filesystem `EPERM`/`EACCES` failures use the stable `reason: "os-permission"` response marker. Policy denials such as workspace-boundary or missing-grant failures must not use that marker because a native folder picker cannot remediate them.
56
57
  - `GET /api/fs/directory-stat?path=...` uses one `stat` without listing contents or resolving project topology. It follows the same authenticated directory-discovery path policy as `/api/fs/list`, including targets outside the active workspace. A directory returns `{ isDirectory: true }`; `ENOENT` returns `not-found`, and a file or `ENOTDIR` returns `not-directory`. Permission and other failures remain distinct from a missing path. VS Code explicitly returns 501, so the shared client treats its probe as unknown.
57
58
  - Read-only routes authorize the requested path against the workspace before resolving symlinks. A symlink reached through the workspace may therefore target a file outside it, while a directly requested outside path still requires an exact-path grant. Write routes keep canonical-target boundary checks.