diff --git a/docs/q9-local-acceptance.md b/docs/q9-local-acceptance.md index 0a13808..6ec6fc9 100644 --- a/docs/q9-local-acceptance.md +++ b/docs/q9-local-acceptance.md @@ -131,6 +131,37 @@ and each fails under it, so they exercise the invariant rather than restating th 4. Probe the same profile into both slots in turn; the second must be refused — F-19. 5. Confirm a streamed reply renders each character once in a production build — F-15. +## SSE frame capture procedure + +Two open defects — a reply rendering as two bubbles, and plugin images never appearing — depend on +what the server actually sends. `trace_frame_shape` records the structure of every SSE frame: +frame type, whether `message_id` and `session_id` are present, the streaming flag, and the key +names inside `data`. **No payload text is recorded**, so the output is safe to share. + +The log target is `astrbot::frame`. A filter naming the crate instead will not reach it. + +PowerShell, from the repository root: + +```powershell +$env:RUST_LOG = "astrbot::frame=debug"; npm run tauri:dev 2>&1 | Tee-Object -FilePath frames.log +``` + +Then, in the running application: + +1. Send one message and watch for the reply splitting into two bubbles. +2. Trigger a plugin that replies with an image. +3. Right-click the desktop pet and note whether an error appears in the pet window. + +What the log answers: + +- If every `frame_type="plain"` line has `has_message_id=true`, the double bubble is **not** F-34 + and the `historySessionId` fallback is the remaining candidate. +- The `data_keys` of an `image` frame, and whether an `attachment_saved` frame follows it, decide + whether the mapper can emit an attachment marker at all. + +`frame_trace_fires_under_the_documented_filter_and_leaks_no_payload` in `astrbot_client.rs` guards +this exact `RUST_LOG` value, so the command above cannot silently produce nothing. + ## Explicitly pending external acceptance - HTTPS certificate pinning requires a real HTTPS endpoint and first-trust confirmation. diff --git a/src-tauri/src/networking/astrbot_client.rs b/src-tauri/src/networking/astrbot_client.rs index 8b4ab03..b3db220 100644 --- a/src-tauri/src/networking/astrbot_client.rs +++ b/src-tauri/src/networking/astrbot_client.rs @@ -722,6 +722,10 @@ impl AstrBotClient { } } +/// Log target for the SSE frame trace. Named in the acceptance instructions, so +/// it must stay in sync with `docs/q9-local-acceptance.md`. +const FRAME_TRACE_TARGET: &str = "astrbot::frame"; + /// Records the *shape* of one SSE frame at debug level. /// /// Deliberately logs no payload text — only the frame type, which identity @@ -731,9 +735,13 @@ impl AstrBotClient { /// reply to a single message and splits it), and what shape a plugin's /// attachment frames actually take. /// -/// Enable with `RUST_LOG=astrbot_desktop=debug`. Off by default. +/// Enable with `RUST_LOG=astrbot::frame=debug`. Off by default. +/// +/// The guard must name the same target as the event. `enabled!` defaults to the +/// module path, so without it the filter would enable the event while the guard +/// still returned false and nothing would ever be written. fn trace_frame_shape(line: &str) { - if !tracing::enabled!(tracing::Level::DEBUG) { + if !tracing::enabled!(target: FRAME_TRACE_TARGET, tracing::Level::DEBUG) { return; } let Some(body) = line.strip_prefix("data:") else { @@ -762,7 +770,7 @@ fn trace_frame_shape(line: &str) { .is_some_and(|field| !field.is_null()); let streaming = value.get("streaming").and_then(|field| field.as_bool()); tracing::debug!( - target: "astrbot::frame", + target: FRAME_TRACE_TARGET, frame_type, has_message_id, has_session_id, @@ -830,6 +838,90 @@ fn decode_base64url(value: &str) -> Option> { #[cfg(test)] mod tests { use super::*; + use std::sync::Mutex; + + #[derive(Clone, Default)] + struct CapturedLog(Arc>>); + + impl CapturedLog { + fn contents(&self) -> String { + String::from_utf8_lossy(&self.0.lock().expect("log lock")).into_owned() + } + } + + impl std::io::Write for CapturedLog { + fn write(&mut self, buffer: &[u8]) -> std::io::Result { + self.0.lock().expect("log lock").extend_from_slice(buffer); + Ok(buffer.len()) + } + + fn flush(&mut self) -> std::io::Result<()> { + Ok(()) + } + } + + impl<'writer> tracing_subscriber::fmt::MakeWriter<'writer> for CapturedLog { + type Writer = CapturedLog; + + fn make_writer(&'writer self) -> Self::Writer { + self.clone() + } + } + + /// Guards the exact `RUST_LOG` value printed in the acceptance instructions. + /// + /// The event sets an explicit target, so a filter naming the crate would not + /// reach it, and an `enabled!` guard using the default target would suppress + /// it even when the filter matched. Both are silent failures that would look + /// like "the app produced no logs". + #[test] + fn frame_trace_fires_under_the_documented_filter_and_leaks_no_payload() { + 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(); + + tracing::subscriber::with_default(subscriber, || { + trace_frame_shape( + r#"data: {"type":"plain","data":"你好","streaming":true,"message_id":"42"}"#, + ); + trace_frame_shape(r#"data: {"type":"image","data":{"url":"http://x/y.png"}}"#); + }); + + let output = sink.contents(); + assert!( + output.contains("astrbot sse frame"), + "the documented filter must actually emit: {output}" + ); + assert!(output.contains("frame_type=\"plain\"")); + assert!(output.contains("has_message_id=true")); + assert!(output.contains("frame_type=\"image\"")); + // The key names are what identify the plugin attachment shape. + assert!(output.contains("data_keys=\"url\"")); + // No payload text, and no attachment URL, may reach the log. + assert!(!output.contains("\u{4f60}\u{597d}"), "payload text leaked"); + assert!(!output.contains("y.png"), "attachment url leaked"); + } + + #[test] + fn frame_trace_stays_silent_when_the_filter_excludes_it() { + let sink = CapturedLog::default(); + let subscriber = tracing_subscriber::fmt() + .without_time() + .with_ansi(false) + .with_env_filter(tracing_subscriber::EnvFilter::new("info")) + .with_writer(sink.clone()) + .finish(); + + tracing::subscriber::with_default(subscriber, || { + trace_frame_shape(r#"data: {"type":"plain","message_id":"42"}"#); + }); + + assert!(sink.contents().is_empty()); + } #[test] fn rejects_empty_api_key_and_username() {