With time and effort, you can train yourself to move your thoughts elsewhere. Deliberately moving your attention to something in the present can help break your train of thought, and can help break the sort of obsessive cycle humans can get into.
Also, thoughts of past failure can be trigger by things that upset you in the present. So when your mind turns to the past, stop and try to figure out what might be upsetting you in the present.