feat(astrbot): summarise how stream blocks relate to the final text
一些检测仍在等待运行
ci / Portable quality checks (push) Waiting to run
ci / Windows Tauri build (push) Waiting to run

The first capture identified the real cause of a reply appearing twice,
and it was neither candidate. All 524 frames carried a message_id and
each reply used exactly one, so the event_id fallback is not involved
and neither is the history session fallback.

AstrBot interleaves two kinds of plain frame in one reply. Frames with
streaming true are incremental deltas whose total matches the complete
frame exactly. Frames with streaming false carry whole finished blocks —
one reply had five of them totalling 915 characters against 173
streamed. map_plain appends both, so the blocks show up inline during
generation, and map_complete then replaces the accumulation with the
streamed text alone, discarding them. That is the reply appearing twice
and then collapsing, and the collapse loses content.

One thing lengths cannot settle: whether a block repeats streamed text
or carries its own. One reply had 133 block characters against 142
streamed, which looks like a repeat; another had 915 against 173, which
cannot be. The two need opposite fixes, so this adds a per-stream
summary reporting how many blocks appear in the final text and in the
streamed text, as counts and lengths only.

Also records that plugin images were never sent: no media or attachment
frame appeared in any capture, and no plain frame carried a message
chain array. The attachment path has still never received data.

Refs: N-9 (new), N-4 (root cause found), N-6 (not a client defect).

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

查看文件

@ -231,6 +231,57 @@
**验证**:在真实样式表下用浏览器实测——头像与气泡顶部差 0px,按钮 34×34 / `border-width: 0px`,
标签盒 1×1(已裁剪),聚焦后操作条 opacity 由 0 变 1。
### 2026-07-27 抓帧结果:N-4 / N-6 定论,两个候选销案
首轮 524 帧(4 轮对话)已捕获并解析。结论如下,**此前记录的两个候选均不成立**。
**N-6 插件图片:根本没发出来。** 524 帧中 `image` / `record` / `file` / `video` /
`attachment_saved` **数量为 0**,且 `plain` 帧的 `data` **从未是数组**(消息链)——全部是纯字符串。
所以不是客户端渲染失败,是 AstrBot 侧压根没投递媒体帧。我们的附件链路至今零数据,
F-33 / N-6 / N-7 全部**无法验证**,需先让服务端真的发出图片。
**N-4 双气泡:F-34 排除,`historySessionId` 回退排除。**
- 全部 `plain` 帧**均携带 `message_id`**(77 / 136 / 172 / 112 条,无一缺失)→ 候选 A(`messageId || eventId`
回退产生每事件唯一 id)**不成立**。
- 每轮对话**只有一个 `message_id`**,全程不变 → 也不是服务端分成了多条消息。
- 因此候选 B(`historySessionId` 回退到 `storedSessions[0]`)也不是本症状的原因。
该回退仍是缺陷,但**降级为独立项 N-5**,不再与双气泡绑定。
**真因:`streaming: false` 的整段 `plain` 帧被当作增量拼接,随后被 `complete` 替换丢弃。**
AstrBot 在一次回复中**交错**投递两类 `plain` 帧:
| 类型 | 语义 | 实测 |
| --- | --- | --- |
| `streaming: true` | 逐字增量,累计等于 `complete` 的内容 | 第2轮 173 字 = complete 173;第3轮 209 = 209;第4轮 142 = 142 |
| `streaming: false` | **整段已完成的消息** | 第2轮 5 段共 915 字;第3轮 2 段共 469 字;第4轮 1 段 133 字 |
实测帧序(第2轮):`[增量×14] → 整段217 → 整段192 → [增量×23] → 整段201 → 整段192 →
[增量×94] → 整段113 → complete(173)`。
`map_plain`(`astrbot.rs:379`)对两类帧一视同仁,**全部 `emit_delta` 追加**。于是:
1. 生成过程中气泡里先出现整段内容,再继续逐字追加流式内容 → 用户看到的「两个回复」
2. `map_complete`(`astrbot.rs:405`)用 `complete` 的内容**替换** `accumulated` →
整段那部分**被丢弃** → 「结束后恢复成一个」,且**最终显示的内容比 AstrBot 实际发出的少**
**尚未定论的一点**:整段内容与流式内容是否重复。第4轮的 133 字 vs 142 字非常接近(疑似重复),
而第2/3轮的 915 vs 173、469 vs 209 明显是额外内容。两者需要相反的处理——重复则单独开气泡会显示两遍,
额外则丢弃会少内容。**仅凭长度无法区分**,已加 `astrbot stream summary` 探针
(`blocks_repeated_in_final` / `blocks_repeated_in_stream`,只输出布尔与长度,不输出正文),
待下一次复现后定论。
**产品决定(2026-07-27)**:分段按**每段一个气泡**呈现,忠实还原 AstrBot 的分段节奏。
### 新增项
| ID | 问题 | 位置 | 状态 |
| --- | --- | --- | --- |
| **N-9** | 分段消息被拼接进流式消息,并被 `complete` 替换丢弃 | `astrbot.rs:379`(`map_plain`)、`astrbot.rs:405`(`map_complete`) | 真因已定位,待重复性探针结果后实施 |
| **F-44** | 发送后不滚动到底部,流式生成发生在视口外 | `ChatPanel.tsx` 全文件无 scroll 代码 | 已归档于 R6,用户 2026-07-27 反馈确认 |
| **N-8** | 消息进入列表无过渡动画,生硬出现 | `ChatPanel.tsx` 消息列表渲染 | 新记录,用户要求类 TG 的淡入过渡 |
### 待抓帧定论
已加 `trace_frame_shape()`(`astrbot_client.rs`,`RUST_LOG=astrbot_desktop=debug` 开启,只记结构不记内容)。

查看文件

@ -741,15 +741,87 @@ const FRAME_TRACE_TARGET: &str = "astrbot::frame";
/// 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!(target: FRAME_TRACE_TARGET, tracing::Level::DEBUG) {
return;
FRAME_TRACE.with(|trace| trace.borrow_mut().observe(line));
}
/// Per-stream state for the one question frame shapes cannot answer.
///
/// AstrBot interleaves `plain` frames carrying incremental deltas
/// (`streaming: true`) with `plain` frames carrying a whole block
/// (`streaming: false`). Whether those blocks repeat text the delta stream also
/// delivers, or carry content of their own, decides opposite fixes: showing a
/// repeat as its own message duplicates it, while dropping distinct content
/// loses it. Lengths alone cannot tell them apart — one captured reply had a
/// 133-character block against 142 streamed characters, another had 915 against
/// 173.
///
/// Only lengths and containment booleans are logged. No text is recorded.
#[derive(Default)]
struct FrameTrace {
streamed: String,
blocks: Vec<String>,
}
thread_local! {
static FRAME_TRACE: std::cell::RefCell<FrameTrace> =
std::cell::RefCell::new(FrameTrace::default());
}
impl FrameTrace {
fn observe(&mut self, line: &str) {
if !tracing::enabled!(target: FRAME_TRACE_TARGET, tracing::Level::DEBUG) {
return;
}
let Some(body) = line.strip_prefix("data:") else {
return;
};
let Ok(value) = serde_json::from_str::<Value>(body.trim()) else {
return;
};
let frame_type = value.get("type").and_then(|field| field.as_str());
let text = value.get("data").and_then(|field| field.as_str());
let streaming = value.get("streaming").and_then(|field| field.as_bool());
match (frame_type, text) {
(Some("session_id"), _) => *self = Self::default(),
(Some("plain"), Some(text)) if streaming == Some(true) => {
self.streamed.push_str(text);
}
(Some("plain"), Some(text)) if streaming == Some(false) => {
self.blocks.push(text.to_owned());
}
(Some("complete"), Some(final_text)) => self.report(final_text),
_ => {}
}
trace_frame_fields(&value);
}
let Some(body) = line.strip_prefix("data:") else {
return;
};
let Ok(value) = serde_json::from_str::<Value>(body.trim()) else {
return;
};
fn report(&self, final_text: &str) {
let repeated = self
.blocks
.iter()
.filter(|block| final_text.contains(block.as_str()))
.count();
let in_streamed = self
.blocks
.iter()
.filter(|block| self.streamed.contains(block.as_str()))
.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,
streamed_len = self.streamed.chars().count(),
final_len = final_text.chars().count(),
blocks_len = joined.chars().count(),
final_is_last_block = self.blocks.last().is_some_and(|last| last == final_text),
"astrbot stream summary"
);
}
}
fn trace_frame_fields(value: &Value) {
let data = value.get("data");
let data_keys = match data {
Some(Value::Object(map)) => map.keys().cloned().collect::<Vec<_>>().join(","),
@ -929,6 +1001,49 @@ mod tests {
assert!(!output.contains("y.png"), "attachment url leaked");
}
/// The summary must separate a block that repeats streamed text from one
/// that carries content of its own, because the two need opposite fixes.
#[test]
fn stream_summary_separates_repeated_blocks_from_distinct_ones() {
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, || {
for line in [
r#"data: {"type":"session_id","session_id":"s1"}"#,
r#"data: {"type":"plain","data":"ALPHAWORD","streaming":true}"#,
// Repeats what the delta stream already carried.
r#"data: {"type":"plain","data":"ALPHAWORD","streaming":false}"#,
r#"data: {"type":"plain","data":"BETAWORD","streaming":true}"#,
// Carries content the stream never delivers.
r#"data: {"type":"plain","data":"GAMMAWORD","streaming":false}"#,
r#"data: {"type":"complete","data":"ALPHAWORDBETAWORD"}"#,
] {
trace_frame_shape(line);
}
});
let output = sink.contents();
assert!(output.contains("astrbot stream summary"), "{output}");
assert!(output.contains("blocks=2"));
assert!(output.contains("blocks_repeated_in_final=1"));
assert!(output.contains("blocks_repeated_in_stream=1"));
assert!(output.contains("streamed_len=17"));
assert!(output.contains("final_len=17"));
assert!(output.contains("blocks_len=18"));
for payload in ["ALPHAWORD", "BETAWORD", "GAMMAWORD"] {
assert!(
!output.contains(payload),
"payload {payload} leaked: {output}"
);
}
}
#[test]
fn frame_trace_stays_silent_when_the_filter_excludes_it() {
let sink = CapturedLog::default();