Consider a spring with one end fixed and the other end which can move in the $\hat x$ direction.
When the spring is at its natural length the free end of the spring is at position $0\hat x$.
If the spring is extended the $x >0$ and if the spring is compressed then $x<0$.
The force that the free end of the spring exerts on a body attached to it is $\vec F = - k \vec x =- k x \hat x$ where $k$ is the spring constant.
If $x>0$ then the force that the extended spring exerts on the block is in the $-\hat x$ direction and if $x<0$ thn the force that the compressed spring exerts on the block is in the $\hat i$ direction.
Suppose that the end of the spring starts at position $x \hat x$ and finishes at position $0\hat i$ then the work done by the spring is
$$\int_x^0 \vec F \cdot d\vec x = \int_x^0 (-kx\hat x) \cdot (dx \hat x)= \int_x^0 -kx\, dx = \frac 12 kx^2$$
Now I have done it this way without making any assumption as to whether the spring is extended or compressed ie whether $x$ is positive or negative.
You will note that if $x=-a$ the work done by the spring is positive because $(-a)^2$ is positive and in your example the elastic potential energy of the spring, $\dfrac12 k a^2$, is converted into an increase in the kinetic energy of the block by that amount.
If the block started from rest it would stop again when $x=a$, the elastic potential energy of the spring again being $\dfrac12 k a^2$, and then the block would retrace its steps undergoing simple harmonic motion in the process.