You wouldn't say it to a friend
The decision
Here's the rule I'm suggesting.
When I've got something wrong, I talk to myself the way I'd talk to a friend who'd done the same. I say what happened and what to do about it, and I leave my character out of it.
It has seven parts.
Catch the remark. Notice the commentary when it starts, and name it as the commentary. In the first weeks you'll catch perhaps one remark in five, usually late. That's fine. The proportion rises with practice.
Run the friend test. Ask what you'd say to a friend who'd done this, and say it to yourself, by name if that helps.
Keep the correction. State plainly what went wrong, in specific terms and without always or never. Decide the next step. If the mistake is the kind that recurs, decide what you'll change. This is the part that serves your standards, and the rule keeps all of it.
Repair what needs repairing. If someone else was affected, acknowledge it to them, apologise if an apology is due, and put right what you can. It's harder than calling yourself names, and it's the part that does some good.
Then stop. Once you've corrected and repaired, the matter is closed. If the commentary opens it again at midnight, the answer is "That's been dealt with." One mistake gets one hearing.
Stop the out-loud version first. "I'm so stupid" and "I'm useless", said aloud, are the easiest remarks to catch, because you can hear them. Replace them with something accurate, such as "I forgot that" or "That was a mistake." If there are children in the house, this matters for their sake as well as yours.
Don't attack yourself for attacking yourself. You'll forget the rule, and catch yourself halfway through a savage review of something trivial. The commentary will be pleased to add this to the charge sheet. "You can't even be kind to yourself properly." Apply the rule to that too. A friend would say, "You've been doing this for thirty years, of course it's still happening. You noticed. That's the skill."
Plans of the "when this happens, then I'll do that" kind make a decision much more likely to survive the speed at which the commentary works. Peter Gollwitzer and Paschal Sheeran pooled 94 tests of such plans and found that they made people a good deal more likely to follow through. Here are three, which you can adapt.
When I hear myself say "you idiot", aloud or silently, then I'll say "that's the commentary" and ask what I'd say to a friend.
When I make a mistake at work, then I'll say what it was in one specific sentence and decide the next step before anything else.
When the review starts at night, then I'll tell myself "that's been dealt with", and if it hasn't, I'll write down one step for the morning.
Here's what to expect, in my estimate. For the first fortnight, mostly you'll notice. You'll be dismayed by how often it happens and how vicious it is. That's uncomfortable, but it's progress, because you couldn't hear it before. The friendly replies will feel wooden and unconvincing. Over the next month or two they'll come more easily, and you'll find that the attack loses some force once it's been named. The commentary won't vanish. A habit that's been rehearsed for thirty years gets quieter and you believe it less, and it still speaks up when you're tired or under strain. What changes is that it becomes one voice among others, and you stop taking its word. I can't promise you that your mood will lift, though the research suggests it may. If it doesn't, or if things get worse, go back to the cautions in the preface.