@kabmo-labs/kabmo 0.11.1 → 0.12.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 (324) hide show
  1. package/README.md +67 -421
  2. package/dist/client/index.html +1 -1
  3. package/dist/client/index.js +426 -413
  4. package/dist/client/static/js/{index-amss8pge.js → index-0smc9gm2.js} +1123 -747
  5. package/dist/client/static/js/index-shexbk5f.css +1 -0
  6. package/marketplace/marketplace.json +13 -3
  7. package/marketplace/plugins/command-runner/package.json +4 -2
  8. package/marketplace/plugins/git-forge/README.md +219 -0
  9. package/marketplace/plugins/git-forge/icon.svg +20 -0
  10. package/marketplace/plugins/git-forge/package.json +56 -0
  11. package/marketplace/plugins/git-forge/src/app.tsx +82 -0
  12. package/marketplace/plugins/git-forge/src/command.test.ts +181 -0
  13. package/marketplace/plugins/git-forge/src/command.ts +119 -0
  14. package/marketplace/plugins/git-forge/src/header.tsx +87 -0
  15. package/marketplace/plugins/git-forge/src/panel/Badge.tsx +76 -0
  16. package/marketplace/plugins/git-forge/src/panel/Change.tsx +150 -0
  17. package/marketplace/plugins/git-forge/src/panel/CiBadge.tsx +51 -0
  18. package/marketplace/plugins/git-forge/src/panel/Credential.tsx +107 -0
  19. package/marketplace/plugins/git-forge/src/panel/Glyph.tsx +70 -0
  20. package/marketplace/plugins/git-forge/src/panel/Header.tsx +255 -0
  21. package/marketplace/plugins/git-forge/src/panel/ItemView.tsx +198 -0
  22. package/marketplace/plugins/git-forge/src/panel/List.tsx +89 -0
  23. package/marketplace/plugins/git-forge/src/panel/Row.tsx +68 -0
  24. package/marketplace/plugins/git-forge/src/panel/Sidebar.tsx +94 -0
  25. package/marketplace/plugins/git-forge/src/panel/Write.tsx +236 -0
  26. package/marketplace/plugins/git-forge/src/panel/format.ts +78 -0
  27. package/marketplace/plugins/git-forge/src/panel/index.test.tsx +344 -0
  28. package/marketplace/plugins/git-forge/src/panel/index.tsx +193 -0
  29. package/marketplace/plugins/git-forge/src/panel/use-forge.ts +250 -0
  30. package/marketplace/plugins/git-forge/src/panel/write.test.tsx +269 -0
  31. package/marketplace/plugins/git-forge/src/protocol.ts +308 -0
  32. package/marketplace/plugins/git-forge/src/server/adapters.test.ts +274 -0
  33. package/marketplace/plugins/git-forge/src/server/bitbucket.ts +307 -0
  34. package/marketplace/plugins/git-forge/src/server/capabilities.test.ts +116 -0
  35. package/marketplace/plugins/git-forge/src/server/capabilities.ts +105 -0
  36. package/marketplace/plugins/git-forge/src/server/ci.test.ts +88 -0
  37. package/marketplace/plugins/git-forge/src/server/ci.ts +118 -0
  38. package/marketplace/plugins/git-forge/src/server/context.ts +76 -0
  39. package/marketplace/plugins/git-forge/src/server/diff.test.ts +127 -0
  40. package/marketplace/plugins/git-forge/src/server/diff.ts +120 -0
  41. package/marketplace/plugins/git-forge/src/server/filters.test.ts +138 -0
  42. package/marketplace/plugins/git-forge/src/server/filters.ts +138 -0
  43. package/marketplace/plugins/git-forge/src/server/forges.test.ts +235 -0
  44. package/marketplace/plugins/git-forge/src/server/gitea.ts +296 -0
  45. package/marketplace/plugins/git-forge/src/server/github-extras.ts +92 -0
  46. package/marketplace/plugins/git-forge/src/server/github.ts +320 -0
  47. package/marketplace/plugins/git-forge/src/server/gitlab-detail.ts +207 -0
  48. package/marketplace/plugins/git-forge/src/server/gitlab.test.ts +195 -0
  49. package/marketplace/plugins/git-forge/src/server/gitlab.ts +176 -0
  50. package/marketplace/plugins/git-forge/src/server/http.test.ts +270 -0
  51. package/marketplace/plugins/git-forge/src/server/http.ts +329 -0
  52. package/marketplace/plugins/git-forge/src/server/map.ts +120 -0
  53. package/marketplace/plugins/git-forge/src/server/registry.ts +47 -0
  54. package/marketplace/plugins/git-forge/src/server/remote.test.ts +146 -0
  55. package/marketplace/plugins/git-forge/src/server/remote.ts +188 -0
  56. package/marketplace/plugins/git-forge/src/server/scope-probe.test.ts +98 -0
  57. package/marketplace/plugins/git-forge/src/server/scope-probe.ts +128 -0
  58. package/marketplace/plugins/git-forge/src/server/scope.ts +66 -0
  59. package/marketplace/plugins/git-forge/src/server/test-context.ts +67 -0
  60. package/marketplace/plugins/git-forge/src/server/test-host.ts +51 -0
  61. package/marketplace/plugins/git-forge/src/server/token.test.ts +107 -0
  62. package/marketplace/plugins/git-forge/src/server/token.ts +139 -0
  63. package/marketplace/plugins/git-forge/src/server/write-http.test.ts +127 -0
  64. package/marketplace/plugins/git-forge/src/server/write-http.ts +110 -0
  65. package/marketplace/plugins/git-forge/src/server/writes.test.ts +218 -0
  66. package/marketplace/plugins/git-forge/src/server/writes.ts +313 -0
  67. package/marketplace/plugins/git-forge/src/server.ts +284 -0
  68. package/marketplace/plugins/git-forge/tsconfig.json +16 -0
  69. package/marketplace/plugins/session-info/package.json +4 -2
  70. package/marketplace/plugins/stats/package.json +4 -2
  71. package/package.json +6 -1
  72. package/src/client/components/common/code-surface/blame-hover.ts +188 -0
  73. package/src/client/components/common/code-surface/blame-panel.test.ts +304 -0
  74. package/src/client/components/common/code-surface/blame-panel.ts +306 -0
  75. package/src/client/components/common/code-surface/blame.test.ts +303 -0
  76. package/src/client/components/common/code-surface/blame.ts +227 -0
  77. package/src/client/components/common/code-surface/conflict-markers.test.ts +135 -0
  78. package/src/client/components/common/code-surface/conflict-markers.ts +144 -0
  79. package/src/client/components/common/code-surface/documents.ts +48 -1
  80. package/src/client/components/common/code-surface/extensions.test.ts +4 -0
  81. package/src/client/components/common/code-surface/extensions.ts +44 -0
  82. package/src/client/components/common/code-surface/fold-stack.test.ts +4 -0
  83. package/src/client/components/common/code-surface/indent.test.ts +5 -0
  84. package/src/client/components/common/code-surface/index.tsx +16 -2
  85. package/src/client/components/common/code-surface/lint-parsers.test.ts +109 -0
  86. package/src/client/components/common/code-surface/lint-parsers.ts +134 -0
  87. package/src/client/components/common/code-surface/lint.test.ts +101 -0
  88. package/src/client/components/common/code-surface/lint.ts +85 -0
  89. package/src/client/components/common/code-surface/props.ts +17 -0
  90. package/src/client/components/common/code-surface/surface-failures.test.ts +132 -0
  91. package/src/client/components/common/code-surface/theme.ts +71 -0
  92. package/src/client/components/common/header-controls/index.test.tsx +1 -1
  93. package/src/client/components/common/header-controls/plugin-entry.test.tsx +1 -1
  94. package/src/client/components/common/header-controls/shell-chrome.test.tsx +1 -1
  95. package/src/client/components/common/provider-icon/Plate.tsx +88 -0
  96. package/src/client/components/common/provider-icon/index.tsx +55 -11
  97. package/src/client/components/layout/session-sidebar/SessionsView.tsx +5 -2
  98. package/src/client/components/mobile/SessionSidebar.tsx +2 -10
  99. package/src/client/components/mobile/mobile-right-sidebar/FullEditor.tsx +6 -0
  100. package/src/client/components/mobile/mobile-right-sidebar/StatusBar.tsx +8 -0
  101. package/src/client/components/settings/categories/AppearanceSettings.tsx +3 -0
  102. package/src/client/components/settings/categories/ProviderSettings.tsx +43 -41
  103. package/src/client/components/settings/categories/appearance-settings/EditorBlameSection.tsx +97 -0
  104. package/src/client/components/settings/categories/panel-plugins/AvailableList.tsx +106 -54
  105. package/src/client/components/settings/categories/panel-plugins/GitFreshness.tsx +14 -1
  106. package/src/client/components/settings/categories/panel-plugins/InstalledList.tsx +39 -150
  107. package/src/client/components/settings/categories/panel-plugins/Meta.test.tsx +143 -0
  108. package/src/client/components/settings/categories/panel-plugins/Meta.tsx +157 -0
  109. package/src/client/components/settings/categories/panel-plugins/Placement.test.tsx +149 -0
  110. package/src/client/components/settings/categories/panel-plugins/Placement.tsx +99 -0
  111. package/src/client/components/settings/categories/panel-plugins/PluginCard.tsx +270 -0
  112. package/src/client/components/settings/categories/panel-plugins/Section.tsx +11 -3
  113. package/src/client/components/settings/categories/provider-settings/AddModelModal.tsx +25 -0
  114. package/src/client/components/settings/categories/provider-settings/AuthSection.tsx +135 -80
  115. package/src/client/components/settings/categories/provider-settings/DeleteProviderModal.tsx +51 -21
  116. package/src/client/components/settings/categories/provider-settings/EntryAdvanced.tsx +175 -0
  117. package/src/client/components/settings/categories/provider-settings/EntryModal.tsx +275 -0
  118. package/src/client/components/settings/categories/provider-settings/Header.tsx +40 -9
  119. package/src/client/components/settings/categories/provider-settings/LoginModal.tsx +142 -160
  120. package/src/client/components/settings/categories/provider-settings/LogoutProviderModal.tsx +2 -2
  121. package/src/client/components/settings/categories/provider-settings/ModelCapabilitiesModal.tsx +84 -46
  122. package/src/client/components/settings/categories/provider-settings/ModelsList.tsx +217 -102
  123. package/src/client/components/settings/categories/provider-settings/PresetPicker.tsx +5 -2
  124. package/src/client/components/settings/categories/provider-settings/SidebarList.plugin.test.ts +3 -3
  125. package/src/client/components/settings/categories/provider-settings/SidebarList.tsx +158 -102
  126. package/src/client/components/settings/categories/provider-settings/auth-section.test.tsx +135 -0
  127. package/src/client/components/settings/categories/provider-settings/groups.test.ts +98 -0
  128. package/src/client/components/settings/categories/provider-settings/groups.ts +108 -0
  129. package/src/client/components/settings/categories/provider-settings/layout.test.tsx +325 -0
  130. package/src/client/components/settings/categories/provider-settings/model-dialogs.test.tsx +135 -0
  131. package/src/client/components/settings/categories/provider-settings/preset-picker.test.tsx +73 -0
  132. package/src/client/components/settings/categories/usage-settings/ProviderCard.tsx +22 -2
  133. package/src/client/components/settings/categories/usage-settings/ProviderDetail.tsx +2 -0
  134. package/src/client/components/settings/categories/usage-settings/SidebarList.tsx +16 -6
  135. package/src/client/components/settings/categories/usage-settings/providers.ts +3 -0
  136. package/src/client/components/settings/categories/usage-settings/usage-and-picker.test.ts +19 -0
  137. package/src/client/components/settings/categories/usage-settings/usage-icons.test.tsx +143 -0
  138. package/src/client/components/workspace/chat-timeline/MinimapShortcuts.test.ts +2 -1
  139. package/src/client/components/workspace/chat-timeline/chat-input/GoalBanner.test.ts +23 -19
  140. package/src/client/components/workspace/chat-timeline/chat-input/ModeToggles.test.ts +23 -19
  141. package/src/client/components/workspace/chat-timeline/chat-input/Toolbar.test.ts +20 -18
  142. package/src/client/components/workspace/chat-timeline/chat-input/compact.test.tsx +19 -6
  143. package/src/client/components/workspace/chat-timeline/index.tsx +7 -16
  144. package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Wait.missing.test.ts +100 -0
  145. package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Wait.test.ts +3 -47
  146. package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Wait.tsx +8 -1
  147. package/src/client/components/workspace/chat-timeline/tool-renderers/panels/wait-test-util.ts +62 -0
  148. package/src/client/components/workspace/diff-panel/HunkStepper.tsx +62 -0
  149. package/src/client/components/workspace/diff-panel/MergeDiffView.tsx +29 -0
  150. package/src/client/components/workspace/diff-panel/Toolbar.tsx +32 -1
  151. package/src/client/components/workspace/diff-panel/changes.ts +69 -0
  152. package/src/client/components/workspace/diff-panel/chunks.ts +85 -0
  153. package/src/client/components/workspace/diff-panel/index.tsx +21 -1
  154. package/src/client/components/workspace/diff-panel/merge.test.ts +145 -0
  155. package/src/client/components/workspace/diff-panel/merge.ts +63 -68
  156. package/src/client/components/workspace/editor/EditorDropOverlay.tsx +6 -1
  157. package/src/client/components/workspace/editor/FileDocument.tsx +51 -1
  158. package/src/client/components/workspace/editor/FilePane.tsx +25 -1
  159. package/src/client/components/workspace/editor/StatusBar.tsx +19 -0
  160. package/src/client/components/workspace/editor/Toolbar.tsx +29 -5
  161. package/src/client/components/workspace/editor/toolbar-clusters.test.ts +86 -0
  162. package/src/client/components/workspace/editor/toolbar-clusters.tsx +48 -16
  163. package/src/client/components/workspace/editor/viewers/bytes.ts +16 -0
  164. package/src/client/components/workspace/git-panel/index.test.ts +46 -1
  165. package/src/client/components/workspace/git-panel/index.tsx +11 -38
  166. package/src/client/components/workspace/model-dropdown/Item.plugin.test.ts +66 -0
  167. package/src/client/components/workspace/model-dropdown/Item.tsx +71 -62
  168. package/src/client/components/workspace/model-dropdown/Section.tsx +31 -14
  169. package/src/client/components/workspace/model-dropdown/collapsed.ts +72 -0
  170. package/src/client/components/workspace/model-dropdown/index.tsx +42 -19
  171. package/src/client/components/workspace/model-dropdown/refresh.test.tsx +260 -0
  172. package/src/client/components/workspace/model-dropdown/trigger.test.ts +34 -0
  173. package/src/client/components/workspace/model-dropdown/use-catalog.ts +13 -2
  174. package/src/client/components/workspace/usage-panel/ProviderSelect.tsx +19 -3
  175. package/src/client/hooks/chat/omp/folder-auto-expand.ts +113 -0
  176. package/src/client/hooks/chat/omp/index.ts +20 -0
  177. package/src/client/hooks/chat/omp/pending-rows.ts +21 -0
  178. package/src/client/hooks/chat/omp/pending-sessions.test.ts +16 -1
  179. package/src/client/hooks/chat/omp/session-sidebar-controller.test.ts +102 -0
  180. package/src/client/hooks/chat/omp/session-sidebar-controller.ts +21 -1
  181. package/src/client/hooks/chat/omp/stream.ts +8 -1
  182. package/src/client/hooks/chat/timeline/index.ts +1 -1
  183. package/src/client/hooks/chat/timeline/new-chat-open.ts +60 -0
  184. package/src/client/hooks/editor/blame-settings.ts +67 -0
  185. package/src/client/hooks/editor/blame.test.ts +272 -0
  186. package/src/client/hooks/editor/blame.ts +232 -0
  187. package/src/client/hooks/editor/file-editor-types.ts +23 -0
  188. package/src/client/hooks/editor/save-file.test.ts +20 -0
  189. package/src/client/hooks/editor/use-editor-drop.test-util.ts +198 -0
  190. package/src/client/hooks/editor/use-editor-drop.test.ts +70 -175
  191. package/src/client/hooks/editor/use-editor-drop.ts +19 -36
  192. package/src/client/hooks/editor/use-file-editor.ts +7 -1
  193. package/src/client/hooks/settings/providers/actions.ts +90 -194
  194. package/src/client/hooks/settings/providers/api.ts +7 -1
  195. package/src/client/hooks/settings/providers/index.ts +171 -59
  196. package/src/client/hooks/settings/providers/login.test.ts +79 -0
  197. package/src/client/hooks/settings/providers/login.ts +276 -0
  198. package/src/client/hooks/settings/providers/messages.ts +28 -0
  199. package/src/client/hooks/settings/providers/model-actions.ts +178 -0
  200. package/src/client/hooks/ui/editor-error-toast.ts +47 -0
  201. package/src/client/hooks/ui/open-file.ts +7 -0
  202. package/src/client/hooks/ui/toasts.ts +7 -0
  203. package/src/client/hooks/workspace/file-tabs.ts +4 -1
  204. package/src/client/hooks/workspace/folder-expansion.test.ts +62 -0
  205. package/src/client/hooks/workspace/folder-expansion.ts +33 -0
  206. package/src/client/hooks/workspace/git-action-outcome.ts +86 -0
  207. package/src/client/hooks/workspace/git-actions.ts +29 -2
  208. package/src/client/hooks/workspace/plugin-contributions.test.ts +117 -0
  209. package/src/client/hooks/workspace/plugin-contributions.ts +146 -0
  210. package/src/client/lib/editor-errors.test.ts +85 -0
  211. package/src/client/lib/editor-errors.ts +71 -0
  212. package/src/client/lib/fs/resolve-open.test.ts +125 -0
  213. package/src/client/lib/fs/resolve-open.ts +90 -0
  214. package/src/client/lib/git/action-message.test.ts +82 -0
  215. package/src/client/lib/git/action-message.ts +160 -0
  216. package/src/client/lib/launch/device-files.test.ts +160 -0
  217. package/src/client/lib/launch/device-files.ts +197 -0
  218. package/src/client/lib/native/open-paths.test.ts +140 -0
  219. package/src/client/lib/native/open-paths.ts +157 -0
  220. package/src/client/lib/plugins/bootstrap.test.ts +1 -1
  221. package/src/client/main.tsx +20 -0
  222. package/src/server/db.server.test.ts +39 -0
  223. package/src/server/db.server.ts +21 -2
  224. package/src/server/lib/db/schema.ts +2 -0
  225. package/src/server/lib/fs/external-roots.test.ts +100 -0
  226. package/src/server/lib/fs/external-roots.ts +101 -0
  227. package/src/server/lib/fs/git-actions-branch.ts +15 -2
  228. package/src/server/lib/fs/git-actions-shared.ts +24 -0
  229. package/src/server/lib/fs/git-actions-sync.test.ts +18 -7
  230. package/src/server/lib/fs/git-actions-worktree.ts +5 -1
  231. package/src/server/lib/fs/git-blame.test.ts +206 -0
  232. package/src/server/lib/fs/git-blame.ts +189 -0
  233. package/src/server/lib/fs/root.ts +14 -0
  234. package/src/server/lib/http/same-origin.ts +28 -7
  235. package/src/server/lib/models/catalog.server.ts +230 -0
  236. package/src/server/lib/models/catalog.test.ts +83 -0
  237. package/src/server/lib/models/credential-origin.server.ts +108 -0
  238. package/src/server/lib/models/credential-origin.test.ts +132 -0
  239. package/src/server/lib/models/provider-icon-fetch.ts +115 -0
  240. package/src/server/lib/models/provider-icon-parse.test.ts +134 -0
  241. package/src/server/lib/models/provider-icon-parse.ts +139 -0
  242. package/src/server/lib/models/provider-icon-store.server.ts +118 -0
  243. package/src/server/lib/models/provider-icon-store.test.ts +107 -0
  244. package/src/server/lib/models/provider-icon.server.ts +209 -0
  245. package/src/server/lib/models/provider-items.server.ts +211 -37
  246. package/src/server/lib/models/provider-items.test.ts +201 -0
  247. package/src/server/lib/models/provider-registry.server.test.ts +27 -9
  248. package/src/server/lib/models/provider-registry.server.ts +160 -67
  249. package/src/server/lib/models/registry.server.test.ts +3 -3
  250. package/src/server/lib/models/registry.server.ts +8 -0
  251. package/src/server/lib/omp/config/definitions.test.ts +5 -4
  252. package/src/server/lib/omp/config/models-config.ts +42 -0
  253. package/src/server/lib/omp/config/provider-entry.test.ts +205 -0
  254. package/src/server/lib/omp/config/provider-entry.ts +197 -0
  255. package/src/server/lib/omp/config/provider-seeds.ts +10 -0
  256. package/src/server/lib/omp/core/models-json.ts +69 -0
  257. package/src/server/lib/omp/session/messages.ts +17 -3
  258. package/src/server/lib/omp/session/sidebar-data.canonical.test.ts +13 -3
  259. package/src/server/lib/panels/bundled-catalog.server.ts +7 -0
  260. package/src/server/lib/panels/catalog-offers.server.ts +68 -0
  261. package/src/server/lib/panels/git-update.server.test.ts +14 -0
  262. package/src/server/lib/panels/git-update.server.ts +63 -1
  263. package/src/server/lib/panels/manifest.test.ts +65 -0
  264. package/src/server/lib/panels/manifest.ts +55 -1
  265. package/src/server/lib/panels/offers.server.test.ts +191 -0
  266. package/src/server/lib/panels/offers.server.ts +108 -0
  267. package/src/server/lib/panels/registry.server.test.ts +35 -1
  268. package/src/server/lib/panels/registry.server.ts +51 -142
  269. package/src/server/lib/panels/scan.server.ts +162 -0
  270. package/src/server/lib/route-adapter.ts +13 -2
  271. package/src/server/lib/tray/tray.test.ts +2 -1
  272. package/src/server/lib/usage/providers.server.ts +4 -1
  273. package/src/server/routes/chat/session.ts +7 -1
  274. package/src/server/routes/fs/git-blame-loader.test.ts +146 -0
  275. package/src/server/routes/fs/git.ts +17 -0
  276. package/src/server/routes/fs/grant.test.ts +135 -0
  277. package/src/server/routes/fs/grant.ts +79 -0
  278. package/src/server/routes/fs/index.ts +2 -0
  279. package/src/server/routes/fs/scope.ts +34 -17
  280. package/src/server/routes/omp/login.ts +41 -14
  281. package/src/server/routes/omp/routes.test.ts +23 -10
  282. package/src/server/routes/settings/index.ts +8 -0
  283. package/src/server/routes/settings/provider-entry.ts +108 -0
  284. package/src/server/routes/settings/provider-icon.ts +99 -0
  285. package/src/server/routes/settings/provider-model.ts +4 -1
  286. package/src/server/routes/settings/provider-refresh.ts +87 -0
  287. package/src/server/routes/settings/providers.ts +31 -2
  288. package/src/shared/lib/chat/omp/fold.test.ts +34 -0
  289. package/src/shared/lib/chat/omp/tool-frames.test.ts +20 -0
  290. package/src/shared/lib/chat/omp/tool-frames.ts +5 -1
  291. package/src/shared/lib/chat/omp/tool-results.ts +29 -3
  292. package/src/shared/lib/chat/tool/wait-facts.ts +4 -0
  293. package/src/shared/lib/fs/git-blame.test.ts +266 -0
  294. package/src/shared/lib/fs/git-blame.ts +246 -0
  295. package/src/shared/lib/git/url.ts +18 -0
  296. package/src/shared/lib/markdown/sanitize.test.ts +57 -0
  297. package/src/shared/lib/markdown/sanitize.ts +30 -7
  298. package/src/shared/lib/models/provider/connection.ts +135 -0
  299. package/src/shared/lib/models/provider/dialect.test.ts +4 -2
  300. package/src/shared/lib/models/provider/dialect.ts +76 -6
  301. package/src/shared/lib/models/provider/models.ts +2 -0
  302. package/src/shared/lib/models/provider/origin.ts +94 -0
  303. package/src/shared/lib/models/provider/provider-ui.test.ts +7 -0
  304. package/src/shared/lib/models/server-cache.ts +8 -3
  305. package/src/shared/lib/omp/session/messages-map-pending.test.ts +110 -0
  306. package/src/shared/lib/omp/session/messages-map.ts +34 -2
  307. package/src/shared/lib/omp/session/prewarm-misc.test.ts +7 -90
  308. package/src/shared/lib/omp/session/prewarm.test.ts +133 -0
  309. package/src/shared/lib/omp/session/prewarm.ts +23 -1
  310. package/src/shared/lib/omp/session/wait.test.ts +28 -0
  311. package/src/shared/lib/omp/session/wait.ts +38 -3
  312. package/src/shared/lib/panels/slot-labels.test.ts +61 -0
  313. package/src/shared/lib/panels/slot-labels.ts +133 -0
  314. package/src/shared/lib/workspace/panel-ids.ts +11 -0
  315. package/src/shared/types/fs.ts +6 -0
  316. package/src/shared/types/index.ts +1 -0
  317. package/src/shared/types/models.ts +12 -0
  318. package/src/shared/types/panels-registry.ts +277 -0
  319. package/src/shared/types/panels.ts +46 -213
  320. package/src/shared/types/settings/provider.ts +58 -0
  321. package/src/shared/types/settings/usage.ts +10 -0
  322. package/src/test-support/pristine-globals.ts +7 -0
  323. package/dist/client/static/js/index-bq6qeayr.css +0 -1
  324. package/src/client/components/settings/categories/provider-settings/ReconnectModal.tsx +0 -184
@@ -0,0 +1,140 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ /**
7
+ * The page's half of an OS file launch.
8
+ *
9
+ * Three shells hand paths over (`__kabmoOpenPaths`), and the rules that fail
10
+ * silently are all about TIMING: a launch that beats hydration must still open
11
+ * its tab, and a shell that delivers the same batch twice must open one tab per
12
+ * file rather than two.
13
+ */
14
+
15
+ import { afterAll, beforeAll, beforeEach, describe, expect, test } from 'bun:test';
16
+ import { Window } from 'happy-dom';
17
+ import {
18
+ OPEN_PATHS_PROPERTY,
19
+ deliverOpenPaths,
20
+ drainPendingOpenPaths,
21
+ installOpenPaths,
22
+ onOpenPaths,
23
+ openPathsNow,
24
+ } from '@/client/lib/native/open-paths';
25
+
26
+ const DOM_GLOBALS = ['window', 'document', 'navigator', 'CustomEvent'] as const;
27
+ const nativeGlobals: Partial<Record<(typeof DOM_GLOBALS)[number], unknown>> = {};
28
+
29
+ let responses: { status: number; body: unknown }[] = [];
30
+ let opened: unknown[] = [];
31
+
32
+ beforeAll(() => {
33
+ const win = new Window({ url: 'http://localhost' });
34
+ const target = globalThis as unknown as Record<string, unknown>;
35
+ for (const key of DOM_GLOBALS) {
36
+ nativeGlobals[key] = target[key];
37
+ target[key] = (win as unknown as Record<string, unknown>)[key];
38
+ }
39
+ globalThis.fetch = (async () => {
40
+ const next = responses.shift() ?? { status: 500, body: {} };
41
+ return { ok: next.status >= 200 && next.status < 300, status: next.status, json: async () => next.body };
42
+ }) as unknown as typeof globalThis.fetch;
43
+ // The tab-opening half is a window event; recording it here proves a file was
44
+ // handed to the editor rather than merely resolved.
45
+ win.addEventListener('omp:open-file', (event) => {
46
+ opened.push((event as unknown as CustomEvent).detail);
47
+ });
48
+ });
49
+
50
+ afterAll(() => {
51
+ const target = globalThis as unknown as Record<string, unknown>;
52
+ for (const key of DOM_GLOBALS) {
53
+ if (nativeGlobals[key] === undefined) delete target[key];
54
+ else target[key] = nativeGlobals[key];
55
+ }
56
+ });
57
+
58
+ beforeEach(() => {
59
+ responses = [];
60
+ opened = [];
61
+ });
62
+
63
+ describe('openPathsNow', () => {
64
+ test('a resolved file opens a tab with its scope', async () => {
65
+ responses = [{ status: 200, body: { root: '/w', path: 'a/b.md', name: 'b.md', kind: 'text' } }];
66
+ const outcome = await openPathsNow(['/w/a/b.md']);
67
+ expect(outcome).toEqual({ opened: ['/w/a/b.md'], refused: [] });
68
+ expect(opened).toHaveLength(1);
69
+ expect(opened[0]).toMatchObject({ path: 'a/b.md', name: 'b.md', root: '/w' });
70
+ });
71
+
72
+ test('a refusal is reported and opens nothing', async () => {
73
+ responses = [{ status: 404, body: { error: 'File not found' } }];
74
+ const outcome = await openPathsNow(['/w/gone.md']);
75
+ expect(outcome.opened).toEqual([]);
76
+ expect(outcome.refused).toEqual([{ path: '/w/gone.md', reason: 'File not found' }]);
77
+ expect(opened).toHaveLength(0);
78
+ });
79
+
80
+ test('a batch keeps its order and skips blanks', async () => {
81
+ responses = [
82
+ { status: 200, body: { root: '/w', path: 'a.md', name: 'a.md' } },
83
+ { status: 200, body: { root: '/w', path: 'b.md', name: 'b.md' } },
84
+ ];
85
+ const outcome = await openPathsNow([' ', '/w/a.md', '/w/b.md']);
86
+ expect(outcome.opened).toEqual(['/w/a.md', '/w/b.md']);
87
+ });
88
+ });
89
+
90
+ describe('delivery', () => {
91
+ test('a path that arrives before a listener waits, and is drained once', () => {
92
+ deliverOpenPaths(['/w/early.md']);
93
+ const drained = drainPendingOpenPaths();
94
+ expect(drained).toEqual(['/w/early.md']);
95
+ // The queue is empty afterwards: a second drain must not reopen the file on
96
+ // the next navigation.
97
+ expect(drainPendingOpenPaths()).toEqual([]);
98
+ });
99
+
100
+ test('a duplicate arrival is held once', () => {
101
+ deliverOpenPaths(['/w/x.md']);
102
+ deliverOpenPaths(['/w/x.md']);
103
+ expect(drainPendingOpenPaths()).toEqual(['/w/x.md']);
104
+ });
105
+
106
+ test('a listener takes what was already waiting', async () => {
107
+ deliverOpenPaths(['/w/queued.md']);
108
+ responses = [{ status: 200, body: { root: '/w', path: 'queued.md', name: 'queued.md' } }];
109
+ const seen: string[][] = [];
110
+ const off = onOpenPaths(async (paths) => {
111
+ seen.push(paths);
112
+ return { opened: paths, refused: [] };
113
+ });
114
+ // The handler runs on registration, so a launch that beat the listener is
115
+ // not lost.
116
+ await Promise.resolve();
117
+ expect(seen).toEqual([['/w/queued.md']]);
118
+ off();
119
+ });
120
+
121
+ test('the global the shell calls is installed, and refuses a non-array', () => {
122
+ installOpenPaths({ onResult: () => {} });
123
+ const call = (globalThis.window as unknown as Record<string, unknown>)[OPEN_PATHS_PROPERTY];
124
+ expect(typeof call).toBe('function');
125
+ // A shell too old to send an array must not throw inside the page.
126
+ (call as (value: unknown) => void)('not an array');
127
+ });
128
+
129
+ test('the installed global routes a batch through the tab opener', async () => {
130
+ // The handler the install registers is what a shell's call reaches, so
131
+ // awaiting IT is the condition — no clock involved.
132
+ const done = Promise.withResolvers<unknown>();
133
+ installOpenPaths({ onResult: () => done.resolve(undefined) });
134
+ responses = [{ status: 200, body: { root: '/w', path: 'from-shell.md', name: 'from-shell.md' } }];
135
+ const call = (globalThis.window as unknown as Record<string, unknown>)[OPEN_PATHS_PROPERTY] as (value: unknown) => void;
136
+ call(['/w/from-shell.md']);
137
+ await done.promise;
138
+ expect(opened.some((entry) => (entry as { name?: string }).name === 'from-shell.md')).toBe(true);
139
+ });
140
+ });
@@ -0,0 +1,157 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ /**
7
+ * Files the operating system handed to Kabmo.
8
+ *
9
+ * A double-click in Finder, "Open With → Kabmo" on a `.docx`, a drop of three
10
+ * PDFs on the Dock icon: all of them arrive as PATHS on the app's argv or in an
11
+ * `application:openFiles:` callback, and the only layer that sees them is the
12
+ * native shell. The page owns the tabs, so the paths cross to it as values.
13
+ *
14
+ * Two channels, because the two moments are different:
15
+ *
16
+ * - **A cold start** — the shell launches BECAUSE of the file, so the paths
17
+ * exist before the page does. They are injected as `window.__kabmoLaunchPaths`
18
+ * before the page's first script (the same way `__kabmoDesktop` is), and read
19
+ * once at boot.
20
+ * - **A warm app** — the window is already on a console and the user opens
21
+ * another file. The shell calls `window.__kabmoOpenPaths([...])`.
22
+ *
23
+ * Both end in the same place: `consumeOpenPaths`, which asks the server to
24
+ * grant the file's directory and then opens a tab. The grant is what makes this
25
+ * work at all — a file in `~/Desktop` is outside every workspace, and the fs
26
+ * routes refuse a root they do not recognise. `POST /api/fs/grant` is that
27
+ * permission, and it is deliberately reached only from a gesture the OS
28
+ * delivered.
29
+ *
30
+ * **A shell with no console yet buffers.** The window may be sitting on the
31
+ * server picker, or on a console that has not finished loading; opening a tab
32
+ * before a session exists has nowhere to put it. The listener is installed at
33
+ * boot and the paths are re-delivered when a console appears, which is why
34
+ * `deliver` is idempotent and why the queue is module-level.
35
+ */
36
+
37
+ import { openFileInEditor } from '@/client/hooks/ui/open-file';
38
+ import { resolveOpenableFile } from '@/client/lib/fs/resolve-open';
39
+
40
+ /** The property a shell calls for a file opened while the app is running. */
41
+ export const OPEN_PATHS_PROPERTY = '__kabmoOpenPaths';
42
+
43
+ /** Where the last batch of paths went, for a diagnostic and for the tests. */
44
+ export interface OpenPathsOutcome {
45
+ opened: string[];
46
+ refused: { path: string; reason: string }[];
47
+ }
48
+
49
+ type PathHandler = (paths: string[]) => Promise<OpenPathsOutcome>;
50
+
51
+ const handlers = new Set<PathHandler>();
52
+ /** The handler `installOpenPaths` registered, so a second call replaces it. */
53
+ let installedHandler: PathHandler | null = null;
54
+ /** Paths that arrived before anything was listening (or before a console). */
55
+ let pending: string[] = [];
56
+
57
+ /**
58
+ * The tab-opening half, kept as one function so the boot install and the tests
59
+ * exercise the same code.
60
+ *
61
+ * It is installed by `main.tsx` rather than by this module at import time: the
62
+ * import graph is what decides when `openFileInEditor` is reachable, and a
63
+ * module-level side effect would make the order an accident.
64
+ */
65
+ export async function openPathsNow(paths: string[]): Promise<OpenPathsOutcome> {
66
+ const outcome: OpenPathsOutcome = { opened: [], refused: [] };
67
+ for (const raw of paths) {
68
+ const path = raw.trim();
69
+ if (!path) continue;
70
+ // The scope resolver owns the one retry a gesture earns: a file outside
71
+ // every workspace is refused once, then granted (`resolve-open.ts`).
72
+ const resolved = await resolveOpenableFile(path);
73
+ if (!resolved.ok) {
74
+ outcome.refused.push({ path, reason: resolved.message });
75
+ continue;
76
+ }
77
+ openFileInEditor({ path: resolved.file.path, name: resolved.file.name, root: resolved.file.root });
78
+ outcome.opened.push(path);
79
+ }
80
+ return outcome;
81
+ }
82
+
83
+ /**
84
+ * Deliver paths to whoever is listening, or hold them until someone is.
85
+ *
86
+ * The shell may call this on a console that is still booting (the launch paths
87
+ * are delivered by the shell as soon as the page's document exists, which can
88
+ * be before hydration). Holding them is what makes the cold start work: the
89
+ * listener registered at boot drains the queue in the same tick it registers.
90
+ */
91
+ export function deliverOpenPaths(paths: string[]): void {
92
+ const cleaned = paths.map((entry) => entry.trim()).filter(Boolean);
93
+ if (cleaned.length === 0) return;
94
+ if (handlers.size === 0) {
95
+ // De-duplicated: a shell that delivers the same launch twice (a reload, a
96
+ // second `open` before the page hydrated) must open one tab per file.
97
+ pending = [...new Set([...pending, ...cleaned])];
98
+ return;
99
+ }
100
+ for (const handler of handlers) void handler(cleaned);
101
+ }
102
+
103
+ /** Drain what arrived before a listener existed. */
104
+ export function drainPendingOpenPaths(): string[] {
105
+ const paths = pending;
106
+ pending = [];
107
+ return paths;
108
+ }
109
+
110
+ /**
111
+ * Register a handler and take whatever was waiting.
112
+ *
113
+ * Returns the unsubscribe, so a hot reload cannot accumulate handlers that open
114
+ * the same file several times.
115
+ */
116
+ export function onOpenPaths(handler: PathHandler): () => void {
117
+ handlers.add(handler);
118
+ const waiting = drainPendingOpenPaths();
119
+ if (waiting.length > 0) void handler(waiting);
120
+ return () => handlers.delete(handler);
121
+ }
122
+
123
+ /**
124
+ * Install the global a shell calls, and register the tab opener.
125
+ *
126
+ * Idempotent, and installed unconditionally: the property is inert in a browser
127
+ * (nothing calls it), and gating on a user agent would be a second place that has
128
+ * to know which shells exist.
129
+ *
130
+ * `onResult` is told about EVERY batch, not only a refused one: a batch of three
131
+ * files can open two and refuse one, and a reporter that only heard about
132
+ * failures could not say what happened. The DEFAULT reporter warns about the
133
+ * refusals and says nothing about the rest, because the console has no toast
134
+ * stack at this level — the tab that opened is its own feedback.
135
+ */
136
+ export function installOpenPaths(
137
+ options: { onResult?: (outcome: OpenPathsOutcome) => void } = {},
138
+ ): void {
139
+ if (typeof window === 'undefined') return;
140
+ const host = window as unknown as Record<string, unknown>;
141
+ host[OPEN_PATHS_PROPERTY] = (paths: unknown) => {
142
+ if (Array.isArray(paths)) deliverOpenPaths(paths.filter((entry): entry is string => typeof entry === 'string'));
143
+ };
144
+
145
+ // Replacing rather than adding: a second install would open every file twice,
146
+ // and the doc above promises idempotence.
147
+ if (installedHandler) handlers.delete(installedHandler);
148
+ installedHandler = async (paths) => {
149
+ const outcome = await openPathsNow(paths);
150
+ const report = options.onResult ?? ((result: OpenPathsOutcome) => {
151
+ for (const refusal of result.refused) console.warn(`[kabmo] ${refusal.path}: ${refusal.reason}`);
152
+ });
153
+ report(outcome);
154
+ return outcome;
155
+ };
156
+ onOpenPaths(installedHandler);
157
+ }
@@ -60,7 +60,7 @@ function payload(enabled: boolean): PanelRegistryPayload {
60
60
  : [],
61
61
  marketplaces: [],
62
62
  errors: [],
63
- plugins: [{ pluginId: 'demo', name: 'Demo', version: '1.0.0', isPackage: true, built: true, enabled, bundled: false, gitOrigin: null }],
63
+ plugins: [{ pluginId: 'demo', name: 'Demo', version: '1.0.0', isPackage: true, built: true, enabled, bundled: false, origin: 'local', gitOrigin: null }],
64
64
  catalog: [],
65
65
  disabledPanels: enabled ? [] : ['plugin:demo'],
66
66
  gitStatus: {},
@@ -13,6 +13,8 @@ import { installBackDispatcher } from '@/client/lib/native/back';
13
13
  import { installPalettePublisher } from '@/client/lib/native/palette';
14
14
  import { installResumeDispatcher } from '@/client/lib/native/resume';
15
15
  import { installFileLaunchHandler } from '@/client/lib/launch/file-handlers';
16
+ import { installOpenPaths } from '@/client/lib/native/open-paths';
17
+ import { installDeviceFiles } from '@/client/lib/launch/device-files';
16
18
 
17
19
  import '@/client/tailwind.css';
18
20
  import '@/shared/lib/markdown/katex-fonts.css';
@@ -86,6 +88,24 @@ installFileLaunchHandler((result) => {
86
88
  if (!result.ok) console.warn(`[kabmo] ${result.message}`);
87
89
  });
88
90
 
91
+ // A file the OS handed to a DESKTOP shell — a double-click in Finder, "Open
92
+ // With → Kabmo", a drop on the Dock icon — arrives as a PATH, which no browser
93
+ // API can see. The shell injects the launch batch before this script and calls
94
+ // `window.__kabmoOpenPaths` for a file opened while the app runs; both end in
95
+ // `POST /api/fs/grant` (the file is usually outside every workspace) and then a
96
+ // tab. Installed before the first render so a launch that beat hydration is
97
+ // drained by the same tick that registers the opener.
98
+ installOpenPaths();
99
+
100
+ // A document the PHONE handed over — picked in the Files app, or "Open with →
101
+ // Kabmo" from Gmail — arrives as BYTES, not a path: the console may be on
102
+ // another machine, so it could never read the file itself. The shell announces
103
+ // each file and streams its base64 (`__kabmoOpenFiles` / `__kabmoOpenChunk`),
104
+ // and the tab is device-backed: it renders and is read-only. Installed before
105
+ // the first render for the same reason as the path receiver above — a launch
106
+ // that beat hydration must still open its tab.
107
+ installDeviceFiles();
108
+
89
109
  // Fire-and-forget: warms the Shiki highlighter in parallel with hydration.
90
110
  // Once the eager grammars land, each is tokenized once on a tiny sample during
91
111
  // idle time — loading a grammar is not compiling it, and the compile used to be
@@ -150,6 +150,45 @@ describe('a failed open is not cached', () => {
150
150
  globalThis.__kabmoDb = savedSlot;
151
151
  }
152
152
  });
153
+
154
+ test('a failure nobody is awaiting yet is not reported as an unhandled rejection', async () => {
155
+ // The shape `closeDb` produces: it retires the slot and reads its promise
156
+ // LATER, so the rejection lands while no caller holds a handler — and the
157
+ // handler that clears the slot runs first, leaving a rejected promise with
158
+ // no owner. Bun reports that as "Unhandled error between tests", attributed
159
+ // to whichever case happens to be running; measured on the Release gate,
160
+ // where one Windows timeout reported a second, unrelated failure.
161
+ const blocker = path.join(roots[0] ?? tmpdir(), `blocker-unhandled-${process.pid}`);
162
+ await Bun.write(blocker, 'not a directory');
163
+ const savedSlot = globalThis.__kabmoDb;
164
+ const unhandled: unknown[] = [];
165
+ const record = (reason: unknown) => unhandled.push(reason);
166
+ process.on('unhandledRejection', record);
167
+ try {
168
+ Bun.env.KABMO_DB_PATH = path.join(blocker, 'db.sqlite');
169
+ globalThis.__kabmoDb = undefined;
170
+
171
+ const pending = getDb();
172
+ // Let the rejection settle with no handler attached. A macrotask, not a
173
+ // duration: Bun decides "unhandled" once the microtask queue drains, and
174
+ // the check has to happen after that decision either way.
175
+ await new Promise<void>((resolve) => setImmediate(resolve));
176
+ // `.then` rather than `expect(...).rejects`: measured, Bun's `rejects`
177
+ // matcher fails on a promise that settled in an EARLIER turn, reporting
178
+ // the rejection reason as the assertion's own error.
179
+ const failure = await pending.then(
180
+ () => null,
181
+ (error: unknown) => error,
182
+ );
183
+ expect(failure).toBeInstanceOf(Error);
184
+ await new Promise<void>((resolve) => setImmediate(resolve));
185
+
186
+ expect(unhandled).toEqual([]);
187
+ } finally {
188
+ process.off('unhandledRejection', record);
189
+ globalThis.__kabmoDb = savedSlot;
190
+ }
191
+ });
153
192
  });
154
193
 
155
194
  describe('closeDb', () => {
@@ -143,12 +143,24 @@ export async function getDatabasePath(): Promise<string> {
143
143
  return path.join(defaultDir, 'db.sqlite');
144
144
  }
145
145
 
146
- export async function getDb(): Promise<DbClient> {
146
+ /**
147
+ * The process-wide handle, opened on first use.
148
+ *
149
+ * **Deliberately NOT `async`.** An `async` wrapper returns a NEW promise that
150
+ * only adopts the slot's, so a handler attached to the slot does not mark the
151
+ * wrapper handled: `closeDb` retires the slot and reads it later, which left
152
+ * the caller's wrapper rejecting with no owner and Bun reporting "Unhandled
153
+ * error between tests" against whichever case happened to be running (measured
154
+ * on the Release gate: one Windows timeout reported a second, unrelated
155
+ * failure). Returning the slot's own promise is also what makes every caller
156
+ * share ONE open — an async wrapper would hand each of them a distinct promise.
157
+ */
158
+ export function getDb(): Promise<DbClient> {
147
159
  const state = slot();
148
160
  if (state.promise) return state.promise;
149
161
  // A reload keeps the previous generation's handle: the schema was already
150
162
  // initialized in this process, and re-opening would leak the old one.
151
- if (state.resolved) return state.resolved;
163
+ if (state.resolved) return Promise.resolve(state.resolved);
152
164
 
153
165
  const opening = (async () => {
154
166
  const dbPath = await getDatabasePath();
@@ -199,6 +211,13 @@ export async function getDb(): Promise<DbClient> {
199
211
  state.promise = null;
200
212
  throw error;
201
213
  });
214
+ // Mark the rejection as observed. A caller usually awaits this same promise,
215
+ // but `closeDb` can retire the slot first — it reads `state.promise` only
216
+ // after clearing it, and the handler above clears it again — so the rejection
217
+ // can land with no owner at all, and Bun reports that as "Unhandled error
218
+ // between tests" against whichever case happens to be running. Measured on
219
+ // the Release gate: one Windows timeout reported a second, unrelated failure.
220
+ state.promise.catch(noop);
202
221
 
203
222
  return state.promise;
204
223
  }
@@ -18,6 +18,7 @@ import { ensureScheduleSchema } from '@/server/lib/schedule/schema.server';
18
18
  import { ensureWaitingSchema } from '@/server/lib/waiting/schema.server';
19
19
  import { ensurePluginSchema } from '@/server/lib/plugins/schema.server';
20
20
  import { ensureModelRolePresetSchema } from '@/server/lib/model-roles/schema.server';
21
+ import { ensureProviderIconSchema } from '@/server/lib/models/provider-icon-store.server';
21
22
  import { ensureWorktreeSchema } from '@/server/lib/db/worktree-registry.server';
22
23
  import { migrateWorkspaceFolderColumns } from '@/shared/lib/workspace/schema-migrations';
23
24
  import { migrateSessionStreamStateColumns } from '@/shared/lib/omp/session/schema-migrations';
@@ -113,6 +114,7 @@ export async function initSchema(db: DbClient): Promise<void> {
113
114
  await ensurePluginSchema(db);
114
115
  // Named model-role presets (the role editor's preset picker).
115
116
  await ensureModelRolePresetSchema(db);
117
+ await ensureProviderIconSchema(db);
116
118
  // Worktree registry: the ref each worktree branched from, and the identity
117
119
  // cache that keys its folder.
118
120
  await ensureWorktreeSchema(db);
@@ -0,0 +1,100 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ /**
7
+ * The directories a DROP made readable, and the bounds on that.
8
+ *
9
+ * This is the one place the fs allow-list can grow at run time, so every rule
10
+ * here is a security property rather than a convenience: a grant names a
11
+ * directory, it never covers the filesystem root, and the set cannot grow
12
+ * without bound.
13
+ */
14
+
15
+ import { afterEach, describe, expect, test } from 'bun:test';
16
+ import path from 'path';
17
+ import {
18
+ MAX_EXTERNAL_ROOTS,
19
+ clearExternalRoots,
20
+ externalRoots,
21
+ grantExternalRoot,
22
+ isExternalRoot,
23
+ } from '@/server/lib/fs/external-roots';
24
+
25
+ afterEach(() => clearExternalRoots());
26
+
27
+ /** The slot's shape, narrowed rather than asserted — the test reads a global. */
28
+ function isRootSlot(value: unknown): value is { roots: string[] } {
29
+ if (typeof value !== 'object' || value === null || !('roots' in value)) return false;
30
+ return Array.isArray(value.roots);
31
+ }
32
+
33
+ describe('external root grants', () => {
34
+ test('a granted directory is readable, and nothing else is', () => {
35
+ grantExternalRoot('/Users/someone/Desktop');
36
+ expect(isExternalRoot('/Users/someone/Desktop')).toBe(true);
37
+ // The parent is a different directory: dropping `~/Desktop/x.pdf` must not
38
+ // make the whole home directory readable.
39
+ expect(isExternalRoot('/Users/someone')).toBe(false);
40
+ expect(isExternalRoot('/tmp')).toBe(false);
41
+ });
42
+
43
+ test('the spelling does not matter, but the directory does', () => {
44
+ grantExternalRoot('/Users/someone/Desktop/');
45
+ expect(isExternalRoot('/Users/someone/Desktop')).toBe(true);
46
+ expect(isExternalRoot('/Users/someone/Desktop/../Desktop')).toBe(true);
47
+ expect(isExternalRoot('/Users/someone/Desktopp')).toBe(false);
48
+ });
49
+
50
+ test('the filesystem root is refused', () => {
51
+ // `/` would make every path on the machine readable, which is not what any
52
+ // drop expressed.
53
+ expect(grantExternalRoot('/')).toBeNull();
54
+ expect(externalRoots()).toEqual([]);
55
+ });
56
+
57
+ test('a relative path is refused', () => {
58
+ expect(grantExternalRoot('Desktop')).toBeNull();
59
+ expect(externalRoots()).toEqual([]);
60
+ });
61
+
62
+ test('the set is bounded, and the oldest grant goes first', () => {
63
+ for (let index = 0; index < MAX_EXTERNAL_ROOTS + 2; index += 1) {
64
+ grantExternalRoot(`/tmp/kabmo-grant-${index}`);
65
+ }
66
+ expect(externalRoots().length).toBe(MAX_EXTERNAL_ROOTS);
67
+ // The two oldest are gone; the newest are here.
68
+ expect(isExternalRoot('/tmp/kabmo-grant-0')).toBe(false);
69
+ expect(isExternalRoot('/tmp/kabmo-grant-1')).toBe(false);
70
+ expect(isExternalRoot(`/tmp/kabmo-grant-${MAX_EXTERNAL_ROOTS + 1}`)).toBe(true);
71
+ });
72
+
73
+ test('re-granting moves a root to the newest position', () => {
74
+ grantExternalRoot('/tmp/kabmo-first');
75
+ for (let index = 0; index < MAX_EXTERNAL_ROOTS - 1; index += 1) {
76
+ grantExternalRoot(`/tmp/kabmo-other-${index}`);
77
+ }
78
+ // One more would evict the oldest — unless it was just used again.
79
+ grantExternalRoot('/tmp/kabmo-first');
80
+ grantExternalRoot('/tmp/kabmo-last');
81
+ expect(isExternalRoot('/tmp/kabmo-first')).toBe(true);
82
+ });
83
+
84
+ test('the grant list lives on globalThis, so a hot reload cannot drop it', () => {
85
+ grantExternalRoot('/tmp/kabmo-hot-reload');
86
+ // The property, not the module binding: `bun --hot` re-evaluates a changed
87
+ // module in place, and a module-level array would be reset while the tab
88
+ // whose file it describes stays open.
89
+ const host: object = globalThis;
90
+ expect('__kabmoExternalRoots' in host).toBe(true);
91
+ const slot: unknown = Reflect.get(host, '__kabmoExternalRoots');
92
+ expect(isRootSlot(slot) && slot.roots.includes(path.resolve('/tmp/kabmo-hot-reload'))).toBe(true);
93
+ });
94
+
95
+ test('the resolved path is what a grant stores', () => {
96
+ const granted = grantExternalRoot('/tmp/kabmo-resolved/../kabmo-resolved');
97
+ expect(granted).toBe(path.resolve('/tmp/kabmo-resolved'));
98
+ expect(externalRoots()).toEqual([path.resolve('/tmp/kabmo-resolved')]);
99
+ });
100
+ });
@@ -0,0 +1,101 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ /**
7
+ * Directories the user handed the console by DROPPING a file from them.
8
+ *
9
+ * Every fs route resolves a client `root` through an allow-list: the app root,
10
+ * a path beneath it, or a registered workspace. That is the right rule for a
11
+ * panel — a client-supplied path must not become a filesystem grant — but it
12
+ * refuses the most ordinary gesture there is: dragging a report off the Desktop
13
+ * onto the editor. The file is real, the user pointed at it, and the console
14
+ * answered "outside every workspace this console can read".
15
+ *
16
+ * A grant is therefore created by ONE gesture, in ONE place (`POST /api/fs/grant`,
17
+ * called only by the drop path), and it names the dropped file's own DIRECTORY —
18
+ * the granularity a drop actually expresses. Three rules bound it:
19
+ *
20
+ * - **Only a same-origin POST creates one.** A `GET` cannot: a cross-origin page
21
+ * can fire a `no-cors` request at localhost without reading the answer, and a
22
+ * grant registered as a side effect would outlive the page that made it.
23
+ * - **A grant is a DIRECTORY, not a subtree of the filesystem.** Dropping
24
+ * `~/Desktop/report.pdf` makes `~/Desktop` readable; it does not make `~`
25
+ * readable, and the filesystem root is refused outright.
26
+ * - **The set is small and bounded.** A grant is remembered for the life of the
27
+ * process, and past `MAX_EXTERNAL_ROOTS` the oldest is dropped — a console
28
+ * that has been running for a week must not accumulate a path allow-list.
29
+ *
30
+ * It lives on `globalThis` for the reason the database handle does: `bun --hot`
31
+ * re-evaluates this module in place, and a module-level set would forget every
32
+ * open tab's root on the next save of any file.
33
+ */
34
+
35
+ import path from 'path';
36
+
37
+ /** How many dropped directories are remembered at once. */
38
+ export const MAX_EXTERNAL_ROOTS = 8;
39
+
40
+ /** The slot, anchored so a hot reload cannot drop the grants an open tab needs. */
41
+ interface ExternalRootSlot {
42
+ /** Canonical directories, oldest first. */
43
+ roots: string[];
44
+ }
45
+
46
+ const SLOT_KEY = '__kabmoExternalRoots';
47
+
48
+ function slot(): ExternalRootSlot {
49
+ const host = globalThis as typeof globalThis & { [SLOT_KEY]?: ExternalRootSlot };
50
+ host[SLOT_KEY] ??= { roots: [] };
51
+ return host[SLOT_KEY];
52
+ }
53
+
54
+ /**
55
+ * The directories currently granted, newest last.
56
+ *
57
+ * Exposed for the tests and for a diagnostic read; nothing in the request path
58
+ * needs the list itself.
59
+ */
60
+ export function externalRoots(): readonly string[] {
61
+ return slot().roots;
62
+ }
63
+
64
+ /** Forgets every grant. The tests' own seam. */
65
+ export function clearExternalRoots(): void {
66
+ slot().roots = [];
67
+ }
68
+
69
+ /**
70
+ * Whether `dir` is a directory the user dropped a file from.
71
+ *
72
+ * Compared by RESOLVED path: `path.resolve` collapses `..` and a trailing
73
+ * separator, and both spellings name the same directory as far as the fs routes
74
+ * are concerned.
75
+ */
76
+ export function isExternalRoot(dir: string): boolean {
77
+ const resolved = path.resolve(dir);
78
+ return slot().roots.includes(resolved);
79
+ }
80
+
81
+ /**
82
+ * Remember `dir` as a root the console may read.
83
+ *
84
+ * Returns the resolved directory, or null when it cannot be a grant: the
85
+ * filesystem root, or a path that is not absolute. `/` is refused because it is
86
+ * the one directory that makes every other path readable, which is not what any
87
+ * drop expressed.
88
+ */
89
+ export function grantExternalRoot(dir: string): string | null {
90
+ if (!path.isAbsolute(dir)) return null;
91
+ const resolved = path.resolve(dir);
92
+ if (resolved === path.parse(resolved).root) return null;
93
+
94
+ const store = slot();
95
+ // Re-inserting moves it to the newest position, so a root in constant use is
96
+ // not the one evicted.
97
+ store.roots = store.roots.filter((entry) => entry !== resolved);
98
+ store.roots.push(resolved);
99
+ while (store.roots.length > MAX_EXTERNAL_ROOTS) store.roots.shift();
100
+ return resolved;
101
+ }