@@ -70,17 +70,49 @@ async function extractIndexText(
7070 }
7171}
7272
73- async function clearRevision ( fileId : string , sourceContentUpdatedAt : Date ) : Promise < void > {
73+ async function clearRevision (
74+ workspaceId : string ,
75+ fileId : string ,
76+ sourceContentUpdatedAt : Date
77+ ) : Promise < void > {
7478 await db
7579 . delete ( workspaceFileSearchSegment )
7680 . where (
7781 and (
82+ eq ( workspaceFileSearchSegment . workspaceId , workspaceId ) ,
7883 eq ( workspaceFileSearchSegment . fileId , fileId ) ,
7984 eq ( workspaceFileSearchSegment . sourceContentUpdatedAt , sourceContentUpdatedAt )
8085 )
8186 )
8287}
8388
89+ async function discardObsoleteRevision ( options : {
90+ workspaceId : string
91+ fileId : string
92+ sourceContentUpdatedAt : Date
93+ } ) : Promise < void > {
94+ await db . transaction ( async ( tx ) => {
95+ await tx
96+ . delete ( workspaceFileSearchSegment )
97+ . where (
98+ and (
99+ eq ( workspaceFileSearchSegment . workspaceId , options . workspaceId ) ,
100+ eq ( workspaceFileSearchSegment . fileId , options . fileId ) ,
101+ eq ( workspaceFileSearchSegment . sourceContentUpdatedAt , options . sourceContentUpdatedAt )
102+ )
103+ )
104+ await tx
105+ . delete ( workspaceFileSearchIndex )
106+ . where (
107+ and (
108+ eq ( workspaceFileSearchIndex . workspaceId , options . workspaceId ) ,
109+ eq ( workspaceFileSearchIndex . fileId , options . fileId ) ,
110+ eq ( workspaceFileSearchIndex . sourceContentUpdatedAt , options . sourceContentUpdatedAt )
111+ )
112+ )
113+ } )
114+ }
115+
84116async function markTerminal ( options : {
85117 workspaceId : string
86118 fileId : string
@@ -114,6 +146,7 @@ async function markTerminal(options: {
114146 . delete ( workspaceFileSearchSegment )
115147 . where (
116148 and (
149+ eq ( workspaceFileSearchSegment . workspaceId , options . workspaceId ) ,
117150 eq ( workspaceFileSearchSegment . fileId , options . fileId ) ,
118151 eq ( workspaceFileSearchSegment . sourceContentUpdatedAt , options . sourceContentUpdatedAt )
119152 )
@@ -122,6 +155,7 @@ async function markTerminal(options: {
122155 . delete ( workspaceFileSearchIndex )
123156 . where (
124157 and (
158+ eq ( workspaceFileSearchIndex . workspaceId , options . workspaceId ) ,
125159 eq ( workspaceFileSearchIndex . fileId , options . fileId ) ,
126160 eq ( workspaceFileSearchIndex . sourceContentUpdatedAt , options . sourceContentUpdatedAt )
127161 )
@@ -234,12 +268,15 @@ export async function indexWorkspaceFileForSearch(
234268 throwOnError : true ,
235269 } )
236270 if ( ! file || ! sameRevision ( file . contentUpdatedAt , sourceContentUpdatedAt ) ) {
237- await clearRevision ( payload . fileId , sourceContentUpdatedAt )
271+ await discardObsoleteRevision ( { ... payload , sourceContentUpdatedAt } )
238272 return
239273 }
240274
241275 const [ state ] = await db
242- . select ( { status : workspaceFileSearchIndex . status } )
276+ . select ( {
277+ status : workspaceFileSearchIndex . status ,
278+ workspaceId : workspaceFileSearchIndex . workspaceId ,
279+ } )
243280 . from ( workspaceFileSearchIndex )
244281 . where (
245282 and (
@@ -248,6 +285,10 @@ export async function indexWorkspaceFileForSearch(
248285 )
249286 )
250287 . limit ( 1 )
288+ if ( state && state . workspaceId !== payload . workspaceId ) {
289+ await discardObsoleteRevision ( { ...payload , sourceContentUpdatedAt } )
290+ return
291+ }
251292 if ( state ?. status === 'ready' || state ?. status === 'skipped' ) return
252293
253294 await db
@@ -270,7 +311,7 @@ export async function indexWorkspaceFileForSearch(
270311 updatedAt : new Date ( ) ,
271312 } ,
272313 } )
273- await clearRevision ( payload . fileId , sourceContentUpdatedAt )
314+ await clearRevision ( payload . workspaceId , payload . fileId , sourceContentUpdatedAt )
274315
275316 if ( file . size > FILE_SEARCH_MAX_SOURCE_BYTES ) {
276317 await markTerminal ( {
@@ -315,7 +356,7 @@ export async function indexWorkspaceFileForSearch(
315356 } catch ( error ) {
316357 if ( signal . aborted ) throw error
317358 if ( isPayloadSizeLimitError ( error ) ) {
318- await clearRevision ( payload . fileId , sourceContentUpdatedAt )
359+ await clearRevision ( payload . workspaceId , payload . fileId , sourceContentUpdatedAt )
319360 await markTerminal ( {
320361 ...payload ,
321362 sourceContentUpdatedAt,
@@ -324,7 +365,7 @@ export async function indexWorkspaceFileForSearch(
324365 } )
325366 return
326367 }
327- await clearRevision ( payload . fileId , sourceContentUpdatedAt )
368+ await clearRevision ( payload . workspaceId , payload . fileId , sourceContentUpdatedAt )
328369 logger . error ( 'Workspace file search indexing failed' , {
329370 workspaceId : payload . workspaceId ,
330371 fileId : payload . fileId ,
@@ -340,7 +381,7 @@ export async function markWorkspaceFileSearchIndexFailed(
340381) : Promise < void > {
341382 const sourceContentUpdatedAt = new Date ( payload . sourceContentUpdatedAt )
342383 if ( Number . isNaN ( sourceContentUpdatedAt . getTime ( ) ) ) return
343- await clearRevision ( payload . fileId , sourceContentUpdatedAt )
384+ await clearRevision ( payload . workspaceId , payload . fileId , sourceContentUpdatedAt )
344385 await markTerminal ( {
345386 ...payload ,
346387 sourceContentUpdatedAt,
0 commit comments