I opened a tdebase issue:
Kate seems unable to consistently restore syntax highlighting selections:
https://mirror.git.trinitydesktop.org/gitea/TDE/tdebase/issues/626
I'm not skilled at reading complex C++, but I believe I found two causes for the bug. One is a change in the file's MD5 sum. The other is an incorrect expiration date calculation.
I believe one reason the bug seems random and inconsistent is that most of the time the Kate file type detection algorithm is correct. Although the related meta data is being deleted, most users don't notice because the detection is correct, which is mostly all that people care about. But the meta data likely is being deleted often.