@@ -51,6 +51,11 @@ import {
5151 CURSOR_SHELL_ALIAS_SYSTEM_NOTE ,
5252 OCX_RESPONSES_TOOL_PROVIDER ,
5353} from "./tool-definitions" ;
54+ import {
55+ CURSOR_TRUNCATION_MARKER ,
56+ compactComputerUsePayload ,
57+ formatToolResultToWireText ,
58+ } from "./tool-result-compaction" ;
5459
5560const encoder = new TextEncoder ( ) ;
5661const decoder = new TextDecoder ( ) ;
@@ -122,7 +127,19 @@ function rootBlobCandidate(
122127function truncateToolResultBlob ( entry : RootBlobCandidate , maxBytes : number ) : RootBlobCandidate | null {
123128 if ( entry . byteLength <= maxBytes ) return entry ;
124129 if ( entry . role !== "toolResult" || entry . text === undefined ) return null ;
125- const marker = "\n…[truncated for Cursor external replay budget]" ;
130+ const marker = CURSOR_TRUNCATION_MARKER ;
131+
132+ // 1. Try structured compaction first for Computer Use / large payloads
133+ const compacted = compactComputerUsePayload ( entry . text , maxBytes ) ;
134+ if ( compacted !== entry . text ) {
135+ const candidate = rootBlobCandidate (
136+ { role : "user" , content : [ { type : "text" , text : compacted } ] } ,
137+ "toolResult" ,
138+ { messageIndex : entry . messageIndex , text : compacted } ,
139+ ) ;
140+ if ( candidate . byteLength <= maxBytes ) return candidate ;
141+ }
142+
126143 const encoded = encoder . encode ( entry . text ) ;
127144 // Leave headroom for JSON envelope (`role`/`content` wrapper) around the truncated text.
128145 let keepBytes = Math . min ( encoded . byteLength , Math . max ( 0 , maxBytes - encoder . encode ( marker ) . byteLength - 96 ) ) ;
@@ -226,8 +243,9 @@ function rootPromptMessages(request: CursorRunRequest, requestScope: CursorBlobR
226243 }
227244 // Assistant tool CALLS are intentionally NOT replayed as visible "[Tool Call]" text here.
228245 } else if ( message . role === "toolResult" ) {
229- const prefix = message . isError ? "[Tool Error]" : "[Tool Result]" ;
230- const text = `${ prefix } \n${ toolResultToText ( message ) } ` ;
246+ const { wireOutput, isError } = formatToolResultToWireText ( message , { maxBytes : CURSOR_EXTERNAL_ROOT_BYTE_LIMIT } ) ;
247+ const prefix = isError ? "[Tool Error]" : "[Tool Result]" ;
248+ const text = `${ prefix } \n${ wireOutput } ` ;
231249 entries . push ( rootBlobCandidate (
232250 { role : "user" , content : [ { type : "text" , text } ] } ,
233251 "toolResult" ,
@@ -246,8 +264,8 @@ function rootPromptMessages(request: CursorRunRequest, requestScope: CursorBlobR
246264 const historyBudget = Math . max ( 0 , CURSOR_EXTERNAL_ROOT_BYTE_LIMIT - systemBytes ) ;
247265
248266 // Retain the active trailing tool-result block when it fits (may truncate text).
249- // If even a truncation marker cannot fit the remaining budget, omit it rather than
250- // emitting an oversized root blob .
267+ // If even a truncation marker cannot fit the remaining budget, retain a minimal marker
268+ // rather than dropping the active tool result completely (#1866) .
251269 let activeStart = history . length ;
252270 while ( activeStart > 0 && history [ activeStart - 1 ] ?. role === "toolResult" ) activeStart -= 1 ;
253271 const active = history
@@ -265,8 +283,30 @@ function rootPromptMessages(request: CursorRunRequest, requestScope: CursorBlobR
265283 active [ 0 ] = truncated ;
266284 activeBytes = truncated . byteLength ;
267285 } else {
268- active . length = 0 ;
269- activeBytes = 0 ;
286+ const minimal = rootBlobCandidate (
287+ { role : "user" , content : [ { type : "text" , text : `[Tool Result]\n${ CURSOR_TRUNCATION_MARKER . trimStart ( ) } ` } ] } ,
288+ "toolResult" ,
289+ { messageIndex : active [ 0 ] . messageIndex , text : `[Tool Result]\n${ CURSOR_TRUNCATION_MARKER . trimStart ( ) } ` } ,
290+ ) ;
291+ if ( minimal . byteLength <= historyBudget ) {
292+ active [ 0 ] = minimal ;
293+ activeBytes = minimal . byteLength ;
294+ } else {
295+ active . length = 0 ;
296+ activeBytes = 0 ;
297+ }
298+ }
299+ } else if ( active . length === 0 && history . length > activeStart ) {
300+ const lastActive = history [ history . length - 1 ] ;
301+ if ( lastActive ) {
302+ const minimal = rootBlobCandidate (
303+ { role : "user" , content : [ { type : "text" , text : `[Tool Result]\n${ CURSOR_TRUNCATION_MARKER . trimStart ( ) } ` } ] } ,
304+ "toolResult" ,
305+ { messageIndex : lastActive . messageIndex , text : `[Tool Result]\n${ CURSOR_TRUNCATION_MARKER . trimStart ( ) } ` } ,
306+ ) ;
307+ if ( minimal . byteLength <= historyBudget ) {
308+ active . push ( minimal ) ;
309+ }
270310 }
271311 }
272312
@@ -333,14 +373,7 @@ function contentToText(content: OcxToolResultMessage["content"]): string {
333373}
334374
335375function toolResultToText ( message : OcxToolResultMessage ) : string {
336- return [
337- "[tool_result]" ,
338- `call_id: ${ message . toolCallId } ` ,
339- `name: ${ namespacedToolName ( message . toolNamespace , message . toolName ) } ` ,
340- `is_error: ${ message . isError } ` ,
341- "output:" ,
342- contentToText ( message . content ) ,
343- ] . join ( "\n" ) ;
376+ return formatToolResultToWireText ( message , { maxBytes : CURSOR_EXTERNAL_ROOT_BYTE_LIMIT } ) . wireOutput ;
344377}
345378
346379function argBytes ( value : unknown ) : Uint8Array {
@@ -382,13 +415,14 @@ function toolCallStep(
382415}
383416
384417function toolResultPart ( message : OcxToolResultMessage ) {
418+ const formatted = formatToolResultToWireText ( message , { maxBytes : CURSOR_EXTERNAL_ROOT_BYTE_LIMIT } ) ;
385419 return create ( McpToolResultSchema , {
386420 result : {
387421 case : "success" ,
388422 value : create ( McpSuccessSchema , {
389- isError : message . isError ,
423+ isError : formatted . isError ,
390424 content : [ create ( McpToolResultContentItemSchema , {
391- content : { case : "text" , value : create ( McpTextContentSchema , { text : contentToText ( message . content ) } ) } ,
425+ content : { case : "text" , value : create ( McpTextContentSchema , { text : formatted . text } ) } ,
392426 } ) ] ,
393427 } ) ,
394428 } ,
@@ -480,12 +514,13 @@ function conversationTurns(
480514 }
481515 if ( message . role === "toolResult" ) {
482516 if ( ! current ) continue ;
517+ const formatted = formatToolResultToWireText ( message , { maxBytes : CURSOR_EXTERNAL_ROOT_BYTE_LIMIT } ) ;
483518 if ( externalModel ) {
484- const prefix = message . isError ? "[Tool Error]" : "[Tool Result]" ;
519+ const prefix = formatted . isError ? "[Tool Error]" : "[Tool Result]" ;
485520 current . steps . push ( storeCursorBlob ( toBinary ( ConversationStepSchema , create ( ConversationStepSchema , {
486521 message : {
487522 case : "assistantMessage" ,
488- value : create ( AssistantMessageSchema , { text : `${ prefix } \n${ contentToText ( message . content ) } ` } ) ,
523+ value : create ( AssistantMessageSchema , { text : `${ prefix } \n${ formatted . text } ` } ) ,
489524 } ,
490525 } ) ) , requestScope ) ) ;
491526 continue ;
@@ -498,7 +533,7 @@ function conversationTurns(
498533 current . steps . push ( storeCursorBlob ( toBinary ( ConversationStepSchema , create ( ConversationStepSchema , {
499534 message : {
500535 case : "assistantMessage" ,
501- value : create ( AssistantMessageSchema , { text : toolResultToText ( message ) } ) ,
536+ value : create ( AssistantMessageSchema , { text : formatted . wireOutput } ) ,
502537 } ,
503538 } ) ) , requestScope ) ) ;
504539 }
0 commit comments