@kabmo-labs/kabmo 0.28.0 → 0.30.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 (232) hide show
  1. package/bunfig.toml +6 -0
  2. package/dist/client/index.html +2 -2
  3. package/dist/client/index.js +481 -472
  4. package/dist/client/static/js/{index-xzvyrxya.js → index-5vjbp04s.js} +1193 -1049
  5. package/dist/client/static/js/index-naa91727.css +1 -0
  6. package/dist/client/static/{manifest-q6h8k9e0.webmanifest → manifest-vdvdqpre.webmanifest} +1 -0
  7. package/package.json +3 -2
  8. package/public/manifest.webmanifest +1 -0
  9. package/src/client/components/common/ComposerTextarea.test.tsx +109 -0
  10. package/src/client/components/common/ComposerTextarea.tsx +6 -0
  11. package/src/client/components/common/ReloadPageButton.test.tsx +14 -0
  12. package/src/client/components/common/ReloadPageButton.tsx +11 -2
  13. package/src/client/components/common/diagram-viewer/read-block.test.ts +71 -0
  14. package/src/client/components/common/diagram-viewer/read-block.ts +40 -16
  15. package/src/client/components/common/file-icon/index.tsx +7 -1
  16. package/src/client/components/common/harness-glyph/brands.ts +7 -1
  17. package/src/client/components/common/image-viewer/index.tsx +37 -165
  18. package/src/client/components/common/picker-search/index.tsx +70 -0
  19. package/src/client/components/common/search-select/index.tsx +2 -7
  20. package/src/client/components/common/zoom-pan-stage/index.tsx +258 -0
  21. package/src/client/components/common/zoom-pan-stage/zoom-pan-stage.test.tsx +87 -0
  22. package/src/client/components/layout/session-sidebar/ClassicView.tsx +1 -1
  23. package/src/client/components/layout/session-sidebar/SessionListNotice.tsx +7 -3
  24. package/src/client/components/layout/session-sidebar/about-modal/HarnessRow.tsx +4 -1
  25. package/src/client/components/layout/session-sidebar/about-modal/HarnessTable.tsx +12 -1
  26. package/src/client/components/layout/session-sidebar/about-modal/UpdateLogLine.test.tsx +82 -0
  27. package/src/client/components/layout/session-sidebar/about-modal/UpdateLogLine.tsx +23 -3
  28. package/src/client/components/layout/session-sidebar/about-modal/index.tsx +1 -0
  29. package/src/client/components/layout/session-sidebar/modern/index.tsx +1 -1
  30. package/src/client/components/layout/session-sidebar/scheduler-modal/TaskList.tsx +6 -4
  31. package/src/client/components/layout/session-sidebar/session-list-states.test.tsx +8 -0
  32. package/src/client/components/mobile/mobile-right-sidebar/EditorSurface.tsx +25 -0
  33. package/src/client/components/mobile/mobile-session-sidebar/ClassicDrawer.tsx +1 -1
  34. package/src/client/components/settings/Sidebar.tsx +6 -8
  35. package/src/client/components/settings/categories/HarnessSettings.tsx +4 -2
  36. package/src/client/components/settings/categories/PanelPluginSettings.tsx +2 -2
  37. package/src/client/components/settings/categories/PluginSettings.tsx +1 -1
  38. package/src/client/components/settings/categories/harness-settings/ConfigForm.tsx +3 -6
  39. package/src/client/components/settings/categories/panel-plugins/GitFreshness.test.tsx +36 -0
  40. package/src/client/components/settings/categories/panel-plugins/Section.tsx +1 -3
  41. package/src/client/components/settings/categories/panel-plugins/Toolbar.tsx +18 -4
  42. package/src/client/components/settings/categories/plugin-settings/MarketplacePane.tsx +64 -61
  43. package/src/client/components/settings/categories/plugin-settings/SidebarList.tsx +15 -4
  44. package/src/client/components/settings/groups.test.tsx +2 -2
  45. package/src/client/components/workspace/chat-timeline/IrcNotice.test.tsx +55 -0
  46. package/src/client/components/workspace/chat-timeline/IrcNotice.tsx +71 -0
  47. package/src/client/components/workspace/chat-timeline/MessageItem.tsx +4 -1
  48. package/src/client/components/workspace/chat-timeline/chat-input/Toolbar.test.ts +19 -0
  49. package/src/client/components/workspace/chat-timeline/chat-input/Toolbar.tsx +8 -3
  50. package/src/client/components/workspace/chat-timeline/chat-input/WaitingDependencyPicker.search.test.tsx +72 -0
  51. package/src/client/components/workspace/chat-timeline/chat-input/WaitingDependencyPicker.tsx +42 -15
  52. package/src/client/components/workspace/chat-timeline/target-picker/ParentSessionPicker.test.tsx +28 -0
  53. package/src/client/components/workspace/chat-timeline/target-picker/ParentSessionPicker.tsx +28 -10
  54. package/src/client/components/workspace/chat-timeline/target-picker/WorkspacePicker.test.tsx +47 -1
  55. package/src/client/components/workspace/chat-timeline/target-picker/WorkspacePicker.tsx +55 -5
  56. package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Wait.tsx +3 -2
  57. package/src/client/components/workspace/editor/FilePane.tsx +32 -0
  58. package/src/client/components/workspace/editor/Toolbar.tsx +4 -0
  59. package/src/client/components/workspace/editor/toolbar-clusters.test.ts +1 -0
  60. package/src/client/components/workspace/editor/toolbar-clusters.tsx +5 -2
  61. package/src/client/components/workspace/editor/viewers/CsvGrid.tsx +52 -5
  62. package/src/client/components/workspace/editor/viewers/DataGrid.tsx +227 -41
  63. package/src/client/components/workspace/editor/viewers/DrawioView.tsx +93 -0
  64. package/src/client/components/workspace/editor/viewers/GridRow.tsx +91 -0
  65. package/src/client/components/workspace/editor/viewers/GridToolbar.tsx +53 -0
  66. package/src/client/components/workspace/editor/viewers/SheetView.tsx +152 -57
  67. package/src/client/components/workspace/editor/viewers/dispatch.tsx +42 -9
  68. package/src/client/components/workspace/editor/viewers/drawio-pages.test.ts +80 -0
  69. package/src/client/components/workspace/editor/viewers/drawio-pages.ts +63 -0
  70. package/src/client/components/workspace/editor/viewers/grid.test.ts +296 -0
  71. package/src/client/components/workspace/editor/viewers/use-cell-edit.ts +64 -0
  72. package/src/client/components/workspace/editor/viewers/use-grid-interaction.ts +216 -0
  73. package/src/client/components/workspace/editor/viewers/viewers.test.ts +47 -2
  74. package/src/client/components/workspace/editor/viewers/workbook.ts +149 -0
  75. package/src/client/components/workspace/file-explorer/ArchiveModals.tsx +262 -0
  76. package/src/client/components/workspace/file-explorer/ContextMenu.tsx +30 -6
  77. package/src/client/components/workspace/file-explorer/Modals.tsx +8 -3
  78. package/src/client/components/workspace/file-explorer/RootOverlays.tsx +60 -0
  79. package/src/client/components/workspace/file-explorer/RowLabel.tsx +6 -2
  80. package/src/client/components/workspace/file-explorer/TreeItem.tsx +39 -49
  81. package/src/client/components/workspace/file-explorer/TreeItemModals.tsx +80 -0
  82. package/src/client/components/workspace/file-explorer/index.tsx +53 -29
  83. package/src/client/components/workspace/file-explorer/selection.test.tsx +109 -0
  84. package/src/client/data/settings/omp-schema.json +169 -22
  85. package/src/client/glass-palette.test.ts +72 -0
  86. package/src/client/glass.css +39 -14
  87. package/src/client/glass.test.ts +28 -67
  88. package/src/client/hooks/chat/composer/history.test.ts +267 -0
  89. package/src/client/hooks/chat/composer/history.ts +146 -0
  90. package/src/client/hooks/chat/timeline/ask-frames.test.ts +54 -0
  91. package/src/client/hooks/chat/timeline/ask-frames.ts +29 -1
  92. package/src/client/hooks/chat/timeline/timeline-rail.test.ts +34 -2
  93. package/src/client/hooks/chat/timeline/user-turns.ts +31 -2
  94. package/src/client/hooks/editor/file-editor-types.ts +15 -0
  95. package/src/client/hooks/editor/save-bytes.ts +79 -0
  96. package/src/client/hooks/editor/use-dirty-tracking.test.ts +73 -0
  97. package/src/client/hooks/editor/use-dirty-tracking.ts +96 -0
  98. package/src/client/hooks/editor/use-file-cache.ts +1 -0
  99. package/src/client/hooks/editor/use-file-editor.test.ts +30 -0
  100. package/src/client/hooks/editor/use-file-editor.ts +20 -17
  101. package/src/client/hooks/editor/use-file-read.ts +21 -2
  102. package/src/client/hooks/editor/use-grid-selection.ts +160 -0
  103. package/src/client/hooks/models/use-harness-update.test.ts +35 -2
  104. package/src/client/hooks/models/use-harness-update.ts +49 -10
  105. package/src/client/hooks/workspace/diff-file.test.ts +72 -0
  106. package/src/client/hooks/workspace/diff-file.ts +12 -6
  107. package/src/client/hooks/workspace/file-archive.ts +220 -0
  108. package/src/client/hooks/workspace/file-clipboard.test.ts +4 -4
  109. package/src/client/hooks/workspace/file-clipboard.ts +37 -26
  110. package/src/client/hooks/workspace/file-selection.test.ts +67 -0
  111. package/src/client/hooks/workspace/file-selection.ts +156 -0
  112. package/src/client/hooks/workspace/file-tree-actions-clipboard.test.ts +5 -5
  113. package/src/client/hooks/workspace/file-tree-actions.ts +36 -8
  114. package/src/client/lib/editor/workbook-drafts.ts +42 -0
  115. package/src/client/lib/launch/file-handlers.ts +2 -2
  116. package/src/client/lib/scrollbar-activity.test.ts +21 -88
  117. package/src/client/lib/scrollbar-activity.ts +71 -73
  118. package/src/client/lib/scrollbar-events.ts +143 -0
  119. package/src/client/lib/scrollbar-input.test.ts +126 -0
  120. package/src/client/lib/scrollbar-probe.test-util.ts +107 -0
  121. package/src/client/tailwind.css +32 -1
  122. package/src/server/db.server.test.ts +50 -1
  123. package/src/server/db.server.ts +36 -3
  124. package/src/server/lib/chat/prompt-history.server.test.ts +92 -0
  125. package/src/server/lib/chat/prompt-history.server.ts +65 -0
  126. package/src/server/lib/chat/schema.server.ts +35 -0
  127. package/src/server/lib/db/schema.ts +3 -0
  128. package/src/server/lib/fs/archive.test.ts +198 -0
  129. package/src/server/lib/fs/archive.ts +278 -0
  130. package/src/server/lib/fs/git-diff.test.ts +26 -1
  131. package/src/server/lib/fs/git-diff.ts +43 -15
  132. package/src/server/lib/harness/launch-table.ts +19 -0
  133. package/src/server/lib/harness/provider-entry.ts +1 -1
  134. package/src/server/lib/harness/provider-file.server.ts +1 -1
  135. package/src/server/lib/harness/provider-sources.server.ts +2 -1
  136. package/src/server/lib/harness/provider-write.server.ts +2 -1
  137. package/src/server/lib/harness/update-job-store.ts +12 -0
  138. package/src/server/lib/harness/update-queue.server.ts +15 -10
  139. package/src/server/lib/omp/rpc/frame-fold-signals.test.ts +5 -1
  140. package/src/server/lib/omp/rpc/frame-fold.test.ts +5 -1
  141. package/src/server/lib/omp/rpc/lifecycle.test.ts +4 -0
  142. package/src/server/lib/omp/session/auto-title.server.test.ts +4 -0
  143. package/src/server/lib/omp/session/delete.server.ts +1 -0
  144. package/src/server/lib/omp/session/goal-driver.test.ts +5 -1
  145. package/src/server/lib/omp/session/goal-settings.test.ts +5 -1
  146. package/src/server/lib/panels/auto-update.server.test.ts +5 -2
  147. package/src/server/lib/panels/git-update.server.test.ts +5 -2
  148. package/src/server/lib/panels/install.server.test.ts +5 -2
  149. package/src/server/lib/panels/remove.server.test.ts +5 -2
  150. package/src/server/routes/agent/command.ts +11 -0
  151. package/src/server/routes/agent/new.ts +5 -0
  152. package/src/server/routes/chat/index.ts +5 -0
  153. package/src/server/routes/chat/prompt-history.test.ts +67 -0
  154. package/src/server/routes/chat/prompt-history.ts +31 -0
  155. package/src/server/routes/chat/stream.test.ts +21 -0
  156. package/src/server/routes/chat/stream.ts +5 -0
  157. package/src/server/routes/fs/action-archive.test.ts +128 -0
  158. package/src/server/routes/fs/action-bulk.ts +106 -0
  159. package/src/server/routes/fs/action-save-bytes.test.ts +59 -0
  160. package/src/server/routes/fs/action-transfer.test.ts +26 -0
  161. package/src/server/routes/fs/action.ts +91 -11
  162. package/src/server/routes/fs/archive.ts +20 -0
  163. package/src/server/routes/fs/grant.test.ts +5 -2
  164. package/src/server/routes/fs/index.ts +2 -0
  165. package/src/server/routes/fs/scope.test.ts +6 -2
  166. package/src/server/routes/harness/import-action.ts +1 -1
  167. package/src/server/routes/harness/root.ts +6 -1
  168. package/src/server/routes/harness/summary.test.ts +50 -0
  169. package/src/server/routes/harness/update-status.ts +41 -2
  170. package/src/server/routes/harness/update.test.ts +56 -2
  171. package/src/server/routes/sessions/delete.test.ts +4 -0
  172. package/src/shared/lib/chat/ask-form/index.ts +39 -31
  173. package/src/shared/lib/chat/ask-form/labels.ts +46 -0
  174. package/src/shared/lib/chat/ask-form/tokens.ts +11 -2
  175. package/src/shared/lib/chat/ask-form.test.ts +57 -0
  176. package/src/shared/lib/chat/ask-questions.ts +31 -0
  177. package/src/shared/lib/chat/composer/history.test.ts +67 -0
  178. package/src/shared/lib/chat/composer/history.ts +61 -0
  179. package/src/shared/lib/chat/irc-notice.ts +42 -0
  180. package/src/shared/lib/chat/timeline/fork-anchor.test.ts +2 -0
  181. package/src/shared/lib/chat/todo/parser.ts +6 -4
  182. package/src/shared/lib/fs/archive.test.ts +101 -0
  183. package/src/shared/lib/fs/archive.ts +191 -0
  184. package/src/shared/lib/fs/clipboard.test.ts +32 -7
  185. package/src/shared/lib/fs/clipboard.ts +59 -14
  186. package/src/shared/lib/fs/csv.test.ts +50 -0
  187. package/src/shared/lib/fs/csv.ts +40 -0
  188. package/src/shared/lib/fs/document-kind.test.ts +13 -6
  189. package/src/shared/lib/fs/document-kind.ts +18 -6
  190. package/src/shared/lib/fs/file-kind-classify.test.ts +4 -0
  191. package/src/shared/lib/fs/file-kind.test.ts +2 -7
  192. package/src/shared/lib/fs/file-kind.ts +4 -2
  193. package/src/shared/lib/fs/grid-edit.test.ts +24 -0
  194. package/src/shared/lib/fs/grid-edit.ts +29 -0
  195. package/src/shared/lib/fs/grid-selection.test.ts +137 -0
  196. package/src/shared/lib/fs/grid-selection.ts +138 -0
  197. package/src/shared/lib/harness/brand-color.ts +5 -0
  198. package/src/shared/lib/harness/catalog.test.ts +22 -0
  199. package/src/shared/lib/harness/catalog.ts +12 -1
  200. package/src/shared/lib/harness/config-fields.test.ts +25 -0
  201. package/src/shared/lib/harness/config-fields.ts +33 -3
  202. package/src/shared/lib/harness/dialects.ts +1 -1
  203. package/src/shared/lib/harness/mcp-facts.ts +4 -0
  204. package/src/shared/lib/harness/provider-contracts.ts +109 -0
  205. package/src/shared/lib/harness/provider-facts.test.ts +2 -1
  206. package/src/shared/lib/harness/provider-facts.ts +24 -115
  207. package/src/shared/lib/harness/provider-lookup.ts +37 -0
  208. package/src/shared/lib/harness/providers.ts +2 -1
  209. package/src/shared/lib/harness/updates.ts +10 -0
  210. package/src/shared/lib/icons/tint-hues.generated.ts +14 -13
  211. package/src/shared/lib/omp/session/mapper.test.ts +31 -0
  212. package/src/shared/lib/omp/session/mapper.ts +17 -0
  213. package/src/shared/lib/omp/session/messages-map-notice.test.ts +68 -0
  214. package/src/shared/lib/omp/session/messages-map.test.ts +1 -35
  215. package/src/shared/lib/omp/session/messages-map.ts +17 -0
  216. package/src/shared/lib/omp/session/wait.ts +10 -1
  217. package/src/shared/lib/settings/sections.ts +8 -4
  218. package/src/shared/lib/ui/picker-filter.test.ts +23 -0
  219. package/src/shared/lib/ui/picker-filter.ts +20 -0
  220. package/src/shared/types/chat.ts +4 -0
  221. package/src/shared/types/harness.ts +5 -1
  222. package/src/shared/types/omp/session.ts +18 -0
  223. package/src/shared/types/settings/state.ts +6 -6
  224. package/src/shared/types/todo.ts +3 -1
  225. package/src/test-support/file-actions.test-util.ts +4 -0
  226. package/src/test-support/file-archive.test-util.ts +41 -0
  227. package/src/test-support/file-clipboard.test-util.ts +9 -6
  228. package/src/test-support/file-editor.test-util.ts +3 -2
  229. package/src/test-support/glass-css.ts +30 -0
  230. package/src/test-support/harness-table.test-util.ts +2 -0
  231. package/src/test-support/preload.ts +46 -0
  232. package/dist/client/static/js/index-81f7berm.css +0 -1
@@ -74,10 +74,16 @@ export interface HarnessUpdateController {
74
74
  /** Start an update; resolves to a refusal, or null when the run was started
75
75
  * (immediately or queued behind the one already installing). */
76
76
  start: (id: string) => Promise<string | null>;
77
+ /** Drop a settled record (the result is read and has nothing left to say);
78
+ * resolves to the refusal, or null when the record was deleted. */
79
+ dismiss: (id: string) => Promise<string | null>;
77
80
  }
78
81
 
79
82
  type Settled = (message: string, ok: boolean) => void;
80
83
 
84
+ /** One run's identity: a new update for the same harness replaces the record. */
85
+ const runKey = (job: HarnessUpdateJob): string => `${job.harness}:${job.startedAt}`;
86
+
81
87
  export function useHarnessUpdate(
82
88
  onSettled: Settled,
83
89
  { pollMs = HARNESS_UPDATE_POLL_MS, enabled = true }: HarnessUpdateOptions = {},
@@ -91,6 +97,13 @@ export function useHarnessUpdate(
91
97
  const reported = useRef(new Set<string>());
92
98
  // Runs whose post-install probe answer has already been reflected in the rows.
93
99
  const probed = useRef(new Set<string>());
100
+ // The runs this card dismissed, keyed by `harness:startedAt`. A poll already
101
+ // in flight when the delete landed would otherwise paint the record back for
102
+ // one frame; the key, not the harness, stays out so a NEW run is shown.
103
+ const dismissed = useRef(new Set<string>());
104
+ // The jobs as last rendered, so `dismiss` can name the run it is dropping
105
+ // without waiting for a render.
106
+ const shown = useRef<HarnessUpdateJob[]>([]);
94
107
  // The callback is read through a ref so an inline arrow from the caller does
95
108
  // not restart the mount effect (and re-adopt the records) on every render.
96
109
  const settled = useRef(onSettled);
@@ -99,13 +112,12 @@ export function useHarnessUpdate(
99
112
  /** Fold one harness's record into the list, keeping the newer one. */
100
113
  const merge = useCallback((incoming: HarnessUpdateJob | null) => {
101
114
  if (!incoming) return;
102
- setJobs((current) => {
103
- const index = current.findIndex((job) => job.harness === incoming.harness);
104
- if (index < 0) return [...current, incoming];
105
- const next = current.slice();
106
- next[index] = incoming;
107
- return next;
108
- });
115
+ const index = shown.current.findIndex((job) => job.harness === incoming.harness);
116
+ const next = index < 0
117
+ ? [...shown.current, incoming]
118
+ : shown.current.map((job) => (job.harness === incoming.harness ? incoming : job));
119
+ shown.current = next;
120
+ setJobs(next);
109
121
  }, []);
110
122
 
111
123
  /**
@@ -117,8 +129,12 @@ export function useHarnessUpdate(
117
129
  * are two separate moments, not one.
118
130
  */
119
131
  const observe = useCallback((incoming: HarnessUpdateJob[]) => {
120
- setJobs(incoming);
121
- for (const job of incoming) {
132
+ // A poll that was already in flight when a dismissal deleted the record
133
+ // still carries it; a dismissed run must not be painted back.
134
+ const visible = incoming.filter((job) => !dismissed.current.has(runKey(job)));
135
+ shown.current = visible;
136
+ setJobs(visible);
137
+ for (const job of visible) {
122
138
  if (jobUnfinished(job)) {
123
139
  watched.current.add(job.harness);
124
140
  reported.current.delete(job.harness);
@@ -246,5 +262,28 @@ export function useHarnessUpdate(
246
262
  return payload.error ?? 'Update failed';
247
263
  }, [follow, merge]);
248
264
 
249
- return { jobs, start };
265
+ const dismiss = useCallback(async (id: string): Promise<string | null> => {
266
+ let response: Response;
267
+ try {
268
+ response = await fetch('/api/harness/update/status', {
269
+ method: 'DELETE',
270
+ headers: { 'Content-Type': 'application/json' },
271
+ body: JSON.stringify({ harness: id }),
272
+ });
273
+ } catch (cause) {
274
+ return cause instanceof Error ? cause.message : 'Could not dismiss the update result';
275
+ }
276
+ const payload = (await response.json().catch(() => ({}))) as { error?: string };
277
+ if (!response.ok) return payload.error ?? `Could not dismiss the update result (HTTP ${response.status})`;
278
+ // Remember the RUN, not the harness: the record is gone on disk, and this
279
+ // keeps a poll that predates the delete from painting it back.
280
+ const target = shown.current.find((job) => job.harness === id);
281
+ if (target) dismissed.current.add(runKey(target));
282
+ const next = shown.current.filter((job) => job.harness !== id);
283
+ shown.current = next;
284
+ setJobs(next);
285
+ return null;
286
+ }, []);
287
+
288
+ return { jobs, start, dismiss };
250
289
  }
@@ -0,0 +1,72 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ /**
7
+ * The diff panel's read is keyed on the staged flag, not only on the path.
8
+ *
9
+ * One file can be open as two tabs — its staged diff and its unstaged diff —
10
+ * and the editor mounts ONE `DiffPanel` for whichever tab is active, so the
11
+ * second tab arrives as a prop change on the same hook instance. When the
12
+ * fetch was keyed on the path alone, that second tab kept the first tab's
13
+ * patch on screen: the reported "staged and unstaged diffs are identical".
14
+ */
15
+
16
+ import { afterAll, beforeAll, beforeEach, describe, expect, test } from 'bun:test';
17
+ import { Window } from 'happy-dom';
18
+ import { h, render } from 'preact';
19
+ import { act } from 'preact/test-utils';
20
+ import { useFileDiff } from '@/client/hooks/workspace/diff-file';
21
+ import { installDomGlobals, restoreDomGlobals } from '@/test-support/pristine-globals';
22
+
23
+ const originalFetch = globalThis.fetch;
24
+ let requested: string[] = [];
25
+
26
+ function Probe({ isStaged }: { isStaged: boolean }) {
27
+ useFileDiff({ filePath: 'src/a.ts', status: 'M', isStaged, repo: '.' });
28
+ return null;
29
+ }
30
+
31
+ beforeAll(() => {
32
+ installDomGlobals(new Window({ url: 'http://localhost' }));
33
+ });
34
+
35
+ afterAll(() => {
36
+ restoreDomGlobals();
37
+ globalThis.fetch = originalFetch;
38
+ });
39
+
40
+ beforeEach(() => {
41
+ requested = [];
42
+ globalThis.fetch = (async (input: RequestInfo | URL) => {
43
+ const url = String(input);
44
+ requested.push(url);
45
+ const staged = new URL(url, 'http://localhost').searchParams.get('staged') === '1';
46
+ return new Response(
47
+ JSON.stringify({ success: true, diff: '', oldContent: '', newContent: '', status: 'M', staged }),
48
+ { headers: { 'content-type': 'application/json' } },
49
+ );
50
+ }) as typeof fetch;
51
+ });
52
+
53
+ describe('useFileDiff', () => {
54
+ test('re-reads when the same path is opened as the other staged view', async () => {
55
+ const container = document.createElement('div');
56
+ await act(async () => {
57
+ render(h(Probe, { isStaged: false }), container);
58
+ });
59
+ expect(requested).toHaveLength(1);
60
+ expect(requested[0]).toContain('staged=0');
61
+
62
+ await act(async () => {
63
+ render(h(Probe, { isStaged: true }), container);
64
+ });
65
+ expect(requested).toHaveLength(2);
66
+ expect(requested[1]).toContain('staged=1');
67
+
68
+ await act(async () => {
69
+ render(null, container);
70
+ });
71
+ });
72
+ });
@@ -111,16 +111,22 @@ export function useFileDiff({ filePath, status, isStaged, repo, root }: UseFileD
111
111
  }
112
112
  }, [filePath, repo, root]);
113
113
 
114
+ /**
115
+ * The read is keyed on the pair the caller asked for, not only on the path.
116
+ *
117
+ * One file can be open twice — its staged diff and its unstaged diff — and
118
+ * the two tabs share one mounted panel with only the props changing. Keying
119
+ * the fetch on `fetchDiff` alone (path/repo/root) left the previous read on
120
+ * screen, so the second tab showed the first tab's patch. The refs are
121
+ * seeded first: `fetchDiff` reads them, and the state copies lag a render.
122
+ */
114
123
  useEffect(() => {
115
- setCurrentStatus(status);
116
- setCurrentStaged(isStaged);
117
124
  stagedRef.current = isStaged;
118
125
  statusRef.current = status;
119
- }, [status, isStaged, filePath]);
120
-
121
- useEffect(() => {
126
+ setCurrentStatus(status);
127
+ setCurrentStaged(isStaged);
122
128
  void fetchDiff();
123
- }, [fetchDiff]);
129
+ }, [fetchDiff, isStaged, status]);
124
130
 
125
131
  const adoptState = useCallback((next: { staged?: boolean; status?: string }) => {
126
132
  if (typeof next.staged === 'boolean') {
@@ -0,0 +1,220 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ /**
7
+ * The Files panel's archive dialogs: the draft a modal edits, the server call
8
+ * it submits, and the format list this machine can actually write.
9
+ *
10
+ * The format list is READ from `/api/fs/archive` rather than hardcoded, because
11
+ * `rar` and `7z` are binaries the machine may not have and a dialog that offers
12
+ * them would only produce a refusal after the user typed a name. When the read
13
+ * fails the full list is still offered — the server's own refusal is the honest
14
+ * answer, and hiding every format because one probe failed is worse.
15
+ *
16
+ * A refusal keeps the dialog OPEN with the server's words beside the field,
17
+ * exactly like Create and Rename: "already exists" is a message about what to
18
+ * type next, not a notification about something that happened.
19
+ */
20
+
21
+ import { useCallback, useEffect, useRef, useState } from 'preact/hooks';
22
+ import { useFetcher } from '@/client/lib/router/fetcher';
23
+ import type { ClipboardTarget } from '@/client/hooks/workspace/file-clipboard';
24
+ import {
25
+ COMPRESS_FORMATS,
26
+ defaultArchiveDestination,
27
+ defaultArchiveName,
28
+ defaultExtractDestination,
29
+ type ArchiveFormat,
30
+ } from '@/shared/lib/fs/archive';
31
+
32
+ /** What the capability route answers. */
33
+ interface ArchiveFormatAvailability {
34
+ id: ArchiveFormat;
35
+ label: string;
36
+ extension: string;
37
+ available: boolean;
38
+ tool: string;
39
+ }
40
+
41
+ interface ArchiveResponse {
42
+ success?: boolean;
43
+ path?: string;
44
+ error?: string;
45
+ }
46
+
47
+ export interface CompressDraft {
48
+ entries: ClipboardTarget[];
49
+ format: ArchiveFormat;
50
+ name: string;
51
+ destination: string;
52
+ }
53
+
54
+ export interface ExtractDraft {
55
+ archive: ClipboardTarget;
56
+ destination: string;
57
+ }
58
+
59
+ export interface FileArchiveDeps {
60
+ rootPath?: string;
61
+ repo: string;
62
+ /** Fired after an archive landed or a folder was unpacked. */
63
+ onComplete: () => void;
64
+ }
65
+
66
+ export interface FileArchive {
67
+ formats: ArchiveFormatAvailability[];
68
+ compress: CompressDraft | null;
69
+ extract: ExtractDraft | null;
70
+ /** The server's refusal for the open dialog. */
71
+ error: string | null;
72
+ busy: boolean;
73
+ openCompress: (entries: readonly ClipboardTarget[]) => void;
74
+ openExtract: (archive: ClipboardTarget) => void;
75
+ setFormat: (format: ArchiveFormat) => void;
76
+ setName: (name: string) => void;
77
+ setCompressDestination: (destination: string) => void;
78
+ setExtractDestination: (destination: string) => void;
79
+ cancel: () => void;
80
+ submitCompress: () => void;
81
+ submitExtract: () => void;
82
+ }
83
+
84
+ const FALLBACK_FORMATS: ArchiveFormatAvailability[] = COMPRESS_FORMATS.map((entry) => ({
85
+ ...entry,
86
+ available: true,
87
+ tool: entry.id,
88
+ }));
89
+
90
+ export function useFileArchive({ rootPath, repo, onComplete }: FileArchiveDeps): FileArchive {
91
+ const fetcher = useFetcher<ArchiveResponse>();
92
+ const [formats, setFormats] = useState<ArchiveFormatAvailability[]>(FALLBACK_FORMATS);
93
+ const [compress, setCompress] = useState<CompressDraft | null>(null);
94
+ const [extract, setExtract] = useState<ExtractDraft | null>(null);
95
+ const [error, setError] = useState<string | null>(null);
96
+ /** Whether the response in flight came from the compress or extract dialog. */
97
+ const pendingRef = useRef<'compress' | 'extract' | null>(null);
98
+ /** The response already acted on; the fetcher keeps `data` until the next call. */
99
+ const handledRef = useRef<ArchiveResponse | null>(null);
100
+
101
+ useEffect(() => {
102
+ let cancelled = false;
103
+ void (async () => {
104
+ try {
105
+ const res = await fetch('/api/fs/archive');
106
+ const body = (await res.json()) as { formats?: ArchiveFormatAvailability[] };
107
+ if (!cancelled && Array.isArray(body.formats) && body.formats.length > 0) setFormats(body.formats);
108
+ } catch {
109
+ // Keep the fallback list; the server's refusal names a missing tool.
110
+ }
111
+ })();
112
+ return () => {
113
+ cancelled = true;
114
+ };
115
+ }, []);
116
+
117
+ useEffect(() => {
118
+ if (fetcher.state !== 'idle' || !fetcher.data) return;
119
+ const data = fetcher.data;
120
+ if (handledRef.current === data) return;
121
+ handledRef.current = data;
122
+ const pending = pendingRef.current;
123
+ pendingRef.current = null;
124
+ if (data.success) {
125
+ setError(null);
126
+ if (pending === 'compress') setCompress(null);
127
+ if (pending === 'extract') setExtract(null);
128
+ onComplete();
129
+ } else if (pending) {
130
+ setError(data.error || 'The archive request failed');
131
+ }
132
+ // eslint-disable-next-line react-hooks/exhaustive-deps
133
+ }, [fetcher.state, fetcher.data]);
134
+
135
+ const post = useCallback((fields: Record<string, string>) => {
136
+ const formData = new FormData();
137
+ for (const [key, value] of Object.entries(fields)) formData.append(key, value);
138
+ if (repo && repo !== '.') formData.append('repo', repo);
139
+ if (rootPath) formData.append('root', rootPath);
140
+ void fetcher.submit(formData, { method: 'post', action: '/api/fs/action' });
141
+ }, [fetcher, repo, rootPath]);
142
+
143
+ const openCompress = useCallback((entries: readonly ClipboardTarget[]) => {
144
+ if (entries.length === 0) return;
145
+ // Zip first when the machine can write it, otherwise the first writer it
146
+ // has: a dialog that opens on a format the server cannot run is a refusal
147
+ // the user has to discover by submitting.
148
+ const format = formats.find((option) => option.id === 'zip' && option.available)?.id
149
+ ?? formats.find((option) => option.available)?.id
150
+ ?? 'zip';
151
+ setError(null);
152
+ setExtract(null);
153
+ setCompress({
154
+ entries: entries.map(({ path, name, kind }) => ({ path, name, kind })),
155
+ format,
156
+ name: defaultArchiveName(entries, format),
157
+ destination: defaultArchiveDestination(entries),
158
+ });
159
+ }, [formats]);
160
+
161
+ const openExtract = useCallback((archive: ClipboardTarget) => {
162
+ setError(null);
163
+ setCompress(null);
164
+ setExtract({ archive, destination: defaultExtractDestination(archive.path) });
165
+ }, []);
166
+
167
+ const setFormat = useCallback((format: ArchiveFormat) => {
168
+ setCompress((current) => {
169
+ if (!current) return current;
170
+ // Only rewrite the name when it is still the one the dialog proposed;
171
+ // a name the user typed is theirs, even if the extension disagrees.
172
+ const proposed = defaultArchiveName(current.entries, current.format);
173
+ const name = current.name === proposed ? defaultArchiveName(current.entries, format) : current.name;
174
+ return { ...current, format, name };
175
+ });
176
+ }, []);
177
+
178
+ return {
179
+ formats,
180
+ compress,
181
+ extract,
182
+ error,
183
+ busy: fetcher.state !== 'idle',
184
+ openCompress,
185
+ openExtract,
186
+ setFormat,
187
+ setName: (name) => setCompress((current) => current && { ...current, name }),
188
+ setCompressDestination: (destination) => setCompress((current) => current && { ...current, destination }),
189
+ setExtractDestination: (destination) => setExtract((current) => current && { ...current, destination }),
190
+ cancel: () => {
191
+ setCompress(null);
192
+ setExtract(null);
193
+ setError(null);
194
+ },
195
+ submitCompress: () => {
196
+ const draft = compress;
197
+ if (!draft) return;
198
+ pendingRef.current = 'compress';
199
+ setError(null);
200
+ post({
201
+ actionType: 'compress',
202
+ format: draft.format,
203
+ name: draft.name.trim(),
204
+ destination: draft.destination.trim() || '.',
205
+ paths: JSON.stringify(draft.entries.map((entry) => entry.path)),
206
+ });
207
+ },
208
+ submitExtract: () => {
209
+ const draft = extract;
210
+ if (!draft) return;
211
+ pendingRef.current = 'extract';
212
+ setError(null);
213
+ post({
214
+ actionType: 'extract',
215
+ path: draft.archive.path,
216
+ destination: draft.destination.trim() || '.',
217
+ });
218
+ },
219
+ };
220
+ }
@@ -165,7 +165,7 @@ describe('useFileClipboard', () => {
165
165
  await mountHook();
166
166
 
167
167
  await act(async () => {
168
- mounted?.cut({ path: 'src/a.ts', name: 'a.ts', kind: 'file' });
168
+ mounted?.cut([{ path: 'src/a.ts', name: 'a.ts', kind: 'file' }]);
169
169
  });
170
170
  expect(mounted?.clip?.mode).toBe('cut');
171
171
 
@@ -182,7 +182,7 @@ describe('useFileClipboard', () => {
182
182
  await mountHook();
183
183
 
184
184
  await act(async () => {
185
- mounted?.copy({ path: 'src/a.ts', name: 'a.ts', kind: 'file' });
185
+ mounted?.copy([{ path: 'src/a.ts', name: 'a.ts', kind: 'file' }]);
186
186
  });
187
187
  await act(async () => {
188
188
  mounted?.paste({ path: 'dest', name: 'dest', kind: 'folder' });
@@ -194,13 +194,13 @@ describe('useFileClipboard', () => {
194
194
  await settle();
195
195
 
196
196
  expect(requests.map((request) => request.mode)).toEqual(['copy', 'copy']);
197
- expect(mounted?.clip?.path).toBe('src/a.ts');
197
+ expect(mounted?.clip?.entries[0]?.path).toBe('src/a.ts');
198
198
  });
199
199
 
200
200
  test('a paste of an entry from another listing is refused with the reason', async () => {
201
201
  await mountHook();
202
202
  await act(async () => {
203
- mounted?.copy({ path: 'src/a.ts', name: 'a.ts', kind: 'file' });
203
+ mounted?.copy([{ path: 'src/a.ts', name: 'a.ts', kind: 'file' }]);
204
204
  });
205
205
  // The scope is part of the entry: the same relative path in another
206
206
  // workspace names a different file.
@@ -39,6 +39,10 @@ import {
39
39
  interface TransferResponse {
40
40
  success?: boolean;
41
41
  path?: string;
42
+ /** One entry per pasted source, when a selection was moved. */
43
+ paths?: string[];
44
+ /** Entries that could not land, after the rest of the selection did. */
45
+ failures?: { path: string; error: string }[];
42
46
  error?: string;
43
47
  }
44
48
 
@@ -54,8 +58,9 @@ export interface FileClipboard {
54
58
  busy: boolean;
55
59
  /** The reason a paste onto this target would be refused, or null when legal. */
56
60
  refusalFor: (target: ClipboardTarget) => string | null;
57
- copy: (target: ClipboardTarget) => void;
58
- cut: (target: ClipboardTarget) => void;
61
+ /** Put one row, or a whole selection, on the clipboard. */
62
+ copy: (targets: readonly ClipboardTarget[]) => void;
63
+ cut: (targets: readonly ClipboardTarget[]) => void;
59
64
  duplicate: (target: ClipboardTarget) => void;
60
65
  paste: (target: ClipboardTarget) => void;
61
66
  /**
@@ -109,6 +114,12 @@ export function useFileClipboard({ rootPath, repo, onComplete, onError }: FileCl
109
114
  clearsClipRef.current = false;
110
115
  if (data.success) {
111
116
  if (clearClip) setStoredClip(null);
117
+ // A selection can land partly: the entries that did must be reported as
118
+ // done while the ones that did not are named, or the tree re-read looks
119
+ // like it silently dropped a file.
120
+ if (data.failures?.length) {
121
+ onError(`${data.failures.length} item(s) could not be moved: ${data.failures[0]!.error}`);
122
+ }
112
123
  onComplete();
113
124
  } else {
114
125
  onError(data.error || 'The transfer did not complete');
@@ -136,16 +147,34 @@ export function useFileClipboard({ rootPath, repo, onComplete, onError }: FileCl
136
147
  [],
137
148
  );
138
149
 
150
+ /** Post a paste of `sources`, as repeated form fields the route reads back. */
151
+ const postPaste = useCallback((sources: readonly ClipboardTarget[], target: ClipboardTarget, mode: 'copy' | 'cut', clearsClip: boolean) => {
152
+ const formData = new FormData();
153
+ formData.append('actionType', 'paste');
154
+ formData.append('mode', mode);
155
+ formData.append('targetPath', target.path);
156
+ formData.append('targetKind', target.kind);
157
+ // The route stats each source for its kind; a `sourceKind` field would be
158
+ // a second, unchecked answer to the same question.
159
+ for (const source of sources) formData.append('sourcePath', source.path);
160
+ if (repo && repo !== '.') formData.append('repo', repo);
161
+ if (rootPath) formData.append('root', rootPath);
162
+ clearsClipRef.current = clearsClip;
163
+ void fetcher.submit(formData, { method: 'post', action: '/api/fs/action' });
164
+ }, [fetcher, repo, rootPath]);
165
+
139
166
  return {
140
167
  clip,
141
168
  busy: fetcher.state !== 'idle',
142
169
  refusalFor,
143
170
  moveRefusalFor,
144
- copy: (target) => {
145
- setStoredClip({ scope, path: target.path, name: target.name, kind: target.kind, mode: 'copy' });
171
+ copy: (targets) => {
172
+ if (targets.length === 0) return;
173
+ setStoredClip({ scope, entries: targets.map(({ path, name, kind }) => ({ path, name, kind })), mode: 'copy' });
146
174
  },
147
- cut: (target) => {
148
- setStoredClip({ scope, path: target.path, name: target.name, kind: target.kind, mode: 'cut' });
175
+ cut: (targets) => {
176
+ if (targets.length === 0) return;
177
+ setStoredClip({ scope, entries: targets.map(({ path, name, kind }) => ({ path, name, kind })), mode: 'cut' });
149
178
  },
150
179
  duplicate: (target) => {
151
180
  post({ actionType: 'duplicate', path: target.path }, false);
@@ -159,16 +188,7 @@ export function useFileClipboard({ rootPath, repo, onComplete, onError }: FileCl
159
188
  // The same `paste` request a Cut's paste posts: a drag IS a cut, so the
160
189
  // two must not diverge on what moving an entry does. It clears the
161
190
  // clipboard only when the entry it holds was the one moved.
162
- post(
163
- {
164
- actionType: 'paste',
165
- sourcePath: source.path,
166
- mode: 'cut',
167
- targetPath: target.path,
168
- targetKind: target.kind,
169
- },
170
- clip?.path === source.path && clip?.mode === 'cut',
171
- );
191
+ postPaste([source], target, 'cut', Boolean(clip?.mode === 'cut' && clip.entries.some((entry) => entry.path === source.path)));
172
192
  },
173
193
  paste: (target) => {
174
194
  if (!clip) return;
@@ -177,16 +197,7 @@ export function useFileClipboard({ rootPath, repo, onComplete, onError }: FileCl
177
197
  onError(refusal);
178
198
  return;
179
199
  }
180
- post(
181
- {
182
- actionType: 'paste',
183
- sourcePath: clip.path,
184
- mode: clip.mode,
185
- targetPath: target.path,
186
- targetKind: target.kind,
187
- },
188
- clip.mode === 'cut',
189
- );
200
+ postPaste(clip.entries.map(({ path, name, kind }) => ({ path, name, kind })), target, clip.mode, clip.mode === 'cut');
190
201
  },
191
202
  };
192
203
  }
@@ -0,0 +1,67 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ /**
7
+ * The Files panel's selection arithmetic: what a modifier means, which rows a
8
+ * shift-range covers, and the flat order the range is computed against.
9
+ *
10
+ * The range is the one rule that can be wrong without looking wrong: a range
11
+ * computed from the TREE instead of the DRAWN rows selects collapsed children
12
+ * the user never saw, and a range whose anchor disappeared must not select the
13
+ * whole tree.
14
+ */
15
+
16
+ import { describe, expect, test } from 'bun:test';
17
+ import { rangePaths, selectionGestureOf, visibleEntriesOf, type SelectionNode } from '@/client/hooks/workspace/file-selection';
18
+
19
+ const node = (path: string, type: 'file' | 'folder', children?: SelectionNode[]): SelectionNode => ({
20
+ path,
21
+ name: path.split('/').pop() ?? path,
22
+ type,
23
+ children,
24
+ });
25
+
26
+ describe('selectionGestureOf', () => {
27
+ test('shift ranges, meta or ctrl toggles, otherwise plain', () => {
28
+ expect(selectionGestureOf({ shiftKey: true, metaKey: true, ctrlKey: true })).toBe('range');
29
+ expect(selectionGestureOf({ metaKey: true })).toBe('toggle');
30
+ expect(selectionGestureOf({ ctrlKey: true })).toBe('toggle');
31
+ expect(selectionGestureOf({})).toBe('plain');
32
+ });
33
+ });
34
+
35
+ describe('rangePaths', () => {
36
+ const order = ['a.ts', 'b.ts', 'c.ts', 'd.ts'];
37
+
38
+ test('covers both ends regardless of direction', () => {
39
+ expect(rangePaths(order, 'b.ts', 'd.ts')).toEqual(['b.ts', 'c.ts', 'd.ts']);
40
+ expect(rangePaths(order, 'd.ts', 'b.ts')).toEqual(['b.ts', 'c.ts', 'd.ts']);
41
+ });
42
+
43
+ test('falls back to the target when the anchor is gone from the visible rows', () => {
44
+ // A collapsed folder's anchor is no longer drawn; selecting the rest of the
45
+ // tree because of that would be worse than selecting only the click.
46
+ expect(rangePaths(order, 'gone.ts', 'c.ts')).toEqual(['c.ts']);
47
+ expect(rangePaths(order, null, 'c.ts')).toEqual(['c.ts']);
48
+ });
49
+ });
50
+
51
+ describe('visibleEntriesOf', () => {
52
+ const tree: SelectionNode[] = [
53
+ node('src', 'folder', [node('src/a.ts', 'file'), node('src/deep', 'folder', [node('src/deep/b.ts', 'file')])]),
54
+ node('readme.md', 'file'),
55
+ ];
56
+
57
+ test('an open folder contributes its children immediately after it', () => {
58
+ const entries = visibleEntriesOf(tree, (entry) => entry.type === 'folder');
59
+ expect(entries.map((entry) => entry.path)).toEqual(['src', 'src/a.ts', 'src/deep', 'src/deep/b.ts', 'readme.md']);
60
+ expect(entries.find((entry) => entry.path === 'src/deep/b.ts')?.depth).toBe(2);
61
+ });
62
+
63
+ test('a closed folder contributes only itself', () => {
64
+ const entries = visibleEntriesOf(tree, () => false);
65
+ expect(entries.map((entry) => entry.path)).toEqual(['src', 'readme.md']);
66
+ });
67
+ });