Question on Control Group Assignment Lua

Background

Hoping what I need isn't locked away in the engine: can anyone help me find the logic for factories assigning control groups to the units they build? I've gotten a rough handle on the hotkey assignment (controlgroups.lua) and selection (selection.lua), but am missing that last piece.

If anyone can point me in the direction of the factory assist scripting that would also be neat.