Skip to content

Commit dda5c8c

Browse files
committed
fix: resolve race condition in message editor
Fixes data loss when user sends message concurrently with tool response. Problem: When a tool response arrives (sendingDisabled=true) while user is typing, clicking Send queues the message but immediately calls clearDraft(), causing the input to be wiped before processing. Solution: - Remove clearDraft() call when message is queued (sendingDisabled path) - Reduce autosave debounce from 300ms to 100ms - Message remains visible until properly processed Changes: - webview-ui/src/components/chat/ChatView.tsx - webview-ui/src/hooks/useAutosaveDraft.ts - webview-ui/src/components/chat/__tests__/ChatView.raceCondition.spec.tsx Tests: 10 unit tests covering bug reproduction, fix verification, and autosave interaction (all passing)
1 parent c944549 commit dda5c8c

File tree

4 files changed

+608
-7
lines changed

4 files changed

+608
-7
lines changed

.changeset/fix-message-editor-autosave-draft.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,9 @@
22
"roo-cline": patch
33
---
44

5-
Fix critical data loss bug in message editor by implementing autosave draft functionality with localStorage persistence. Messages are now automatically saved as users type and restored after webview reloads, with proper conversation isolation and debouncing for optimal performance.
5+
Fix critical data loss bug in message editor caused by race condition when sending messages while tool responses arrive. This fix includes two complementary solutions:
6+
7+
1. **Race Condition Fix**: Modified message sending logic to preserve user input when messages are queued but not yet sent, preventing data loss during concurrent state updates
8+
2. **Autosave Draft**: Implemented localStorage-based draft persistence with automatic save/restore on webview reloads, conversation isolation, and optimized 100ms debouncing
9+
10+
Both fixes work together to ensure user input is never lost, whether from race conditions or webview reloads.

webview-ui/src/components/chat/ChatView.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ const ChatViewComponent: React.ForwardRefRenderFunction<ChatViewRef, ChatViewPro
180180
hasInitialDraft,
181181
} = useAutosaveDraft({
182182
key: currentTaskItem?.id || "default",
183-
debounceMs: 300,
183+
debounceMs: 100,
184184
clearOnSubmit: true,
185185
})
186186
const inputValueRef = useRef(inputValue)
@@ -609,8 +609,12 @@ const ChatViewComponent: React.ForwardRefRenderFunction<ChatViewRef, ChatViewPro
609609
try {
610610
console.log("queueMessage", text, images)
611611
vscode.postMessage({ type: "queueMessage", text, images })
612-
clearDraft()
613-
setSelectedImages([])
612+
// RACE CONDITION FIX: Do NOT clear draft immediately when queueing
613+
// The message is queued, not sent yet. Keep the draft visible until
614+
// the system processes the queue. This prevents data loss if user
615+
// typed quickly (<100ms) or if concurrent tool response arrived.
616+
// The draft will be cleared when the queued message is actually processed.
617+
// Note: setSelectedImages is also kept to maintain the message state
614618
} catch (error) {
615619
console.error(
616620
`Failed to queue message: ${error instanceof Error ? error.message : String(error)}`,

0 commit comments

Comments
 (0)