@dexhq/canvas 0.13.0 → 0.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (341) hide show
  1. package/CHANGELOG.md +53 -0
  2. package/README.md +21 -18
  3. package/app/canvas/activity.tsx +8 -15
  4. package/app/canvas/anchoring.ts +3 -5
  5. package/app/canvas/canvas-input.ts +151 -14
  6. package/app/canvas/canvas-keys.ts +9 -0
  7. package/app/canvas/canvas-model.ts +5 -42
  8. package/app/canvas/canvas-panels.ts +1 -17
  9. package/app/canvas/canvas-scene.ts +7 -3
  10. package/app/canvas/canvas-ui.ts +2 -1
  11. package/app/canvas/canvas.css +61 -26
  12. package/app/canvas/change-notice.tsx +10 -1
  13. package/app/canvas/comment-layer.tsx +2 -1
  14. package/app/canvas/comments.tsx +204 -82
  15. package/app/canvas/element-picker.ts +200 -27
  16. package/app/canvas/engine/canvas-gestures.ts +72 -17
  17. package/app/canvas/engine/canvas-store.ts +17 -1
  18. package/app/canvas/engine/engine.css +6 -3
  19. package/app/canvas/find-bar.tsx +817 -0
  20. package/app/canvas/find-model.ts +135 -0
  21. package/app/canvas/flow-canvas.tsx +18 -2
  22. package/app/canvas/follow-pin.ts +26 -6
  23. package/app/canvas/frame-node.tsx +43 -26
  24. package/app/canvas/imported-reviews.tsx +12 -22
  25. package/app/canvas/layout.ts +27 -0
  26. package/app/canvas/live-page.ts +142 -19
  27. package/app/canvas/page-find.ts +220 -0
  28. package/app/canvas/page-style.ts +23 -0
  29. package/app/canvas/section-node.tsx +2 -0
  30. package/app/canvas/spec-threads.ts +24 -23
  31. package/app/canvas/thread-focus.tsx +14 -12
  32. package/app/canvas/tool-cursors.ts +16 -16
  33. package/app/canvas/use-activity.ts +0 -8
  34. package/app/canvas/use-agent-changes.tsx +34 -40
  35. package/app/canvas/use-canvas-clipboard.tsx +11 -14
  36. package/app/canvas/xray.ts +3 -12
  37. package/app/frame.tsx +5 -0
  38. package/app/i18n/locales/en/messages.po +114 -12
  39. package/app/i18n/locales/en/messages.ts +1 -1
  40. package/app/i18n/locales/pseudo/messages.po +114 -12
  41. package/app/i18n/locales/pseudo/messages.ts +1 -1
  42. package/app/icons/{sets/lucide.tsx → glyphs.tsx} +7 -5
  43. package/app/icons/index.tsx +3 -3
  44. package/app/icons/types.ts +3 -10
  45. package/app/index.html +3 -3
  46. package/app/main.tsx +3 -0
  47. package/app/pages/canvas-settings.tsx +35 -0
  48. package/app/pages/canvas.tsx +27 -8
  49. package/app/pages/commands-settings.tsx +16 -2
  50. package/app/shell/about.tsx +5 -4
  51. package/app/shell/feedback-details.ts +0 -3
  52. package/app/shell/feedback-form.tsx +10 -21
  53. package/app/shell/feedback-link.tsx +2 -1
  54. package/app/shell/instructions-editor.tsx +1 -1
  55. package/app/shell/presence-style.ts +59 -0
  56. package/app/shell/presence.tsx +6 -3
  57. package/app/shell/scaled-frame.tsx +21 -9
  58. package/app/shell/settings-sections.ts +12 -0
  59. package/app/shell/sidebar.tsx +1 -1
  60. package/app/studio-ui.d.ts +0 -9
  61. package/app/types.ts +21 -205
  62. package/app/ui/file-drop.ts +128 -0
  63. package/app/ui/prompt-editor.tsx +1 -1
  64. package/app/ui/shadcn/input-group.tsx +2 -2
  65. package/dist/cli/banner.d.mts +4 -2
  66. package/dist/cli/banner.mjs +34 -11
  67. package/dist/cli/commands/app.mjs +19 -11
  68. package/dist/cli/commands/create.d.mts +26 -14
  69. package/dist/cli/commands/create.mjs +13 -7
  70. package/dist/cli/commands/dev.mjs +2 -3
  71. package/dist/cli/commands/hook.mjs +2 -1
  72. package/dist/cli/commands/init.d.mts +11 -6
  73. package/dist/cli/help.d.mts +1 -1
  74. package/dist/cli/help.mjs +3 -3
  75. package/dist/cli/main.d.mts +22 -12
  76. package/dist/config/index.d.mts +6 -5
  77. package/dist/config/project-files.d.mts +6 -3
  78. package/dist/config/project-files.mjs +14 -5
  79. package/dist/config/renamed.d.mts +0 -2
  80. package/dist/config/renamed.mjs +1 -3
  81. package/dist/config/schema.d.mts +0 -5
  82. package/dist/config/schema.mjs +0 -5
  83. package/dist/content/capture.d.mts +2 -2
  84. package/dist/content/capture.mjs +2 -2
  85. package/dist/content/content.d.mts +3 -141
  86. package/dist/content/content.mjs +7 -65
  87. package/dist/content/flow-specs.mjs +1 -1
  88. package/dist/content/prototype-summary.d.mts +3 -39
  89. package/dist/content/prototype-summary.mjs +2 -3
  90. package/dist/content/prototypes.mjs +1 -1
  91. package/dist/content/snapshot-client.mjs +1 -1
  92. package/dist/content/text-replace.d.mts +30 -0
  93. package/dist/content/text-replace.mjs +309 -0
  94. package/dist/content/thread-module.mjs +1 -1
  95. package/dist/content/threads.mjs +1 -1
  96. package/dist/paths.d.mts +0 -2
  97. package/dist/paths.mjs +1 -3
  98. package/dist/server/agent-env.d.mts +5 -1
  99. package/dist/server/agent-env.mjs +7 -2
  100. package/dist/server/agent-session.d.mts +14 -1
  101. package/dist/server/agent-session.mjs +84 -19
  102. package/dist/server/brief.mjs +3 -3
  103. package/dist/server/build-pictures.mjs +1 -1
  104. package/dist/server/channel.d.mts +19 -24
  105. package/dist/server/channel.mjs +70 -37
  106. package/dist/server/claimed-presence.d.mts +18 -0
  107. package/dist/server/claimed-presence.mjs +58 -0
  108. package/dist/server/close-check.mjs +2 -2
  109. package/dist/server/comments/actions.d.mts +153 -0
  110. package/dist/server/comments/actions.mjs +154 -0
  111. package/dist/server/comments/routes.d.mts +25 -0
  112. package/dist/server/comments/routes.mjs +88 -0
  113. package/dist/server/comments/tools.d.mts +6 -0
  114. package/dist/server/comments/tools.mjs +154 -0
  115. package/dist/server/dev-server.mjs +36 -13
  116. package/dist/server/file-errors.d.mts +7 -2
  117. package/dist/server/file-errors.mjs +31 -8
  118. package/dist/server/file-lock.d.mts +2 -2
  119. package/dist/server/file-lock.mjs +130 -36
  120. package/dist/server/mcp.d.mts +0 -2
  121. package/dist/server/mcp.mjs +7 -8
  122. package/dist/server/me.d.mts +1 -1
  123. package/dist/server/open-page.d.mts +22 -0
  124. package/dist/server/open-page.mjs +20 -2
  125. package/dist/server/outcome.d.mts +22 -0
  126. package/dist/server/outcome.mjs +8 -0
  127. package/dist/server/own-copies.mjs +2 -2
  128. package/dist/server/people.d.mts +1 -1
  129. package/dist/server/port.d.mts +12 -0
  130. package/dist/server/port.mjs +66 -14
  131. package/dist/server/presence.d.mts +4 -1
  132. package/dist/server/presence.mjs +2 -2
  133. package/dist/server/requests.d.mts +5 -1
  134. package/dist/server/requests.mjs +32 -47
  135. package/dist/server/studio-for.d.mts +5 -0
  136. package/dist/server/studio-for.mjs +18 -4
  137. package/dist/server/sync-install.d.mts +8 -12
  138. package/dist/server/sync-install.mjs +12 -30
  139. package/dist/server/thread-source.mjs +2 -2
  140. package/dist/server/tools/describe.d.mts +1 -1
  141. package/dist/server/tools/describe.mjs +1 -1
  142. package/dist/server/tools/feedback.d.mts +1 -1
  143. package/dist/server/tools/reading.mjs +13 -4
  144. package/dist/server/tools/show.d.mts +1 -1
  145. package/dist/server/tools/snapshot.d.mts +1 -1
  146. package/dist/server/tools/thread-list.mjs +1 -1
  147. package/dist/server/tools/watch.mjs +5 -3
  148. package/dist/server/vite.mjs +28 -31
  149. package/dist/server/web-app.mjs +7 -10
  150. package/dist/server/work-groups.mjs +1 -1
  151. package/dist/setup/apply.mjs +8 -14
  152. package/dist/setup/create.d.mts +4 -2
  153. package/dist/setup/create.mjs +118 -36
  154. package/dist/setup/design-system-checks.mjs +3 -12
  155. package/dist/setup/desktop-app-script.mjs +4 -3
  156. package/dist/setup/desktop-app-start.d.mts +8 -2
  157. package/dist/setup/desktop-app-start.mjs +64 -29
  158. package/dist/setup/desktop-app.d.mts +8 -0
  159. package/dist/setup/desktop-app.mjs +20 -4
  160. package/dist/setup/detect.d.mts +14 -7
  161. package/dist/setup/detect.mjs +56 -10
  162. package/dist/setup/doctor.mjs +2 -4
  163. package/dist/setup/existing-setup.d.mts +1 -3
  164. package/dist/setup/existing-setup.mjs +10 -11
  165. package/dist/setup/migrate/row-titles.mjs +1 -1
  166. package/dist/setup/own-app.d.mts +1 -2
  167. package/dist/setup/own-app.mjs +4 -5
  168. package/dist/setup/plan.d.mts +8 -6
  169. package/dist/setup/plan.mjs +64 -48
  170. package/dist/setup/project.d.mts +6 -17
  171. package/dist/setup/project.mjs +17 -61
  172. package/dist/setup/prompts.d.mts +5 -5
  173. package/dist/setup/prompts.mjs +28 -61
  174. package/dist/setup/questions.d.mts +12 -4
  175. package/dist/setup/questions.mjs +10 -25
  176. package/dist/setup/starters.d.mts +2 -0
  177. package/dist/setup/starters.mjs +26 -9
  178. package/dist/setup/workspace-setup.d.mts +1 -2
  179. package/dist/setup/workspace-setup.mjs +4 -6
  180. package/dist/setup/write.d.mts +15 -5
  181. package/dist/setup/write.mjs +35 -9
  182. package/dist/shared/annotations-core.mjs +1 -1
  183. package/dist/shared/canvas-core.mjs +1 -1
  184. package/dist/shared/canvas-file.d.mts +201 -0
  185. package/dist/shared/canvas-file.mjs +1 -0
  186. package/dist/shared/comments/routes.d.mts +53 -0
  187. package/dist/shared/comments/routes.mjs +37 -0
  188. package/dist/shared/feedback.d.mts +8 -0
  189. package/dist/shared/feedback.mjs +10 -2
  190. package/dist/shared/page-review.d.mts +2 -11
  191. package/dist/shared/page-review.mjs +4 -13
  192. package/dist/shared/presence-core.d.mts +11 -0
  193. package/dist/shared/presence-core.mjs +11 -2
  194. package/dist/shared/review-file.d.mts +5 -0
  195. package/dist/shared/review-file.mjs +23 -13
  196. package/dist/shared/server-page.mjs +7 -1
  197. package/dist/shared/signup.d.mts +2 -5
  198. package/dist/shared/signup.mjs +2 -35
  199. package/dist/shared/studio-events.d.mts +0 -6
  200. package/dist/shared/studio-events.mjs +1 -5
  201. package/dist/shared/studio-files.d.mts +12 -0
  202. package/dist/shared/studio-files.mjs +6 -1
  203. package/dist/shared/studio-routes.d.mts +16 -62
  204. package/dist/shared/studio-routes.mjs +1 -69
  205. package/dist/shared/threads-core.d.mts +2 -2
  206. package/dist/shared/threads-core.mjs +1 -1
  207. package/extension/dist/feedback.js +148 -138
  208. package/extension/dist/manifest.json +1 -1
  209. package/extension/dist/picker.js +146 -136
  210. package/package.json +3 -20
  211. package/src/cli/banner.ts +45 -11
  212. package/src/cli/commands/app.ts +23 -8
  213. package/src/cli/commands/create.ts +17 -9
  214. package/src/cli/commands/dev.ts +0 -3
  215. package/src/cli/commands/hook.ts +4 -1
  216. package/src/cli/help.ts +3 -3
  217. package/src/config/index.ts +6 -5
  218. package/src/config/project-files.ts +21 -11
  219. package/src/config/renamed.ts +0 -3
  220. package/src/config/schema.ts +0 -1
  221. package/src/content/capture.ts +6 -12
  222. package/src/content/content.ts +7 -244
  223. package/src/content/flow-specs.ts +2 -1
  224. package/src/content/prototype-summary.ts +4 -44
  225. package/src/content/review-pages.ts +2 -1
  226. package/src/content/snapshot-client.ts +1 -1
  227. package/src/content/text-replace.ts +449 -0
  228. package/src/paths.ts +0 -8
  229. package/src/server/agent-env.ts +12 -2
  230. package/src/server/agent-session.ts +124 -34
  231. package/src/server/brief.ts +8 -3
  232. package/src/server/channel.ts +121 -52
  233. package/src/server/claimed-presence.ts +93 -0
  234. package/src/server/comments/actions.ts +352 -0
  235. package/src/server/comments/routes.ts +176 -0
  236. package/src/server/comments/tools.ts +317 -0
  237. package/src/server/dev-server.ts +49 -15
  238. package/src/server/file-errors.ts +47 -13
  239. package/src/server/file-lock.ts +205 -54
  240. package/src/server/mcp.ts +13 -14
  241. package/src/server/me.ts +2 -5
  242. package/src/server/open-page.ts +54 -4
  243. package/src/server/outcome.ts +26 -0
  244. package/src/server/people.ts +2 -1
  245. package/src/server/port.ts +94 -20
  246. package/src/server/presence.ts +11 -3
  247. package/src/server/requests.ts +60 -70
  248. package/src/server/studio-for.ts +27 -3
  249. package/src/server/sync-install.ts +35 -26
  250. package/src/server/thread-place.ts +2 -4
  251. package/src/server/tools/reading.ts +16 -3
  252. package/src/server/tools/watch.ts +7 -7
  253. package/src/server/vite.ts +37 -40
  254. package/src/server/web-app.ts +14 -12
  255. package/src/setup/apply.ts +10 -16
  256. package/src/setup/create.ts +143 -37
  257. package/src/setup/design-system-checks.ts +7 -15
  258. package/src/setup/desktop-app-script.ts +4 -3
  259. package/src/setup/desktop-app-start.ts +94 -26
  260. package/src/setup/desktop-app.ts +24 -3
  261. package/src/setup/detect.ts +56 -14
  262. package/src/setup/doctor.ts +4 -4
  263. package/src/setup/existing-setup.ts +10 -12
  264. package/src/setup/own-app.ts +2 -5
  265. package/src/setup/plan.ts +93 -84
  266. package/src/setup/project.ts +25 -118
  267. package/src/setup/prompts.ts +44 -127
  268. package/src/setup/questions.ts +27 -29
  269. package/src/setup/starters.ts +35 -10
  270. package/src/setup/workspace-setup.ts +8 -18
  271. package/src/setup/write.ts +52 -9
  272. package/src/shared/canvas-file.ts +205 -0
  273. package/src/shared/comments/routes.ts +79 -0
  274. package/src/shared/feedback.ts +14 -4
  275. package/src/shared/page-review.ts +11 -16
  276. package/src/shared/presence-core.ts +16 -2
  277. package/src/shared/review-file.ts +28 -12
  278. package/src/shared/server-page.ts +11 -1
  279. package/src/shared/signup.ts +4 -28
  280. package/src/shared/studio-events.ts +0 -13
  281. package/src/shared/studio-files.ts +14 -0
  282. package/src/shared/studio-routes.ts +21 -64
  283. package/src/shared/threads-core.ts +2 -2
  284. package/templates/methods/review.md +2 -2
  285. package/templates/rules/canvas-frames.md +104 -0
  286. package/templates/rules/canvas.md +8 -142
  287. package/templates/rules/prototypes.md +1 -1
  288. package/templates/rules/snapshots.md +52 -0
  289. package/templates/starters/standalone/README.md +7 -7
  290. package/templates/starters/standalone/components.json +1 -1
  291. package/templates/starters/standalone/package.json +1 -2
  292. package/templates/starters/turborepo/README.md +6 -6
  293. package/templates/starters/turborepo/apps/web/components.json +1 -1
  294. package/templates/starters/turborepo/apps/web/package.json +1 -2
  295. package/templates/starters/turborepo/apps/web/src/app.tsx +4 -5
  296. package/templates/starters/turborepo/packages/ui/components.json +1 -1
  297. package/templates/starters/turborepo/packages/ui/package.json +1 -2
  298. package/templates/starters/vite/README.md +7 -6
  299. package/templates/starters/vite/components.json +1 -1
  300. package/templates/starters/vite/package.json +1 -2
  301. package/templates/starters/vite/src/app.tsx +3 -4
  302. package/templates/starters/vite/src/components/ui/accordion.tsx +71 -0
  303. package/templates/starters/vite/src/components/ui/attachment.tsx +1 -1
  304. package/templates/starters/vite/src/components/ui/badge.tsx +2 -2
  305. package/templates/starters/vite/src/components/ui/button.tsx +17 -12
  306. package/templates/starters/vite/src/components/ui/card.tsx +1 -1
  307. package/templates/starters/vite/src/components/ui/checkbox.tsx +28 -0
  308. package/templates/starters/vite/src/components/ui/dialog.tsx +5 -5
  309. package/templates/starters/vite/src/components/ui/dropdown-menu.tsx +17 -14
  310. package/templates/starters/vite/src/components/ui/field.tsx +3 -1
  311. package/templates/starters/vite/src/components/ui/input-group.tsx +5 -3
  312. package/templates/starters/vite/src/components/ui/input.tsx +1 -1
  313. package/templates/starters/vite/src/components/ui/label.tsx +2 -0
  314. package/templates/starters/vite/src/components/ui/native-select.tsx +3 -4
  315. package/templates/starters/vite/src/components/ui/popover.tsx +1 -1
  316. package/templates/starters/vite/src/components/ui/separator.tsx +2 -0
  317. package/templates/starters/vite/src/components/ui/spinner.tsx +3 -7
  318. package/templates/starters/vite/src/components/ui/switch.tsx +1 -1
  319. package/templates/starters/vite/src/components/ui/textarea.tsx +1 -1
  320. package/templates/starters/vite/src/components/ui/toggle.tsx +4 -2
  321. package/templates/starters/vite/src/index.css +533 -111
  322. package/app/icons/sets/hugeicons.tsx +0 -259
  323. package/app/icons/sets/phosphor.tsx +0 -251
  324. package/dist/server/icon-set.d.mts +0 -15
  325. package/dist/server/icon-set.mjs +0 -31
  326. package/dist/server/tools/close-reply.d.mts +0 -6
  327. package/dist/server/tools/close-reply.mjs +0 -138
  328. package/dist/server/tools/comment.d.mts +0 -6
  329. package/dist/server/tools/comment.mjs +0 -49
  330. package/dist/shared/icon-sets.d.mts +0 -9
  331. package/dist/shared/icon-sets.mjs +0 -21
  332. package/src/server/icon-set.ts +0 -56
  333. package/src/server/tools/close-reply.ts +0 -271
  334. package/src/server/tools/comment.ts +0 -96
  335. package/src/shared/icon-sets.ts +0 -23
  336. /package/app/public/{sudio-192.png → dex-192.png} +0 -0
  337. /package/app/public/{sudio-512.png → dex-512.png} +0 -0
  338. /package/app/public/{apple-touch-icon.png → dex-apple-touch-icon.png} +0 -0
  339. /package/app/public/{favicon-32.png → dex-icon-32.png} +0 -0
  340. /package/app/public/{favicon.svg → dex-icon.svg} +0 -0
  341. /package/app/public/{sudio-maskable-512.png → dex-maskable-512.png} +0 -0
@@ -1,93 +1,239 @@
1
- // A lock every process on this machine shares: a folder, since making one
2
- // succeeds for one process only. Presence, thread files and canvas.json are
3
- // each changed under one, so two agents changing the same file at once both
4
- // survive, and a claim is only ever won by one of them. A lock left by a
5
- // process that stopped halfway is taken over once it is old.
1
+ // A lock every process on this machine shares: a folder holding one claim, a
2
+ // file named for its holder and when its lease ends. Presence, thread files
3
+ // and canvas.json are each changed under one, so two agents changing the
4
+ // same file at once both survive, and a claim is only ever won by one of
5
+ // them. A lease left by a process that stopped halfway is taken over once
6
+ // it has ended.
7
+ //
8
+ // Every change to a claim is one rename or unlink of the exact name its
9
+ // changer last saw, so it fails when the claim has changed since: a holder
10
+ // that comes back late never removes the next holder's lock, and two
11
+ // waiters never both take over one left behind. The design is model-checked:
12
+ // .claude/skills/verify-file-lock.
6
13
 
7
- import { mkdir, rm, stat, utimes } from "node:fs/promises";
14
+ import { randomBytes } from "node:crypto";
15
+ import {
16
+ mkdir,
17
+ readdir,
18
+ rename,
19
+ rm,
20
+ rmdir,
21
+ stat,
22
+ unlink,
23
+ writeFile,
24
+ } from "node:fs/promises";
25
+ import { join } from "node:path";
8
26
  import { setTimeout as delay } from "node:timers/promises";
9
27
 
10
- // A change takes milliseconds. A lock older than this was left by a process
11
- // that stopped halfway, and is taken over.
28
+ // A change takes milliseconds. A lease longer than this was left by a
29
+ // process that stopped halfway, and is taken over.
12
30
  const LOCK_STALE_MS = 5000;
13
31
  const LOCK_RETRY_MS = 10;
14
32
 
15
33
  /** How a lock is held. */
16
34
  export interface LockOptions {
17
35
  /**
18
- * Keep the lock fresh while the work runs, so work longer than `staleMs`
36
+ * Keep the lease fresh while the work runs, so work longer than `staleMs`
19
37
  * keeps it: only a lock whose process stopped goes stale.
20
38
  */
21
39
  keepAlive?: boolean;
22
- /** How old a lock is before it counts as left behind. */
40
+ /** How long a lease lasts before it counts as left behind. */
23
41
  staleMs?: number;
24
42
  }
25
43
 
26
- /** A lock kept alive is touched this often per `staleMs`, so one late touch still lands in time. */
44
+ /** A lock kept alive is renewed this often per `staleMs`, so one late renewal still lands in time. */
27
45
  const TOUCHES_PER_STALE = 3;
28
46
 
29
- const isStale = (folder: string, staleMs: number): Promise<boolean> =>
30
- stat(folder).then(
31
- (info) => Date.now() - info.mtimeMs > staleMs,
32
- () => false
33
- );
47
+ /**
48
+ * How many times in a row publishing may be refused with no folder there
49
+ * before the refusal is taken as a real error rather than a release racing it.
50
+ */
51
+ const REFUSALS_WITHOUT_FOLDER = 100;
52
+
53
+ const CLAIM_PREFIX = "holder-";
54
+
55
+ const claimName = (token: string, staleMs: number): string =>
56
+ `${CLAIM_PREFIX}${token}-${Date.now() + staleMs}`;
34
57
 
35
- const remove = (folder: string) => rm(folder, { recursive: true, force: true });
58
+ const isClaim = (name: string): boolean => name.startsWith(CLAIM_PREFIX);
36
59
 
37
- /** Whether a failed call failed because the folder was already there. */
38
- const alreadyThere = (error: unknown): boolean =>
39
- (error as NodeJS.ErrnoException).code === "EEXIST";
60
+ const leaseEnded = (claim: string): boolean =>
61
+ Number(claim.slice(claim.lastIndexOf("-") + 1)) <= Date.now();
62
+
63
+ const code = (error: unknown): string | undefined =>
64
+ (error as NodeJS.ErrnoException).code;
65
+
66
+ /** Whether a call failed for want of what it named: it changed since it was seen. */
67
+ const gone = (error: unknown): boolean => code(error) === "ENOENT";
40
68
 
41
69
  /**
42
- * Remove a stale lock. Two waiters can both see it stale, and the second
43
- * must not remove the lock the first has just taken in its place, so the
44
- * removal happens under a second folder, after looking at the age again.
45
- * Returns whether this process did the takeover.
70
+ * Whether publishing failed because a folder is there: POSIX refuses a full
71
+ * folder, Windows any folder.
46
72
  */
47
- async function takeOver(folder: string, staleMs: number): Promise<boolean> {
48
- const guard = `${folder}.takeover`;
49
- try {
50
- await mkdir(guard);
51
- } catch (error) {
52
- if (!alreadyThere(error)) {
73
+ const refused = (error: unknown): boolean =>
74
+ ["EEXIST", "ENOTEMPTY", "EPERM", "EACCES"].includes(code(error) ?? "");
75
+
76
+ /** Run `call`, true when it worked, false when what it named was gone. */
77
+ const unlessGone = (call: Promise<unknown>): Promise<boolean> =>
78
+ call.then(
79
+ () => true,
80
+ (error) => {
81
+ if (gone(error)) {
82
+ return false;
83
+ }
53
84
  throw error;
54
85
  }
55
- // A guard is held for milliseconds; an old one was left by a process
56
- // that stopped during a takeover.
57
- if (await isStale(guard, LOCK_STALE_MS)) {
58
- await remove(guard);
86
+ );
87
+
88
+ /**
89
+ * Remove what `work` left in the lock's folder. Never a claim: one that is
90
+ * not this caller's is a holder's that took the lock over meanwhile.
91
+ */
92
+ async function clearBesides(folder: string): Promise<void> {
93
+ for (const entry of await readdir(folder)) {
94
+ if (!isClaim(entry)) {
95
+ await rm(join(folder, entry), { recursive: true, force: true });
59
96
  }
60
- return false;
61
97
  }
62
- try {
63
- if (await isStale(folder, staleMs)) {
64
- await remove(folder);
65
- }
66
- return true;
67
- } finally {
68
- await remove(guard);
98
+ }
99
+
100
+ /**
101
+ * A folder with no claim is empty for a moment while it is let go, or was
102
+ * left by an older Dex, which held a lock as a bare folder. Once older than
103
+ * `staleMs`, what is in it goes, and then the folder if it is still empty:
104
+ * never a claim, so never a lock that is held.
105
+ */
106
+ async function clearUnclaimed(
107
+ folder: string,
108
+ entries: string[],
109
+ staleMs: number
110
+ ): Promise<void> {
111
+ const old = await stat(folder).then(
112
+ (info) => Date.now() - info.mtimeMs > staleMs,
113
+ () => false
114
+ );
115
+ if (!old) {
116
+ await delay(LOCK_RETRY_MS);
117
+ return;
118
+ }
119
+ for (const entry of entries) {
120
+ await rm(join(folder, entry), { recursive: true, force: true });
69
121
  }
122
+ await rmdir(folder).catch(() => undefined);
70
123
  }
71
124
 
72
- /** Take the lock `folder`, waiting while another process holds it. */
73
- async function lock(folder: string, staleMs: number): Promise<void> {
125
+ /**
126
+ * Take the lock `folder`, waiting while another process holds it. Returns
127
+ * the name of this caller's claim.
128
+ */
129
+ async function lock(
130
+ folder: string,
131
+ token: string,
132
+ staleMs: number
133
+ ): Promise<string> {
134
+ const temporary = `${folder}.${token}`;
135
+ let refusals = 0;
74
136
  for (;;) {
137
+ // A folder that already holds the claim appears whole, or not at all.
138
+ const mine = claimName(token, staleMs);
139
+ await mkdir(temporary);
75
140
  try {
76
- await mkdir(folder);
77
- return;
141
+ await writeFile(join(temporary, mine), "");
142
+ await rename(temporary, folder);
143
+ // Nothing more is awaited before `work` starts. A caller held up so
144
+ // long that its lease ended first is a holder that lapsed.
145
+ return mine;
78
146
  } catch (error) {
79
- if (!alreadyThere(error)) {
147
+ await rm(temporary, { recursive: true, force: true });
148
+ if (!refused(error)) {
80
149
  throw error;
81
150
  }
82
151
  }
83
- const tookOver =
84
- (await isStale(folder, staleMs)) && (await takeOver(folder, staleMs));
85
- if (!tookOver) {
152
+
153
+ const entries = await readdir(folder).catch((error: unknown) => {
154
+ if (gone(error)) {
155
+ return;
156
+ }
157
+ throw error;
158
+ });
159
+ if (!entries) {
160
+ refusals += 1;
161
+ if (refusals >= REFUSALS_WITHOUT_FOLDER) {
162
+ throw new Error(`The lock ${folder} could not be made.`);
163
+ }
164
+ continue;
165
+ }
166
+ refusals = 0;
167
+ const held = entries.find(isClaim);
168
+ if (!held) {
169
+ await clearUnclaimed(folder, entries, staleMs);
170
+ continue;
171
+ }
172
+ if (!leaseEnded(held)) {
86
173
  await delay(LOCK_RETRY_MS);
174
+ continue;
175
+ }
176
+ // Take over the claim seen, if it is still the one there.
177
+ const taken = claimName(token, staleMs);
178
+ if (await unlessGone(rename(join(folder, held), join(folder, taken)))) {
179
+ await clearBesides(folder);
180
+ return taken;
87
181
  }
88
182
  }
89
183
  }
90
184
 
185
+ /** A held lock's claim, renewed one at a time. */
186
+ function holding(
187
+ folder: string,
188
+ token: string,
189
+ claim: string,
190
+ staleMs: number
191
+ ) {
192
+ let current: string | undefined = claim;
193
+ let last: Promise<void> = Promise.resolve();
194
+ /** Renew the lease, unless it was lost: the name now, or undefined. */
195
+ const renew = (): Promise<string | undefined> => {
196
+ last = last.then(async () => {
197
+ if (!current) {
198
+ return;
199
+ }
200
+ const next = claimName(token, staleMs);
201
+ try {
202
+ await rename(join(folder, current), join(folder, next));
203
+ current = next;
204
+ } catch (error) {
205
+ // Gone: taken over. Any other failure keeps the claim as it was.
206
+ if (gone(error)) {
207
+ current = undefined;
208
+ }
209
+ }
210
+ });
211
+ return last.then(() => current);
212
+ };
213
+ return { renew };
214
+ }
215
+
216
+ /**
217
+ * Let the lock go, if this caller still holds it: renew it first, so it
218
+ * stays this caller's while the folder is cleared; touch nothing if it was
219
+ * taken over.
220
+ */
221
+ async function release(
222
+ folder: string,
223
+ claim: ReturnType<typeof holding>
224
+ ): Promise<void> {
225
+ const mine = await claim.renew();
226
+ if (!mine) {
227
+ return;
228
+ }
229
+ await clearBesides(folder);
230
+ if (!(await unlessGone(unlink(join(folder, mine))))) {
231
+ return;
232
+ }
233
+ // Only an empty folder goes: one published into it meanwhile stays.
234
+ await rmdir(folder).catch(() => undefined);
235
+ }
236
+
91
237
  /**
92
238
  * Run `work` holding the lock `folder` (its parent must exist), and let it
93
239
  * go after, whatever happens. `work` may keep files in the folder while it
@@ -98,17 +244,22 @@ export async function withLock<T>(
98
244
  work: () => Promise<T>,
99
245
  { keepAlive = false, staleMs = LOCK_STALE_MS }: LockOptions = {}
100
246
  ): Promise<T> {
101
- await lock(folder, staleMs);
247
+ const token = randomBytes(8).toString("hex");
248
+ const claim = holding(
249
+ folder,
250
+ token,
251
+ await lock(folder, token, staleMs),
252
+ staleMs
253
+ );
102
254
  const fresh = keepAlive
103
255
  ? setInterval(() => {
104
- const now = new Date();
105
- utimes(folder, now, now).catch(() => undefined);
256
+ claim.renew().catch(() => undefined);
106
257
  }, staleMs / TOUCHES_PER_STALE)
107
258
  : undefined;
108
259
  try {
109
260
  return await work();
110
261
  } finally {
111
262
  clearInterval(fresh);
112
- await remove(folder);
263
+ await release(folder, claim);
113
264
  }
114
265
  }
package/src/server/mcp.ts CHANGED
@@ -15,7 +15,7 @@
15
15
  //
16
16
  // This module creates the server, builds what its tools share
17
17
  // (tools/context.ts), registers each group of tools from its module in
18
- // tools/, and serves.
18
+ // tools/ (the comment tools from comments/tools.ts), and serves.
19
19
 
20
20
  import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
21
21
  import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
@@ -41,10 +41,9 @@ import {
41
41
  startChannel,
42
42
  startHeartbeat,
43
43
  } from "./channel.ts";
44
+ import { registerCommentTools } from "./comments/tools.ts";
44
45
  import { HANDOVER, type Handover, registerHandover } from "./mcp-handover.ts";
45
46
  import { type StudioFor, studioFor } from "./studio-for.ts";
46
- import { registerCloseAndReplyTools } from "./tools/close-reply.ts";
47
- import { registerCommentTool } from "./tools/comment.ts";
48
47
  import { openToolContext } from "./tools/context.ts";
49
48
  import { registerFeedbackTool } from "./tools/feedback.ts";
50
49
  import { registerPlaceFrameTool } from "./tools/place-frame.ts";
@@ -137,12 +136,6 @@ const designSystemOf = (
137
136
  () => undefined
138
137
  );
139
138
 
140
- /** The studio's MCP server (see createStudio). */
141
- export const createStudioServer = (
142
- root: string,
143
- options: StudioServerOptions = {}
144
- ): McpServer => createStudio(root, options).server;
145
-
146
139
  /**
147
140
  * The studio's MCP server and its agent session. `threads`
148
141
  * (thread-module.ts) is shared with the channel, so one change feed serves
@@ -190,6 +183,7 @@ export function createStudio(
190
183
  };
191
184
  const session = openAgentSession(root, threads, {
192
185
  agent: agentName,
186
+ claudeSession,
193
187
  ...(sessionId ? { id: sessionId } : {}),
194
188
  });
195
189
  const handover = registerHandover(server, session, receipt);
@@ -216,8 +210,8 @@ export function createStudio(
216
210
  registerReadingTools(server, context, briefing);
217
211
  registerThreadsTool(server, context);
218
212
  registerWatchTool(server, context);
219
- registerCommentTool(server, context);
220
- registerCloseAndReplyTools(server, context);
213
+ // `comment`, `close` and `reply`.
214
+ registerCommentTools(server, context);
221
215
  registerPlaceFrameTool(server, context);
222
216
  registerShowTool(server, context, findStudio);
223
217
  registerSnapshotTool(server, context, findStudio);
@@ -235,12 +229,13 @@ export async function serveMcp(
235
229
  const channel = process.env[CHANNEL_ENV] === "1";
236
230
  // One thread module, so the channel and `watch` share its change feed.
237
231
  const threads = openThreads(root);
232
+ // Claude Code gives its MCP servers the session's id, as it gives hooks.
233
+ const claudeSession = process.env.CLAUDE_CODE_SESSION_ID;
238
234
  const { handover, receipt, server, session } = createStudio(root, {
239
235
  ...options,
240
236
  channel,
241
237
  threads,
242
- // Claude Code gives its MCP servers the session's id, as it gives hooks.
243
- claudeSession: process.env.CLAUDE_CODE_SESSION_ID,
238
+ claudeSession,
244
239
  });
245
240
  const client = () => server.server.getClientVersion()?.name ?? "";
246
241
  const pushUntilHandedOver = async () => {
@@ -265,7 +260,11 @@ export async function serveMcp(
265
260
  holding: session.holding().length,
266
261
  receipt,
267
262
  }),
268
- { onEnd: session.leave, session: session.id }
263
+ {
264
+ claudeSession,
265
+ onEnd: session.leave,
266
+ session: session.id,
267
+ }
269
268
  );
270
269
  // The studio sees a watch start or stop, its grace run out, and the
271
270
  // comments the session holds change, as they happen, not a beat later.
package/src/server/me.ts CHANGED
@@ -3,11 +3,8 @@
3
3
  // `meTag` in vite.ts) and asks for it again when it may have changed.
4
4
 
5
5
  import { execFile } from "node:child_process";
6
- import {
7
- type Contributor,
8
- identify,
9
- joinCurrentPerson,
10
- } from "../content/content.ts";
6
+ import { identify, joinCurrentPerson } from "../content/content.ts";
7
+ import type { Contributor } from "../shared/studio-files.ts";
11
8
  import { nameOfMe } from "../shared/studio-routes.ts";
12
9
  import { readCloudConfig } from "./cloud.ts";
13
10
 
@@ -1,9 +1,9 @@
1
1
  // Where the person is in Dex, as an agent asks for it: the file they have
2
2
  // open, the page of it, the sections of its canvas in view and the frame
3
- // selected, and how long ago Dex's interface last said so. The interface
4
- // tells the server as the person moves (shared/open-page-core.ts) and the
5
- // server keeps it with the open file (state.ts). Threads on that page come
6
- // first when they are listed.
3
+ // selected, how long ago Dex's interface last said so, and who the canvas
4
+ // shows at work on the file. The interface tells the server as the person
5
+ // moves (shared/open-page-core.ts) and the server keeps it with the open
6
+ // file (state.ts). Threads on that page come first when they are listed.
7
7
 
8
8
  import { readCanvas } from "../content/content.ts";
9
9
  import { prototypeTitle } from "../content/prototypes.ts";
@@ -13,7 +13,13 @@ import {
13
13
  parseFrameKey,
14
14
  viewOf,
15
15
  } from "../shared/canvas-core.ts";
16
+ import { fromRoom } from "../shared/live-presence.ts";
16
17
  import { onOpenPage } from "../shared/open-page-core.ts";
18
+ import {
19
+ endsAt,
20
+ type PresenceEntry,
21
+ underSession,
22
+ } from "../shared/presence-core.ts";
17
23
  import type { ThreadWithPrototype } from "../shared/threads-core.ts";
18
24
  import { plural } from "./response-budget.ts";
19
25
  import { readFocus, type SavedFocus } from "./state.ts";
@@ -159,6 +165,50 @@ export async function whereThePersonIs(
159
165
  };
160
166
  }
161
167
 
168
+ /** An agent the canvas shows at work on a file, as an agent reads it (`where`). */
169
+ export interface AtWork {
170
+ agent: string;
171
+ /**
172
+ * Whose mark it is: the agent reading it (its own session, or the Claude
173
+ * Code session it serves, sub-agents too), another session on this
174
+ * machine, or a teammate in the live workspace.
175
+ */
176
+ by: "you" | "another session" | "a teammate";
177
+ /** When it ends, unless it is marked again or its work ends first. */
178
+ ends: string;
179
+ /** When it was last marked. */
180
+ marked: string;
181
+ /** "view:<name>", "doc:<name>", or "prototype": shared code, so every view. */
182
+ on: string;
183
+ }
184
+
185
+ /**
186
+ * Who the canvas shows at work on file `slug`, from the fresh `entries`, as
187
+ * the agent whose marks are under the sessions `mine` reads it.
188
+ */
189
+ export const atWorkOn = (
190
+ entries: readonly PresenceEntry[],
191
+ slug: string,
192
+ mine: readonly string[]
193
+ ): AtWork[] =>
194
+ entries
195
+ .filter((entry) => entry.slug === slug)
196
+ .map((entry) => {
197
+ let by: AtWork["by"] = "another session";
198
+ if (fromRoom(entry)) {
199
+ by = "a teammate";
200
+ } else if (mine.some((session) => underSession(entry.session, session))) {
201
+ by = "you";
202
+ }
203
+ return {
204
+ agent: entry.agent,
205
+ on: entry.target,
206
+ by,
207
+ marked: entry.updated,
208
+ ends: new Date(endsAt(entry)).toISOString(),
209
+ };
210
+ });
211
+
162
212
  /** A thread with whether it is on the page the person has open. */
163
213
  export interface PlacedThread<T> {
164
214
  onOpenPage: boolean;
@@ -0,0 +1,26 @@
1
+ // What a core action answers: a value, or the problem that stopped it. An
2
+ // action never throws for what it expects to refuse, and never names an
3
+ // HTTP status: each adapter turns a problem into its own answer, a status
4
+ // for the interface's requests and a refusal for an agent's tools.
5
+
6
+ /** What kind of refusal a problem is. */
7
+ export type ProblemKind = "not-found" | "conflict" | "invalid" | "not-allowed";
8
+
9
+ /** Why an action did not happen, in words for people: most say one thing. */
10
+ export interface Problem {
11
+ kind: ProblemKind;
12
+ messages: string[];
13
+ }
14
+
15
+ /** What an action did, or the problem that stopped it. */
16
+ export type Outcome<T> =
17
+ | { problem?: undefined; value: T }
18
+ | { problem: Problem; value?: undefined };
19
+
20
+ /** An action refused, of `kind`, with what to tell people. */
21
+ export const refused = (
22
+ kind: ProblemKind,
23
+ messages: string[]
24
+ ): { problem: Problem; value?: undefined } => ({
25
+ problem: { kind, messages },
26
+ });
@@ -2,7 +2,8 @@
2
2
  // contributors.json, with when they last committed and how often, from git's
3
3
  // log under any of their git names. Read-only.
4
4
 
5
- import { type Contributor, loadStudio } from "../content/content.ts";
5
+ import { loadStudio } from "../content/content.ts";
6
+ import type { Contributor } from "../shared/studio-files.ts";
6
7
  import type { Person } from "../shared/studio-routes.ts";
7
8
  import { runGit } from "./git.ts";
8
9