Skip to content

Commit fd04b5d

Browse files
committed
fix(cursor): keep a collapsed root's run note through a rebuild
An adversarial counter-read of the restoration pass found the real defect one layer down. pushDeduped builds the collapsed root's wire payload from the marked text but stored the UNMARKED text in the candidate's `text` field, so every consumer that rebuilds a root from `text` silently deleted the "produced N times in a row" note: truncateToolResultBlob already did, and the new invocation restoration did too. That note is the repetition breaker's per-entry half, so losing it re-primes the self-reinforcing loop the breaker exists to end. Store the marked text, which makes `text` a true mirror of the stored payload for the first time, and fixes the truncation path by the same change. Also anchor the restoration's search on the preceding newline. toolResultToText always emits the invocation after the [tool_result], call_id: and name: lines, so the real line is never first; name: renders the result's tool name, which nothing sanitizes, so an unanchored search could be satisfied by a crafted tool name and rewrite that header instead of the invocation. The regression test fails with the pushDeduped change reverted and passes with it.
1 parent 4ca2979 commit fd04b5d

2 files changed

Lines changed: 40 additions & 3 deletions

File tree

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,12 @@ function rootPromptMessages(
325325
const replacement = rootBlobCandidate(
326326
{ role: payload.role, content: [{ type: "text", text: marked }] },
327327
role,
328-
{ ...opts, messageIndex: previous.entry.messageIndex ?? opts.messageIndex },
328+
// `text` must mirror the payload actually stored, not the unmarked text it was built from.
329+
// It did not, and every consumer that rebuilds a root from `text` therefore dropped the run
330+
// note: truncating a collapsed root silently deleted the "produced N times" line, and so did
331+
// the invocation-argument restoration below. The note is the repetition breaker's per-entry
332+
// half, so losing it re-primes the self-reinforcing loop the breaker exists to end.
333+
{ ...opts, text: marked, messageIndex: previous.entry.messageIndex ?? opts.messageIndex },
329334
);
330335
entries[entries.indexOf(previous.entry)] = replacement;
331336
replayRuns.set(role, { text: normalized, entry: replacement, length: runLength });
@@ -1034,13 +1039,17 @@ function restoreClippedInvocationArguments(
10341039
const clipped = toolCallArgumentsText(call.arguments);
10351040
if (clipped === full) continue;
10361041
const name = namespacedToolName(call.namespace, call.name);
1037-
const clippedLine = `invoked: ${name} with ${clipped}`;
1042+
// Anchored on the preceding newline. `toolResultToText` always emits the invocation after the
1043+
// `[tool_result]`, `call_id:` and `name:` lines, so the real line is never first — and
1044+
// `name:` renders the RESULT's tool name, which nothing sanitizes, so an unanchored search could
1045+
// be satisfied by a crafted tool name and rewrite that header instead of the invocation.
1046+
const clippedLine = `\ninvoked: ${name} with ${clipped}`;
10381047
// Absent when truncation already cut through the invocation line itself; there is nothing to
10391048
// widen in that root, and re-rendering the envelope would undo the output truncation too.
10401049
if (!entry.text.includes(clippedLine)) continue;
10411050
// Callback replacement: serialized arguments routinely contain `$&`, `$'` and `$1`, and the
10421051
// 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}`);
1052+
const widened = entry.text.replace(clippedLine, () => `\ninvoked: ${name} with ${full}`);
10441053
const candidate = rootBlobCandidate(
10451054
toolResultRootPayload(widened),
10461055
"toolResult",

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,4 +655,32 @@ describe("cursor spare envelope budget restores clipped invocation arguments", (
655655
const stillClipped = results.filter(text => invokedLine(text)?.includes("…[arguments truncated]"));
656656
expect(stillClipped.length).toBeGreaterThan(0);
657657
});
658+
659+
// An adversarial counter-read of this change found the real defect here: pushDeduped built the
660+
// collapsed root's wire payload from the marked text but stored the UNMARKED text in `entry.text`,
661+
// so anything that rebuilt a root from `text` silently deleted the "produced N times in a row"
662+
// note — the restoration pass below, and truncation before it. That note is the repetition
663+
// breaker's per-entry half, so losing it re-primes the self-reinforcing loop the breaker exists to
664+
// end. Restoring the arguments and keeping the note are both required.
665+
test("a collapsed repeat run keeps its run note while its arguments are restored", () => {
666+
const args = { contents: "A".repeat(4600) };
667+
const messages: OcxMessage[] = [
668+
{ role: "user", content: "Write the file.", timestamp: 1 },
669+
{
670+
role: "assistant",
671+
content: [{ type: "toolCall", id: CALL_ID, name: "write_file", arguments: args }],
672+
timestamp: 2,
673+
},
674+
{ role: "toolResult", toolCallId: CALL_ID, toolName: "write_file", content: "SAME_OUTPUT", isError: false, timestamp: 3 },
675+
{ role: "toolResult", toolCallId: CALL_ID, toolName: "write_file", content: "SAME_OUTPUT", isError: false, timestamp: 4 },
676+
{ role: "toolResult", toolCallId: CALL_ID, toolName: "write_file", content: "SAME_OUTPUT", isError: false, timestamp: 5 },
677+
];
678+
const results = rootTexts(encode(messages, "grok-4.6-high")).filter(text => text.startsWith("[Tool Result]"));
679+
expect(results).toHaveLength(1);
680+
const collapsed = results[0]!;
681+
expect(collapsed).toContain("[note: this exact output was produced 3 times in a row]");
682+
const line = invokedLine(collapsed);
683+
expect(line).not.toContain("…[arguments truncated]");
684+
expect(line).toContain(JSON.stringify(args));
685+
});
658686
});

0 commit comments

Comments
 (0)