-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Fix: Orphaned Partial Ask Messages // Message Guard #3514
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
Open
catrielmuller
wants to merge
2
commits into
main
Choose a base branch
from
catrielmuller/fix-partial-ask-msg-reviewed
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+463
−22
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| /** | ||
| * Utility functions for message handling in Task | ||
| * kilocode_change - Created to fix orphaned partial ask messages bug | ||
| */ | ||
|
|
||
| import pWaitFor from "p-wait-for" | ||
| import type { ClineMessage, ClineAsk, ClineSay } from "@roo-code/types" | ||
|
|
||
| const GUARD_TIMEOUT = 30 * 1000 // 30 seconds | ||
| const GUARD_INTERVAL = 50 // 50 milliseconds | ||
|
|
||
| /** | ||
| * Message insertion guard to prevent concurrent message insertions. | ||
| * This prevents race conditions where checkpoint_saved messages can arrive | ||
| * during partial message updates, breaking message history order. | ||
| */ | ||
| export class MessageInsertionGuard { | ||
| private isInserting = false | ||
| private readonly timeout: number | ||
| private readonly interval: number | ||
|
|
||
| constructor(timeout = GUARD_TIMEOUT, interval = GUARD_INTERVAL) { | ||
| this.timeout = timeout | ||
| this.interval = interval | ||
| } | ||
|
|
||
| /** | ||
| * Wait for any in-flight message insertions to complete before proceeding. | ||
| * This ensures messages are inserted sequentially and prevents race conditions. | ||
| */ | ||
| async waitForClearance(): Promise<void> { | ||
| await pWaitFor(() => !this.isInserting, { | ||
| interval: this.interval, | ||
| timeout: this.timeout, | ||
| }) | ||
| } | ||
|
|
||
| /** | ||
| * Acquire the insertion lock. Must be released with release() after insertion completes. | ||
| */ | ||
| acquire(): void { | ||
| this.isInserting = true | ||
| } | ||
|
|
||
| /** | ||
| * Release the insertion lock, allowing other insertions to proceed. | ||
| */ | ||
| release(): void { | ||
| this.isInserting = false | ||
| } | ||
|
|
||
| /** | ||
| * Check if a message insertion is currently in progress. | ||
| */ | ||
| isLocked(): boolean { | ||
| return this.isInserting | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Search backwards through messages to find the most recent partial ask message | ||
| * of the specified type. This handles cases where non-interactive messages | ||
| * (like checkpoint_saved) are inserted between partial start and completion. | ||
| * | ||
| * @param messages - Array of Cline messages to search | ||
| * @param type - The ask type to search for | ||
| * @returns The partial message and its index, or undefined if not found | ||
| */ | ||
| export function findPartialAskMessage( | ||
| messages: ClineMessage[], | ||
| type: ClineAsk, | ||
| ): { message: ClineMessage; index: number } | undefined { | ||
| for (let i = messages.length - 1; i >= 0; i--) { | ||
| const msg = messages[i] | ||
| if (msg.type === "ask" && msg.ask === type && msg.partial === true) { | ||
| return { message: msg, index: i } | ||
| } | ||
| } | ||
| return undefined | ||
| } | ||
|
|
||
| /** | ||
| * Search backwards through messages to find the most recent partial say message | ||
| * of the specified type. Similar to findPartialAskMessage but for say messages. | ||
| * | ||
| * @param messages - Array of Cline messages to search | ||
| * @param type - The say type to search for | ||
| * @returns The partial message and its index, or undefined if not found | ||
| */ | ||
| export function findPartialSayMessage( | ||
| messages: ClineMessage[], | ||
| type: ClineSay, | ||
| ): { message: ClineMessage; index: number } | undefined { | ||
| for (let i = messages.length - 1; i >= 0; i--) { | ||
| const msg = messages[i] | ||
| if (msg.type === "say" && msg.say === type && msg.partial === true) { | ||
| return { message: msg, index: i } | ||
| } | ||
| } | ||
| return undefined | ||
| } |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.