mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-07-21 13:38:05 +00:00
After SummarizationMiddleware runs, the merged conversation view could drop already-displayed messages (previous assistant output, current user input), leaving a nearly-empty thread. Root cause: the display merge combines `visibleHistory` (archived history, a React `useState` in useThreadHistory) with `persistedMessages` (live thread, the LangGraph SDK external store via useSyncExternalStore). On summarization the backend removes every live message and onUpdateEvent re-archives them via an async `appendMessages` setState. Those two state systems are scheduled independently, so a render can observe the post-summary (shrunk) thread before the archive setState commits — the rescued messages are then absent from BOTH merge inputs and get dropped. Fix: bridge the async gap with a synchronous `pendingArchivedMessagesRef` buffer written the moment onUpdateEvent computes the moved messages and read by the merge on every render, so correctness no longer depends on how the two channels interleave. The buffer drains once history confirms absorption and only injects messages missing from history (live copies stay authoritative, order preserved). It is tagged with the thread it was captured from and the merge overlays it only when that matches the viewed `threadId` (the same prop visibleHistory is gated on), so it can never leak into another thread or the new-chat screen — a read-only check, no render-phase ref mutation. Extracts the moved-message derivation and the merge overlay into pure, unit-tested helpers (computeSummarizationMovedMessages, resolvePreservedHistory, pruneConfirmedArchivedMessages) with regression coverage for the full rescue pipeline.