Agreed, this needs to be fixed.  One simple fix would be to compute the loss for iteration i during iteration i+1.  To do so, we'd need to pass the gradient function both the current and previous weights.