Skip to content

fix(token-capture): preserve safe continuations after system updates - #5138

Merged
terrykong merged 3 commits into
mainfrom
aroshanghias/capture-system-suffix-20261109
Dec 4, 2026
Merged

terrykong merged 3 commits into
mainfrom
aroshanghias/capture-system-suffix-20261109

Conversation

@aroshanghias-nvd

@aroshanghias-nvd aroshanghias-nvd commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor
  • I, the PR author, have personally reviewed every line of this PR.

What and why

Claude Code can append system messages, such as token-budget updates, after an assistant response. A chat template may move or drop those instructions, and replacing its historical tokens with a captured prefix can silently change the model's context.

The shared native-vLLM/Dynamo chat-rendering path verifies late system instructions before reading a captured prefix or starting capture. It reuses the captured prefix only when the full render begins with the historical render. Benign history-rendering differences start a new segment with the complete prompt, preserving generation-cut metadata. A marker probe rejects dropped/reordered instructions or uninspectable prompt text with HTTP 400 and system_message_reordered, in both candidate and ordinary token-in capture.

Enable Gym's default-off allow_system_suffix gate only for native-vLLM forest capture; keep it disabled for Dynamo and non-forest configurations. The marker render is limited to captured calls with late system messages. It checks marker preservation/order relative to the preceding user/tool message, not arbitrary content-dependent template behavior.

Issues

Related: Gym #5771 and the Claude Code subagent example #5113.

Convergence / perf / functionality proof

Rebased onto main after #5432. All three commits are patch-identical to the previously qualified range; dependency pins, lockfiles, and submodules are unchanged relative to main.

On refreshed head 8423d417a with pinned Gym 91666f9, 292 focused CPU tests plus the new two-turn Messages capture integration test passed (1 installed-vLLM-specific test deselected). The current subagent recipe also passes schema/SingleController configuration validation. These refresh checks ran in the prepared environment; the container/source fingerprint warning was retained. They do not repeat the earlier GPU qualification below.

Earlier GPU qualification on 8770e7ab7:

  • Real Qwen3-0.6B, vLLM 0.33.0 HTTP and actual TQ: 6/6 cases passed. Both candidate and token-in preserved the late budget instruction. Token-in retained the exact captured prefix; candidate replay safely opened a new root for a benign template difference.
  • DeepSeek-R1-Distill-Qwen and Nemotron-Nano-v2 template overrides were rejected in both modes, before prefix reads/capture began. These were negative template inputs on Qwen weights, not qualification of those model weights.
  • Stock SingleController CC L1: 10 training steps and all 7 checks passed. Maximum TMPE 1.02494, maximum generation KL error 0.001055, positive reward, and 8–17 valid physical rows per step from 8 logical rollouts. BrowseComp over public Workplace Assistant data, Megatron learner, and real capture/finalization. The stock test disables W&B and exports TensorBoard metrics.
  • The earlier runtime paired Gym override contained #5771; main now includes it through ci: Bump Gym to 91666f9 #5432. That validation used Ray 2.58.0 / driver PyTorch 2.14.0+cu130, retaining the image's actor-environment fingerprint warning against newer source. No new 20-step or Dynamo GPU qualification is claimed.

Needs upstream changes

None. #5432 merged the Gym revision containing #5771. This PR adds no dependency or lockfile update.

@copy-pr-bot

copy-pr-bot Bot commented Nov 9, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@github-actions github-actions Bot added the Documentation Improvements or additions to documentation label Nov 9, 2026
@aroshanghias-nvd
aroshanghias-nvd force-pushed the aroshanghias/capture-system-suffix-20261109 branch from e927785 to 439d37b Compare November 20, 2026 18:51
@aroshanghias-nvd

Copy link
Copy Markdown
Contributor Author

Pushed 439d37b0ee9467d48a5c1f9c3da07a5ab0f31255.

Refreshed onto main's shared chat-rendering implementation. The prefix proof runs before any captured-prefix fetch, preserves the full prompt on a mismatch, and retains checkpoint generation-cut metadata. Automatic system-suffix opt-in remains limited to native vLLM staging; no additional harness contract or dependency changes are added.

Validation: 65 CPU tests passed (one installed-vLLM-specific test deselected), the focused capture-context typecheck passed, and scoped pre-commit checks passed. All nine new system-suffix cases ran, including checkpoint-cut preservation and both shared-render-only and native preprocessing paths. No fresh live-model or learner qualification is claimed.

Do not merge until RL main's Gym pin includes Gym #5771 (merge commit 461f7e6cdc1182c372c6e6637a0b6ee087cc1854). The current pin still precedes it; the pin and lockfile update should land upstream separately. No pin or lockfile changes are included here.

@hemildesai hemildesai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, the change looks correct to me. When a candidate has a system suffix, it reuses captured tokens only if the full render starts with the separately rendered history. In every other case it roots a new segment. Moving resolve_capture_input after the history render is the right order, and the Dynamo path picks up the same check through _render_chat_call. A rooted candidate with a generation_cut still never splices, because prepare_prompt splices only for token_in or an explicit required_prefix_token_ids.

I'm not approving yet, for two reasons:

  1. Merge order (inline): this PR breaks forest training on the current Gym pin, so the pin bump has to land first or in this PR.
  2. CI: only the lint and metadata checks have run on 439d37b so far, not the unit or functional tests.

Non-blocking: please extend test_spinup_passes_parent_resolution_to_gym in tests/unit/environments/test_nemo_gym_utils.py. It should assert that allow_system_suffix is True for forest training plus vllm, and that it is absent for non-forest training and for the Dynamo backend. Then the gate wiring in nemo_gym.py has a test, and it validates against the real Gym config once the pin moves.

@terrykong terrykong removed the Documentation Improvements or additions to documentation label Nov 28, 2026

@terrykong terrykong left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First: this PR conflicts with main in docs/guides/context-compaction.md, because main's #5047 changed the same line. Please rebase on main; C3's suggestion already uses main's wording.

Thanks for this. The check is small and in the right place: it runs in _render_chat_call, which both native vLLM and Dynamo use, and when the rendered prefix changes it falls back to a new root, so a system message that the template moves to the top can no longer be dropped. The team checked it against Gym #5771's contract and against real Qwen3 templates, and found no case where it reuses tokens it should not.

Asks:

  1. (C1) Blocking: rebase onto main once a Gym bump with #5771 lands (today #5432). At the current pin an existing nemo_gym unit test fails, and forest-training runs on vLLM fail at Gym startup.
  2. (C2) Add a test that checks when allow_system_suffix is set.
  3. (C3) Fix the last sentence of the context-compaction guide.
  4. (C4) For every captured call (forest and non-forest), when a system message follows the last answer, check where the template puts it, and reject the call with a 400 system_message_reordered if the template moves or drops it. In non-forest mode this is an older bug, not added by this PR, that we ask you to fix here.
  5. (C5) Reword the new-segment warning for the case left after C4, and add a line to the context-compaction guide that such calls are rejected.

C1 and C2 turn the two points from the earlier review on this PR into threads that can be resolved.

FYI, no action: some templates render the last assistant turn differently once something follows it (Qwen3 with empty reasoning adds an empty think block only when the assistant turn is last). There the check sees a different prefix and starts a new root. That is safe, and no worse than before this PR.

Visual explainer: https://terrykong.github.io/gh-pages-poc/terryk/pr-5138-system-suffix-check.html

Generated by Claude Code

Comment thread nemo_rl/environments/nemo_gym.py Resolved
Comment thread nemo_rl/environments/nemo_gym.py Resolved
Comment thread docs/guides/context-compaction.md Resolved
@aroshanghias-nvd
aroshanghias-nvd force-pushed the aroshanghias/capture-system-suffix-20261109 branch from 439d37b to 8770e7a Compare December 2, 2026 23:54
@svcnvidia-nemo-ci svcnvidia-nemo-ci added complexity:medium Moderate change. Set by Autobot PR classify (skills/pr-classify) convergence-risk:high Likely to change training results. Set by Autobot PR classify (skills/pr-classify) proof:functionality Needs proof that it works. Set by Autobot PR classify (skills/pr-classify) proof:convergence Needs convergence proof (W&B runs). Set by Autobot PR classify (skills/pr-classify) labels Dec 2, 2026
@aroshanghias-nvd
aroshanghias-nvd force-pushed the aroshanghias/capture-system-suffix-20261109 branch from 8770e7a to 8423d41 Compare December 4, 2026 02:15
@github-actions github-actions Bot added the CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) label Dec 4, 2026
@terrykong
terrykong enabled auto-merge (squash) December 4, 2026 02:58
@terrykong

Copy link
Copy Markdown
Collaborator

/ok to test 8423d41

@terrykong
terrykong merged commit f0e41ac into main Dec 4, 2026
103 of 106 checks passed
@terrykong
terrykong deleted the aroshanghias/capture-system-suffix-20261109 branch December 4, 2026 05:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) complexity:medium Moderate change. Set by Autobot PR classify (skills/pr-classify) convergence-risk:high Likely to change training results. Set by Autobot PR classify (skills/pr-classify) proof:convergence Needs convergence proof (W&B runs). Set by Autobot PR classify (skills/pr-classify) proof:functionality Needs proof that it works. Set by Autobot PR classify (skills/pr-classify)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants