First off, if there are enemy units in structure you will not see a unit icon on the map, but rather a wimple on the flag pole of the structure.
See the flagpole on New Orleans in the illustration below.
As soon as you have any unit in the same region as enemy units in a structure, you are besieging them and a siege icon will appear the next turn in that region.
See the Iberiaville region in the illustration below.
If other enemy units are in the same region as the besieged structure I believe that their artillery is applied to the breach-check each turn. The more artillery that the besieged units have compared to the besieging units, the less likely that a breach will occur and the more likely that a breach will be repaired.
If you hover your mouse pointer over the siege icon you will get a tool-tip telling you how many breaches have been made in that structure and if the structure still affords protection. If the structure as been completely breached and affords no protection you may continue the siege, but the only results will be occasionally getting hits on the besieged unit(s).
To attack units in a besieged structure -- whether it is intact or completely breached -- you must set your units to Assault Posture. Assault Posture is no different than Offensive Posture other than that units in AP will ignore that the defending units are in a structure. Units in Offensive Posture will not attack units inside a structure, but will attack units outside the structure. If you attack units outside of a structure it can cause units inside the structure to become involved in the battle, but I believe that units inside the structure will still enjoy the advantages of being inside the structure.
So in the battle of Trappahannock, if the Union had units outside the town, with Lee in Offensive Posture he would attack them, even if those units were leaving the town and trying to escape. If this happens a general engagement can -- often will -- occur involving up to all units in the region, also those inside the besieged structure. This is what I believe happened in that battle.
As far as the Rules of Engagement (RoE) go, I would not set to All-Out-Assault vs units entrenched in a structure unless I had overwhelming odds, at least 5-1 and more likely 10-1, otherwise it will be extremely costly on your losses. If I were attaching Richmond, I might consider it, but attacking Trappahannock, no way. If I were intending to assault, I would use normal assault RoE to lessen my casualties.
--------------
A word about how I believe the Trappahannock battle occurred and this gamey tactic. Since it is know to many and I am against secret rules I'll explain this further. If there are units of opposing sides deadlocked in a region -- both in Defensive Posture -- no engagement will occur as long as nobody attacks. However even if only the smallest unit on either side changes to Offensive Posture or a moving unit not even on Offensive Posture is intercepted by defending units, a general engagement can occur.
In the illustration above, this is what happened and how Johnston was repulsed and retreated across the Mississippi. Both the Union and CSA were close to equal in size and strength in Iberiaville. I, the Union player, received a single militia unit -- 1st LA Chasseur d'Afrique -- which was still building. I didn't want to put this unit into my army stack, because it would have caused it to be under-commanded, so I left it outside.
The next turn the 1st LA completed training and for reasons unsure to me attacked Johnston, which caused Johnston to start a general engagement. Since my entire army was well entrenched and in Defensive Posture and Johnston was at corp size outside of New Orleans the battle that ensued had Rosencrans' 51,690 vs Johnstons 13,015 men. The results was a slaughter and since it occurred through a strange quark that was completely unintentional and seemed very gamey to us, we backed up to the previous turn and I put the 1st LA into the army to prevent this gamey battle from occurring.
The thing is, that this reactionary type of battle can be provoked by one side sacrificing a single regiment to attack the other side and provoke a battle, even if the attacking regiment is immediately destroyed -- the battle report showed that this is what happened to the 1st LA -- the battle still develops into a full engagement.
I've also seen this happen when sending a single cavalry regiment out of such a region in Defensive Posture with avoid engagement in effect. The cavalry regiment got caught and the same thing happened. Admittedly, this it is very rare that such a cavalry unit would be caught, but it can happen with spectacular results.
But I find this to be a very gamey tactic to provoke a battle with a single unit like that and it's not something I'd do intentionally and were somebody to do that to me in a PBEM game I'd call foul.