Skip to content

Commit b7869c0

Browse files
fix(cursor): estimate context after restart
Use a request-local input estimate when Cursor emits no checkpoint and the process-local context cache is empty.\n\nFixes #373\n\nCo-authored-by: Copilot App <[email protected]>
1 parent 9c02cad commit b7869c0

5 files changed

Lines changed: 58 additions & 4 deletions

File tree

‎src/adapters/cursor/live-transport.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import http2 from "node:http2";
22
import { create, fromBinary, toBinary } from "@bufbuild/protobuf";
33
import { namespacedToolName, type OcxProviderConfig, type OcxUsage } from "../../types";
44
import { CONNECT_FLAG_END_STREAM, decodeAvailableConnectFrames, encodeConnectFrame } from "./framing";
5-
import { activePromptText, encodeCursorRunRequest } from "./protobuf-request";
5+
import { activePromptText, encodeCursorRunRequest, estimateCursorInputTokens } from "./protobuf-request";
66
import {
77
createCursorContextUsageTracker,
88
createCursorProtobufEventState,
@@ -514,6 +514,7 @@ class LiveCursorTransport implements CursorTransport {
514514
parallelToolCalls: request.parallelToolCalls,
515515
toolSchemas,
516516
cursorToolNameMap,
517+
estimatedInputTokens: estimateCursorInputTokens(request),
517518
contextUsage: cursorContextUsageTracker.controlsForConversation(request.conversationId, {
518519
clearPrior: request.contextUsageReset === true,
519520
storeCheckpoints: request.contextUsageStoreCheckpoints !== false,
@@ -883,7 +884,14 @@ export function partialUsageFromEventState(state: ReturnType<typeof createCursor
883884
const ctx = reportableContextTokens(state);
884885
return ctx !== undefined
885886
? { ...usageFromContextTokens(state, ctx), estimated: true }
886-
: { ...state.usage, estimated: true };
887+
: state.estimatedInputTokens
888+
? {
889+
...state.usage,
890+
inputTokens: state.estimatedInputTokens,
891+
totalTokens: state.estimatedInputTokens + out,
892+
estimated: true,
893+
}
894+
: { ...state.usage, estimated: true };
887895
}
888896

889897
/**

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ export interface CursorProtobufEventState {
132132
* conversation cache before the turn.
133133
*/
134134
contextCarryForwardTokens?: number;
135+
/** Request-local estimate used only when Cursor supplied no checkpoint or carried total. */
136+
estimatedInputTokens?: number;
135137
recordContextTokens?: (tokens: number) => void;
136138
openToolCalls: Map<string, { name: string; args: string }>;
137139
completedToolCalls: Set<string>;
@@ -152,6 +154,7 @@ export function createCursorProtobufEventState(options: {
152154
toolSchemas?: Map<string, unknown>;
153155
cursorToolNameMap?: Map<string, string>;
154156
contextUsage?: CursorContextUsageControls;
157+
estimatedInputTokens?: number;
155158
} = {}): CursorProtobufEventState {
156159
return {
157160
// Cursor provides no authoritative usage frame; token counts are heuristic estimates from
@@ -166,6 +169,7 @@ export function createCursorProtobufEventState(options: {
166169
...(options.cursorToolNameMap ? { cursorToolNameMap: options.cursorToolNameMap } : {}),
167170
...(options.contextUsage?.carryForwardTokens !== undefined ? { contextCarryForwardTokens: options.contextUsage.carryForwardTokens } : {}),
168171
...(options.contextUsage?.recordContextTokens ? { recordContextTokens: options.contextUsage.recordContextTokens } : {}),
172+
...(options.estimatedInputTokens && options.estimatedInputTokens > 0 ? { estimatedInputTokens: options.estimatedInputTokens } : {}),
169173
};
170174
}
171175

@@ -450,6 +454,12 @@ export function finalizeTurnEvents(state: CursorProtobufEventState): CursorServe
450454
const contextTokens = reportableContextTokens(state);
451455
const usage: OcxUsage = contextTokens !== undefined
452456
? usageFromContextTokens(state, contextTokens)
453-
: { ...state.usage };
457+
: state.estimatedInputTokens
458+
? {
459+
...state.usage,
460+
inputTokens: state.estimatedInputTokens,
461+
totalTokens: state.estimatedInputTokens + state.usage.outputTokens,
462+
}
463+
: { ...state.usage };
454464
return [{ type: "done", usage }];
455465
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { fromJson, type JsonValue } from "@bufbuild/protobuf";
33
import { ValueSchema } from "@bufbuild/protobuf/wkt";
44
import type { OcxAssistantContentPart, OcxMessage, OcxToolResultMessage } from "../../types";
55
import { namespacedToolName } from "../../types";
6+
import { estimateTokens } from "../../lib/token-estimate";
67
import type { CursorRunRequest } from "./types";
78
import { isCursorExternalWireModel } from "./discovery";
89
import { debugProviderDiagnostic } from "../../lib/debug";
@@ -316,6 +317,19 @@ export function activePromptText(request: CursorRunRequest): string {
316317
return last?.role === "tool" ? last.content : "";
317318
}
318319

320+
/**
321+
* Conservative request-local fallback when Cursor emits neither a checkpoint nor a previously
322+
* observed context total. This is deliberately not persisted: a later authoritative checkpoint
323+
* remains the only source carried across turns.
324+
*/
325+
export function estimateCursorInputTokens(request: CursorRunRequest): number {
326+
return estimateTokens(JSON.stringify({
327+
system: request.system,
328+
messages: request.rawMessages ?? request.messages,
329+
tools: request.tools,
330+
}), request.modelId);
331+
}
332+
319333
export function encodeCursorRunRequest(request: CursorRunRequest): Uint8Array {
320334
const rawText = activePromptText(request);
321335
const lastRole = request.messages.at(-1)?.role;

‎tests/cursor-interaction-query.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,19 @@ describe("partialUsageFromEventState", () => {
168168
expect(partialUsageFromEventState(state)).toEqual({ inputTokens: 0, outputTokens: 7, estimated: true });
169169
});
170170

171+
test("uses the request-local estimate after output when no checkpoint or carry exists", async () => {
172+
const { partialUsageFromEventState } = await import("../src/adapters/cursor/live-transport");
173+
const { createCursorProtobufEventState } = await import("../src/adapters/cursor/protobuf-events");
174+
const state = createCursorProtobufEventState({ estimatedInputTokens: 1_200 });
175+
state.usage.outputTokens = 7;
176+
expect(partialUsageFromEventState(state)).toEqual({
177+
inputTokens: 1_200,
178+
outputTokens: 7,
179+
totalTokens: 1_207,
180+
estimated: true,
181+
});
182+
});
183+
171184
test("carries prior context usage on failure when the current turn had no checkpoint", async () => {
172185
const { partialUsageFromEventState } = await import("../src/adapters/cursor/live-transport");
173186
const { createCursorContextUsageTracker, createCursorProtobufEventState } = await import("../src/adapters/cursor/protobuf-events");

‎tests/cursor-protobuf-events.test.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,15 @@ describe("Cursor protobuf tool-call events", () => {
413413
expect(events).toEqual([{ type: "done", usage: { inputTokens: 0, outputTokens: 0, estimated: true } }]);
414414
});
415415

416+
test("uses a request-local input estimate when no checkpoint or carry-forward context exists", () => {
417+
const state = createCursorProtobufEventState({ estimatedInputTokens: 1_200 });
418+
state.usage.outputTokens = 7;
419+
420+
expect(finalizeTurnEvents(state)).toEqual([
421+
{ type: "done", usage: { inputTokens: 1_200, outputTokens: 7, totalTokens: 1_207, estimated: true } },
422+
]);
423+
});
424+
416425
test("normalizes a mis-keyed completed tool-call arg against the advertised schema", () => {
417426
// The model called the right tool but used `filepath` instead of the schema's `path`.
418427
const toolSchemas = new Map<string, unknown>([
@@ -456,7 +465,7 @@ describe("Cursor protobuf tool-call events", () => {
456465
// Regression for the 10000-then-10300-shows-as-20300 bug. Cursor's checkpoint usedTokens is the
457466
// ABSOLUTE conversation context size; tokenDelta is additive per-turn output. They must land in
458467
// separate fields: totalTokens (absolute) vs outputTokens (additive), mirroring the Kiro SOT fix.
459-
const state = createCursorProtobufEventState();
468+
const state = createCursorProtobufEventState({ estimatedInputTokens: 1_200 });
460469

461470
const checkpoint = (usedTokens: number) => create(AgentServerMessageSchema, {
462471
message: {

0 commit comments

Comments
 (0)