@@ -224,7 +224,7 @@ async function resolveActiveWakeWithRetries(
224224 message : string ,
225225 wakeOptions : EmbeddedAgentQueueMessageOptions ,
226226 signal ?: AbortSignal ,
227- isSourceSessionEffectsAllowed ?: ( ) => boolean ,
227+ isAttemptAllowed ?: ( ) => boolean ,
228228) : Promise < EmbeddedAgentQueueMessageOutcome | typeof SOURCE_OWNER_CHANGED > {
229229 // Bound the whole active wake by the caller's delivery window. Each retry
230230 // passes only the remaining window into transcript-commit waiting so a
@@ -247,10 +247,13 @@ async function resolveActiveWakeWithRetries(
247247 deliveryTimeoutMs : remainingDeliveryTimeoutMs ,
248248 } ;
249249 } ;
250- const attemptWake = ( options : EmbeddedAgentQueueMessageOptions ) =>
251- isSourceSessionEffectsAllowed ?.( ) === false
252- ? SOURCE_OWNER_CHANGED
253- : resolveQueueEmbeddedAgentMessageOutcome ( sessionId , message , options ) ;
250+ const attemptWake = async ( options : EmbeddedAgentQueueMessageOptions ) => {
251+ if ( isAttemptAllowed ?.( ) === false ) {
252+ return SOURCE_OWNER_CHANGED ;
253+ }
254+ const result = await resolveQueueEmbeddedAgentMessageOutcome ( sessionId , message , options ) ;
255+ return isAttemptAllowed ?.( ) === false ? SOURCE_OWNER_CHANGED : result ;
256+ } ;
254257 let outcome = await attemptWake ( currentOptions ) ;
255258 const compactionRetryDelaysMs = resolveCompactionSteerRetryDelaysMs ( ) ;
256259 let compactionRetryIndex = 0 ;
@@ -261,7 +264,7 @@ async function resolveActiveWakeWithRetries(
261264 if ( outcome . queued || signal ?. aborted ) {
262265 break ;
263266 }
264- if ( isSourceSessionEffectsAllowed ?.( ) === false ) {
267+ if ( isAttemptAllowed ?.( ) === false ) {
265268 outcome = SOURCE_OWNER_CHANGED ;
266269 break ;
267270 }
@@ -958,7 +961,10 @@ async function sendSubagentAnnounceDirectly(params: {
958961 error : "requester session abandoned after timeout" ,
959962 } ;
960963 }
961- if ( params . expectsCompletionMessage && params . isCompletionOwnedByRequesterYield ?.( ) ) {
964+ const isCompletionDeliveryAllowed = ( ) =>
965+ params . isSourceSessionEffectsAllowed ?.( ) !== false &&
966+ ! ( params . expectsCompletionMessage && params . isCompletionOwnedByRequesterYield ?.( ) ) ;
967+ if ( ! isCompletionDeliveryAllowed ( ) ) {
962968 // sessions_yield owns the post-turn synthesis. Starting or steering a
963969 // requester turn here would replay the original fanout during handoff.
964970 return {
@@ -977,7 +983,7 @@ async function sendSubagentAnnounceDirectly(params: {
977983 deliveryTarget,
978984 internalEvents : params . internalEvents ,
979985 onDeliveryResult : params . onDeliveryResult ,
980- isSourceSessionEffectsAllowed : params . isSourceSessionEffectsAllowed ,
986+ isSourceSessionEffectsAllowed : isCompletionDeliveryAllowed ,
981987 } ) ;
982988 const completionSourceReplyDeliveryMode = requiresMessageToolDelivery
983989 ? "message_tool_only"
@@ -1014,7 +1020,7 @@ async function sendSubagentAnnounceDirectly(params: {
10141020 params . triggerMessage ,
10151021 wakeOptions ,
10161022 params . signal ,
1017- params . isSourceSessionEffectsAllowed ,
1023+ isCompletionDeliveryAllowed ,
10181024 ) ;
10191025 if ( wakeOutcome === SOURCE_OWNER_CHANGED ) {
10201026 return sourceOwnerChangedResult ( ) ;
@@ -1092,9 +1098,9 @@ async function sendSubagentAnnounceDirectly(params: {
10921098 ? "completion direct announce agent call"
10931099 : "direct announce agent call" ,
10941100 signal : params . signal ,
1095- isAttemptAllowed : params . isSourceSessionEffectsAllowed ,
1101+ isAttemptAllowed : isCompletionDeliveryAllowed ,
10961102 run : async ( ) => {
1097- if ( params . isSourceSessionEffectsAllowed ?. ( ) === false ) {
1103+ if ( ! isCompletionDeliveryAllowed ( ) ) {
10981104 throw new SourceOwnerChangedError ( ) ;
10991105 }
11001106 return await runAnnounceAgentCall ( {
@@ -1120,6 +1126,9 @@ async function sendSubagentAnnounceDirectly(params: {
11201126 } ) ;
11211127 } ,
11221128 } ) ;
1129+ if ( ! isCompletionDeliveryAllowed ( ) ) {
1130+ return sourceOwnerChangedResult ( ) ;
1131+ }
11231132 } catch ( err ) {
11241133 if ( err instanceof SourceOwnerChangedError ) {
11251134 return sourceOwnerChangedResult ( ) ;
0 commit comments