@@ -84,6 +84,17 @@ export const webviewMessageHandler = async (
8484 return { messageIndex, apiConversationHistoryIndex }
8585 }
8686
87+ /**
88+ * Fallback: find first API history index at or after a timestamp.
89+ * Used when the exact user message isn't present in apiConversationHistory (e.g., after condense).
90+ */
91+ const findFirstApiIndexAtOrAfter = ( ts : number , currentCline : any ) => {
92+ if ( typeof ts !== "number" ) return - 1
93+ return currentCline . apiConversationHistory . findIndex (
94+ ( msg : ApiMessage ) => typeof msg ?. ts === "number" && ( msg . ts as number ) >= ts ,
95+ )
96+ }
97+
8798 /**
8899 * Removes the target message and all subsequent messages
89100 */
@@ -144,6 +155,12 @@ export const webviewMessageHandler = async (
144155 }
145156
146157 const { messageIndex, apiConversationHistoryIndex } = findMessageIndices ( messageTs , currentCline )
158+ // Determine API truncation index with timestamp fallback if exact match not found
159+ let apiIndexToUse = apiConversationHistoryIndex
160+ const tsThreshold = currentCline . clineMessages [ messageIndex ] ?. ts
161+ if ( apiIndexToUse === - 1 && typeof tsThreshold === "number" ) {
162+ apiIndexToUse = findFirstApiIndexAtOrAfter ( tsThreshold , currentCline )
163+ }
147164
148165 if ( messageIndex === - 1 ) {
149166 const errorMessage = `Message with timestamp ${ messageTs } not found`
@@ -189,7 +206,7 @@ export const webviewMessageHandler = async (
189206 }
190207
191208 // Delete this message and all subsequent messages
192- await removeMessagesThisAndSubsequent ( currentCline , messageIndex , apiConversationHistoryIndex )
209+ await removeMessagesThisAndSubsequent ( currentCline , messageIndex , apiIndexToUse )
193210
194211 // Restore checkpoint associations for preserved messages
195212 for ( const [ ts , checkpoint ] of preservedCheckpoints ) {
@@ -336,6 +353,14 @@ export const webviewMessageHandler = async (
336353 }
337354 }
338355
356+ // Timestamp fallback for API history when exact user message isn't present
357+ if ( deleteFromApiIndex === - 1 ) {
358+ const tsThresholdForEdit = currentCline . clineMessages [ deleteFromMessageIndex ] ?. ts
359+ if ( typeof tsThresholdForEdit === "number" ) {
360+ deleteFromApiIndex = findFirstApiIndexAtOrAfter ( tsThresholdForEdit , currentCline )
361+ }
362+ }
363+
339364 // Store checkpoints from messages that will be preserved
340365 const preservedCheckpoints = new Map < number , any > ( )
341366 for ( let i = 0 ; i < deleteFromMessageIndex ; i ++ ) {
@@ -366,9 +391,7 @@ export const webviewMessageHandler = async (
366391 // Update the UI to reflect the deletion
367392 await provider . postStateToWebview ( )
368393
369- // Immediately send the edited message as a new user message
370- // This restarts the conversation from the edited point
371- await currentCline . handleWebviewAskResponse ( "messageResponse" , editedContent , images )
394+ await currentCline . submitUserMessage ( editedContent , images )
372395 } catch ( error ) {
373396 console . error ( "Error in edit message:" , error )
374397 vscode . window . showErrorMessage (
0 commit comments