[Rust Frontend] Backtrack from safe text at a complete marker (#59563)
Signed-off-by: Bugen Zhao <i@bugenzhao.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
e6fc81bc78
commit
7436a7f119
@@ -378,9 +378,16 @@ fn safe_text_event(
|
|||||||
input: &mut JsonToolInput<'_>,
|
input: &mut JsonToolInput<'_>,
|
||||||
config: JsonToolCallConfig,
|
config: JsonToolCallConfig,
|
||||||
) -> ModalResult<JsonToolCallEvent> {
|
) -> ModalResult<JsonToolCallEvent> {
|
||||||
|
// Stop only where a tool call can start: with exact whitespace, a bare
|
||||||
|
// start marker without it is text.
|
||||||
|
let with_whitespace = |marker: &str| match config.marker_whitespace {
|
||||||
|
JsonToolCallWhitespace::Optional => marker.to_string(),
|
||||||
|
JsonToolCallWhitespace::Exact(whitespace) => format!("{marker}{whitespace}"),
|
||||||
|
};
|
||||||
|
let start_marker = with_whitespace(config.start_marker);
|
||||||
match config.framed_start_marker {
|
match config.framed_start_marker {
|
||||||
Some(marker) => safe_text_len_mul(input, &[marker, config.start_marker]),
|
Some(marker) => safe_text_len_mul(input, &[with_whitespace(marker), start_marker]),
|
||||||
None => safe_text_len(input, config.start_marker),
|
None => safe_text_len(input, start_marker),
|
||||||
}
|
}
|
||||||
.map(|len| JsonToolCallEvent::Text { len })
|
.map(|len| JsonToolCallEvent::Text { len })
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -205,6 +205,23 @@ mod tests {
|
|||||||
assert!(output.calls().is_empty());
|
assert!(output.calls().is_empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn qwen_xml_streams_start_marker_without_newline_as_text() {
|
||||||
|
for chunk in [
|
||||||
|
"<tool_call>{\"name\":\"get_weather\"",
|
||||||
|
"hi\n<tool_call>{\"name\":\"get_weather\"",
|
||||||
|
] {
|
||||||
|
let mut parser = Qwen3XmlToolParser::new(&test_tools());
|
||||||
|
let mut output = ToolParserOutput::default();
|
||||||
|
|
||||||
|
parser.parse_into(chunk, &mut output).unwrap();
|
||||||
|
|
||||||
|
// Not a tool-call start: released without waiting for the end of the stream.
|
||||||
|
assert_eq!(output.normal_text(), chunk);
|
||||||
|
assert!(output.calls().is_empty());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn qwen_xml_requires_newline_before_tool_call_end() {
|
fn qwen_xml_requires_newline_before_tool_call_end() {
|
||||||
let mut parser = Qwen3XmlToolParser::new(&test_tools());
|
let mut parser = Qwen3XmlToolParser::new(&test_tools());
|
||||||
|
|||||||
@@ -599,7 +599,7 @@ fn parse_next_event(
|
|||||||
match safe_text_len_mul(input, &markers) {
|
match safe_text_len_mul(input, &markers) {
|
||||||
Ok(_) => return Ok(HfEvent::Text),
|
Ok(_) => return Ok(HfEvent::Text),
|
||||||
// A marker, complete or partial, starts here (or the input is empty).
|
// A marker, complete or partial, starts here (or the input is empty).
|
||||||
Err(ErrMode::Incomplete(_)) => {}
|
Err(ErrMode::Backtrack(_) | ErrMode::Incomplete(_)) => {}
|
||||||
Err(error) => return Err(error),
|
Err(error) => return Err(error),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -118,7 +118,10 @@ pub fn safe_text_len<'i, I: MarkerStream<'i>, M: MarkerLike>(
|
|||||||
/// the end of the input for plain markers, and only a genuine special token's
|
/// the end of the input for plain markers, and only a genuine special token's
|
||||||
/// incomplete marker for guarded ones.
|
/// incomplete marker for guarded ones.
|
||||||
///
|
///
|
||||||
/// Returns the text length in bytes, and advances the input.
|
/// Returns the text length in bytes, and advances the input. Fails with
|
||||||
|
/// `Backtrack` when a complete marker starts the input, so another branch can
|
||||||
|
/// consume it, and with `Incomplete` when the input is empty or starts with a
|
||||||
|
/// marker that needs more input to decide.
|
||||||
pub fn safe_text_len_mul<'i, I: MarkerStream<'i>, M: MarkerLike>(
|
pub fn safe_text_len_mul<'i, I: MarkerStream<'i>, M: MarkerLike>(
|
||||||
input: &mut I,
|
input: &mut I,
|
||||||
markers: &[M],
|
markers: &[M],
|
||||||
@@ -128,12 +131,18 @@ pub fn safe_text_len_mul<'i, I: MarkerStream<'i>, M: MarkerLike>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
let start = input.offset();
|
let start = input.offset();
|
||||||
let stop = match scan_markers(input, markers, start)? {
|
let (stop, found) = match scan_markers(input, markers, start)? {
|
||||||
Scan::Found(at) | Scan::Pending(at) | Scan::Clear(at) => at,
|
Scan::Found(at) => (at, true),
|
||||||
|
Scan::Pending(at) | Scan::Clear(at) => (at, false),
|
||||||
};
|
};
|
||||||
let emit_len = stop - start;
|
let emit_len = stop - start;
|
||||||
if emit_len == 0 {
|
if emit_len == 0 {
|
||||||
return incomplete();
|
// A complete marker starts here: there is no text, whatever input follows.
|
||||||
|
return if found {
|
||||||
|
Err(ErrMode::Backtrack(ContextError::new()))
|
||||||
|
} else {
|
||||||
|
incomplete()
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
input.next_slice(emit_len);
|
input.next_slice(emit_len);
|
||||||
@@ -529,6 +538,17 @@ mod tests {
|
|||||||
assert!(matches!(error, ErrMode::Incomplete(_)));
|
assert!(matches!(error, ErrMode::Incomplete(_)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn safe_text_len_backtracks_at_complete_marker() {
|
||||||
|
let mut input = Partial::new("<tool_call>body");
|
||||||
|
let checkpoint = input.checkpoint();
|
||||||
|
|
||||||
|
let error = safe_text_len(&mut input, "<tool_call>").unwrap_err();
|
||||||
|
|
||||||
|
assert!(matches!(error, ErrMode::Backtrack(_)));
|
||||||
|
assert_eq!(input.offset_from(&checkpoint), 0);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn safe_text_len_mul_stops_before_earliest_marker() {
|
fn safe_text_len_mul_stops_before_earliest_marker() {
|
||||||
let mut input = Partial::new("hello<channel|><|tool_call>");
|
let mut input = Partial::new("hello<channel|><|tool_call>");
|
||||||
|
|||||||
Reference in New Issue
Block a user