Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions crates/tinytools-agent/src/stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,30 @@ fn hold_from(tail: &str) -> usize {
}
}
}
if let Some(idx) = trailing_bare_angle_bracket(tail) {
best = best.min(idx);
}
best
}

/// Byte index of a trailing `<` or `</` in `tail` that is followed only by
/// whitespace to the end of the string, if any (`<\s*$` / `</\s*$`).
///
/// The invoke-XML grammar's DSML/namespace prefix tolerates whitespace
/// between the bracket and the marker (`< | DSML | invoke`), which the
/// fixed-literal scan above cannot express — a gap of unknown width is not
/// a literal to prefix-match against. Without this, a fragment boundary
/// landing on the bracket and its whitespace (`"< "`) is released as plain
/// text before the marker arrives, and the opener is unrecoverable once
/// split from its `<`.
fn trailing_bare_angle_bracket(tail: &str) -> Option<usize> {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

priority high critique confident

Hold partially received XML prefixes after the bracket

This only detects a trailing < or </, so it stops protecting the opener as soon as any prefix character arrives. For example, feeding "< | DSML | D" or "< atem:" releases the text because the tail no longer ends in a bare angle bracket, even though both are valid prefixes of < | DSML | invoke ... and < atem:invoke ... respectively. The next fragment ("SML | invoke ..." or "invoke ...") then lacks the original <... prefix and the call is permanently lost. Hold these whitespace-prefixed DSML and namespace prefixes until they are either recognized as a complete opener or disambiguated as ordinary text.

[RULE] partial-input-loss ·

let trimmed = tail.trim_end_matches(char::is_whitespace);
if let Some(stripped) = trimmed.strip_suffix("</") {
Some(stripped.len())
} else {
trimmed.strip_suffix('<').map(str::len)
}
}

#[cfg(test)]
mod test;
20 changes: 20 additions & 0 deletions crates/tinytools-agent/src/stream/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,26 @@ fn dsml_split_across_fragments_is_scrubbed() {
assert_eq!(calls, 1);
}

#[test]
fn a_whitespace_prefixed_dsml_opener_split_before_the_marker_is_held() {
// The complete-tag regex tolerates a space before the marker
// (`< | DSML | invoke`), but that must not regress streaming: a
// fragment boundary landing right after `<` and before the marker has
// arrived must still be held, or the opener is lost the moment the
// held-back fragment is flushed as narrative.
let mut s = StreamScrubber::new();
let first = s.feed("< ");
assert_eq!(
first.text, "",
"a possible whitespace-prefixed DSML opener must be held"
);
assert!(first.calls.is_empty());

let second = s.feed("| DSML | invoke name=\"read\">{\"path\":\"a\"}</| DSML | invoke>");
assert_eq!(second.calls.len(), 1);
assert_eq!(second.calls[0].name, "read");
}

#[test]
fn plural_tool_calls_prose_is_not_held() {
assert_eq!(
Expand Down
Loading