According to Grammarly's blog, you should use a comma with non-restrictive phrases:
Jeff’s new car, which is less than a month old, already started leaking oil.
And leave it out with prepositional phrases:
The platform on which we built our program is very stable.
Here's my confusion:
He forgot the present he had bought for Mary, which meant that he had to go back home a second time.
I have no idea what the bolded past is. A non-restrictive phrase? A prepositional phrase? Should there be a comma before it?