Dynamical stability in scalar-tensor cosmology