@@ -30,6 +30,7 @@ import { isTerminalFailureChatSendAck } from "./chat-send-ack.ts";
3030import { cancelChatDelivery , canRestoreComposer , restoreComposer } from "./chat-send-composer.ts" ;
3131import type { ChatHost } from "./chat-send-contract.ts" ;
3232import {
33+ captureChatConnectionOwner ,
3334 deliveryStateWriter ,
3435 finishScopedChatSending ,
3536 reconnectSafeQueuedSendState ,
@@ -279,12 +280,7 @@ async function sendQueuedChatMessage(
279280 return "pending" ;
280281 }
281282
282- const requestClient = host . client ;
283- const requestConnectionEpoch = host . connectionEpoch ;
284- const requestConnectionIsCurrent = ( ) =>
285- host . connected &&
286- host . client === requestClient &&
287- host . connectionEpoch === requestConnectionEpoch ;
283+ const requestConnectionIsCurrent = captureChatConnectionOwner ( host ) ;
288284 const runId = prepared . sendRunId ?? generateUUID ( ) ;
289285 const startedAt = Date . now ( ) ;
290286 const requestStartedAtMs = controlUiNowMs ( ) ;
@@ -593,8 +589,7 @@ export async function deliverChatQueueItem(
593589 const sessionKey = item . sessionKey ?? host . sessionKey ;
594590 const storageMode = options . storageMode ?? "durable" ;
595591 const routingSessionKey = options . routingSessionKey ?? sessionKey ;
596- const deliveryClient = host . client ;
597- const deliveryConnectionEpoch = host . connectionEpoch ;
592+ const deliveryConnectionIsCurrent = captureChatConnectionOwner ( host , false ) ;
598593 const deliveryAgentId =
599594 item . agentId ?? scopedAgentIdForSession ( host , routingSessionKey ) ?? undefined ;
600595 const sendOptions = { ...options , routingSessionKey, storageMode } ;
@@ -697,8 +692,7 @@ export async function deliverChatQueueItem(
697692 }
698693 }
699694 if (
700- host . client === deliveryClient &&
701- host . connectionEpoch === deliveryConnectionEpoch &&
695+ deliveryConnectionIsCurrent ( ) &&
702696 host . sessionKey === routingSessionKey &&
703697 visibleSessionMatches ( host , routingSessionKey , deliveryAgentId )
704698 ) {
0 commit comments