From 3017145102a7a463b02c9eb4b2a5519ccebbb1ae Mon Sep 17 00:00:00 2001 From: hataa <79907651+hata33@users.noreply.github.com> Date: Sun, 20 Sep 2026 12:02:09 +0800 Subject: [PATCH] fix(frontend): stop seeding new answer events with prior-round text (#3386) A new answer round with no tool_call between rounds (stream-stall retry, length-truncation continuation, empty-answer nudge, finalize synthesis) inherited the concatenated text of every prior round via the message.content seed, duplicating it on every recompose with no way to retract. Seed only when no other live answer event exists in the stream. Fixes #3383 --- .../composables/useChatStreamHandler.test.mjs | 75 +++++++++++++++++++ .../src/composables/useChatStreamHandler.ts | 16 +++- 2 files changed, 90 insertions(+), 1 deletion(-) diff --git a/frontend/src/composables/useChatStreamHandler.test.mjs b/frontend/src/composables/useChatStreamHandler.test.mjs index d4e31044..3b7ecca8 100644 --- a/frontend/src/composables/useChatStreamHandler.test.mjs +++ b/frontend/src/composables/useChatStreamHandler.test.mjs @@ -161,3 +161,78 @@ test('agent and non-agent stop notify onGenerationStopped', () => { assert.notEqual(nonAgent, -1) assert.match(source.slice(nonAgent, nonAgent + 500), /onGenerationStopped\?\.\(\)/) }) + +// A new answer round with no tool_call between rounds (retry / length-continuation / +// nudge / finalize) used to inherit the concatenated text of every prior round via +// the message.content seed, duplicating it on screen with no way to retract. +const runAnswerCase = () => { + const chunkStart = source.indexOf("case 'answer':") + const chunkEnd = source.indexOf("case 'artifacts_pending'", chunkStart) + const chunk = source.slice(chunkStart, chunkEnd) + const recompose = (message) => { + let out = '' + for (const e of message.agentEventStream || []) { + if (e.type === 'answer' && !e.superseded && e.content) out += e.content + } + return out + } + const state = { fullContent: { value: '' }, doneCount: 0 } + const process = vm.runInNewContext( + ts.transpile(`(message, data, dataPayload) => { switch ('answer') { ${chunk} } }`), + { + ...state, + recomposeAgentAnswer: recompose, + log() {}, + onAgentAnswerDone() { state.doneCount++ }, + isAgentStreamSession: () => true, + loading: { value: true }, + isReplying: { value: true }, + }, + ) + return { process, state } +} + +test('a new answer round does not inherit text from live prior answer events', () => { + const { process, state } = runAnswerCase() + const message = { agentEventStream: [], _eventMap: new Map(), content: '' } + + // Round 1 streams to completion. + process(message, { content: '第一点' }, { event_id: 'r1' }) + process(message, { content: '' }, { event_id: 'r1', done: true }) + + // Round 2 starts with no tool_call in between (the issue's exact sequence). + process(message, { content: '第二点' }, { event_id: 'r2' }) + process(message, { content: '' }, { event_id: 'r2', done: true }) + + const r1 = message._eventMap.get('r1') + const r2 = message._eventMap.get('r2') + assert.equal(r1.content, '第一点') + assert.equal(r2.content, '第二点') + assert.equal(message.content, '第一点第二点') + assert.equal(state.fullContent.value, '第一点第二点') +}) + +test('seeding still recovers a resumption whose text lives only in message.content', () => { + const { process } = runAnswerCase() + const message = { + agentEventStream: [], + _eventMap: new Map(), + // Resume path: content was restored from history but no answer event exists. + content: '已恢复的文本', + } + process(message, { content: ' 续写' }, { event_id: 'r' }) + assert.equal(message._eventMap.get('r').content, '已恢复的文本 续写') + assert.equal(message.content, '已恢复的文本 续写') +}) + +test('a superseded prior round does not block or re-seed the new round', () => { + const { process } = runAnswerCase() + const prior = { type: 'answer', event_id: 'r1', content: '旧稿', done: true, superseded: true } + const message = { agentEventStream: [prior], _eventMap: new Map(), content: '' } + + process(message, { content: '正式答案' }, { event_id: 'r2' }) + + assert.equal(prior.content, '旧稿') + assert.equal(message._eventMap.get('r2').content, '正式答案') + assert.equal(message.content, '正式答案') +}) diff --git a/frontend/src/composables/useChatStreamHandler.ts b/frontend/src/composables/useChatStreamHandler.ts index de8549f3..3268dada 100644 --- a/frontend/src/composables/useChatStreamHandler.ts +++ b/frontend/src/composables/useChatStreamHandler.ts @@ -909,7 +909,21 @@ export function useChatStreamHandler(options: UseChatStreamHandlerOptions) { if (eventId) eventMap.set(eventId, answerEvent) } if (!answerEvent.content && message.content && String(message.content).trim()) { - answerEvent.content = message.content + // Seeding exists for resume paths where message.content holds text no + // stream event carries. When a live prior answer event exists, its + // text is already counted by recomposeAgentAnswer, so seeding the new + // event would duplicate every prior round on the next recompose. + const hasLivePriorAnswer = stream.some( + (e) => + e !== answerEvent && + e.type === 'answer' && + !e.superseded && + e.content && + String(e.content).trim(), + ) + if (!hasLivePriorAnswer) { + answerEvent.content = message.content + } } if (data.content) { answerEvent.content = String(answerEvent.content || '') + String(data.content)