Use of Helmholtz free energy in problem involving spring-gas subsystem in thermal contact with a reservoir I have come across a problem from "Biological Physics" by Philip Nelson (pg212) which involves finding the equilibrium position of a spring, $x_{eq}$, which is compressing an ideal gas whilst in contact with a heat reservoir of temperature, $T_{res}$. A diagram of the setup is shown below:

I believe that solving it involves the minimisation of the system's free energy $F$:
$$F = U - TS $$
$$dF = dU - TdS - SdT $$
which simplifies to 
$$ dF = -SdT -pdV$$
using $dU = TdS - pdV$.
Since the system is at thermal equilibrium with a head bath, $dT = 0$. Also, the pressure and volume can be written in terms of the spring extension and constant: $V = A(L-x_{1})$ and $pA = \frac{1}{2} kx^{2}$ (when at equilibrium) so that $dV = -Adx_{1}$ and $ dp = \frac{k}{A}x$.
However, setting the equilibrium condition $dF=0$ leaves me with 
$$ dF (=0) = dW = -pdV = \frac{1}{2}kx^{3}dx $$
which gives an extension of 0 as a (somewhat unhelpful) result.
A second attempt involved writing $dW$ as
$$ dW = fdx_{1} $$ where $f$ is the force on the compression plate so that 
$$ f(x_1) = \frac{1}{2}x_{1}^{2} - pA $$
with the solutions of $f(x_1) = 0$ giving the equilibrium position of the plate assuming the ideal gas law $pV = pA(L-x_{1}) = nRT$. I believe this involves solving
$$ x_{eq}^{3} - Lx_{eq}^{2} - \frac{2nRT}{k} = 0 $$
However, this solution appears to avoid all thermodynamic arguments, and just simplify to solving a simple mechanical problem assuming ideal gas behavior, which makes me believe I have make a mistake somewhere along the derivation.
Any advice with the exercise, and on understanding the application of free energies to thermodynamic problems would be much appreciated.
---------------------EDIT-----------------------
I realise that I erred on the penultimate equation ( $f(x_{1}) = kx_{1} - pA$), which then leads to the  equation $kx_{eq}(L - x_{eq}) = nRT$. This seems to suggest that the total internal energy of the system can be split into the energy of the spring, and the energy of the gas so that $$dU_{total} = dU_{gas}  +dU_{spring} = -pdV + kxdx$$ It appears that my mistake was to treatthe spring as a seperate entity, and forgetting its energy addition to the total internal energy of the system
 A: If $V_{total}$ is the total volume of the system, then in the final equilibrium state: $x_{eq}A+V_{gas}=V_{total}$. Since the gas is ideal and at temperature $T_{res}$, $V_{gas}=\frac{p_{gas}}{m_{gas}RT_{res}}$, where $p_{gas}=k(x_{eq}-x_0)/A$, where $x_0$ is the spring length when there is no load on it. You may now get $x_{eq}$.
A: If you wish to solve this via thermodynamic arguments, you are on the right track by minimizing the Helmholtz free energy. In your edit, you correctly identify the error of not accounting for the spring's compression in the total internal energy. So, you can start with
$$
U(S,V,x) = U_{\mathrm{gas}}+U_{\mathrm{spring}}
  = TS - pV + \frac{1}{2}k (x-x_0)^2
$$
where $x$ is the position of the wall, $x_0$ is the unstretched length of the spring, and $V(x)=A(L-x)$ is the gas volume. Variations in $U$ are described by
$$
dU = TdS - pdV + k(x - x_0)dx
$$
so that variations in the free energy $F(T,V,x) = U - TS$ are described by
$$
dF = -SdT - pdV + k(x-x_0)dx = [pA + k(x-x_0)] dx
$$
Minimizing $F$ with respect to the wall position $x$ then returns you to the same equation you arrived at from force-balance arguments.
