What helps me is a really good manager - of which I have had two out of ??? in 25 years. When I'm out of tasks they allowed me to refactor and fix longstanding difficult bugs. It has the added advantage of getting me deeper into the code and allowing me to build better foundations for future work which makes me more productive for a nice feedback loop.
They also have allowed me to train up other developers and write up official coding standards with down time and that has been great, too. It's nice when all your code looks mostly the same.
Also being able to repeatedly decline a promotion to management but still get paid well has been good for company loyalty.
There is a downside in that most days feel like, well, kind of like a Games Workshop orc going "Waaaaaaaah!" where I'm just hurtling through the work and my brain is just spinning max RPM and then I go home and it's hard to stop the momentum. I have to excercise to clear my head or I won't sleep.