It requires a very complex trigger system to detect what builder starts building the foundation of a building.
Firstly all active building foundation orders (building type, target location, ordered unit) must be kept track of. When the building construction starts all active foundation orders must be searched for the one corresponding to the building. This is done by checking owner of building and ordered units are the same, checking the foundation and building types match, and seeing if the target ordered location is approximately the same as the building location (non-pathing buildings can be offset +/- ~32 units). If multiple matches are found then the ordered unit closest to the foundation/building location can be assumed the responsible builder.
The accuracy of this technique is very good, but not perfect. If a player orders 2 separate workers to build the same building type at the same location and they arrive at approximately the same time then it may return the wrong builder. Seeing how specific this failure case is, and that usually it makes little difference, one can say the technique is reliable.