It doesn't appear that any one mission prompts the attack other than exploring the derelict Reaper to acquire the IFF. Because EDI needs time to safely integrate the IFF into the Normandy's systems the player character is allowed to continue on as usual until a side mission is completed in the meantime (such as Legion's loyalty mission which you acquire soon after recruiting him).
After that EDI should inform you that the IFF is integrated and any further use of the Starmap will prompt the Collector attack.
After obtaining the IFF in the derelict Reaper, 1 mission anywhere in the galaxy and finishing it and use the galaxy map will trigger the collector attack.
It's kinda tricky to say, because after I got the IFF I did Tail and Legion's loyalty missions, which were the last ones, before the attack. So it may or may not depend on the type of mission.
There is nothing tricky or random about it, you can do precisely two missions after the Reaper IFF before the Collector attack on the Normandy, if you activate Legion and do not talk to it immediately after. Activate it, go do another mission, go back and talk to Legion, do its loyalty mission, boom, Collectors attack. TiberiusYakushev 14:50, October 19, 2011 (UTC)