@@ -4496,7 +4496,36 @@ describe("deliverSubagentAnnouncement completion delivery", () => {
44964496 reason : "visible_reply_missing" ,
44974497 } as const ;
44984498
4499- it . each ( [
4499+ const externalRequesterSettleRoute = {
4500+ name : "Discord" ,
4501+ sessionKey : "agent:main:discord:dm:U123" ,
4502+ origin : { channel : "discord" , to : "dm:U123" , accountId : "acct-1" } ,
4503+ agentParams : { deliver : true , channel : "discord" , accountId : "acct-1" , to : "dm:U123" } ,
4504+ } ;
4505+ const requesterSettleRoutes = [
4506+ externalRequesterSettleRoute ,
4507+ {
4508+ name : "no origin" ,
4509+ sessionKey : "agent:main:requester-settle" ,
4510+ origin : undefined ,
4511+ agentParams : { deliver : false , channel : undefined , accountId : undefined , to : undefined } ,
4512+ } ,
4513+ {
4514+ name : "WebChat" ,
4515+ sessionKey : "agent:main:webchat:dm:requester-settle" ,
4516+ origin : { channel : "webchat" } ,
4517+ agentParams : { deliver : false , channel : "webchat" , accountId : undefined , to : undefined } ,
4518+ } ,
4519+ {
4520+ name : "nested requester with inherited Discord origin" ,
4521+ sessionKey : "agent:main:subagent:requester-settle" ,
4522+ origin : externalRequesterSettleRoute . origin ,
4523+ requesterIsSubagent : true ,
4524+ agentParams : { deliver : false , channel : undefined , accountId : undefined , to : undefined } ,
4525+ } ,
4526+ ] ;
4527+
4528+ const requesterSettleCases = [
45004529 {
45014530 name : "preserves an ordinary non-yielded direct settle turn" ,
45024531 response : { } ,
@@ -4511,6 +4540,7 @@ describe("deliverSubagentAnnouncement completion delivery", () => {
45114540 } ,
45124541 {
45134542 name : "accepts a yielded requester's visible final answer" ,
4543+ routes : requesterSettleRoutes ,
45144544 response : { result : { payloads : [ { text : "The consolidated answer." } ] } } ,
45154545 requireVisibleReply : true ,
45164546 expected : deliveredRequesterFinal ,
@@ -4528,54 +4558,65 @@ describe("deliverSubagentAnnouncement completion delivery", () => {
45284558 } ,
45294559 {
45304560 name : "rejects a yielded turn without a result" ,
4561+ routes : requesterSettleRoutes ,
45314562 response : { } ,
45324563 requireVisibleReply : true ,
45334564 expected : missingRequesterFinal ,
45344565 } ,
45354566 {
45364567 name : "rejects a yielded turn with no response payloads" ,
4568+ routes : requesterSettleRoutes ,
45374569 response : { result : { payloads : [ ] } } ,
45384570 requireVisibleReply : true ,
45394571 expected : missingRequesterFinal ,
45404572 } ,
45414573 {
45424574 name : "rejects a yielded turn that emits only an error" ,
4575+ routes : requesterSettleRoutes ,
45434576 response : { result : { payloads : [ { text : "tool failed" , isError : true } ] } } ,
45444577 requireVisibleReply : true ,
45454578 expected : missingRequesterFinal ,
45464579 } ,
45474580 {
45484581 name : "rejects a yielded turn that emits only private reasoning" ,
4582+ routes : requesterSettleRoutes ,
45494583 response : { result : { payloads : [ { text : "thinking" , isReasoning : true } ] } } ,
45504584 requireVisibleReply : true ,
45514585 expected : missingRequesterFinal ,
45524586 } ,
45534587 {
45544588 name : "rejects pre-tool commentary instead of a final answer" ,
4589+ routes : requesterSettleRoutes ,
45554590 response : { result : { payloads : [ { text : "working on it" , isCommentary : true } ] } } ,
45564591 requireVisibleReply : true ,
45574592 expected : missingRequesterFinal ,
45584593 } ,
45594594 {
45604595 name : "rejects a compaction notice instead of a final answer" ,
4596+ routes : requesterSettleRoutes ,
45614597 response : { result : { payloads : [ { text : "compacting" , isCompactionNotice : true } ] } } ,
45624598 requireVisibleReply : true ,
45634599 expected : missingRequesterFinal ,
45644600 } ,
45654601 {
45664602 name : "rejects a provider-fallback notice instead of a final answer" ,
4567- response : { result : { payloads : [ { text : "switching providers" , isFallbackNotice : true } ] } } ,
4603+ routes : requesterSettleRoutes ,
4604+ response : {
4605+ result : { payloads : [ { text : "switching providers" , isFallbackNotice : true } ] } ,
4606+ } ,
45684607 requireVisibleReply : true ,
45694608 expected : missingRequesterFinal ,
45704609 } ,
45714610 {
45724611 name : "rejects a transient status notice instead of a final answer" ,
4612+ routes : requesterSettleRoutes ,
45734613 response : { result : { payloads : [ { text : "still working" , isStatusNotice : true } ] } } ,
45744614 requireVisibleReply : true ,
45754615 expected : missingRequesterFinal ,
45764616 } ,
45774617 {
45784618 name : "rejects supplemental TTS audio instead of a final answer" ,
4619+ routes : requesterSettleRoutes ,
45794620 response : {
45804621 result : {
45814622 payloads : [
@@ -4603,18 +4644,21 @@ describe("deliverSubagentAnnouncement completion delivery", () => {
46034644 } ,
46044645 {
46054646 name : "rejects an explicitly hidden assistant payload" ,
4647+ routes : requesterSettleRoutes ,
46064648 response : { result : { payloads : [ { text : "not user visible" , visible : false } ] } } ,
46074649 requireVisibleReply : true ,
46084650 expected : missingRequesterFinal ,
46094651 } ,
46104652 {
46114653 name : "rejects a yielded turn that emits only the silent reply token" ,
4654+ routes : requesterSettleRoutes ,
46124655 response : { result : { payloads : [ { text : "NO_REPLY" } ] } } ,
46134656 requireVisibleReply : true ,
46144657 expected : missingRequesterFinal ,
46154658 } ,
46164659 {
4617- name : "rejects a visible final whose external delivery was suppressed" ,
4660+ name : "rejects a visible final whose delivery was suppressed" ,
4661+ routes : requesterSettleRoutes ,
46184662 response : {
46194663 result : {
46204664 payloads : [ { text : "never delivered" } ] ,
@@ -4774,14 +4818,62 @@ describe("deliverSubagentAnnouncement completion delivery", () => {
47744818 requireVisibleReply : true ,
47754819 expected : deliveredRequesterFinal ,
47764820 } ,
4777- ] ) ( "$name" , async ( { response, requireVisibleReply, expected } ) => {
4821+ {
4822+ name : "rejects terminal text when an external origin cannot be resolved" ,
4823+ routes : [
4824+ {
4825+ name : "external channel without destination" ,
4826+ sessionKey : "agent:main:requester-settle" ,
4827+ origin : { channel : "discord" } ,
4828+ agentParams : {
4829+ deliver : false ,
4830+ channel : "discord" ,
4831+ accountId : undefined ,
4832+ to : undefined ,
4833+ } ,
4834+ } ,
4835+ {
4836+ name : "destination without channel" ,
4837+ sessionKey : "agent:main:requester-settle" ,
4838+ origin : { to : "dm:U123" } ,
4839+ agentParams : {
4840+ deliver : false ,
4841+ channel : undefined ,
4842+ accountId : undefined ,
4843+ to : undefined ,
4844+ } ,
4845+ } ,
4846+ {
4847+ name : "unknown channel" ,
4848+ sessionKey : "agent:main:requester-settle" ,
4849+ origin : { channel : "unknown-external" , to : "dm:U123" } ,
4850+ agentParams : {
4851+ deliver : false ,
4852+ channel : undefined ,
4853+ accountId : undefined ,
4854+ to : undefined ,
4855+ } ,
4856+ } ,
4857+ ] ,
4858+ response : { result : { payloads : [ { text : "The consolidated answer." } ] } } ,
4859+ requireVisibleReply : true ,
4860+ expected : missingRequesterFinal ,
4861+ } ,
4862+ ] ;
4863+
4864+ it . each (
4865+ requesterSettleCases . flatMap ( ( testCase ) =>
4866+ ( testCase . routes ?? [ externalRequesterSettleRoute ] ) . map ( ( route ) => ( {
4867+ testCase,
4868+ route,
4869+ } ) ) ,
4870+ ) ,
4871+ ) ( "$route.name: $testCase.name" , async ( { testCase, route } ) => {
4872+ const { response, requireVisibleReply, expected } = testCase ;
47784873 const callGateway = createGatewayMock ( response ) ;
47794874 const queueEmbeddedAgentMessageWithOutcome = createQueueOutcomeMock ( true ) ;
4780- const origin = {
4781- channel : "discord" ,
4782- to : "dm:U123" ,
4783- accountId : "acct-1" ,
4784- } ;
4875+ const sendMessage = createSendMessageMock ( ) ;
4876+ const origin = route . origin ;
47854877 testing . setDepsForTest ( {
47864878 callGateway,
47874879 getRequesterSessionActivity : ( ) => ( {
@@ -4790,17 +4882,18 @@ describe("deliverSubagentAnnouncement completion delivery", () => {
47904882 } ) ,
47914883 getRuntimeConfig : ( ) => ( { } ) as never ,
47924884 queueEmbeddedAgentMessageWithOutcome,
4885+ sendMessage,
47934886 } ) ;
47944887
47954888 const result = await deliverSubagentAnnouncement ( {
4796- requesterSessionKey : "agent:main:discord:dm:U123" ,
4797- targetRequesterSessionKey : "agent:main:discord:dm:U123" ,
4889+ requesterSessionKey : route . sessionKey ,
4890+ targetRequesterSessionKey : route . sessionKey ,
47984891 triggerMessage : "all spawned subagents settled" ,
47994892 steerMessage : "all spawned subagents settled" ,
48004893 requesterOrigin : origin ,
48014894 requesterSessionOrigin : origin ,
48024895 directOrigin : origin ,
4803- requesterIsSubagent : false ,
4896+ requesterIsSubagent : "requesterIsSubagent" in route && route . requesterIsSubagent === true ,
48044897 expectsCompletionMessage : false ,
48054898 requireDirectDelivery : true ,
48064899 ...( requireVisibleReply ? { requireVisibleReply : true } : { } ) ,
@@ -4809,14 +4902,13 @@ describe("deliverSubagentAnnouncement completion delivery", () => {
48094902 } ) ;
48104903
48114904 expect ( result ) . toMatchObject ( expected ) ;
4905+ expect ( result . requesterVisibleFinalDelivered ) . toBeUndefined ( ) ;
48124906 expect ( queueEmbeddedAgentMessageWithOutcome ) . not . toHaveBeenCalled ( ) ;
4813- const agentParams = expectGatewayAgentParams ( callGateway , {
4814- deliver : true ,
4815- channel : "discord" ,
4816- accountId : "acct-1" ,
4817- to : "dm:U123" ,
4818- } ) ;
4819- expect ( agentParams . sourceReplyDeliveryMode ) . toBe ( requireVisibleReply ? "automatic" : undefined ) ;
4907+ expect ( sendMessage ) . not . toHaveBeenCalled ( ) ;
4908+ const agentParams = expectGatewayAgentParams ( callGateway , route . agentParams ) ;
4909+ expect ( agentParams . sourceReplyDeliveryMode ) . toBe (
4910+ requireVisibleReply && route . agentParams . deliver ? "automatic" : undefined ,
4911+ ) ;
48204912 } ) ;
48214913
48224914 it . each ( [
0 commit comments