Skip to content

fix(gui): prevent message flicker and data loss from race conditions - #59

Merged
huichen merged 1 commit into
mainfrom
fix/gui-message-race-condition
Jul 28, 2026
Merged

huichen merged 1 commit into
mainfrom
fix/gui-message-race-condition

Conversation

@huichen

@huichen huichen commented Jul 28, 2026 •

Copy link
Copy Markdown
Collaborator

问题

  1. 回切 streaming 会话闪烁 — 缓存后台刷新用 setMessages(restored) 直接替换,覆盖了 useRunReattach 插入的流式气泡,导致最后一条 AI 消息消失-出现-消失-出现。
  2. 实时用户消息被覆盖 — 首次加载 / reloadMessagesQuiet 在 await 期间,future:agent-event 监听器插入的 user message 会被加载完成后的直接替换覆盖丢失。
  3. mid-run partial snapshot 重复渲染 — 流式气泡活跃时,agent save_callback 持久化的同 turn 中间快照(无 runId 的 assistant 消息)会与气泡并排渲染成两条。

改动

1. Generation counter (messagesGenRef)

在所有 messages 写入方之间共享版本号:

  • 直接替换(首次加载、reloadMessagesQuiet)先 snapshot gen,await 后检查 gen 未变才写入;变了说明期间有函数式更新写入,丢弃 stale 快照
  • 函数式更新(real-time user message 事件)写入后 bump gen
  • Settle 路径 reloadMessagesQuiet(threadId, true) 传 force 跳过 gen 检查 — 此时 streaming interval 已停止,持久化结果是权威的

2. 后台刷新 merge 改为函数式更新器

缓存命中后台刷新不再用 setMessages(restored) 直接替换,改为:

  • 保留未 settled 的流式气泡(stream_<runId> 且其 runId 未出现在 restored 中),追加在持久化历史之后
  • 气泡活跃时对 restored 执行 dropInFlightSnapshot(),剔掉同 turn 的无 runId assistant 中间快照,避免重复渲染

3. Entry count 跳过投影

ThreadCacheEntry 记录 raw entry 数量。后台刷新时若 entryCount 未变,跳过整个投影+merge — 流式气泡已由 useRunReattach 正常渲染。

4. 过滤 mid-run partial snapshot

dropInFlightSnapshot() — 定位最后一条 user 消息之后、无 runId 且非 compaction divider 的 assistant 消息并剔除。

文件

文件 改动
useThreadMessages.ts Gen counter + entry count skip + 函数式 merge + dropInFlightSnapshot
useRunReattach.ts 接收 messagesGenRef,settle 时 force 重载
useAgentThreadState.ts 透传 messagesGenRef

Add a generation counter (messagesGenRef) shared across concurrent
message writers to protect against overwrites between:

- Cache background refresh (direct replacement) vs streaming upserts
  (useRunReattach functional updater) — preserves live bubbles via merge
  instead of clobbering them, then skips the full projection+merge when
  the raw entry count is unchanged.

- First load / reloadMessagesQuiet (direct replacement) vs real-time
  user-message events (future:agent-event functional updater) — bumps
  gen after each event write so in-flight loads see the change and
  discard their stale snapshot.

- reloadMessagesQuiet settle path (useRunReattach settle effect) uses
  force=true to skip gen checks since the streaming interval has
  stopped and the persisted reply is authoritative.

Also filters mid-run partial snapshots (save_callback entries without
a runId) from the restored array when a streaming bubble is active,
preventing duplicate rendering of the same assistant turn.
@huichen
huichen force-pushed the fix/gui-message-race-condition branch 2 times, most recently from 699afdb to af7f9b6 Compare July 28, 2026 05:39
@huichen
huichen merged commit d9f7c58 into main Jul 28, 2026
27 checks passed
@huichen
huichen deleted the fix/gui-message-race-condition branch July 28, 2026 05:42
huichen added a commit that referenced this pull request Sep 27, 2026
…0) (#879)

#877 (coverage) carried a stale gitlink and reverted the pin #872 had
just moved, so `main` pointed at 1ece355 while the skills repo had
moved on. Restore the pin to 23925fc (future-skills main), which is
the same content #872 shipped.

Skills repo commits covered by the range 1ece355..23925fc:
- #57 document task write scopes and safe dependency wiring
- #59 repair builtin lookup/design helpers, retire unsafe review generator
- #58 future-slides defaults to editable decks with native validation
- light-weight code-development skill (future-code)
- #60 coalesced supervisor delivery for future-loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant