@@ -27,7 +27,9 @@ import {
2727import { lookupCursorThreadConversation } from "./thread-continuity" ;
2828import {
2929 getCursorCheckpoint ,
30+ getLatestCursorCheckpoint ,
3031 type CursorCheckpointInvalidationReason ,
32+ type CursorCheckpointSnapshot ,
3133} from "./checkpoint-store" ;
3234
3335/** Probe-verified Cursor Connect boundaries, with byte headroom for the enclosing field. */
@@ -289,53 +291,76 @@ export function resolveCursorConversationId(
289291 options : CreateCursorRequestOptions = { } ,
290292) : string {
291293 if ( options . forceFreshConversation === true ) return generatedCursorConversationId ( ) ;
292- // Helper/shadow/compaction turns must not append into the parent's Cursor conversation,
293- // even when previous_response_id restored the parent's remembered id.
294- if ( parsed . _cursorIsolateConversation === true ) return generatedCursorConversationId ( ) ;
294+ // Helper/shadow turns must not append into the parent's Cursor conversation, even when
295+ // previous_response_id restored the parent's remembered id. They still keep their own
296+ // cache, keyed off this turn's first user/developer text.
297+ if ( parsed . _cursorIsolateConversation === true ) {
298+ const isolatedSeed = cursorHistorySeed ( parsed ) ;
299+ if ( isolatedSeed ) {
300+ return cursorConversationIdFromClientThread ( `isolate:${ isolatedSeed } ` , parsed . _cursorIdentityScope ) ;
301+ }
302+ return generatedCursorConversationId ( ) ;
303+ }
295304 if ( parsed . _cursorConversationId ) return parsed . _cursorConversationId ;
296305 const threadId = parsed . _clientThreadId ?. trim ( ) ;
297306 if ( threadId ) {
298307 const recovered = lookupCursorThreadConversation ( threadId , parsed . _cursorIdentityScope ) ;
299308 if ( recovered ) return recovered ;
300- return cursorConversationIdFromClientThread ( threadId , parsed . _cursorIdentityScope ) ;
309+ return cursorConversationIdFromClientThread ( `thread: ${ threadId } ` , parsed . _cursorIdentityScope ) ;
301310 }
311+ const historySeed = cursorHistorySeed ( parsed ) ;
312+ if ( historySeed ) return cursorConversationIdFromClientThread ( `history:${ historySeed } ` , parsed . _cursorIdentityScope ) ;
302313 return generatedCursorConversationId ( ) ;
303314}
304315
316+ function cursorHistorySeed ( parsed : OcxParsedRequest ) : string | undefined {
317+ for ( const message of parsed . context . messages ) {
318+ if ( message . role !== "user" && message . role !== "developer" ) continue ;
319+ const text = contentToText ( message . content ) . trim ( ) ;
320+ if ( text ) return text ;
321+ }
322+ return undefined ;
323+ }
324+
305325export interface CreateCursorRequestOptions {
306326 /** Force a brand-new Cursor conversation id even when remembered state exists. */
307327 forceFreshConversation ?: boolean ;
308328}
309329
310- function checkpointInvalidationReason (
330+ function resolveCursorCheckpoint (
311331 parsed : OcxParsedRequest ,
312332 request : CursorRunRequest ,
313333 options : CreateCursorRequestOptions ,
314- ) : CursorCheckpointInvalidationReason | undefined {
315- if ( options . forceFreshConversation === true ) return "force_fresh" ;
316- if ( parsed . _cursorIsolateConversation === true ) return "isolated_turn" ;
317- if ( parsed . _compactionRequest === true || parsed . _contextCompactionBoundary === true ) return "compaction" ;
334+ ) : { snapshot : CursorCheckpointSnapshot } | { reason : CursorCheckpointInvalidationReason } {
335+ if ( options . forceFreshConversation === true ) return { reason : "force_fresh" } ;
336+ if ( parsed . _compactionRequest === true || parsed . _contextCompactionBoundary === true ) return { reason : "compaction" } ;
337+ const isolated = parsed . _cursorIsolateConversation === true ;
318338 const cursorState = parsed . _providerContinuation ?. cursor ;
319- const ref = cursorState ?. checkpointRef ;
320- if ( ! ref ) return "missing_ref" ;
321- const snapshot = getCursorCheckpoint ( ref ) ;
322- if ( ! snapshot ) return "expired" ;
323- if ( snapshot . conversationId !== request . conversationId ) return "conversation_changed" ;
339+ const ref = isolated ? undefined : cursorState ?. checkpointRef ;
324340 const identityScope = parsed . _cursorIdentityScope ?. trim ( ) || "local" ;
325- if ( snapshot . identityScope !== identityScope ) return "identity_changed" ;
326- if ( cursorCheckpointModelAffinityId ( snapshot . modelId ) !== cursorCheckpointModelAffinityId ( request . modelId ) ) {
327- return "model_changed" ;
341+ const modelAffinity = cursorCheckpointModelAffinityId ( request . modelId ) ;
342+ let snapshot = getCursorCheckpoint ( ref ) ;
343+ if ( ! snapshot ) {
344+ snapshot = getLatestCursorCheckpoint ( candidate =>
345+ candidate . conversationId === request . conversationId
346+ && candidate . identityScope === identityScope
347+ && cursorCheckpointModelAffinityId ( candidate . modelId ) === modelAffinity
348+ ) ;
328349 }
350+ if ( ! snapshot ) return { reason : ref ? "expired" : "missing_ref" } ;
351+ if ( snapshot . conversationId !== request . conversationId ) return { reason : "conversation_changed" } ;
352+ if ( snapshot . identityScope !== identityScope ) return { reason : "identity_changed" } ;
353+ if ( cursorCheckpointModelAffinityId ( snapshot . modelId ) !== modelAffinity ) return { reason : "model_changed" } ;
329354 const lastRole = parsed . context . messages . at ( - 1 ) ?. role ;
330355 if ( lastRole === "toolResult" ) {
331- if ( snapshot . coveredMessageCount === undefined ) return "trailing_tool_result" ;
356+ if ( snapshot . coveredMessageCount === undefined ) return { reason : "trailing_tool_result" } ;
332357 if ( snapshot . coveredMessageCount < 0 || snapshot . coveredMessageCount >= parsed . context . messages . length ) {
333- return "trailing_tool_result" ;
358+ return { reason : "trailing_tool_result" } ;
334359 }
335360 } else if ( cursorState ?. checkpointUsable === false ) {
336- return "trailing_tool_result" ;
361+ return { reason : "trailing_tool_result" } ;
337362 }
338- return undefined ;
363+ return { snapshot } ;
339364}
340365
341366export function createCursorRequest (
@@ -364,22 +389,16 @@ export function createCursorRequest(
364389 ...( parsed . options . toolChoice ? { toolChoice : parsed . options . toolChoice } : { } ) ,
365390 ...( parsed . options . parallelToolCalls !== undefined ? { parallelToolCalls : parsed . options . parallelToolCalls } : { } ) ,
366391 } ;
367- const invalidation = checkpointInvalidationReason ( parsed , request , options ) ;
368- if ( invalidation ) {
369- request . continuationMode = "full-replay" ;
370- request . checkpointInvalidationReason = invalidation ;
371- return request ;
372- }
373- const snapshot = getCursorCheckpoint ( parsed . _providerContinuation ?. cursor ?. checkpointRef ) ;
374- if ( ! snapshot ) {
392+ const resolved = resolveCursorCheckpoint ( parsed , request , options ) ;
393+ if ( "reason" in resolved ) {
375394 request . continuationMode = "full-replay" ;
376- request . checkpointInvalidationReason = "missing_ref" ;
395+ request . checkpointInvalidationReason = resolved . reason ;
377396 return request ;
378397 }
379- request . checkpointBytes = snapshot . checkpointBytes ;
398+ request . checkpointBytes = resolved . snapshot . checkpointBytes ;
380399 request . continuationMode = "checkpoint" ;
381- if ( parsed . context . messages . at ( - 1 ) ?. role === "toolResult" && snapshot . coveredMessageCount !== undefined ) {
382- request . checkpointSuffixStart = snapshot . coveredMessageCount ;
400+ if ( parsed . context . messages . at ( - 1 ) ?. role === "toolResult" && resolved . snapshot . coveredMessageCount !== undefined ) {
401+ request . checkpointSuffixStart = resolved . snapshot . coveredMessageCount ;
383402 }
384403 return request ;
385404}
0 commit comments