Faced a contradiction while calculating the work done by an outside agent from two different ways I solved this problem in two ways.



Since $dV$ and $dy$ are given, we sum the work done on the hydro-static system and the change in the potential energy of the spring.
$$\delta W=-PdV+\frac12 k dy^2$$
The other way is to consider the force times displacement notion.
$$\delta W=-PdV+PAdy$$
In both of them $k$ is the spring constant and $A$ is the cylinder bottom area.
First: Is my solution logical and correct?
Second: In the first expression of $\delta W$, the order of the second term on the right hand side is 2, but for the rest it's 2. What's wrong? Shouldn't the second term vanish? Then the spring wouldn't compress at all. But this is not right as we know.
 A: The first expression is almost correct; it is correct for $y=0$ but not as we go further from equilibrium.
Let's ignore the piston for the rest of this answer, you know the $P~dV$ stuff, you're fine with that. The spring is confusing you. I will even take a step back as a sort of "refresher" on springs. 
We know that we have some spring with some spring constant $k$ and some equilibrium length $y_0$ and we place some mass $m$ upon it. We know that it compresses a little bit, to satisfy $$m \frac{d^2y}{dt^2} = -k (y -y_0) - m g.$$So you might think "okay, this is already at some $y\ne y_0$ when I do all of this other stuff with it, I probably have to add that into my potential energy." However, and this may surprise you if you haven't seen it before, we can just incorporate the $mg$ constant into the $y_0$ to find $y_0' = y_0 - mg/k,$ so that the force is just $-k(y - y_0')$ with no explicit $mg$ term. Then a suitable potential energy is $\frac12 k (y - y_0')^2.$
Control question: prove that this is also the potential energy you get, up to a constant, when you use the spring's $y_0$ potential energy explicitly but include $mgy$ for the potential energy of gravity.
So now you say, "okay, set $y_0' = 0$ by a serendipitous choice of coordinates!" and that is valid, now the potential energy is $\frac 12 k y^2.$ That is fine, too.
So now you say "okay, the work needed to compress this thing by an amount $\Delta y$ is $\frac12 k (\Delta y)^2,$" and that is correct, but only for compressing it an arbitrary amount around its equilibrium at $y=0$. It would not be adequate for what you're suggesting, which is integrating over $dy$ to find total work, because that is fundamentally a Riemann sum: 

We take a step of size $\delta y$ from $y=0$, find a work $\delta W_1,$ then we take a step of size $\delta y$ from $y = \delta y$, find a work $\delta W_2,$ [...] then we take a step of size $\delta y$ from $y = (n-1)\delta y,$ find a work $\delta W_n$, now the work done as $y$ varies from $0$ to $n~\delta y$, is approximately $\delta W_1 + \dots + \delta W_n,$ with the approximation getting better as $n$ goes to infinity and $\delta y$ commensurately goes to 0.

Notice that these must both agree at the end but the $dy^2$ approach is not the right way to get the above process going, because it only is correct for calculating $\delta W_1$ and then its assumption that you're starting from $y=0$ is invalid for all of the other terms.
Instead for the above process you will want to investigate, $$dU = U(y + dy) - U(y) = \frac12 k (y + dy)^2 - \frac 12 k y^2 = k~y~dy + \frac 12 ~k~dy^2.$$For small enough steps $dy$ you can neglect the second term and you simply have the first term which... (fanfare)... is precisely the work done against that spring force $F = -k y$.
A: The infintesimal work on a spring is still force times distance, so the expression should be $ky\delta{}y$.
A: Work is a frame-dependent entity.  As reckoned from the frame of reference of the cylinder, the work done by the piston on the gas is $-PdV$.  As reckoned from the laboratory frame of reference, the work done by the piston on the gas is $-PdV+PAdy$. If the process is carried out reversibly, PA=ky, and from the laboratory frame of reference, the work done by the piston on the gas is also equal to $-PdV+kydy$.  Furthermore, in the laboratory frame, there is also work being done on the gas at the base of the cylinder, which is displacing by dy.  This work is equal to $-PAdy$.  So the net work done on the gas by external agents as reckoned from either frame of reference is just $-PdV$.
A: if you sketch a free body diagram, the force acting on the spring equals to the force acting on the piston. So the work done by the external agent is the sum of the work done to compress the gas and the work done to compress the spring
$$\frac{F}{A}dV + Fdy$$
