This happens to me as well, it's easy to keep churning out code when it's coming from my current bag of knowledge. My flow gets wrecked when I try to improve my code, because I have to stop writing the way I'm currently writing things the way I want to. When I mean "improve" code, it has to attain a certain metric and would usually entail giving benefits to other people working on the same codebase. Do I want the code to be more readable? more performant? more modular? more abstracted? would it depend-on/use another library?
> What type of work do you do that regularly gets you in this state?
I find that I go back to a stop-start fashion of working when I don't know have a list of small wins I need to do to finish a big feature or "improving" code. Without that list, it leads me to procrastination.
So the first type of work I do? I bust out my pen and I get a piece of paper. Then I write down a small checklist of classes/methods I need to write, or small things I need to research (with a timebox).