fix(astrbot): stop late content from overwriting a finished reply

A captured stream ended complete(272 chars) → image → attachment_saved
→ end, all under one message id. complete emptied the accumulation, the
attachment marker re-accumulated onto it, and end emitted a second
completion for the same message whose body was the marker alone. The
client assigns completed content over the existing message, so 272
characters of reply were replaced by an image tag — the reply visibly
vanished behind a broken image placeholder.

Mark a message as completed when its completion is emitted, and open a
new message on the next delta rather than reusing the finished id. The
reopened id carries a #part- suffix so the client reads it as a separate
message; frame comparisons use the base id so a later frame bearing the
server's original id is not treated as a different message.

The regression test replays the captured frame order and was checked
against a mutation that drops the completed flag, where it fails with
the attachment reusing the finished id.

Refs: F-32 (audit P2-17), which predicted this as "content arriving
after complete overwrites the full text".

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
这个提交存在于:
YokiiroBW 2026-07-27 02:21:15 +08:00
父节点 03b99efda8
当前提交 3a221a22d6

查看文件

@ -222,6 +222,12 @@ struct PendingMedia {
media_type: String, media_type: String,
} }
/// Separates a message id from its reopen index.
///
/// Content that arrives after `complete` reuses the server's message id, so
/// the reopened message needs a suffix the client will read as a new message.
const REOPEN_SEPARATOR: &str = "#part-";
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct AstrBotStreamMapper { pub struct AstrBotStreamMapper {
server_instance_id: String, server_instance_id: String,
@ -233,6 +239,17 @@ pub struct AstrBotStreamMapper {
sequence: u64, sequence: u64,
saw_assistant_delta: bool, saw_assistant_delta: bool,
pending_media: Option<PendingMedia>, pending_media: Option<PendingMedia>,
/// True once the active message has been completed.
///
/// AstrBot can deliver content *after* the `complete` frame — an observed
/// stream ended `complete(272 chars)` → `image` → `attachment_saved` →
/// `end`, all under one message id. Re-accumulating that late content under
/// the finished id made `end` emit a second completion whose body was the
/// attachment marker alone, and the client overwrote the whole reply with
/// it. Late content therefore has to open a message of its own.
active_message_completed: bool,
/// Distinguishes the ids of messages reopened after a completion.
reopen_index: u32,
} }
impl AstrBotStreamMapper { impl AstrBotStreamMapper {
@ -340,14 +357,14 @@ impl AstrBotStreamMapper {
let mut events = Vec::new(); let mut events = Vec::new();
if let Some(message_id) = frame.message_id { if let Some(message_id) = frame.message_id {
if self if self
.active_message_id .active_base_id()
.as_ref() .is_some_and(|current| current != message_id)
.is_some_and(|current| current != &message_id)
{ {
events = self.complete_if_needed(FinishReason::Stop); events = self.complete_if_needed(FinishReason::Stop);
self.accumulated.clear(); self.accumulated.clear();
self.delta_index = 0; self.delta_index = 0;
self.saw_assistant_delta = false; self.saw_assistant_delta = false;
self.active_message_completed = false;
} }
self.active_message_id = Some(message_id); self.active_message_id = Some(message_id);
} }
@ -384,9 +401,8 @@ impl AstrBotStreamMapper {
} }
if let Some(message_id) = frame.message_id.clone() { if let Some(message_id) = frame.message_id.clone() {
if self if self
.active_message_id .active_base_id()
.as_ref() .is_some_and(|current| current != message_id)
.is_some_and(|current| current != &message_id)
{ {
// New assistant message id: complete the previous accumulation first. // New assistant message id: complete the previous accumulation first.
let mut events = self.complete_if_needed(FinishReason::Stop); let mut events = self.complete_if_needed(FinishReason::Stop);
@ -394,6 +410,7 @@ impl AstrBotStreamMapper {
self.accumulated.clear(); self.accumulated.clear();
self.delta_index = 0; self.delta_index = 0;
self.saw_assistant_delta = false; self.saw_assistant_delta = false;
self.active_message_completed = false;
events.extend(self.emit_delta(chunk)); events.extend(self.emit_delta(chunk));
return events; return events;
} }
@ -405,15 +422,15 @@ impl AstrBotStreamMapper {
fn map_complete(&mut self, frame: AstrBotStreamFrame) -> Vec<GatewayEvent> { fn map_complete(&mut self, frame: AstrBotStreamFrame) -> Vec<GatewayEvent> {
if let Some(message_id) = frame.message_id.clone() { if let Some(message_id) = frame.message_id.clone() {
if self if self
.active_message_id .active_base_id()
.as_ref() .is_some_and(|current| current != message_id)
.is_some_and(|current| current != &message_id)
{ {
let mut events = self.complete_if_needed(FinishReason::Stop); let mut events = self.complete_if_needed(FinishReason::Stop);
self.active_message_id = Some(message_id); self.active_message_id = Some(message_id);
self.accumulated.clear(); self.accumulated.clear();
self.delta_index = 0; self.delta_index = 0;
self.saw_assistant_delta = false; self.saw_assistant_delta = false;
self.active_message_completed = false;
let final_text = text_from_value(&frame.data).unwrap_or_default(); let final_text = text_from_value(&frame.data).unwrap_or_default();
if !final_text.is_empty() { if !final_text.is_empty() {
self.accumulated = final_text; self.accumulated = final_text;
@ -433,10 +450,41 @@ impl AstrBotStreamMapper {
self.complete_if_needed(FinishReason::Stop) self.complete_if_needed(FinishReason::Stop)
} }
/// The active id without any reopen suffix, for comparing against a frame.
fn active_base_id(&self) -> Option<&str> {
self.active_message_id
.as_deref()
.map(|id| id.split_once(REOPEN_SEPARATOR).map_or(id, |(head, _)| head))
}
/// Starts a new message after the active one has already been completed.
///
/// The server keeps the same message id for content that arrives after
/// `complete`, so the id has to be made distinct here or the client merges
/// the late content into the finished message and overwrites it.
fn reopen_message(&mut self) {
let base = self
.active_base_id()
.map(str::to_owned)
.unwrap_or_else(|| self.stream_id.clone());
self.reopen_index = self.reopen_index.saturating_add(1);
self.active_message_id = Some(format!(
"{base}{REOPEN_SEPARATOR}{index}",
index = self.reopen_index
));
self.accumulated.clear();
self.delta_index = 0;
self.saw_assistant_delta = false;
self.active_message_completed = false;
}
fn emit_delta(&mut self, chunk: String) -> Vec<GatewayEvent> { fn emit_delta(&mut self, chunk: String) -> Vec<GatewayEvent> {
if chunk.is_empty() { if chunk.is_empty() {
return Vec::new(); return Vec::new();
} }
if self.active_message_completed {
self.reopen_message();
}
self.accumulated.push_str(&chunk); self.accumulated.push_str(&chunk);
self.saw_assistant_delta = true; self.saw_assistant_delta = true;
let index = self.delta_index; let index = self.delta_index;
@ -457,6 +505,7 @@ impl AstrBotStreamMapper {
let content = std::mem::take(&mut self.accumulated); let content = std::mem::take(&mut self.accumulated);
self.saw_assistant_delta = false; self.saw_assistant_delta = false;
self.delta_index = 0; self.delta_index = 0;
self.active_message_completed = true;
let message_id = self.active_message_id.clone(); let message_id = self.active_message_id.clone();
vec![self.event( vec![self.event(
message_id, message_id,
@ -660,6 +709,49 @@ mod tests {
assert_eq!(delta, "![image](astrbot-attachment://att-1)"); assert_eq!(delta, "![image](astrbot-attachment://att-1)");
} }
/// Replays the frame order of a captured stream that destroyed its own reply.
///
/// The server sent `complete` and then continued: `image`,
/// `attachment_saved`, `end`, all under the same message id. The attachment
/// marker re-accumulated onto the just-emptied buffer and `end` emitted a
/// second completion for the finished message whose body was the marker
/// alone, so the client replaced 272 characters of reply with an image tag.
#[test]
fn content_after_complete_does_not_overwrite_the_finished_message() {
let mut mapper = AstrBotStreamMapper::new("astrbot-test", "stream-a");
let mut events = Vec::new();
for line in [
r#"data: {"type":"plain","data":"完整的回复正文","streaming":true,"message_id":"m1"}"#,
r#"data: {"type":"complete","data":"完整的回复正文","message_id":"m1"}"#,
r#"data: {"type":"image","data":"[IMAGE]sticker.png","message_id":"m1"}"#,
r#"data: {"type":"attachment_saved","data":{"id":"att-9","type":"image"},"message_id":"m1"}"#,
] {
events.extend(mapper.push_line(line).expect("frame"));
}
events.extend(mapper.finish_stream());
let completions: Vec<(&str, &str)> = events
.iter()
.filter_map(|event| match &event.body {
GatewayEventBody::MessageCompleted(payload) => Some((
event.metadata.message_id.as_deref().unwrap_or("<none>"),
payload.content.as_deref().unwrap_or(""),
)),
_ => None,
})
.collect();
assert_eq!(completions.len(), 2, "{completions:?}");
assert_eq!(completions[0], ("m1", "完整的回复正文"));
// The attachment lands in a message of its own, leaving the reply intact.
assert_eq!(completions[1].1, "![image](astrbot-attachment://att-9)");
assert_ne!(
completions[1].0, "m1",
"the attachment must not reuse the finished message id"
);
assert!(completions[1].0.starts_with("m1#part-"));
}
#[test] #[test]
fn consecutive_streams_do_not_reuse_event_ids() { fn consecutive_streams_do_not_reuse_event_ids() {
// Regression: a client keeps one deduplicator for its whole lifetime, // Regression: a client keeps one deduplicator for its whole lifetime,