I'm having the exact same problem. I had this months ago initially and upgraded to Windows 11 24H2 preview release and it fixed the issue. However that introduced a known bug with ant cheat games that occasionally caused me to get a green screen of death with a memory management error.
I recently (some point in the last two weeks) received a windows update as part of the release preview that has fixed the green screens of death with anti cheat games but it has reintroduced the gamebar issue causing the mouse cursor to no longer stay within the game window.
I've noticed I'm able to use gamebar fine if the xbox social widget isn't open, I can click on elements of other widgets just fine and the cursor stays locked within the games window. As soon as I open gamebar then open the xbox social widget to see my friends list, I close gamebar and the problem occurs, the mouse cursor now doesn't stay within the games window and moves onto monitors at either side of my main gaming screen.