I left. My new job has lots of tech debt, lots of processes that are broken, but when I propose changes people listen. When I put in the work I can see progress. It's hard work and frustrating, but I know that it's because the problems I'm tackling are hard, not because I'm a failure or lacking. That's a very different environment.
I can only give a very broad bit of advice here -- having a positive, open environment is very important. Gaslighting will bring down even the most secure, confident person over time. If you can't change the environment, leaving to find a better one is the right thing to do. What that looks like will differ on one's situation -- might be a long break from coding, might be changing teams, might be changing companies. But we absolutely need people around us who believe in us to work sustainably.