I am trying to apply Ito's formula to $$P(t, X_t) = \mathbb{E}^\mathbb{Q}\left[\exp\left(-\int_t^{T}r(X_s)ds\right)\middle|X_t\right]$$ where $T$ is a fixed constant and $$ dX_t = \mu\left(X_t\right)dt + \sigma(X_t)dZ_t.$$ I'm only interested in obtaining the term in front of dt which I understand should be $r(X_t)P(t,X_t)$ but I do not see how?
If you apply Ito's formula you get \begin{equation} \tag{1} dP = r(X_t)P(t,X_t)dt +\ldots dX_t + \frac12\frac{\partial^2P}{\partial X_t^2}\sigma^2dt \end{equation} where I'm not sure what should be in the dots, presumably it must be $0$ otherwise the result would not be correct but I do not see this. Also can anyone provide a justification for applying Ito under the expectation?
[For context this appears in Duffie-Kan 1996 in the derivation for equation (3.5) https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&ved=2ahUKEwjfo_Oqg5b5AhVxm1wKHf3FByoQFnoECAgQAQ&url=https%3A%2F%2Fwww.darrellduffie.com%2Fuploads%2Fpubs%2FDuffieKan1996.pdf&usg=AOvVaw2eP5RVrNc3m5LJ7rBiT44c]
I think you are missing the key assumption: we are considering only affine models, i.e. models that are of the form $f(x,t,T) = \exp{\{A(t,T) + B(t,T) x\}}$.
Here we are saying that the ZCB has this form, hence: $P(t,T)= \exp{\{A(T-t) + B(T-t) X_t\}}$ (typically $X_t = r_t$ is the (stochastic) interest rate).
So you just need to apply Ito to this process and not the definition of P as expectation of discounted payoff.
Edit [proof of $\mathcal{D}F-R(X_t)F = 0$]:
We can use Feynman-Kac representation formula. Without all the details we have that for a function $u(x,t)$ following a PDE like BS and with terminal condition $u(x,T) = \psi(x)$ the solution is given as:
$$ u(x,t) = \mathbb{E}_{t,x}^{\mathbb{Q}}\bigg[ \int_{t}^{T} e^{-\int_{t}^{s}R(X_s,s)ds } f(X_s,s)ds + e^{-\int_{t}^{T}R(X_s,s)ds }\psi(X_T)\bigg] $$
In our case $\psi(X_T) = 1, u=P(t,T)$ and $f(x,t)=0$. We just need to reason in the opposite direction: we know the conditional expectation then we know the PDE followd by the function is:
$$ \mathcal{D}P(t,T) - R(X_t)P(t,T) = 0 $$
where $\mathcal{D}$ is the Kolmogorov infinitesimal generator (the one you computed previously).