But this I also wrote in the program's comments, so it's probably not what you're worrying about. So AO means you sample the hemisphere above the intersection position. In order to find the hemisphere, you need the normal. Then you (may) form an orthonormal basis with the normal being the up direction. Then you sample the hemisphere. Rays that hit an occluder "darken" the resulting color.I build an orthonormal basis with the normal being an axis - the cosine_sample_hemisphere() function (which just happens to be already there) assumes that the hemisphere is above the x/z plane. PBRT guys do it similarly (I believe), but doing it in object space would also be ok.
Probably no news to you, just wasn't sure if I got your question right.