@oceanic-panic-panic bold of you to assume that Perry doesn’t let himself get trapped on purpose at least 2/3 of the time. Perry always escapes the traps. And he always waits for Doofenshmirtz to finish monologuing before escaping from them. It’s part of their routine, something they both expect: Perry bursts in, gets trapped, patiently listens to doof’s rant of the day, escapes the trap, fights his nemesis, and blows up the Inator. Whenever this routine is broken, they work to maintain it anyway - I can think of at least like 3 instances where Perry purposefully, politely traps himself, and several more where Doofenshmirtz gets impatient and sets Perry free from the trap himself so they can fight.
Perry getting trapped isn’t a sign of some intellectual folley - it’s a sign of his incredible problem-solving skills that he can escape them so easily, and a sign of his social intelligence that he knows to politely wait until his nemesis is ready to stop venting and start fighting.
But why does Perry need to be trapped for his nemesis to vent? Easy. Doofenshmirtz is a victim of severe child abuse, and needs to feel as though he’s somewhat in control of the situation before allowing himself to be vulnerable. Perry being trapped makes him feel safe, and Perry catches on to this. It’s an intricate social dance that none of his coworkers have mastered, the ability to communicate with and accomidate for a villain with special needs. And though it takes the whole summer, they eventually don’t even need the traps, because Perry makes Heinz feel safe.