-
Notifications
You must be signed in to change notification settings - Fork 2.5k
fix: resolve message editor data loss via race condition fix and autosave draft #8438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: resolve message editor data loss via race condition fix and autosave draft #8438
Conversation
- Fix critical data loss bug when VSCode webview reloads - Add useAutosaveDraft hook with localStorage persistence - Implement debounced saving to prevent excessive writes - Add conversation isolation using task/conversation IDs - Include comprehensive error handling for storage limitations - Add 95%+ test coverage with unit and integration tests Technical details: - Hook provides draftContent, updateDraft, clearDraft, hasInitialDraft - Uses 300ms debounce for optimal UX performance - Gracefully handles localStorage errors and quota limits - Automatically clears drafts after successful message sends - Maintains backward compatibility with existing ChatView behavior Tests: - Unit tests: webview-ui/src/hooks/__tests__/useAutosaveDraft.test.tsx - Integration tests: webview-ui/src/components/chat/__tests__/ChatView.autosave.test.tsx - Tests demonstrate bug reproduction and fix verification Resolves the message editor data loss issue described in architectural analysis.
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)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Review summary: Solid fix for the race condition and a useful autosave hook. A few items to address before merge: P1 stale-closure risks around clearDraft in callbacks; P2 cleanup for unused API/local and queued images UX; P3 debounce timer edge-case on conversation switches. Inline comments point to exact spots.
- Remove unused imports (React, useState, useEffect, waitFor) - Remove unused clearOnSubmit parameter from useAutosaveDraft - Prefix hasInitialDraft with _ to indicate intentionally unused - Fix useCallback dependency arrays in ChatView - Fix TypeScript error in ChatView.autosave.test.tsx
P1 fixes: - clearDraft already in dependency arrays (verified) P2 fixes: - Clear selectedImages after queueing to prevent image duplication P3 fixes: - Add cleanup for pending debounce timers when storageKey changes in useAutosaveDraft
Summary
This PR addresses issue #8236 (draft persistence across reloads) and issue #8033 (message queue data loss) by fixing a race condition in the message sending logic and adding a draft autosave mechanism.
Problem Analysis
Previous Attempts
Two previous PRs (#8237 and #8034) attempted to solve related issues but were closed. Our analysis identified that the core issue is a race condition in the
handleSendMessagefunction.Root Cause
When a user sends a message while
sendingDisabled=true(e.g., AI is processing), the input is cleared immediately before the message is queued:Solution
Part 1: Race Condition Fix
Modified
handleSendMessageinChatView.tsxto defer draft clearing:Draft is now cleared only when message is successfully sent or chat is reset.
Part 2: Autosave Draft Hook
Implemented
useAutosaveDrafthook providing:safeLocalStorageTesting
Comprehensive test suite includes:
useAutosaveDrafthookChatViewautosave behaviorChatView.raceCondition.spec.tsxAll existing tests pass (3833+ tests), TypeScript compilation successful, linting clean.
Key Differences from Previous Approaches
Our implementation differs from previous PRs in these areas:
Architectural Documentation
Technical analysis available at:
C:/dev/roo-extensions/docs/roo-code/ARCHITECTURE-HOOK-AUTOSAVE-DRAFT.mdKnown Limitations
Currently text-only. Image support can be added in a follow-up PR.
Related Issues
Closes #8236
Closes #8033
Migration Notes
No breaking changes. The fix is transparent to users.
Changeset
Changeset included for changelog.
Important
Fixes message editor data loss by addressing race condition and adding autosave draft feature.
handleSendMessageinChatView.tsxby deferring draft clearing until message processing.useAutosaveDrafthook for draft persistence with localStorage, 100ms debounce, and error handling.useAutosaveDraftinuseAutosaveDraft.test.tsx.ChatView.autosave.test.tsx.ChatView.raceCondition.spec.tsx.ChatView.tsxto useuseAutosaveDraftfor input management.This description was created by
for dda5c8c. You can customize this summary. It will automatically update as commits are pushed.