\documentclass[11pt]{article} \usepackage{common} \begin{document} \notehead{0204}{Self-correction as a multi-turn MDP: masks and per-turn credit}{Batch M2. Companion notebook: \texttt{notebooks/0204-self-correction-multiturn-mdp.ipynb} (every episode and every group of sampled episodes enumerated exactly; fp64, executed).} \begin{problem} \question{Which tokens of a multi-turn transcript receive gradient, how is credit assigned across turns, and which baselines keep it unbiased?} A prover that sees the Lean compiler's error message and revises its proof can solve problems that independent resampling does not; Goedel-Prover-V2 runs up to two revision rounds after the first attempt and reports a gain of about two points at pass@32 over its non-revising mode \cite{gps}. To \emph{train} such a model with RL, three things must be pinned down: (i) which tokens of the long multi-turn transcript receive gradient, (ii) how credit is assigned across turns, and (iii) which baselines keep the gradient unbiased. Getting any of these wrong gives a run that logs a healthy loss while optimising something else. \end{problem} \stepcounter{section}% the problem box counts as section 1: keeps every \S and notebook cross-reference stable \section{Notation} \begin{center} \begin{tabular}{@{}p{0.36\linewidth}p{0.60\linewidth}@{}} \toprule Symbol & Meaning \\ \midrule $T$ & maximum number of turns (initial attempt plus revisions) \\ $y^t$ & the model's output at turn $t$ (a token sequence) \\ $f^t=\Psi(x,y^t)$ & environment feedback (verifier verdict plus compiler messages); $\theta$-independent \\ $c^t=(x,y^1,f^1,\dots,y^{t-1},f^{t-1})$ & context of turn $t$ \\ $r_t=V(x,y^t)$, $\ R(\tau)$ & per-turn success; episode return (e.g.\ $\sum_tr_t$ or $\max_tr_t$) \\ $s_t=\nabla_\theta\log\pi_\theta(y^t|c^t)$ & turn-$t$ score; sums over the tokens of $y^t$ only \\ $m_\ell\in\{0,1\}$ & loss mask: $1$ for policy-sampled tokens, $0$ for the prompt, feedback and template tokens \\ $G$ & episodes per prompt in a group; $\bar R=\frac1G\sum_jR_j$, $\bar R_{-i}=\frac1{G-1}\sum_{j\ne i}R_j$ \\ $Q(c^t)=\E[R_{\ge t}|c^t]$ & value of a context ($R_{\ge t}=\sum_{s\ge t}r_s$) \\ \bottomrule \end{tabular} \end{center} \section{Derivation} \subsection{The trajectory and what the policy controls} An episode is $\tau=(x,y^1,f^1,y^2,f^2,\dots,y^T)$, stopping early on the first success if the harness is configured so. Its probability is \[ P_\theta(\tau)=D(x)\ \prod_{t=1}^{T}\Bigl[\pi_\theta(y^t\mid c^t)\ \Psi(f^t\mid x,y^t)\Bigr],\qquad \pi_\theta(y^t|c^t)=\prod_{\ell}\pi_\theta(y^t_\ell\mid c^t,y^t_{<\ell}). \] The prompt distribution $D$ and the verifier $\Psi$ do not depend on $\theta$. Hence \begin{equation}\label{eq:mask} \nabla_\theta\log P_\theta(\tau)=\sum_{t=1}^{T}\sum_\ell\nabla_\theta\log\pi_\theta(y^t_\ell|\cdot), \end{equation} containing \emph{only} policy-sampled tokens. With the score-function identity of note 0201, $\nabla J=\E[R(\tau)\sum_ts_t]$. The mask $m_\ell$ is therefore not an implementation convenience; it is the statement that the environment factors drop out of (\ref{eq:mask}). Notebook: enumerating all 9 episodes of a two-turn tabular environment, $\E[R(s_1+s_2)]$ equals the autograd $\nabla J$ to $8\times10^{-17}$. \subsection{Unmasked feedback tokens bias the gradient} If the loss also scores the feedback tokens, the LM (which assigns them probability through the same output softmax) contributes $R\,\nabla_\theta\log\pi_\theta(f^t|\cdot)$. Since $f^t$ is \emph{not} drawn from $\pi_\theta$, the zero-mean identity (Eq.~(6) of note 0201) does not apply: $\E[\nabla\log\pi_\theta(f)]\ne0$, and the term is a reward-weighted \emph{supervised} signal to imitate compiler output. In the toy (feedback token tied to the turn-2 head) the spurious term has norm $0.68$ against $\|\nabla J\|=0.48$. In an LM the contamination reaches every shared parameter. \subsection{Per-turn credit: causality} Take $s