@@ -859,6 +859,172 @@ describe("buildGoogleRealtimeVoiceProvider", () => {
859859 ] ) ;
860860 } ) ;
861861
862+ it ( "preserves tool ownership while reusing a resumption handle" , async ( ) => {
863+ vi . useFakeTimers ( ) ;
864+ const provider = buildGoogleRealtimeVoiceProvider ( ) ;
865+ const onToolCall = vi . fn ( ) ;
866+ const bridge = provider . createBridge ( {
867+ providerConfig : { apiKey : "gemini-key" } ,
868+ onAudio : vi . fn ( ) ,
869+ onClearAudio : vi . fn ( ) ,
870+ onToolCall,
871+ } ) ;
872+
873+ await bridge . connect ( ) ;
874+ const firstSession = lastConnectParams ( ) . callbacks ;
875+ firstSession . onmessage ( {
876+ sessionResumptionUpdate : { resumable : true , newHandle : "resume-1" } ,
877+ toolCall : {
878+ functionCalls : [ { id : "call-1" , name : "lookup" , args : { query : "before" } } ] ,
879+ } ,
880+ } ) ;
881+ firstSession . onclose ( { code : 1011 , reason : "temporary" } ) ;
882+ void bridge . submitToolResult ( "call-1" , { result : "ok" } ) ;
883+ expect ( session . sendToolResponse ) . not . toHaveBeenCalled ( ) ;
884+ await vi . advanceTimersByTimeAsync ( 250 ) ;
885+
886+ const resumedSession = lastConnectParams ( ) . callbacks ;
887+ resumedSession . onmessage ( {
888+ toolCall : {
889+ functionCalls : [ { id : "call-1" , name : "different" , args : { query : "replay" } } ] ,
890+ } ,
891+ } ) ;
892+ resumedSession . onopen ( ) ;
893+ resumedSession . onmessage ( { setupComplete : { } } ) ;
894+
895+ expect ( lastConnectParams ( ) . config . sessionResumption ) . toEqual ( { handle : "resume-1" } ) ;
896+ expect ( onToolCall ) . toHaveBeenCalledOnce ( ) ;
897+ expect ( session . sendToolResponse ) . toHaveBeenCalledWith ( {
898+ functionResponses : [
899+ {
900+ id : "call-1" ,
901+ name : "lookup" ,
902+ response : { result : "ok" } ,
903+ } ,
904+ ] ,
905+ } ) ;
906+ } ) ;
907+
908+ it ( "fails closed when resumable tool responses exceed the reconnect buffer" , async ( ) => {
909+ vi . useFakeTimers ( ) ;
910+ const provider = buildGoogleRealtimeVoiceProvider ( ) ;
911+ const onError = vi . fn ( ) ;
912+ const onClose = vi . fn ( ) ;
913+ const bridge = provider . createBridge ( {
914+ providerConfig : { apiKey : "gemini-key" } ,
915+ onAudio : vi . fn ( ) ,
916+ onClearAudio : vi . fn ( ) ,
917+ onToolCall : vi . fn ( ) ,
918+ onError,
919+ onClose,
920+ } ) ;
921+
922+ await bridge . connect ( ) ;
923+ const firstSession = lastConnectParams ( ) . callbacks ;
924+ firstSession . onmessage ( {
925+ sessionResumptionUpdate : { resumable : true , newHandle : "resume-1" } ,
926+ toolCall : {
927+ functionCalls : [ { id : "call-1" , name : "lookup" , args : { } } ] ,
928+ } ,
929+ } ) ;
930+ firstSession . onclose ( { code : 1011 , reason : "temporary" } ) ;
931+ onError . mockClear ( ) ;
932+
933+ void bridge . submitToolResult ( "call-1" , { result : "x" . repeat ( 1024 * 1024 ) } ) ;
934+
935+ expect ( requireFirstError ( onError ) . message ) . toBe (
936+ "Google Live reconnect tool-response buffer limit exceeded" ,
937+ ) ;
938+ expect ( onClose ) . toHaveBeenCalledWith ( "error" ) ;
939+ expect ( session . sendToolResponse ) . not . toHaveBeenCalled ( ) ;
940+ } ) ;
941+
942+ it ( "drops queued reconnect responses when the resumed session cancels their call" , async ( ) => {
943+ vi . useFakeTimers ( ) ;
944+ const provider = buildGoogleRealtimeVoiceProvider ( ) ;
945+ const onEvent = vi . fn ( ) ;
946+ const bridge = provider . createBridge ( {
947+ providerConfig : { apiKey : "gemini-key" } ,
948+ onAudio : vi . fn ( ) ,
949+ onClearAudio : vi . fn ( ) ,
950+ onToolCall : vi . fn ( ) ,
951+ onEvent,
952+ } ) ;
953+
954+ await bridge . connect ( ) ;
955+ const firstSession = lastConnectParams ( ) . callbacks ;
956+ firstSession . onmessage ( {
957+ sessionResumptionUpdate : { resumable : true , newHandle : "resume-1" } ,
958+ toolCall : {
959+ functionCalls : [ { id : "call-1" , name : "lookup" , args : { } } ] ,
960+ } ,
961+ } ) ;
962+ firstSession . onclose ( { code : 1011 , reason : "temporary" } ) ;
963+ await vi . advanceTimersByTimeAsync ( 250 ) ;
964+
965+ const resumedSession = lastConnectParams ( ) . callbacks ;
966+ void bridge . submitToolResult ( "call-1" , { result : "stale" } ) ;
967+ expect ( session . sendToolResponse ) . not . toHaveBeenCalled ( ) ;
968+ resumedSession . onopen ( ) ;
969+ resumedSession . onmessage ( {
970+ setupComplete : { } ,
971+ toolCallCancellation : { ids : [ "call-1" ] } ,
972+ } ) ;
973+
974+ expect ( session . sendToolResponse ) . not . toHaveBeenCalled ( ) ;
975+ expect ( onEvent ) . toHaveBeenCalledWith ( {
976+ direction : "server" ,
977+ type : "tool.call.cancelled" ,
978+ itemId : "call-1" ,
979+ } ) ;
980+ } ) ;
981+
982+ it ( "resets tool ownership before a fresh automatic reconnect" , async ( ) => {
983+ vi . useFakeTimers ( ) ;
984+ const provider = buildGoogleRealtimeVoiceProvider ( ) ;
985+ const onToolCall = vi . fn ( ) ;
986+ const onEvent = vi . fn ( ) ;
987+ const bridge = provider . createBridge ( {
988+ providerConfig : { apiKey : "gemini-key" } ,
989+ onAudio : vi . fn ( ) ,
990+ onClearAudio : vi . fn ( ) ,
991+ onToolCall,
992+ onEvent,
993+ } ) ;
994+
995+ await bridge . connect ( ) ;
996+ const firstSession = lastConnectParams ( ) . callbacks ;
997+ firstSession . onmessage ( {
998+ toolCall : {
999+ functionCalls : [ { id : "call-1" , name : "old_lookup" , args : { } } ] ,
1000+ } ,
1001+ } ) ;
1002+ firstSession . onclose ( { code : 1011 , reason : "temporary" } ) ;
1003+ await vi . advanceTimersByTimeAsync ( 250 ) ;
1004+
1005+ lastConnectParams ( ) . callbacks . onmessage ( {
1006+ toolCall : {
1007+ functionCalls : [ { id : "call-1" , name : "new_lookup" , args : { } } ] ,
1008+ } ,
1009+ } ) ;
1010+ void bridge . submitToolResult ( "call-1" , { result : "ok" } ) ;
1011+
1012+ expect ( onEvent ) . toHaveBeenCalledWith ( {
1013+ direction : "client" ,
1014+ type : "session.continuity.reset" ,
1015+ } ) ;
1016+ expect ( onToolCall ) . toHaveBeenCalledTimes ( 2 ) ;
1017+ expect ( session . sendToolResponse ) . toHaveBeenCalledWith ( {
1018+ functionResponses : [
1019+ {
1020+ id : "call-1" ,
1021+ name : "new_lookup" ,
1022+ response : { result : "ok" } ,
1023+ } ,
1024+ ] ,
1025+ } ) ;
1026+ } ) ;
1027+
8621028 it ( "preserves continuity when resumability recovers before reconnect" , async ( ) => {
8631029 vi . useFakeTimers ( ) ;
8641030 const provider = buildGoogleRealtimeVoiceProvider ( ) ;
@@ -1952,6 +2118,114 @@ describe("buildGoogleRealtimeVoiceProvider", () => {
19522118 } ) ;
19532119 } ) ;
19542120
2121+ it ( "deduplicates replayed Google Live tool calls by call id" , async ( ) => {
2122+ const provider = buildGoogleRealtimeVoiceProvider ( ) ;
2123+ const onToolCall = vi . fn ( ) ;
2124+ const bridge = provider . createBridge ( {
2125+ providerConfig : { apiKey : "gemini-key" } ,
2126+ onAudio : vi . fn ( ) ,
2127+ onClearAudio : vi . fn ( ) ,
2128+ onToolCall,
2129+ } ) ;
2130+
2131+ await bridge . connect ( ) ;
2132+ const callbacks = lastConnectParams ( ) . callbacks ;
2133+ callbacks . onmessage ( {
2134+ toolCall : {
2135+ functionCalls : [ { id : "call-1" , name : "lookup" , args : { query : "first" } } ] ,
2136+ } ,
2137+ } ) ;
2138+ callbacks . onmessage ( {
2139+ toolCall : {
2140+ functionCalls : [ { id : "call-1" , name : "different" , args : { query : "replay" } } ] ,
2141+ } ,
2142+ } ) ;
2143+
2144+ expect ( onToolCall ) . toHaveBeenCalledOnce ( ) ;
2145+ void bridge . submitToolResult ( "call-1" , { result : "ok" } ) ;
2146+ expect ( session . sendToolResponse ) . toHaveBeenCalledWith ( {
2147+ functionResponses : [
2148+ {
2149+ id : "call-1" ,
2150+ name : "lookup" ,
2151+ response : { result : "ok" } ,
2152+ } ,
2153+ ] ,
2154+ } ) ;
2155+ callbacks . onmessage ( {
2156+ toolCall : {
2157+ functionCalls : [ { id : "call-1" , name : "lookup" , args : { query : "late replay" } } ] ,
2158+ } ,
2159+ } ) ;
2160+ expect ( onToolCall ) . toHaveBeenCalledOnce ( ) ;
2161+ } ) ;
2162+
2163+ it ( "ignores late results after Google cancels a tool call" , async ( ) => {
2164+ const provider = buildGoogleRealtimeVoiceProvider ( ) ;
2165+ const onEvent = vi . fn ( ) ;
2166+ const onError = vi . fn ( ) ;
2167+ const bridge = provider . createBridge ( {
2168+ providerConfig : { apiKey : "gemini-key" } ,
2169+ onAudio : vi . fn ( ) ,
2170+ onClearAudio : vi . fn ( ) ,
2171+ onToolCall : vi . fn ( ) ,
2172+ onEvent,
2173+ onError,
2174+ } ) ;
2175+
2176+ await bridge . connect ( ) ;
2177+ const callbacks = lastConnectParams ( ) . callbacks ;
2178+ callbacks . onmessage ( {
2179+ toolCall : {
2180+ functionCalls : [ { id : "call-1" , name : "lookup" , args : { query : "hi" } } ] ,
2181+ } ,
2182+ } ) ;
2183+ callbacks . onmessage ( {
2184+ toolCallCancellation : { ids : [ "call-1" ] } ,
2185+ } ) ;
2186+
2187+ void bridge . submitToolResult ( "call-1" , { result : "late" } ) ;
2188+
2189+ expect ( session . sendToolResponse ) . not . toHaveBeenCalled ( ) ;
2190+ expect ( onError ) . not . toHaveBeenCalled ( ) ;
2191+ expect ( onEvent ) . toHaveBeenCalledWith ( {
2192+ direction : "server" ,
2193+ type : "tool.call.cancelled" ,
2194+ itemId : "call-1" ,
2195+ } ) ;
2196+ } ) ;
2197+
2198+ it ( "fails closed when Google exceeds the tool-call session limit" , async ( ) => {
2199+ const provider = buildGoogleRealtimeVoiceProvider ( ) ;
2200+ const onToolCall = vi . fn ( ) ;
2201+ const onError = vi . fn ( ) ;
2202+ const onClose = vi . fn ( ) ;
2203+ const bridge = provider . createBridge ( {
2204+ providerConfig : { apiKey : "gemini-key" } ,
2205+ onAudio : vi . fn ( ) ,
2206+ onClearAudio : vi . fn ( ) ,
2207+ onToolCall,
2208+ onError,
2209+ onClose,
2210+ } ) ;
2211+
2212+ await bridge . connect ( ) ;
2213+ lastConnectParams ( ) . callbacks . onmessage ( {
2214+ toolCall : {
2215+ functionCalls : Array . from ( { length : 1_025 } , ( _ , index ) => ( {
2216+ id : `call-${ index } ` ,
2217+ name : "lookup" ,
2218+ args : { } ,
2219+ } ) ) ,
2220+ } ,
2221+ } ) ;
2222+
2223+ expect ( onToolCall ) . toHaveBeenCalledTimes ( 1_024 ) ;
2224+ expect ( requireFirstError ( onError ) . message ) . toBe ( "Google Live tool-call session limit exceeded" ) ;
2225+ expect ( session . close ) . toHaveBeenCalledOnce ( ) ;
2226+ expect ( onClose ) . toHaveBeenCalledWith ( "error" ) ;
2227+ } ) ;
2228+
19552229 it ( "keeps Google Live consult calls open after continuing tool responses" , async ( ) => {
19562230 const provider = buildGoogleRealtimeVoiceProvider ( ) ;
19572231 const bridge = provider . createBridge ( {
0 commit comments