bug(desktop): 流式回复可能显示在对应用户消息之前
- Dominant language
- TypeScript
- Stars
- 2.7k
- Forks
- 395
- Avg merge
- 21h 48m
- Merged PRs (30d)
- 776
Description
**提交人**: nian1
**客户端版本**: 0.1.71
---
## 问题
聊天消息偶发出现时间顺序颠倒:assistant 的回复显示在触发它的 user 消息之前。
例如:用户发送「这对话显示还是乱的」后,对应的 assistant 回复已经显示在它上方,导致阅读顺序错误。
## 复现条件
在 Desktop 聊天中发送消息。若 assistant 流式事件先于 renderer 插入 optimistic user message 到达,就可能复现。响应越快或 `sessions:send` 返回与事件分发之间存在时序差异时越容易发生。
## 预期行为
始终按会话时间线显示:
```text
user 消息
assistant 回复
```
## 初步定位
- `apps/desktop/src/main/sessions-ipc-main.ts` 的 `sessions:send` 启动 `streamEvents(...)` 后立即返回 `{ ok: true }`。
- `apps/desktop/src/renderer/app-shell-chat-actions.ts` 等待 `window.maka.sessions.send()` 返回后,才调用 `showOptimisticUserMessage()`。
- 在这段窗口内,assistant 的流式事件或持久化刷新可能先更新 `messages`。
- `packages/ui/src/materialize.ts` 的 `materializeTurns()` 按传入 `messages` 的顺序建立 turn,未对 user/assistant 进行时序修复,因此后到的 optimistic user 会被追加到 assistant 后面。
## 影响
对话内容本身没有丢失,但阅读顺序错误,用户会误以为回复属于更早的消息。
## 建议
在启动发送或流式事件前先建立 user turn,或让事件/持久化层保证 user 记录先于 assistant 可见,并增加覆盖该竞态的 renderer/UI 回归测试。
---
## 提交时的任务环境
仅代表提交时快照,不一定是故障环境。OS 来自提交客户端本机,不含 SSH 远端主机;Harness / 模型来自当前任务。与运行环境无关的反馈可忽略本段。
**版本区域**: CN
**OS**: darwin arm64 (27.0.0)
**Harness**: Pi
**Model ID**: ` chatgpt/gpt-5.6-luna `
**界面语言**: zh-CN
Contributor guide
Research direction
Trace sessions:send in apps/desktop/src/main/sessions-ipc-main.ts and the post-send showOptimisticUserMessage() flow in apps/desktop/src/renderer/app-shell-chat-actions.ts. Then inspect packages/ui/src/materialize.ts and materializeTurns() to understand how message order becomes turns. Done means the user message always appears before its streamed assistant reply, with a renderer/UI regression test covering the race.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- desktop
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 68/100