@@ -45,6 +45,7 @@ import {
4545} from "./chat-server-timing.js" ;
4646import type { createGatewayChatUserTurnController } from "./chat-user-turn-recorder.js" ;
4747import { emitSessionsChanged } from "./session-change-event.js" ;
48+ import { prepareSessionProjectWorkspace } from "./session-create-project.js" ;
4849import type { GatewayRequestHandlerOptions } from "./types.js" ;
4950
5051type PreparedChatSendAttachments = Extract <
@@ -220,32 +221,49 @@ export function startChatDispatch(params: StartChatDispatchParams): void {
220221 measureDiagnosticsTimelineSpan (
221222 "gateway.chat_send.dispatch_inbound" ,
222223 async ( ) => {
224+ // Preparation stays after the ACK but inside admitted dispatch, so the
225+ // same visible run owns workspace progress, cancellation, and errors.
226+ let assertWorkspaceRunOwnership : ( ( ) => void ) | undefined ;
227+ if ( entry && Object . hasOwn ( entry , "pendingProjectGitUrl" ) ) {
228+ assertWorkspaceRunOwnership = await prepareSessionProjectWorkspace ( {
229+ admission,
230+ client,
231+ context,
232+ session,
233+ } ) ;
234+ assertWorkspaceRunOwnership ( ) ;
235+ }
223236 if ( replyContextFieldsPromise && ! preAckReplyContextPromise ) {
224- applyChatSendReplyContextFields ( ctx , await replyContextFieldsPromise ) ;
237+ const replyContextFields = await replyContextFieldsPromise ;
238+ assertWorkspaceRunOwnership ?.( ) ;
239+ applyChatSendReplyContextFields ( ctx , replyContextFields ) ;
225240 messageInjectionAttempt = beginCapturedMessageInjection ( ) ;
226241 }
227242 if ( messageInjectionAttempt ) {
228- if (
229- await finalizeAcceptedChatSendMessageInjection ( {
230- attempt : messageInjectionAttempt ,
231- context ,
232- ctx ,
233- persistUserTurnTranscriptBestEffort : persistGatewayUserTurnTranscriptBestEffort ,
234- session ,
235- startedAt : admissionStartedAt ,
236- target : messageInjectionTarget ! ,
237- } )
238- ) {
243+ const injected = await finalizeAcceptedChatSendMessageInjection ( {
244+ attempt : messageInjectionAttempt ,
245+ context ,
246+ ctx ,
247+ persistUserTurnTranscriptBestEffort : persistGatewayUserTurnTranscriptBestEffort ,
248+ session ,
249+ startedAt : admissionStartedAt ,
250+ target : messageInjectionTarget ! ,
251+ } ) ;
252+ assertWorkspaceRunOwnership ?. ( ) ;
253+ if ( injected ) {
239254 acceptedMessageInjection = true ;
240255 return {
241256 queuedFinal : false ,
242257 counts : { tool : 0 , block : 0 , final : 0 } ,
243258 } ;
244259 }
245260 }
246- applyChatSendManagedMedia ( ctx , await pluginBoundMediaPromise ) ;
247- const dispatchInbound = ( ) =>
248- dispatchInboundMessageWithProjectedDispatcher ( {
261+ const pluginBoundMedia = await pluginBoundMediaPromise ;
262+ assertWorkspaceRunOwnership ?.( ) ;
263+ applyChatSendManagedMedia ( ctx , pluginBoundMedia ) ;
264+ const dispatchInbound = ( ) => {
265+ assertWorkspaceRunOwnership ?.( ) ;
266+ return dispatchInboundMessageWithProjectedDispatcher ( {
249267 ctx,
250268 cfg,
251269 toolsAllow,
@@ -369,6 +387,7 @@ export function startChatDispatch(params: StartChatDispatchParams): void {
369387 } ,
370388 } ,
371389 } ) ;
390+ } ;
372391 const dispatchResult = await ( cronCreatorAuthority && externalAuthorityAdmission
373392 ? externalAuthorityAdmission . run (
374393 cronCreatorAuthority ,
0 commit comments