1+ import { createHash } from "node:crypto" ;
12import type {
23 OcxAssistantContentPart ,
34 OcxContentPart ,
@@ -8,7 +9,7 @@ import type {
89} from "../../types" ;
910import { isAllowedToolChoice , namespacedToolName , toolChoiceAliases , type OcxTool , type OcxToolChoice } from "../../types" ;
1011import type { CursorRequestMessage , CursorRunRequest } from "./types" ;
11- import { cursorWireModelSelection , isCursorExternalWireModel , type CursorRoutingLevel } from "./discovery" ;
12+ import { cursorWireModelSelection , isCursorExternalWireModel , isCursorNativeWireModel , type CursorRoutingLevel } from "./discovery" ;
1213import { cursorEffortSuffix } from "./effort-map" ;
1314import {
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+
167208export 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 ,
0 commit comments