feat(astrbot): compare stream blocks ignoring formatting
一些检测仍在等待运行
ci / Portable quality checks (push) Waiting to run
ci / Windows Tauri build (push) Waiting to run

Exact containment cannot separate "the same text formatted differently"
from "content the stream never carried". A capture had a 278-character
block against 286 streamed characters and reported no containment either
way, which is the reading the segmentation fix depends on.

Report containment with whitespace removed alongside the exact result.
Both are logged: a block that matches only once whitespace is dropped is
the same content, while a block that matches under neither is genuinely
separate.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
这个提交存在于:
YokiiroBW 2026-07-27 03:03:10 +08:00
父节点 5c064acf6c
当前提交 d1e4cdf471
共有 2 个文件被更改,包括 56 次插入 和 1 次删除

查看文件

@ -824,7 +824,10 @@ mod tests {
assert_eq!(completions[0], ("m1", "完整的回复正文")); assert_eq!(completions[0], ("m1", "完整的回复正文"));
// The attachment lands in a message of its own, leaving the reply intact, // The attachment lands in a message of its own, leaving the reply intact,
// and names the file rather than the id of the frame that followed. // and names the file rather than the id of the frame that followed.
assert_eq!(completions[1].1, "![image](astrbot-attachment://sticker.png)"); assert_eq!(
completions[1].1,
"![image](astrbot-attachment://sticker.png)"
);
assert_ne!( assert_ne!(
completions[1].0, "m1", completions[1].0, "m1",
"the attachment must not reuse the finished message id" "the attachment must not reuse the finished message id"

查看文件

@ -873,6 +873,16 @@ impl FrameTrace {
trace_frame_fields(&value); trace_frame_fields(&value);
} }
/// Drops whitespace so a block differing only in line breaks still matches.
///
/// Exact containment could not separate "the same text formatted
/// differently" from "content the stream never carried": one capture had a
/// 278-character block against 286 streamed characters and reported no
/// containment either way.
fn squeezed(text: &str) -> String {
text.chars().filter(|c| !c.is_whitespace()).collect()
}
fn report(&self, final_text: &str) { fn report(&self, final_text: &str) {
let repeated = self let repeated = self
.blocks .blocks
@ -884,12 +894,19 @@ impl FrameTrace {
.iter() .iter()
.filter(|block| self.streamed.contains(block.as_str())) .filter(|block| self.streamed.contains(block.as_str()))
.count(); .count();
let squeezed_final = Self::squeezed(final_text);
let squeezed_repeated = self
.blocks
.iter()
.filter(|block| squeezed_final.contains(&Self::squeezed(block)))
.count();
let joined = self.blocks.concat(); let joined = self.blocks.concat();
tracing::debug!( tracing::debug!(
target: FRAME_TRACE_TARGET, target: FRAME_TRACE_TARGET,
blocks = self.blocks.len(), blocks = self.blocks.len(),
blocks_repeated_in_final = repeated, blocks_repeated_in_final = repeated,
blocks_repeated_in_stream = in_streamed, blocks_repeated_in_stream = in_streamed,
blocks_repeated_ignoring_whitespace = squeezed_repeated,
streamed_len = self.streamed.chars().count(), streamed_len = self.streamed.chars().count(),
final_len = final_text.chars().count(), final_len = final_text.chars().count(),
blocks_len = joined.chars().count(), blocks_len = joined.chars().count(),
@ -1143,6 +1160,7 @@ mod tests {
assert!(output.contains("blocks=2")); assert!(output.contains("blocks=2"));
assert!(output.contains("blocks_repeated_in_final=1")); assert!(output.contains("blocks_repeated_in_final=1"));
assert!(output.contains("blocks_repeated_in_stream=1")); assert!(output.contains("blocks_repeated_in_stream=1"));
assert!(output.contains("blocks_repeated_ignoring_whitespace=1"));
assert!(output.contains("streamed_len=17")); assert!(output.contains("streamed_len=17"));
assert!(output.contains("final_len=17")); assert!(output.contains("final_len=17"));
assert!(output.contains("blocks_len=18")); assert!(output.contains("blocks_len=18"));
@ -1175,6 +1193,40 @@ mod tests {
assert_eq!(sniff_image_mime(b""), None); assert_eq!(sniff_image_mime(b""), None);
} }
/// A block that differs from the streamed text only in line breaks is the
/// same content, and must not be counted as content the stream never
/// carried. Exact containment says otherwise, which is why both are logged.
#[test]
fn stream_summary_sees_through_formatting_differences() {
let sink = CapturedLog::default();
let subscriber = tracing_subscriber::fmt()
.without_time()
.with_ansi(false)
.with_env_filter(tracing_subscriber::EnvFilter::new("astrbot::frame=debug"))
.with_writer(sink.clone())
.finish();
let mut trace = FrameTrace::default();
tracing::subscriber::with_default(subscriber, || {
for line in [
r#"data: {"type":"session_id","session_id":"s1"}"#,
r#"data: {"type":"plain","data":"ONE\nTWO","streaming":true}"#,
// Same words, different line breaks.
r#"data: {"type":"plain","data":"ONE TWO","streaming":false}"#,
r#"data: {"type":"complete","data":"ONE\nTWO"}"#,
] {
trace.observe(line);
}
});
let output = sink.contents();
assert!(output.contains("blocks_repeated_in_final=0"), "{output}");
assert!(
output.contains("blocks_repeated_ignoring_whitespace=1"),
"{output}"
);
}
#[test] #[test]
fn frame_trace_stays_silent_when_the_filter_excludes_it() { fn frame_trace_stays_silent_when_the_filter_excludes_it() {
let sink = CapturedLog::default(); let sink = CapturedLog::default();