From d1e4cdf4717bf677f06ca0aaed81742cc241a5b6 Mon Sep 17 00:00:00 2001 From: YokiiroBW Date: Mon, 27 Jul 2026 03:03:10 +0800 Subject: [PATCH] feat(astrbot): compare stream blocks ignoring formatting 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 --- crates/desktop-protocol/src/astrbot.rs | 5 ++- src-tauri/src/networking/astrbot_client.rs | 52 ++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/crates/desktop-protocol/src/astrbot.rs b/crates/desktop-protocol/src/astrbot.rs index 077f111..403a754 100644 --- a/crates/desktop-protocol/src/astrbot.rs +++ b/crates/desktop-protocol/src/astrbot.rs @@ -824,7 +824,10 @@ mod tests { assert_eq!(completions[0], ("m1", "完整的回复正文")); // 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. - assert_eq!(completions[1].1, "![image](astrbot-attachment://sticker.png)"); + assert_eq!( + completions[1].1, + "![image](astrbot-attachment://sticker.png)" + ); assert_ne!( completions[1].0, "m1", "the attachment must not reuse the finished message id" diff --git a/src-tauri/src/networking/astrbot_client.rs b/src-tauri/src/networking/astrbot_client.rs index 6339b67..1b97e44 100644 --- a/src-tauri/src/networking/astrbot_client.rs +++ b/src-tauri/src/networking/astrbot_client.rs @@ -873,6 +873,16 @@ impl FrameTrace { 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) { let repeated = self .blocks @@ -884,12 +894,19 @@ impl FrameTrace { .iter() .filter(|block| self.streamed.contains(block.as_str())) .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(); tracing::debug!( target: FRAME_TRACE_TARGET, blocks = self.blocks.len(), blocks_repeated_in_final = repeated, blocks_repeated_in_stream = in_streamed, + blocks_repeated_ignoring_whitespace = squeezed_repeated, streamed_len = self.streamed.chars().count(), final_len = final_text.chars().count(), blocks_len = joined.chars().count(), @@ -1143,6 +1160,7 @@ mod tests { assert!(output.contains("blocks=2")); assert!(output.contains("blocks_repeated_in_final=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("final_len=17")); assert!(output.contains("blocks_len=18")); @@ -1175,6 +1193,40 @@ mod tests { 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] fn frame_trace_stays_silent_when_the_filter_excludes_it() { let sink = CapturedLog::default();