@@ -4649,6 +4649,145 @@ describe("runEmbeddedAgent incomplete-turn safety", () => {
46494649 expect ( result . meta . livenessState ) . toBe ( "working" ) ;
46504650 } ) ;
46514651
4652+ it ( "treats reply-optional post-tool empty stops as silent even after side-effecting tools" , ( ) => {
4653+ // Regression: a cron agentTurn without a delivery route ran a successful
4654+ // replay-unsafe sessions patch and intentionally sent no final text; the run
4655+ // must finish silent, not as an incomplete-turn error.
4656+ const sideEffectToolAttempt = makeAttemptResult ( {
4657+ assistantTexts : [ ] ,
4658+ toolMetas : [ { toolName : "sessions" , meta : "patch archived" , replaySafe : false } ] ,
4659+ lastAssistant : {
4660+ role : "assistant" ,
4661+ stopReason : "stop" ,
4662+ provider : "openai" ,
4663+ model : "gpt-5.5" ,
4664+ content : [ { type : "text" , text : "" } ] ,
4665+ } as unknown as EmbeddedRunAttemptResult [ "lastAssistant" ] ,
4666+ } ) ;
4667+
4668+ expect (
4669+ shouldTreatEmptyAssistantReplyAsSilent ( {
4670+ allowEmptyAssistantReplyAsSilent : true ,
4671+ terminalReplyExpectation : "optional" ,
4672+ payloadCount : 0 ,
4673+ aborted : false ,
4674+ timedOut : false ,
4675+ attempt : sideEffectToolAttempt ,
4676+ } ) ,
4677+ ) . toBe ( true ) ;
4678+ // A required or unspecified terminal reply keeps the ambiguous-failure path.
4679+ expect (
4680+ shouldTreatEmptyAssistantReplyAsSilent ( {
4681+ allowEmptyAssistantReplyAsSilent : true ,
4682+ terminalReplyExpectation : "required" ,
4683+ payloadCount : 0 ,
4684+ aborted : false ,
4685+ timedOut : false ,
4686+ attempt : sideEffectToolAttempt ,
4687+ } ) ,
4688+ ) . toBe ( false ) ;
4689+ expect (
4690+ shouldTreatEmptyAssistantReplyAsSilent ( {
4691+ allowEmptyAssistantReplyAsSilent : true ,
4692+ payloadCount : 0 ,
4693+ aborted : false ,
4694+ timedOut : false ,
4695+ attempt : sideEffectToolAttempt ,
4696+ } ) ,
4697+ ) . toBe ( false ) ;
4698+ } ) ;
4699+
4700+ it ( "keeps reply-optional runs erroring on real failure states" , ( ) => {
4701+ const toolErrorAttempt = makeAttemptResult ( {
4702+ assistantTexts : [ ] ,
4703+ toolMetas : [ { toolName : "sessions" , meta : "patch failed" , replaySafe : false , isError : true } ] ,
4704+ lastToolError : { toolName : "sessions" , error : "patch failed" } ,
4705+ lastAssistant : {
4706+ role : "assistant" ,
4707+ stopReason : "stop" ,
4708+ provider : "openai" ,
4709+ model : "gpt-5.5" ,
4710+ content : [ { type : "text" , text : "" } ] ,
4711+ } as unknown as EmbeddedRunAttemptResult [ "lastAssistant" ] ,
4712+ } ) ;
4713+ const errorStopAttempt = makeAttemptResult ( {
4714+ assistantTexts : [ ] ,
4715+ toolMetas : [ { toolName : "sessions" , meta : "patch archived" , replaySafe : false } ] ,
4716+ lastAssistant : {
4717+ role : "assistant" ,
4718+ stopReason : "error" ,
4719+ provider : "openai" ,
4720+ model : "gpt-5.5" ,
4721+ content : [ ] ,
4722+ } as unknown as EmbeddedRunAttemptResult [ "lastAssistant" ] ,
4723+ } ) ;
4724+
4725+ expect (
4726+ shouldTreatEmptyAssistantReplyAsSilent ( {
4727+ allowEmptyAssistantReplyAsSilent : true ,
4728+ terminalReplyExpectation : "optional" ,
4729+ payloadCount : 0 ,
4730+ aborted : false ,
4731+ timedOut : false ,
4732+ attempt : toolErrorAttempt ,
4733+ } ) ,
4734+ ) . toBe ( false ) ;
4735+ expect (
4736+ shouldTreatEmptyAssistantReplyAsSilent ( {
4737+ allowEmptyAssistantReplyAsSilent : true ,
4738+ terminalReplyExpectation : "optional" ,
4739+ payloadCount : 0 ,
4740+ aborted : false ,
4741+ timedOut : false ,
4742+ attempt : errorStopAttempt ,
4743+ } ) ,
4744+ ) . toBe ( false ) ;
4745+ expect (
4746+ shouldTreatEmptyAssistantReplyAsSilent ( {
4747+ allowEmptyAssistantReplyAsSilent : true ,
4748+ terminalReplyExpectation : "optional" ,
4749+ payloadCount : 0 ,
4750+ aborted : true ,
4751+ timedOut : false ,
4752+ attempt : errorStopAttempt ,
4753+ } ) ,
4754+ ) . toBe ( false ) ;
4755+ } ) ;
4756+
4757+ it ( "returns NO_REPLY for reply-optional cron-style runs whose side-effecting tools succeeded" , async ( ) => {
4758+ mockedClassifyFailoverReason . mockReturnValue ( null ) ;
4759+ mockedRunEmbeddedAttempt . mockResolvedValue (
4760+ makeAttemptResult ( {
4761+ assistantTexts : [ ] ,
4762+ toolMetas : [ { toolName : "sessions" , meta : "patch archived" , replaySafe : false } ] ,
4763+ itemLifecycle : { startedCount : 1 , completedCount : 1 , activeCount : 0 } ,
4764+ lastAssistant : {
4765+ role : "assistant" ,
4766+ stopReason : "stop" ,
4767+ provider : "openai" ,
4768+ model : "gpt-5.5" ,
4769+ content : [ { type : "text" , text : "" } ] ,
4770+ } as unknown as EmbeddedRunAttemptResult [ "lastAssistant" ] ,
4771+ } ) ,
4772+ ) ;
4773+
4774+ const result = await runEmbeddedAgent ( {
4775+ ...overflowBaseRunParams ,
4776+ allowEmptyAssistantReplyAsSilent : true ,
4777+ terminalReplyExpectation : "optional" ,
4778+ provider : "openai" ,
4779+ model : "gpt-5.5" ,
4780+ runId : "run-reply-optional-post-tool-silent" ,
4781+ } ) ;
4782+
4783+ expect ( mockedRunEmbeddedAttempt ) . toHaveBeenCalledTimes ( 1 ) ;
4784+ expectNoWarnMessageWith ( "incomplete turn detected" ) ;
4785+ expect ( result . payloads ) . toEqual ( [ { text : "NO_REPLY" } ] ) ;
4786+ expect ( result . meta . error ) . toBeUndefined ( ) ;
4787+ expect ( result . meta . terminalReplyKind ) . toBe ( "silent-empty" ) ;
4788+ expect ( result . meta . livenessState ) . toBe ( "working" ) ;
4789+ } ) ;
4790+
46524791 it ( "keeps retrying and surfacing clean empty assistant turns without the silence flag" , async ( ) => {
46534792 mockedClassifyFailoverReason . mockReturnValue ( null ) ;
46544793 mockedRunEmbeddedAttempt . mockResolvedValue (
0 commit comments