@kabmo-labs/kabmo 0.8.0 → 0.10.0

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 (391) hide show
  1. package/README.md +52 -0
  2. package/dist/client/index.html +1 -1
  3. package/dist/client/index.js +336 -336
  4. package/dist/client/static/js/index-18xe7655.css +1 -0
  5. package/dist/client/static/js/{index-qwpvd9fv.js → index-nxpe7835.js} +933 -898
  6. package/package.json +11 -1
  7. package/src/client/components/common/code-surface/bracket-guide.test.ts +249 -0
  8. package/src/client/components/common/code-surface/bracket-guide.ts +131 -0
  9. package/src/client/components/common/code-surface/documents.test.ts +122 -0
  10. package/src/client/components/common/code-surface/documents.ts +286 -0
  11. package/src/client/components/common/code-surface/extensions.test.ts +115 -0
  12. package/src/client/components/common/code-surface/extensions.ts +295 -0
  13. package/src/client/components/common/code-surface/fold-stack.test.ts +218 -0
  14. package/src/client/components/common/code-surface/fold.test.ts +148 -0
  15. package/src/client/components/common/code-surface/fold.ts +118 -0
  16. package/src/client/components/common/code-surface/handle.ts +102 -0
  17. package/src/client/components/common/code-surface/indent-guide.ts +104 -0
  18. package/src/client/components/common/code-surface/indent.test.ts +239 -0
  19. package/src/client/components/common/code-surface/indent.ts +124 -0
  20. package/src/client/components/common/code-surface/index.tsx +283 -314
  21. package/src/client/components/common/code-surface/keymap.test.ts +133 -0
  22. package/src/client/components/common/code-surface/keymap.ts +256 -0
  23. package/src/client/components/common/code-surface/line-commands.test.ts +90 -0
  24. package/src/client/components/common/code-surface/line-commands.ts +103 -0
  25. package/src/client/components/common/code-surface/marks.ts +74 -0
  26. package/src/client/components/common/code-surface/props.ts +87 -0
  27. package/src/client/components/common/code-surface/shiki.ts +234 -0
  28. package/src/client/components/common/code-surface/stream-language.test.ts +118 -0
  29. package/src/client/components/common/code-surface/stream-language.ts +123 -0
  30. package/src/client/components/common/code-surface/sync.test.ts +185 -0
  31. package/src/client/components/common/code-surface/sync.ts +58 -0
  32. package/src/client/components/common/code-surface/theme.test.ts +237 -0
  33. package/src/client/components/common/code-surface/theme.ts +224 -0
  34. package/src/client/components/common/code-surface/typing.test.ts +97 -0
  35. package/src/client/components/common/code-surface/typing.ts +111 -0
  36. package/src/client/components/common/editor-view-buttons/index.test.tsx +148 -0
  37. package/src/client/components/common/editor-view-buttons/index.tsx +109 -0
  38. package/src/client/components/common/shell-update-button/Modal.tsx +163 -0
  39. package/src/client/components/common/shell-update-button/index.test.tsx +251 -0
  40. package/src/client/components/common/shell-update-button/index.tsx +125 -0
  41. package/src/client/components/common/toolbar-row/index.test.tsx +215 -0
  42. package/src/client/components/common/toolbar-row/index.tsx +260 -0
  43. package/src/client/components/layout/desktop-layout/WorkspacePanels.tsx +58 -78
  44. package/src/client/components/layout/desktop-layout/index.tsx +65 -67
  45. package/src/client/components/layout/desktop-layout/resizer/index.tsx +6 -3
  46. package/src/client/components/layout/session-sidebar/Footer.tsx +8 -15
  47. package/src/client/components/mobile/LayoutWrapper.tsx +1 -0
  48. package/src/client/components/mobile/mobile-right-sidebar/FullDiff.tsx +3 -0
  49. package/src/client/components/mobile/mobile-right-sidebar/FullEditor.tsx +98 -71
  50. package/src/client/components/mobile/mobile-right-sidebar/Header.tsx +20 -1
  51. package/src/client/components/mobile/mobile-right-sidebar/OutlineButton.test.tsx +137 -0
  52. package/src/client/components/mobile/mobile-right-sidebar/OutlineButton.tsx +14 -3
  53. package/src/client/components/mobile/mobile-right-sidebar/editor-props.ts +25 -0
  54. package/src/client/components/mobile/mobile-session-sidebar/Footer.tsx +9 -21
  55. package/src/client/components/settings/Modal.tsx +8 -0
  56. package/src/client/components/settings/categories/AppearanceSettings.tsx +9 -0
  57. package/src/client/components/settings/categories/appearance-settings/EditorIndentSection.tsx +87 -0
  58. package/src/client/components/settings/categories/appearance-settings/EditorThemeSection.test.tsx +167 -0
  59. package/src/client/components/settings/categories/appearance-settings/EditorThemeSection.tsx +184 -0
  60. package/src/client/components/settings/categories/appearance-settings/EditorTypingSection.tsx +99 -0
  61. package/src/client/components/settings/categories/project-settings/WorktreeSettings.tsx +8 -2
  62. package/src/client/components/workspace/chat-timeline/EmptyWorkspacePrompt.tsx +16 -8
  63. package/src/client/components/workspace/chat-timeline/SystemNotice.tsx +9 -4
  64. package/src/client/components/workspace/chat-timeline/TaskResultContent.tsx +9 -7
  65. package/src/client/components/workspace/chat-timeline/chat-input/expansion.ts +9 -4
  66. package/src/client/components/workspace/chat-timeline/chat-input/index.tsx +15 -11
  67. package/src/client/components/workspace/chat-timeline/chat-input/stretch-send.test.tsx +165 -0
  68. package/src/client/components/workspace/chat-timeline/target-picker/WorkspacePicker.test.tsx +132 -0
  69. package/src/client/components/workspace/chat-timeline/target-picker/WorkspacePicker.tsx +120 -0
  70. package/src/client/components/workspace/chat-timeline/target-picker/WorktreePicker.test.tsx +319 -0
  71. package/src/client/components/workspace/chat-timeline/target-picker/WorktreePicker.tsx +285 -0
  72. package/src/client/components/workspace/chat-timeline/tool-renderers/ask-panel/index.test.ts +2 -0
  73. package/src/client/components/workspace/chat-timeline/tool-renderers/hashline-patch/index.tsx +34 -29
  74. package/src/client/components/workspace/chat-timeline/tool-renderers/panels/AstEdit.tsx +11 -9
  75. package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Bash.tsx +10 -7
  76. package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Edit.tsx +25 -8
  77. package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Eval.tsx +8 -11
  78. package/src/client/components/workspace/chat-timeline/tool-renderers/panels/FileMatches.test.ts +114 -0
  79. package/src/client/components/workspace/chat-timeline/tool-renderers/panels/FileMatches.tsx +111 -0
  80. package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Read.tsx +7 -7
  81. package/src/client/components/workspace/chat-timeline/tool-renderers/panels/SearchTool.tsx +9 -59
  82. package/src/client/components/workspace/chat-timeline/tool-renderers/shared/ArtifactBody.tsx +2 -7
  83. package/src/client/components/workspace/chat-timeline/tool-renderers/shared/DiffView.test.ts +43 -30
  84. package/src/client/components/workspace/chat-timeline/tool-renderers/shared/DiffView.tsx +49 -32
  85. package/src/client/components/workspace/chat-timeline/tool-renderers/shared/ExcerptCode.tsx +8 -8
  86. package/src/client/components/workspace/chat-timeline/tool-renderers/shared/HighlightedCode.tsx +35 -0
  87. package/src/client/components/workspace/chat-timeline/tool-renderers/shared/JsonCodeBlock.tsx +4 -4
  88. package/src/client/components/workspace/diff-panel/MergeDiffView.tsx +179 -0
  89. package/src/client/components/workspace/diff-panel/ThemedMergeDiffView.tsx +24 -0
  90. package/src/client/components/workspace/diff-panel/Toolbar.tsx +224 -186
  91. package/src/client/components/workspace/diff-panel/ToolbarSegments.tsx +98 -0
  92. package/src/client/components/workspace/diff-panel/index.tsx +76 -145
  93. package/src/client/components/workspace/diff-panel/merge.ts +315 -0
  94. package/src/client/components/workspace/editor/Body.tsx +28 -7
  95. package/src/client/components/workspace/editor/CommandPalette.tsx +6 -6
  96. package/src/client/components/workspace/editor/ConflictStrip.tsx +77 -0
  97. package/src/client/components/workspace/editor/Dialogs.tsx +53 -0
  98. package/src/client/components/workspace/editor/EmptyEditor.tsx +23 -0
  99. package/src/client/components/workspace/editor/ExternalChangeStrip.tsx +61 -0
  100. package/src/client/components/workspace/editor/FileDocument.tsx +146 -63
  101. package/src/client/components/workspace/editor/FilePane.tsx +47 -7
  102. package/src/client/components/workspace/editor/FindWidget.test.ts +9 -5
  103. package/src/client/components/workspace/editor/FindWidget.tsx +36 -2
  104. package/src/client/components/workspace/editor/GoToLineModal.tsx +123 -0
  105. package/src/client/components/workspace/editor/Notices.tsx +71 -0
  106. package/src/client/components/workspace/editor/Pane.tsx +118 -0
  107. package/src/client/components/workspace/editor/PluginColumn.tsx +32 -0
  108. package/src/client/components/workspace/editor/QuickOpenModal.tsx +146 -0
  109. package/src/client/components/workspace/editor/SaveAsModal.tsx +175 -0
  110. package/src/client/components/workspace/editor/SplitColumn.tsx +98 -0
  111. package/src/client/components/workspace/editor/SplitPane.tsx +57 -0
  112. package/src/client/components/workspace/editor/StatusBar.tsx +106 -0
  113. package/src/client/components/workspace/editor/TabContextMenu.tsx +75 -0
  114. package/src/client/components/workspace/editor/Tabs.tsx +195 -18
  115. package/src/client/components/workspace/editor/Toolbar.tsx +110 -140
  116. package/src/client/components/workspace/editor/index.tsx +201 -193
  117. package/src/client/components/workspace/editor/props.ts +64 -0
  118. package/src/client/components/workspace/editor/toolbar-clusters.tsx +300 -0
  119. package/src/client/components/workspace/file-explorer/Modals.tsx +14 -1
  120. package/src/client/components/workspace/file-explorer/OutlineSection.test.tsx +18 -0
  121. package/src/client/components/workspace/file-explorer/OutlineSection.tsx +10 -3
  122. package/src/client/components/workspace/file-explorer/TreeItem.tsx +2 -1
  123. package/src/client/components/workspace/file-explorer/clipboard.test.ts +3 -1
  124. package/src/client/components/workspace/file-explorer/index.test.ts +61 -0
  125. package/src/client/components/workspace/file-explorer/index.tsx +10 -48
  126. package/src/client/components/workspace/file-explorer/local-edit.test.ts +8 -3
  127. package/src/client/components/workspace/search-panel/Body.tsx +7 -2
  128. package/src/client/components/workspace/worktree-modal/index.test.tsx +118 -0
  129. package/src/client/hooks/chat/composer/file-drop.ts +22 -37
  130. package/src/client/hooks/chat/composer/pipeline.test.ts +76 -0
  131. package/src/client/hooks/chat/composer/pipeline.ts +12 -1
  132. package/src/client/hooks/chat/omp/reconnect.test.ts +338 -0
  133. package/src/client/hooks/chat/omp/reconnect.ts +187 -0
  134. package/src/client/hooks/chat/omp/run-liveness.test.ts +71 -0
  135. package/src/client/hooks/chat/omp/run-liveness.ts +65 -0
  136. package/src/client/hooks/chat/omp/session-list.activity.test.ts +114 -0
  137. package/src/client/hooks/chat/omp/session-list.test-util.ts +164 -0
  138. package/src/client/hooks/chat/omp/session-list.test.ts +30 -150
  139. package/src/client/hooks/chat/omp/session-list.tsx +69 -9
  140. package/src/client/hooks/chat/timeline/generating.ts +19 -2
  141. package/src/client/hooks/chat/timeline/index.ts +19 -24
  142. package/src/client/hooks/chat/timeline/lost-run.test.ts +329 -0
  143. package/src/client/hooks/chat/timeline/lost-run.ts +177 -0
  144. package/src/client/hooks/chat/timeline/session-load.test-util.ts +205 -0
  145. package/src/client/hooks/chat/timeline/session-load.test.ts +61 -162
  146. package/src/client/hooks/chat/timeline/session-load.ts +69 -37
  147. package/src/client/hooks/chat/timeline/session-switch.test.ts +65 -0
  148. package/src/client/hooks/chat/timeline/session-switch.ts +63 -0
  149. package/src/client/hooks/code/deferred-highlight.test.ts +182 -0
  150. package/src/client/hooks/code/deferred-highlight.ts +129 -0
  151. package/src/client/hooks/editor/caret-position.test.ts +58 -0
  152. package/src/client/hooks/editor/caret-position.ts +48 -0
  153. package/src/client/hooks/editor/file-editor-types.ts +200 -0
  154. package/src/client/hooks/editor/find-history.test.ts +132 -0
  155. package/src/client/hooks/editor/find-history.ts +46 -0
  156. package/src/client/hooks/editor/find-selection-range.test.ts +100 -0
  157. package/src/client/hooks/editor/find-selection-range.ts +49 -0
  158. package/src/client/hooks/editor/find-state.ts +14 -1
  159. package/src/client/hooks/editor/font.test.ts +0 -93
  160. package/src/client/hooks/editor/indent-settings.test.ts +104 -0
  161. package/src/client/hooks/editor/indent-settings.ts +88 -0
  162. package/src/client/hooks/editor/quick-open.test.ts +141 -0
  163. package/src/client/hooks/editor/quick-open.ts +127 -0
  164. package/src/client/hooks/editor/save-as.test.ts +156 -0
  165. package/src/client/hooks/editor/save-as.ts +122 -0
  166. package/src/client/hooks/editor/save-file.test.ts +179 -0
  167. package/src/client/hooks/editor/save-file.ts +73 -28
  168. package/src/client/hooks/editor/theme.test.ts +206 -0
  169. package/src/client/hooks/editor/theme.ts +82 -0
  170. package/src/client/hooks/editor/typing-settings.test.ts +73 -0
  171. package/src/client/hooks/editor/typing-settings.ts +65 -0
  172. package/src/client/hooks/editor/use-close-guard.ts +98 -0
  173. package/src/client/hooks/editor/use-editor-drop-overlay.test.ts +159 -0
  174. package/src/client/hooks/editor/use-editor-drop.shell.test.ts +224 -0
  175. package/src/client/hooks/editor/use-editor-drop.test.ts +101 -0
  176. package/src/client/hooks/editor/use-editor-drop.ts +120 -77
  177. package/src/client/hooks/editor/use-editor-find.test.ts +2 -2
  178. package/src/client/hooks/editor/use-editor-find.ts +45 -10
  179. package/src/client/hooks/editor/use-editor-panel-keys.ts +152 -0
  180. package/src/client/hooks/editor/use-editor-tab-state.ts +71 -0
  181. package/src/client/hooks/editor/use-external-change-notice.ts +71 -0
  182. package/src/client/hooks/editor/use-external-change.test.ts +88 -0
  183. package/src/client/hooks/editor/use-external-change.ts +146 -0
  184. package/src/client/hooks/editor/use-file-editor.ts +83 -116
  185. package/src/client/hooks/editor/use-file-read.ts +16 -8
  186. package/src/client/hooks/editor/use-file-writes.ts +28 -3
  187. package/src/client/hooks/editor/use-mobile-editor-find.ts +69 -0
  188. package/src/client/hooks/editor/use-mobile-reveal.ts +53 -0
  189. package/src/client/hooks/editor/use-outline-view.test.ts +182 -0
  190. package/src/client/hooks/editor/use-outline-view.ts +94 -7
  191. package/src/client/hooks/editor/use-scroll-restore.test.ts +55 -2
  192. package/src/client/hooks/editor/use-scroll-restore.ts +124 -4
  193. package/src/client/hooks/editor/use-search-reveal-jump.ts +57 -0
  194. package/src/client/hooks/editor/use-search-reveal.ts +1 -1
  195. package/src/client/hooks/editor/use-split-editor.test.tsx +169 -0
  196. package/src/client/hooks/editor/use-split-editor.ts +91 -0
  197. package/src/client/hooks/editor/use-unload-guard.tsx +117 -0
  198. package/src/client/hooks/editor/use-write-conflict.test.ts +164 -0
  199. package/src/client/hooks/editor/use-write-conflict.ts +152 -0
  200. package/src/client/hooks/editor/wheel-zoom.test.ts +141 -0
  201. package/src/client/hooks/editor/wheel-zoom.ts +96 -0
  202. package/src/client/hooks/ui/drag-presence.test.ts +193 -0
  203. package/src/client/hooks/ui/drag-presence.ts +108 -0
  204. package/src/client/hooks/ui/settings-modal.test.ts +115 -0
  205. package/src/client/hooks/ui/settings-modal.ts +56 -0
  206. package/src/client/hooks/ui/updates/shell-download.test.ts +2 -0
  207. package/src/client/hooks/ui/updates/shell-download.ts +55 -14
  208. package/src/client/hooks/ui/updates/shell-offer.test.ts +26 -0
  209. package/src/client/hooks/ui/updates/shell-update.test.ts +140 -0
  210. package/src/client/hooks/ui/updates/shell-update.ts +119 -0
  211. package/src/client/hooks/workspace/diff-file.ts +29 -10
  212. package/src/client/hooks/workspace/diff-git-actions.ts +147 -0
  213. package/src/client/hooks/workspace/editor-view.ts +61 -0
  214. package/src/client/hooks/workspace/file-listing.ts +36 -24
  215. package/src/client/hooks/workspace/file-tabs.test.ts +152 -5
  216. package/src/client/hooks/workspace/file-tabs.ts +70 -11
  217. package/src/client/hooks/workspace/file-tree-edits.ts +103 -0
  218. package/src/client/hooks/workspace/navbar-session-actions.ts +72 -0
  219. package/src/client/hooks/workspace/worktree/index.ts +20 -5
  220. package/src/client/lib/desktop/bridge.ts +45 -0
  221. package/src/client/lib/desktop/dropped-paths.ts +83 -0
  222. package/src/client/lib/editor/tab-menu.test.ts +127 -0
  223. package/src/client/lib/editor/tab-menu.ts +127 -0
  224. package/src/client/lib/editor-dirty-tabs.test.ts +102 -0
  225. package/src/client/lib/editor-dirty-tabs.ts +103 -0
  226. package/src/client/lib/native/resume.test.ts +66 -0
  227. package/src/client/lib/native/resume.ts +42 -0
  228. package/src/client/lib/search-reveal.ts +1 -1
  229. package/src/client/main.tsx +13 -1
  230. package/src/client/tailwind.css +75 -8
  231. package/src/server/lib/auth/guard.test.ts +28 -0
  232. package/src/server/lib/auth/guard.ts +10 -0
  233. package/src/server/lib/bundler/css.test.ts +31 -0
  234. package/src/server/lib/fs/listing.test.ts +81 -16
  235. package/src/server/lib/fs/listing.ts +72 -5
  236. package/src/server/lib/fs/worktree/create.ts +2 -3
  237. package/src/server/lib/fs/worktree/list.ts +13 -0
  238. package/src/server/lib/fs/worktree/remove.ts +3 -3
  239. package/src/server/lib/fs/write-conflict.test.ts +68 -0
  240. package/src/server/lib/fs/write-conflict.ts +84 -0
  241. package/src/server/lib/lifecycle/claim-port.test.ts +156 -0
  242. package/src/server/lib/lifecycle/port-guard.test.ts +20 -164
  243. package/src/server/lib/lifecycle/port-guard.ts +39 -7
  244. package/src/server/lib/lifecycle/probe-health.test.ts +166 -0
  245. package/src/server/lib/lifecycle/probe.test.ts +77 -164
  246. package/src/server/lib/lifecycle/probe.ts +121 -13
  247. package/src/server/lib/omp/rpc/frame-fold-signals.test.ts +112 -0
  248. package/src/server/lib/omp/rpc/frame-fold.ts +10 -5
  249. package/src/server/lib/omp/session/sidebar-activity.server.test.ts +162 -0
  250. package/src/server/lib/omp/session/sidebar-activity.server.ts +70 -0
  251. package/src/server/lib/omp/session/telemetry/compute.ts +13 -1
  252. package/src/server/lib/omp/session/telemetry.test.ts +33 -0
  253. package/src/server/lib/realtime/signals.server.ts +10 -0
  254. package/src/server/lib/realtime/topics/catalog.server.ts +4 -1
  255. package/src/server/lib/realtime/topics.server.ts +16 -0
  256. package/src/server/lib/updates/shell-assets.ts +94 -3
  257. package/src/server/lib/updates/shell-cache.ts +105 -0
  258. package/src/server/lib/updates/shell-pages.test.ts +33 -2
  259. package/src/server/lib/updates/shell-pages.ts +17 -5
  260. package/src/server/lib/updates/shell-release-desktop.test.ts +166 -0
  261. package/src/server/lib/updates/shell-release.test.ts +10 -27
  262. package/src/server/lib/updates/shell-release.ts +107 -111
  263. package/src/server/routes/fs/action-save-as.test.ts +82 -0
  264. package/src/server/routes/fs/action.test.ts +82 -0
  265. package/src/server/routes/fs/action.ts +54 -1
  266. package/src/server/routes/fs/exists.ts +11 -1
  267. package/src/server/routes/fs/git-merge.test.ts +11 -1
  268. package/src/server/routes/fs/read.test.ts +24 -7
  269. package/src/server/routes/fs/read.ts +6 -1
  270. package/src/server/routes/fs/scope.test.ts +15 -10
  271. package/src/server/routes/fs/scope.ts +3 -14
  272. package/src/server/routes/updates/android.ts +6 -1
  273. package/src/server/routes/updates/desktop.ts +29 -7
  274. package/src/server/routes/updates/ios.ts +5 -1
  275. package/src/server/routes/worktrees/list.ts +11 -1
  276. package/src/server/routes/worktrees/routes.test.ts +17 -0
  277. package/src/shared/lib/chat/timeline/omp-callbacks.test.ts +36 -0
  278. package/src/shared/lib/chat/timeline/omp-callbacks.ts +18 -0
  279. package/src/shared/lib/chat/timeline/run-signals.test.ts +2 -0
  280. package/src/shared/lib/code/editor/comment-syntax.test.ts +37 -0
  281. package/src/shared/lib/code/editor/comment-syntax.ts +55 -0
  282. package/src/shared/lib/code/editor/find-replace.test.ts +134 -0
  283. package/src/shared/lib/code/editor/{find.ts → find-replace.ts} +23 -107
  284. package/src/shared/lib/code/editor/{find.test.ts → find-scan.test.ts} +20 -74
  285. package/src/shared/lib/code/editor/find-scan.ts +198 -0
  286. package/src/shared/lib/code/editor/find-selection.test.ts +56 -0
  287. package/src/shared/lib/code/editor/indent-guide-rule.test.ts +65 -0
  288. package/src/shared/lib/code/editor/indent-guide-rule.ts +47 -0
  289. package/src/shared/lib/code/editor/indent-rules.test.ts +219 -0
  290. package/src/shared/lib/code/editor/indent-rules.ts +198 -0
  291. package/src/shared/lib/code/editor/keymap.test.ts +23 -0
  292. package/src/shared/lib/code/editor/keymap.ts +43 -6
  293. package/src/shared/lib/code/editor/lines.test.ts +7 -55
  294. package/src/shared/lib/code/editor/lines.ts +8 -48
  295. package/src/shared/lib/code/editor/marks.test.ts +4 -5
  296. package/src/shared/lib/code/editor/marks.ts +13 -25
  297. package/src/shared/lib/code/editor/palette.test.ts +25 -2
  298. package/src/shared/lib/code/editor/palette.ts +10 -0
  299. package/src/shared/lib/code/editor/stream-modes.ts +225 -0
  300. package/src/shared/lib/code/editor/theme-loader.ts +97 -0
  301. package/src/shared/lib/code/editor/themes.generated.ts +156 -0
  302. package/src/shared/lib/code/editor/themes.test.ts +165 -0
  303. package/src/shared/lib/code/editor/themes.ts +165 -0
  304. package/src/shared/lib/code/highlight-memo.test.ts +103 -0
  305. package/src/shared/lib/code/highlight-memo.ts +149 -0
  306. package/src/shared/lib/code/highlighter-warmup.ts +112 -0
  307. package/src/shared/lib/code/highlighter.ts +3 -1
  308. package/src/shared/lib/code/idle.ts +37 -0
  309. package/src/shared/lib/code/outline/rows.test.ts +96 -0
  310. package/src/shared/lib/code/outline/rows.ts +94 -0
  311. package/src/shared/lib/code/syntax-highlight.test.ts +2 -80
  312. package/src/shared/lib/code/syntax-highlight.ts +2 -3
  313. package/src/shared/lib/fs/file-kind-classify.test.ts +25 -11
  314. package/src/shared/lib/fs/file-kind.ts +47 -13
  315. package/src/shared/lib/fs/file-tree.test.ts +41 -0
  316. package/src/shared/lib/fs/file-tree.ts +126 -34
  317. package/src/shared/lib/fs/file-url.test.ts +52 -0
  318. package/src/shared/lib/fs/file-url.ts +37 -0
  319. package/src/shared/lib/fs/folder-chain.test.ts +87 -0
  320. package/src/shared/lib/fs/folder-chain.ts +91 -0
  321. package/src/shared/lib/fs/fs-helpers.test.ts +4 -50
  322. package/src/shared/lib/fs/patch-sides.test.ts +114 -0
  323. package/src/shared/lib/fs/patch-sides.ts +139 -0
  324. package/src/shared/lib/realtime/connection.test.ts +5 -1
  325. package/src/shared/lib/realtime/connection.ts +25 -119
  326. package/src/shared/lib/realtime/frozen-recovery.test.ts +93 -0
  327. package/src/shared/lib/realtime/protocol.ts +16 -0
  328. package/src/shared/lib/realtime/resume.ts +223 -0
  329. package/src/shared/lib/realtime/state.ts +22 -0
  330. package/src/shared/lib/realtime/watchdog-wiring.test.ts +224 -0
  331. package/src/shared/lib/realtime/watchdog.test.ts +99 -0
  332. package/src/shared/lib/realtime/watchdog.ts +72 -0
  333. package/src/shared/lib/ui/toolbar-overflow.test.ts +83 -0
  334. package/src/shared/lib/ui/toolbar-overflow.ts +86 -0
  335. package/src/shared/lib/updates/shell-arch.ts +54 -0
  336. package/src/shared/lib/updates/shell-ua.ts +4 -4
  337. package/src/shared/lib/workspace/editor-view-mode.test.ts +70 -0
  338. package/src/shared/lib/workspace/editor-view-mode.ts +48 -0
  339. package/src/shared/lib/workspace/panel-widths.test.ts +30 -0
  340. package/src/shared/lib/workspace/panel-widths.ts +25 -5
  341. package/src/shared/lib/workspace/session-state/cache.ts +159 -0
  342. package/src/shared/lib/workspace/session-state/harness.test-util.ts +55 -0
  343. package/src/shared/lib/workspace/session-state/merge.ts +100 -0
  344. package/src/shared/lib/workspace/session-state/store-load.test.ts +206 -0
  345. package/src/shared/lib/workspace/session-state/store.test.ts +17 -139
  346. package/src/shared/lib/workspace/session-state/store.ts +92 -130
  347. package/src/shared/lib/workspace/split-editor.test.ts +104 -0
  348. package/src/shared/lib/workspace/split-editor.ts +129 -0
  349. package/src/shared/lib/workspace/workspace-panel-widths.ts +124 -0
  350. package/src/shared/types/realtime.ts +29 -0
  351. package/src/shared/types/settings/state.ts +10 -0
  352. package/src/test-support/file-explorer-disk.test-util.ts +30 -3
  353. package/src/test-support/loopback.ts +183 -0
  354. package/src/test-support/pristine-globals.ts +7 -0
  355. package/dist/client/static/js/index-gtbpewr7.css +0 -1
  356. package/src/client/components/common/code-editor/handler.test.ts +0 -274
  357. package/src/client/components/common/code-editor/handler.ts +0 -175
  358. package/src/client/components/common/code-editor/index.tsx +0 -314
  359. package/src/client/components/common/code-surface/measure.ts +0 -148
  360. package/src/client/components/workspace/chat-timeline/target-picker/index.test.tsx +0 -227
  361. package/src/client/components/workspace/chat-timeline/target-picker/index.tsx +0 -260
  362. package/src/client/components/workspace/diff-panel/SplitView.tsx +0 -209
  363. package/src/client/components/workspace/diff-panel/UnifiedView.tsx +0 -150
  364. package/src/client/hooks/editor/scroll-container.test.ts +0 -102
  365. package/src/client/hooks/editor/scroll-container.ts +0 -26
  366. package/src/client/hooks/editor/use-code-window.ts +0 -100
  367. package/src/client/hooks/editor/use-editor-history.test.ts +0 -216
  368. package/src/client/hooks/editor/use-editor-history.ts +0 -134
  369. package/src/client/hooks/editor/use-occurrences.test.ts +0 -236
  370. package/src/client/hooks/editor/use-occurrences.ts +0 -98
  371. package/src/shared/lib/code/editor/commands.test.ts +0 -155
  372. package/src/shared/lib/code/editor/commands.ts +0 -260
  373. package/src/shared/lib/code/editor/edits.test.ts +0 -100
  374. package/src/shared/lib/code/editor/edits.ts +0 -122
  375. package/src/shared/lib/code/editor/history.test.ts +0 -118
  376. package/src/shared/lib/code/editor/history.ts +0 -101
  377. package/src/shared/lib/code/editor/layers.test.ts +0 -77
  378. package/src/shared/lib/code/editor/layers.ts +0 -67
  379. package/src/shared/lib/code/editor/line-edits.test.ts +0 -131
  380. package/src/shared/lib/code/editor/line-edits.ts +0 -159
  381. package/src/shared/lib/code/editor/occurrences.test.ts +0 -117
  382. package/src/shared/lib/code/editor/occurrences.ts +0 -111
  383. package/src/shared/lib/code/editor/reset-css.test.ts +0 -35
  384. package/src/shared/lib/code/editor/reset-css.ts +0 -32
  385. package/src/shared/lib/code/editor/text-commands.test.ts +0 -178
  386. package/src/shared/lib/code/editor/text-commands.ts +0 -154
  387. package/src/shared/lib/code/lazy-window.test.ts +0 -161
  388. package/src/shared/lib/code/lazy-window.ts +0 -156
  389. package/src/shared/lib/code/windowed-highlight.ts +0 -114
  390. package/src/shared/lib/fs/diff-parser.test.ts +0 -116
  391. package/src/shared/lib/fs/diff-parser.ts +0 -213
@@ -0,0 +1,93 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ /**
7
+ * The reported Android failure, end to end against a REAL server.
8
+ *
9
+ * A backgrounded phone freezes the page: the server's 30s protocol heartbeat
10
+ * reaps the socket because a frozen tab cannot answer a ping, and the browser
11
+ * never learns — `readyState` still says OPEN and no `close` is delivered. The
12
+ * channel used to sit on that corpse until the user reloaded the page.
13
+ *
14
+ * What this suite adds over the unit ones: the client is driven through its
15
+ * PUBLIC surface against a real `WebSocket` and the real route, so the wiring is
16
+ * production's; and the repair is triggered by nothing but time — no
17
+ * `visibilitychange`, no `pageshow`, no `online`, no native shell hook.
18
+ * `watchdog.test.ts` pins the decision table and `connection.test.ts` the
19
+ * interval that drives it; this pins that the decision reaches a live socket,
20
+ * abandons it, re-dials and RESUBSCRIBES.
21
+ *
22
+ * The real-time dependency is deliberate and named: the watchdog ticks on the
23
+ * platform clock, and faking that clock would fake the thing under test. Nothing
24
+ * here waits for a duration — the assertion is a topic value that can only
25
+ * arrive through the new socket's subscription.
26
+ */
27
+
28
+ import { afterEach, describe, expect, test } from 'bun:test';
29
+ import { Window } from 'happy-dom';
30
+
31
+ import { realtimeClient, resetRealtimeClient } from '@/shared/lib/realtime/client';
32
+ import { state } from '@/shared/lib/realtime/state';
33
+ import { installDomGlobals, restoreDomGlobals } from '@/test-support/pristine-globals';
34
+ import { startRealtimeTestServer, type RealtimeTestServer } from '@/test-support/realtime-server';
35
+
36
+ let server: RealtimeTestServer | undefined;
37
+
38
+ afterEach(() => {
39
+ server?.stop();
40
+ server = undefined;
41
+ // The channel is PROCESS state, and this file is the one that dials a REAL
42
+ // socket. `restoreDomGlobals` only puts the runner's globals back; the live
43
+ // socket, its retry timer and the wanted flag survive into the next file,
44
+ // where `dial()` then early-returns on the socket it still holds and the next
45
+ // suite's constructor override never produces an instance. Measured on
46
+ // `Gates / Checks (Windows)`: `watchdog-wiring.test.ts` failed with
47
+ // `undefined is not an object (evaluating 'socket.open')` while passing when
48
+ // run alone.
49
+ resetRealtimeClient();
50
+ restoreDomGlobals();
51
+ });
52
+
53
+ describe('a frozen page with a socket the server already reaped', () => {
54
+ test('the watchdog re-dials and resubscribes with no event and no reload', async () => {
55
+ let value = 1;
56
+ server = await startRealtimeTestServer(new Map([['probe:topic', async () => value]]));
57
+ // The dial path builds its URL from `window.location` and uses its timers, so
58
+ // the window has to exist — and it has to name the listener's own port, or
59
+ // the socket dials a port nothing serves. `installDomGlobals` also swaps in
60
+ // the runner's own `WebSocket`, which is what makes the socket REAL.
61
+ installDomGlobals(new Window({ url: `http://127.0.0.1:${server.port}` }));
62
+
63
+ const unsubscribe = realtimeClient.subscribe('probe:topic', () => {});
64
+ try {
65
+ await server.waitForTopic<number>('probe:topic', (current) => current !== null, 5_000);
66
+ expect(realtimeClient.status()).toBe('open');
67
+ const reaped = state().socket!;
68
+
69
+ // The freeze, written as the state a frozen page leaves behind rather than
70
+ // by advancing a clock: an open-looking socket, silence older than the idle
71
+ // window, and a connection the server no longer answers on. `send` throwing
72
+ // is what a reaped socket does — the probe cannot be delivered, which is how
73
+ // the client discovers it is dead.
74
+ state().lastInboundAt = Date.now() - 60_000;
75
+ reaped.send = () => {
76
+ throw new Error('socket was reaped while the page was frozen');
77
+ };
78
+
79
+ // The NEW value can only reach this client through the replacement socket's
80
+ // subscription: the server answers every subscribe with a fresh snapshot,
81
+ // and nothing is buffered for a dead one. So this one assertion covers the
82
+ // whole chain — the probe failing, the socket being abandoned, the re-dial,
83
+ // and the live topic set being resubscribed.
84
+ value = 42;
85
+ const recovered = await server.waitForTopic<number>('probe:topic', (current) => current === 42, 25_000);
86
+ expect(recovered).toBe(42);
87
+ expect(realtimeClient.status()).toBe('open');
88
+ expect(state().socket).not.toBe(reaped);
89
+ } finally {
90
+ unsubscribe();
91
+ }
92
+ }, 40_000);
93
+ });
@@ -45,6 +45,22 @@ export const TOPIC_SIDEBAR = 'sidebar';
45
45
  * the structure needs a JSONL scan to produce. */
46
46
  export const TOPIC_SIDEBAR_STATUS = 'sidebar:status';
47
47
 
48
+ /**
49
+ * The last-activity stamp of each session with a run IN FLIGHT, keyed by id.
50
+ *
51
+ * Split from `sidebar` for the same reason `sidebar:status` is, but the cost
52
+ * difference is far larger: a transcript grows on every tool call, so this
53
+ * changes per tool call while the structure is a 216 KB payload over 731
54
+ * sessions (measured on the author's install) that must be re-scanned to
55
+ * produce. Only a RUNNING session's stamp can move between turn boundaries, so
56
+ * only those are carried — the payload is one ISO string per live run
57
+ * (measured: 87 bytes for one), against 216 KB for the whole list.
58
+ *
59
+ * The structure remains the authority for every other row; this only ever
60
+ * ADVANCES a stamp the structure already carries (see `applySidebarActivity`).
61
+ */
62
+ export const TOPIC_SIDEBAR_ACTIVITY = 'sidebar:activity';
63
+
48
64
  /** One session's live conversation: agent frames, queue and modes. */
49
65
  export function sessionTopic(sessionId: string): string {
50
66
  return `session:${sessionId}`;
@@ -0,0 +1,223 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ /**
7
+ * Resume: the half of the channel's lifecycle that repairs a connection the
8
+ * browser never told us was gone.
9
+ *
10
+ * Split from `connection.ts` (which owns dial, backoff and teardown) because a
11
+ * phone broke the event-based half, and the two are now genuinely different
12
+ * mechanisms. `connection.ts` reacts to events the PLATFORM delivers; this
13
+ * module reacts to ones it does not.
14
+ *
15
+ * ## Why events alone are not enough
16
+ *
17
+ * A backgrounded Android tab is frozen: timers stop, and the server's 30s
18
+ * protocol heartbeat tears the socket down because a frozen tab cannot answer a
19
+ * ping. The repair used to depend on `visibilitychange`, `pageshow` or `online`.
20
+ * Measured on the shipped Android shell, none of them reliably arrives: the
21
+ * WebView is never reported hidden when the app goes to the background, so
22
+ * returning to it fires no `visibilitychange`, and a socket the server reaped
23
+ * while the page was frozen produces no `close` — the browser still reports
24
+ * `readyState === OPEN`. The channel therefore sat on a corpse: status `open`,
25
+ * no frames, the stream frozen mid-answer, and only a page reload repaired it.
26
+ *
27
+ * ## The watchdog
28
+ *
29
+ * A running interval is the one signal a frozen page cannot fake, because a
30
+ * frozen page stops running it:
31
+ *
32
+ * - a tick that arrives much later than its deadline means the page was
33
+ * suspended, which is exactly the moment a resume has to be treated like one;
34
+ * - an OPEN socket with no inbound traffic for `IDLE_PROBE_AFTER_MS` is probed
35
+ * even when nothing woke the page, which catches a connection that died while
36
+ * the tab stayed visible (a Wi-Fi to cellular handoff).
37
+ *
38
+ * Both branches are idempotent: a probe already in flight is left alone, and
39
+ * `wake()` is a no-op on a healthy connection.
40
+ */
41
+
42
+ import {
43
+ WS_CONNECTING,
44
+ WS_OPEN,
45
+ notifyClosed,
46
+ send,
47
+ state,
48
+ } from '@/shared/lib/realtime/state';
49
+ import {
50
+ LIVENESS_TICK_MS,
51
+ watchdogAction,
52
+ } from '@/shared/lib/realtime/watchdog';
53
+
54
+ /**
55
+ * How long a socket that LOOKS open has to answer a resume probe. A frozen tab
56
+ * keeps `readyState === OPEN` for a connection the server already tore down, so
57
+ * the only trustworthy answer is the server's own pong.
58
+ */
59
+ const WAKE_PROBE_MS = 2_000;
60
+
61
+ /** Ask for a fresh socket. Injected by `connection.ts` so this module never
62
+ * imports the dial path back and the two stay acyclic. */
63
+ export type Dialer = () => void;
64
+
65
+ export interface ResumeWatch {
66
+ /** Install the tab-resume listeners once (a no-op outside a document). */
67
+ ensureListeners: () => void;
68
+ /** Start the watchdog. Idempotent. */
69
+ start: () => void;
70
+ /** Stop the watchdog and remove the listeners. */
71
+ stop: () => void;
72
+ /** Treat the page as back: reset the budget, probe or re-dial. */
73
+ wake: () => void;
74
+ }
75
+
76
+ /**
77
+ * Ask a socket that LOOKS open whether it still is.
78
+ *
79
+ * A frozen tab keeps `readyState === OPEN` for a connection the server's
80
+ * heartbeat already tore down, and no `close` is delivered until the browser
81
+ * notices. Without this probe the resume path would trust a corpse and the
82
+ * indicator would stay green over a stream that never resumes.
83
+ */
84
+ function probeLiveness(socket: WebSocket): void {
85
+ const host = state();
86
+ // A probe whose deadline passed while the page was frozen can never fire its
87
+ // own abandon: the timer was frozen with it. Discard it rather than letting it
88
+ // block every later probe — that is a socket that is never questioned and so
89
+ // never replaced.
90
+ if (host.probeId !== undefined && Date.now() - host.probeArmedAt > WAKE_PROBE_MS) {
91
+ host.probeId = undefined;
92
+ clearTimeout(host.probeTimer);
93
+ host.probeTimer = undefined;
94
+ }
95
+ if (host.probeId !== undefined) return;
96
+ const id = (host.probeSeq += 1);
97
+ host.probeId = id;
98
+ host.probeArmedAt = Date.now();
99
+ const abandon = (): void => {
100
+ // A late answer from a socket that has since been replaced must not close
101
+ // the live one: the deadline is only ever about THIS socket.
102
+ if (host.probeId !== id) return;
103
+ host.probeId = undefined;
104
+ host.probeTimer = undefined;
105
+ if (host.socket !== socket) return;
106
+ try {
107
+ socket.close();
108
+ } catch {
109
+ // Already gone.
110
+ }
111
+ };
112
+ host.probeTimer = window.setTimeout(abandon, WAKE_PROBE_MS);
113
+ if (!send({ t: 'ping', id })) abandon();
114
+ }
115
+
116
+ export function createResumeWatch(dial: Dialer): ResumeWatch {
117
+ /**
118
+ * The tab is back: a background freeze stopped our timers and the server's
119
+ * heartbeat dropped the socket, so the failure count describes a window in
120
+ * which nothing could run. Reset it and dial now if the socket is gone.
121
+ */
122
+ const wake = (): void => {
123
+ const host = state();
124
+ if (!host.wanted) return;
125
+ host.failures = 0;
126
+ const socket = host.socket;
127
+ if (socket && socket.readyState === WS_CONNECTING) return;
128
+ if (socket && socket.readyState === WS_OPEN) {
129
+ probeLiveness(socket);
130
+ return;
131
+ }
132
+ if (host.retryTimer !== undefined) {
133
+ clearTimeout(host.retryTimer);
134
+ host.retryTimer = undefined;
135
+ }
136
+ if (socket) {
137
+ // Nulled FIRST: `close()` delivers `onclose` synchronously in some
138
+ // engines, and that handler owns the retry decision for a socket it still
139
+ // believes in. This socket is being abandoned on purpose, so its late
140
+ // close must report nothing.
141
+ host.socket = null;
142
+ try {
143
+ socket.close();
144
+ } catch {
145
+ // Already gone.
146
+ }
147
+ // The server tore this connection down while the tab was frozen, so every
148
+ // value on screen is of unknown age even though no `close` arrived.
149
+ notifyClosed();
150
+ }
151
+ dial();
152
+ };
153
+
154
+ /**
155
+ * Install the resume listeners once. A no-op outside a document.
156
+ *
157
+ * The cleanup closes over the `document`/`window` it REGISTERED on rather than
158
+ * reading the globals again when it runs. Those are not necessarily the same
159
+ * objects: a test suite replaces them per case and deletes them when it is
160
+ * done, and the teardown would then dereference an absent `document` and throw
161
+ * — measured on CI, where a suite that reset the client after its own
162
+ * `restoreDomGlobals` failed the NEXT file with `ReferenceError: document is
163
+ * not defined`. In a browser the two are always the same object.
164
+ */
165
+ const ensureListeners = (): void => {
166
+ const host = state();
167
+ if (host.wakeCleanup) return;
168
+ if (typeof window === 'undefined' || typeof document === 'undefined') return;
169
+ const doc = document;
170
+ const win = window;
171
+ const onVisible = (): void => {
172
+ if (!doc.hidden) wake();
173
+ };
174
+ const onOnline = (): void => wake();
175
+ const onPageShow = (): void => wake();
176
+ doc.addEventListener('visibilitychange', onVisible);
177
+ win.addEventListener('online', onOnline);
178
+ win.addEventListener('pageshow', onPageShow);
179
+ host.wakeCleanup = () => {
180
+ doc.removeEventListener('visibilitychange', onVisible);
181
+ win.removeEventListener('online', onOnline);
182
+ win.removeEventListener('pageshow', onPageShow);
183
+ };
184
+ };
185
+
186
+ const start = (): void => {
187
+ const host = state();
188
+ if (host.livenessTimer !== undefined) return;
189
+ if (typeof window === 'undefined') return;
190
+ let lastTick = Date.now();
191
+ host.livenessTimer = window.setInterval(() => {
192
+ const now = Date.now();
193
+ const sinceTick = now - lastTick;
194
+ lastTick = now;
195
+ const socket = host.socket;
196
+ const action = watchdogAction({
197
+ sinceTick,
198
+ sinceInbound: now - host.lastInboundAt,
199
+ hidden: document.hidden,
200
+ open: socket?.readyState === WS_OPEN,
201
+ wanted: host.wanted,
202
+ });
203
+ if (action === 'wake') wake();
204
+ else if (action === 'probe' && socket) probeLiveness(socket);
205
+ }, LIVENESS_TICK_MS);
206
+ };
207
+
208
+ const stop = (): void => {
209
+ const host = state();
210
+ // Nulled BEFORE the call: a cleanup that throws (a listener target that is
211
+ // already gone) must not leave the slot set, or the next stop retries the
212
+ // same throw.
213
+ const cleanup = host.wakeCleanup;
214
+ host.wakeCleanup = null;
215
+ cleanup?.();
216
+ if (host.livenessTimer !== undefined) {
217
+ clearInterval(host.livenessTimer);
218
+ host.livenessTimer = undefined;
219
+ }
220
+ };
221
+
222
+ return { ensureListeners, start, stop, wake };
223
+ }
@@ -69,6 +69,25 @@ export interface ConnectionState {
69
69
  probeId: number | undefined;
70
70
  probeTimer: number | undefined;
71
71
  probeSeq: number;
72
+ /**
73
+ * When the armed probe was sent.
74
+ *
75
+ * A probe armed before the page was frozen keeps its deadline timer frozen
76
+ * with it, so `probeId` stays set forever and every later probe would return
77
+ * early — the socket is then never abandoned and nothing ever re-dials. The
78
+ * timestamp is what lets a probe that outlived its own deadline be discarded.
79
+ */
80
+ probeArmedAt: number;
81
+ /**
82
+ * When the last frame (or probe answer) arrived on the current socket.
83
+ *
84
+ * The watchdog compares this against `IDLE_PROBE_AFTER_MS` to decide whether
85
+ * an OPEN socket has gone quiet — which is the ONLY signal available when the
86
+ * browser never delivers a `close` (see `connection.ts`).
87
+ */
88
+ lastInboundAt: number;
89
+ /** The watchdog interval. `undefined` until the first subscription. */
90
+ livenessTimer: number | undefined;
72
91
  }
73
92
 
74
93
  /**
@@ -101,6 +120,9 @@ export function state(): ConnectionState {
101
120
  probeId: undefined,
102
121
  probeTimer: undefined,
103
122
  probeSeq: 0,
123
+ probeArmedAt: 0,
124
+ lastInboundAt: 0,
125
+ livenessTimer: undefined,
104
126
  });
105
127
  }
106
128
 
@@ -0,0 +1,224 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ /**
7
+ * The watchdog's WIRING, driven through the interval the client really runs.
8
+ *
9
+ * The DECISION (freeze versus silence versus throttling) is pinned in
10
+ * `watchdog.test.ts`, where a timing rule can be asserted without a clock. What
11
+ * is left here is that the interval exists, that it reaches the client's own
12
+ * probe and re-dial paths, and that its clocks come from the socket's real
13
+ * traffic — the parts a pure function cannot cover.
14
+ *
15
+ * The reported failure these exist for: a backgrounded Android WebView fires no
16
+ * `visibilitychange` on return, and a socket the server's 30s heartbeat reaped
17
+ * while the page was frozen produces no `close` — `readyState` still says OPEN.
18
+ * The channel sat on a corpse with no frames, the stream frozen mid-answer, and
19
+ * only a page reload repaired it.
20
+ *
21
+ * Driven through the PUBLIC client (`subscribe`), so the wiring under test is
22
+ * the one production uses; the socket is a double only because these cases are
23
+ * about what the client does with a socket that stops answering.
24
+ */
25
+
26
+ import { afterEach, beforeEach, describe, expect, test, vi } from 'bun:test';
27
+ import { Window } from 'happy-dom';
28
+
29
+ import { realtimeClient, resetRealtimeClient } from '@/shared/lib/realtime/client';
30
+ import { currentStatus, state } from '@/shared/lib/realtime/state';
31
+ import { IDLE_PROBE_AFTER_MS } from '@/shared/lib/realtime/watchdog';
32
+ import { installDomGlobals, restoreDomGlobals } from '@/test-support/pristine-globals';
33
+
34
+ /** A socket whose answer the case decides, and whose frames are inspectable. */
35
+ class FakeSocket {
36
+ static instances: FakeSocket[] = [];
37
+ readyState = 0;
38
+ sent: string[] = [];
39
+ onopen: (() => void) | null = null;
40
+ onclose: (() => void) | null = null;
41
+ onmessage: ((event: { data: string }) => void) | null = null;
42
+ onerror: (() => void) | null = null;
43
+
44
+ constructor(public url: string) {
45
+ FakeSocket.instances.push(this);
46
+ }
47
+
48
+ open(): void {
49
+ this.readyState = 1;
50
+ this.onopen?.();
51
+ }
52
+
53
+ /** The server reaped it while the page was frozen: the browser never learned. */
54
+ silentDeath(): void {
55
+ this.readyState = 1;
56
+ }
57
+
58
+ send(data: string): void {
59
+ this.sent.push(data);
60
+ }
61
+
62
+ close(): void {
63
+ if (this.readyState === 3) return;
64
+ this.readyState = 3;
65
+ this.onclose?.();
66
+ }
67
+
68
+ frames(): Array<Record<string, unknown>> {
69
+ return this.sent.map((raw) => JSON.parse(raw) as Record<string, unknown>);
70
+ }
71
+ }
72
+
73
+ const WAKE_PROBE_MS = 2_000;
74
+ const RETRY_AFTER_ESTABLISHED_DROP_MS = 500;
75
+
76
+ let win: Window;
77
+ let unsubscribe: (() => void) | null = null;
78
+
79
+ beforeEach(() => {
80
+ FakeSocket.instances = [];
81
+ // Start from NO connection. A suite that dialed a real socket and did not
82
+ // release it leaves `state().socket` set, and `dial()` then early-returns on
83
+ // it — no FakeSocket is ever constructed, so every case here fails on a
84
+ // missing instance rather than on the watchdog. File order is the runner's,
85
+ // so this file must not depend on another suite's teardown.
86
+ resetRealtimeClient();
87
+ win = new Window({ url: 'http://localhost' });
88
+ // `vi.useFakeTimers` patches the RUNNER's globals, while happy-dom captured
89
+ // its own bound copies when it was imported. Delegating at call time is what
90
+ // lets the watchdog's `window.setInterval` be driven deterministically.
91
+ win.setTimeout = ((fn: () => void, ms?: number) =>
92
+ globalThis.setTimeout(fn, ms)) as unknown as typeof win.setTimeout;
93
+ win.clearTimeout = ((id?: number) => globalThis.clearTimeout(id)) as unknown as typeof win.clearTimeout;
94
+ win.setInterval = ((fn: () => void, ms?: number) =>
95
+ globalThis.setInterval(fn, ms)) as unknown as typeof win.setInterval;
96
+ win.clearInterval = ((id?: number) => globalThis.clearInterval(id)) as unknown as typeof win.clearInterval;
97
+ installDomGlobals(win);
98
+ realtimeClient.setSocketConstructor(FakeSocket as unknown as typeof WebSocket);
99
+ vi.useFakeTimers();
100
+ });
101
+
102
+ afterEach(() => {
103
+ unsubscribe?.();
104
+ unsubscribe = null;
105
+ resetRealtimeClient();
106
+ vi.useRealTimers();
107
+ restoreDomGlobals();
108
+ });
109
+
110
+ function subscribe(): void {
111
+ unsubscribe = realtimeClient.subscribe('probe:topic', () => {});
112
+ }
113
+
114
+ function lastSocket(): FakeSocket {
115
+ return FakeSocket.instances[FakeSocket.instances.length - 1]!;
116
+ }
117
+
118
+ describe('the liveness watchdog', () => {
119
+ test('a silent open socket is probed without any resume event', () => {
120
+ subscribe();
121
+ const socket = lastSocket();
122
+ socket.open();
123
+ // No `visibilitychange`, no `pageshow`, no `online`: only time passing.
124
+ vi.advanceTimersByTime(IDLE_PROBE_AFTER_MS);
125
+
126
+ expect(socket.frames().filter((frame) => frame.t === 'ping')).toHaveLength(1);
127
+ expect(FakeSocket.instances).toHaveLength(1);
128
+ expect(currentStatus()).toBe('open');
129
+ });
130
+
131
+ test('a probe that is never answered replaces the socket', () => {
132
+ subscribe();
133
+ const socket = lastSocket();
134
+ socket.open();
135
+ vi.advanceTimersByTime(IDLE_PROBE_AFTER_MS);
136
+ expect(socket.frames().filter((frame) => frame.t === 'ping')).toHaveLength(1);
137
+
138
+ // No pong: the deadline abandons the socket and the retry path dials.
139
+ vi.advanceTimersByTime(WAKE_PROBE_MS);
140
+ vi.advanceTimersByTime(RETRY_AFTER_ESTABLISHED_DROP_MS);
141
+ expect(FakeSocket.instances).toHaveLength(2);
142
+ expect(lastSocket()).not.toBe(socket);
143
+ });
144
+
145
+ test('a probe that IS answered leaves the connection alone', () => {
146
+ subscribe();
147
+ const socket = lastSocket();
148
+ socket.open();
149
+ vi.advanceTimersByTime(IDLE_PROBE_AFTER_MS);
150
+ const ping = socket.frames().find((frame) => frame.t === 'ping') as { id: number };
151
+ socket.onmessage?.({ data: JSON.stringify({ t: 'pong', id: ping.id }) });
152
+
153
+ // The pong restarted the idle clock, so the next window is quiet again.
154
+ vi.advanceTimersByTime(IDLE_PROBE_AFTER_MS - WAKE_PROBE_MS);
155
+ expect(FakeSocket.instances).toHaveLength(1);
156
+ expect(currentStatus()).toBe('open');
157
+ });
158
+
159
+ test('real traffic keeps the socket out of the idle probe', () => {
160
+ subscribe();
161
+ const socket = lastSocket();
162
+ socket.open();
163
+ // A quiet-but-healthy connection: deltas and the server's own heartbeat keep
164
+ // the clock fresh, so the watchdog must never question it.
165
+ for (let elapsed = 0; elapsed < IDLE_PROBE_AFTER_MS * 2; elapsed += 5_000) {
166
+ socket.onmessage?.({ data: JSON.stringify({ t: 'pong', id: 999 }) });
167
+ vi.advanceTimersByTime(5_000);
168
+ }
169
+ expect(socket.frames().filter((frame) => frame.t === 'ping')).toHaveLength(0);
170
+ expect(FakeSocket.instances).toHaveLength(1);
171
+ });
172
+
173
+ test('the watchdog stops with the last subscription', () => {
174
+ subscribe();
175
+ const socket = lastSocket();
176
+ socket.open();
177
+ unsubscribe?.();
178
+ unsubscribe = null;
179
+
180
+ const before = FakeSocket.instances.length;
181
+ vi.advanceTimersByTime(IDLE_PROBE_AFTER_MS * 2);
182
+ expect(socket.frames().filter((frame) => frame.t === 'ping')).toHaveLength(0);
183
+ expect(FakeSocket.instances).toHaveLength(before);
184
+ });
185
+
186
+ test('a reset stops the watchdog', () => {
187
+ subscribe();
188
+ const socket = lastSocket();
189
+ socket.open();
190
+ resetRealtimeClient();
191
+
192
+ vi.advanceTimersByTime(IDLE_PROBE_AFTER_MS * 2);
193
+ expect(socket.frames().filter((frame) => frame.t === 'ping')).toHaveLength(0);
194
+ });
195
+
196
+ test('a probe armed before a freeze does not block every later probe', () => {
197
+ subscribe();
198
+ const socket = lastSocket();
199
+ socket.open();
200
+
201
+ // A probe is armed, then the page freezes before its own deadline can fire —
202
+ // and a frozen page freezes that deadline timer WITH it, so `probeId` stays
203
+ // set with nothing left to clear it. Both halves are written directly: fake
204
+ // timers fire the deadline on schedule, so advancing the clock cannot
205
+ // produce this state.
206
+ socket.silentDeath();
207
+ const host = state();
208
+ host.probeId = 99;
209
+ host.probeArmedAt = Date.now() - (WAKE_PROBE_MS + IDLE_PROBE_AFTER_MS);
210
+ clearTimeout(host.probeTimer);
211
+ host.probeTimer = undefined;
212
+
213
+ // The next watchdog probe must discard that stale arm rather than return
214
+ // early on it: an armed-but-unanswerable probe that blocks every later one is
215
+ // a socket that is never questioned, so nothing ever re-dials.
216
+ vi.advanceTimersByTime(IDLE_PROBE_AFTER_MS);
217
+ expect(socket.frames().filter((frame) => frame.t === 'ping')).toHaveLength(1);
218
+
219
+ // And this one DOES get its deadline, so the dead socket is replaced.
220
+ vi.advanceTimersByTime(WAKE_PROBE_MS);
221
+ vi.advanceTimersByTime(RETRY_AFTER_ESTABLISHED_DROP_MS);
222
+ expect(lastSocket()).not.toBe(socket);
223
+ });
224
+ });