@dexhq/canvas 0.15.0 → 0.16.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 (307) hide show
  1. package/CHANGELOG.md +34 -0
  2. package/app/canvas/activity-parts.tsx +50 -6
  3. package/app/canvas/activity.tsx +2 -3
  4. package/app/canvas/agent-changes.ts +18 -8
  5. package/app/canvas/anchoring.ts +13 -3
  6. package/app/canvas/canvas-context.tsx +6 -2
  7. package/app/canvas/canvas-input.ts +35 -0
  8. package/app/canvas/canvas-model.ts +2 -2
  9. package/app/canvas/canvas-scene.ts +243 -24
  10. package/app/canvas/canvas-ui.ts +11 -2
  11. package/app/canvas/canvas.css +86 -3
  12. package/app/canvas/comments.tsx +66 -29
  13. package/app/canvas/element-link.ts +135 -0
  14. package/app/canvas/element-picker.ts +23 -0
  15. package/app/canvas/export-dialog.tsx +265 -0
  16. package/app/canvas/flow-canvas.tsx +65 -14
  17. package/app/canvas/frame-context-menu.tsx +61 -7
  18. package/app/canvas/frame-node.tsx +10 -11
  19. package/app/canvas/layout.ts +41 -22
  20. package/app/canvas/linked-element.tsx +196 -0
  21. package/app/canvas/live-page.ts +14 -0
  22. package/app/canvas/mentions.tsx +209 -0
  23. package/app/canvas/picked-box.tsx +21 -0
  24. package/app/canvas/removed-views-panel.tsx +4 -4
  25. package/app/canvas/request-node.tsx +151 -0
  26. package/app/canvas/review-panel.tsx +8 -3
  27. package/app/canvas/section-node.tsx +4 -133
  28. package/app/canvas/spec-margin.tsx +4 -1
  29. package/app/canvas/teammates-layer.tsx +205 -0
  30. package/app/canvas/text-layer.tsx +103 -17
  31. package/app/canvas/tool-cursors.ts +7 -6
  32. package/app/canvas/tool-icons.tsx +24 -56
  33. package/app/canvas/typing.ts +12 -0
  34. package/app/canvas/use-agent-changes.tsx +6 -2
  35. package/app/canvas/use-canvas-editor.ts +4 -2
  36. package/app/export/player-model.ts +159 -0
  37. package/app/export/player.tsx +198 -0
  38. package/app/export.html +31 -0
  39. package/app/export.tsx +9 -0
  40. package/app/frame/announce-refreshes.ts +21 -0
  41. package/app/frame/start-frame.tsx +50 -0
  42. package/app/frame/view-loads.ts +161 -0
  43. package/app/frame/views.ts +13 -49
  44. package/app/frame.tsx +2 -38
  45. package/app/i18n/locales/en/messages.po +145 -6
  46. package/app/i18n/locales/en/messages.ts +1 -1
  47. package/app/i18n/locales/pseudo/messages.po +144 -5
  48. package/app/i18n/locales/pseudo/messages.ts +1 -1
  49. package/app/icons/dex-glyphs.tsx +219 -0
  50. package/app/icons/glyphs.tsx +76 -80
  51. package/app/kit/use-flow.ts +2 -1
  52. package/app/live/canvas-teammates.ts +90 -0
  53. package/app/live/studio-api.ts +30 -20
  54. package/app/pages/canvas.tsx +18 -6
  55. package/app/pages/commands-settings.tsx +1 -1
  56. package/app/pages/render.tsx +39 -11
  57. package/app/shell/bell.tsx +15 -5
  58. package/app/shell/presence.tsx +123 -26
  59. package/app/shell/thread-list.tsx +25 -8
  60. package/app/shell/working-on.tsx +35 -3
  61. package/app/ui/prompt-editor.tsx +39 -11
  62. package/app/ui/reference.tsx +21 -8
  63. package/bin/sudio.mjs +1 -1
  64. package/dist/cli/commands/create.d.mts +5 -5
  65. package/dist/cli/commands/create.mjs +4 -4
  66. package/dist/cli/commands/export.d.mts +40 -0
  67. package/dist/cli/commands/export.mjs +66 -0
  68. package/dist/cli/commands/init.d.mts +2 -2
  69. package/dist/cli/commands/upgrade.mjs +1 -1
  70. package/dist/cli/help.d.mts +1 -1
  71. package/dist/cli/help.mjs +13 -7
  72. package/dist/cli/main.d.mts +42 -5
  73. package/dist/cli/main.mjs +1 -0
  74. package/dist/config/load.mjs +14 -2
  75. package/dist/config/project-files.d.mts +5 -0
  76. package/dist/config/project-files.mjs +10 -1
  77. package/dist/config/renamed.d.mts +7 -0
  78. package/dist/config/renamed.mjs +13 -1
  79. package/dist/content/commands.d.mts +11 -9
  80. package/dist/content/commands.mjs +22 -16
  81. package/dist/content/comments-log.mjs +2 -1
  82. package/dist/content/content.d.mts +11 -3
  83. package/dist/content/content.mjs +49 -15
  84. package/dist/content/element-words.d.mts +42 -0
  85. package/dist/content/element-words.mjs +112 -0
  86. package/dist/content/hosted-person.d.mts +31 -0
  87. package/dist/content/hosted-person.mjs +68 -0
  88. package/dist/content/merge-rules.d.mts +5 -0
  89. package/dist/content/merge-rules.mjs +17 -1
  90. package/dist/content/screenshot.mjs +9 -0
  91. package/dist/content/text-edit.d.mts +24 -14
  92. package/dist/content/text-edit.mjs +92 -81
  93. package/dist/content/text-replace.mjs +2 -26
  94. package/dist/content/thread-module.d.mts +2 -4
  95. package/dist/content/thread-module.mjs +4 -5
  96. package/dist/server/agents.d.mts +10 -0
  97. package/dist/server/agents.mjs +21 -6
  98. package/dist/server/canvas-presence.d.mts +22 -0
  99. package/dist/server/canvas-presence.mjs +160 -0
  100. package/dist/server/command-settings.mjs +1 -1
  101. package/dist/server/comments/actions.d.mts +0 -8
  102. package/dist/server/comments/actions.mjs +1 -9
  103. package/dist/server/comments/routes.mjs +10 -31
  104. package/dist/server/dev-server.mjs +35 -13
  105. package/dist/server/export/build.d.mts +15 -0
  106. package/dist/server/export/build.mjs +220 -0
  107. package/dist/server/export/page.d.mts +25 -0
  108. package/dist/server/export/page.mjs +40 -0
  109. package/dist/server/export/plugin.d.mts +31 -0
  110. package/dist/server/export/plugin.mjs +69 -0
  111. package/dist/server/export/run.d.mts +9 -0
  112. package/dist/server/export/run.mjs +61 -0
  113. package/dist/server/export/save.d.mts +7 -0
  114. package/dist/server/export/save.mjs +29 -0
  115. package/dist/server/hosted.d.mts +8 -0
  116. package/dist/server/hosted.mjs +78 -0
  117. package/dist/server/live-mirror.mjs +66 -10
  118. package/dist/server/live.mjs +28 -1
  119. package/dist/server/mcp.mjs +2 -0
  120. package/dist/server/me.d.mts +2 -0
  121. package/dist/server/me.mjs +8 -2
  122. package/dist/server/notices.d.mts +1 -1
  123. package/dist/server/notices.mjs +1 -1
  124. package/dist/server/own-copies.mjs +1 -1
  125. package/dist/server/people.d.mts +7 -2
  126. package/dist/server/people.mjs +6 -2
  127. package/dist/server/pictures.d.mts +1 -1
  128. package/dist/server/pictures.mjs +2 -2
  129. package/dist/server/prebundle.d.mts +32 -0
  130. package/dist/server/prebundle.mjs +115 -0
  131. package/dist/server/problems.d.mts +8 -0
  132. package/dist/server/problems.mjs +13 -0
  133. package/dist/server/requests.mjs +43 -30
  134. package/dist/server/source-stamp.d.mts +4 -3
  135. package/dist/server/source-stamp.mjs +60 -31
  136. package/dist/server/state.d.mts +19 -10
  137. package/dist/server/state.mjs +27 -18
  138. package/dist/server/thread-source.d.mts +8 -0
  139. package/dist/server/thread-source.mjs +24 -5
  140. package/dist/server/tools/export.d.mts +5 -0
  141. package/dist/server/tools/export.mjs +35 -0
  142. package/dist/server/tools/place-frame.mjs +16 -16
  143. package/dist/server/tools/thread-list.mjs +1 -1
  144. package/dist/server/vite.d.mts +9 -4
  145. package/dist/server/vite.mjs +63 -61
  146. package/dist/server/welcome.mjs +2 -1
  147. package/dist/setup/apply.d.mts +4 -0
  148. package/dist/setup/apply.mjs +5 -4
  149. package/dist/setup/create.mjs +5 -5
  150. package/dist/setup/design-system-checks.mjs +1 -6
  151. package/dist/setup/detect/workspace.d.mts +10 -10
  152. package/dist/setup/detect/workspace.mjs +10 -12
  153. package/dist/setup/doctor.d.mts +4 -1
  154. package/dist/setup/doctor.mjs +7 -5
  155. package/dist/setup/migrate/dex-folder.d.mts +61 -0
  156. package/dist/setup/migrate/dex-folder.mjs +544 -0
  157. package/dist/setup/migrate/files-folder.mjs +1 -1
  158. package/dist/setup/migrate/guides.mjs +1 -1
  159. package/dist/setup/migrate/migrations.mjs +4 -0
  160. package/dist/setup/migrate/setup-files.d.mts +5 -0
  161. package/dist/setup/migrate/setup-files.mjs +115 -0
  162. package/dist/setup/migrate/studio-state.d.mts +2 -2
  163. package/dist/setup/migrate/studio-state.mjs +4 -4
  164. package/dist/setup/placement.d.mts +1 -1
  165. package/dist/setup/placement.mjs +5 -4
  166. package/dist/setup/plan.d.mts +5 -5
  167. package/dist/setup/plan.mjs +8 -12
  168. package/dist/setup/project.mjs +1 -1
  169. package/dist/setup/prompts.d.mts +2 -1
  170. package/dist/setup/prompts.mjs +4 -3
  171. package/dist/setup/questions.mjs +3 -2
  172. package/dist/setup/starters.d.mts +1 -1
  173. package/dist/setup/starters.mjs +2 -2
  174. package/dist/setup/write.d.mts +15 -3
  175. package/dist/setup/write.mjs +23 -3
  176. package/dist/shared/canvas-core.d.mts +22 -7
  177. package/dist/shared/canvas-core.mjs +38 -10
  178. package/dist/shared/comments/routes.d.mts +3 -1
  179. package/dist/shared/comments/routes.mjs +4 -2
  180. package/dist/shared/export-core.d.mts +62 -0
  181. package/dist/shared/export-core.mjs +101 -0
  182. package/dist/shared/live-core.d.mts +2 -0
  183. package/dist/shared/live-core.mjs +6 -1
  184. package/dist/shared/live-presence.d.mts +57 -2
  185. package/dist/shared/live-presence.mjs +90 -4
  186. package/dist/shared/page-messages.d.mts +14 -1
  187. package/dist/shared/page-messages.mjs +30 -3
  188. package/dist/shared/people-core.d.mts +28 -0
  189. package/dist/shared/people-core.mjs +27 -1
  190. package/dist/shared/presence-core.d.mts +2 -0
  191. package/dist/shared/references-core.d.mts +4 -0
  192. package/dist/shared/references-core.mjs +12 -4
  193. package/dist/shared/studio-files.d.mts +9 -0
  194. package/dist/shared/studio-files.mjs +11 -2
  195. package/dist/shared/studio-routes.d.mts +13 -1
  196. package/dist/shared/threads-core.d.mts +27 -4
  197. package/dist/shared/threads-core.mjs +67 -12
  198. package/extension/dist/feedback.js +104 -294
  199. package/extension/dist/manifest.json +1 -1
  200. package/extension/dist/picker.js +104 -294
  201. package/package.json +2 -2
  202. package/src/cli/commands/create.ts +4 -4
  203. package/src/cli/commands/export.ts +78 -0
  204. package/src/cli/commands/upgrade.ts +1 -1
  205. package/src/cli/help.ts +13 -7
  206. package/src/cli/main.ts +1 -0
  207. package/src/config/load.ts +26 -2
  208. package/src/config/project-files.ts +15 -0
  209. package/src/config/renamed.ts +19 -1
  210. package/src/content/commands.ts +30 -18
  211. package/src/content/comments-log.ts +4 -1
  212. package/src/content/content.ts +64 -16
  213. package/src/content/element-words.ts +175 -0
  214. package/src/content/hosted-person.ts +107 -0
  215. package/src/content/merge-rules.ts +29 -0
  216. package/src/content/screenshot.ts +14 -0
  217. package/src/content/text-edit.ts +172 -105
  218. package/src/content/text-replace.ts +3 -32
  219. package/src/content/thread-module.ts +2 -4
  220. package/src/content/trash.ts +1 -1
  221. package/src/server/agents.ts +48 -17
  222. package/src/server/canvas-presence.ts +237 -0
  223. package/src/server/channel.ts +1 -1
  224. package/src/server/command-settings.ts +1 -1
  225. package/src/server/comments/actions.ts +0 -19
  226. package/src/server/comments/routes.ts +9 -54
  227. package/src/server/dev-server.ts +66 -26
  228. package/src/server/export/build.ts +310 -0
  229. package/src/server/export/page.ts +85 -0
  230. package/src/server/export/plugin.ts +138 -0
  231. package/src/server/export/run.ts +79 -0
  232. package/src/server/export/save.ts +38 -0
  233. package/src/server/hosted.ts +113 -0
  234. package/src/server/live-mirror.ts +93 -8
  235. package/src/server/live.ts +46 -1
  236. package/src/server/machine-update.ts +3 -3
  237. package/src/server/mcp.ts +2 -0
  238. package/src/server/me.ts +10 -1
  239. package/src/server/notices.ts +2 -2
  240. package/src/server/people.ts +11 -2
  241. package/src/server/pictures.ts +2 -2
  242. package/src/server/prebundle.ts +230 -0
  243. package/src/server/presence.ts +1 -1
  244. package/src/server/problems.ts +11 -0
  245. package/src/server/requests.ts +55 -40
  246. package/src/server/roster.ts +1 -1
  247. package/src/server/saved.ts +1 -1
  248. package/src/server/source-stamp.ts +118 -37
  249. package/src/server/state.ts +53 -31
  250. package/src/server/thread-source.ts +35 -3
  251. package/src/server/tools/export.ts +56 -0
  252. package/src/server/tools/place-frame.ts +23 -15
  253. package/src/server/tools/thread-list.ts +1 -1
  254. package/src/server/updates.ts +2 -2
  255. package/src/server/vite.ts +106 -110
  256. package/src/server/welcome.ts +3 -1
  257. package/src/setup/apply.ts +8 -5
  258. package/src/setup/create.ts +10 -10
  259. package/src/setup/design-system-checks.ts +1 -6
  260. package/src/setup/detect/workspace.ts +20 -21
  261. package/src/setup/doctor.ts +13 -6
  262. package/src/setup/migrate/dex-folder.ts +842 -0
  263. package/src/setup/migrate/files-folder.ts +1 -1
  264. package/src/setup/migrate/guides.ts +3 -1
  265. package/src/setup/migrate/migrations.ts +8 -2
  266. package/src/setup/migrate/setup-files.ts +198 -0
  267. package/src/setup/migrate/studio-state.ts +12 -10
  268. package/src/setup/own-app.ts +1 -1
  269. package/src/setup/placement.ts +7 -6
  270. package/src/setup/plan.ts +13 -18
  271. package/src/setup/project.ts +2 -2
  272. package/src/setup/prompts.ts +5 -4
  273. package/src/setup/questions.ts +6 -3
  274. package/src/setup/starters.ts +3 -3
  275. package/src/setup/write.ts +34 -3
  276. package/src/shared/canvas-core.ts +48 -9
  277. package/src/shared/comments/routes.ts +3 -1
  278. package/src/shared/export-core.ts +198 -0
  279. package/src/shared/live-core.ts +8 -0
  280. package/src/shared/live-presence.ts +171 -2
  281. package/src/shared/page-messages.ts +38 -2
  282. package/src/shared/people-core.ts +44 -0
  283. package/src/shared/presence-core.ts +2 -0
  284. package/src/shared/references-core.ts +22 -3
  285. package/src/shared/studio-files.ts +15 -1
  286. package/src/shared/studio-routes.ts +11 -1
  287. package/src/shared/threads-core.ts +132 -10
  288. package/templates/methods/explore.md +1 -1
  289. package/templates/methods/handoff.md +1 -1
  290. package/templates/methods/review.md +2 -1
  291. package/templates/rules/canvas-frames.md +6 -3
  292. package/templates/rules/commands.md +1 -0
  293. package/templates/rules/prototypes.md +1 -1
  294. package/templates/rules/studio.md +17 -11
  295. package/templates/skills/comprehend/SKILL.md +56 -0
  296. package/templates/skills/distill/SKILL.md +172 -0
  297. package/templates/skills/export-prototype/SKILL.md +35 -0
  298. package/templates/skills/riff/SKILL.md +21 -0
  299. package/templates/skills/riff-deep/SKILL.md +40 -0
  300. package/templates/skills/riff-max/SKILL.md +46 -0
  301. package/templates/skills/riff-state/SKILL.md +45 -0
  302. package/templates/skills/riff-up/SKILL.md +38 -0
  303. package/templates/skills/riff-wide/SKILL.md +40 -0
  304. package/templates/starters/standalone/README.md +1 -1
  305. package/templates/starters/turborepo/README.md +2 -2
  306. package/templates/starters/turborepo/apps/web/src/app.tsx +2 -2
  307. package/templates/starters/vite/README.md +1 -1
@@ -0,0 +1,842 @@
1
+ // The Dex folder was `studio/` until it was `dex/` (`apps/web/studio`
2
+ // until `apps/web/dex`). In it, what only this machine has was `.studio/`,
3
+ // and is `.local/`; the team's skills were `.dex/skills/` at the project's
4
+ // root, and are `skills/` in the Dex folder. This moves all three, then
5
+ // points what named the old folder at the new one, keeping the rest of each
6
+ // file as it was: the .gitignore's lines, the Dex folder's tsconfig,
7
+ // agents' configs, the root AGENTS.md's pointer, the project's scripts and,
8
+ // last, the config. An app of its own (`apps/studio`) keeps its name, as it
9
+ // is its Dex folder and renaming a workspace package is more than a move;
10
+ // so does a folder the team named itself (`--dir prototypes`). Only their
11
+ // state and skills move.
12
+ //
13
+ // Each step looks at what is there, so a move half done carries on, and
14
+ // the config changes last, so until it does upgrade finds the move still to
15
+ // finish. A teammate whose pull moved the folder has its `studio/.studio/`
16
+ // left behind, which joins what came. Nothing is written over: a `dex/`
17
+ // there already stops it, unless all `studio/` still holds is state, and
18
+ // what a folder has that something moves into stays. `doctor` plans it and
19
+ // warns; `dex upgrade` runs it.
20
+
21
+ import { existsSync } from "node:fs";
22
+ import {
23
+ cp,
24
+ lstat,
25
+ readdir,
26
+ readFile,
27
+ readlink,
28
+ rename,
29
+ rm,
30
+ symlink,
31
+ unlink,
32
+ writeFile,
33
+ } from "node:fs/promises";
34
+ import {
35
+ basename,
36
+ dirname,
37
+ isAbsolute,
38
+ join,
39
+ relative,
40
+ resolve,
41
+ } from "node:path";
42
+ import type { StudioProject } from "../../config/load.ts";
43
+ import { OLD_TEAM_SKILLS } from "../../content/commands.ts";
44
+ import { posix } from "../../paths.ts";
45
+ import { agentRootOf } from "../../server/agents.ts";
46
+ import {
47
+ OLD_STATE_FOLDER,
48
+ prepareState,
49
+ STATE_FOLDER,
50
+ stateIgnoreLine,
51
+ } from "../../server/state.ts";
52
+ import { listing } from "../../shared/listing.ts";
53
+ import {
54
+ DEX_FOLDER,
55
+ OLD_DEX_FOLDER,
56
+ STUDIO_FILES,
57
+ } from "../../shared/studio-files.ts";
58
+ import { editManifest } from "../apply.ts";
59
+ import { ConfigNotEditable, configData } from "../config-source.ts";
60
+ import { AGENT_FILES } from "../plan.ts";
61
+ import { type Migration, type Step, UPGRADE_COMMAND } from "./migration.ts";
62
+
63
+ /** Where a project's Dex folder is, and where it goes. */
64
+ export interface DexFolderPlace {
65
+ /** Where agents start, and their configs are (server/agents.ts). */
66
+ agentRoot: string;
67
+ /** The Dex folder as the config names it now. */
68
+ content: string;
69
+ /** The config file. */
70
+ file: string;
71
+ /** Where the Dex folder goes: `dex/`, or where it is when it keeps its name. */
72
+ folder: string;
73
+ /** The folder before, when it is another one: `studio/`, still to move or left behind. */
74
+ old?: string;
75
+ /** The project, which holds the config. */
76
+ root: string;
77
+ }
78
+
79
+ /** Both names the state has had, which a folder of Dex's may hold. */
80
+ const STATE_FOLDERS = [OLD_STATE_FOLDER, STATE_FOLDER];
81
+
82
+ /** Where the folder goes, from where the config says it is. */
83
+ export function dexFolderPlace({
84
+ root,
85
+ content,
86
+ file,
87
+ }: Pick<StudioProject, "content" | "file" | "root">): DexFolderPlace {
88
+ const place = { root, content, file, agentRoot: agentRootOf(root) };
89
+ // An app of its own is its Dex folder, whatever it is called.
90
+ if (resolve(content) === resolve(root)) {
91
+ return { ...place, folder: content };
92
+ }
93
+ if (basename(content) === OLD_DEX_FOLDER) {
94
+ return {
95
+ ...place,
96
+ old: content,
97
+ folder: join(dirname(content), DEX_FOLDER),
98
+ };
99
+ }
100
+ // Moved already, by this machine or by a pull: a studio/ beside it is the
101
+ // old folder only when it holds Dex's state, which is this machine's own.
102
+ const left = join(dirname(content), OLD_DEX_FOLDER);
103
+ const holdsState = STATE_FOLDERS.some((name) => existsSync(join(left, name)));
104
+ return basename(content) === DEX_FOLDER && holdsState
105
+ ? { ...place, old: left, folder: content }
106
+ : { ...place, folder: content };
107
+ }
108
+
109
+ /** One change the move makes, with how to make it. */
110
+ interface Change extends Step {
111
+ apply?: () => Promise<void>;
112
+ }
113
+
114
+ /** What a plan reads its changes against. */
115
+ interface Planning extends DexFolderPlace {
116
+ /** Whether upgrade moves the whole folder, old to folder. */
117
+ moves: boolean;
118
+ /** Where something in the Dex folder is until then. */
119
+ now: (name: string) => string;
120
+ /** A path as people see it, from where agents start: "apps/web/dex". */
121
+ shown: (path: string) => string;
122
+ }
123
+
124
+ const REGEXP_SPECIAL_RE = /[.*+?^${}()|[\]\\]/g;
125
+ const NEWLINE_RE = /\r?\n/;
126
+ /** A .gitignore line: a negation or anchor, its path, and spaces after. */
127
+ const IGNORE_LINE_RE = /^(\s*!?\/?)(.*?)(\s*)$/;
128
+ const CONTENT_KEY_RE = /\bcontent\s*:\s*$/;
129
+
130
+ const escaped = (text: string): string =>
131
+ text.replace(REGEXP_SPECIAL_RE, "\\$&");
132
+
133
+ const readIfThere = async (file: string): Promise<string | undefined> =>
134
+ existsSync(file) ? await readFile(file, "utf8") : undefined;
135
+
136
+ /** Files that say nothing: what a Mac leaves in a folder. */
137
+ const NOTHING = new Set([".DS_Store"]);
138
+
139
+ /** Whether `dir` holds any file but `skip` at its top and what says nothing. */
140
+ async function holdsFiles(
141
+ dir: string,
142
+ skip: readonly string[] = []
143
+ ): Promise<boolean> {
144
+ const entries = await readdir(dir, { withFileTypes: true }).catch(() => []);
145
+ for (const entry of entries) {
146
+ if (skip.includes(entry.name) || NOTHING.has(entry.name)) {
147
+ continue;
148
+ }
149
+ if (!entry.isDirectory() || (await holdsFiles(join(dir, entry.name)))) {
150
+ return true;
151
+ }
152
+ }
153
+ return false;
154
+ }
155
+
156
+ /** What a folder of Dex's has, any one of which says it is one. */
157
+ const DEX_MARKS = [
158
+ "files",
159
+ STUDIO_FILES.instructions,
160
+ STUDIO_FILES.contributors,
161
+ ...STATE_FOLDERS,
162
+ ];
163
+
164
+ const looksLikeDexFolder = (dir: string): boolean =>
165
+ DEX_MARKS.some((name) => existsSync(join(dir, name)));
166
+
167
+ /** The links at the top of `dir` that point by a relative path: name → target. */
168
+ async function relativeLinks(dir: string): Promise<Map<string, string>> {
169
+ const links = new Map<string, string>();
170
+ for (const name of await readdir(dir).catch(() => [])) {
171
+ const path = join(dir, name);
172
+ if (!(await lstat(path)).isSymbolicLink()) {
173
+ continue;
174
+ }
175
+ const link = await readlink(path);
176
+ if (!isAbsolute(link)) {
177
+ links.set(name, resolve(dir, link));
178
+ }
179
+ }
180
+ return links;
181
+ }
182
+
183
+ /**
184
+ * Move `from` to `to`, or into it when it is there: what `to` has already
185
+ * stays, and the rest joins it. A link that moved, pointing at something by
186
+ * a relative path (as skill installers link skills in), still points at it.
187
+ */
188
+ export async function moveInto(from: string, to: string): Promise<void> {
189
+ const links = await relativeLinks(from);
190
+ const kept = new Set(await readdir(to).catch(() => []));
191
+ if (existsSync(to)) {
192
+ await cp(from, to, {
193
+ recursive: true,
194
+ force: false,
195
+ errorOnExist: false,
196
+ verbatimSymlinks: true,
197
+ });
198
+ await rm(from, { recursive: true, force: true });
199
+ } else {
200
+ await rename(from, to);
201
+ }
202
+ for (const [name, target] of links) {
203
+ const landed = join(to, name);
204
+ const pointing = relative(dirname(landed), target);
205
+ if (!kept.has(name) && (await readlink(landed)) !== pointing) {
206
+ await unlink(landed);
207
+ await symlink(pointing, landed);
208
+ }
209
+ }
210
+ }
211
+
212
+ /** Remove `dir` when nothing in it says anything. */
213
+ async function removeIfEmpty(dir: string): Promise<void> {
214
+ if (existsSync(dir) && !(await holdsFiles(dir))) {
215
+ await rm(dir, { recursive: true, force: true });
216
+ }
217
+ }
218
+
219
+ // --- The folder, its state and the team's skills -------------------------
220
+
221
+ /** The whole folder, or why it can't move. */
222
+ async function folderChanges(plan: Planning, file: string): Promise<Change[]> {
223
+ const { content, folder, old, moves, shown } = plan;
224
+ if (old === undefined || old !== content) {
225
+ return [];
226
+ }
227
+ const oldThere = existsSync(old);
228
+ const taken = existsSync(folder);
229
+ if (oldThere && taken && (await holdsFiles(old, STATE_FOLDERS))) {
230
+ return [
231
+ {
232
+ kind: "stop",
233
+ does: `move ${shown(old)}/ to ${shown(folder)}/: ${shown(folder)}/ is there already, and upgrade never writes over it`,
234
+ detail: `Keep one of them: move what you want from ${shown(old)}/ into ${shown(folder)}/ and delete ${shown(old)}/, or rename ${shown(folder)}/. Then run ${UPGRADE_COMMAND} again.`,
235
+ path: old,
236
+ },
237
+ ];
238
+ }
239
+ if (!oldThere && taken && !looksLikeDexFolder(folder)) {
240
+ return [
241
+ {
242
+ kind: "stop",
243
+ does: `point the config at ${shown(folder)}/: it is there already, and isn't a Dex folder`,
244
+ detail: `Rename ${shown(folder)}/, or set content in ${basename(file)} to where Dex's files are. Then run ${UPGRADE_COMMAND} again.`,
245
+ path: folder,
246
+ },
247
+ ];
248
+ }
249
+ return moves
250
+ ? [
251
+ {
252
+ kind: "change",
253
+ does: `move ${shown(old)}/ to ${shown(folder)}/`,
254
+ path: old,
255
+ apply: () => rename(old, folder),
256
+ },
257
+ ]
258
+ : [];
259
+ }
260
+
261
+ /** The state, `.studio/` in the folder, as `.local/`; and any left behind. */
262
+ async function stateChanges(plan: Planning): Promise<Change[]> {
263
+ const { folder, old, moves, now, shown } = plan;
264
+ const state = join(folder, STATE_FOLDER);
265
+ const changes: Change[] = [];
266
+ if (existsSync(now(OLD_STATE_FOLDER))) {
267
+ const joins = existsSync(now(STATE_FOLDER));
268
+ changes.push({
269
+ kind: "change",
270
+ does: `move ${shown(now(OLD_STATE_FOLDER))}/ ${joins ? "into" : "to"} ${shown(state)}/${joins ? " (keeping what is there)" : ""}`,
271
+ path: now(OLD_STATE_FOLDER),
272
+ apply: async () => {
273
+ await moveInto(join(folder, OLD_STATE_FOLDER), state);
274
+ // Its own .gitignore and README, as Dex makes them: git leaves it
275
+ // out before Dex next starts, and before the .gitignore's line is.
276
+ await prepareState(folder);
277
+ },
278
+ });
279
+ }
280
+ // State the old folder kept once the rest moved (a pull moved it, or a
281
+ // Dex still running wrote there): it joins the folder's.
282
+ if (old === undefined || moves || !existsSync(old)) {
283
+ return changes;
284
+ }
285
+ for (const name of STATE_FOLDERS) {
286
+ const left = join(old, name);
287
+ if (existsSync(left)) {
288
+ changes.push({
289
+ kind: "change",
290
+ does: `move ${shown(left)}/ into ${shown(state)}/ (keeping what is there)`,
291
+ path: left,
292
+ apply: async () => {
293
+ await moveInto(left, state);
294
+ await prepareState(folder);
295
+ },
296
+ });
297
+ }
298
+ }
299
+ if (await holdsFiles(old, STATE_FOLDERS)) {
300
+ changes.push({
301
+ kind: "keep",
302
+ does: `${shown(old)}/ still holds files Dex no longer reads`,
303
+ detail: `Move what you want to keep into ${shown(folder)}/, then delete ${shown(old)}/.`,
304
+ path: old,
305
+ });
306
+ } else {
307
+ changes.push({
308
+ kind: "change",
309
+ does: `delete ${shown(old)}/, empty once its state moved`,
310
+ path: old,
311
+ apply: () => removeIfEmpty(old),
312
+ });
313
+ }
314
+ return changes;
315
+ }
316
+
317
+ /** The team's skills: `.dex/skills/` at the root, as `skills/` in the folder. */
318
+ function skillsChanges({ folder, now, root, shown }: Planning): Change[] {
319
+ const skills = OLD_TEAM_SKILLS.map((name) => join(root, name)).find((path) =>
320
+ existsSync(path)
321
+ );
322
+ if (!skills) {
323
+ return [];
324
+ }
325
+ const target = join(folder, STUDIO_FILES.skills);
326
+ const joins = existsSync(now(STUDIO_FILES.skills));
327
+ return [
328
+ {
329
+ kind: "change",
330
+ does: `move ${shown(skills)}/ ${joins ? "into" : "to"} ${shown(target)}/${joins ? " (keeping the skills there)" : ""}`,
331
+ path: skills,
332
+ apply: async () => {
333
+ await moveInto(skills, target);
334
+ await removeIfEmpty(dirname(skills));
335
+ },
336
+ },
337
+ ];
338
+ }
339
+
340
+ // --- The .gitignore and the tsconfig ---------------------------------------
341
+
342
+ /** Said above the old state line upgrade keeps. */
343
+ export const KEPT_STATE_COMMENT =
344
+ "# Where Dex kept this machine's state before .local/: an older Dex still running may write there.";
345
+
346
+ /**
347
+ * A .gitignore's lines with the old folder's moved to the new one, and the
348
+ * state's line (`dex/.local/`) added where the old one (`studio/.studio/`)
349
+ * is. The old line stays, under a comment: a Dex from before still running
350
+ * (the one whose Update ran upgrade, which then commits every change) writes
351
+ * there until it restarts, and a teammate's pull leaves it there, so it is
352
+ * never committed. `from` and `to` are the folder before and after, from
353
+ * the .gitignore's own folder ("." in it).
354
+ */
355
+ export function movedIgnore(text: string, from: string, to: string): string {
356
+ const prefix = (folder: string) => (folder === "." ? "" : `${folder}/`);
357
+ const state = stateIgnoreLine(to);
358
+ const oldStates = new Set(
359
+ [prefix(from), prefix(to)].flatMap((at) => [
360
+ `${at}${OLD_STATE_FOLDER}`,
361
+ `${at}${OLD_STATE_FOLDER}/`,
362
+ ])
363
+ );
364
+ const moves = from !== to && from !== ".";
365
+ const movedPath = (path: string): string =>
366
+ moves && path.startsWith(prefix(from))
367
+ ? `${prefix(to)}${path.slice(prefix(from).length)}`
368
+ : path;
369
+ const lines = text.split(NEWLINE_RE).map((line) => {
370
+ const [, head = "", path = "", tail = ""] = IGNORE_LINE_RE.exec(line) ?? [];
371
+ return { line, head, path, tail };
372
+ });
373
+ const isState = (path: string) => movedPath(path) === state;
374
+ let placed = lines.some(({ path }) => isState(path));
375
+ const kept: string[] = [];
376
+ for (const { line, head, path, tail } of lines) {
377
+ if (!path || path.startsWith("#")) {
378
+ kept.push(line);
379
+ } else if (oldStates.has(path)) {
380
+ if (!placed) {
381
+ kept.push(`${head}${state}${tail}`, KEPT_STATE_COMMENT);
382
+ placed = true;
383
+ }
384
+ kept.push(line);
385
+ } else if (!(isState(path) && kept.some((each) => each.trim() === state))) {
386
+ // A line both names had (`create` run again added `studio/.local/`
387
+ // beside `studio/.studio/`) is kept once.
388
+ kept.push(`${head}${movedPath(path)}${tail}`);
389
+ }
390
+ }
391
+ return kept.join(text.includes("\r\n") ? "\r\n" : "\n");
392
+ }
393
+
394
+ /** Every folder from `dir` up to `top`, nearest first (`dir` alone outside it). */
395
+ function foldersUpTo(dir: string, top: string): string[] {
396
+ const folders = [resolve(dir)];
397
+ if (relative(top, dir).startsWith("..")) {
398
+ return folders;
399
+ }
400
+ for (let up = folders[0]; up !== resolve(top) && dirname(up) !== up; ) {
401
+ up = dirname(up);
402
+ folders.push(up);
403
+ }
404
+ return folders;
405
+ }
406
+
407
+ /** The .gitignore files that name the old folder or the state's old name. */
408
+ async function ignoreChanges(plan: Planning): Promise<Change[]> {
409
+ const { agentRoot, folder, old, root, now, shown } = plan;
410
+ const dirs = new Set([
411
+ folder,
412
+ ...foldersUpTo(dirname(folder), agentRoot),
413
+ resolve(root),
414
+ ]);
415
+ const changes: Change[] = [];
416
+ for (const dir of dirs) {
417
+ // The folder's own .gitignore is in the old folder until it moves.
418
+ const inFolder = dir === folder;
419
+ const path = inFolder ? now(".gitignore") : join(dir, ".gitignore");
420
+ const text = await readIfThere(path);
421
+ const from = inFolder ? "." : posix(relative(dir, old ?? folder));
422
+ const to = inFolder ? "." : posix(relative(dir, folder));
423
+ const updated = text === undefined ? text : movedIgnore(text, from, to);
424
+ if (updated !== undefined && updated !== text) {
425
+ const target = join(dir, ".gitignore");
426
+ changes.push({
427
+ kind: "change",
428
+ does: `ignore ${stateIgnoreLine(to)} in ${shown(target)}${from === to ? "" : `, and ${to}/ where it said ${from}/`}`,
429
+ path,
430
+ apply: () => writeFile(target, updated),
431
+ });
432
+ }
433
+ }
434
+ return changes;
435
+ }
436
+
437
+ const TSCONFIG_STATE_RE = new RegExp(
438
+ `(["'])(\\./)?${escaped(OLD_STATE_FOLDER)}(/[^"']*)?\\1`,
439
+ "g"
440
+ );
441
+
442
+ /** The folder's tsconfig, which leaves the state out of type checks. */
443
+ async function tsconfigChanges({
444
+ folder,
445
+ now,
446
+ shown,
447
+ }: Planning): Promise<Change[]> {
448
+ const path = now("tsconfig.json");
449
+ const text = await readIfThere(path);
450
+ const updated = text?.replace(
451
+ TSCONFIG_STATE_RE,
452
+ (_match, quote: string, dot = "", rest = "") =>
453
+ `${quote}${dot}${STATE_FOLDER}${rest}${quote}`
454
+ );
455
+ if (updated === undefined || updated === text) {
456
+ return [];
457
+ }
458
+ const target = join(folder, "tsconfig.json");
459
+ return [
460
+ {
461
+ kind: "change",
462
+ does: `exclude ${STATE_FOLDER} in ${shown(target)}, not ${OLD_STATE_FOLDER}`,
463
+ path,
464
+ apply: () => writeFile(target, updated),
465
+ },
466
+ ];
467
+ }
468
+
469
+ // --- Agents' configs, scripts and the config -------------------------------
470
+
471
+ /**
472
+ * `text` with a `--root` that names `from` naming `to`: as a server's
473
+ * arguments have it ("--root", "studio"), or a command
474
+ * (--root \"$CLAUDE_PROJECT_DIR/studio\", --root studio, --root=./studio).
475
+ */
476
+ export function movedRoot(text: string, from: string, to: string): string {
477
+ const root = new RegExp(
478
+ `(--root(?:\\\\?["'])?(?:\\s*,\\s*|\\s+|=)(?:\\\\?["'])?(?:\\$CLAUDE_PROJECT_DIR/)?(?:\\./)?)${escaped(from)}(?=\\\\?["']|[\\s,\\]]|$)`,
479
+ "g"
480
+ );
481
+ return text.replace(root, (_match, head: string) => `${head}${to}`);
482
+ }
483
+
484
+ /** A script with what names `from` naming `to`: `--root`, `cd`, and paths in it. */
485
+ export function movedScript(script: string, from: string, to: string): string {
486
+ const cd = new RegExp(
487
+ `(\\bcd\\s+(?:\\./)?)${escaped(from)}(?=/|\\s|$|[&;|])`,
488
+ "g"
489
+ );
490
+ const path = new RegExp(`(^|[\\s"'=])(\\./)?${escaped(from)}/`, "g");
491
+ return movedRoot(script, from, to)
492
+ .replace(cd, (_match, head: string) => `${head}${to}`)
493
+ .replace(path, (_match, head: string, dot = "") => `${head}${dot}${to}/`);
494
+ }
495
+
496
+ /** The guides at the agent root that setup points at the Dex folder. */
497
+ const ROOT_GUIDES = ["AGENTS.md", "CLAUDE.md"];
498
+
499
+ /**
500
+ * A root guide's pointer at the Dex folder ("Dex files live in studio/."),
501
+ * as this version and older ones wrote it, naming `to` for `from`.
502
+ */
503
+ export function movedPointer(text: string, from: string, to: string): string {
504
+ const place = new RegExp(
505
+ `((?:Dex files live in|Sudio files live in|Prototyping happens in) )${escaped(from)}(?=/\\.)|(Read )${escaped(from)}(?=/AGENTS\\.md)`,
506
+ "g"
507
+ );
508
+ return text.replace(
509
+ place,
510
+ (_match, sentence: string | undefined, read: string | undefined) =>
511
+ `${sentence ?? read ?? ""}${to}`
512
+ );
513
+ }
514
+
515
+ /** Whether the config still names the old folder, so what names it moves. */
516
+ const stillNamed = ({ content, folder, old }: Planning): boolean =>
517
+ old !== undefined && old !== folder && old === content;
518
+
519
+ /**
520
+ * Where agents' configs and the root guide may be: the agent root, and the
521
+ * project itself, which keeps its own when it was set up before agents'
522
+ * configs moved to the workspace's root.
523
+ */
524
+ const agentFolders = ({ agentRoot, root }: Planning): string[] => [
525
+ ...new Set([resolve(agentRoot), resolve(root)]),
526
+ ];
527
+
528
+ /** The agent configs that start Dex at the old folder. */
529
+ async function agentChanges(plan: Planning): Promise<Change[]> {
530
+ const { folder, old, shown } = plan;
531
+ if (!stillNamed(plan) || old === undefined) {
532
+ return [];
533
+ }
534
+ const changes: Change[] = [];
535
+ for (const dir of agentFolders(plan)) {
536
+ const from = posix(relative(dir, old));
537
+ const to = posix(relative(dir, folder));
538
+ for (const name of new Set(Object.values(AGENT_FILES).flat())) {
539
+ const path = join(dir, name);
540
+ const text = await readIfThere(path);
541
+ const updated =
542
+ text === undefined
543
+ ? text
544
+ : movedRoot(movedRoot(text, from, to), old, folder);
545
+ if (updated !== undefined && updated !== text) {
546
+ changes.push({
547
+ kind: "change",
548
+ does: `start Dex at ${to} in ${shown(path)}`,
549
+ path,
550
+ apply: () => writeFile(path, updated),
551
+ });
552
+ }
553
+ }
554
+ }
555
+ return changes;
556
+ }
557
+
558
+ /** The root guides whose pointer names the old folder. */
559
+ async function guideChanges(plan: Planning): Promise<Change[]> {
560
+ const { folder, old, shown } = plan;
561
+ if (!stillNamed(plan) || old === undefined) {
562
+ return [];
563
+ }
564
+ const changes: Change[] = [];
565
+ for (const dir of agentFolders(plan)) {
566
+ const from = posix(relative(dir, old));
567
+ const to = posix(relative(dir, folder));
568
+ for (const name of ROOT_GUIDES) {
569
+ const path = join(dir, name);
570
+ const text = await readIfThere(path);
571
+ const updated = text === undefined ? text : movedPointer(text, from, to);
572
+ if (updated !== undefined && updated !== text) {
573
+ changes.push({
574
+ kind: "change",
575
+ does: `point ${shown(path)} at ${to}/`,
576
+ path,
577
+ apply: () => writeFile(path, updated),
578
+ });
579
+ }
580
+ }
581
+ }
582
+ return changes;
583
+ }
584
+
585
+ /** The scripts in `dir`'s package.json that would name the old folder. */
586
+ async function scriptsIn(
587
+ dir: string,
588
+ old: string,
589
+ folder: string
590
+ ): Promise<Record<string, string>> {
591
+ let scripts: Record<string, unknown> = {};
592
+ try {
593
+ const text = await readIfThere(join(dir, "package.json"));
594
+ scripts =
595
+ (JSON.parse(text ?? "{}") as { scripts?: Record<string, unknown> })
596
+ .scripts ?? {};
597
+ } catch {
598
+ // Not JSON: nothing upgrade could edit.
599
+ return {};
600
+ }
601
+ const from = posix(relative(dir, old));
602
+ const to = posix(relative(dir, folder));
603
+ return Object.fromEntries(
604
+ Object.entries(scripts).flatMap(([name, script]): [string, string][] => {
605
+ if (typeof script !== "string") {
606
+ return [];
607
+ }
608
+ const updated = movedScript(script, from, to);
609
+ return updated === script ? [] : [[name, updated]];
610
+ })
611
+ );
612
+ }
613
+
614
+ /** The scripts that name the old folder, in the project and the agent root. */
615
+ async function scriptChanges(plan: Planning): Promise<Change[]> {
616
+ const { folder, old, shown } = plan;
617
+ if (!stillNamed(plan) || old === undefined) {
618
+ return [];
619
+ }
620
+ const changes: Change[] = [];
621
+ for (const dir of agentFolders(plan)) {
622
+ const path = join(dir, "package.json");
623
+ const moved = await scriptsIn(dir, old, folder);
624
+ const names = Object.keys(moved).map((name) => `"${name}"`);
625
+ if (names.length > 0) {
626
+ changes.push({
627
+ kind: "change",
628
+ does: `point ${shown(path)} ${names.length === 1 ? "script" : "scripts"} ${listing(names)} at ${shown(folder)}`,
629
+ path,
630
+ apply: () =>
631
+ editManifest(path, (manifest) => {
632
+ manifest.scripts = {
633
+ ...(manifest.scripts as Record<string, string>),
634
+ ...moved,
635
+ };
636
+ }),
637
+ });
638
+ }
639
+ }
640
+ return changes;
641
+ }
642
+
643
+ /**
644
+ * The config with its paths into the old folder pointing into the new one:
645
+ * `content: "./studio"`, and any "./studio/…" (a guide, a stylesheet). A
646
+ * plain "studio" is changed only as `content`, as `ui.theme` takes
647
+ * "studio" too.
648
+ */
649
+ export function movedConfig(
650
+ source: string,
651
+ oldPath: string,
652
+ newPath: string
653
+ ): string {
654
+ const literal = new RegExp(
655
+ `(["'\`])(\\./)?${escaped(oldPath)}(/[^"'\`\\n]*)?\\1`,
656
+ "g"
657
+ );
658
+ return source.replace(
659
+ literal,
660
+ (
661
+ match,
662
+ quote: string,
663
+ dot: string | undefined,
664
+ rest: string | undefined,
665
+ offset: number
666
+ ) => {
667
+ const isContent = CONTENT_KEY_RE.test(source.slice(0, offset));
668
+ return dot || rest || isContent
669
+ ? `${quote}${dot ?? ""}${newPath}${rest ?? ""}${quote}`
670
+ : match;
671
+ }
672
+ );
673
+ }
674
+
675
+ /** The config pointed at the new folder, or why upgrade can't. */
676
+ async function configChanges(plan: Planning): Promise<Change[]> {
677
+ const { file, root, content, folder, old } = plan;
678
+ if (old === undefined || old !== content) {
679
+ return [];
680
+ }
681
+ const source = await readFile(file, "utf8");
682
+ const to = posix(relative(root, folder));
683
+ const name = basename(file);
684
+ const updated = movedConfig(source, posix(relative(root, content)), to);
685
+ let pointed: unknown;
686
+ try {
687
+ pointed = configData(updated).data.content;
688
+ } catch (error) {
689
+ if (!(error instanceof ConfigNotEditable)) {
690
+ throw error;
691
+ }
692
+ }
693
+ if (typeof pointed !== "string" || resolve(root, pointed) !== folder) {
694
+ return [
695
+ {
696
+ kind: "stop",
697
+ does: `point ${name} at ./${to}: upgrade can't edit how ${name} sets content`,
698
+ detail: `Set content in ${name} to "./${to}", then run ${UPGRADE_COMMAND} again.`,
699
+ path: file,
700
+ },
701
+ ];
702
+ }
703
+ return [
704
+ {
705
+ kind: "change",
706
+ does: `point ${name} at ./${to}`,
707
+ path: file,
708
+ apply: () => writeFile(file, updated),
709
+ },
710
+ ];
711
+ }
712
+
713
+ // --- The plan ---------------------------------------------------------------
714
+
715
+ /** What a plan reads against: the place, and where things are until the move. */
716
+ function planning(project: StudioProject): Planning {
717
+ const place = dexFolderPlace(project);
718
+ const { agentRoot, content, folder, old } = place;
719
+ const moves =
720
+ old !== undefined &&
721
+ old === content &&
722
+ existsSync(old) &&
723
+ !existsSync(folder);
724
+ return {
725
+ ...place,
726
+ moves,
727
+ now: (name) => join(moves && old ? old : folder, name),
728
+ shown: (path) => posix(relative(agentRoot, path)) || ".",
729
+ };
730
+ }
731
+
732
+ /**
733
+ * Every change the move makes, in the order upgrade makes them: the
734
+ * folder, what is in it, what names it, and last the config. A plan that
735
+ * stops is that alone.
736
+ */
737
+ async function changesFor(project: StudioProject): Promise<Change[]> {
738
+ const plan = planning(project);
739
+ const steps: Change[] = [
740
+ ...(await folderChanges(plan, project.file)),
741
+ ...(await stateChanges(plan)),
742
+ ...skillsChanges(plan),
743
+ ...(await ignoreChanges(plan)),
744
+ ...(await tsconfigChanges(plan)),
745
+ ...(await agentChanges(plan)),
746
+ ...(await guideChanges(plan)),
747
+ ...(await scriptChanges(plan)),
748
+ ...(await configChanges(plan)),
749
+ ];
750
+ const stopped = steps.filter(({ kind }) => kind === "stop");
751
+ return stopped.length > 0 ? stopped : steps;
752
+ }
753
+
754
+ /** The move in a few words: "studio/ → dex/", or "dex/" when it stays. */
755
+ function moveSaid(project: StudioProject): string {
756
+ const { old, folder, shown } = planning(project);
757
+ return old && old !== folder
758
+ ? `${shown(old)}/ → ${shown(folder)}/`
759
+ : `${shown(folder)}/`;
760
+ }
761
+
762
+ /** What the project still has from before, for doctor: "studio/, .dex/skills/". */
763
+ function leftFromBefore(project: StudioProject): string[] {
764
+ const { content, folder, old, now, root, shown } = planning(project);
765
+ return [
766
+ ...(old !== undefined && old === content ? [`${shown(old)}/`] : []),
767
+ ...(existsSync(now(OLD_STATE_FOLDER))
768
+ ? [`${shown(now(OLD_STATE_FOLDER))}/`]
769
+ : []),
770
+ ...(old !== undefined && old !== content && existsSync(old)
771
+ ? [`${shown(old)}/`]
772
+ : []),
773
+ ...OLD_TEAM_SKILLS.map((name) => join(root, name))
774
+ .filter((path) => existsSync(path))
775
+ .map((path) => `${shown(path)}/`),
776
+ ].filter((path) => path !== `${shown(folder)}/`);
777
+ }
778
+
779
+ /**
780
+ * Whether the plan changes what the team shares: anything but this
781
+ * machine's state, and the old folder a pull left it in (once the config
782
+ * names dex/, a studio/ is only ever that). Read from the plan alone, as
783
+ * upgrade reports after the move.
784
+ */
785
+ function sharesAny(steps: Step[], { content }: StudioProject): boolean {
786
+ const moved = basename(content) !== OLD_DEX_FOLDER;
787
+ return steps.some(({ kind, path = "" }) => {
788
+ const name = basename(path);
789
+ return (
790
+ kind === "change" &&
791
+ !STATE_FOLDERS.includes(name) &&
792
+ !(moved && name === OLD_DEX_FOLDER)
793
+ );
794
+ });
795
+ }
796
+
797
+ /** studio/ moves to dex/, its state to .local/, and the team's skills into it. */
798
+ export const dexFolderMigration: Migration = {
799
+ name: "dex folder",
800
+ plan: async (project) =>
801
+ (await changesFor(project)).map(({ apply: _apply, ...step }) => step),
802
+ apply: async (project) => {
803
+ for (const change of await changesFor(project)) {
804
+ await change.apply?.();
805
+ }
806
+ },
807
+ report: (steps, { dryRun, project }) => {
808
+ if (steps.length === 0) {
809
+ return [];
810
+ }
811
+ const stopped = steps.some(({ kind }) => kind === "stop");
812
+ const lines = [
813
+ `Dex folder${dryRun ? " (would move)" : ""}: ${moveSaid(project)}`,
814
+ ...steps.flatMap(({ does, detail, kind }) => [
815
+ ` ${kind === "keep" ? "left: " : ""}${does}`,
816
+ ...(detail ? [` ${detail}`] : []),
817
+ ]),
818
+ "",
819
+ ];
820
+ if (!(dryRun || stopped) && sharesAny(steps, project)) {
821
+ lines.push(
822
+ "Dex keeps a project's files in dex/ now. Commit the move so your team gets it: each machine moves its own .local/ as it next upgrades.",
823
+ ""
824
+ );
825
+ }
826
+ return lines;
827
+ },
828
+ byHand: (steps) =>
829
+ steps
830
+ .filter(({ kind }) => kind === "keep")
831
+ .map(({ does, detail }) => ({ message: does, fix: detail ?? "" })),
832
+ warning: (steps, project) => {
833
+ const left = leftFromBefore(project);
834
+ return {
835
+ message: `Dex's folder is dex/ now, with its state in .local/ and the team's skills in it${left.length > 0 ? `, and this project still has ${listing(left)}` : ": files here still name the folders from before"}`,
836
+ fix: `Run ${UPGRADE_COMMAND} to ${steps
837
+ .filter(({ kind }) => kind !== "keep")
838
+ .map(({ does }) => does)
839
+ .join("; ")}.`,
840
+ };
841
+ },
842
+ };