fix(orchestrator): match longest active prefix in interleave_rollout#2634
Merged
Conversation
ca38614 to
b62f65e
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
interleave_rollout's prefix-matching loop to pick the longest matching active prefix instead of the first one (break-on-first).Targeted at #2632 (
r3-delta).Bug
The loop at
src/prime_rl/orchestrator/trajectories.pywalkedactive_samplesin creation order and took the first prefix thatstep_prompt_idsstarted with. The slice checkstep_prompt_ids[:len(P)] == Pis not mutually exclusive across active samples: if one active prefix is a strict prefix of another, both satisfy the check and the loop returns the older/shorter one.This is reachable from a compaction/rollback turn whose prompt is shorter than an existing sample's prefix and whose completion re-generates the same tokens and then keeps going. Walking through it:
prompt=[1,2],completion=[3,4]-> sample A,P_A=[1,2,3,4]prompt=[1,2,3,4,5],completion=[6]->P_A=[1,2,3,4,5,6]prompt=[1,2],completion=[3,4,5,6,7]. The prompt is shorter thanP_Aso the slice check is false-by-length -> new sample B,P_B=[1,2,3,4,5,6,7]. NoteP_Bstarts withP_Aby construction.prompt=[1,2,3,4,5,6,7,8],completion=[9].P_A=[1,2,3,4,5,6]first:step_prompt[:6] == P_A-> match,break. Wrong —P_Bis the actual extension.When the bug triggers,
extend_sampleappends the leftover tokens (B's generated tokens[7]plus step 3's new prompt[8]) to A'scompletion_idswithcompletion_mask=Falseandcompletion_logprobs=0.0. Token7was generated by the model in step 2 with a real logprob, but it ends up filed in A as user input. B is left stale and never receives step 3.This is a silent violation of the Exact-Prefix invariant documented in
docs/trajectories.md— the very class of corruption interleaving is supposed to refuse by starting a new sample.Fix
Track the longest matching prefix instead of breaking on the first match. Longest-match is strictly better:
Example warning produced by the regression test:
Verification
test_interleave_rollout_picks_longest_matching_prefix) constructs the 4-step trajectory above and asserts:[3, 4, 5, 6]with mask[T, T, F, T](steps 0+1 only),[3, 4, 5, 6, 7, 8, 9]with mask[T, T, T, T, T, F, T](steps 2+3 merged, token 7 stays a generated token).r3-delta: sample A gets[3, 4, 5, 6, 7, 8, ...](B's generated7folded in as user input).uv run pytest tests/unit/orchestrator/test_trajectories.py -q: 22 passed.uv run ruff check src/prime_rl/orchestrator/trajectories.py tests/unit/orchestrator/test_trajectories.py: clean.Note
Medium Risk
Changes how rollout steps are merged into training samples on the orchestrator path; wrong behavior previously corrupted masks/logprobs silently, but impact is limited to rare multi-prefix cases.
Overview
Fixes
interleave_rolloutso when a trajectory step matches more than one active token prefix, it extends the longest match instead of stopping at the first. That matters after compaction/rollback, where one sample’s prefix can be a strict prefix of another’s—first-match could merge later steps into the wrong sample and treat regenerated tokens as non-trainable prompt tokens.When multiple prefixes match, the orchestrator now logs a warning (example id, step, matching lengths) so ambiguous cases are visible in production rollouts. A regression test covers the rollback + overlapping-prefix scenario and asserts correct
completion_ids, masks, and logprobs per sample.Reviewed by Cursor Bugbot for commit b62f65e. Bugbot is set up for automated code reviews on this repo. Configure here.