Skip to content

Commit bf8e04b

Browse files
committed
fix(cursor): refund spare envelope bytes to clipped invocation arguments
The 2 KiB per-call cap on the arguments named inside a replayed tool-result envelope is charged while the envelope is still being built, so it cost a call 2 KiB whether or not anything else wanted those bytes. In a small replay nearly the whole 192-root / 512 KiB envelope went unused and the cap still bit: a 4,693-byte successful write_file lost its tail inside a 6,011-byte replay, and because the result text does not repeat the argument, the model could no longer see what it had just written. Add a second pass after the root set is assembled and before it is stored. It spends only leftover aggregate bytes, newest tool result first, skips a root whose own output was already elided, and never drops, shrinks or reorders a retained root. The cap itself is unchanged and still decides admission on its 2 KiB prefix, so a 600 KiB argument stays clipped rather than evicting the output it describes. The gate is echoToolResultInRoot, not externalModel: native composer-2.5 echoes results into roots without being an external wire model, so the narrower gate would have left the one native model with clipped invocation lines capped for no reason. The widening uses the callback form of String.prototype.replace, because serialized arguments routinely contain $&, $' and $1, which the string form would expand into the surrounding match. Closes #4516
1 parent a84e6e8 commit bf8e04b

3 files changed

Lines changed: 206 additions & 1 deletion

File tree

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

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,20 @@ function rootPromptMessages(
694694
historyMessageStart = firstKept?.messageIndex ?? (messages.length);
695695
}
696696

697+
// Refund envelope bytes the assembled set left unused to invocation arguments the per-call cap
698+
// clipped. Gated on `echoToolResultInRoot`, not `externalModel`: native `composer-2.5` echoes its
699+
// results into roots without being an external wire model, so the narrower gate would leave the one
700+
// native model that has clipped invocation lines capped for no reason (#4516).
701+
if (echoToolResultInRoot && replayedCalls) {
702+
selected = restoreClippedInvocationArguments(
703+
selected,
704+
messages,
705+
replayedCalls,
706+
knownCallsOffset,
707+
carriedRoots.byteLength,
708+
);
709+
}
710+
697711
return {
698712
ids: selected.map(entry => storeCursorBlob(entry.data, requestScope)),
699713
byteLength: selected.reduce((sum, entry) => sum + entry.byteLength, 0),
@@ -967,6 +981,79 @@ function toolInvocationLine(call: Extract<OcxAssistantContentPart, { type: "tool
967981
return `invoked: ${namespacedToolName(call.namespace, call.name)} with ${toolCallArgumentsText(call.arguments)}`;
968982
}
969983

984+
/**
985+
* Second pass over the assembled root set: spend envelope bytes nothing else claimed on invocation
986+
* arguments the per-call cap clipped.
987+
*
988+
* `CURSOR_INVOCATION_ARGUMENTS_BYTE_LIMIT` is charged while the envelope is still being built, so it
989+
* costs a call 2 KiB whether or not anything else wants those bytes. In a small replay nearly the
990+
* whole 192-root / 512 KiB envelope goes unused and the cap still bites: a 4,693-byte successful
991+
* `write_file` lost its tail inside a 6,011-byte replay, and because the result text does not repeat
992+
* the argument, the model could no longer see what it had just written (#4516).
993+
*
994+
* The cap stays, and admission is still decided on its 2 KiB prefix — it is what keeps a 600 KiB
995+
* argument from evicting the output it describes. This pass only refunds leftover aggregate bytes,
996+
* after every pruning and truncation decision is already final:
997+
*
998+
* - newest `toolResult` first, because the argument the model is most likely to still need is the
999+
* one belonging to the call it just made;
1000+
* - only out of `spare`, so restoring can never push the envelope past its own limit;
1001+
* - never for an `outputElided` root, whose own output is already gone — widening the invocation
1002+
* there would spend the last free bytes describing an answer that is not present;
1003+
* - never by dropping, shrinking or reordering another root, so nothing pruning chose to keep is
1004+
* evicted to pay for a wider invocation line.
1005+
*/
1006+
function restoreClippedInvocationArguments(
1007+
selected: RootBlobCandidate[],
1008+
messages: OcxMessage[],
1009+
replayedCalls: Map<string, Extract<OcxAssistantContentPart, { type: "toolCall" }>>,
1010+
knownCallsOffset: number,
1011+
carriedBytes: number,
1012+
): RootBlobCandidate[] {
1013+
let spare = CURSOR_EXTERNAL_ROOT_BYTE_LIMIT
1014+
- carriedBytes
1015+
- selected.reduce((sum, entry) => sum + entry.byteLength, 0);
1016+
if (spare <= 0) return selected;
1017+
const restored = [...selected];
1018+
for (let i = restored.length - 1; i >= 0 && spare > 0; i--) {
1019+
const entry = restored[i];
1020+
if (!entry || entry.role !== "toolResult" || entry.outputElided === true) continue;
1021+
if (entry.text === undefined || entry.messageIndex === undefined) continue;
1022+
const message = messages[entry.messageIndex];
1023+
if (message?.role !== "toolResult") continue;
1024+
// Same full-history bound the envelope builder used: `messageIndex` is local to this call's
1025+
// `rawMessages`, and `knownCallsOffset` re-bases it when only a suffix is replayed.
1026+
const call = callBefore(
1027+
replayedCalls,
1028+
decodeCursorCallId(message.toolCallId),
1029+
knownCallsOffset + entry.messageIndex,
1030+
);
1031+
if (!call) continue;
1032+
const full = serializeToolCallArguments(call.arguments);
1033+
if (full === undefined) continue;
1034+
const clipped = toolCallArgumentsText(call.arguments);
1035+
if (clipped === full) continue;
1036+
const name = namespacedToolName(call.namespace, call.name);
1037+
const clippedLine = `invoked: ${name} with ${clipped}`;
1038+
// Absent when truncation already cut through the invocation line itself; there is nothing to
1039+
// widen in that root, and re-rendering the envelope would undo the output truncation too.
1040+
if (!entry.text.includes(clippedLine)) continue;
1041+
// Callback replacement: serialized arguments routinely contain `$&`, `$'` and `$1`, and the
1042+
// string form of `replace` expands those into the surrounding match instead of inserting them.
1043+
const widened = entry.text.replace(clippedLine, () => `invoked: ${name} with ${full}`);
1044+
const candidate = rootBlobCandidate(
1045+
toolResultRootPayload(widened),
1046+
"toolResult",
1047+
{ messageIndex: entry.messageIndex, text: widened },
1048+
);
1049+
const cost = candidate.byteLength - entry.byteLength;
1050+
if (cost <= 0 || cost > spare) continue;
1051+
restored[i] = candidate;
1052+
spare -= cost;
1053+
}
1054+
return restored;
1055+
}
1056+
9701057
/**
9711058
* History position of each indexed call, keyed by the map `toolCallsByCallId` returned.
9721059
*

‎structure/providers/cursor.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,21 @@ does not expose authoritative cache_read_tokens.
8181

8282
> Decision record: [ADR-0054](../decisions/ADR-0054-cursor-conversation-checkpoint-reuse.md)
8383
84+
## Cursor root replay budgets
85+
86+
`src/adapters/cursor/protobuf-request.ts` bounds the replayed root set at 192 blobs and 512 KiB, and
87+
caps the serialized arguments named inside one replayed tool-result envelope at 2 KiB. That per-call
88+
cap is what keeps a 600 KiB argument from consuming the aggregate budget and evicting the output it
89+
describes, and it still decides admission. Because it is charged while the envelope is being built,
90+
a small replay would otherwise clip a completed call's arguments with nearly the whole envelope
91+
unused. After every pruning and truncation decision is final, a second pass re-widens clipped
92+
invocation lines out of the leftover aggregate bytes only: newest tool result first, skipping a root
93+
whose own output was already elided, and never dropping, shrinking or reordering a retained root.
94+
Root-echo eligibility is `cursorNeedsExternalToolContinuation`, which includes native
95+
`composer-2.5`, not only external wire models, so the restoration reaches every replay that carries
96+
an invocation line. Coverage lives in
97+
`tests/providers/cursor/cursor-tool-result-invocation.test.ts`.
98+
8499
## Cursor executable tool schema ownership
85100

86101
`src/adapters/cursor/tool-schemas.ts` owns advertised and argument-normalization

‎tests/providers/cursor/cursor-tool-result-invocation.test.ts‎

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, test } from "bun:test";
22
import { create, fromBinary, toBinary } from "@bufbuild/protobuf";
3-
import { CURSOR_INVOCATION_ARGUMENTS_BYTE_LIMIT, encodeCursorRunRequest } from "../../../src/adapters/cursor/protobuf-request";
3+
import { CURSOR_EXTERNAL_ROOT_BYTE_LIMIT, CURSOR_INVOCATION_ARGUMENTS_BYTE_LIMIT, encodeCursorRunRequest } from "../../../src/adapters/cursor/protobuf-request";
44
import { handleCursorNativeKv, storeCursorBlob } from "../../../src/adapters/cursor/native-exec";
55
import {
66
AgentClientMessageSchema,
@@ -553,3 +553,106 @@ describe("cursor invocation lookup is bounded by history position", () => {
553553
expect(step).toContain("echo COVERED");
554554
});
555555
});
556+
557+
/**
558+
* #4516: the 2 KiB invocation-argument cap is charged while the envelope is still being built, so
559+
* it cost a call 2 KiB even when nearly the whole 512 KiB envelope went unused. A 4.6 KiB
560+
* successful write_file lost its argument tail inside a 6 KiB replay, and because the result text
561+
* does not repeat the argument, the model could no longer see what it had just written.
562+
*
563+
* The cap stays — it is what stops a 600 KiB argument from evicting the output it describes — but a
564+
* second pass now refunds leftover aggregate bytes to clipped invocation lines, newest result
565+
* first, without evicting or shrinking any root. These tests pin the refund: full restoration when
566+
* the envelope is idle, a no-op below the cap, coverage of the native composer-2.5 path the gate
567+
* exists for, verbatim handling of String.replace patterns inside arguments, and a hard stop at
568+
* the envelope boundary.
569+
*/
570+
describe("cursor spare envelope budget restores clipped invocation arguments", () => {
571+
function writeFileHistory(args: Record<string, unknown>): OcxMessage[] {
572+
return [
573+
{ role: "user", content: "Write the file.", timestamp: 1 },
574+
{
575+
role: "assistant",
576+
content: [{ type: "toolCall", id: CALL_ID, name: "write_file", arguments: args }],
577+
timestamp: 2,
578+
},
579+
{ role: "toolResult", toolCallId: CALL_ID, toolName: "write_file", content: "SENTINEL_OUTPUT", isError: false, timestamp: 3 },
580+
];
581+
}
582+
583+
function invokedLine(root: string | undefined): string | undefined {
584+
return root?.split("\n").find(text => text.startsWith("invoked: "));
585+
}
586+
587+
test("an oversized argument is restored in full when the envelope is idle", () => {
588+
const args = { contents: "A".repeat(4600) };
589+
const root = resultRoot(encode(writeFileHistory(args), "grok-4.6-high"));
590+
expect(root).toBeDefined();
591+
expect(root).toContain("SENTINEL_OUTPUT");
592+
const line = invokedLine(root);
593+
expect(line).toBeDefined();
594+
expect(line).not.toContain("…[arguments truncated]");
595+
expect(line).toContain(JSON.stringify(args));
596+
});
597+
598+
// The refund pass must be a no-op below the cap: a line that was never clipped has nothing to
599+
// restore, and rewriting it would only risk drift from the admission-time rendering.
600+
test("an under-cap argument is unchanged", () => {
601+
const args = { contents: "A".repeat(64) };
602+
const root = resultRoot(encode(writeFileHistory(args), "grok-4.6-high"));
603+
expect(invokedLine(root)).toBe("invoked: write_file with " + JSON.stringify(args));
604+
});
605+
606+
// composer-2.5 is a NATIVE wire model (isCursorExternalWireModel is false) that still routes
607+
// through the external tool-continuation path, so it echoes results into roots and accumulates
608+
// the same clipped lines. This is the case the echoToolResultInRoot gate exists for: a gate
609+
// written as externalModel would leave the one native model with clipped lines capped.
610+
test("native composer-2.5 root replay is restored too", () => {
611+
const args = { contents: "A".repeat(4600) };
612+
const root = resultRoot(encode(writeFileHistory(args), "composer-2.5"));
613+
expect(root).toBeDefined();
614+
const line = invokedLine(root);
615+
expect(line).toBeDefined();
616+
expect(line).not.toContain("…[arguments truncated]");
617+
expect(line).toContain(JSON.stringify(args));
618+
});
619+
620+
// Serialized arguments routinely contain $&, $', $` and $1. The widening must use the callback
621+
// form of String.prototype.replace: the string form expands those sequences into the surrounding
622+
// match and writes corrupted arguments into the root.
623+
test("replacement patterns inside arguments are not expanded", () => {
624+
const args = { contents: "$&$'`$1" + "B".repeat(4600) };
625+
const root = resultRoot(encode(writeFileHistory(args), "grok-4.6-high"));
626+
expect(invokedLine(root)).toContain(JSON.stringify(args));
627+
});
628+
629+
// The refund is bounded by the envelope's own leftover bytes, newest result first: when the spare
630+
// cannot cover every clipped line, the pass must stop mid-set rather than overrun the limit, and
631+
// the result the model most likely still needs — the one it just produced — is restored first.
632+
test("restoration stops at the envelope and prefers the newest result", () => {
633+
const messages: OcxMessage[] = [];
634+
for (let n = 0; n < 60; n++) {
635+
messages.push(
636+
{ role: "user", content: "round " + n, timestamp: n * 3 + 1 },
637+
{
638+
role: "assistant",
639+
content: [{ type: "toolCall", id: "call_" + n, name: "write_file", arguments: { path: "/f" + n, contents: "C".repeat(16 * 1024) } }],
640+
timestamp: n * 3 + 2,
641+
},
642+
{ role: "toolResult", toolCallId: "call_" + n, toolName: "write_file", content: "OUT_" + n, isError: false, timestamp: n * 3 + 3 },
643+
);
644+
}
645+
const bytes = encode(messages, "grok-4.6-high");
646+
const blobIds = runRequest(bytes)?.conversationState?.rootPromptMessagesJson ?? [];
647+
const total = blobIds.reduce((sum, blobId) => sum + blobData(blobId).byteLength, 0);
648+
expect(total).toBeLessThanOrEqual(CURSOR_EXTERNAL_ROOT_BYTE_LIMIT);
649+
650+
const results = rootTexts(bytes).filter(text => text.startsWith("[Tool Result]"));
651+
const newest = results.find(text => text.includes("OUT_59"));
652+
expect(newest).toBeDefined();
653+
expect(invokedLine(newest)).toBeDefined();
654+
expect(invokedLine(newest)).not.toContain("…[arguments truncated]");
655+
const stillClipped = results.filter(text => invokedLine(text)?.includes("…[arguments truncated]"));
656+
expect(stillClipped.length).toBeGreaterThan(0);
657+
});
658+
});

0 commit comments

Comments
 (0)