@@ -37,7 +37,11 @@ import { MOTHERSHIP_CHAT_DEFAULT_MODEL } from '@/lib/mothership/constants'
3737import { runHeadlessCopilotLifecycle } from '@/lib/mothership/request/lifecycle/headless'
3838import {
3939 acquirePendingChatStream ,
40+ cleanupAbortMarker ,
41+ registerActiveStream ,
4042 releasePendingChatStream ,
43+ startAbortPoller ,
44+ unregisterActiveStream ,
4145} from '@/lib/mothership/request/session/abort'
4246import { requestExplicitStreamAbort } from '@/lib/mothership/request/session/explicit-abort'
4347import {
@@ -159,24 +163,26 @@ export const organizationSearchChat: OperationUseCase<
159163 'The question contains protected content. Rephrase it.'
160164 )
161165 }
166+ const resumed = input . chatId !== undefined
167+ const existing =
168+ input . chatId === undefined
169+ ? undefined
170+ : await findConversation ( input . chatId , userId , organizationId )
171+ if ( resumed && ! existing ) throw new OrchestrationError ( 'not_found' , CONVERSATION_NOT_FOUND )
162172 const billingAttribution = await resolveOrganizationBillingAttribution ( {
163173 actorUserId : userId ,
164174 organizationId,
165175 } )
166176 input . signal ?. throwIfAborted ( )
167- const resumed = input . chatId !== undefined
168- const chat =
169- input . chatId === undefined
170- ? await createConversation ( userId , organizationId , query )
171- : await findConversation ( input . chatId , userId , organizationId )
172- if ( ! chat ) throw new OrchestrationError ( 'not_found' , CONVERSATION_NOT_FOUND )
177+ const chat = existing ?? ( await createConversation ( userId , organizationId , query ) )
173178
174179 const chatId = chat . id
175180 const messageId = generateId ( )
176181 const controller = new AbortController ( )
177182 let running = false
178183 let persisted = false
179184 let explicitAbort : Promise < void > | undefined
185+ let lockPoller : ReturnType < typeof setInterval > | undefined
180186 const abort = ( ) => {
181187 controller . abort ( new DOMException ( 'Search chat cancelled' , 'AbortError' ) )
182188 if ( running && ! explicitAbort ) {
@@ -199,6 +205,9 @@ export const organizationSearchChat: OperationUseCase<
199205 'conflict' ,
200206 'This conversation is still answering an earlier question. Wait for that answer, then ask again.'
201207 )
208+ /** Renews the chat lock for the whole turn and lets Stop in the app cancel it. */
209+ registerActiveStream ( messageId , controller )
210+ lockPoller = startAbortPoller ( messageId , controller , { chatId } )
202211 controller . signal . throwIfAborted ( )
203212 running = true
204213 const payload = await buildCopilotRequestPayload ( {
@@ -284,6 +293,11 @@ export const organizationSearchChat: OperationUseCase<
284293 clearTimeout ( timeout )
285294 input . signal ?. removeEventListener ( 'abort' , abort )
286295 await explicitAbort
296+ if ( lockPoller ) {
297+ clearInterval ( lockPoller )
298+ unregisterActiveStream ( messageId )
299+ await cleanupAbortMarker ( messageId )
300+ }
287301 await releasePendingChatStream ( chatId , messageId )
288302 /**
289303 * Retain run diagnostics, but keep an unsuccessful empty conversation this turn created out
0 commit comments