Fix off-by-one in is_partial_stop causing false positives#3848
Open
Chessing234 wants to merge 1 commit intolm-sys:mainfrom
Open
Fix off-by-one in is_partial_stop causing false positives#3848Chessing234 wants to merge 1 commit intolm-sys:mainfrom
Chessing234 wants to merge 1 commit intolm-sys:mainfrom
Conversation
The range started at 0, so on the first iteration output[-0:] evaluates to the entire string (Python treats -0 as 0). This made stop_str.startswith(output) return True whenever the full output happened to be a prefix of the stop string, incorrectly suppressing streaming chunks. The exclusive upper bound also skipped the longest valid suffix check. Fix: start the range at 1 and add 1 to the upper bound. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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
is_partial_stopchecks whether the tail of the streaming output is a partial match for a stop string. The loop rangerange(0, min(len(output), len(stop_str)))has two bugs:i=0on first iteration:output[-0:]in Python evaluates tooutput[0:]— the entire string, not an empty suffix. Sostop_str.startswith(output)is tested, returningTruewhenever the full output happens to be a prefix of the stop string. This incorrectly suppresses streaming chunks.Exclusive upper bound: The largest valid suffix (of length
min(len(output), len(stop_str))) is never checked.Fix: Change
range(0, ...)torange(1, ... + 1)so the loop checks suffixes of length 1 throughmin(len(output), len(stop_str)).You can verify in a Python REPL:
Test plan
output[-0:]returns full string in Python REPLtests/test_utils.pyshould continue to pass🤖 Generated with Claude Code