I'm thinking about path integrals with the Euclidean time formalism, where I have partition function $Z=\operatorname{Tr} e^{-\beta \hat H}$. I'm used to the following derivation of the path integral:
\begin{align*} Z&=\int_{\Omega} \langle x_0|e^{-\beta \hat H}|x_0\rangle dx_0\\ &=\iiint_{\Omega^N} dx_0\cdots dx_{N-1} \langle x_0|e^{-\Delta\tau\hat H}|x_1\rangle\langle x_1| (\ldots) |x_{N-1}\rangle\langle x_{N-1}|e^{-\Delta \tau \hat H}|x_0\rangle \\ &=\vdots\\ &=\int [Dx] e^{-\int_0^\beta Ld\tau} \\ \end{align*}
This relies on the repeated application of the identity ${\bf 1}=\int_{\Omega}|x\rangle\langle x|dx$ ($\Omega$ being the volume in question), and the trace enforces periodicity in Euclidean time $x(\tau)=x(0)$. My question is: how can I save this derivation if I put periodic boundary condition on space?
For concreteness I consider a particle in a box of size $2\pi$. If the box has Dirichlet boundary conditions, the proper basis of eigenstates is $\psi_n^d(\theta)=\frac{1}{\sqrt{\pi}}\sin(\frac{n\theta}{2})$, the orthogonality relation $$\int_0^{2\pi}\psi_n^d (\theta)^*\psi_m^d(\theta)d\theta=\delta_{nm},$$ and the completeness relation $$\sum_{n=1}^\infty \frac{1}{\pi}\sin(\frac{n \theta}{2})\sin(\frac{n \theta'}{2})=\delta(\theta-\theta').$$ Each time we insert the identity or take the trace, we're really using this resolution. We should get the following partition function, where paths are constrained to have $\theta(\tau)\in [0,2\pi]$: \begin{align*} Z&=\sum_{n=1}^\infty e^{-\beta \frac{\hbar^2}{2m}\left(\frac{n}{2}\right)^2}\\ &=\int [D\theta] e^{-\int_0^\beta Ld\tau} \end{align*}
If we have periodic boundary conditions, we have $\psi^p_k(\theta)=\frac{1}{\sqrt{2\pi}}e^{ik\theta}$ for any integer $k$, and completeness relation $$\sum_{k\in\mathbb{Z}} \frac{1}{2\pi}e^{ik(\theta-\theta')}=\delta(\theta-\theta').$$ We should get the following path integral which includes a sum over winding numbers and allows $\theta(\tau)$ to step outside the region $[0,2\pi]$.
\begin{align*} Z&=1+\sum_{k=1}^\infty 2e^{-\beta \frac{\hbar^2}{2m}k^2}\\ &=\int [D\theta] e^{-\int_0^\beta Ld\tau} \end{align*}
In the second case, how can I derive this? It seems like the key must be in the two different resolutions of the identity.
I don't have a problem with the intuition, just the mathematics of resolving the identity with these two different boundary conditions.