Magnesium supplementation solved my sleep issues. I have seen many doctors, including sleep specialists, regarding insomnia. They all pointed to one source as the reason for the sleep issues: stress. And they all wanted to put me on prescription sleeping pills. I said no to that. Sleeping pills can cause dependence, and they often treat the symptom rather than the underlying cause. As a software developer, I am used…
Your body isn't a piece of software.
It can get easily get stuck in conditions that we aren't used to happening in computers. Eg, some injury causes inflammation and pain in a different part of your body, and even after the initial injury heals itself, the inflammation and pain in another part of your body continues of its own accord, and a "quick fix" of some NSAIDs can cure it within hours.
I'm glad you are feeling better, but reading all that great stuff about magnesium could have primed you to think that supplementing it would work, therefore it worked. The placebo (and nocebo) effects are very powerful.