I had an AI opponent that walled off an area with barracks and houses but in the process trapped itself. Due to the terrain where the wooden wall was made up of short zig zags that were all too short for a gate. It had a huge army but they could not participate in the battle until I used a ram on the palisade wall. Then I got owned by the avalanche of angry units. I won in the end. It was clear that the AI could not detect that it was trapped and delete the wall.
A sally port might be a solution. A port one wooden wall unit wide with limited unit rate.
If this is a duplicate move it appropriately. I found nothing in a search but then I may have had the wrong terms.