@@ -6,7 +6,11 @@ import { isCursorExternalWireModel } from "./cursor/discovery";
66import { createCursorKvStore , type CursorKvStore } from "./cursor/kv-store" ;
77import { mapCursorServerMessage } from "./cursor/message-mapper" ;
88import { createCursorRequest , generatedCursorConversationId } from "./cursor/request-builder" ;
9- import { createLiveCursorTransport , CursorMissingCredentialError } from "./cursor/live-transport" ;
9+ import {
10+ createLiveCursorTransport ,
11+ CursorMissingCredentialError ,
12+ rekeyCursorContextUsage ,
13+ } from "./cursor/live-transport" ;
1014import { runCursorTurnWithRetry } from "./cursor/transport-retry" ;
1115import {
1216 createDisabledCursorTransport ,
@@ -71,10 +75,16 @@ export function createCursorAdapter(provider: OcxProviderConfig, deps: CursorAda
7175 const makeTransport = deps . createTransport ?? createLiveCursorTransport ;
7276 const kv = deps . kv ?? createCursorKvStore ( ) ;
7377 _parsed . _cursorConversationId ??= generatedCursorConversationId ( ) ;
78+ const previousConversationId = _parsed . _cursorConversationId ;
7479 let request = createCursorRequest ( _parsed ) ;
7580 // Keep remembered conversation id in sync when the request builder mints a fresh id
7681 // for external-model tool-result continuations (stateless replay).
82+ if ( request . conversationId !== previousConversationId ) {
83+ rekeyCursorContextUsage ( previousConversationId , request . conversationId ) ;
84+ }
7785 _parsed . _cursorConversationId = request . conversationId ;
86+ let emittedOutput = false ;
87+ const lastRawIsToolResult = _parsed . context . messages . at ( - 1 ) ?. role === "toolResult" ;
7888
7989 const runOnce = async ( activeRequest : ReturnType < typeof createCursorRequest > ) => {
8090 await runCursorTurnWithRetry (
@@ -97,26 +107,33 @@ export function createCursorAdapter(provider: OcxProviderConfig, deps: CursorAda
97107 void activeTransport . writeClient ( clientMessage ) ;
98108 } ,
99109 } ) ;
100- for ( const event of events ) emit ( event ) ;
110+ for ( const event of events ) {
111+ if ( event . type !== "heartbeat" ) emittedOutput = true ;
112+ emit ( event ) ;
113+ }
101114 } ,
102115 ) ;
103116 } ;
104117
105118 try {
106119 await runOnce ( request ) ;
107120 } catch ( err ) {
108- // One-shot fallback: external Cursor models can reject resume/continuation on a
109- // stale server-side conversation with Connect invalid_argument after stepCompleted .
110- // Replay once with a fresh conversation id and full history .
121+ // One-shot fallback: only for external-model tool-result continuations that fail
122+ // with Connect invalid_argument before any non-heartbeat output was forwarded .
123+ // Replaying after text/tool events would duplicate output .
111124 if (
112125 ! isCursorInvalidArgumentError ( err )
113126 || ! isCursorExternalWireModel ( request . modelId )
127+ || ! lastRawIsToolResult
128+ || emittedOutput
114129 || incoming . abortSignal ?. aborted
115130 ) {
116131 throw err ;
117132 }
133+ const failedConversationId = request . conversationId ;
118134 _parsed . _cursorConversationId = undefined ;
119135 request = createCursorRequest ( _parsed , { forceFreshConversation : true } ) ;
136+ rekeyCursorContextUsage ( failedConversationId , request . conversationId ) ;
120137 _parsed . _cursorConversationId = request . conversationId ;
121138 await runOnce ( request ) ;
122139 }
0 commit comments