Does your dog know it’s doing something “wrong”, or something it was taught not to??

*more of a discussion rather than question. More so wanting to hear thoughts/opinions. I got a puppy a few months ago and she already knows limitations and boundaries. For example, the gates/doors on both sides of my house are bars. She’s so tiny she can fit through them so I teach her not to go to those areas. So I let her go outside to do her business because I was on a call (work from home). However, I could see her from the window. I saw her go to the gate and literally look back first to see if I was coming before she decided to slip through. It honestly caught me off guard….and made me wonder what things do puppies/dogs do that aren’t as impulsive as we think…but are actually thought about. She’s a puppy and I’m usually out with her so she doesn’t get the chance to get away. I realize I gave her that opportunity by not supervising her and grabbed her from the front yard. But has anyone experienced something similar to this with an older dog, a dog with more training etc. In those cases, did you have expectations that your dog was capable of following the rule even if you weren’t present? How did you handle it or correct the situation on your end? your dogs end? Or did you have to change your expectations?