@kabmo-labs/kabmo 0.23.0 → 0.25.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 (325) hide show
  1. package/dist/client/index.html +1 -1
  2. package/dist/client/index.js +805 -736
  3. package/dist/client/static/js/index-bc9aa4mx.css +1 -0
  4. package/dist/client/static/js/{index-ph0q59rs.js → index-ga8cbpt4.js} +1071 -1171
  5. package/package.json +2 -4
  6. package/src/cli/lib/args.js +24 -1
  7. package/src/cli/lib/args.test.js +16 -0
  8. package/src/cli/lib/commands/session.js +233 -0
  9. package/src/cli/lib/commands/session.test.js +103 -0
  10. package/src/cli/main.js +1 -1
  11. package/src/client/components/common/ComposerPicker.tsx +3 -0
  12. package/src/client/components/common/Modal.tsx +2 -6
  13. package/src/client/components/common/code-surface/shiki.test.ts +82 -0
  14. package/src/client/components/common/code-surface/shiki.ts +28 -7
  15. package/src/client/components/common/session-actions-menu/NewChildSessionDialog.tsx +113 -0
  16. package/src/client/components/common/session-actions-menu/index.tsx +33 -2
  17. package/src/client/components/common/session-row/StatusSlot.tsx +4 -4
  18. package/src/client/components/common/session-row/index.tsx +27 -1
  19. package/src/client/components/layout/desktop-layout/WorkspacePanels.tsx +7 -1
  20. package/src/client/components/layout/desktop-layout/resizer/separator-drag.ts +3 -3
  21. package/src/client/components/layout/session-sidebar/CategoryItem.tsx +25 -12
  22. package/src/client/components/layout/session-sidebar/ChildSessionList.tsx +66 -0
  23. package/src/client/components/layout/session-sidebar/SessionItem.tsx +28 -3
  24. package/src/client/components/layout/session-sidebar/about-modal/HarnessTable.test.tsx +20 -0
  25. package/src/client/components/layout/session-sidebar/about-modal/HarnessTable.tsx +59 -12
  26. package/src/client/components/layout/session-sidebar/about-modal/HarnessTableLog.test.tsx +94 -0
  27. package/src/client/components/layout/session-sidebar/about-modal/UpdateLogDialog.test.tsx +130 -0
  28. package/src/client/components/layout/session-sidebar/about-modal/UpdateLogDialog.tsx +120 -0
  29. package/src/client/components/layout/session-sidebar/about-modal/UpdateLogLine.tsx +47 -0
  30. package/src/client/components/layout/session-sidebar/about-modal/index.test.tsx +122 -0
  31. package/src/client/components/layout/session-sidebar/about-modal/index.tsx +55 -46
  32. package/src/client/components/layout/session-sidebar/modern/WorkspaceSection.tsx +18 -5
  33. package/src/client/components/mobile/mobile-session-sidebar/Item.tsx +21 -4
  34. package/src/client/components/mobile/mobile-session-sidebar/SessionRow.tsx +27 -5
  35. package/src/client/components/settings/categories/harness-settings/EnvProviderForm.tsx +31 -0
  36. package/src/client/components/settings/categories/harness-settings/provider-panel.test.tsx +49 -0
  37. package/src/client/components/workspace/chat-timeline/ChatOverlays.tsx +5 -2
  38. package/src/client/components/workspace/chat-timeline/ComposerDock.tsx +64 -70
  39. package/src/client/components/workspace/chat-timeline/ComposerDockFrame.tsx +35 -0
  40. package/src/client/components/workspace/chat-timeline/EmptyWorkspacePrompt.tsx +135 -118
  41. package/src/client/components/workspace/chat-timeline/MessageItem.tsx +22 -1
  42. package/src/client/components/workspace/chat-timeline/MessageList.test.ts +14 -0
  43. package/src/client/components/workspace/chat-timeline/MessageList.tsx +4 -0
  44. package/src/client/components/workspace/chat-timeline/TimelineBody.tsx +3 -0
  45. package/src/client/components/workspace/chat-timeline/chat-input/index.tsx +4 -3
  46. package/src/client/components/workspace/chat-timeline/composer-dock-frame.test.tsx +155 -0
  47. package/src/client/components/workspace/chat-timeline/index.tsx +5 -2
  48. package/src/client/components/workspace/chat-timeline/pending-harness.test.tsx +1 -1
  49. package/src/client/components/workspace/chat-timeline/tool-renderers/ask-panel/FrameControl.tsx +11 -20
  50. package/src/client/components/workspace/chat-timeline/tool-renderers/ask-panel/GroupedAsk.tsx +1 -1
  51. package/src/client/components/workspace/chat-timeline/tool-renderers/ask-panel/QuestionBlock.tsx +1 -1
  52. package/src/client/components/workspace/chat-timeline/tool-renderers/ask-panel/drafts.ts +1 -1
  53. package/src/client/components/workspace/chat-timeline/tool-renderers/ask-panel/index.tsx +2 -1
  54. package/src/client/components/workspace/file-explorer/index.test.ts +25 -0
  55. package/src/client/components/workspace/file-explorer/index.tsx +9 -3
  56. package/src/client/components/workspace/model-dropdown/harness-models.test.ts +17 -0
  57. package/src/client/components/workspace/model-dropdown/harness-models.ts +33 -18
  58. package/src/client/components/workspace/model-dropdown/index.tsx +3 -3
  59. package/src/client/components/workspace/model-dropdown/popover-metrics.ts +4 -1
  60. package/src/client/components/workspace/terminal-panel/KeyBar.tsx +10 -8
  61. package/src/client/components/workspace/terminal-panel/{RealtimeXtermView.tsx → RealtimeTerminalView.tsx} +51 -58
  62. package/src/client/components/workspace/terminal-panel/fit.test.ts +37 -0
  63. package/src/client/components/workspace/terminal-panel/fit.ts +64 -0
  64. package/src/client/components/workspace/terminal-panel/fonts.test.ts +161 -0
  65. package/src/client/components/workspace/terminal-panel/fonts.ts +164 -0
  66. package/src/client/components/workspace/terminal-panel/grid.test.ts +52 -0
  67. package/src/client/components/workspace/terminal-panel/grid.ts +118 -0
  68. package/src/client/components/workspace/terminal-panel/index.tsx +25 -21
  69. package/src/client/components/workspace/terminal-panel/mount.ts +247 -223
  70. package/src/client/components/workspace/terminal-panel/navigation.test.ts +54 -0
  71. package/src/client/components/workspace/terminal-panel/navigation.ts +47 -0
  72. package/src/client/data/settings/omp-schema.json +5 -5
  73. package/src/client/data/theme/terminal.test.ts +25 -0
  74. package/src/client/data/theme/terminal.ts +96 -79
  75. package/src/client/hooks/chat/composer/items.test.ts +12 -26
  76. package/src/client/hooks/chat/composer/pick-mapping.test.ts +42 -0
  77. package/src/client/hooks/chat/omp/prompt-send.test.ts +7 -7
  78. package/src/client/hooks/chat/omp/prompt-send.ts +26 -14
  79. package/src/client/hooks/chat/timeline/actions.ts +2 -2
  80. package/src/client/hooks/chat/timeline/ask-frames.ts +5 -4
  81. package/src/client/hooks/chat/timeline/new-chat-open.test.ts +86 -0
  82. package/src/client/hooks/chat/timeline/new-chat-open.ts +23 -5
  83. package/src/client/hooks/chat/timeline/new-chat.test.ts +32 -4
  84. package/src/client/hooks/chat/timeline/new-chat.ts +18 -6
  85. package/src/client/hooks/chat/timeline/prompt-text.test.ts +34 -1
  86. package/src/client/hooks/chat/timeline/prompt-text.ts +47 -20
  87. package/src/client/hooks/chat/timeline/rewind-actions.test.ts +24 -1
  88. package/src/client/hooks/chat/timeline/rewind-actions.ts +15 -11
  89. package/src/client/hooks/chat/timeline/run-identity.test.ts +52 -0
  90. package/src/client/hooks/chat/timeline/run-identity.ts +23 -11
  91. package/src/client/hooks/chat/timeline/send-execute.test.ts +56 -0
  92. package/src/client/hooks/chat/timeline/send-helpers.ts +30 -1
  93. package/src/client/hooks/chat/timeline/send-props.ts +5 -3
  94. package/src/client/hooks/chat/timeline/send-steer.test.ts +4 -4
  95. package/src/client/hooks/chat/timeline/send.test-util.ts +8 -2
  96. package/src/client/hooks/chat/timeline/send.test.ts +4 -4
  97. package/src/client/hooks/chat/timeline/send.ts +59 -53
  98. package/src/client/hooks/chat/timeline/session-load.test-util.ts +6 -2
  99. package/src/client/hooks/chat/timeline/session-load.test.ts +31 -0
  100. package/src/client/hooks/chat/timeline/session-load.ts +6 -14
  101. package/src/client/hooks/chat/timeline/session-shapes.ts +32 -0
  102. package/src/client/hooks/chat/timeline/use-fork-session.ts +49 -0
  103. package/src/client/hooks/models/harness-catalog.ts +114 -0
  104. package/src/client/hooks/models/provider-writes.test.ts +81 -0
  105. package/src/client/hooks/models/provider-writes.ts +10 -3
  106. package/src/client/hooks/models/use-harness-update.test.ts +193 -0
  107. package/src/client/hooks/models/use-harness-update.ts +202 -0
  108. package/src/client/hooks/models/use-harnesses-controller.ts +57 -0
  109. package/src/client/hooks/models/use-harnesses-signal.test.ts +128 -0
  110. package/src/client/hooks/models/use-harnesses.test.ts +2 -1
  111. package/src/client/hooks/models/use-harnesses.ts +48 -114
  112. package/src/client/hooks/workspace/child-expansion.ts +33 -0
  113. package/src/client/hooks/workspace/terminal/index.ts +17 -3
  114. package/src/client/hooks/workspace/terminal/socket.ts +9 -4
  115. package/src/client/lib/session-tree.test.ts +50 -0
  116. package/src/client/lib/session-tree.ts +59 -0
  117. package/src/client/tailwind.css +26 -12
  118. package/src/server/index.ts +2 -0
  119. package/src/server/lib/auth/guard.ts +10 -0
  120. package/src/server/lib/btw/session-copy.server.ts +6 -44
  121. package/src/server/lib/btw/session-copy.test.ts +2 -1
  122. package/src/server/lib/db/schema.ts +4 -0
  123. package/src/server/lib/db/seed-terminal.test.ts +1 -1
  124. package/src/server/lib/db/seed.test.ts +1 -1
  125. package/src/server/lib/harness/acp/activation.test.ts +81 -0
  126. package/src/server/lib/harness/acp/activation.ts +72 -0
  127. package/src/server/lib/harness/acp/background-work.test.ts +262 -0
  128. package/src/server/lib/harness/acp/background-work.ts +320 -0
  129. package/src/server/lib/harness/acp/client.ts +2 -2
  130. package/src/server/lib/harness/acp/frames.test.ts +50 -1
  131. package/src/server/lib/harness/acp/frames.ts +17 -5
  132. package/src/server/lib/harness/acp/gateway-env.test.ts +64 -0
  133. package/src/server/lib/harness/acp/gateway-env.ts +70 -0
  134. package/src/server/lib/harness/acp/mapper.ts +6 -4
  135. package/src/server/lib/harness/acp/prompt.test.ts +20 -0
  136. package/src/server/lib/harness/acp/prompt.ts +19 -3
  137. package/src/server/lib/harness/acp/session-background.test.ts +129 -0
  138. package/src/server/lib/harness/acp/session-config.test.ts +73 -0
  139. package/src/server/lib/harness/acp/session-config.ts +87 -6
  140. package/src/server/lib/harness/acp/session-exit.ts +31 -0
  141. package/src/server/lib/harness/acp/session-handshake.test.ts +108 -0
  142. package/src/server/lib/harness/acp/session-handshake.ts +36 -3
  143. package/src/server/lib/harness/acp/session-idle.ts +51 -0
  144. package/src/server/lib/harness/acp/session-listeners.ts +31 -0
  145. package/src/server/lib/harness/acp/session-notifications.ts +19 -3
  146. package/src/server/lib/harness/acp/session-observers.test.ts +9 -0
  147. package/src/server/lib/harness/acp/session-options.ts +33 -0
  148. package/src/server/lib/harness/acp/session-reaper.test.ts +96 -0
  149. package/src/server/lib/harness/acp/session-requests.ts +47 -0
  150. package/src/server/lib/harness/acp/session-turns.ts +20 -5
  151. package/src/server/lib/harness/acp/session.test-util.ts +54 -3
  152. package/src/server/lib/harness/acp/session.test.ts +65 -3
  153. package/src/server/lib/harness/acp/session.ts +79 -79
  154. package/src/server/lib/harness/acp/turn.test.ts +81 -0
  155. package/src/server/lib/harness/acp/turn.ts +54 -13
  156. package/src/server/lib/harness/acp/verbs.ts +12 -2
  157. package/src/server/lib/harness/auto-probe.server.test.ts +121 -0
  158. package/src/server/lib/harness/auto-probe.server.ts +178 -0
  159. package/src/server/lib/harness/config.server.test.ts +1 -1
  160. package/src/server/lib/harness/config.server.ts +1 -1
  161. package/src/server/lib/harness/launch-table.ts +195 -0
  162. package/src/server/lib/harness/launch.test.ts +37 -0
  163. package/src/server/lib/harness/launch.ts +16 -116
  164. package/src/server/lib/harness/provider-entry.ts +32 -0
  165. package/src/server/lib/harness/provider-file.server.ts +3 -0
  166. package/src/server/lib/harness/provider-sources.server.test.ts +45 -0
  167. package/src/server/lib/harness/provider-sources.server.ts +26 -2
  168. package/src/server/lib/harness/provider-write.server.ts +21 -32
  169. package/src/server/lib/harness/provider-zcode.server.test.ts +270 -0
  170. package/src/server/lib/harness/provider-zcode.server.ts +324 -0
  171. package/src/server/lib/harness/spawn.server.ts +15 -2
  172. package/src/server/lib/harness/update-job-store.test.ts +126 -0
  173. package/src/server/lib/harness/update-job-store.ts +109 -0
  174. package/src/server/lib/harness/update-job.server.ts +203 -0
  175. package/src/server/lib/harness/update-state.server.ts +15 -20
  176. package/src/server/lib/harness/update-worker.test.ts +153 -0
  177. package/src/server/lib/harness/update-worker.ts +197 -0
  178. package/src/server/lib/lifecycle/after-listen.ts +7 -0
  179. package/src/server/lib/lifecycle/paths.ts +23 -0
  180. package/src/server/lib/models/registry-refresh.server.test.ts +87 -0
  181. package/src/server/lib/models/registry-refresh.server.ts +98 -0
  182. package/src/server/lib/omp/rpc/child-config.ts +5 -4
  183. package/src/server/lib/omp/rpc/constants.test.ts +2 -1
  184. package/src/server/lib/omp/rpc/forked-resume.test.ts +14 -3
  185. package/src/server/lib/omp/rpc/manager.ts +5 -5
  186. package/src/server/lib/omp/rpc/prewarm-pool.ts +12 -3
  187. package/src/server/lib/omp/rpc/session-registry.ts +14 -2
  188. package/src/server/lib/omp/rpc/terminal-settle.ts +53 -5
  189. package/src/server/lib/omp/session/delete-flow.server.ts +62 -8
  190. package/src/server/lib/omp/session/delete.server.ts +17 -0
  191. package/src/server/lib/omp/session/rewind-file.ts +39 -21
  192. package/src/server/lib/omp/session/sidebar-data.server.ts +6 -6
  193. package/src/server/lib/omp/session/transcript-copy.ts +123 -0
  194. package/src/server/lib/sessions/agent-token.server.test.ts +64 -0
  195. package/src/server/lib/sessions/agent-token.server.ts +134 -0
  196. package/src/server/lib/sessions/answers.server.test.ts +33 -0
  197. package/src/server/lib/sessions/answers.server.ts +37 -0
  198. package/src/server/lib/sessions/dispatch.server.test.ts +143 -0
  199. package/src/server/lib/sessions/dispatch.server.ts +128 -0
  200. package/src/server/lib/sessions/fork.server.test.ts +209 -0
  201. package/src/server/lib/sessions/fork.server.ts +304 -0
  202. package/src/server/lib/sessions/lifecycle.server.test.ts +75 -0
  203. package/src/server/lib/sessions/lifecycle.server.ts +71 -0
  204. package/src/server/lib/sessions/notify.server.test.ts +112 -0
  205. package/src/server/lib/sessions/notify.server.ts +212 -0
  206. package/src/server/lib/sessions/relation-types.ts +76 -0
  207. package/src/server/lib/sessions/relations.server.test.ts +207 -0
  208. package/src/server/lib/sessions/relations.server.ts +337 -0
  209. package/src/server/lib/sessions/resolve.server.ts +106 -0
  210. package/src/server/lib/sessions/schema.server.ts +82 -0
  211. package/src/server/lib/sessions/sidebar-relations.server.test.ts +61 -0
  212. package/src/server/lib/sessions/sidebar-relations.server.ts +47 -0
  213. package/src/server/lib/sessions/spawn.server.test.ts +196 -0
  214. package/src/server/lib/sessions/spawn.server.ts +310 -0
  215. package/src/server/lib/terminal/attach-replay.test.ts +2 -2
  216. package/src/server/lib/terminal/output.test.ts +2 -2
  217. package/src/server/lib/terminal/output.ts +1 -1
  218. package/src/server/lib/terminal/resize.test.ts +99 -0
  219. package/src/server/lib/terminal/runtime.server.ts +26 -6
  220. package/src/server/lib/terminal/send-result.test.ts +18 -1
  221. package/src/server/lib/terminal/send-result.ts +26 -2
  222. package/src/server/lib/terminal/session.server.ts +6 -6
  223. package/src/server/lib/usage/new-api.test.ts +92 -0
  224. package/src/server/lib/usage/new-api.ts +131 -0
  225. package/src/server/lib/usage/provider-endpoint.test.ts +106 -0
  226. package/src/server/lib/usage/provider-key.ts +33 -0
  227. package/src/server/lib/usage/providers.server.ts +10 -3
  228. package/src/server/lib/usage/vendor-balance.server.ts +4 -53
  229. package/src/server/lib/usage/vendor-http.ts +41 -0
  230. package/src/server/lib/usage/window.ts +55 -0
  231. package/src/server/routes/agent/command.ts +14 -1
  232. package/src/server/routes/agent/harness-command.ts +6 -0
  233. package/src/server/routes/agent/new.ts +15 -0
  234. package/src/server/routes/chat/rewind.test.ts +147 -0
  235. package/src/server/routes/chat/rewind.ts +29 -7
  236. package/src/server/routes/chat/session.test.ts +165 -10
  237. package/src/server/routes/chat/session.ts +60 -7
  238. package/src/server/routes/harness/add-provider.test.ts +9 -1
  239. package/src/server/routes/harness/import-action.ts +22 -4
  240. package/src/server/routes/harness/import.test.ts +1 -1
  241. package/src/server/routes/harness/index.ts +5 -1
  242. package/src/server/routes/harness/root.ts +8 -49
  243. package/src/server/routes/harness/toggle.test.ts +20 -0
  244. package/src/server/routes/harness/update-status.ts +26 -0
  245. package/src/server/routes/harness/update.test.ts +119 -0
  246. package/src/server/routes/harness/update.ts +67 -113
  247. package/src/server/routes/harness/versions.ts +5 -1
  248. package/src/server/routes/sessions/answer.ts +24 -0
  249. package/src/server/routes/sessions/batch-delete.ts +11 -5
  250. package/src/server/routes/sessions/delete-acp.test.ts +227 -0
  251. package/src/server/routes/sessions/delete.ts +4 -2
  252. package/src/server/routes/sessions/fork.ts +74 -0
  253. package/src/server/routes/sessions/index.ts +14 -0
  254. package/src/server/routes/sessions/relations.ts +85 -0
  255. package/src/server/routes/sessions/session.ts +4 -0
  256. package/src/server/routes/sessions/spawn.ts +134 -0
  257. package/src/server/routes/settings/model-override.ts +5 -2
  258. package/src/server/routes/settings/provider-entry.ts +2 -2
  259. package/src/server/routes/settings/provider-model.ts +2 -2
  260. package/src/server/routes/settings/provider-models.ts +2 -2
  261. package/src/server/routes/settings/provider-refresh.ts +6 -4
  262. package/src/server/routes/settings/providers.ts +3 -1
  263. package/src/server/routes/settings/settings-config-routes.test.ts +10 -2
  264. package/src/server/routes/terminal/ws.ts +3 -3
  265. package/src/shared/lib/chat/ask-form/fields.ts +76 -0
  266. package/src/shared/lib/chat/ask-form/index.ts +323 -0
  267. package/src/shared/lib/chat/ask-form/tokens.ts +199 -0
  268. package/src/shared/lib/chat/ask-form/types.ts +43 -0
  269. package/src/shared/lib/chat/ask-form.test.ts +137 -0
  270. package/src/shared/lib/chat/ask-questions.test.ts +65 -1
  271. package/src/shared/lib/chat/ask-questions.ts +51 -145
  272. package/src/shared/lib/chat/ask-results.ts +198 -0
  273. package/src/shared/lib/chat/composer/client.ts +31 -2
  274. package/src/shared/lib/chat/composer/delegation.test.ts +93 -0
  275. package/src/shared/lib/chat/composer/delegation.ts +206 -0
  276. package/src/shared/lib/chat/notice-text.ts +1 -1
  277. package/src/shared/lib/chat/timeline/omp-callbacks.ts +13 -1
  278. package/src/shared/lib/chat/timeline/prompt-error.test.ts +146 -0
  279. package/src/shared/lib/chat/timeline/prompt-error.ts +88 -0
  280. package/src/shared/lib/chat/tool/summary-wait.test.ts +11 -0
  281. package/src/shared/lib/code/ansi.ts +1 -1
  282. package/src/shared/lib/fonts/nerd-symbols.css +4 -3
  283. package/src/shared/lib/fonts/nerd-symbols.test.ts +9 -9
  284. package/src/shared/lib/harness/catalog.test.ts +1 -1
  285. package/src/shared/lib/harness/config-fields.test.ts +4 -0
  286. package/src/shared/lib/harness/config-fields.ts +19 -4
  287. package/src/shared/lib/harness/dialects.test.ts +8 -0
  288. package/src/shared/lib/harness/dialects.ts +12 -0
  289. package/src/shared/lib/harness/provider-facts.test.ts +59 -8
  290. package/src/shared/lib/harness/provider-facts.ts +46 -11
  291. package/src/shared/lib/harness/providers.ts +44 -1
  292. package/src/shared/lib/harness/rewind.ts +24 -0
  293. package/src/shared/lib/harness/update-job.ts +33 -0
  294. package/src/shared/lib/harness/update-plan.test.ts +69 -4
  295. package/src/shared/lib/harness/update-plan.ts +57 -9
  296. package/src/shared/lib/harness/updates.test.ts +22 -0
  297. package/src/shared/lib/harness/updates.ts +24 -1
  298. package/src/shared/lib/models/client.ts +10 -1
  299. package/src/shared/lib/omp/dialog-methods.ts +4 -3
  300. package/src/shared/lib/omp/session/wait-idle.test.ts +11 -0
  301. package/src/shared/lib/omp/session/wait-idle.ts +12 -2
  302. package/src/shared/lib/theme/catalog.test.ts +13 -9
  303. package/src/shared/lib/theme/color.test.ts +2 -2
  304. package/src/shared/lib/theme/color.ts +4 -4
  305. package/src/shared/lib/theme/css.ts +2 -0
  306. package/src/shared/lib/theme/ink-scale.ts +21 -1
  307. package/src/shared/lib/theme/theme.test.ts +26 -10
  308. package/src/shared/lib/ui/safe-area.test.ts +28 -0
  309. package/src/shared/lib/ui/safe-area.ts +42 -0
  310. package/src/shared/lib/workspace/terminal/input.test.ts +27 -0
  311. package/src/shared/lib/workspace/terminal/input.ts +33 -0
  312. package/src/shared/lib/workspace/terminal/keys.test.ts +1 -1
  313. package/src/shared/lib/workspace/terminal/keys.ts +7 -7
  314. package/src/shared/types/chat.ts +1 -1
  315. package/src/shared/types/harness-update.ts +65 -0
  316. package/src/shared/types/harness.ts +30 -4
  317. package/src/shared/types/omp/agent.ts +19 -2
  318. package/src/shared/types/workspace.ts +4 -0
  319. package/src/test-support/fake-omp-fork.ts +16 -2
  320. package/src/test-support/file-explorer-render.test-util.ts +37 -0
  321. package/src/test-support/file-explorer.test-util.ts +20 -18
  322. package/src/test-support/harness-table.test-util.ts +21 -4
  323. package/dist/client/static/js/index-jhmg4w91.css +0 -1
  324. package/src/client/components/workspace/terminal-panel/webgl.test.ts +0 -75
  325. package/src/client/components/workspace/terminal-panel/webgl.ts +0 -108
@@ -2,11 +2,10 @@ import { useEffect, useRef, useState, useImperativeHandle, useCallback } from 'p
2
2
  import { forwardRef } from 'preact/compat';
3
3
  import { ArrowDown } from 'lucide-preact';
4
4
  import { useTheme } from '@/client/hooks/ui/theme';
5
- import { getXtermTheme } from '@/client/data/theme/terminal';
6
- import { mountXterm, type XtermMount } from '@/client/components/workspace/terminal-panel/mount';
5
+ import { mountTerminal, type TerminalMount } from '@/client/components/workspace/terminal-panel/mount';
7
6
 
8
- export interface RealtimeXtermHandle {
9
- /** Raw PTY output. Bytes, not text: xterm's decoder reassembles them. */
7
+ export interface RealtimeTerminalHandle {
8
+ /** Raw PTY output. Bytes, not text: the WASM parser reassembles them. */
10
9
  write: (bytes: Uint8Array) => void;
11
10
  /**
12
11
  * Write the retained scrollback at the size it was drawn for, then fit back
@@ -26,40 +25,43 @@ export interface RealtimeXtermHandle {
26
25
  /** The current selection, empty when nothing is selected. */
27
26
  getSelection: () => string;
28
27
  /**
29
- * Feed text in as a paste. Goes through xterm's own `paste`, which is what
30
- * wraps it in the bracketed-paste markers when the shell asked for them —
31
- * writing the bytes directly would make a multi-line paste execute line by
32
- * line in an editor that expects one block.
28
+ * Feed text in as a paste. Goes through the terminal's own `paste`, which is
29
+ * what wraps it in the bracketed-paste markers when the shell asked for
30
+ * them — writing the bytes directly would make a multi-line paste execute
31
+ * line by line in an editor that expects one block.
33
32
  */
34
33
  paste: (text: string) => void;
35
34
  }
36
35
 
37
- interface RealtimeXtermViewProps {
36
+ interface RealtimeTerminalViewProps {
38
37
  /** Keystrokes and paste, raw. */
39
38
  onInput: (data: string) => void;
40
- /** Non-UTF-8 payloads xterm reports separately (some mouse reports). */
41
- onBinaryInput: (data: string) => void;
39
+ /** Non-UTF-8 payloads (legacy X10 mouse reports), as latin-1 bytes. */
40
+ onBinaryInput: (bytes: Uint8Array) => void;
42
41
  /** The grid changed; the PTY must be resized to match. */
43
42
  onGridChange: (cols: number, rows: number) => void;
44
43
  /** The terminal is measurable and knows its size; attach with these. */
45
44
  onReady: (cols: number, rows: number) => void;
46
- /** False while another panel is on screen: the WebGL context is released. */
47
- visible?: boolean;
45
+ /**
46
+ * The theme changed and the terminal was rebuilt with the new palette; the
47
+ * server's retained scrollback has to be requested again.
48
+ */
49
+ onReattach?: () => void;
48
50
  }
49
51
 
50
52
  /**
51
- * xterm view bound to a real PTY.
53
+ * ghostty-web view bound to a real PTY.
52
54
  *
53
55
  * Every keystroke goes to the shell as bytes — line editing, history, tab
54
56
  * completion and Ctrl+C are the shell's job, not this component's. Output is
55
57
  * written as bytes so a character split across two PTY reads is reassembled by
56
- * xterm's own streaming decoder rather than by a decode step here.
58
+ * the WASM parser rather than by a decode step here.
57
59
  */
58
- export const RealtimeXtermView = forwardRef<RealtimeXtermHandle, RealtimeXtermViewProps>(
59
- ({ onInput, onBinaryInput, onGridChange, onReady, visible = true }, ref) => {
60
+ export const RealtimeTerminalView = forwardRef<RealtimeTerminalHandle, RealtimeTerminalViewProps>(
61
+ ({ onInput, onBinaryInput, onGridChange, onReady, onReattach }, ref) => {
60
62
  const { theme } = useTheme();
61
63
  const containerRef = useRef<HTMLDivElement>(null);
62
- const mountRef = useRef<XtermMount | null>(null);
64
+ const mountRef = useRef<TerminalMount | null>(null);
63
65
  const [isScrolledUp, setIsScrolledUp] = useState(false);
64
66
 
65
67
  const inputRef = useRef(onInput);
@@ -70,63 +72,50 @@ export const RealtimeXtermView = forwardRef<RealtimeXtermHandle, RealtimeXtermVi
70
72
  gridChangeRef.current = onGridChange;
71
73
  const readyRef = useRef(onReady);
72
74
  readyRef.current = onReady;
75
+ const reattachRef = useRef(onReattach);
76
+ reattachRef.current = onReattach;
73
77
  const themeRef = useRef(theme);
74
78
  themeRef.current = theme;
75
- // Read when the async mount RESOLVES, not when this effect ran: `mountXterm`
76
- // imports xterm and the WebGL addon, so `mountRef.current` is still null on
77
- // the first pass and an effect that only ran then would leave the context
78
- // held by a panel that was never on screen.
79
- const visibleRef = useRef(visible);
80
- visibleRef.current = visible;
81
-
82
- useEffect(() => {
83
- const term = mountRef.current?.term;
84
- if (!term) return;
85
- term.options.theme = getXtermTheme(theme);
86
- term.refresh(0, term.rows - 1);
87
- }, [theme]);
88
79
 
89
80
  /**
90
- * Hold the WebGL context only while this view is on screen.
91
- *
92
- * The right-panel stack keeps every built-in view MOUNTED and hides the
93
- * inactive ones with CSS; the terminal is the one view that stays live while
94
- * hidden, because it owns a PTY. That made its GPU context permanent: eight
95
- * terminals held eight contexts, and `vmmap` on the desktop shell showed
96
- * `owned unmapped (graphics)` at 541.8 MB of an 817.7 MB footprint while the
97
- * JS heap was 12.6 MB. Releasing on hide drops the context — xterm's DOM
98
- * renderer keeps the buffer readable — and showing the panel re-creates it.
81
+ * The WASM palette is fixed when the terminal is created, so a theme switch
82
+ * is a terminal rebuild plus a scrollback replay, not a repaint. The effect
83
+ * runs before the async mount resolves on first render (`mountRef` is null
84
+ * then); that initial theme is the one `mountTerminal` was called with.
99
85
  */
100
86
  useEffect(() => {
101
- mountRef.current?.setWebgl(visible);
102
- }, [visible]);
87
+ const mount = mountRef.current;
88
+ if (!mount) return;
89
+ mount.setTheme(theme);
90
+ reattachRef.current?.();
91
+ }, [theme]);
103
92
 
104
93
  useImperativeHandle(ref, () => ({
105
94
  write(bytes: Uint8Array) {
106
- mountRef.current?.term.write(bytes);
95
+ mountRef.current?.write(bytes);
107
96
  },
108
97
  writeReplay(bytes: Uint8Array, cols: number, rows: number) {
109
98
  mountRef.current?.writeReplay(bytes, cols, rows);
110
99
  },
111
100
  reset() {
112
- mountRef.current?.term.reset();
101
+ mountRef.current?.reset();
113
102
  },
114
103
  focus() {
115
- mountRef.current?.term.focus();
104
+ mountRef.current?.focus();
116
105
  },
117
106
  applicationCursorKeys() {
118
- return mountRef.current?.term.modes.applicationCursorKeysMode ?? false;
107
+ return mountRef.current?.applicationCursorKeys() ?? false;
119
108
  },
120
109
  getSelection() {
121
- return mountRef.current?.term.getSelection() ?? '';
110
+ return mountRef.current?.getSelection() ?? '';
122
111
  },
123
112
  paste(text: string) {
124
- mountRef.current?.term.paste(text);
113
+ mountRef.current?.paste(text);
125
114
  },
126
115
  }), []);
127
116
 
128
117
  const scrollToBottom = useCallback(() => {
129
- mountRef.current?.term.scrollToBottom();
118
+ mountRef.current?.scrollToBottom();
130
119
  setIsScrolledUp(false);
131
120
  }, []);
132
121
 
@@ -137,12 +126,12 @@ export const RealtimeXtermView = forwardRef<RealtimeXtermHandle, RealtimeXtermVi
137
126
  if (!container) return;
138
127
  let cancelled = false;
139
128
 
140
- void mountXterm(
129
+ void mountTerminal(
141
130
  container,
142
131
  themeRef.current,
143
132
  {
144
133
  onInput: (data) => inputRef.current(data),
145
- onBinaryInput: (data) => binaryInputRef.current(data),
134
+ onBinaryInput: (bytes) => binaryInputRef.current(bytes),
146
135
  onGridChange: (cols, rows) => gridChangeRef.current(cols, rows),
147
136
  onReady: (cols, rows) => readyRef.current(cols, rows),
148
137
  onScrolledUpChange: setIsScrolledUp,
@@ -155,10 +144,9 @@ export const RealtimeXtermView = forwardRef<RealtimeXtermHandle, RealtimeXtermVi
155
144
  return;
156
145
  }
157
146
  mountRef.current = mount;
158
- // The panel can mount while another view is on screen — the stack keeps
159
- // every built-in mounted — so the context is released on arrival rather
160
- // than on the next visibility change.
161
- mount.setWebgl(visibleRef.current);
147
+ // E2E types into the terminal's hidden input. The renderer creates it
148
+ // inside `open`, so the hook cannot be written on the JSX element.
149
+ container.querySelector('textarea')?.setAttribute('data-terminal-input', '');
162
150
  });
163
151
 
164
152
  return () => {
@@ -168,11 +156,16 @@ export const RealtimeXtermView = forwardRef<RealtimeXtermHandle, RealtimeXtermVi
168
156
  };
169
157
  }, []);
170
158
 
159
+ // No background and no padding: the terminal is not a surface of its own,
160
+ // and the panel's paper shows through the strip the fit addon leaves for
161
+ // its in-canvas scrollbar. `p-1.5` also made the grid narrower than the
162
+ // panel, which read as a terminal that was not full width.
171
163
  return (
172
- <div className="relative flex-1 w-full h-full bg-canvas overflow-hidden">
164
+ <div className="relative flex-1 w-full h-full overflow-hidden">
173
165
  <div
174
166
  ref={containerRef}
175
- className="w-full h-full cursor-text p-1.5"
167
+ data-terminal-root=""
168
+ className="kabmo-terminal w-full h-full cursor-text"
176
169
  style={{ height: '100%', width: '100%' }}
177
170
  />
178
171
 
@@ -192,4 +185,4 @@ export const RealtimeXtermView = forwardRef<RealtimeXtermHandle, RealtimeXtermVi
192
185
  }
193
186
  );
194
187
 
195
- RealtimeXtermView.displayName = 'RealtimeXtermView';
188
+ RealtimeTerminalView.displayName = 'RealtimeTerminalView';
@@ -0,0 +1,37 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ import { describe, expect, test } from 'bun:test';
7
+ import { gridForContainer } from '@/client/components/workspace/terminal-panel/fit';
8
+
9
+ // The measured cell box at 12 px Fira Code.
10
+ const CELL = { width: 8, height: 16 };
11
+
12
+ describe('gridForContainer', () => {
13
+ test('divides the whole panel, with no scrollbar reservation', () => {
14
+ // 334 × 759 is the host measured in the right panel at 1440 × 900.
15
+ // ghostty-web's own proposal subtracts 15 px first and would answer 39;
16
+ // that shortfall is what read as a terminal that was not full width.
17
+ expect(gridForContainer(334, 759, CELL)).toEqual({ cols: 41, rows: 47 });
18
+ });
19
+
20
+ test('drops the sub-cell remainder instead of overflowing the box', () => {
21
+ // One pixel short of a column still fits 41; the extra 7 px of a 335 box
22
+ // must not become a 42nd cell that hangs outside the panel.
23
+ expect(gridForContainer(335, 767, CELL)).toEqual({ cols: 41, rows: 47 });
24
+ expect(gridForContainer(333, 758, CELL)).toEqual({ cols: 41, rows: 47 });
25
+ });
26
+
27
+ test('keeps a usable grid for a box smaller than a cell', () => {
28
+ // A hidden panel measures 0 while the view stays mounted; the caller guards
29
+ // that, and this floor is the last line of defence against 0 × 0.
30
+ expect(gridForContainer(0, 0, CELL)).toEqual({ cols: 2, rows: 1 });
31
+ });
32
+
33
+ test('refuses to divide by an unmeasured cell', () => {
34
+ expect(gridForContainer(320, 640, { width: 0, height: 16 })).toBeNull();
35
+ expect(gridForContainer(320, 640, { width: 8, height: 0 })).toBeNull();
36
+ });
37
+ });
@@ -0,0 +1,64 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ /**
7
+ * Fit the terminal's grid to the whole container.
8
+ *
9
+ * ghostty-web's `FitAddon.proposeDimensions` subtracts a hard-coded 15 px for
10
+ * its in-canvas scrollbar before dividing by the cell width, so the grid always
11
+ * stopped one to three columns short of the panel: with the terminal painting
12
+ * its own background that showed as a strip, and a full-width TUI (tmux status
13
+ * line, `htop`) ran out of room the panel visibly had. The scrollbar is drawn
14
+ * INSIDE the canvas, 8 px at the right edge and only while scrolling, so the
15
+ * reservation buys nothing here — it can overlap the last column instead.
16
+ *
17
+ * Everything else stays the addon's: it resizes only when the grid changes and
18
+ * it still reads `renderer.getMetrics()`, which is Kabmo's measured cell box
19
+ * (see `installCellMetrics` in `fonts.ts`).
20
+ */
21
+
22
+ import type { FitAddon, Terminal } from 'ghostty-web';
23
+ import type { TerminalCellMetrics } from '@/client/components/workspace/terminal-panel/fonts';
24
+
25
+ export interface TerminalGrid {
26
+ cols: number;
27
+ rows: number;
28
+ }
29
+
30
+ /**
31
+ * The grid a box of `width` × `height` CSS px holds, at the measured cell size.
32
+ *
33
+ * Pure, so the arithmetic is pinned without a renderer: it divides the FULL box,
34
+ * with the container's own padding subtracted because the host may keep some.
35
+ */
36
+ export function gridForContainer(
37
+ width: number,
38
+ height: number,
39
+ metrics: Pick<TerminalCellMetrics, 'width' | 'height'>,
40
+ ): TerminalGrid | null {
41
+ if (!metrics.width || !metrics.height) return null;
42
+ return {
43
+ cols: Math.max(2, Math.floor(width / metrics.width)),
44
+ rows: Math.max(1, Math.floor(height / metrics.height)),
45
+ };
46
+ }
47
+
48
+ /** Replace the addon's proposal with `gridForContainer` over the live host. */
49
+ export function installFullWidthFit(fitAddon: FitAddon, term: Terminal): void {
50
+ const addon = fitAddon as unknown as {
51
+ proposeDimensions: () => TerminalGrid | undefined;
52
+ };
53
+ addon.proposeDimensions = () => {
54
+ const host = term.element;
55
+ const metrics = term.renderer?.getMetrics();
56
+ if (!host || !metrics) return undefined;
57
+ const style = getComputedStyle(host);
58
+ const padX = (parseFloat(style.paddingLeft) || 0) + (parseFloat(style.paddingRight) || 0);
59
+ const padY = (parseFloat(style.paddingTop) || 0) + (parseFloat(style.paddingBottom) || 0);
60
+ return (
61
+ gridForContainer(host.clientWidth - padX, host.clientHeight - padY, metrics) ?? undefined
62
+ );
63
+ };
64
+ }
@@ -0,0 +1,161 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ import { afterEach, describe, expect, test } from 'bun:test';
7
+ import {
8
+ measureTerminalCell,
9
+ waitForTerminalFonts,
10
+ watchTerminalFonts,
11
+ } from '@/client/components/workspace/terminal-panel/fonts';
12
+
13
+ type LoadingListener = (event: { fontfaces: { family: string }[] }) => void;
14
+
15
+ interface FakeFonts {
16
+ load: (font: string, text?: string) => Promise<unknown>;
17
+ addEventListener: (_type: string, listener: LoadingListener) => void;
18
+ removeEventListener: (_type: string, listener: LoadingListener) => void;
19
+ }
20
+
21
+ function installFakeDocument(): { fonts: FakeFonts; listeners: Set<LoadingListener> } {
22
+ const listeners = new Set<LoadingListener>();
23
+ const fonts: FakeFonts = {
24
+ load: async () => [],
25
+ addEventListener: (_type, listener) => listeners.add(listener),
26
+ removeEventListener: (_type, listener) => listeners.delete(listener),
27
+ };
28
+ // Bun has no DOM; install the one object the helpers read.
29
+ const host = globalThis as unknown as { document?: unknown };
30
+ host.document = { fonts };
31
+ return { fonts, listeners };
32
+ }
33
+
34
+ afterEach(() => {
35
+ const host = globalThis as unknown as { document?: unknown };
36
+ delete host.document;
37
+ });
38
+
39
+ describe('waitForTerminalFonts', () => {
40
+ test('loads the text face only, so the bundled symbols face stays lazy', async () => {
41
+ const { fonts } = installFakeDocument();
42
+ const calls: { font: string; text?: string }[] = [];
43
+ fonts.load = async (font, text) => {
44
+ calls.push({ font, ...(text ? { text } : {}) });
45
+ return [];
46
+ };
47
+
48
+ await waitForTerminalFonts();
49
+
50
+ expect(calls).toEqual([{ font: '12px "Fira Code"' }]);
51
+ expect(calls.some((call) => call.font.includes('Kabmo Nerd Symbols'))).toBe(false);
52
+ });
53
+
54
+ test('a face that fails to load does not keep the terminal closed', async () => {
55
+ const { fonts } = installFakeDocument();
56
+ fonts.load = async () => {
57
+ throw new Error('offline');
58
+ };
59
+
60
+ expect(await waitForTerminalFonts()).toBeUndefined();
61
+ });
62
+ });
63
+
64
+ describe('watchTerminalFonts', () => {
65
+ test('calls back only for a terminal face', () => {
66
+ const { listeners } = installFakeDocument();
67
+ let loaded = 0;
68
+ const dispose = watchTerminalFonts(() => {
69
+ loaded += 1;
70
+ });
71
+
72
+ for (const listener of listeners) listener({ fontfaces: [{ family: 'Inter' }] });
73
+ expect(loaded).toBe(0);
74
+
75
+ for (const listener of listeners) listener({ fontfaces: [{ family: '"Fira Code"' }] });
76
+ expect(loaded).toBe(1);
77
+
78
+ dispose();
79
+ expect(listeners.size).toBe(0);
80
+ });
81
+ });
82
+
83
+ describe('measureTerminalCell', () => {
84
+ interface Box {
85
+ advance: number;
86
+ /** Ink of `Mg`. */
87
+ ascent: number;
88
+ descent: number;
89
+ /** The font's own line box, as `fontBoundingBox*` reports it. */
90
+ lineAscent: number;
91
+ lineDescent: number;
92
+ }
93
+
94
+ function installFakeCanvas(box: Box): { fonts: unknown } {
95
+ const host = globalThis as unknown as { document?: unknown };
96
+ const context = {
97
+ font: '',
98
+ measureText: (text: string) => {
99
+ const descent = text === 'Mg' ? box.descent : 0;
100
+ return {
101
+ width: box.advance,
102
+ actualBoundingBoxAscent: box.ascent,
103
+ actualBoundingBoxDescent: descent,
104
+ fontBoundingBoxAscent: box.lineAscent,
105
+ fontBoundingBoxDescent: box.lineDescent,
106
+ };
107
+ },
108
+ };
109
+ host.document = { fonts: {}, createElement: () => ({ getContext: () => context }) };
110
+ return { fonts: {} };
111
+ }
112
+
113
+ // Fira Code at 12 px, measured in Chromium: an `M` advance of 7.2, an `Mg`
114
+ // ink of 8.26 above and 2.54 below the baseline, and a line box of 12 + 4.
115
+ const FIRA_CODE_12: Box = {
116
+ advance: 7.2,
117
+ ascent: 8.26,
118
+ descent: 2.54,
119
+ lineAscent: 12,
120
+ lineDescent: 4,
121
+ };
122
+
123
+ test('pads the ink 2.5 px above and 1 px below', () => {
124
+ installFakeCanvas(FIRA_CODE_12);
125
+
126
+ const cell = measureTerminalCell(12, "'Fira Code'");
127
+
128
+ // The cell is the paintable advance rounded up, not the font size.
129
+ expect(cell.width).toBe(8);
130
+ // 11 px of ink + 2.5 + 1, rounded up: a cell built from `M` alone was the
131
+ // reported bug — the descender ended on the cell's last pixel and the next
132
+ // row clipped it.
133
+ expect(cell.height).toBe(15);
134
+ const above = cell.baseline - FIRA_CODE_12.ascent;
135
+ const below = cell.height - cell.baseline - FIRA_CODE_12.descent;
136
+ expect(Math.abs(above - 2.5)).toBeLessThanOrEqual(0.5);
137
+ expect(Math.abs(below - 1)).toBeLessThanOrEqual(0.5);
138
+ });
139
+
140
+ test('never lets ink exceed the cell when it reaches past the line box', () => {
141
+ // A face whose `g` hangs lower than its `fontBoundingBox` says it should.
142
+ const box: Box = { advance: 7.2, ascent: 9, descent: 6, lineAscent: 10, lineDescent: 2 };
143
+ installFakeCanvas(box);
144
+
145
+ const cell = measureTerminalCell(12, "'Fira Code'");
146
+
147
+ expect(cell.height).toBeGreaterThanOrEqual(Math.ceil(box.ascent + box.descent) + 4);
148
+ expect(cell.baseline - box.ascent).toBeGreaterThanOrEqual(1);
149
+ expect(cell.height - cell.baseline - box.descent).toBeGreaterThanOrEqual(1);
150
+ });
151
+
152
+ test('answers a usable box when there is no DOM', () => {
153
+ const host = globalThis as unknown as { document?: unknown };
154
+ delete host.document;
155
+
156
+ const cell = measureTerminalCell(12, "'Fira Code'");
157
+
158
+ expect(cell.width).toBeGreaterThan(0);
159
+ expect(cell.height).toBeGreaterThan(cell.baseline);
160
+ });
161
+ });
@@ -0,0 +1,164 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ /**
7
+ * Font readiness for the terminal canvas.
8
+ *
9
+ * The renderer measures its cell box when the terminal opens. Measuring against
10
+ * a fallback face gives the grid the wrong width, and a phone has no installed
11
+ * Nerd Font to fall through to. A face that lands later is not fatal: the
12
+ * caller re-measures and fits.
13
+ */
14
+
15
+ import type { Terminal } from 'ghostty-web';
16
+ import {
17
+ TERMINAL_FACES,
18
+ TERMINAL_FONT_FAMILY,
19
+ TERMINAL_FONT_SIZE,
20
+ TERMINAL_TEXT_FAMILY,
21
+ } from '@/client/data/theme/terminal';
22
+
23
+ const FONT_LOAD_TIMEOUT_MS = 2000;
24
+
25
+ /**
26
+ * Wait for the text face, bounded by a timeout.
27
+ *
28
+ * Only the text face is loaded explicitly: the renderer measures `M` for its
29
+ * cell width, and that glyph comes from the text face. Asking for the bundled
30
+ * symbols face here would force its 1.1 MB download on every machine, including
31
+ * the ones whose own Nerd Font the fallback chain would have used — the
32
+ * `@font-face` is deliberately lazy. PUA glyphs that arrive late are repainted
33
+ * by `watchTerminalFonts`.
34
+ */
35
+ export async function waitForTerminalFonts(): Promise<void> {
36
+ if (typeof document === 'undefined' || !document.fonts?.load) return;
37
+ const timeout = Promise.withResolvers<void>();
38
+ const timer = setTimeout(timeout.resolve, FONT_LOAD_TIMEOUT_MS);
39
+ try {
40
+ await Promise.race([
41
+ document.fonts.load(`${TERMINAL_FONT_SIZE}px "${TERMINAL_TEXT_FAMILY}"`).then(
42
+ () => undefined,
43
+ () => undefined,
44
+ ),
45
+ timeout.promise,
46
+ ]);
47
+ } finally {
48
+ clearTimeout(timer);
49
+ }
50
+ }
51
+
52
+ /**
53
+ * Call `onLoaded` when one of the terminal's faces lands late.
54
+ *
55
+ * Returns a disposer.
56
+ */
57
+ export function watchTerminalFonts(onLoaded: () => void): () => void {
58
+ if (typeof document === 'undefined' || !document.fonts) return () => {};
59
+ const faces = new Set<string>(TERMINAL_FACES);
60
+ const listener = (event: FontFaceSetLoadEvent) => {
61
+ const loaded = [...event.fontfaces].some((face) =>
62
+ faces.has(face.family.replace(/['"]/g, '').trim()),
63
+ );
64
+ if (loaded) onLoaded();
65
+ };
66
+ document.fonts.addEventListener('loadingdone', listener);
67
+ return () => document.fonts.removeEventListener('loadingdone', listener);
68
+ }
69
+
70
+ /** One cell's box in CSS px, as the canvas renderer needs it. */
71
+ export interface TerminalCellMetrics {
72
+ width: number;
73
+ height: number;
74
+ baseline: number;
75
+ }
76
+
77
+ /**
78
+ * Block padding above the ink's ascent and below its descent, in CSS px.
79
+ *
80
+ * Measured against the glyph ink, not the em box: the rectangle a person sees
81
+ * around a coloured run is compared to the letters, so these are the numbers
82
+ * chosen on the real panel — 2.5 over 1, which puts the letters high in the
83
+ * rectangle; the 1 px below is still enough for a `g`/`y`/`p` descender (2.54)
84
+ * because the padding is measured from the *ink*, not the baseline.
85
+ *
86
+ * Fractional on purpose: the cell height is `ceil` of the ink plus both paddings
87
+ * (15 px at 12 px Fira Code), and the baseline is `round(ascent + top)`, which
88
+ * is what makes a 2.5 request land as 3 px of block above a cap that sits 2.74
89
+ * px below it.
90
+ */
91
+ const CELL_PADDING_TOP_PX = 2.5;
92
+ const CELL_PADDING_BOTTOM_PX = 1;
93
+
94
+ /**
95
+ * Measure the cell box from the font's real ink, not one glyph's.
96
+ *
97
+ * ghostty-web's own `measureFont` measures `M` alone. `M` has no descender, so
98
+ * it substitutes `0.2em`, and the box comes out **13 px** at 12 px Fira Code
99
+ * (ink ascent 8.3, baseline 10, cell ends at 13). A `g`/`y`/`p` descender then
100
+ * ends on the row's last pixel, and since every row is repainted with the theme
101
+ * background before its cells (`renderLine`), the next row's fill clips whatever
102
+ * hangs below — measured: a `\e[41m` block around `ggyypp` had 0 px of block
103
+ * under the descenders, and every coloured block read as cropped.
104
+ *
105
+ * Measuring `Mg` supplies a real descent, and the box is the ink plus the two
106
+ * paddings above: at 12 px Fira Code (ink 8.26 / 2.54) that is `height = 15`
107
+ * and `baseline = 11`, i.e. 2.74 px of block above the cap and 1.46 px below
108
+ * the descender. The box is deliberately not the font's line box (line box plus
109
+ * padding came out 20 px, which read as too loose on the real panel).
110
+ */
111
+ export function measureTerminalCell(fontSize: number, fontFamily: string): TerminalCellMetrics {
112
+ // The fallback is the CSS default ratio, used when there is no canvas to
113
+ // measure against (a unit test, a non-DOM import).
114
+ const fallbackAscent = fontSize * 0.8;
115
+ const fallbackDescent = fontSize * 0.2;
116
+ let width = Math.ceil(fontSize * 0.6);
117
+ let ascent = fallbackAscent;
118
+ let descent = fallbackDescent;
119
+ const context =
120
+ typeof document === 'undefined' ? null : document.createElement('canvas').getContext('2d');
121
+ if (context) {
122
+ context.font = `${fontSize}px ${fontFamily}`;
123
+ width = Math.ceil(context.measureText('M').width) || width;
124
+ const ink = context.measureText('Mg');
125
+ ascent = ink.actualBoundingBoxAscent || fallbackAscent;
126
+ descent = ink.actualBoundingBoxDescent || fallbackDescent;
127
+ }
128
+ return {
129
+ width,
130
+ height: Math.ceil(ascent + descent + CELL_PADDING_TOP_PX + CELL_PADDING_BOTTOM_PX),
131
+ baseline: Math.round(ascent + CELL_PADDING_TOP_PX),
132
+ };
133
+ }
134
+
135
+ /** The private end of `CanvasRenderer` this module corrects. */
136
+ interface MetricsRenderer {
137
+ measureFont: () => TerminalCellMetrics;
138
+ remeasureFont: () => void;
139
+ }
140
+
141
+ /**
142
+ * Give the renderer a cell box that holds a descender.
143
+ *
144
+ * `CanvasRenderer.measureFont` and the field it writes are private, and the
145
+ * cell height it derives from `M` leaves a descender on the row's last pixel
146
+ * (see `measureTerminalCell`). Replacing the instance method and re-measuring
147
+ * is the only way to correct it without patching the dependency:
148
+ * `remeasureFont()` calls `this.measureFont()`, and every consumer — fit,
149
+ * `resize`, `render`, the mouse-report cell size — reads `getMetrics()`.
150
+ *
151
+ * The override reads `term.options`, so the font-size round-trip in
152
+ * `watchTerminalFonts` re-measures with this rule instead of restoring the
153
+ * vendor's 13 px box.
154
+ */
155
+ export function installCellMetrics(term: Terminal): void {
156
+ const renderer = term.renderer as unknown as MetricsRenderer | undefined;
157
+ if (!renderer) return;
158
+ renderer.measureFont = () =>
159
+ measureTerminalCell(
160
+ term.options.fontSize ?? TERMINAL_FONT_SIZE,
161
+ term.options.fontFamily ?? TERMINAL_FONT_FAMILY,
162
+ );
163
+ renderer.remeasureFont();
164
+ }