My sheep pen design seems to work for escape prevention. What seems to happen is a combination of the animals squeezing each other forcing them to piggyback, and server-side collision detection I think. When part of an animal gets "inside" the fence or wall block, it's raised above them all to avoid suffocation damage, which is why simply building taller doesn't work. With fences, they'll seem to be on top, but the game appears to keep track of which side they're on, so they usually don't escape when you're around. The problem appears to happen when the chunk is first loaded in, and the game has either lost track of which side of the fence it's on, or the fences load in after the animal, giving it a brief opportunity to cross the line to "outside".
What I've done is to build my pens with a stepped fence. The outer ring is a block with a fence on top, and then an inner ring that's just single-high fence. This way, they can pop up to the top all they like. They'll still mostly stay on their side of the line, but on the occasions where loading give them a chance to escape, they'll ALSO have to pull the same maneuver over the 2nd higher fence. I haven't seen it happen yet. My other trick is that with the gates, I've created an airlock system where 2 different pens feed to the same smaller entrance/exit pen, which has another gate leading to outside. In any case where the server delay allows sheep to escape into the exit pen, they'd still have to pull the same trick in the right place again to get completely out. So I won't call my system escape proof, just escape resistant. So far, I haven't even had one get into the exit pen unless it snuck in after me (if they jump up on the gate when you close it, they can freely cross the line either way just like we can when we close it with ourselves still within that block).