@@ -28,7 +28,11 @@ import {
2828import { deriveSessionChatTypeFromKey } from "../sessions/session-chat-type-shared.js" ;
2929import { isCronRunSessionKey , isCronSessionKey } from "../sessions/session-key-utils.js" ;
3030import { isNonTerminalAgentRunStatus } from "../shared/agent-run-status.js" ;
31- import { mergeDeliveryContext , normalizeDeliveryContext } from "../utils/delivery-context.js" ;
31+ import {
32+ deliveryContextFromSession ,
33+ mergeDeliveryContext ,
34+ normalizeDeliveryContext ,
35+ } from "../utils/delivery-context.js" ;
3236import {
3337 INTERNAL_MESSAGE_CHANNEL ,
3438 isDeliverableMessageChannel ,
@@ -596,6 +600,7 @@ export function loadSessionEntryByKey(sessionKey: string) {
596600}
597601
598602async function maybeSteerSubagentAnnounce ( params : {
603+ deliveryContext ?: DeliveryContext ;
599604 deliveryTimeoutMs ?: number ;
600605 requesterSessionKey : string ;
601606 steerMessage : string ;
@@ -624,7 +629,12 @@ async function maybeSteerSubagentAnnounce(params: {
624629
625630 // Subagent announcements are internal handoffs into an active requester turn.
626631 // Queue modes such as followup/collect apply to user prompts, not this path.
632+ const queueDeliveryContext = resolveActiveWakeDeliveryContext (
633+ params . deliveryContext ,
634+ deliveryContextFromSession ( entry ) ,
635+ ) ;
627636 const queueOptions : EmbeddedAgentQueueMessageOptions = {
637+ ...( queueDeliveryContext ? { deliveryContext : queueDeliveryContext } : { } ) ,
628638 deliveryTimeoutMs : params . deliveryTimeoutMs ,
629639 steeringMode : "all" ,
630640 ...( queueSettings . debounceMs !== undefined ? { debounceMs : queueSettings . debounceMs } : { } ) ,
@@ -1176,6 +1186,41 @@ function collectMessagingToolDeliveredMediaUrlsForTarget(
11761186 return Array . from ( urls ) ;
11771187}
11781188
1189+ function isExternalQueueDeliveryContext ( context ?: DeliveryContext ) : boolean {
1190+ const normalized = normalizeDeliveryContext ( context ) ;
1191+ const channel = normalizeMessageChannel ( normalized ?. channel ) ;
1192+ return Boolean (
1193+ normalized ?. to &&
1194+ channel &&
1195+ isDeliverableMessageChannel ( channel ) &&
1196+ ! isInternalMessageChannel ( channel ) ,
1197+ ) ;
1198+ }
1199+
1200+ function resolveActiveWakeDeliveryContext (
1201+ ...contexts : Array < DeliveryContext | undefined >
1202+ ) : DeliveryContext | undefined {
1203+ const normalizedContexts = contexts
1204+ . map ( ( context ) => normalizeDeliveryContext ( context ) )
1205+ . filter ( ( context ) : context is DeliveryContext => Boolean ( context ) ) ;
1206+ const externalContext = normalizedContexts . find ( ( context ) =>
1207+ isExternalQueueDeliveryContext ( context ) ,
1208+ ) ;
1209+ if ( externalContext ) {
1210+ const sameChannelFallback = normalizedContexts . find (
1211+ ( context ) =>
1212+ context !== externalContext &&
1213+ normalizeMessageChannel ( context . channel ) ===
1214+ normalizeMessageChannel ( externalContext . channel ) ,
1215+ ) ;
1216+ return mergeDeliveryContext ( externalContext , sameChannelFallback ) ;
1217+ }
1218+ return normalizedContexts . reduce < DeliveryContext | undefined > (
1219+ ( merged , context ) => mergeDeliveryContext ( merged , context ) ,
1220+ undefined ,
1221+ ) ;
1222+ }
1223+
11791224function stripNonDeliverableChannelForCompletionOrigin (
11801225 context ?: DeliveryContext ,
11811226) : DeliveryContext | undefined {
@@ -1680,6 +1725,12 @@ export async function deliverSubagentAnnouncement(params: {
16801725 signal : params . signal ,
16811726 steer : async ( ) =>
16821727 await maybeSteerSubagentAnnounce ( {
1728+ deliveryContext : resolveActiveWakeDeliveryContext (
1729+ params . completionDirectOrigin ,
1730+ params . directOrigin ,
1731+ params . requesterOrigin ,
1732+ params . requesterSessionOrigin ,
1733+ ) ,
16831734 deliveryTimeoutMs : resolveSubagentAnnounceTimeoutMs (
16841735 subagentAnnounceDeliveryDeps . getRuntimeConfig ( ) ,
16851736 ) ,
0 commit comments