This is not a simple problem. The centre of mass of the system will accelerate with aCM=F/(m1+m2) and the two blocks oscillate with respect to the CM. Have you learned about two-body problems?
You can set-up the equation of motion for both blocks, and express their position coordinate with the position of the CM and the length of the spring. If x1 is the coordinate of block1 and x2 is the coordinate of block2, then the the position of CM is Xcm=(m1x1+m2x2)/(m1+m2), and the length of the spring is L=x2-x1.
The CM moves with acceleration acm=F/(m1+m2)
Write out Newton's second law for both x1 and x2. Express x2 and x1 in terms of Xcm and L. You get a second-order differential equation for L, similar the one when a mass is hanged on a spring. The mass will oscillate around an equilibrium position, determined by gravity and the spring constant. At this equilibrium position the net force is zero. If the spring was unstretched initially and then released, the amplitude of the vibration is equal to the difference between the equilibrium length and the unstretched length. So the length is maximum when it is stretched by two amplitudes.
The situation is similar here: the constant force plays the role of gravity. Find the length L of the spring when the acceleration of the two blocks are equal. Calculate L-L0, and add to L: that will be the maximum length of the spring.
ehild