@@ -112,7 +112,7 @@ import { type AssistantMessageContent, presentAssistantMessage } from "../assist
112112import { DispatchState , type PendingWork } from "./dispatchState"
113113import { type RequestPolicySnapshot } from "../prompts/tools/effective-tool-policy"
114114import { NativeToolCallParser } from "../assistant-message/NativeToolCallParser"
115- import { manageContext , willManageContext } from "../context-management"
115+ import { type ContextManagementResult , manageContext , willManageContext } from "../context-management"
116116import { ClineProvider } from "../webview/ClineProvider"
117117import { MultiSearchReplaceDiffStrategy } from "../diff/strategies/multi-search-replace"
118118import {
@@ -5430,6 +5430,56 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
54305430 return this . api . getModel ( ) . info
54315431 }
54325432
5433+ /**
5434+ * Report a manageContext result to the webview. Shared by the automatic and emergency
5435+ * condense paths so both emit the same events: a surfaced error, and a condense event
5436+ * carrying the condenseId that rewind cleanup uses to remove the matching summary.
5437+ */
5438+ private async reportContextManagementResult ( result : ContextManagementResult ) : Promise < void > {
5439+ if ( result . error ) {
5440+ await this . say ( "condense_context_error" , result . error )
5441+ }
5442+ if ( result . summary ) {
5443+ const { summary, cost, prevContextTokens, newContextTokens = 0 , condenseId } = result
5444+ const contextCondense : ContextCondense = {
5445+ summary,
5446+ cost,
5447+ newContextTokens,
5448+ prevContextTokens,
5449+ condenseId,
5450+ }
5451+ await this . say (
5452+ "condense_context" ,
5453+ undefined /* text */ ,
5454+ undefined /* images */ ,
5455+ false /* partial */ ,
5456+ undefined /* checkpoint */ ,
5457+ undefined /* progressStatus */ ,
5458+ { isNonInteractive : true } /* options */ ,
5459+ contextCondense ,
5460+ )
5461+ } else if ( result . truncationId ) {
5462+ // Sliding window truncation occurred (fallback when condensing fails or is disabled)
5463+ const contextTruncation : ContextTruncation = {
5464+ truncationId : result . truncationId ,
5465+ messagesRemoved : result . messagesRemoved ?? 0 ,
5466+ prevContextTokens : result . prevContextTokens ,
5467+ newContextTokens : result . newContextTokensAfterTruncation ?? 0 ,
5468+ }
5469+ await this . say (
5470+ "sliding_window_truncation" ,
5471+ undefined /* text */ ,
5472+ undefined /* images */ ,
5473+ false /* partial */ ,
5474+ undefined /* checkpoint */ ,
5475+ undefined /* progressStatus */ ,
5476+ { isNonInteractive : true } /* options */ ,
5477+ undefined /* contextCondense */ ,
5478+ contextTruncation ,
5479+ )
5480+ }
5481+ }
5482+
54335483 private async handleContextWindowExceededError ( requestModelInfo : ModelInfo ) : Promise < void > {
54345484 const state = await this . providerRef . deref ( ) ?. getState ( )
54355485 const { profileThresholds = { } } = state ?? { }
@@ -5506,6 +5556,7 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
55065556 try {
55075557 // Generate environment details to include in the condensed summary
55085558 const environmentDetails = await getEnvironmentDetails ( this , true )
5559+ const filesReadByRoo = await this . getFilesReadByRooSafely ( "handleContextWindowExceededError" )
55095560
55105561 // Force aggressive truncation by keeping only 75% of the conversation history
55115562 const truncateResult = await manageContext ( {
@@ -5518,50 +5569,22 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
55185569 autoCondenseContextPercent : FORCED_CONTEXT_REDUCTION_PERCENT ,
55195570 systemPrompt : await this . getSystemPrompt ( state , modelInfo ) ,
55205571 taskId : this . taskId ,
5572+ // Recovery must summarize like the automatic path: same prompt and folded file context.
5573+ customCondensingPrompt : state ?. customSupportPrompts ?. CONDENSE ,
55215574 profileThresholds,
55225575 currentProfileId,
55235576 metadata,
55245577 environmentDetails,
5578+ filesReadByRoo,
5579+ cwd : this . cwd ,
5580+ rooIgnoreController : this . rooIgnoreController ,
55255581 useAvailableInputForContextPercent,
55265582 } )
55275583
55285584 if ( truncateResult . messages !== this . apiConversationHistory ) {
55295585 await this . overwriteApiConversationHistory ( truncateResult . messages )
55305586 }
5531-
5532- if ( truncateResult . summary ) {
5533- const { summary, cost, prevContextTokens, newContextTokens = 0 } = truncateResult
5534- const contextCondense : ContextCondense = { summary, cost, newContextTokens, prevContextTokens }
5535- await this . say (
5536- "condense_context" ,
5537- undefined /* text */ ,
5538- undefined /* images */ ,
5539- false /* partial */ ,
5540- undefined /* checkpoint */ ,
5541- undefined /* progressStatus */ ,
5542- { isNonInteractive : true } /* options */ ,
5543- contextCondense ,
5544- )
5545- } else if ( truncateResult . truncationId ) {
5546- // Sliding window truncation occurred (fallback when condensing fails or is disabled)
5547- const contextTruncation : ContextTruncation = {
5548- truncationId : truncateResult . truncationId ,
5549- messagesRemoved : truncateResult . messagesRemoved ?? 0 ,
5550- prevContextTokens : truncateResult . prevContextTokens ,
5551- newContextTokens : truncateResult . newContextTokensAfterTruncation ?? 0 ,
5552- }
5553- await this . say (
5554- "sliding_window_truncation" ,
5555- undefined /* text */ ,
5556- undefined /* images */ ,
5557- false /* partial */ ,
5558- undefined /* checkpoint */ ,
5559- undefined /* progressStatus */ ,
5560- { isNonInteractive : true } /* options */ ,
5561- undefined /* contextCondense */ ,
5562- contextTruncation ,
5563- )
5564- }
5587+ await this . reportContextManagementResult ( truncateResult )
55655588 } finally {
55665589 // Notify webview that context management is complete (removes in-progress spinner)
55675590 // IMPORTANT: Must always be sent to dismiss the spinner, even on error
@@ -5818,48 +5841,7 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
58185841 if ( truncateResult . messages !== this . apiConversationHistory ) {
58195842 await this . overwriteApiConversationHistory ( truncateResult . messages )
58205843 }
5821- if ( truncateResult . error ) {
5822- await this . say ( "condense_context_error" , truncateResult . error )
5823- }
5824- if ( truncateResult . summary ) {
5825- const { summary, cost, prevContextTokens, newContextTokens = 0 , condenseId } = truncateResult
5826- const contextCondense : ContextCondense = {
5827- summary,
5828- cost,
5829- newContextTokens,
5830- prevContextTokens,
5831- condenseId,
5832- }
5833- await this . say (
5834- "condense_context" ,
5835- undefined /* text */ ,
5836- undefined /* images */ ,
5837- false /* partial */ ,
5838- undefined /* checkpoint */ ,
5839- undefined /* progressStatus */ ,
5840- { isNonInteractive : true } /* options */ ,
5841- contextCondense ,
5842- )
5843- } else if ( truncateResult . truncationId ) {
5844- // Sliding window truncation occurred (fallback when condensing fails or is disabled)
5845- const contextTruncation : ContextTruncation = {
5846- truncationId : truncateResult . truncationId ,
5847- messagesRemoved : truncateResult . messagesRemoved ?? 0 ,
5848- prevContextTokens : truncateResult . prevContextTokens ,
5849- newContextTokens : truncateResult . newContextTokensAfterTruncation ?? 0 ,
5850- }
5851- await this . say (
5852- "sliding_window_truncation" ,
5853- undefined /* text */ ,
5854- undefined /* images */ ,
5855- false /* partial */ ,
5856- undefined /* checkpoint */ ,
5857- undefined /* progressStatus */ ,
5858- { isNonInteractive : true } /* options */ ,
5859- undefined /* contextCondense */ ,
5860- contextTruncation ,
5861- )
5862- }
5844+ await this . reportContextManagementResult ( truncateResult )
58635845 } finally {
58645846 // Notify webview that context management is complete (sets isCondensing = false)
58655847 // This removes the in-progress spinner and allows the completed result to show
0 commit comments