Skip to content

Commit 24d2ac0

Browse files
kimrincursoragent
andcommitted
fix(cursor): pin native composer conversation id to prompt_cache_key
Codex Desktop often replays full history under store:false without a usable previous_response_id restore, so each turn minted a fresh cursor_* id and Composer 2.5 never hit Cursor cache/context carry-forward. Co-authored-by: Cursor <[email protected]>
1 parent 5157c49 commit 24d2ac0

4 files changed

Lines changed: 190 additions & 18 deletions

File tree

‎src/adapters/cursor.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { isCursorBenignCancelError, isCursorInvalidArgumentError, safeCursorErro
55
import { isCursorExternalWireModel } from "./cursor/discovery";
66
import { createCursorKvStore, type CursorKvStore } from "./cursor/kv-store";
77
import { mapCursorServerMessage } from "./cursor/message-mapper";
8-
import { createCursorRequest, generatedCursorConversationId } from "./cursor/request-builder";
8+
import { createCursorRequest } from "./cursor/request-builder";
99
import {
1010
createLiveCursorTransport,
1111
CursorMissingCredentialError,
@@ -77,12 +77,15 @@ export function createCursorAdapter(provider: OcxProviderConfig, deps: CursorAda
7777
const makeTransport = deps.createTransport ?? createLiveCursorTransport;
7878
const kv = deps.kv ?? createCursorKvStore();
7979
const rekeyContextUsage = deps.rekeyContextUsage ?? rekeyCursorContextUsage;
80-
_parsed._cursorConversationId ??= generatedCursorConversationId();
80+
// Do NOT pre-mint a random conversation id here. createCursorRequest resolves
81+
// remembered id → native prompt_cache_key pin → random. Pre-minting would hide
82+
// the prompt_cache_key pin and break Cursor cache/context carry-forward when
83+
// Codex sends full history under store:false without previous_response_id.
8184
const previousConversationId = _parsed._cursorConversationId;
8285
let request = createCursorRequest(_parsed);
8386
// Keep remembered conversation id in sync when the request builder mints a fresh id
8487
// for external-model tool-result continuations (stateless replay).
85-
if (request.conversationId !== previousConversationId) {
88+
if (previousConversationId && request.conversationId !== previousConversationId) {
8689
rekeyContextUsage(previousConversationId, request.conversationId);
8790
}
8891
_parsed._cursorConversationId = request.conversationId;

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

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { createHash } from "node:crypto";
12
import type {
23
OcxAssistantContentPart,
34
OcxContentPart,
@@ -8,7 +9,7 @@ import type {
89
} from "../../types";
910
import { isAllowedToolChoice, namespacedToolName, toolChoiceAliases, type OcxTool, type OcxToolChoice } from "../../types";
1011
import type { CursorRequestMessage, CursorRunRequest } from "./types";
11-
import { cursorWireModelSelection, isCursorExternalWireModel, type CursorRoutingLevel } from "./discovery";
12+
import { cursorWireModelSelection, isCursorExternalWireModel, isCursorNativeWireModel, type CursorRoutingLevel } from "./discovery";
1213
import { cursorEffortSuffix } from "./effort-map";
1314
import {
1415
cursorMcpToolEncodedSize,
@@ -164,6 +165,46 @@ export interface CreateCursorRequestOptions {
164165
forceFreshConversation?: boolean;
165166
}
166167

168+
/**
169+
* Stable Cursor conversation id derived from Codex's prompt_cache_key (thread id).
170+
* Used when previous_response_id continuation state is missing but the client still
171+
* replays full history under store:false — without this, every turn mints a fresh
172+
* cursor_* id and Cursor prompt-cache / context-usage carry-forward never hit.
173+
*/
174+
export function stableCursorConversationIdFromPromptCacheKey(promptCacheKey: string): string {
175+
const digest = createHash("sha256")
176+
.update("ocx-cursor-conv:")
177+
.update(promptCacheKey)
178+
.digest("hex")
179+
.slice(0, 32);
180+
return `cursor_${digest}`;
181+
}
182+
183+
/**
184+
* Resolve the Cursor conversation id for this turn.
185+
* Priority: force-fresh → remembered `_cursorConversationId` → native+prompt_cache_key → random.
186+
* Never use OpenAI Responses `previous_response_id` (resp_*) — different namespace.
187+
*/
188+
export function resolveCursorConversationId(
189+
parsed: OcxParsedRequest,
190+
wireModelId: string,
191+
options: CreateCursorRequestOptions = {},
192+
): string {
193+
const lastRaw = parsed.context.messages.at(-1);
194+
const forceFreshConversation =
195+
options.forceFreshConversation === true
196+
|| (lastRaw?.role === "toolResult" && isCursorExternalWireModel(wireModelId));
197+
if (forceFreshConversation) return generatedCursorConversationId();
198+
if (parsed._cursorConversationId) return parsed._cursorConversationId;
199+
// Native composer/auto: pin to the Codex thread so full-history turns without
200+
// previous_response_id still share one Cursor conversation (cache + checkpoints).
201+
if (isCursorNativeWireModel(wireModelId)) {
202+
const key = parsed.options.promptCacheKey?.trim();
203+
if (key) return stableCursorConversationIdFromPromptCacheKey(key);
204+
}
205+
return generatedCursorConversationId();
206+
}
207+
167208
export function createCursorRequest(
168209
parsed: OcxParsedRequest,
169210
options: CreateCursorRequestOptions = {},
@@ -176,23 +217,10 @@ export function createCursorRequest(
176217
const budget = applyCursorToolBudget(visibleTools, parsed.options.toolChoice);
177218
const limitNote = catalogLimitNote(budget.tools, budget.omitted);
178219
const model = normalizeCursorModelId(parsed.modelId, parsed.options.reasoning);
179-
const lastRaw = parsed.context.messages.at(-1);
180-
// External Cursor models (e.g. gpt-5.6-sol) can corrupt server-side conversation state across
181-
// tool-result continuations when ResumeAction reuses the same conversationId. Force a fresh id
182-
// so the full history is replayed without depending on that state.
183-
const forceFreshConversation =
184-
options.forceFreshConversation === true
185-
|| (lastRaw?.role === "toolResult" && isCursorExternalWireModel(model.modelId));
186220
return {
187221
modelId: model.modelId,
188222
...(model.routingLevel ? { routingLevel: model.routingLevel } : {}),
189-
// The Cursor conversation id comes ONLY from remembered state (_cursorConversationId). Do NOT fall
190-
// back to the OpenAI Responses previous_response_id (resp_*): that is a Responses-chain id in a
191-
// different namespace and would start an unrelated Cursor conversation, breaking tool-result
192-
// continuation. If we have no remembered Cursor conversation, start a fresh one.
193-
conversationId: forceFreshConversation
194-
? generatedCursorConversationId()
195-
: (parsed._cursorConversationId ?? generatedCursorConversationId()),
223+
conversationId: resolveCursorConversationId(parsed, model.modelId, options),
196224
system: [...(parsed.context.systemPrompt ?? []), ...(limitNote ? [limitNote] : [])],
197225
messages,
198226
rawMessages: parsed.context.messages,

‎tests/cursor-request-builder.test.ts‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,68 @@ describe("Cursor request builder", () => {
3838
expect(request.conversationId).toBe("cursor_stable");
3939
});
4040

41+
test("native composer pins conversation id to prompt_cache_key when none remembered", () => {
42+
const a = createCursorRequest({
43+
modelId: "cursor/composer-2.5",
44+
context: { messages: [{ role: "user", content: "hi", timestamp: 1 }] },
45+
stream: false,
46+
options: { promptCacheKey: "thread-abc" },
47+
});
48+
const b = createCursorRequest({
49+
modelId: "composer-2.5",
50+
context: {
51+
messages: [
52+
{ role: "user", content: "hi", timestamp: 1 },
53+
{
54+
role: "toolResult",
55+
toolCallId: "call_1",
56+
toolName: "shell",
57+
content: "ok",
58+
isError: false,
59+
timestamp: 2,
60+
},
61+
],
62+
},
63+
stream: false,
64+
options: { promptCacheKey: "thread-abc" },
65+
});
66+
expect(a.conversationId).toBe(b.conversationId);
67+
expect(a.conversationId.startsWith("cursor_")).toBe(true);
68+
expect(a.conversationId).toHaveLength("cursor_".length + 32);
69+
});
70+
71+
test("external toolResult still force-fresh even with prompt_cache_key", () => {
72+
const first = createCursorRequest({
73+
modelId: "cursor/grok-4.5",
74+
context: { messages: [{ role: "user", content: "hi", timestamp: 1 }] },
75+
stream: false,
76+
options: { promptCacheKey: "thread-xyz" },
77+
_cursorConversationId: "cursor_prior",
78+
});
79+
expect(first.conversationId).toBe("cursor_prior");
80+
81+
const second = createCursorRequest({
82+
modelId: "cursor/grok-4.5",
83+
context: {
84+
messages: [
85+
{
86+
role: "toolResult",
87+
toolCallId: "call_1",
88+
toolName: "shell",
89+
content: "ok",
90+
isError: false,
91+
timestamp: 2,
92+
},
93+
],
94+
},
95+
stream: false,
96+
options: { promptCacheKey: "thread-xyz" },
97+
_cursorConversationId: "cursor_prior",
98+
});
99+
expect(second.conversationId).not.toBe("cursor_prior");
100+
expect(second.conversationId.startsWith("cursor_")).toBe(true);
101+
});
102+
41103
test("marks Cursor context-usage boundaries for compaction epochs", () => {
42104
expect(createCursorRequest({ ...base, _contextCompactionBoundary: true }).contextUsageReset).toBe(true);
43105

‎tests/server-combo-failover-e2e.test.ts‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,4 +1555,83 @@ describe("cursor conversation continuity across store:false chains", () => {
15551555
const { previousResponseProviderState } = await import("../src/responses/state");
15561556
expect(previousResponseProviderState(secondJson.id)?.cursor?.conversationId).toBe(seen[1]);
15571557
});
1558+
1559+
test("native composer-2.5 toolResult stream chain reuses conversationId", async () => {
1560+
const seen: string[] = [];
1561+
customCursorTransportFactory = () => ({
1562+
async *run(request) {
1563+
seen.push(request.conversationId);
1564+
if (seen.length === 1) {
1565+
yield { type: "tool_call_start", id: "call_1", name: "shell" };
1566+
yield { type: "tool_call_delta", arguments: "{}" };
1567+
yield { type: "tool_call_end", id: "call_1" };
1568+
} else {
1569+
yield { type: "text", text: "done" };
1570+
}
1571+
yield { type: "done", usage: { inputTokens: 10, outputTokens: 2, estimated: true } };
1572+
},
1573+
writeClient() {},
1574+
close() {},
1575+
});
1576+
const config = cursorConfig();
1577+
1578+
const postStream = async (raw: Record<string, unknown>) =>
1579+
handleResponses(new Request("http://localhost/v1/responses", {
1580+
method: "POST",
1581+
headers: { "content-type": "application/json" },
1582+
body: JSON.stringify({ stream: true, store: false, ...raw }),
1583+
}), config, { model: "", provider: "" }, {});
1584+
1585+
const first = await postStream({ model: "cursortest/composer-2.5", input: "hello" });
1586+
expect(first.status).toBe(200);
1587+
const text1 = await first.text();
1588+
const completed = [...text1.matchAll(/event: response\.completed\ndata: ({.*})\n/g)]
1589+
.map(match => JSON.parse(match[1]!) as { response?: { id?: string }; id?: string });
1590+
const firstId = completed.at(-1)?.response?.id ?? completed.at(-1)?.id;
1591+
expect(typeof firstId).toBe("string");
1592+
expect(seen).toHaveLength(1);
1593+
1594+
const { previousResponseProviderState } = await import("../src/responses/state");
1595+
expect(previousResponseProviderState(firstId!)?.cursor?.conversationId).toBe(seen[0]);
1596+
1597+
const second = await postStream({
1598+
model: "cursortest/composer-2.5",
1599+
previous_response_id: firstId,
1600+
input: [{ type: "function_call_output", call_id: "call_1", output: "ok" }],
1601+
});
1602+
expect(second.status).toBe(200);
1603+
await second.text();
1604+
expect(seen).toHaveLength(2);
1605+
expect(seen[1]).toBe(seen[0]);
1606+
});
1607+
1608+
test("native composer reuses conversationId across store:false turns via prompt_cache_key alone", async () => {
1609+
const seen: string[] = [];
1610+
customCursorTransportFactory = fakeCursorTransportFactory(seen);
1611+
const config = cursorConfig();
1612+
1613+
const first = await postCursor(config, {
1614+
model: "cursortest/composer-2.5",
1615+
input: "hello",
1616+
prompt_cache_key: "desktop-thread-1",
1617+
});
1618+
expect(first.status).toBe(200);
1619+
await first.json();
1620+
expect(seen).toHaveLength(1);
1621+
1622+
// No previous_response_id — Codex Desktop often replays full history under store:false.
1623+
const second = await postCursor(config, {
1624+
model: "cursortest/composer-2.5",
1625+
input: [
1626+
{ role: "user", content: "hello" },
1627+
{ role: "assistant", content: "cursor ok" },
1628+
{ role: "user", content: "continue" },
1629+
],
1630+
prompt_cache_key: "desktop-thread-1",
1631+
});
1632+
expect(second.status).toBe(200);
1633+
await second.json();
1634+
expect(seen).toHaveLength(2);
1635+
expect(seen[1]).toBe(seen[0]);
1636+
});
15581637
});

0 commit comments

Comments
 (0)