This needs to be commit.copy().  AnyObjectId can be a MutableObjectId, which can change after-the-fact.  copy() returns you an ObjectId which cannot be mutated.  If the input was already an immutable ObjectId, copy() just returns the same object.