@@ -864,4 +864,97 @@ describe("sessions_send gating", () => {
864864 expect ( details . reply ) . toBeUndefined ( ) ;
865865 expect ( details . sessionKey ) . toBe ( MAIN_AGENT_SESSION_KEY ) ;
866866 } ) ;
867+
868+ it ( "preserves announce delivery metadata on accepted timeout compensation" , async ( ) => {
869+ const tool = createMainSessionsSendTool ( ) ;
870+ let historyCalls = 0 ;
871+ const staleAssistantMessage = {
872+ role : "assistant" ,
873+ content : [ { type : "text" , text : "older reply" } ] ,
874+ timestamp : 20 ,
875+ } ;
876+
877+ callGatewayMock . mockImplementation ( async ( opts : unknown ) => {
878+ const request = opts as { method ?: string } ;
879+ if ( request . method === "sessions.list" ) {
880+ return {
881+ path : "/tmp/sessions.json" ,
882+ sessions : [ { key : MAIN_AGENT_SESSION_KEY , kind : "direct" } ] ,
883+ } ;
884+ }
885+ if ( request . method === "agent" ) {
886+ return { runId : "run-compensated" , acceptedAt : 123 } ;
887+ }
888+ if ( request . method === "agent.wait" ) {
889+ return { runId : "run-compensated" , status : "timeout" , error : "soft wait timed out" } ;
890+ }
891+ if ( request . method === "chat.history" ) {
892+ historyCalls += 1 ;
893+ return { messages : [ staleAssistantMessage ] } ;
894+ }
895+ return { } ;
896+ } ) ;
897+
898+ const result = await tool . execute ( "call-compensated-timeout" , {
899+ sessionKey : MAIN_AGENT_SESSION_KEY ,
900+ message : "ping" ,
901+ timeoutSeconds : 1 ,
902+ } ) ;
903+
904+ expect ( historyCalls ) . toBe ( 2 ) ;
905+ const details = requireDetails ( result ) ;
906+ expect ( details . status ) . toBe ( "accepted" ) ;
907+ expect ( details . delivery ) . toMatchObject ( {
908+ status : "pending" ,
909+ mode : "announce" ,
910+ note : "run accepted, no reply within timeout" ,
911+ } ) ;
912+ } ) ;
913+
914+ it ( "reports compensation read failures on terminal timeouts" , async ( ) => {
915+ const tool = createMainSessionsSendTool ( ) ;
916+ let historyCalls = 0 ;
917+
918+ callGatewayMock . mockImplementation ( async ( opts : unknown ) => {
919+ const request = opts as { method ?: string } ;
920+ if ( request . method === "sessions.list" ) {
921+ return {
922+ path : "/tmp/sessions.json" ,
923+ sessions : [ { key : MAIN_AGENT_SESSION_KEY , kind : "direct" } ] ,
924+ } ;
925+ }
926+ if ( request . method === "agent" ) {
927+ return { runId : "run-terminal-timeout" , acceptedAt : 123 } ;
928+ }
929+ if ( request . method === "agent.wait" ) {
930+ return {
931+ runId : "run-terminal-timeout" ,
932+ status : "timeout" ,
933+ error : "agent wait timed out" ,
934+ endedAt : 1_780_000_000_000 ,
935+ } ;
936+ }
937+ if ( request . method === "chat.history" ) {
938+ historyCalls += 1 ;
939+ if ( historyCalls === 1 ) {
940+ return { messages : [ ] } ;
941+ }
942+ throw new Error ( "history unavailable" ) ;
943+ }
944+ return { } ;
945+ } ) ;
946+
947+ const result = await tool . execute ( "call-terminal-timeout" , {
948+ sessionKey : MAIN_AGENT_SESSION_KEY ,
949+ message : "ping" ,
950+ timeoutSeconds : 1 ,
951+ } ) ;
952+
953+ expect ( historyCalls ) . toBe ( 2 ) ;
954+ const details = requireDetails ( result ) ;
955+ expect ( details . status ) . toBe ( "timeout" ) ;
956+ expect ( details . error ) . toBe (
957+ "agent wait timed out; post-timeout compensation failed: history unavailable" ,
958+ ) ;
959+ } ) ;
867960} ) ;
0 commit comments