Skip to content

Commit f7585ee

Browse files
committed
fix(cursor): compact and normalize Computer Use / node_repl tool results (#1866)
- Compact large Computer Use and node_repl tool result payloads (e.g. AXTree, base64 screenshots) under external replay byte limits. - Preserve key UI context (window title, focused elements, URL) and strip bulk base64 image data from wire replays. - Normalize empty outer exec wrapper outputs and known runtime errors (SkyComputerUseError, node_repl variable redeclarations, missing sky globals) into actionable tool errors with recovery guidance. - Guard active trailing tool results from being completely dropped under constrained external replay budgets. - Add comprehensive regression tests in tests/cursor-computer-use-replay.test.ts.
1 parent 1208bd2 commit f7585ee

4 files changed

Lines changed: 611 additions & 28 deletions

File tree

‎src/adapters/cursor/protobuf-request.ts‎

Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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

5560
const encoder = new TextEncoder();
5661
const decoder = new TextDecoder();
@@ -122,7 +127,19 @@ function rootBlobCandidate(
122127
function 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

335375
function 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

346379
function argBytes(value: unknown): Uint8Array {
@@ -382,13 +415,14 @@ function toolCallStep(
382415
}
383416

384417
function 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
}

‎src/adapters/cursor/request-builder.ts‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
isCursorWaitTool,
2626
} from "./tool-definitions";
2727
import { lookupCursorThreadConversation } from "./thread-continuity";
28+
import { formatToolResultToWireText } from "./tool-result-compaction";
2829

2930
/** Probe-verified Cursor Connect boundaries, with byte headroom for the enclosing field. */
3031
export const CURSOR_TOOL_COUNT_LIMIT = 330;
@@ -216,14 +217,7 @@ function contentPartToText(part: OcxContentPart | OcxAssistantContentPart): stri
216217
}
217218

218219
function toolResultToText(message: OcxToolResultMessage): string {
219-
return [
220-
"[tool_result]",
221-
`call_id: ${message.toolCallId}`,
222-
`name: ${namespacedToolName(message.toolNamespace, message.toolName)}`,
223-
`is_error: ${message.isError}`,
224-
"output:",
225-
contentToText(message.content),
226-
].join("\n");
220+
return formatToolResultToWireText(message).wireOutput;
227221
}
228222

229223
function contentToText(content: string | readonly (OcxContentPart | OcxAssistantContentPart)[]): string {

0 commit comments

Comments
 (0)