You sound exactly like me, except that your problems developed later in life. I have ADHD and have never had much success in managing it. I've spent years trying to figure out why that is and I've got a theory/solution that I'm working on.
The theory: I've developed a cycle of habits involving research and I have few ways to disrupt that cycle. The cycle's usually entered through wondering if something exists (I have a new idea) or wanting to answer a question (best practices for TDD?).
It explains the difficulty in trying to avoid the cycle (it gets triggered routinely throughout the day...complete avoidance = massive willpower drain), as well as my difficulty in establishing new habits (the likelihood of a habit I'm developing gets interrupted before it's solid increases with time, violating the "don't break the chain" concept). It also explains why a highly collaborative work environment overcomes the issues, though I've only experienced that at hackathons. It also explains why the cycle developed when you were isolated and without work: the brain pumps out tons of dopamine when we encounter novelty (eg. learn something new) and without a project, that's one of the main things we programmers do.
Here's what I'm working on (first draft): https://github.com/ADHDExperiments/Rehabbit
Right now, I'm trying to write up something I can start posting in behavioral health groups online to try and get experts to help flesh it out. It may be overkill, but I'm not sure of another way out.
Please contact me; info's in profile. We should definitely talk!