Mr. Murder by Dean Koontz plot twist. Are there other books that have done this? Open spoilers.

Open spoilers ahead.
.
.
.
.
.
.
.
.
.
.
.
One of the characters, Karl Clocker, seems like a typical goon that works for the bad guys and is sent to kill the hero and his family. It turns out, however, that he had joined up with the bad guys with the intent to betray them and help the good guys out when a moment of crisis arrives. He saves the lives of the main character and his family and helps them escape from the clutches of the evil company he works for. I know that there are a ton of stories about a cop going undercover, but this seemed somehow different. He wasn’t a cop or member of some other law enforcement agency, he was just someone who decided that he was going to join up with an outfit of evildoers and screw them over by helping out the good guys when an opportunity arose to do that. Are there other stories where this plot device is used? Feel free to post in a spoiler box, as any response would obviously spoil the plot of whatever book or movie you are posting about.

Do superheroes count as law enforcement? I know a couple of superhero books where new superheroes join villain organizations with the intent of turning the bad guys over to the authorities, usually after neutralizing some advantage of the villains that has prevented the authorities from taking action already.

I would count that as the same basic idea. Basically I’m interested in a situation where someone does it on there own rather than being sent by their boss.

The Shroud did this in the 1980’s, taking over the villainous team Night Shift, composed of C- List supervillains like Gypsy Moth, Digger, and Dansen Macabre.

The 2007 David Cronenberg movie Eastern Promises has a similar twist.

How do you distinguish this from a heel-face turn or Reverse Mole or similar trope? (TV Tropes links.)

Well, it sounds like this character was never actually a bad guy, so the former wouldn’t apply. The latter, or The Infiltration, sounds a lot closer.

“You saved our lives, mysterious stranger! Who are you, and where did you come from?”

“I…I don’t know! It’s as if I had been created for this very purpose!”

In John Scalzi’s “The Android’s Dream”, the alien muscle hired by the bad guys is on some alien version of rumspringa in which he is allowed to go out and do whatever morally questionable thing he wants, including devouring people alive. At a critical juncture in the book (and a fortunate one for the protagonists), he decides he’s done with his journey of discovery and is tired of hanging around with lowlives.

In the comic book New Mutants, the character of Sam “Cannonball” Guthrie is recruited as a guard by a supervillain. Sam’s not “infiltrating” them - he’s a little naive, and doesn’t realize that when his boss says, “Take care of that guy,” he means, “Kill him.” Once he figures out what sort of outfit he’s working for, he joins up with the heroes and helps them escape.

The Man Who Used The Universe was the story of an undercover cop who joined a crime syndicate, making his way up through the ranks before revealing what he’d been up to all along – you know, finally breaking cover to provide useful information and key evidence, getting lots of career criminals busted. Why, just look at our hero, up there, getting VIP treatment from the enthusiastic official shaking his hand with real gusto!

That’s all true except the first part: he’s a crook who made his way up through the ranks, gathering info and evidence until the day he called the authorities and said, hey, how’d you like to earn a promotion and a ton of good publicity? I just need you to start saying I’m an undercover cop who got assigned to do what I actually did on my own initiative; give me a medal, throw me a parade – whatever you would’ve done.

He then goes into the entertainment business.

There was a nonfiction book called Cop Without a Badge

It’s also pretty common in political/spy thrillers (tho not so much in crime thrillers) for a character to be “undercover” for personal ideological reasons.

James Bond in Licence to Kill.