fix(astrbot): make the frame trace reachable by its documented filter
一些检测仍在等待运行
ci / Portable quality checks (push) Waiting to run
ci / Windows Tauri build (push) Waiting to run

The event set an explicit target of astrbot::frame while the enabled!
guard used the default module-path target. A filter naming either one
therefore failed: naming the crate did not reach the event, and naming
the target left the guard returning false. Either way the trace emitted
nothing, which would have looked like the application producing no logs
at all and wasted a reproduction cycle.

Give the guard the same target, hoist it into a constant, and add a test
that drives the exact RUST_LOG value printed in the acceptance
instructions. The test was checked against the broken guard and fails
with empty output. A second test asserts the trace stays silent at the
default level, and both assert no payload text or attachment URL reaches
the log.

Document the capture procedure in the acceptance checklist.

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

查看文件

@ -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. 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. 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 ## Explicitly pending external acceptance
- HTTPS certificate pinning requires a real HTTPS endpoint and first-trust confirmation. - HTTPS certificate pinning requires a real HTTPS endpoint and first-trust confirmation.

查看文件

@ -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. /// Records the *shape* of one SSE frame at debug level.
/// ///
/// Deliberately logs no payload text — only the frame type, which identity /// 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 /// reply to a single message and splits it), and what shape a plugin's
/// attachment frames actually take. /// 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) { fn trace_frame_shape(line: &str) {
if !tracing::enabled!(tracing::Level::DEBUG) { if !tracing::enabled!(target: FRAME_TRACE_TARGET, tracing::Level::DEBUG) {
return; return;
} }
let Some(body) = line.strip_prefix("data:") else { 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()); .is_some_and(|field| !field.is_null());
let streaming = value.get("streaming").and_then(|field| field.as_bool()); let streaming = value.get("streaming").and_then(|field| field.as_bool());
tracing::debug!( tracing::debug!(
target: "astrbot::frame", target: FRAME_TRACE_TARGET,
frame_type, frame_type,
has_message_id, has_message_id,
has_session_id, has_session_id,
@ -830,6 +838,90 @@ fn decode_base64url(value: &str) -> Option<Vec<u8>> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use std::sync::Mutex;
#[derive(Clone, Default)]
struct CapturedLog(Arc<Mutex<Vec<u8>>>);
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<usize> {
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] #[test]
fn rejects_empty_api_key_and_username() { fn rejects_empty_api_key_and_username() {