The rule says "If during an assault one player suffers more casualties than he inflicts (morale changes don't count, only step losses), all his units and leaders must exit the cave hex completely just like normally exiting an assault hex, receiving the "free shot" as they exit (
12.12). No unit may dig in while occupying a cave."
Okay, the procedure is easy enough. The question is "when?" Does it happen immediately after the assault? It seems that would be the only logical time, and wanted to make sure that was correct.