@@ -355,6 +355,7 @@ async function deliverSlackThreadAnnouncement(params: {
355355 queueEmbeddedAgentMessageWithOutcome ?: QueueEmbeddedAgentMessageWithOutcome ;
356356 sendMessage ?: typeof runtimeSendMessage ;
357357 internalEvents ?: AgentInternalEvent [ ] ;
358+ sourceSessionKey ?: string ;
358359 sourceTool ?: string ;
359360 requesterAbandoned ?: boolean ;
360361 isSourceSessionEffectsAllowed ?: ( ) => boolean ;
@@ -416,6 +417,7 @@ async function deliverSlackThreadAnnouncement(params: {
416417 directIdempotencyKey : params . directIdempotencyKey ,
417418 internalEvents : params . internalEvents ,
418419 sourceRunId : "run-generated-media" ,
420+ sourceSessionKey : params . sourceSessionKey ,
419421 sourceTool : params . sourceTool ,
420422 isSourceSessionEffectsAllowed : params . isSourceSessionEffectsAllowed ,
421423 isCompletionOwnedByRequesterYield : params . isCompletionOwnedByRequesterYield ,
@@ -1951,6 +1953,134 @@ describe("deliverSubagentAnnouncement completion delivery", () => {
19511953 expect ( sendMessage ) . not . toHaveBeenCalled ( ) ;
19521954 } ) ;
19531955
1956+ it . each ( [ "error" , "timeout" , "unknown" ] as const ) (
1957+ "sends one generic direct notice when requester synthesis repeats a %s child completion" ,
1958+ async ( status ) => {
1959+ const childSessionKey = `agent:worker:subagent:${ status } -child` ;
1960+ const providerFailure = "provider rejected private-model-alias with status 400" ;
1961+ const childResult = "private child output must not reach the requester" ;
1962+ const callGateway = vi . fn ( async ( ) => {
1963+ throw new Error ( providerFailure ) ;
1964+ } ) as unknown as typeof runtimeCallGateway ;
1965+ const sendMessage = createSendMessageMock ( ) ;
1966+
1967+ const result = await deliverDiscordDirectMessageCompletion ( {
1968+ callGateway,
1969+ sendMessage,
1970+ sourceSessionKey : childSessionKey ,
1971+ sourceTool : "subagent_announce" ,
1972+ internalEvents : taskCompletionEvents ( {
1973+ childSessionKey,
1974+ childSessionId : `${ status } -child-session-id` ,
1975+ status,
1976+ statusLabel : `${ status } : ${ providerFailure } ` ,
1977+ result : childResult ,
1978+ } ) ,
1979+ } ) ;
1980+
1981+ expectRecordFields ( result , { delivered : true , path : "direct" } ) ;
1982+ expect ( callGateway ) . toHaveBeenCalledOnce ( ) ;
1983+ expect ( sendMessage ) . toHaveBeenCalledOnce ( ) ;
1984+ const content = mockCallArg ( sendMessage , 0 , 0 ) . content ;
1985+ expect ( content ) . toBe (
1986+ "A delegated task failed before it could report a result. Please retry the task." ,
1987+ ) ;
1988+ expect ( content ) . not . toContain ( providerFailure ) ;
1989+ expect ( content ) . not . toContain ( childResult ) ;
1990+ } ,
1991+ ) ;
1992+
1993+ it . each ( [ "cancelled" , "source owner changed" ] as const ) (
1994+ "stops a failed-child notice when %s at platform dispatch" ,
1995+ async ( blockedBy ) => {
1996+ const childSessionKey = `agent:worker:subagent:${ blockedBy . replaceAll ( " " , "-" ) } ` ;
1997+ const controller = new AbortController ( ) ;
1998+ let sourceEffectsAllowed = true ;
1999+ const platformSend = vi . fn ( ) ;
2000+ const callGateway = vi . fn ( async ( ) => {
2001+ throw new Error ( "provider rejected requester synthesis" ) ;
2002+ } ) as unknown as typeof runtimeCallGateway ;
2003+ const sendMessage = vi . fn ( async ( params : Parameters < typeof runtimeSendMessage > [ 0 ] ) => {
2004+ expect ( params . skipQueue ) . toBe ( true ) ;
2005+ expect ( params . abortSignal ) . toBe ( controller . signal ) ;
2006+ if ( blockedBy === "cancelled" ) {
2007+ controller . abort ( ) ;
2008+ } else {
2009+ sourceEffectsAllowed = false ;
2010+ }
2011+ await params . onPlatformSendDispatch ?.( ) ;
2012+ platformSend ( ) ;
2013+ return {
2014+ channel : "discord" ,
2015+ to : "dm:U123" ,
2016+ via : "direct" as const ,
2017+ mediaUrl : null ,
2018+ result : { messageId : "msg-after-stale-dispatch" } ,
2019+ } ;
2020+ } ) as unknown as typeof runtimeSendMessage ;
2021+
2022+ const result = await deliverDiscordDirectMessageCompletion ( {
2023+ callGateway,
2024+ sendMessage,
2025+ signal : controller . signal ,
2026+ sourceSessionKey : childSessionKey ,
2027+ sourceTool : "subagent_announce" ,
2028+ isSourceSessionEffectsAllowed : ( ) => sourceEffectsAllowed ,
2029+ internalEvents : taskCompletionEvents ( {
2030+ childSessionKey,
2031+ status : "error" ,
2032+ statusLabel : "failed before fallback dispatch" ,
2033+ result : "private child output" ,
2034+ } ) ,
2035+ } ) ;
2036+
2037+ expect ( result ) . toMatchObject (
2038+ blockedBy === "cancelled"
2039+ ? { delivered : false , path : "none" }
2040+ : { delivered : false , path : "none" , reason : "source_owner_changed" , terminal : true } ,
2041+ ) ;
2042+ expect ( sendMessage ) . toHaveBeenCalledOnce ( ) ;
2043+ expect ( platformSend ) . not . toHaveBeenCalled ( ) ;
2044+ } ,
2045+ ) ;
2046+
2047+ it ( "does not send a failed-child notice for an untrusted event or non-direct target" , async ( ) => {
2048+ const callGateway = vi . fn ( async ( ) => {
2049+ throw new Error ( "provider rejected requester synthesis" ) ;
2050+ } ) as unknown as typeof runtimeCallGateway ;
2051+ const sendMessage = createSendMessageMock ( ) ;
2052+
2053+ const untrusted = await deliverDiscordDirectMessageCompletion ( {
2054+ callGateway,
2055+ sendMessage,
2056+ sourceSessionKey : "agent:worker:subagent:expected-child" ,
2057+ sourceTool : "subagent_announce" ,
2058+ internalEvents : taskCompletionEvents ( {
2059+ childSessionKey : "agent:worker:subagent:other-child" ,
2060+ status : "error" ,
2061+ statusLabel : "failed: provider rejected child run" ,
2062+ result : "private child output" ,
2063+ } ) ,
2064+ } ) ;
2065+ const nonDirect = await deliverSlackThreadAnnouncement ( {
2066+ callGateway,
2067+ sendMessage,
2068+ directIdempotencyKey : "announce-failed-thread-child" ,
2069+ sourceSessionKey : "agent:worker:subagent:failed-thread-child" ,
2070+ sourceTool : "subagent_announce" ,
2071+ internalEvents : taskCompletionEvents ( {
2072+ childSessionKey : "agent:worker:subagent:failed-thread-child" ,
2073+ status : "error" ,
2074+ statusLabel : "failed: provider rejected child run" ,
2075+ result : "private child output" ,
2076+ } ) ,
2077+ } ) ;
2078+
2079+ expectRecordFields ( untrusted , { delivered : false , path : "direct" } ) ;
2080+ expectRecordFields ( nonDirect , { delivered : false , path : "direct" } ) ;
2081+ expect ( sendMessage ) . not . toHaveBeenCalled ( ) ;
2082+ } ) ;
2083+
19542084 it ( "directly delivers unprefixed direct targets recognized by the channel grammar" , async ( ) => {
19552085 registerDirectTargetTestChannel ( "qa-channel" ) ;
19562086 const callGateway = createPayloadGatewayMock ( ) ;
0 commit comments