Lagrangian variational principle in stochastic mechanics: Gauge structure and stability