It's fairly straightforward to write a function that detects looping output -- I wrote one for promptfoo in [1].
For training you would need to hook it up to a LoRA training harness, identify the looping weight direction, and adjust the weights accordingly. I've not (yet) looked into how to do this.