@@ -9,6 +9,7 @@ import type { DbOrTx } from '@/lib/db/types'
99import { acquireFolderMutationLock } from '@/lib/folders/locks'
1010import { deduplicateFolderName } from '@/lib/folders/naming'
1111import {
12+ buildFolderPath ,
1213 buildFolderPathIndex ,
1314 folderNameFromPath ,
1415 parentFolderPath ,
@@ -558,10 +559,15 @@ export async function ensureWorkspaceFileFolderPath(params: {
558559} ) : Promise < string | null > {
559560 if ( params . pathSegments . length === 0 ) return null
560561
562+ const pathSegments = params . pathSegments . map ( ( segment ) =>
563+ normalizeWorkspaceFileItemName ( segment , 'Folder' )
564+ )
565+ buildFolderPath ( pathSegments )
566+
561567 // Fast path: the whole chain already exists (the common case for repeated
562568 // writes into known folders) — per-segment indexed lookups instead of
563569 // loading the workspace's entire folder table.
564- const existing = await findWorkspaceFileFolderIdByPath ( params . workspaceId , params . pathSegments )
570+ const existing = await findWorkspaceFileFolderIdByPath ( params . workspaceId , pathSegments )
565571 if ( existing ) return existing
566572
567573 // Load all active folders once and build a lookup keyed by "name|parentId"
@@ -585,8 +591,7 @@ export async function ensureWorkspaceFileFolderPath(params: {
585591
586592 let parentId : string | null = null
587593
588- for ( const rawSegment of params . pathSegments ) {
589- const name = normalizeWorkspaceFileItemName ( rawSegment , 'Folder' )
594+ for ( const name of pathSegments ) {
590595 const lookupKey = `${ name } |${ parentId ?? '' } `
591596
592597 const cached = folderByNameParent . get ( lookupKey )
0 commit comments