-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix: prevent race condition from deleting wrong API messages #10113
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
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…essage deletion When deleting a user message from chat history, assistant API messages were incorrectly removed due to a timestamp race condition. During async tool execution, a clineMessage (e.g., user_feedback) could be created BEFORE the assistant API message was added to history. The fix finds the first API user message at or after the cutoff timestamp and uses that as the actual boundary, ensuring assistant messages that logically preceded the user's response are preserved. Added 4 test cases covering: - Race condition scenario - Normal timestamp ordering - Fallback when no user message found - Multiple assistant messages in race condition
Contributor
Re-review complete. No new issues found in the latest changes; the existing follow-up item remains.
Mention @roomote in a comment to request specific changes to this pull request or fix all unresolved issues. |
…lineMessage Adds handling for a second race condition scenario where the API user message (tool_result) timestamp is BEFORE the clineMessage timestamp. This can happen when the tool_result is logged before the user_feedback clineMessage due to timing. The fix: 1. Detects the inverse race pattern by finding the last assistant message before the cutoff and checking if there are user messages between that assistant and the cutoff 2. When detected, uses the last assistant timestamp + 1 as the cutoff to ensure we keep the assistant response but remove the user message that belongs to the turn being deleted Added test case demonstrating the inverse race condition scenario.
…before clineMessage" This reverts commit acc5567.
daniel-lxs
approved these changes
Dec 16, 2025
cte
approved these changes
Dec 16, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
bug
Something isn't working
Issue/PR - Triage
New issue. Needs quick review to confirm validity and assign labels.
lgtm
This PR has been approved by a maintainer
size:L
This PR changes 100-499 lines, ignoring generated files.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a race condition bug where deleting a user message from chat history incorrectly removed assistant API messages that logically preceded the deleted message.
Problem
When deleting a user_feedback clineMessage, the system was using that message's timestamp as the cutoff for filtering API messages. Due to async execution during streaming (tool execution happens concurrently with stream completion), a clineMessage could have a timestamp BEFORE the corresponding assistant API message. This caused the assistant message to be incorrectly removed.
Timeline Example (Bug Scenario)
With the old logic (
ts < 100), both API messages at T2 and T3 were removed.Solution
Modified
src/core/message-manager/index.tsto detect the race condition scenario by checking:When both conditions are true (indicating a race condition), the fix finds the first API user message at or after the cutoff and uses that as the actual boundary. This ensures assistant messages that logically preceded the user's response are preserved.
Changes
src/core/message-manager/index.ts- Added race condition detection and handling intruncateApiHistoryWithCleanupsrc/core/message-manager/index.spec.ts- Added 4 new test cases covering:Important
Fixes race condition in message deletion logic by adjusting cutoff timestamp handling in
truncateApiHistoryWithCleanupto ensure correct message preservation.truncateApiHistoryWithCleanupinindex.tsby adjusting cutoff timestamp logic to preserve assistant messages whenclineMessagetimestamp is earlier.index.spec.tsfor race condition handling, normal timestamp alignment, fallback when no user message at or after cutoff, and multiple assistant messages in race condition.This description was created by
for f6096b1. You can customize this summary. It will automatically update as commits are pushed.