Reusing saved tokens after an added system message: only when the prompt's start did not move

NVIDIA-NeMo/RL #5138 · depends on Gym #5771 · permalinks pinned to head 439d37b0

Before
When an agent added a system message after the model's answer (Claude Code does this for token-budget updates), Gym refused to link the call to the earlier one. Every such call started over as a new root, so its saved tokens were never reused.
This PR
Turns on Gym's allow_system_suffix for forest training on native vLLM, and adds an RL check: reuse the saved tokens only if the full prompt starts with the history exactly as rendered on its own. Otherwise, start a new root, quietly.
Status
The reuse check is correct: it never reuses tokens it should not. Open: it waits for a Gym bump that has Gym #5771 (C1, today bot PR #5432). The review also asks that a template which moves or drops the added system message be rejected with a clear error in both modes (C4, C5), not quietly rooted.

Dotted words have a short explanation: hover or tap them.

Background. In multi-turn training, RL keeps the saved tokens of each model call. On the next call, the vLLM chat server renders the whole chat with the template, then splices the saved tokens over the history part, so training sees exactly what the model saw. The splice finds the cut by counting end-of-turn tokens, not by position. A call that cannot be spliced safely becomes a new root.

Some chat templates move every system message to the top of the prompt. If a system message arrives after the answer, the template renders it before the history. Counting end-of-turn tokens then cuts in the wrong place: the system message is cut away and the history appears twice. The PR's fix is one comparison in _render_chat_call: does the full render start with the history render? Only decide_capture_input reads the answer, and only when the new messages include a system message.

One call, two chat templates · each word below is one token BUDGET = the system message the agent added after the answer · END = end of turn · ▶ = the "assistant:" prompt saved tokens, exactly as the model made them: [TASK, ans, wer, END] the same history, rendered again: [TASK, answer, END] The model wrote its reply as two tokens ("ans" "wer"); the template writes the same text as one ("answer"). The splice swaps the saved tokens in, so training sees what the model really produced. A · template keeps the system message at the end full render [TASK, answer, END, BUDGET, END, ▶] check: does it start with the history? [TASK, answer, END] … yes → reuse swap the saved tokens in: [TASK, ans, wer, END, BUDGET, END, ▶] BUDGET kept, saved tokens reused Before this PR (forest mode): Gym refused this call, so it became a new root with no reuse. Gym sink.py L423-L428 (the gate) B · template moves the system message to the top (DeepSeek-R1-Distill does this; Nemotron-Nano-v2 drops it instead) full render [BUDGET, END, TASK, answer, END, ▶] non-forest today: no check (C4) cut after the 1st END, saved tokens first: [TASK, ans, wer, END, TASK, answer, END, ▶] BUDGET is gone, history appears twice forest, this PR: silent new root (C5) [BUDGET, END, TASK, answer, END, ▶] Matches what the model saw, but nobody is told the template moved the harness's message, and every budget update starts a new segment. review asks, both modes (proposed, not in PR) reject the call: HTTP 400 code system_message_reordered "the chat template drops or moves a system message sent after the last answer"

Lane A and the red box use the PR's own test, test_system_suffix_checks_rendered_prefix_before_capture, with token ids written as words. The review ran the red box's case on this PR and on its base, and both give exactly that prompt. The green dashed box is what the review asks for; it is not in the PR.

The change, as pseudocode

Render step, before (base 9a7640c)

# vLLM chat server, once per chat call (event loop); Dynamo's render pool runs the same method
def _render_chat_call(request, messages, admission):
    full = render(messages)
    if managed_capture:
        decision = resolve_capture_input(admission, messages)  # Gym never sent a system suffix
    if decision.serving.mode == "token_in":                       # only calls that will splice
        history = render(messages[:last_answer + 1])
    return full, history, decision

Render step, after

# same place, same thread
def _render_chat_call(request, messages, admission):
    full = render(messages)
    if admission.mode in ("candidate", "token_in"):            # moved up: now before the decision
        history = render(messages[:last_answer + 1])
    if managed_capture:
        stable = bool(history) and full[:len(history)] == history  # the new check
        decision = resolve_capture_input(admission, messages, stable)
    if decision.serving.mode == "token_in": history = render(...)
    return full, history, decision

Decision, after (new lines only)

# called from the render step above
def decide_capture_input(admission, messages, rendered_prefix_is_stable=False):
    ...                                                   # text mode, missing proof → root (unchanged)
    suffix_roles = roles of the new messages
    if "system" in suffix_roles and not rendered_prefix_is_stable:
        warn("rewrites the rendered prefix"); return root  # root keeps any checkpoint cut
    ...                                                   # boundary check, then token_in (unchanged)

Gym startup config, after

# Gym actor startup (one Ray actor per Gym stack)
def _spinup():
    token_id_capture = {..., parent_resolution: "source_history" if forest else "fingerprint"}
    if forest_training and backend == "vllm_worker":          # the only pair Gym #5771 accepts
        token_id_capture["allow_system_suffix"] = True     # C1: the pinned Gym rejects this key

Real code: _render_chat_call, decide_capture_input / resolve_capture_input, _spinup.

What the review checked, and what it asks for

The check matches what Gym #5771 asks of the serving side (Gym doc). Gym admits a call when every new message is a user, tool or system message. RL runs the check whenever a system message is among them, in any order. If the first len(history) tokens are unchanged, a moved system message would have to sit after the history, so the splice cannot cut it away. A template that renders the last answer differently once something follows it (Qwen3 with empty reasoning adds an empty think block only when the answer is last) fails the check and starts a new root. That is safe, and no worse than before.

The asks are about landing, tests and docs, plus what to do when the template and the harness disagree:

🔴 C1 — nemo_gym.py:1429 C2 — nemo_gym.py:1433 C3 — context-compaction.md:100 C4 — chat_server.py:262 C5 — capture_context.py:71

So what. With this PR, Claude Code's budget updates keep reusing saved tokens whenever the template leaves the system message where the harness put it. When a template moves or drops it, the template and the harness disagree. The review asks RL to say so with a clear error in both modes, instead of quietly starting a new root (forest) or serving a broken prompt (non-forest today). The one merge blocker is still the Gym bump.