@@ -385,6 +385,102 @@ describe("GooseProcess", () => {
385385 } ,
386386 ) ;
387387
388+ it ( "ends the turn once goose reports a final send's result" , async ( ) => {
389+ const { open, process } = await start ( ) ;
390+ process . hold = true ;
391+ const session = await open ( ) ;
392+ const settled = session . send ( "hello" ) ;
393+ await flush ( ) ;
394+ await process . mcp ( session . id , "tools/call" , {
395+ name : "send" ,
396+ arguments : { content : "hi" , final : true } ,
397+ } ) ;
398+ await expect ( settled ) . resolves . toEqual ( { ok : true , answered : true } ) ;
399+ // Cancelled before goose has the result, the send would be saved as
400+ // interrupted.
401+ process . update ( session . id , {
402+ sessionUpdate : "tool_call_update" ,
403+ toolCallId : "t1" ,
404+ status : "in_progress" ,
405+ } ) ;
406+ await flush ( ) ;
407+ expect ( process . cancelled ) . toEqual ( [ ] ) ;
408+ process . update ( session . id , {
409+ sessionUpdate : "tool_call_update" ,
410+ toolCallId : "t1" ,
411+ status : "completed" ,
412+ } ) ;
413+ await flush ( ) ;
414+ expect ( process . cancelled ) . toEqual ( [ session . id ] ) ;
415+ await session . idle ( ) ;
416+ expect ( session . working ) . toBe ( false ) ;
417+ // The next turn is not cancelled by its own results.
418+ const next = session . send ( "again" ) ;
419+ await flush ( ) ;
420+ process . update ( session . id , {
421+ sessionUpdate : "tool_call_update" ,
422+ toolCallId : "t2" ,
423+ status : "completed" ,
424+ } ) ;
425+ await flush ( ) ;
426+ expect ( process . cancelled ) . toEqual ( [ session . id ] ) ;
427+ process . turns [ 1 ] ?. finish ( ) ;
428+ await expect ( next ) . resolves . toEqual ( { ok : true } ) ;
429+ } ) ;
430+
431+ it ( "runs a message that arrives as a final send ends its turn as the next turn" , async ( ) => {
432+ const { open, process } = await start ( ) ;
433+ process . hold = true ;
434+ const session = await open ( ) ;
435+ const first = session . send ( "one" ) ;
436+ await flush ( ) ;
437+ await process . mcp ( session . id , "tools/call" , {
438+ name : "send" ,
439+ arguments : { content : "hi" , final : true } ,
440+ } ) ;
441+ await expect ( first ) . resolves . toEqual ( { ok : true , answered : true } ) ;
442+ const second = session . send ( "two" ) ;
443+ await flush ( ) ;
444+ expect ( process . turns [ 0 ] ?. steered ) . toEqual ( [ ] ) ;
445+ process . update ( session . id , {
446+ sessionUpdate : "tool_call_update" ,
447+ toolCallId : "t1" ,
448+ status : "completed" ,
449+ } ) ;
450+ await flush ( ) ;
451+ expect ( process . cancelled ) . toEqual ( [ session . id ] ) ;
452+ expect ( process . turns . map ( ( turn ) => turn . text ) ) . toEqual ( [ "one" , "two" ] ) ;
453+ process . turns [ 1 ] ?. finish ( ) ;
454+ await expect ( second ) . resolves . toEqual ( { ok : true } ) ;
455+ } ) ;
456+
457+ it ( "keeps the turn going after a final send while another message waits" , async ( ) => {
458+ const { open, process } = await start ( ) ;
459+ process . hold = true ;
460+ const session = await open ( ) ;
461+ const first = session . send ( "one" ) ;
462+ await flush ( ) ;
463+ const second = session . send ( "two" ) ;
464+ await flush ( ) ;
465+ expect ( process . turns [ 0 ] ?. steered ) . toEqual ( [ "two" ] ) ;
466+ await process . mcp ( session . id , "tools/call" , {
467+ name : "send" ,
468+ arguments : { content : "hi" , final : true } ,
469+ } ) ;
470+ process . update ( session . id , {
471+ sessionUpdate : "tool_call_update" ,
472+ toolCallId : "t1" ,
473+ status : "completed" ,
474+ } ) ;
475+ await flush ( ) ;
476+ expect ( process . cancelled ) . toEqual ( [ ] ) ;
477+ process . turns [ 0 ] ?. finish ( ) ;
478+ await expect ( Promise . all ( [ first , second ] ) ) . resolves . toEqual ( [
479+ { ok : true } ,
480+ { ok : true , answered : true } ,
481+ ] ) ;
482+ } ) ;
483+
388484 it ( "steers a message that arrives mid-turn into it" , async ( ) => {
389485 const { open, process } = await start ( ) ;
390486 process . hold = true ;
0 commit comments