Solution (source code)

= Solution

Let $A=(ct_A,f(t_A))$ on the rear rocket and $B=(ct_B,h(t_B))$ on the front rocket. The instantaneous inertial <rest frame> of the rear rocket at $A$ moves at $v=f'(t_A)$ relative to S. Transform the time difference of these two events:
$$
\Delta t'=\gamma\left[(t_B-t_A)-\frac v{c^2}(h(t_B)-f(t_A))\right].
$$
Setting $\Delta t'=0$ gives
$$
\boxed{c^2(t_B-t_A)=v[h(t_B)-f(t_A)].}
$$
The velocity belongs to the rear event $A$. It is not to be evaluated at laboratory time $t_B$, nor does the <simultaneity> condition mean $t_A=t_B$ in S. At nonzero velocity the front event on the same instantaneous-rest slice is at a different laboratory time.