Skip to content

Commit 56c9567

Browse files
authored
test(cursor): pin the spare-budget refund's budget and ordering (#4554)
Five regressions for the invocation refund landed in #4543. - A just-over-cap argument (~2,117 bytes against the 2,048 cap) must come back byte-exact. The existing fixture is 4,600 bytes, where thousands of spare bytes surround the decision and an off-by-one in the cost arithmetic or in the newline-anchored search cannot show. - No result may be evicted to pay for a wider invocation line, and under the fixture's uniform per-round costs the restored set must be the newest contiguous suffix. That second claim is a direction check: flipping the walk to oldest-first makes it a prefix and turns this red. The comment says so, and says plainly that contiguity is not guaranteed under mixed sizes, because the pass skips an unaffordable line with continue rather than break. - A checkpoint-covered call must keep its argument tail in the replayed suffix. Drop knownCallsOffset from the pass's callBefore bound and only this case notices, since that term is identically zero on the full-replay path. - A multi-byte argument must survive intact, with U+FFFD asserted absent so a failure names itself rather than only showing unequal strings. - The outputElided skip is load bearing, and finding that out took two tries. A sweep of single-result fixtures said the guard was dead code — elision appeared to always cut the invocation line too — and an adversarial counter-read found the configuration that sweep could not reach. Truncation alone cannot pay for a restoration: it undershoots its own budget by about 28 bytes. Initiator recovery can. With a ~519.7 KiB system prompt the equal-share pass cuts two trailing results to ~2.3 KiB, losing "output:" but keeping the clipped invocation line, and recovery then drops the older elided sibling to fit the user turn; those freed bytes become spare. The test searches that ~24-byte window rather than pinning a literal size, because pinning one made it pass on a two-character call id and fail on a twelve-character one, and it fails loudly if the window disappears. Each of the last three was verified by mutation. The two 600 KiB cap tests are byte-identical. The only src change is the comment recording what the guard actually depends on, and structure/providers/cursor.md records it too — the earlier draft of both called the guard defensive, which was wrong.
1 parent 2176c5b commit 56c9567

3 files changed

Lines changed: 182 additions & 0 deletions

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,14 @@ function toolInvocationLine(call: Extract<OcxAssistantContentPart, { type: "tool
10051005
* - only out of `spare`, so restoring can never push the envelope past its own limit;
10061006
* - never for an `outputElided` root, whose own output is already gone — widening the invocation
10071007
* there would spend the last free bytes describing an answer that is not present;
1008+
* this guard is load bearing, and it is not reachable the obvious way. Truncation undershoots
1009+
* its own budget by ~28 bytes, far less than a restoration costs, so a root that was merely
1010+
* truncated cannot pay. What pays is initiator recovery: after the equal-share pass elides a
1011+
* trailing run, recovery drops an elided sibling to fit the user turn, and the bytes it frees
1012+
* become spare. It needs the share to land in a narrow window — wide enough that the clipped
1013+
* invocation line survives, narrow enough that `output:` does not — and outside it the
1014+
* clipped-line lookup below declines the root first. `the skip refuses to widen an elided root
1015+
* even when spare would pay` pins a measured instance;
10081016
* - never by dropping, shrinking or reordering another root, so nothing pruning chose to keep is
10091017
* evicted to pay for a wider invocation line.
10101018
*/

‎structure/providers/cursor.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ a small replay would otherwise clip a completed call's arguments with nearly the
9191
unused. After every pruning and truncation decision is final, a second pass re-widens clipped
9292
invocation lines out of the leftover aggregate bytes only: newest tool result first, skipping a root
9393
whose own output was already elided, and never dropping, shrinking or reordering a retained root.
94+
The elision skip is load bearing, reached through initiator recovery rather than through truncation
95+
alone: a truncated root undershoots its own budget by far less than a restoration costs, but after
96+
the equal-share pass elides a trailing run, recovery drops an elided sibling to fit the user turn and
97+
the freed bytes become spare. It requires the share to land in a narrow window where the clipped
98+
invocation line survives but `output:` does not; outside that window the clipped-line lookup declines
99+
the root first.
94100
Root-echo eligibility is `cursorNeedsExternalToolContinuation`, which includes native
95101
`composer-2.5`, not only external wire models, so the restoration reaches every replay that carries
96102
an invocation line. Coverage lives in

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

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,4 +691,172 @@ describe("cursor spare envelope budget restores clipped invocation arguments", (
691691
expect(line).not.toContain("…[arguments truncated]");
692692
expect(line).toContain(JSON.stringify(args));
693693
});
694+
695+
// The boundary case the 4,600-byte fixture cannot see: an argument only ~70 bytes over the cap.
696+
// An off-by-one in the cost arithmetic (`cost > spare` vs `>=`) or in the newline-anchored
697+
// clipped-line search is invisible when thousands of spare bytes surround the decision — it only
698+
// shows up when the clip is a handful of bytes and the widened line must match exactly.
699+
test("a just-over-cap argument is preserved complete", () => {
700+
const args = { contents: "A".repeat(2100) };
701+
const root = resultRoot(encode(writeFileHistory(args), "grok-4.6-high"));
702+
expect(root).toBeDefined();
703+
expect(root).toContain("SENTINEL_OUTPUT");
704+
expect(root).not.toContain("…[arguments truncated]");
705+
expect(invokedLine(root)).toBe("invoked: write_file with " + JSON.stringify(args));
706+
});
707+
708+
// Two claims, and they are not equally general — worth saying plainly, because the weaker one
709+
// reads like the stronger one.
710+
//
711+
// No result may be evicted to pay for a wider invocation line. That is a real invariant of the
712+
// pass, which only ever replaces a root with a widened copy of itself, so all sixty outputs must
713+
// survive regardless of sizes.
714+
//
715+
// The contiguous-suffix claim is weaker. The pass walks newest-first but skips an unaffordable
716+
// line with a continue rather than a break, so with UNEVEN costs a cheaper older line can still
717+
// be filled in after a dearer newer one was passed over — non-contiguously, and legitimately.
718+
// This fixture gives every round the same argument size, so the costs are uniform and the
719+
// restored set has to be the newest contiguous suffix. What that buys is a direction check: flip
720+
// the walk to oldest-first and the restored set becomes a PREFIX, which this assertion catches
721+
// (verified by mutation). Do not read it as a guarantee of contiguity under mixed sizes, and do
722+
// not vary the argument size in this fixture without replacing the assertion.
723+
test("restoration never evicts an older result and stops at a contiguous boundary", () => {
724+
const messages: OcxMessage[] = [];
725+
for (let n = 0; n < 60; n++) {
726+
messages.push(
727+
{ role: "user", content: "round " + n, timestamp: n * 3 + 1 },
728+
{
729+
role: "assistant",
730+
content: [{ type: "toolCall", id: "call_" + n, name: "write_file", arguments: { path: "/f" + n, contents: "C".repeat(16 * 1024) } }],
731+
timestamp: n * 3 + 2,
732+
},
733+
{ role: "toolResult", toolCallId: "call_" + n, toolName: "write_file", content: "OUT_" + n, isError: false, timestamp: n * 3 + 3 },
734+
);
735+
}
736+
// Wire order, oldest to newest — the order the model reads them, and the order the suffix
737+
// property is stated in.
738+
const results = rootTexts(encode(messages, "grok-4.6-high")).filter(text => text.startsWith("[Tool Result]"));
739+
for (let n = 0; n < 60; n++) {
740+
expect(results.some(text => text.includes("OUT_" + n))).toBe(true);
741+
}
742+
const clipped = results.map(text => invokedLine(text)?.includes("…[arguments truncated]") === true);
743+
// Exactly one clipped -> restored transition, and never the reverse: under uniform costs a
744+
// newest-first walk can only produce clipped-then-restored in wire order.
745+
let transitions = 0;
746+
for (let i = 1; i < clipped.length; i++) {
747+
if (clipped[i - 1] === true && clipped[i] === false) transitions++;
748+
expect(clipped[i - 1] === false && clipped[i] === true).toBe(false);
749+
}
750+
expect(transitions).toBe(1);
751+
// Both sides non-empty: an all-restored or all-clipped run would make the boundary assertion
752+
// vacuous.
753+
expect(clipped.some(Boolean)).toBe(true);
754+
expect(clipped.every(Boolean)).toBe(false);
755+
});
756+
757+
// On the checkpoint path only the result is replayed — its call sits inside the covered prefix,
758+
// so the pass resolves it with callBefore(replayedCalls, callId, knownCallsOffset + messageIndex).
759+
// Drop the knownCallsOffset term and callBefore compares a full-history call position against a
760+
// slice-local index, returns undefined for the covered call, and the line stays clipped. Only a
761+
// checkpoint fixture catches that: on the full-replay path the term is identically zero.
762+
test("a checkpoint-covered call keeps its argument tail in the suffix", () => {
763+
const args = { contents: "A".repeat(2100) };
764+
const messages: OcxMessage[] = [
765+
{ role: "user", content: "Write the file.", timestamp: 1 },
766+
{
767+
role: "assistant",
768+
content: [{ type: "toolCall", id: CALL_ID, name: "write_file", arguments: args }],
769+
timestamp: 2,
770+
},
771+
{ role: "toolResult", toolCallId: CALL_ID, toolName: "write_file", content: "SENTINEL_OUTPUT", isError: false, timestamp: 3 },
772+
];
773+
const root = resultRoot(encodeCheckpoint(messages, "grok-4.6-high", 2));
774+
expect(root).toBeDefined();
775+
const line = invokedLine(root);
776+
expect(line).toBeDefined();
777+
expect(line).not.toContain("…[arguments truncated]");
778+
expect(line).toContain(JSON.stringify(args));
779+
});
780+
781+
// "한" is three UTF-8 bytes, so 700 of them put the 2 KiB cap boundary inside a character. When
782+
// the spare budget cannot cover the whole line, truncateUtf8 walks back to a character boundary —
783+
// a naive byte slice would leave U+FFFD in the stored text. The equality half alone would not say
784+
// WHICH failure occurred, so the replacement character is asserted absent explicitly. Here the
785+
// envelope is idle and the full argument survives the round trip intact.
786+
test("a multi-byte argument survives the round trip intact", () => {
787+
const args = { contents: "한".repeat(700) };
788+
const root = resultRoot(encode(writeFileHistory(args), "grok-4.6-high"));
789+
expect(root).toBeDefined();
790+
expect(root).not.toContain("\uFFFD");
791+
expect(invokedLine(root)).toContain(JSON.stringify(args));
792+
});
793+
794+
// The outputElided skip, pinned at a configuration the test finds for itself. The guard is load
795+
// bearing, and an earlier pass at this very test asserted the opposite — that elision always cuts
796+
// the invocation line too, so the guard could never decide anything. A sweep of single-result
797+
// fixtures agreed, and it was wrong: it never landed in the share window where the claim fails.
798+
//
799+
// The reachable route is not truncation on its own. A truncated root undershoots its own budget by
800+
// about 28 bytes, nowhere near a restoration's cost. What pays is initiator recovery: a ~519.7 KiB
801+
// system prompt leaves roughly 4.6 KiB of history budget, the equal-share pass cuts each of two
802+
// trailing results to ~2.3 KiB — far enough to lose "output:" but not the clipped invocation line —
803+
// and recovery then drops the older elided sibling so the user turn fits. Those freed bytes become
804+
// spare, and the surviving elided root holds a clipped line the pass could now afford.
805+
//
806+
// That window is only ~24 bytes wide, so it moves when any envelope header changes length: pinning
807+
// one literal system size made this test pass on a two-character call id and fail on a twelve-
808+
// character one. It therefore searches for the window instead, and fails loudly if no size in the
809+
// range produces one — which is the signal that the route closed and the guard needs re-examining,
810+
// not a licence to delete the assertion.
811+
//
812+
// Remove the outputElided term from the pass's guard and the located root comes back widened, with
813+
// the full 3,000-byte argument in a root that shows the model no output at all. Verified by
814+
// mutation.
815+
test("the skip refuses to widen an elided root even when spare would pay", () => {
816+
const args = { contents: "A".repeat(3000) };
817+
const full = JSON.stringify(args);
818+
const probe = (systemBytes: number) => {
819+
const messages: OcxMessage[] = [
820+
{ role: "user", content: "U".repeat(200), timestamp: 1 },
821+
{
822+
role: "assistant",
823+
content: [{ type: "toolCall", id: "c0", name: "write_file", arguments: args }],
824+
timestamp: 2,
825+
},
826+
{ role: "toolResult", toolCallId: "c0", toolName: "write_file", content: "OUT_0_" + "Y".repeat(20000), isError: false, timestamp: 3 },
827+
{
828+
role: "assistant",
829+
content: [{ type: "toolCall", id: "c1", name: "write_file", arguments: args }],
830+
timestamp: 4,
831+
},
832+
{ role: "toolResult", toolCallId: "c1", toolName: "write_file", content: "OUT_1_" + "Y".repeat(20000), isError: false, timestamp: 5 },
833+
];
834+
const bytes = encodeCursorRunRequest({
835+
modelId: "grok-4.6-high",
836+
conversationId: "c_elide_" + systemBytes,
837+
system: ["S".repeat(systemBytes)],
838+
messages: [],
839+
rawMessages: messages,
840+
});
841+
const root = resultRoot(bytes);
842+
const blobIds = runRequest(bytes)?.conversationState?.rootPromptMessagesJson ?? [];
843+
const used = blobIds.reduce((sum, blobId) => sum + blobData(blobId).byteLength, 0);
844+
return { root, spare: CURSOR_EXTERNAL_ROOT_BYTE_LIMIT - used };
845+
};
846+
// The window: "output:" gone, but the clipped invocation line still whole, and enough envelope
847+
// left over to have paid the ~968-byte widening. That last term is what makes this a test of the
848+
// skip rather than of the budget.
849+
let located: { root: string | undefined; spare: number } | undefined;
850+
for (let systemBytes = 519600; systemBytes <= 519800 && !located; systemBytes += 2) {
851+
const candidate = probe(systemBytes);
852+
if (candidate.root === undefined) continue;
853+
if (candidate.root.includes("\noutput:\n")) continue;
854+
if (invokedLine(candidate.root)?.endsWith("…[arguments truncated]") !== true) continue;
855+
if (candidate.spare <= 1024) continue;
856+
located = candidate;
857+
}
858+
expect(located).toBeDefined();
859+
// The pass declined to widen it, even though the bytes were there.
860+
expect(located!.root).not.toContain(full);
861+
});
694862
});

0 commit comments

Comments
 (0)