Skip to content

fix(files): order file folders in SQL like every other folder list - #6599

Merged
waleedlatif1 merged 1 commit into
stagingfrom
investigate/folder-sort
Aug 12, 2026
Merged

fix(files): order file folders in SQL like every other folder list#6599
waleedlatif1 merged 1 commit into
stagingfrom
investigate/folder-sort

Conversation

@waleedlatif1

Copy link
Copy Markdown
Collaborator

Summary

  • executeListWorkspaceFileFolders defaulted to sortBy ?? 'name' — defaults written for the new v2 contract — and sorted in JS. The internal contract (workspace-file-folders.ts) exposes only scope, so the repository's sortOrder ASC, createdAt ASC ordering became unreachable.
  • That ordering is meaningful: a new folder takes sortOrder = minSortOrder - 1, so it is deliberately newest-first, and it is user-modifiable.
  • Pushed the sort into the query like the workflow, knowledge, and table folder lists already do, reusing the shared FOLDER_SORTS map. Omitting sortBy keeps position; v2 always sends one from its contract defaults.

Impact this fixes

Surfaces that render the payload in arrival order and had flipped newest-first → alphabetical:

  • @-mention Folders group — no sort, MAX_PER_GROUP = 8, so a workspace with >8 folders showed the 8 alphabetically-first instead of the 8 most recent, making a just-created folder unreachable without typing.
  • Add-resource dropdown search results — filters, never sorts.
  • Copilot list_file_folders.

Also removes a real SSR mismatch: files/prefetch.ts hydrates workspaceFileFolderKeys.list(workspaceId, 'active') by calling the data layer directly (repository order) while the client hook fetches the same key through the route (name-sorted). Its TSDoc asserts a "correctly-ordered first frame" and that a "hydrated entry matches a client fetch" — true again now.

Unaffected either way (they re-sort client-side): the Files browser (files.tsx sorts by sortOrder then localeCompare), Recently Deleted, and the add-resource submenu tree.

Secondary correctness win

A sortBy=name request previously compared with raw </> on UTF-16 code units, so "Zebra" sorted before "apple" — a third ordering matching neither the old one nor the localeCompare every client surface uses, with no tiebreak for equal names. It now uses the database collation and the shared createdAt tiebreak, consistent with the other folder lists.

Type of Change

  • Bug fix

Testing

  • New tests assert the sort is delegated (not applied in memory) and that omitting sortBy leaves it unset. Verified all three fail against the pre-fix code.
  • bunx vitest run lib/ app/api/ — 15,141 passing.
  • Repo-wide lint:check and type-check (23/23 each), plus check:api-validation and monorepo-boundary gates.
  • Audited every caller of the use case (internal route, v2 route, Copilot tool, Copilot VFS ×2, function-execute mounts); the VFS and function-execute paths consume results by path lookup, so ordering is immaterial there.
  • Confirmed no sibling sweep is needed: listActiveFolderRows' ?? 'name' is a dead default — its only callers are v2 use cases that always receive a defaulted sortBy.
  • Not exercised against a live workspace.

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

The list use case defaulted to a name sort whose defaults were written for the
new v2 contract, so the internal route — whose contract exposes no sort params —
could no longer reach the repository's sortOrder ASC, createdAt ASC ordering.
Surfaces that render the payload positionally (the @-mention Folders group, the
add-resource search results, Copilot's list_file_folders) silently flipped from
newest-first to alphabetical, and the Files browser's SSR prefetch hydrated a
different order than its own refetch.

Push the sort down to the query like the workflow, knowledge, and table folder
lists already do, reusing FOLDER_SORTS. Omitting sortBy keeps the position
ordering; v2 always sends one from its contract defaults. A name sort now also
uses the database collation and the shared createdAt tiebreak instead of a JS
comparator over UTF-16 code units.
@vercel

vercel Bot commented Aug 12, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Skipped Skipped Aug 12, 2026 4:39am

Request Review

@cursor

cursor Bot commented Aug 12, 2026

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes default list ordering for file folders across multiple consumers; correctness-focused but observable behavior shifts for callers that previously received name-sorted results.

Overview
Fixes file-folder list ordering by pushing sort into SQL (via shared FOLDER_SORTS) instead of re-sorting in JS with a name default.

Omitting sortBy now keeps the repository's position order (sortOrder, then createdAt), so internal callers that render arrival order — mentions, search dropdowns, Copilot — see newest/manual order again. Explicit v2 sorts still pass through, and name sorts use DB collation plus the shared createdAt tiebreak.

Reviewed by Cursor Bugbot for commit 771499a. Configure here.

@greptile-apps

greptile-apps Bot commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR moves workspace file-folder sorting from the application layer into the SQL query while preserving positional ordering when no explicit sort is requested.

  • Exports and reuses the shared FOLDER_SORTS map with listOrderBy.
  • Delegates explicit v2 sorting to the repository and removes the in-memory sort.
  • Adds tests covering delegated sorting, omitted sorting, and preservation of repository order.
  • Updates contract documentation to describe the SQL-backed ordering behavior.

Confidence Score: 5/5

The PR appears safe to merge with no actionable correctness, security, or changed-code quality issues identified.

The delegated sort values match the validated API contracts and current callers, the default preserves the prior repository position order, and the shared SQL helper has compatible stable ordering semantics.

Important Files Changed

Filename Overview
apps/sim/lib/uploads/contexts/workspace/workspace-file-folder-manager.ts Adds optional SQL sort parameters with position-ascending defaults and uses the established shared folder-ordering helper.
apps/sim/lib/workspace-files/application/workspace-file-folders.ts Forwards validated sorting options to the repository and preserves repository order instead of sorting results in memory.
apps/sim/lib/folders/queries.ts Exports the existing exhaustive folder-sort mapping for reuse without changing its definitions.
apps/sim/lib/workspace-files/application/workspace-file-folders.test.ts Verifies omitted and explicit sort delegation and confirms application code no longer reorders repository results.
apps/sim/lib/api/contracts/v2/shared.ts Updates list-contract documentation to reflect that file-folder sorting now occurs in SQL.

Reviews (1): Last reviewed commit: "fix(files): order file folders in SQL li..." | Re-trigger Greptile

@waleedlatif1
waleedlatif1 merged commit 22b3569 into staging Aug 12, 2026
30 checks passed
@waleedlatif1
waleedlatif1 deleted the investigate/folder-sort branch August 12, 2026 04:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant