Commit 3aca8f0
committed
fix(folders): close the folder-engine audit findings
Data loss
- Migration 0274 catches up file folders created AFTER 0272. That backfill is guarded
by `WHERE NOT EXISTS (… resource_type = 'file')`, so it fires once; every file folder
created between it and this cutover exists only in `workspace_file_folders`. Without
the catch-up they vanish from Files on deploy and their contents become unreachable
Authorization
- Archived folders were mutable, which bypassed the folder lock: the folder routes and
`updateFolder` filtered id + resourceType but never `deletedAt`, while
`getFolderLockStatus` does — so an archived-but-locked folder reported unlocked. Lock a
subfolder, delete its parent, and any write-level member could rename and reparent it.
PUT and the engine now require an active row; DELETE deliberately still does not, because
it reuses an archived folder's own `deletedAt` to make a partial cascade retryable
- `v1/admin/workflows/import` wrote `folderId` with no validation at all. Migration 0272
dropped the FK, so it accepted a knowledge-base folder, another workspace's folder, or
garbage — and the workflow then rendered under no sidebar node while still executing,
billing, and escaping the folder delete cascade. It now runs the same
`assertFolderInWorkspace` + `assertFolderMutable` pair as `v1/workflows/import`
Restore no longer loses state
- A folder restore ran a bare `archivedAt = null` over its workflows, while the delete had
routed through `archiveWorkflow` — which also disables schedules, webhooks, chats, and MCP
tools. Restoring a folder reported success while every schedule stayed disabled forever
and every chat 404'd. The workflow tree now restores through `restoreWorkflow`, like the
knowledge-base and table trees already did. Deployment state is still deliberately not
revived, matching the single-workflow restore
- Those canonical restores re-root a resource whose folder is archived — correct on its own,
catastrophic inside a cascade, which restores children BEFORE the folder rows and would
therefore dump every child at the workspace root. `resolveRestoredFolderId` takes the
subtree being restored and exempts it. `restoreTable` gained the re-root check it lacked
Correctness
- `getFolderPath` in `lib/folders/tree.ts` walked `parentId` with no `visited` set, so a
cycle in the client cache — reachable through `useReorderFolders`' unchecked optimistic
write — hung the tab. Its twin already had one. Dead `buildFolderMap` deleted
- The sidebar's "New folder" inside a folder hardcoded the name, so the second one 409'd,
was swallowed with only a log line, and the user got no folder and no message. It now
names through the existing `generateSubfolderName` and surfaces failures
- `chunkedBatchDelete` deleted by id with no re-check, so a resource restored between the
SELECT and the DELETE was hard-deleted anyway. Callers now re-assert their soft-delete
predicate on the DELETE
- Recently Deleted and the `restore_resource` tool cover every folder tree, not just
workflow folders, so deleted knowledge-base and table folders are recoverable
Cleanup
- The folder duplicate route stops steering control flow through `throw new Error('literal')`
matched by string, reuses the engine's `nextFolderSortOrder` instead of recomputing it, and
names its workflow scope once
- `servedFolderResourceTypeSchema` documents that its default applies only to an omitted
value; a present-but-unrecognized one is rejected, never coerced to `workflow`
- The `workspace_file_folders` comment described the table as unread and unwritten and
invited a DROP. It was live until this cutover, and it is now the rollback copy — the
comment says so, and names what must be true before anyone drops it
- Pinned rows carry a small non-interactive pin glyph again; they sort to the top and had
nothing explaining why since the inline pin button was removed
Tests
- New `lib/folders/lifecycle.test.ts` covers create/update/delete/restore, the re-root, and
the 23505 mappings; `cleanup-soft-deletes.test.ts` now pins the folder target's
resourceType predicate, which is what keeps the sweep off a not-yet-cut-over type1 parent 31da1eb commit 3aca8f0
28 files changed
Lines changed: 1179 additions & 117 deletions
File tree
- apps/sim
- app
- api
- folders/[id]
- duplicate
- v1/admin/workflows/import
- workspace/[workspaceId]
- components
- folders
- resource
- knowledge
- tables
- w/components/sidebar/components/workflow-list/components/folder-item
- background
- hooks/queries
- lib
- api/contracts
- cleanup
- copilot/tools/handlers
- folders
- knowledge
- resources/orchestration
- table
- workflows
- orchestration
- workspaces
- packages/db
- migrations
- meta
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
| 7 | + | |
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
22 | 47 | | |
23 | 48 | | |
24 | 49 | | |
| |||
46 | 71 | | |
47 | 72 | | |
48 | 73 | | |
49 | | - | |
| 74 | + | |
50 | 75 | | |
51 | 76 | | |
52 | 77 | | |
53 | 78 | | |
54 | 79 | | |
55 | | - | |
| 80 | + | |
56 | 81 | | |
57 | 82 | | |
58 | 83 | | |
| |||
62 | 87 | | |
63 | 88 | | |
64 | 89 | | |
65 | | - | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
66 | 95 | | |
67 | 96 | | |
68 | 97 | | |
69 | 98 | | |
70 | | - | |
| 99 | + | |
71 | 100 | | |
72 | 101 | | |
73 | 102 | | |
| |||
76 | 105 | | |
77 | 106 | | |
78 | 107 | | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
97 | 114 | | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
| 115 | + | |
| 116 | + | |
111 | 117 | | |
112 | 118 | | |
113 | 119 | | |
114 | 120 | | |
115 | 121 | | |
116 | | - | |
| 122 | + | |
117 | 123 | | |
118 | 124 | | |
119 | 125 | | |
120 | 126 | | |
121 | | - | |
| 127 | + | |
122 | 128 | | |
123 | 129 | | |
124 | 130 | | |
| |||
183 | 189 | | |
184 | 190 | | |
185 | 191 | | |
186 | | - | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
187 | 195 | | |
188 | 196 | | |
189 | 197 | | |
190 | 198 | | |
191 | | - | |
192 | | - | |
193 | | - | |
194 | | - | |
195 | | - | |
196 | | - | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
202 | | - | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | | - | |
208 | | - | |
209 | | - | |
210 | | - | |
211 | | - | |
212 | | - | |
213 | | - | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
214 | 202 | | |
215 | | - | |
216 | | - | |
217 | | - | |
218 | | - | |
219 | | - | |
220 | | - | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
221 | 209 | | |
222 | 210 | | |
223 | 211 | | |
| |||
250 | 238 | | |
251 | 239 | | |
252 | 240 | | |
253 | | - | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
254 | 244 | | |
255 | 245 | | |
256 | 246 | | |
257 | | - | |
| 247 | + | |
258 | 248 | | |
259 | 249 | | |
260 | | - | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
261 | 254 | | |
262 | 255 | | |
263 | 256 | | |
| |||
284 | 277 | | |
285 | 278 | | |
286 | 279 | | |
287 | | - | |
| 280 | + | |
288 | 281 | | |
289 | 282 | | |
290 | 283 | | |
| |||
295 | 288 | | |
296 | 289 | | |
297 | 290 | | |
298 | | - | |
| 291 | + | |
299 | 292 | | |
300 | 293 | | |
301 | 294 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
| 5 | + | |
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| |||
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
48 | 54 | | |
49 | 55 | | |
50 | 56 | | |
51 | | - | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
52 | 64 | | |
53 | 65 | | |
54 | 66 | | |
| |||
143 | 155 | | |
144 | 156 | | |
145 | 157 | | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
146 | 164 | | |
147 | 165 | | |
148 | 166 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
20 | 26 | | |
21 | 27 | | |
22 | 28 | | |
| |||
63 | 69 | | |
64 | 70 | | |
65 | 71 | | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
66 | 89 | | |
67 | 90 | | |
68 | 91 | | |
| |||
148 | 171 | | |
149 | 172 | | |
150 | 173 | | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
151 | 180 | | |
152 | 181 | | |
153 | 182 | | |
| |||
Lines changed: 9 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
12 | 19 | | |
13 | 20 | | |
14 | 21 | | |
| |||
30 | 37 | | |
31 | 38 | | |
32 | 39 | | |
33 | | - | |
| 40 | + | |
34 | 41 | | |
35 | 42 | | |
36 | 43 | | |
| |||
39 | 46 | | |
40 | 47 | | |
41 | 48 | | |
| 49 | + | |
42 | 50 | | |
43 | 51 | | |
44 | 52 | | |
| |||
0 commit comments