@myrialabs/clopen 0.4.47 → 0.4.49

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 (390) hide show
  1. package/README.md +11 -11
  2. package/backend/artifacts/project/sources.ts +1 -1
  3. package/backend/auth/auth-service.ts +52 -358
  4. package/backend/auth/onboarding.test.ts +2 -3
  5. package/backend/auth/permissions.test.ts +10 -20
  6. package/backend/auth/permissions.ts +4 -290
  7. package/backend/auth/rate-limiter.test.ts +8 -9
  8. package/backend/auth/rate-limiter.ts +0 -2
  9. package/backend/auth/tokens.ts +1 -8
  10. package/backend/bootstrap.ts +7 -0
  11. package/backend/chat/attachments.test.ts +88 -0
  12. package/backend/chat/attachments.ts +159 -0
  13. package/backend/chat/engine-handoff.test.ts +0 -2
  14. package/backend/chat/message-pages.test.ts +163 -0
  15. package/backend/chat/message-pages.ts +233 -0
  16. package/backend/chat/stream-manager.ts +91 -67
  17. package/backend/chat/turn-answer.test.ts +64 -0
  18. package/backend/chat/turn-answer.ts +125 -0
  19. package/backend/chat-apps/awaiting.test.ts +39 -0
  20. package/backend/chat-apps/awaiting.ts +49 -0
  21. package/backend/chat-apps/conversation.ts +746 -0
  22. package/backend/chat-apps/inbound.ts +324 -0
  23. package/backend/chat-apps/index.ts +309 -0
  24. package/backend/chat-apps/link-events.ts +20 -0
  25. package/backend/chat-apps/media.test.ts +62 -0
  26. package/backend/chat-apps/media.ts +208 -0
  27. package/backend/chat-apps/messages.test.ts +229 -0
  28. package/backend/chat-apps/messages.ts +365 -0
  29. package/backend/chat-apps/outbound.ts +243 -0
  30. package/backend/chat-apps/pairing.test.ts +45 -0
  31. package/backend/chat-apps/pairing.ts +74 -0
  32. package/backend/chat-apps/platforms/index.ts +20 -0
  33. package/backend/chat-apps/platforms/telegram.test.ts +35 -0
  34. package/backend/chat-apps/platforms/telegram.ts +191 -0
  35. package/backend/chat-apps/platforms/types.ts +114 -0
  36. package/backend/chat-apps/questions.ts +133 -0
  37. package/backend/chat-apps/runtime.ts +572 -0
  38. package/backend/chat-apps/storage.test.ts +201 -0
  39. package/backend/chat-apps/turns.test.ts +45 -0
  40. package/backend/chat-apps/turns.ts +208 -0
  41. package/backend/containers/pty.ts +3 -11
  42. package/backend/control/actions/accounts.ts +89 -0
  43. package/backend/control/actions/files.ts +116 -0
  44. package/backend/control/actions/models.ts +188 -0
  45. package/backend/control/actions/projects.ts +272 -0
  46. package/backend/control/actions/sessions.ts +280 -0
  47. package/backend/control/actions/skills.ts +111 -0
  48. package/backend/control/control.test.ts +169 -0
  49. package/backend/control/define.ts +20 -0
  50. package/backend/control/folders.test.ts +67 -0
  51. package/backend/control/index.ts +213 -0
  52. package/backend/control/invoke.ts +44 -0
  53. package/backend/control/links.ts +35 -0
  54. package/backend/control/paging.ts +26 -0
  55. package/backend/control/types.ts +117 -0
  56. package/backend/control/workspace.ts +127 -0
  57. package/backend/database/migrations/084_create_channel_links.ts +71 -0
  58. package/backend/database/migrations/085_add_home_projects.ts +36 -0
  59. package/backend/database/migrations/086_single_owner.ts +418 -0
  60. package/backend/database/migrations/087_create_blob_line_stats.ts +37 -0
  61. package/backend/database/migrations/index.ts +28 -0
  62. package/backend/database/queries/auth-queries.ts +7 -85
  63. package/backend/database/queries/blob-line-stats-queries.ts +48 -0
  64. package/backend/database/queries/channel-queries.ts +270 -0
  65. package/backend/database/queries/db-client-connection-queries.ts +18 -56
  66. package/backend/database/queries/file-share-queries.ts +5 -23
  67. package/backend/database/queries/git-identity-queries.ts +31 -42
  68. package/backend/database/queries/graph-queries.ts +1 -1
  69. package/backend/database/queries/index.ts +15 -1
  70. package/backend/database/queries/message-queries.test.ts +146 -0
  71. package/backend/database/queries/message-queries.ts +42 -1
  72. package/backend/database/queries/permission-set-queries.ts +1 -1
  73. package/backend/database/queries/profile-queries.ts +1 -1
  74. package/backend/database/queries/project-mcp-trust-queries.ts +1 -1
  75. package/backend/database/queries/project-queries.ts +50 -63
  76. package/backend/database/queries/session-queries.ts +28 -56
  77. package/backend/database/queries/ssh-connection-queries.ts +18 -41
  78. package/backend/db-client/env/diff.ts +9 -33
  79. package/backend/db-client/env/env.test.ts +8 -8
  80. package/backend/db-client/env/service.ts +1 -1
  81. package/backend/db-client/integrations/projector.ts +0 -5
  82. package/backend/engine/adapters/claude/message-converter.test.ts +47 -0
  83. package/backend/engine/adapters/claude/message-converter.ts +25 -2
  84. package/backend/engine/adapters/cline/message-converter.ts +0 -1
  85. package/backend/engine/adapters/cline/stream.ts +11 -15
  86. package/backend/engine/adapters/codex/message-converter.ts +0 -2
  87. package/backend/engine/adapters/copilot/message-converter.ts +1 -2
  88. package/backend/engine/adapters/cursor/message-converter.ts +0 -1
  89. package/backend/engine/adapters/opencode/message-converter.ts +2 -4
  90. package/backend/engine/adapters/opencode/stream.ts +11 -0
  91. package/backend/engine/adapters/parent-scope.test.ts +38 -0
  92. package/backend/engine/adapters/parent-scope.ts +44 -0
  93. package/backend/engine/adapters/pi/message-converter.ts +0 -1
  94. package/backend/engine/adapters/pi/stream.ts +4 -5
  95. package/backend/engine/adapters/qwen/message-converter.ts +0 -2
  96. package/backend/engine/bootstrap-default-engine.ts +2 -2
  97. package/backend/engine/docs/adapter-contract.md +8 -1
  98. package/backend/engine/install-runner.ts +8 -17
  99. package/backend/files/file-shares.ts +20 -44
  100. package/backend/files/file-size-limit.ts +3 -3
  101. package/backend/files/file-watcher.ts +19 -0
  102. package/backend/files/image-processing.ts +1 -1
  103. package/backend/files/os-clipboard.ts +2 -2
  104. package/backend/git/contributors.test.ts +35 -0
  105. package/backend/git/contributors.ts +46 -0
  106. package/backend/git/git-executor.ts +25 -5
  107. package/backend/git/git-service.ts +420 -19
  108. package/backend/git/identity/credential-helper.test.ts +2 -7
  109. package/backend/git/identity/import-local.test.ts +11 -16
  110. package/backend/git/identity/import-local.ts +12 -12
  111. package/backend/git/identity/index.ts +10 -24
  112. package/backend/git/identity/resolve.ts +13 -14
  113. package/backend/git/identity/scope-config.ts +33 -28
  114. package/backend/git/identity/service.ts +44 -47
  115. package/backend/git/op-lock.test.ts +111 -0
  116. package/backend/git/op-lock.ts +180 -0
  117. package/backend/git/remote-sync.test.ts +249 -0
  118. package/backend/host/metrics.ts +1 -1
  119. package/backend/http/bearer-auth.ts +1 -2
  120. package/backend/http/files-download.test.ts +8 -9
  121. package/backend/http/files-download.ts +2 -2
  122. package/backend/http/files-shared.test.ts +62 -89
  123. package/backend/http/files-shared.ts +2 -2
  124. package/backend/http/files-upload.test.ts +3 -7
  125. package/backend/http/files-upload.ts +3 -3
  126. package/backend/http/notes-images.test.ts +15 -38
  127. package/backend/http/notes-images.ts +6 -8
  128. package/backend/http/ssh-sftp.ts +5 -10
  129. package/backend/http/static-assets.test.ts +78 -0
  130. package/backend/http/static-assets.ts +104 -0
  131. package/backend/index.ts +32 -16
  132. package/backend/integrations/accounts.ts +61 -3
  133. package/backend/integrations/index.ts +2 -2
  134. package/backend/integrations/registry/define.ts +15 -0
  135. package/backend/integrations/registry/index.ts +2 -0
  136. package/backend/integrations/registry/providers/telegram.ts +54 -0
  137. package/backend/integrations/registry/types.ts +21 -1
  138. package/backend/integrations/webhooks/gateway.ts +7 -6
  139. package/backend/integrations/webhooks/verify.test.ts +30 -0
  140. package/backend/integrations/webhooks/verify.ts +4 -0
  141. package/backend/mcp/external/project.ts +1 -1
  142. package/backend/mcp/internal/remote-server.test.ts +1 -3
  143. package/backend/mcp/internal/remote-server.ts +4 -4
  144. package/backend/mcp/internal/servers/browser-automation/actions/dialogs.ts +67 -0
  145. package/backend/mcp/internal/servers/browser-automation/actions/index.ts +3 -1
  146. package/backend/mcp/internal/servers/browser-automation/actions/input.ts +5 -5
  147. package/backend/mcp/internal/servers/browser-automation/actions/tabs.ts +157 -28
  148. package/backend/mcp/internal/servers/browser-automation/context.ts +1 -0
  149. package/backend/mcp/internal/servers/browser-automation/description.ts +5 -2
  150. package/backend/mcp/internal/servers/browser-automation/format.ts +24 -1
  151. package/backend/mcp/internal/servers/browser-automation/runner.ts +68 -3
  152. package/backend/memory/redact.ts +1 -1
  153. package/backend/notes/access.test.ts +28 -29
  154. package/backend/notes/access.ts +6 -11
  155. package/backend/permissions/service.ts +1 -1
  156. package/backend/preview/browser/browser-clipboard.test.ts +46 -0
  157. package/backend/preview/browser/browser-clipboard.ts +67 -0
  158. package/backend/preview/browser/browser-dialog-handler.ts +95 -6
  159. package/backend/preview/browser/browser-host-bridge.ts +9 -0
  160. package/backend/preview/browser/browser-interaction-handler.ts +316 -86
  161. package/backend/preview/browser/browser-mcp-control.ts +34 -0
  162. package/backend/preview/browser/browser-pool.ts +67 -6
  163. package/backend/preview/browser/browser-preview-service.ts +383 -29
  164. package/backend/preview/browser/browser-tab-manager.ts +66 -23
  165. package/backend/preview/browser/browser-video-capture.ts +92 -5
  166. package/backend/preview/browser/element-locator.ts +190 -17
  167. package/backend/preview/browser/keyboard-chords.test.ts +68 -0
  168. package/backend/preview/browser/keyboard-chords.ts +132 -0
  169. package/backend/preview/browser/paste.ts +81 -0
  170. package/backend/preview/browser/process-usage.ts +142 -0
  171. package/backend/preview/browser/profile-lock.ts +209 -0
  172. package/backend/preview/browser/scripts/host-bridge.ts +59 -0
  173. package/backend/preview/browser/scripts/video-stream.ts +58 -0
  174. package/backend/preview/browser/types.ts +40 -2
  175. package/backend/preview/browser/viewport.test.ts +45 -0
  176. package/backend/preview/browser/viewport.ts +96 -0
  177. package/backend/profiles/service.ts +2 -2
  178. package/backend/project/status-manager.ts +43 -172
  179. package/backend/projects/home.ts +61 -0
  180. package/backend/projects/project-cleanup.integration.test.ts +2 -2
  181. package/backend/projects/project-cleanup.ts +0 -8
  182. package/backend/skills/service.ts +2 -3
  183. package/backend/snapshot/blob-store.ts +9 -1
  184. package/backend/snapshot/helpers.ts +3 -11
  185. package/backend/snapshot/line-stats.test.ts +80 -0
  186. package/backend/snapshot/line-stats.ts +64 -0
  187. package/backend/snapshot/restore.ts +252 -0
  188. package/backend/snapshot/snapshot-service.ts +14 -27
  189. package/backend/snapshot/turn-changes.test.ts +10 -5
  190. package/backend/snapshot/turn-changes.ts +78 -40
  191. package/backend/ssh/ptykit.ts +1 -2
  192. package/backend/terminal/ptykit.ts +10 -8
  193. package/backend/tunnel/public-origin.ts +31 -20
  194. package/backend/utils/user-helpers.ts +0 -55
  195. package/backend/utils/ws-wire.test.ts +87 -0
  196. package/backend/utils/ws-wire.ts +189 -0
  197. package/backend/utils/ws.ts +101 -192
  198. package/backend/worktrees/branching/engine.ts +3 -16
  199. package/backend/worktrees/branching/index.ts +1 -1
  200. package/backend/worktrees/branching/projector.ts +1 -1
  201. package/backend/worktrees/branching/service.ts +13 -25
  202. package/backend/ws/access.ts +12 -21
  203. package/backend/ws/artifacts/generate.ts +2 -2
  204. package/backend/ws/auth/device.ts +16 -71
  205. package/backend/ws/auth/index.ts +4 -22
  206. package/backend/ws/auth/login.test.ts +14 -60
  207. package/backend/ws/auth/login.ts +24 -124
  208. package/backend/ws/auth/status.ts +0 -1
  209. package/backend/ws/chat/composer.ts +118 -118
  210. package/backend/ws/chat/stream.ts +105 -57
  211. package/backend/ws/chat-apps/index.ts +117 -0
  212. package/backend/ws/containers/actions.ts +11 -25
  213. package/backend/ws/containers/index.ts +3 -4
  214. package/backend/ws/containers/list.ts +10 -36
  215. package/backend/ws/containers/logs.ts +2 -2
  216. package/backend/ws/containers/tunnel.ts +1 -2
  217. package/backend/ws/db-client/access.ts +2 -16
  218. package/backend/ws/db-client/accounts.ts +5 -15
  219. package/backend/ws/db-client/connections.ts +17 -25
  220. package/backend/ws/db-client/context.ts +1 -1
  221. package/backend/ws/db-client/env.ts +7 -10
  222. package/backend/ws/db-client/index.ts +2 -2
  223. package/backend/ws/db-client/io.ts +5 -5
  224. package/backend/ws/db-client/query-history-retention.test.ts +8 -8
  225. package/backend/ws/db-client/query.ts +18 -17
  226. package/backend/ws/db-client/schema.ts +13 -13
  227. package/backend/ws/db-client/structure.ts +35 -35
  228. package/backend/ws/db-client/supabase.ts +4 -21
  229. package/backend/ws/deployments/context.ts +1 -1
  230. package/backend/ws/deployments/crud.ts +0 -5
  231. package/backend/ws/engine/access.ts +7 -20
  232. package/backend/ws/engine/claude/accounts.ts +4 -4
  233. package/backend/ws/engine/cline/accounts.ts +5 -5
  234. package/backend/ws/engine/codex/accounts.ts +3 -3
  235. package/backend/ws/engine/pi/accounts.ts +5 -5
  236. package/backend/ws/files/clipboard.ts +8 -27
  237. package/backend/ws/files/image-edit.ts +6 -6
  238. package/backend/ws/files/index.ts +1 -1
  239. package/backend/ws/files/path-access.test.ts +32 -131
  240. package/backend/ws/files/path-access.ts +8 -87
  241. package/backend/ws/files/read.ts +45 -19
  242. package/backend/ws/files/reveal.ts +3 -3
  243. package/backend/ws/files/search.ts +7 -7
  244. package/backend/ws/files/share.ts +9 -13
  245. package/backend/ws/files/state.ts +9 -12
  246. package/backend/ws/files/watch.ts +3 -3
  247. package/backend/ws/files/write.ts +30 -31
  248. package/backend/ws/git/branch.ts +80 -59
  249. package/backend/ws/git/commit.ts +63 -44
  250. package/backend/ws/git/conflict.ts +16 -18
  251. package/backend/ws/git/identity-context.ts +8 -17
  252. package/backend/ws/git/identity.ts +17 -27
  253. package/backend/ws/git/index.ts +3 -1
  254. package/backend/ws/git/log.ts +52 -0
  255. package/backend/ws/git/op-state.ts +139 -0
  256. package/backend/ws/git/remote.ts +241 -141
  257. package/backend/ws/git/repo-context.ts +67 -0
  258. package/backend/ws/git/staging.ts +18 -28
  259. package/backend/ws/git/status.ts +5 -2
  260. package/backend/ws/index.ts +5 -1
  261. package/backend/ws/instructions/crud.ts +1 -1
  262. package/backend/ws/integrations/crud.ts +1 -3
  263. package/backend/ws/mcp/crud.ts +3 -5
  264. package/backend/ws/mcp/tools.ts +1 -1
  265. package/backend/ws/memory/crud.ts +14 -46
  266. package/backend/ws/memory/index.ts +2 -2
  267. package/backend/ws/messages/crud.ts +78 -8
  268. package/backend/ws/notes/index.ts +33 -39
  269. package/backend/ws/permissions/crud.ts +3 -4
  270. package/backend/ws/ports/index.ts +1 -2
  271. package/backend/ws/ports/kill.ts +5 -10
  272. package/backend/ws/ports/list.ts +4 -10
  273. package/backend/ws/preview/access.ts +1 -1
  274. package/backend/ws/preview/browser/interact.ts +52 -79
  275. package/backend/ws/preview/browser/native-ui.ts +24 -0
  276. package/backend/ws/preview/browser/stats.ts +98 -1
  277. package/backend/ws/preview/browser/tab-info.ts +15 -0
  278. package/backend/ws/preview/browser/tab.ts +19 -4
  279. package/backend/ws/preview/index.ts +29 -0
  280. package/backend/ws/preview/watchers.ts +84 -0
  281. package/backend/ws/profiles/crud.ts +6 -8
  282. package/backend/ws/project-artifacts/index.ts +30 -31
  283. package/backend/ws/projects/crud.ts +33 -35
  284. package/backend/ws/projects/index.ts +3 -8
  285. package/backend/ws/projects/info.ts +3 -3
  286. package/backend/ws/projects/overview.ts +19 -30
  287. package/backend/ws/projects/status.ts +17 -57
  288. package/backend/ws/sessions/crud.ts +42 -57
  289. package/backend/ws/sessions/index.ts +1 -1
  290. package/backend/ws/settings/crud.ts +1 -1
  291. package/backend/ws/skills/crud.ts +3 -4
  292. package/backend/ws/skills/index.ts +1 -1
  293. package/backend/ws/snapshot/changes.ts +12 -13
  294. package/backend/ws/snapshot/restore.ts +11 -295
  295. package/backend/ws/snapshot/timeline.ts +2 -4
  296. package/backend/ws/ssh/access.ts +6 -21
  297. package/backend/ws/ssh/connections.ts +21 -31
  298. package/backend/ws/ssh/forwards.ts +12 -12
  299. package/backend/ws/ssh/sftp.ts +34 -34
  300. package/backend/ws/ssh/tunnel.ts +1 -2
  301. package/backend/ws/stack/install.ts +7 -17
  302. package/backend/ws/subagents/crud.ts +1 -1
  303. package/backend/ws/terminal/tunnel.ts +2 -2
  304. package/backend/ws/tunnel/share.ts +0 -2
  305. package/backend/ws/user/crud.ts +1 -59
  306. package/backend/ws/user/index.ts +1 -1
  307. package/backend/ws/work/context.ts +1 -1
  308. package/backend/ws/work/crud.ts +0 -5
  309. package/backend/ws/workspace/state.ts +10 -13
  310. package/backend/ws/worktrees/branching.ts +7 -36
  311. package/backend/ws/worktrees/crud.ts +2 -2
  312. package/bin/clopen.ts +46 -41
  313. package/dist/assets/addon-clipboard-wHJhZAA4.js.br +0 -0
  314. package/dist/assets/addon-clipboard-wHJhZAA4.js.gz +0 -0
  315. package/dist/assets/addon-fit-DX4qG4td.js.br +1 -0
  316. package/dist/assets/addon-fit-DX4qG4td.js.gz +0 -0
  317. package/dist/assets/addon-ligatures-DpDY8N_X.js.br +0 -0
  318. package/dist/assets/addon-ligatures-DpDY8N_X.js.gz +0 -0
  319. package/dist/assets/addon-unicode11-Bt8F3D7-.js.br +0 -0
  320. package/dist/assets/addon-unicode11-Bt8F3D7-.js.gz +0 -0
  321. package/dist/assets/addon-web-links-DIbG5aQx.js.br +0 -0
  322. package/dist/assets/addon-web-links-DIbG5aQx.js.gz +0 -0
  323. package/dist/assets/diff.worker-BoHLxmr2.js +7 -0
  324. package/dist/assets/diff.worker-BoHLxmr2.js.br +0 -0
  325. package/dist/assets/diff.worker-BoHLxmr2.js.gz +0 -0
  326. package/dist/assets/graph-layout.worker-CRFqN7ZQ.js.br +0 -0
  327. package/dist/assets/graph-layout.worker-CRFqN7ZQ.js.gz +0 -0
  328. package/dist/assets/{index-CwuKQelV.js → index-CrVWZH_D.js} +1 -1
  329. package/dist/assets/index-CrVWZH_D.js.br +0 -0
  330. package/dist/assets/index-CrVWZH_D.js.gz +0 -0
  331. package/dist/assets/{index-BTcR1byF.js → index-DD0-3EWg.js} +1 -1
  332. package/dist/assets/index-DD0-3EWg.js.br +0 -0
  333. package/dist/assets/index-DD0-3EWg.js.gz +0 -0
  334. package/dist/assets/main-D2_TmcwQ.js +1240 -0
  335. package/dist/assets/main-D2_TmcwQ.js.br +0 -0
  336. package/dist/assets/main-D2_TmcwQ.js.gz +0 -0
  337. package/dist/assets/main-Yucigcmf.css +1 -0
  338. package/dist/assets/main-Yucigcmf.css.br +0 -0
  339. package/dist/assets/main-Yucigcmf.css.gz +0 -0
  340. package/dist/assets/preview-video.worker-DcpDhCWL.js.br +0 -0
  341. package/dist/assets/preview-video.worker-DcpDhCWL.js.gz +0 -0
  342. package/dist/assets/xterm-B-qIQCd3.js.br +0 -0
  343. package/dist/assets/xterm-B-qIQCd3.js.gz +0 -0
  344. package/dist/assets/zipkit-engine-CaR0t_Uy.wasm.br +0 -0
  345. package/dist/assets/zipkit-engine-CaR0t_Uy.wasm.gz +0 -0
  346. package/dist/fonts/dm-sans.css.br +0 -0
  347. package/dist/fonts/dm-sans.css.gz +0 -0
  348. package/dist/index.html +2 -2
  349. package/dist/index.html.br +0 -0
  350. package/dist/index.html.gz +0 -0
  351. package/dist/sw.js.br +0 -0
  352. package/dist/sw.js.gz +0 -0
  353. package/package.json +3 -3
  354. package/shared/constants/attachments.ts +14 -0
  355. package/shared/constants/engine-tools.ts +1 -1
  356. package/shared/constants/preview.test.ts +259 -0
  357. package/shared/constants/preview.ts +1018 -57
  358. package/shared/constants/tool-icons.ts +5 -0
  359. package/shared/types/containers/container.ts +1 -1
  360. package/shared/types/database/schema.ts +9 -6
  361. package/shared/types/git-identity.ts +1 -1
  362. package/shared/types/git.ts +116 -0
  363. package/shared/types/integrations.ts +7 -0
  364. package/shared/types/stores/settings.ts +1 -1
  365. package/shared/types/unified/index.ts +1 -1
  366. package/shared/types/unified/message.ts +0 -7
  367. package/shared/types/unified/stream.ts +13 -5
  368. package/shared/types/unified/tool.ts +15 -1
  369. package/shared/utils/ask-user-question.test.ts +319 -0
  370. package/shared/utils/ask-user-question.ts +154 -0
  371. package/shared/utils/diff-calculator.test.ts +178 -41
  372. package/shared/utils/diff-calculator.ts +383 -100
  373. package/shared/utils/home-project.ts +25 -0
  374. package/shared/utils/logger.ts +2 -0
  375. package/shared/utils/process-supervisor.test.ts +172 -0
  376. package/shared/utils/process-supervisor.ts +251 -0
  377. package/shared/utils/ws-chunk.test.ts +33 -0
  378. package/shared/utils/ws-chunk.ts +73 -0
  379. package/shared/utils/ws-client-chunks.test.ts +45 -0
  380. package/shared/utils/ws-client.ts +43 -0
  381. package/shared/utils/ws-server.ts +49 -21
  382. package/backend/auth/session-invalidation.test.ts +0 -112
  383. package/backend/auth/session-invalidation.ts +0 -77
  384. package/backend/project/status-manager.test.ts +0 -32
  385. package/backend/ws/auth/invites.ts +0 -94
  386. package/backend/ws/auth/users.ts +0 -112
  387. package/backend/ws/projects/presence.ts +0 -46
  388. package/backend/ws/stack/access.ts +0 -31
  389. package/dist/assets/main-BiDNMWb3.css +0 -1
  390. package/dist/assets/main-BlhKWz2B.js +0 -1241
@@ -0,0 +1,418 @@
1
+ import { rmSync } from 'fs';
2
+ import { basename, dirname } from 'path';
3
+ import type { DatabaseConnection } from '$shared/types/database/connection';
4
+
5
+ import { debug } from '$shared/utils/logger';
6
+
7
+ export const description = 'Collapse multi-user collaboration into a single owner';
8
+
9
+ /**
10
+ * Clopen is one owner reaching their own machine from any number of devices.
11
+ * Multi-user collaboration (roles, invites, per-project membership, per-user
12
+ * scoping) is removed, and this migration takes an existing install there.
13
+ *
14
+ * The owner is the oldest admin — the account the setup wizard created — or,
15
+ * failing that, the oldest user. Everyone else is deleted together with the
16
+ * credentials and personal data that belonged to them. That part is a security
17
+ * requirement, not tidiness: once role checks are gone, any token a former
18
+ * member still holds would otherwise sign in as someone with full control.
19
+ *
20
+ * Shared history stays. Chats a member had in a regular project are project
21
+ * history and remain in that project; only what was theirs alone (their Home,
22
+ * their saved DB/SSH connections, git identities, paired chat apps, file
23
+ * shares, push subscriptions, sessions) goes.
24
+ *
25
+ * The per-user columns that only existed to separate people are dropped:
26
+ * `users.role`, `projects.owner_user_id`, `db_client_connections.owner_user_id`,
27
+ * `ssh_connections.owner_user_id`, `git_identities.owner_user_id`,
28
+ * `git_identity_bindings.user_id`, `chat_sessions.sender_*`,
29
+ * `channel_links.status` (a member's chat waited for an admin), and the user key
30
+ * of `user_projects` / `user_unread_sessions`, which become `workspace_projects`
31
+ * / `unread_sessions`. Columns that record WHO authenticated (auth sessions,
32
+ * device codes, push subscriptions, chat-app links, audit logs, `created_by`)
33
+ * keep their user id — the owner still has one.
34
+ */
35
+
36
+ function tableExists(db: DatabaseConnection, name: string): boolean {
37
+ return Boolean(db.prepare(`SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ?`).get(name));
38
+ }
39
+
40
+ function hasColumn(db: DatabaseConnection, table: string, column: string): boolean {
41
+ const columns = db.prepare(`PRAGMA table_info(${table})`).all() as { name: string }[];
42
+ return columns.some((c) => c.name === column);
43
+ }
44
+
45
+ /** Every chat session of a project and everything hanging off it, mirroring `sessionQueries.deleteAllByProjectId`. */
46
+ function deleteProjectSessions(db: DatabaseConnection, projectId: string): void {
47
+ const inProject = 'SELECT id FROM chat_sessions WHERE project_id = ?';
48
+ if (tableExists(db, 'branches')) db.prepare(`DELETE FROM branches WHERE session_id IN (${inProject})`).run(projectId);
49
+ if (tableExists(db, 'message_snapshots')) db.prepare('DELETE FROM message_snapshots WHERE project_id = ?').run(projectId);
50
+ if (tableExists(db, 'session_relationships')) {
51
+ db.prepare(`DELETE FROM session_relationships WHERE parent_session_id IN (${inProject}) OR child_session_id IN (${inProject})`)
52
+ .run(projectId, projectId);
53
+ }
54
+ db.prepare(`DELETE FROM messages WHERE session_id IN (${inProject})`).run(projectId);
55
+ if (tableExists(db, 'messages_fts')) db.prepare('DELETE FROM messages_fts WHERE project_id = ?').run(projectId);
56
+ db.prepare('DELETE FROM user_unread_sessions WHERE project_id = ?').run(projectId);
57
+ db.prepare('UPDATE user_projects SET current_session_id = NULL WHERE project_id = ?').run(projectId);
58
+ db.prepare('DELETE FROM chat_sessions WHERE project_id = ?').run(projectId);
59
+ }
60
+
61
+ /** Remove a project and every row that names it. FK cascades cover most tables; the rest are swept by column. */
62
+ function deleteProject(db: DatabaseConnection, projectId: string): void {
63
+ deleteProjectSessions(db, projectId);
64
+ const tables = db.prepare(`SELECT name FROM sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%'`).all() as { name: string }[];
65
+ for (const { name } of tables) {
66
+ if (name === 'projects' || name === 'chat_sessions') continue;
67
+ if (!hasColumn(db, name, 'project_id')) continue;
68
+ db.prepare(`DELETE FROM ${name} WHERE project_id = ?`).run(projectId);
69
+ }
70
+ db.prepare('DELETE FROM projects WHERE id = ?').run(projectId);
71
+ }
72
+
73
+ /**
74
+ * A Home directory is `<data dir>/home/<userId>`. Only a path of exactly that
75
+ * shape for the deleted user is removed, so a hand-edited row can never point
76
+ * this at somebody's repository.
77
+ */
78
+ function removeHomeDirectory(path: string, userId: string): void {
79
+ if (basename(path) !== userId || basename(dirname(path)) !== 'home') return;
80
+ try {
81
+ rmSync(path, { recursive: true, force: true });
82
+ } catch (error) {
83
+ debug.warn('migration', `Could not remove Home directory ${path}:`, error);
84
+ }
85
+ }
86
+
87
+ function pickOwner(db: DatabaseConnection): string | null {
88
+ const row = db.prepare(`
89
+ SELECT id FROM users
90
+ ORDER BY CASE WHEN role = 'admin' THEN 0 ELSE 1 END, created_at ASC, id ASC
91
+ LIMIT 1
92
+ `).get() as { id: string } | undefined;
93
+ return row?.id ?? null;
94
+ }
95
+
96
+ function removeOtherUsers(db: DatabaseConnection, ownerId: string): string[] {
97
+ const others = (db.prepare('SELECT id FROM users WHERE id != ?').all(ownerId) as { id: string }[]).map((u) => u.id);
98
+ if (others.length === 0) return [];
99
+ const homeDirs: Array<{ path: string; userId: string }> = [];
100
+
101
+ for (const userId of others) {
102
+ const homes = db.prepare(`SELECT id, path FROM projects WHERE kind = 'home' AND owner_user_id = ?`).all(userId) as { id: string; path: string }[];
103
+ for (const home of homes) {
104
+ deleteProject(db, home.id);
105
+ homeDirs.push({ path: home.path, userId });
106
+ }
107
+
108
+ db.prepare('DELETE FROM db_client_connections WHERE owner_user_id = ?').run(userId);
109
+ db.prepare('DELETE FROM ssh_connections WHERE owner_user_id = ?').run(userId);
110
+ db.prepare('DELETE FROM user_projects WHERE user_id = ?').run(userId);
111
+ db.prepare('DELETE FROM user_unread_sessions WHERE user_id = ?').run(userId);
112
+ if (tableExists(db, 'push_subscriptions')) db.prepare('DELETE FROM push_subscriptions WHERE user_id = ?').run(userId);
113
+ // Cascades: auth_sessions, device_codes, invite_tokens, file_shares,
114
+ // git_identities (+ bindings), git_identity_bindings, channel_links (+ messages).
115
+ db.prepare('DELETE FROM users WHERE id = ?').run(userId);
116
+ }
117
+
118
+ // Rows a member could still reach through a cascade that did not fire
119
+ // (an older schema without the FK) are swept explicitly.
120
+ db.prepare('DELETE FROM auth_sessions WHERE user_id != ?').run(ownerId);
121
+ db.prepare('DELETE FROM device_codes WHERE user_id != ?').run(ownerId);
122
+ if (tableExists(db, 'channel_links')) db.prepare('DELETE FROM channel_links WHERE user_id != ?').run(ownerId);
123
+ if (tableExists(db, 'file_shares')) db.prepare('DELETE FROM file_shares WHERE created_by != ?').run(ownerId);
124
+
125
+ for (const { path, userId } of homeDirs) removeHomeDirectory(path, userId);
126
+ return others;
127
+ }
128
+
129
+ function rebuildGitIdentities(db: DatabaseConnection): void {
130
+ db.exec(`
131
+ CREATE TABLE git_identities_new (
132
+ id TEXT PRIMARY KEY,
133
+ label TEXT NOT NULL UNIQUE,
134
+ name TEXT NOT NULL,
135
+ email TEXT NOT NULL,
136
+ auth_method TEXT NOT NULL DEFAULT 'none'
137
+ CHECK (auth_method IN ('none','ssh-key','https-token','https-account')),
138
+ ssh_private_key TEXT,
139
+ ssh_public_key TEXT,
140
+ ssh_passphrase TEXT,
141
+ https_username TEXT,
142
+ https_token TEXT,
143
+ integration_account_id TEXT REFERENCES integration_accounts(id) ON DELETE SET NULL,
144
+ hosts TEXT NOT NULL DEFAULT '[]',
145
+ is_default INTEGER NOT NULL DEFAULT 0,
146
+ source TEXT NOT NULL DEFAULT 'manual'
147
+ CHECK (source IN ('manual','local-machine')),
148
+ created_at TEXT NOT NULL,
149
+ updated_at TEXT NOT NULL
150
+ )
151
+ `);
152
+ db.exec(`
153
+ INSERT INTO git_identities_new (
154
+ id, label, name, email, auth_method, ssh_private_key, ssh_public_key, ssh_passphrase,
155
+ https_username, https_token, integration_account_id, hosts, is_default, source, created_at, updated_at
156
+ )
157
+ SELECT
158
+ id, label, name, email, auth_method, ssh_private_key, ssh_public_key, ssh_passphrase,
159
+ https_username, https_token, integration_account_id, hosts, is_default, source, created_at, updated_at
160
+ FROM git_identities
161
+ `);
162
+
163
+ db.exec(`
164
+ CREATE TABLE git_identity_bindings_new (
165
+ project_id TEXT PRIMARY KEY REFERENCES projects(id) ON DELETE CASCADE,
166
+ identity_id TEXT NOT NULL REFERENCES git_identities_new(id) ON DELETE CASCADE,
167
+ created_at TEXT NOT NULL,
168
+ updated_at TEXT NOT NULL
169
+ )
170
+ `);
171
+ db.exec(`
172
+ INSERT OR IGNORE INTO git_identity_bindings_new (project_id, identity_id, created_at, updated_at)
173
+ SELECT project_id, identity_id, created_at, updated_at FROM git_identity_bindings
174
+ `);
175
+
176
+ db.exec('DROP TABLE git_identity_bindings');
177
+ db.exec('DROP TABLE git_identities');
178
+ db.exec('ALTER TABLE git_identities_new RENAME TO git_identities');
179
+ db.exec('ALTER TABLE git_identity_bindings_new RENAME TO git_identity_bindings');
180
+
181
+ db.exec('CREATE INDEX idx_git_identity_bindings_identity ON git_identity_bindings (identity_id)');
182
+ // At most one default and one mirrored machine identity, now install-wide.
183
+ db.exec('CREATE UNIQUE INDEX idx_git_identities_one_default ON git_identities (is_default) WHERE is_default = 1');
184
+ db.exec(`CREATE UNIQUE INDEX idx_git_identities_one_local ON git_identities (source) WHERE source = 'local-machine'`);
185
+ }
186
+
187
+ export const up = (db: DatabaseConnection): void => {
188
+ debug.log('migration', '📋 Collapsing collaboration into a single owner...');
189
+
190
+ db.exec('BEGIN');
191
+ try {
192
+ const ownerId = pickOwner(db);
193
+ if (ownerId) {
194
+ const removed = removeOtherUsers(db, ownerId);
195
+ if (removed.length > 0) debug.log('migration', `Removed ${removed.length} non-owner account(s) and their data`);
196
+ }
197
+
198
+ // Invites only ever created members.
199
+ db.exec('DROP TABLE IF EXISTS invite_tokens');
200
+
201
+ db.exec('ALTER TABLE users DROP COLUMN role');
202
+
203
+ // Projects in the workspace list, with their saved workspace state. A
204
+ // project absent from here was "removed" (kept so its chats can be
205
+ // restored by adding the same folder again).
206
+ db.exec(`
207
+ CREATE TABLE workspace_projects (
208
+ project_id TEXT PRIMARY KEY REFERENCES projects(id) ON DELETE CASCADE,
209
+ joined_at TEXT NOT NULL,
210
+ current_session_id TEXT,
211
+ files_panel_state TEXT,
212
+ workspace_state TEXT
213
+ )
214
+ `);
215
+ if (ownerId) {
216
+ db.prepare(`
217
+ INSERT OR IGNORE INTO workspace_projects (project_id, joined_at, current_session_id, files_panel_state, workspace_state)
218
+ SELECT project_id, joined_at, current_session_id, files_panel_state, workspace_state
219
+ FROM user_projects
220
+ WHERE user_id = ? AND project_id IN (SELECT id FROM projects)
221
+ `).run(ownerId);
222
+ }
223
+ db.exec('DROP TABLE user_projects');
224
+
225
+ db.exec(`
226
+ CREATE TABLE unread_sessions (
227
+ session_id TEXT PRIMARY KEY REFERENCES chat_sessions(id) ON DELETE CASCADE,
228
+ project_id TEXT NOT NULL,
229
+ marked_at TEXT NOT NULL
230
+ )
231
+ `);
232
+ db.exec('CREATE INDEX idx_unread_sessions_project ON unread_sessions(project_id)');
233
+ db.exec(`
234
+ INSERT OR IGNORE INTO unread_sessions (session_id, project_id, marked_at)
235
+ SELECT session_id, project_id, MAX(marked_at) FROM user_unread_sessions
236
+ WHERE session_id IN (SELECT id FROM chat_sessions)
237
+ GROUP BY session_id
238
+ `);
239
+ db.exec('DROP TABLE user_unread_sessions');
240
+
241
+ // One Home for the install.
242
+ db.exec('DROP INDEX IF EXISTS idx_projects_home_owner');
243
+ db.exec('ALTER TABLE projects DROP COLUMN owner_user_id');
244
+ db.exec(`CREATE UNIQUE INDEX idx_projects_single_home ON projects (kind) WHERE kind = 'home'`);
245
+
246
+ db.exec('DROP INDEX IF EXISTS idx_db_client_conn_owner');
247
+ db.exec('ALTER TABLE db_client_connections DROP COLUMN owner_user_id');
248
+ db.exec('DROP INDEX IF EXISTS idx_ssh_conn_owner');
249
+ db.exec('ALTER TABLE ssh_connections DROP COLUMN owner_user_id');
250
+
251
+ if (hasColumn(db, 'chat_sessions', 'sender_id')) db.exec('ALTER TABLE chat_sessions DROP COLUMN sender_id');
252
+ if (hasColumn(db, 'chat_sessions', 'sender_name')) db.exec('ALTER TABLE chat_sessions DROP COLUMN sender_name');
253
+
254
+ rebuildGitIdentities(db);
255
+
256
+ if (tableExists(db, 'channel_links') && hasColumn(db, 'channel_links', 'status')) {
257
+ // Only a member's chat could still be waiting for approval.
258
+ db.exec(`DELETE FROM channel_links WHERE status != 'approved'`);
259
+ db.exec('ALTER TABLE channel_links DROP COLUMN status');
260
+ }
261
+
262
+ db.exec('COMMIT');
263
+ } catch (error) {
264
+ db.exec('ROLLBACK');
265
+ throw error;
266
+ }
267
+
268
+ debug.log('migration', '✅ Single owner in place');
269
+ };
270
+
271
+ /**
272
+ * Restores the multi-user SCHEMA so older code can boot. The removed accounts
273
+ * and their data are gone for good; the remaining user becomes the admin.
274
+ */
275
+ export const down = (db: DatabaseConnection): void => {
276
+ debug.log('migration', '🗑️ Restoring multi-user schema...');
277
+ const owner = db.prepare('SELECT id FROM users ORDER BY created_at ASC LIMIT 1').get() as { id: string } | undefined;
278
+
279
+ db.exec(`ALTER TABLE users ADD COLUMN role TEXT NOT NULL DEFAULT 'admin' CHECK(role IN ('admin', 'member'))`);
280
+
281
+ db.exec(`
282
+ CREATE TABLE IF NOT EXISTS invite_tokens (
283
+ id TEXT PRIMARY KEY,
284
+ token_hash TEXT NOT NULL UNIQUE,
285
+ role TEXT NOT NULL CHECK(role IN ('admin', 'member')),
286
+ label TEXT,
287
+ created_by TEXT NOT NULL,
288
+ max_uses INTEGER NOT NULL DEFAULT 1,
289
+ use_count INTEGER NOT NULL DEFAULT 0,
290
+ expires_at TEXT,
291
+ created_at TEXT NOT NULL,
292
+ project_ids TEXT,
293
+ FOREIGN KEY (created_by) REFERENCES users(id) ON DELETE CASCADE
294
+ )
295
+ `);
296
+
297
+ db.exec(`
298
+ CREATE TABLE user_projects (
299
+ user_id TEXT NOT NULL,
300
+ project_id TEXT NOT NULL,
301
+ joined_at TEXT NOT NULL,
302
+ current_session_id TEXT,
303
+ files_panel_state TEXT,
304
+ workspace_state TEXT,
305
+ PRIMARY KEY (user_id, project_id),
306
+ FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE
307
+ )
308
+ `);
309
+ db.exec('CREATE INDEX IF NOT EXISTS idx_user_projects_user_id ON user_projects(user_id)');
310
+ db.exec('CREATE INDEX IF NOT EXISTS idx_user_projects_project_id ON user_projects(project_id)');
311
+ if (owner) {
312
+ db.prepare(`
313
+ INSERT INTO user_projects (user_id, project_id, joined_at, current_session_id, files_panel_state, workspace_state)
314
+ SELECT ?, project_id, joined_at, current_session_id, files_panel_state, workspace_state FROM workspace_projects
315
+ `).run(owner.id);
316
+ }
317
+ db.exec('DROP TABLE workspace_projects');
318
+
319
+ db.exec(`
320
+ CREATE TABLE user_unread_sessions (
321
+ user_id TEXT NOT NULL,
322
+ session_id TEXT NOT NULL,
323
+ project_id TEXT NOT NULL,
324
+ marked_at TEXT NOT NULL,
325
+ PRIMARY KEY (user_id, session_id),
326
+ FOREIGN KEY (session_id) REFERENCES chat_sessions(id) ON DELETE CASCADE
327
+ )
328
+ `);
329
+ db.exec('CREATE INDEX IF NOT EXISTS idx_user_unread_sessions_user_project ON user_unread_sessions(user_id, project_id)');
330
+ if (owner) {
331
+ db.prepare(`
332
+ INSERT INTO user_unread_sessions (user_id, session_id, project_id, marked_at)
333
+ SELECT ?, session_id, project_id, marked_at FROM unread_sessions
334
+ `).run(owner.id);
335
+ }
336
+ db.exec('DROP TABLE unread_sessions');
337
+
338
+ db.exec('DROP INDEX IF EXISTS idx_projects_single_home');
339
+ db.exec('ALTER TABLE projects ADD COLUMN owner_user_id TEXT');
340
+ if (owner) db.prepare(`UPDATE projects SET owner_user_id = ? WHERE kind = 'home'`).run(owner.id);
341
+ db.exec(`CREATE UNIQUE INDEX idx_projects_home_owner ON projects (owner_user_id) WHERE kind = 'home'`);
342
+
343
+ db.exec('ALTER TABLE db_client_connections ADD COLUMN owner_user_id TEXT');
344
+ db.exec('CREATE INDEX IF NOT EXISTS idx_db_client_conn_owner ON db_client_connections(owner_user_id)');
345
+ db.exec('ALTER TABLE ssh_connections ADD COLUMN owner_user_id TEXT');
346
+ db.exec('CREATE INDEX IF NOT EXISTS idx_ssh_conn_owner ON ssh_connections(owner_user_id)');
347
+ if (owner) {
348
+ db.prepare('UPDATE db_client_connections SET owner_user_id = ?').run(owner.id);
349
+ db.prepare('UPDATE ssh_connections SET owner_user_id = ?').run(owner.id);
350
+ }
351
+
352
+ db.exec(`ALTER TABLE channel_links ADD COLUMN status TEXT NOT NULL DEFAULT 'approved' CHECK (status IN ('pending', 'approved'))`);
353
+
354
+ db.exec('ALTER TABLE chat_sessions ADD COLUMN sender_id TEXT');
355
+ db.exec('ALTER TABLE chat_sessions ADD COLUMN sender_name TEXT');
356
+
357
+ // Git identities need an owner; without one there is nobody to give them to.
358
+ db.exec(`
359
+ CREATE TABLE git_identities_old (
360
+ id TEXT PRIMARY KEY,
361
+ owner_user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
362
+ label TEXT NOT NULL,
363
+ name TEXT NOT NULL,
364
+ email TEXT NOT NULL,
365
+ auth_method TEXT NOT NULL DEFAULT 'none'
366
+ CHECK (auth_method IN ('none','ssh-key','https-token','https-account')),
367
+ ssh_private_key TEXT,
368
+ ssh_public_key TEXT,
369
+ ssh_passphrase TEXT,
370
+ https_username TEXT,
371
+ https_token TEXT,
372
+ integration_account_id TEXT REFERENCES integration_accounts(id) ON DELETE SET NULL,
373
+ hosts TEXT NOT NULL DEFAULT '[]',
374
+ is_default INTEGER NOT NULL DEFAULT 0,
375
+ source TEXT NOT NULL DEFAULT 'manual'
376
+ CHECK (source IN ('manual','local-machine')),
377
+ created_at TEXT NOT NULL,
378
+ updated_at TEXT NOT NULL,
379
+ UNIQUE (owner_user_id, label)
380
+ )
381
+ `);
382
+ db.exec(`
383
+ CREATE TABLE git_identity_bindings_old (
384
+ project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
385
+ user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
386
+ identity_id TEXT NOT NULL REFERENCES git_identities_old(id) ON DELETE CASCADE,
387
+ created_at TEXT NOT NULL,
388
+ updated_at TEXT NOT NULL,
389
+ PRIMARY KEY (project_id, user_id)
390
+ )
391
+ `);
392
+ if (owner) {
393
+ db.prepare(`
394
+ INSERT INTO git_identities_old (
395
+ id, owner_user_id, label, name, email, auth_method, ssh_private_key, ssh_public_key, ssh_passphrase,
396
+ https_username, https_token, integration_account_id, hosts, is_default, source, created_at, updated_at
397
+ )
398
+ SELECT
399
+ id, ?, label, name, email, auth_method, ssh_private_key, ssh_public_key, ssh_passphrase,
400
+ https_username, https_token, integration_account_id, hosts, is_default, source, created_at, updated_at
401
+ FROM git_identities
402
+ `).run(owner.id);
403
+ db.prepare(`
404
+ INSERT INTO git_identity_bindings_old (project_id, user_id, identity_id, created_at, updated_at)
405
+ SELECT project_id, ?, identity_id, created_at, updated_at FROM git_identity_bindings
406
+ `).run(owner.id);
407
+ }
408
+ db.exec('DROP TABLE git_identity_bindings');
409
+ db.exec('DROP TABLE git_identities');
410
+ db.exec('ALTER TABLE git_identities_old RENAME TO git_identities');
411
+ db.exec('ALTER TABLE git_identity_bindings_old RENAME TO git_identity_bindings');
412
+ db.exec('CREATE INDEX idx_git_identities_owner ON git_identities (owner_user_id)');
413
+ db.exec('CREATE UNIQUE INDEX idx_git_identities_one_default ON git_identities (owner_user_id) WHERE is_default = 1');
414
+ db.exec(`CREATE UNIQUE INDEX idx_git_identities_one_local ON git_identities (owner_user_id) WHERE source = 'local-machine'`);
415
+ db.exec('CREATE INDEX idx_git_identity_bindings_identity ON git_identity_bindings (identity_id)');
416
+
417
+ debug.log('migration', '✅ Multi-user schema restored');
418
+ };
@@ -0,0 +1,37 @@
1
+ import type { DatabaseConnection } from '$shared/types/database/connection';
2
+ import { debug } from '$shared/utils/logger';
3
+
4
+ export const description = 'Create blob_line_stats to count each pair of file versions once';
5
+
6
+ /**
7
+ * The +/- line counts of one pair of snapshot blobs.
8
+ *
9
+ * Blobs are content-addressed (sha256), so the count for a given
10
+ * (old_hash, new_hash) can never change — it is computed the first time any
11
+ * surface asks and read from here ever after. Every chat open used to re-diff
12
+ * every changed file of every turn, twice; on a chat that touched a large
13
+ * generated file that recomputation alone could block the server for minutes.
14
+ *
15
+ * An empty hash is a side that does not exist (a created or deleted file).
16
+ * Rows are pruned together with the blobs they describe.
17
+ */
18
+ export const up = (db: DatabaseConnection): void => {
19
+ debug.log('migration', '📋 Creating blob_line_stats table...');
20
+ db.exec(`
21
+ CREATE TABLE IF NOT EXISTS blob_line_stats (
22
+ old_hash TEXT NOT NULL,
23
+ new_hash TEXT NOT NULL,
24
+ insertions INTEGER NOT NULL,
25
+ deletions INTEGER NOT NULL,
26
+ is_binary INTEGER NOT NULL DEFAULT 0,
27
+ PRIMARY KEY (old_hash, new_hash)
28
+ ) WITHOUT ROWID
29
+ `);
30
+ db.exec('CREATE INDEX IF NOT EXISTS idx_blob_line_stats_new_hash ON blob_line_stats (new_hash)');
31
+ debug.log('migration', '✅ blob_line_stats table created');
32
+ };
33
+
34
+ export const down = (db: DatabaseConnection): void => {
35
+ db.exec('DROP INDEX IF EXISTS idx_blob_line_stats_new_hash');
36
+ db.exec('DROP TABLE IF EXISTS blob_line_stats');
37
+ };
@@ -82,6 +82,10 @@ import * as migration080 from './080_create_file_shares';
82
82
  import * as migration081 from './081_create_push_subscriptions';
83
83
  import * as migration082 from './082_create_git_identities';
84
84
  import * as migration083 from './083_create_project_mcp_trust';
85
+ import * as migration084 from './084_create_channel_links';
86
+ import * as migration085 from './085_add_home_projects';
87
+ import * as migration086 from './086_single_owner';
88
+ import * as migration087 from './087_create_blob_line_stats';
85
89
 
86
90
  // Export all migrations in order
87
91
  export const migrations = [
@@ -582,6 +586,30 @@ export const migrations = [
582
586
  description: migration083.description,
583
587
  up: migration083.up,
584
588
  down: migration083.down
589
+ },
590
+ {
591
+ id: '084',
592
+ description: migration084.description,
593
+ up: migration084.up,
594
+ down: migration084.down
595
+ },
596
+ {
597
+ id: '085',
598
+ description: migration085.description,
599
+ up: migration085.up,
600
+ down: migration085.down
601
+ },
602
+ {
603
+ id: '086',
604
+ description: migration086.description,
605
+ up: migration086.up,
606
+ down: migration086.down
607
+ },
608
+ {
609
+ id: '087',
610
+ description: migration087.description,
611
+ up: migration087.up,
612
+ down: migration087.down
585
613
  }
586
614
  ];
587
615
 
@@ -5,7 +5,6 @@ export interface DBUser {
5
5
  name: string;
6
6
  color: string;
7
7
  avatar: string;
8
- role: 'admin' | 'member';
9
8
  personal_access_token_hash: string | null;
10
9
  created_at: string;
11
10
  updated_at: string;
@@ -20,24 +19,10 @@ export interface DBAuthSession {
20
19
  last_active_at: string;
21
20
  user_agent: string | null;
22
21
  ip_address: string | null;
23
- /** How the session was created: 'setup' | 'invite' | 'device-link' | 'login' | 'pat' | 'no-auth'. */
22
+ /** How the session was created: 'setup' | 'device-link' | 'login' | 'pat' | 'no-auth'. */
24
23
  source: string | null;
25
24
  }
26
25
 
27
- export interface DBInviteToken {
28
- id: string;
29
- token_hash: string;
30
- role: 'admin' | 'member';
31
- label: string | null;
32
- created_by: string;
33
- max_uses: number;
34
- use_count: number;
35
- expires_at: string | null;
36
- created_at: string;
37
- /** JSON array of project ids to grant the new member on join (null = none). */
38
- project_ids: string | null;
39
- }
40
-
41
26
  export interface DBDeviceCode {
42
27
  id: string;
43
28
  code_hash: string;
@@ -55,14 +40,13 @@ export const authQueries = {
55
40
  const db = getDatabase();
56
41
  const now = new Date().toISOString();
57
42
  db.prepare(`
58
- INSERT INTO users (id, name, color, avatar, role, personal_access_token_hash, created_at, updated_at)
59
- VALUES (?, ?, ?, ?, ?, ?, ?, ?)
43
+ INSERT INTO users (id, name, color, avatar, personal_access_token_hash, created_at, updated_at)
44
+ VALUES (?, ?, ?, ?, ?, ?, ?)
60
45
  `).run(
61
46
  user.id,
62
47
  user.name,
63
48
  user.color,
64
49
  user.avatar,
65
- user.role,
66
50
  user.personal_access_token_hash,
67
51
  user.created_at,
68
52
  user.updated_at ?? now
@@ -80,9 +64,10 @@ export const authQueries = {
80
64
  return db.prepare('SELECT * FROM users WHERE personal_access_token_hash = ?').get(hash) as DBUser | null;
81
65
  },
82
66
 
83
- getAllUsers(): DBUser[] {
67
+ /** The owner — the install's only account — or null before setup. */
68
+ getOwner(): DBUser | null {
84
69
  const db = getDatabase();
85
- return db.prepare('SELECT * FROM users ORDER BY created_at ASC').all() as DBUser[];
70
+ return db.prepare('SELECT * FROM users ORDER BY created_at ASC LIMIT 1').get() as DBUser | null;
86
71
  },
87
72
 
88
73
  countUsers(): number {
@@ -91,12 +76,6 @@ export const authQueries = {
91
76
  return result.count;
92
77
  },
93
78
 
94
- countAdmins(): number {
95
- const db = getDatabase();
96
- const result = db.prepare("SELECT COUNT(*) as count FROM users WHERE role = 'admin'").get() as { count: number };
97
- return result.count;
98
- },
99
-
100
79
  updateUser(id: string, fields: Partial<Pick<DBUser, 'name' | 'color' | 'avatar' | 'personal_access_token_hash'>>): void {
101
80
  const db = getDatabase();
102
81
  const now = new Date().toISOString();
@@ -112,11 +91,6 @@ export const authQueries = {
112
91
  db.prepare(`UPDATE users SET ${sets.join(', ')} WHERE id = ?`).run(...values);
113
92
  },
114
93
 
115
- deleteUser(id: string): void {
116
- const db = getDatabase();
117
- db.prepare('DELETE FROM users WHERE id = ?').run(id);
118
- },
119
-
120
94
  // ===================== Auth Sessions =====================
121
95
 
122
96
  createSession(session: DBAuthSession): DBAuthSession {
@@ -143,12 +117,7 @@ export const authQueries = {
143
117
  return db.prepare('SELECT * FROM auth_sessions WHERE token_hash = ?').get(hash) as DBAuthSession | null;
144
118
  },
145
119
 
146
- getSessionsByUserId(userId: string): DBAuthSession[] {
147
- const db = getDatabase();
148
- return db.prepare('SELECT * FROM auth_sessions WHERE user_id = ? ORDER BY last_active_at DESC').all(userId) as DBAuthSession[];
149
- },
150
-
151
- /** All sessions across every user (admin "Connected devices" view). */
120
+ /** Every signed-in device. */
152
121
  getAllSessions(): DBAuthSession[] {
153
122
  const db = getDatabase();
154
123
  return db.prepare('SELECT * FROM auth_sessions ORDER BY last_active_at DESC').all() as DBAuthSession[];
@@ -175,11 +144,6 @@ export const authQueries = {
175
144
  db.prepare('DELETE FROM auth_sessions WHERE token_hash = ?').run(hash);
176
145
  },
177
146
 
178
- deleteSessionsByUserId(userId: string): void {
179
- const db = getDatabase();
180
- db.prepare('DELETE FROM auth_sessions WHERE user_id = ?').run(userId);
181
- },
182
-
183
147
  deleteExpiredSessions(): number {
184
148
  const db = getDatabase();
185
149
  const now = new Date().toISOString();
@@ -187,48 +151,6 @@ export const authQueries = {
187
151
  return result.changes;
188
152
  },
189
153
 
190
- // ===================== Invite Tokens =====================
191
-
192
- createInvite(invite: DBInviteToken): DBInviteToken {
193
- const db = getDatabase();
194
- db.prepare(`
195
- INSERT INTO invite_tokens (id, token_hash, role, label, created_by, max_uses, use_count, expires_at, created_at, project_ids)
196
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
197
- `).run(
198
- invite.id,
199
- invite.token_hash,
200
- invite.role,
201
- invite.label,
202
- invite.created_by,
203
- invite.max_uses,
204
- invite.use_count,
205
- invite.expires_at,
206
- invite.created_at,
207
- invite.project_ids
208
- );
209
- return db.prepare('SELECT * FROM invite_tokens WHERE id = ?').get(invite.id) as DBInviteToken;
210
- },
211
-
212
- getInviteByTokenHash(hash: string): DBInviteToken | null {
213
- const db = getDatabase();
214
- return db.prepare('SELECT * FROM invite_tokens WHERE token_hash = ?').get(hash) as DBInviteToken | null;
215
- },
216
-
217
- incrementUseCount(id: string): void {
218
- const db = getDatabase();
219
- db.prepare('UPDATE invite_tokens SET use_count = use_count + 1 WHERE id = ?').run(id);
220
- },
221
-
222
- getAllInvites(): DBInviteToken[] {
223
- const db = getDatabase();
224
- return db.prepare('SELECT * FROM invite_tokens ORDER BY created_at DESC').all() as DBInviteToken[];
225
- },
226
-
227
- revokeInvite(id: string): void {
228
- const db = getDatabase();
229
- db.prepare('DELETE FROM invite_tokens WHERE id = ?').run(id);
230
- },
231
-
232
154
  deleteAllSessions(): number {
233
155
  const db = getDatabase();
234
156
  const result = db.prepare('DELETE FROM auth_sessions').run() as { changes: number };