@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
@@ -3,28 +3,18 @@
3
3
  */
4
4
 
5
5
  import { t } from 'elysia';
6
- import path from 'node:path';
7
6
  import { createRouter } from '$shared/utils/ws-server';
8
7
  import { gitService } from '../../git/git-service';
9
8
  import { requireProjectWorkspace } from '../access';
10
9
  import { identityEnvFor, identityEnvForRemote } from './identity-context';
11
- import { debug } from '$shared/utils/logger';
10
+ import { gitRepoContext, resolveRepoCwd, withRepoLock } from './repo-context';
12
11
 
13
- /**
14
- * Resolve the working directory for a git operation. Defaults to the project
15
- * root; when `repoPath` is provided it must already be inside the project.
16
- */
17
- function resolveRepoCwd(projectPath: string, repoPath: string | undefined): string {
18
- if (!repoPath) return projectPath;
19
- const resolved = path.resolve(repoPath);
20
- const projectRoot = path.resolve(projectPath);
21
- const sep = path.sep;
22
- if (resolved !== projectRoot && !resolved.startsWith(projectRoot + sep)) {
23
- debug.warn('git', `Rejected nested repoPath outside project: ${resolved}`);
24
- return projectPath;
25
- }
26
- return resolved;
27
- }
12
+ const PUSH_LABELS = {
13
+ 'with-tags': 'Push with tags',
14
+ 'all-tags': 'Push all tags',
15
+ 'force-lease': 'Force push (lease)',
16
+ force: 'Force push'
17
+ } as const;
28
18
 
29
19
  export const remoteHandler = createRouter()
30
20
  .http('git:remotes', {
@@ -53,14 +43,16 @@ export const remoteHandler = createRouter()
53
43
  message: t.String()
54
44
  })
55
45
  }, async ({ data, conn }) => {
56
- const { root } = requireProjectWorkspace(conn, data.projectId);
57
- const cwd = resolveRepoCwd(root, data.repoPath);
58
- const message = await gitService.fetch(
59
- cwd,
60
- data.remote,
61
- await identityEnvForRemote(conn, data.projectId, cwd, data.remote)
62
- );
63
- return { message };
46
+ const ctx = gitRepoContext(conn, data.projectId, data.repoPath);
47
+ const { cwd } = ctx;
48
+ return withRepoLock(ctx, { category: 'fetch', label: 'Fetch', target: data.remote }, async () => {
49
+ const message = await gitService.fetch(
50
+ cwd,
51
+ data.remote,
52
+ await identityEnvForRemote(data.projectId, cwd, data.remote)
53
+ );
54
+ return { message };
55
+ });
64
56
  })
65
57
 
66
58
  .http('git:pull', {
@@ -68,22 +60,91 @@ export const remoteHandler = createRouter()
68
60
  projectId: t.String(),
69
61
  remote: t.Optional(t.String()),
70
62
  branch: t.Optional(t.String()),
63
+ /** Legacy flag, same as `mode: 'rebase'`. */
71
64
  rebase: t.Optional(t.Boolean()),
65
+ mode: t.Optional(t.Union([
66
+ t.Literal('default'),
67
+ t.Literal('merge'),
68
+ t.Literal('rebase'),
69
+ t.Literal('ff-only')
70
+ ])),
71
+ /** Stash local changes around the pull. */
72
+ autostash: t.Optional(t.Boolean()),
72
73
  repoPath: t.Optional(t.String())
73
74
  }),
74
75
  response: t.Object({
75
76
  success: t.Boolean(),
76
- message: t.String()
77
+ message: t.String(),
78
+ reason: t.Optional(t.Union([t.Literal('diverged'), t.Literal('conflict'), t.Literal('local-changes')])),
79
+ ahead: t.Optional(t.Number()),
80
+ behind: t.Optional(t.Number()),
81
+ pulledCount: t.Optional(t.Number())
77
82
  })
78
83
  }, async ({ data, conn }) => {
79
- const { root } = requireProjectWorkspace(conn, data.projectId);
80
- const cwd = resolveRepoCwd(root, data.repoPath);
81
- return await gitService.pull(
82
- cwd,
83
- data.remote,
84
- data.branch,
85
- data.rebase,
86
- await identityEnvForRemote(conn, data.projectId, cwd, data.remote)
84
+ const ctx = gitRepoContext(conn, data.projectId, data.repoPath);
85
+ const mode = data.mode ?? (data.rebase ? 'rebase' : 'default');
86
+ const label = mode === 'rebase' ? 'Pull (rebase)' : mode === 'ff-only' ? 'Pull (fast-forward)' : mode === 'merge' ? 'Pull (merge)' : 'Pull';
87
+ return withRepoLock(ctx, { category: 'pull', label, target: data.branch }, async () =>
88
+ gitService.pull(
89
+ ctx.cwd,
90
+ data.remote,
91
+ data.branch,
92
+ mode,
93
+ data.autostash ?? false,
94
+ await identityEnvForRemote(data.projectId, ctx.cwd, data.remote)
95
+ )
96
+ );
97
+ })
98
+
99
+ // What a force pull would throw away — fetched fresh, for the confirmation.
100
+ .http('git:force-pull-preview', {
101
+ data: t.Object({
102
+ projectId: t.String(),
103
+ remote: t.Optional(t.String()),
104
+ branch: t.Optional(t.String()),
105
+ repoPath: t.Optional(t.String())
106
+ }),
107
+ response: t.Object({
108
+ target: t.String(),
109
+ localCommits: t.Number(),
110
+ incomingCommits: t.Number(),
111
+ changedFiles: t.Number(),
112
+ collidingUntracked: t.Array(t.String())
113
+ })
114
+ }, async ({ data, conn }) => {
115
+ const ctx = gitRepoContext(conn, data.projectId, data.repoPath);
116
+ return withRepoLock(ctx, { category: 'fetch', label: 'Fetch', target: data.branch }, async () =>
117
+ gitService.forcePullPreview(
118
+ ctx.cwd,
119
+ data.remote,
120
+ data.branch,
121
+ await identityEnvForRemote(data.projectId, ctx.cwd, data.remote)
122
+ )
123
+ );
124
+ })
125
+
126
+ .http('git:force-pull', {
127
+ data: t.Object({
128
+ projectId: t.String(),
129
+ remote: t.Optional(t.String()),
130
+ branch: t.Optional(t.String()),
131
+ repoPath: t.Optional(t.String())
132
+ }),
133
+ response: t.Object({
134
+ previousHead: t.String(),
135
+ newHead: t.String(),
136
+ discardedCommits: t.Number(),
137
+ stashMessage: t.Optional(t.String())
138
+ })
139
+ }, async ({ data, conn }) => {
140
+ const ctx = gitRepoContext(conn, data.projectId, data.repoPath);
141
+ return withRepoLock(ctx, { category: 'pull', label: 'Force pull', target: data.branch }, async () =>
142
+ gitService.forcePull(
143
+ ctx.cwd,
144
+ data.remote,
145
+ data.branch,
146
+ await identityEnvForRemote(data.projectId, ctx.cwd, data.remote)
147
+ )
87
148
  );
88
149
  })
89
150
 
@@ -105,15 +166,17 @@ export const remoteHandler = createRouter()
105
166
  message: t.String()
106
167
  })
107
168
  }, async ({ data, conn }) => {
108
- const { root } = requireProjectWorkspace(conn, data.projectId);
109
- const cwd = resolveRepoCwd(root, data.repoPath);
110
- return await gitService.pushAdvanced(
111
- cwd,
112
- data.mode,
113
- data.remote,
114
- data.branch,
115
- await identityEnvForRemote(conn, data.projectId, cwd, data.remote)
116
- );
169
+ const ctx = gitRepoContext(conn, data.projectId, data.repoPath);
170
+ const { cwd } = ctx;
171
+ return withRepoLock(ctx, { category: 'push', label: PUSH_LABELS[data.mode], target: data.branch }, async () => {
172
+ return await gitService.pushAdvanced(
173
+ cwd,
174
+ data.mode,
175
+ data.remote,
176
+ data.branch,
177
+ await identityEnvForRemote(data.projectId, cwd, data.remote)
178
+ );
179
+ });
117
180
  })
118
181
 
119
182
  .http('git:fetch-all', {
@@ -125,13 +188,15 @@ export const remoteHandler = createRouter()
125
188
  message: t.String()
126
189
  })
127
190
  }, async ({ data, conn }) => {
128
- const { root } = requireProjectWorkspace(conn, data.projectId);
129
- const cwd = resolveRepoCwd(root, data.repoPath);
130
- const message = await gitService.fetchAll(
131
- cwd,
132
- await identityEnvForRemote(conn, data.projectId, cwd)
133
- );
134
- return { message };
191
+ const ctx = gitRepoContext(conn, data.projectId, data.repoPath);
192
+ const { cwd } = ctx;
193
+ return withRepoLock(ctx, { category: 'fetch', label: 'Fetch all' }, async () => {
194
+ const message = await gitService.fetchAll(
195
+ cwd,
196
+ await identityEnvForRemote(data.projectId, cwd)
197
+ );
198
+ return { message };
199
+ });
135
200
  })
136
201
 
137
202
  .http('git:push', {
@@ -152,16 +217,18 @@ export const remoteHandler = createRouter()
152
217
  message: t.String()
153
218
  })
154
219
  }, async ({ data, conn }) => {
155
- const { root } = requireProjectWorkspace(conn, data.projectId);
156
- const cwd = resolveRepoCwd(root, data.repoPath);
157
- return await gitService.push(
158
- cwd,
159
- data.remote,
160
- data.branch,
161
- data.force,
162
- { useUpstream: data.useUpstream ?? true },
163
- await identityEnvForRemote(conn, data.projectId, cwd, data.remote)
164
- );
220
+ const ctx = gitRepoContext(conn, data.projectId, data.repoPath);
221
+ const { cwd } = ctx;
222
+ return withRepoLock(ctx, { category: 'push', label: data.force ? 'Force push (lease)' : 'Push', target: data.branch }, async () => {
223
+ return await gitService.push(
224
+ cwd,
225
+ data.remote,
226
+ data.branch,
227
+ data.force,
228
+ { useUpstream: data.useUpstream ?? true },
229
+ await identityEnvForRemote(data.projectId, cwd, data.remote)
230
+ );
231
+ });
165
232
  })
166
233
 
167
234
  /** Where a push would actually land — drives the Push button's label. */
@@ -194,10 +261,12 @@ export const remoteHandler = createRouter()
194
261
  }),
195
262
  response: t.Object({ ok: t.Boolean() })
196
263
  }, async ({ data, conn }) => {
197
- const { root } = requireProjectWorkspace(conn, data.projectId);
198
- const cwd = resolveRepoCwd(root, data.repoPath);
199
- await gitService.setUpstream(cwd, data.branch, data.remote, data.remoteBranch);
200
- return { ok: true };
264
+ const ctx = gitRepoContext(conn, data.projectId, data.repoPath);
265
+ const { cwd } = ctx;
266
+ return withRepoLock(ctx, { category: 'configure', label: 'Set upstream', target: data.branch }, async () => {
267
+ await gitService.setUpstream(cwd, data.branch, data.remote, data.remoteBranch);
268
+ return { ok: true };
269
+ });
201
270
  })
202
271
 
203
272
  .http('git:unset-upstream', {
@@ -208,10 +277,12 @@ export const remoteHandler = createRouter()
208
277
  }),
209
278
  response: t.Object({ ok: t.Boolean() })
210
279
  }, async ({ data, conn }) => {
211
- const { root } = requireProjectWorkspace(conn, data.projectId);
212
- const cwd = resolveRepoCwd(root, data.repoPath);
213
- await gitService.unsetUpstream(cwd, data.branch);
214
- return { ok: true };
280
+ const ctx = gitRepoContext(conn, data.projectId, data.repoPath);
281
+ const { cwd } = ctx;
282
+ return withRepoLock(ctx, { category: 'configure', label: 'Clear upstream', target: data.branch }, async () => {
283
+ await gitService.unsetUpstream(cwd, data.branch);
284
+ return { ok: true };
285
+ });
215
286
  })
216
287
 
217
288
  .http('git:add-remote', {
@@ -223,10 +294,12 @@ export const remoteHandler = createRouter()
223
294
  }),
224
295
  response: t.Object({ ok: t.Boolean() })
225
296
  }, async ({ data, conn }) => {
226
- const { root } = requireProjectWorkspace(conn, data.projectId);
227
- const cwd = resolveRepoCwd(root, data.repoPath);
228
- await gitService.addRemote(cwd, data.name, data.url);
229
- return { ok: true };
297
+ const ctx = gitRepoContext(conn, data.projectId, data.repoPath);
298
+ const { cwd } = ctx;
299
+ return withRepoLock(ctx, { category: 'configure', label: 'Add remote', target: data.name }, async () => {
300
+ await gitService.addRemote(cwd, data.name, data.url);
301
+ return { ok: true };
302
+ });
230
303
  })
231
304
 
232
305
  .http('git:set-remote-url', {
@@ -238,10 +311,12 @@ export const remoteHandler = createRouter()
238
311
  }),
239
312
  response: t.Object({ ok: t.Boolean() })
240
313
  }, async ({ data, conn }) => {
241
- const { root } = requireProjectWorkspace(conn, data.projectId);
242
- const cwd = resolveRepoCwd(root, data.repoPath);
243
- await gitService.setRemoteUrl(cwd, data.name, data.url);
244
- return { ok: true };
314
+ const ctx = gitRepoContext(conn, data.projectId, data.repoPath);
315
+ const { cwd } = ctx;
316
+ return withRepoLock(ctx, { category: 'configure', label: 'Edit remote', target: data.name }, async () => {
317
+ await gitService.setRemoteUrl(cwd, data.name, data.url);
318
+ return { ok: true };
319
+ });
245
320
  })
246
321
 
247
322
  .http('git:rename-remote', {
@@ -253,10 +328,12 @@ export const remoteHandler = createRouter()
253
328
  }),
254
329
  response: t.Object({ ok: t.Boolean() })
255
330
  }, async ({ data, conn }) => {
256
- const { root } = requireProjectWorkspace(conn, data.projectId);
257
- const cwd = resolveRepoCwd(root, data.repoPath);
258
- await gitService.renameRemote(cwd, data.oldName, data.newName);
259
- return { ok: true };
331
+ const ctx = gitRepoContext(conn, data.projectId, data.repoPath);
332
+ const { cwd } = ctx;
333
+ return withRepoLock(ctx, { category: 'configure', label: 'Rename remote', target: data.oldName }, async () => {
334
+ await gitService.renameRemote(cwd, data.oldName, data.newName);
335
+ return { ok: true };
336
+ });
260
337
  })
261
338
 
262
339
  .http('git:edit-remote', {
@@ -269,13 +346,15 @@ export const remoteHandler = createRouter()
269
346
  }),
270
347
  response: t.Object({ ok: t.Boolean() })
271
348
  }, async ({ data, conn }) => {
272
- const { root } = requireProjectWorkspace(conn, data.projectId);
273
- const cwd = resolveRepoCwd(root, data.repoPath);
274
- if (data.oldName !== data.newName) {
275
- await gitService.renameRemote(cwd, data.oldName, data.newName);
276
- }
277
- await gitService.setRemoteUrl(cwd, data.newName, data.newUrl);
278
- return { ok: true };
349
+ const ctx = gitRepoContext(conn, data.projectId, data.repoPath);
350
+ const { cwd } = ctx;
351
+ return withRepoLock(ctx, { category: 'configure', label: 'Edit remote', target: data.oldName }, async () => {
352
+ if (data.oldName !== data.newName) {
353
+ await gitService.renameRemote(cwd, data.oldName, data.newName);
354
+ }
355
+ await gitService.setRemoteUrl(cwd, data.newName, data.newUrl);
356
+ return { ok: true };
357
+ });
279
358
  })
280
359
 
281
360
  .http('git:remove-remote', {
@@ -286,10 +365,12 @@ export const remoteHandler = createRouter()
286
365
  }),
287
366
  response: t.Object({ ok: t.Boolean() })
288
367
  }, async ({ data, conn }) => {
289
- const { root } = requireProjectWorkspace(conn, data.projectId);
290
- const cwd = resolveRepoCwd(root, data.repoPath);
291
- await gitService.removeRemote(cwd, data.name);
292
- return { ok: true };
368
+ const ctx = gitRepoContext(conn, data.projectId, data.repoPath);
369
+ const { cwd } = ctx;
370
+ return withRepoLock(ctx, { category: 'configure', label: 'Remove remote', target: data.name }, async () => {
371
+ await gitService.removeRemote(cwd, data.name);
372
+ return { ok: true };
373
+ });
293
374
  })
294
375
 
295
376
  .http('git:delete-remote-branch', {
@@ -301,15 +382,17 @@ export const remoteHandler = createRouter()
301
382
  }),
302
383
  response: t.Object({ ok: t.Boolean() })
303
384
  }, async ({ data, conn }) => {
304
- const { root } = requireProjectWorkspace(conn, data.projectId);
305
- const cwd = resolveRepoCwd(root, data.repoPath);
306
- await gitService.deleteRemoteBranch(
307
- cwd,
308
- data.remote,
309
- data.branch,
310
- await identityEnvForRemote(conn, data.projectId, cwd, data.remote)
311
- );
312
- return { ok: true };
385
+ const ctx = gitRepoContext(conn, data.projectId, data.repoPath);
386
+ const { cwd } = ctx;
387
+ return withRepoLock(ctx, { category: 'branch', cls: 'network', label: 'Delete remote branch', target: `${data.remote}/${data.branch}` }, async () => {
388
+ await gitService.deleteRemoteBranch(
389
+ cwd,
390
+ data.remote,
391
+ data.branch,
392
+ await identityEnvForRemote(data.projectId, cwd, data.remote)
393
+ );
394
+ return { ok: true };
395
+ });
313
396
  })
314
397
 
315
398
  .http('git:stash-list', {
@@ -333,19 +416,24 @@ export const remoteHandler = createRouter()
333
416
  projectId: t.String(),
334
417
  message: t.Optional(t.String()),
335
418
  staged: t.Optional(t.Boolean()),
419
+ /** Stash untracked files too (`--include-untracked`). */
420
+ includeUntracked: t.Optional(t.Boolean()),
336
421
  repoPath: t.Optional(t.String())
337
422
  }),
338
423
  response: t.Object({ ok: t.Boolean() })
339
424
  }, async ({ data, conn }) => {
340
- const { root } = requireProjectWorkspace(conn, data.projectId);
341
- const cwd = resolveRepoCwd(root, data.repoPath);
342
- await gitService.stashSave(
343
- cwd,
344
- data.message,
345
- data.staged,
346
- identityEnvFor(conn, data.projectId)
347
- );
348
- return { ok: true };
425
+ const ctx = gitRepoContext(conn, data.projectId, data.repoPath);
426
+ const { cwd } = ctx;
427
+ return withRepoLock(ctx, { category: 'stash', label: 'Stash' }, async () => {
428
+ await gitService.stashSave(
429
+ cwd,
430
+ data.message,
431
+ data.staged,
432
+ identityEnvFor(data.projectId),
433
+ data.includeUntracked
434
+ );
435
+ return { ok: true };
436
+ });
349
437
  })
350
438
 
351
439
  .http('git:stash-pop', {
@@ -360,9 +448,11 @@ export const remoteHandler = createRouter()
360
448
  message: t.String()
361
449
  })
362
450
  }, async ({ data, conn }) => {
363
- const { root } = requireProjectWorkspace(conn, data.projectId);
364
- const cwd = resolveRepoCwd(root, data.repoPath);
365
- return await gitService.stashPop(cwd, data.index);
451
+ const ctx = gitRepoContext(conn, data.projectId, data.repoPath);
452
+ const { cwd } = ctx;
453
+ return withRepoLock(ctx, { category: 'stash', label: 'Pop stash' }, async () => {
454
+ return await gitService.stashPop(cwd, data.index);
455
+ });
366
456
  })
367
457
 
368
458
  .http('git:stash-apply', {
@@ -377,9 +467,11 @@ export const remoteHandler = createRouter()
377
467
  message: t.String()
378
468
  })
379
469
  }, async ({ data, conn }) => {
380
- const { root } = requireProjectWorkspace(conn, data.projectId);
381
- const cwd = resolveRepoCwd(root, data.repoPath);
382
- return await gitService.stashApply(cwd, data.index);
470
+ const ctx = gitRepoContext(conn, data.projectId, data.repoPath);
471
+ const { cwd } = ctx;
472
+ return withRepoLock(ctx, { category: 'stash', label: 'Apply stash' }, async () => {
473
+ return await gitService.stashApply(cwd, data.index);
474
+ });
383
475
  })
384
476
 
385
477
  .http('git:stash-drop', {
@@ -390,10 +482,12 @@ export const remoteHandler = createRouter()
390
482
  }),
391
483
  response: t.Object({ ok: t.Boolean() })
392
484
  }, async ({ data, conn }) => {
393
- const { root } = requireProjectWorkspace(conn, data.projectId);
394
- const cwd = resolveRepoCwd(root, data.repoPath);
395
- await gitService.stashDrop(cwd, data.index);
396
- return { ok: true };
485
+ const ctx = gitRepoContext(conn, data.projectId, data.repoPath);
486
+ const { cwd } = ctx;
487
+ return withRepoLock(ctx, { category: 'stash', label: 'Drop stash' }, async () => {
488
+ await gitService.stashDrop(cwd, data.index);
489
+ return { ok: true };
490
+ });
397
491
  })
398
492
 
399
493
  .http('git:stash-diff', {
@@ -455,16 +549,18 @@ export const remoteHandler = createRouter()
455
549
  }),
456
550
  response: t.Object({ ok: t.Boolean() })
457
551
  }, async ({ data, conn }) => {
458
- const { root } = requireProjectWorkspace(conn, data.projectId);
459
- const cwd = resolveRepoCwd(root, data.repoPath);
460
- await gitService.createTag(
461
- cwd,
462
- data.name,
463
- data.message,
464
- data.commitHash,
465
- identityEnvFor(conn, data.projectId)
466
- );
467
- return { ok: true };
552
+ const ctx = gitRepoContext(conn, data.projectId, data.repoPath);
553
+ const { cwd } = ctx;
554
+ return withRepoLock(ctx, { category: 'tag', label: 'Create tag', target: data.name }, async () => {
555
+ await gitService.createTag(
556
+ cwd,
557
+ data.name,
558
+ data.message,
559
+ data.commitHash,
560
+ identityEnvFor(data.projectId)
561
+ );
562
+ return { ok: true };
563
+ });
468
564
  })
469
565
 
470
566
  .http('git:delete-tag', {
@@ -475,10 +571,12 @@ export const remoteHandler = createRouter()
475
571
  }),
476
572
  response: t.Object({ ok: t.Boolean() })
477
573
  }, async ({ data, conn }) => {
478
- const { root } = requireProjectWorkspace(conn, data.projectId);
479
- const cwd = resolveRepoCwd(root, data.repoPath);
480
- await gitService.deleteTag(cwd, data.name);
481
- return { ok: true };
574
+ const ctx = gitRepoContext(conn, data.projectId, data.repoPath);
575
+ const { cwd } = ctx;
576
+ return withRepoLock(ctx, { category: 'tag', label: 'Delete tag', target: data.name }, async () => {
577
+ await gitService.deleteTag(cwd, data.name);
578
+ return { ok: true };
579
+ });
482
580
  })
483
581
 
484
582
  .http('git:push-tag', {
@@ -493,12 +591,14 @@ export const remoteHandler = createRouter()
493
591
  message: t.String()
494
592
  })
495
593
  }, async ({ data, conn }) => {
496
- const { root } = requireProjectWorkspace(conn, data.projectId);
497
- const cwd = resolveRepoCwd(root, data.repoPath);
498
- return await gitService.pushTag(
499
- cwd,
500
- data.name,
501
- data.remote,
502
- await identityEnvForRemote(conn, data.projectId, cwd, data.remote)
503
- );
594
+ const ctx = gitRepoContext(conn, data.projectId, data.repoPath);
595
+ const { cwd } = ctx;
596
+ return withRepoLock(ctx, { category: 'tag', label: 'Push tag', target: data.name }, async () => {
597
+ return await gitService.pushTag(
598
+ cwd,
599
+ data.name,
600
+ data.remote,
601
+ await identityEnvForRemote(data.projectId, cwd, data.remote)
602
+ );
603
+ });
504
604
  });
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Shared resolution of "which repository does this request address", plus the
3
+ * operation lock every mutating git handler runs under.
4
+ */
5
+
6
+ import path from 'node:path';
7
+ import type { WSConnection } from '$shared/utils/ws-server';
8
+ import { makeScopeKey } from '$shared/utils/workspace-scope';
9
+ import { debug } from '$shared/utils/logger';
10
+ import { requireProjectWorkspace } from '../access';
11
+ import { withGitOpLock, type GitOpSpec } from '../../git/op-lock';
12
+
13
+ /**
14
+ * Resolve the working directory for a git operation. Defaults to the project
15
+ * root; when `repoPath` is provided it must already be inside the project
16
+ * (these paths come from the backend's own `getBranches` response, but we
17
+ * re-validate to defend against a tampered/mismatched client).
18
+ */
19
+ export function resolveRepoCwd(projectPath: string, repoPath: string | undefined): string {
20
+ if (!repoPath) return projectPath;
21
+ const resolved = path.resolve(repoPath);
22
+ const projectRoot = path.resolve(projectPath);
23
+ if (resolved !== projectRoot && !resolved.startsWith(projectRoot + path.sep)) {
24
+ debug.warn('git', `Rejected nested repoPath outside project: ${resolved}`);
25
+ return projectPath;
26
+ }
27
+ return resolved;
28
+ }
29
+
30
+ export interface GitRepoContext {
31
+ projectId: string;
32
+ /** The project's own directory — differs from `root` inside a worktree. */
33
+ projectRoot: string;
34
+ /** Root of the workspace the connection is viewing (project or worktree). */
35
+ root: string;
36
+ /** Working directory of the addressed repository. */
37
+ cwd: string;
38
+ /** Workspace scope key — the same key `git:changed` uses. */
39
+ scope: string;
40
+ /** Nested repo path as clients address it; absent for the workspace's own repo. */
41
+ repoPath?: string;
42
+ }
43
+
44
+ /** Resolve the repository a request addresses. `cwd` overrides `repoPath` when known. */
45
+ export function gitRepoContext(
46
+ conn: WSConnection,
47
+ projectId: string,
48
+ repoPath?: string,
49
+ cwdOverride?: string
50
+ ): GitRepoContext {
51
+ const { project, root, worktreeId } = requireProjectWorkspace(conn, projectId);
52
+ const cwd = cwdOverride ?? resolveRepoCwd(root, repoPath);
53
+ const isRoot = path.resolve(cwd) === path.resolve(root);
54
+ return {
55
+ projectId: project.id,
56
+ projectRoot: project.path,
57
+ root,
58
+ cwd,
59
+ scope: makeScopeKey(project.id, worktreeId),
60
+ ...(!isRoot && { repoPath: cwd })
61
+ };
62
+ }
63
+
64
+ /** Run `fn` holding the repository's operation lock (see `op-lock.ts`). */
65
+ export function withRepoLock<T>(ctx: GitRepoContext, spec: GitOpSpec, fn: () => Promise<T>): Promise<T> {
66
+ return withGitOpLock({ cwd: path.resolve(ctx.cwd), scope: ctx.scope, repoPath: ctx.repoPath }, spec, fn);
67
+ }