\section{Counterfactual Edit Credit}
\label{sec:method}
\Cref{fig:counterfactual-credit} shows the artifact intervention and the two coefficient allocations on a shared reference rollout.
\input{../figures/latex_counterfactual_credit}

\subsection{From interaction turns to surviving edits}
A rollout $\tau_i$ contains assistant turns interleaved with tool observations and ends in a repository patch $P_i$. We record a repository tree after each turn and the paths changed since the previous tree. An \emph{edit block} is a maximal consecutive sequence of turns with nonempty path changes. The tree immediately before the block and the tree after its last turn define its net file effects.

A block is eligible if its net effect is nonzero, it touches no test paths under the rule in \cref{app:algorithm}, and none of its touched paths is modified by a later turn. ``Surviving'' refers to this last-touch condition: a block can also undo an earlier mistake and leave no difference from the task baseline on those paths. One eligible block is selected by the smallest SHA-256 hash of the rollout key and block index, without consulting verifier outcomes. An unavailable selection is retained rather than replaced. \Cref{app:algorithm} specifies snapshots, key fields, and token masks.

\subsection{A conditional final-artifact intervention}
Let $e_i$ be the selected block and $S(e_i)$ its touched paths. The intervention artifact $P_i^{-e_i}$ is constructed from the final tree by restoring each path in $S(e_i)$ to its pre-block contents, including its prior absence when the block created that path. All other final files remain fixed. The implementation constructs this tree directly and produces a patch against the task baseline. It does not use a fuzzy reverse-patch fallback.

\begin{samepage}
The original and intervention artifacts are assessed in matched task contexts. For a test report, define
\begin{equation}
 U(P)=Y(P)+\tfrac14 F(P)-\tfrac14 G(P),\qquad
 D_i=U(P_i)-U(P_i^{-e_i}).
 \label{eq:utility}
\end{equation}
Here $Y$ is the resolved verdict, $F$ is the fraction of a fixed, deduplicated fail-to-pass test list that passes, and $G$ is a binary indicator that any expected pass-to-pass test does not pass. Missing or skipped expected tests are non-passing in both lists. An empty expected fail-to-pass list, or a missing or unparseable report, makes the utility unavailable rather than zero. An empty expected pass-to-pass list gives $G=0$. Under these definitions, $U\in[-1/4,5/4]$ and $D_i\in[-3/2,3/2]$.
\end{samepage}

Only the intervention requires an additional counterfactual (CF) verifier trial: the original rollout already receives its ordinary final verification. Each query-eligible rollout issues at most one such trial, with no additional policy continuation. A failed query gives no auxiliary credit and remains in the availability denominator. The measured contrast fixes the other final files; it consequently describes the contribution of these file effects conditional on the completed artifact.

\subsection{Localized and broadcast coefficients}
Let $\mathcal G_i$ contain the valid generated-token positions in the flattened rollout, excluding observations and padding, and let $L_i=|\mathcal G_i|$. The selected block's producing turns define $\mathcal E_i\subseteq\mathcal G_i$, with $m_i=|\mathcal E_i|$. For a block spanning several turns, this is the union of those turns' generated spans. Every selected span must be present and complete after truncation; a missing, clipped, duplicated, or masked span makes the selection unavailable.

For an available query with $L_i,m_i>0$, coefficient scale $\lambda>0$, and token cap $c>0$, define
\begin{equation}
 s_i=\begin{cases}
 1,&D_i=0,\\
 \min\!\left(1,\dfrac{c\,m_i}{\lambda |D_i|L_i}\right),&D_i\ne0.
 \end{cases}
 \label{eq:shrink}
\end{equation}
The localized arm M and broadcast arm B use
\begin{align}
 C^M_{it}&=\lambda D_i s_i\,\frac{L_i}{m_i}\mathbf 1[t\in\mathcal E_i],
 & C^B_{it}&=\lambda D_i s_i\,\mathbf 1[t\in\mathcal G_i].
 \label{eq:coefficients}
\end{align}
When the cap is active, these coefficients simplify to $c\,\operatorname{sign}(D_i)$ on M's receiving tokens and $c\,\operatorname{sign}(D_i)m_i/L_i$ on B's. The magnitude of $D_i$ and the scale $\lambda$ then cancel: delivery depends on sign and support size. Both are zero when the selection or utility is unavailable, the trajectory is excluded from the policy loss, or there is no valid receiving token. Format-error trajectories receive no auxiliary credit. The terminal reward rule is shared across arms.

On a common reference rollout, summing \cref{eq:coefficients} gives
\begin{align}
 \sum_t C^M_{it}=\sum_t C^B_{it}&=\lambda D_i s_i L_i,
 &\sum_t |C^M_{it}|=\sum_t |C^B_{it}|&=\lambda |D_i|s_i L_i.
 \label{eq:mass}
\end{align}
These are identities of the raw auxiliary coefficients. For nonzero credit, their squared masses have ratio $\sum_t(C^M_{it})^2/\sum_t(C^B_{it})^2=L_i/m_i$. Thus mass matching controls the total signed and absolute signal without imposing the same concentration.

\subsection{Policy optimization and terminal edit focus}
For a prompt group of $n$ sampled rollouts, let $V$ index members retained by the common trajectory masks. The shared terminal group advantage is
\begin{equation}
 A_i=\begin{cases}
 r_i-\dfrac1{|V|}\sum_{j\in V}r_j,&i\in V,\\
 0,&i\notin V,
 \end{cases}\qquad
 \widetilde A_{it}=A_i+C_{it}.
 \label{eq:additive}
\end{equation}
The common terminal reward $r_i$ includes the format-error and pass-to-pass regression rules specified in \cref{sec:design}. An entirely excluded group contributes zero. Substituting the valid-member mean for excluded rewards before centering gives the same valid-member advantages. Group advantages are not divided by their standard deviation. We add the auxiliary tensor after group centering, leave terminal returns unchanged, and use Divergence Proximal Policy Optimization (DPPO) \citep{dppo2026}. For $N_g$ prompt groups, with $T_g$ valid generated tokens in group $g$, the policy surrogate is
\begin{equation}
 \mathcal L(\theta)=-\frac1{N_g}\sum_{g=1}^{N_g}\frac1{\max(1,T_g)}
 \sum_{i\in g}\sum_{t\in\mathcal G_i}
 \rho_{it}(\theta)\,q_{it}(\theta,\widetilde A_{it})\,\widetilde A_{it},
 \label{eq:loss}
\end{equation}
where $\rho_{it}$ is the current-to-behavior token likelihood ratio and $q_{it}$ is the stop-gradient binary-TV mask defined in \cref{app:algorithm}. This prompt-mean reduction averages tokens within each prompt group, then groups; it is not an equal-weight mean of per-rollout means. The combined advantage's sign can affect $q_{it}$, so \cref{eq:mass} does not guarantee equal realized gradient norms or masking rates.

The terminal-only arm O sets $C_{it}=0$. The terminal edit-focus arm E uses the same structural selection and receiving mask, but derives its auxiliary coefficient solely from the original terminal group advantage:
\begin{equation}
 C^E_{it}=\operatorname{clip}\!\left(\lambda A_iL_i/m_i,-c,c\right)
 \mathbf 1[t\in\mathcal E_i].
 \label{eq:terminal-focus}
\end{equation}
Unavailable selections give zero, and E issues no counterfactual query. Its term is additive rather than a relocation that removes the original terminal term. In particular, if all terminal group advantages are zero, E contributes no new signal, whereas M/B can still receive an available nonzero $D_i$.
