@@ -184,6 +184,26 @@ function resolveWorkspaceDirectory(worktree: string | undefined, directory: stri
184184 return dirCandidate || cwd || configPrefix ;
185185}
186186
187+ type ProxyRuntimeState = {
188+ baseURL ?: string ;
189+ baseURLByWorkspace ?: Record < string , string > ;
190+ } ;
191+
192+ export function normalizeWorkspaceForCompare ( pathValue : string ) : string {
193+ return resolve ( pathValue ) ;
194+ }
195+
196+ export function isReusableProxyHealthPayload ( payload : any , workspaceDirectory : string ) : boolean {
197+ if ( ! payload || payload . ok !== true ) {
198+ return false ;
199+ }
200+ if ( typeof payload . workspaceDirectory !== "string" || payload . workspaceDirectory . length === 0 ) {
201+ // Legacy proxies that do not expose workspace cannot be safely reused.
202+ return false ;
203+ }
204+ return normalizeWorkspaceForCompare ( payload . workspaceDirectory ) === normalizeWorkspaceForCompare ( workspaceDirectory ) ;
205+ }
206+
187207const FORCE_TOOL_MODE = process . env . CURSOR_ACP_FORCE !== "false" ;
188208const EMIT_TOOL_UPDATES = process . env . CURSOR_ACP_EMIT_TOOL_UPDATES === "true" ;
189209const FORWARD_TOOL_CALLS = process . env . CURSOR_ACP_FORWARD_TOOL_CALLS !== "false" ;
@@ -454,21 +474,25 @@ async function findFirstAllowedToolCallInOutput(
454474async function ensureCursorProxyServer ( workspaceDirectory : string , toolRouter ?: ToolRouter ) : Promise < string > {
455475 const key = getGlobalKey ( ) ;
456476 const g = globalThis as any ;
477+ const normalizedWorkspace = normalizeWorkspaceForCompare ( workspaceDirectory ) ;
478+ const state : ProxyRuntimeState = g [ key ] ?? { baseURL : "" , baseURLByWorkspace : { } } ;
479+ state . baseURLByWorkspace = state . baseURLByWorkspace ?? { } ;
480+ g [ key ] = state ;
457481
458- const existingBaseURL = g [ key ] ? .baseURL ;
482+ const existingBaseURL = state . baseURLByWorkspace [ normalizedWorkspace ] ?? state . baseURL ;
459483 if ( typeof existingBaseURL === "string" && existingBaseURL . length > 0 ) {
460484 return existingBaseURL ;
461485 }
462486
463487 // Mark as starting to avoid duplicate starts in-process.
464- g [ key ] = { baseURL : "" } ;
488+ state . baseURL = "" ;
465489
466490 const handler = async ( req : Request ) : Promise < Response > => {
467491 try {
468492 const url = new URL ( req . url ) ;
469493
470494 if ( url . pathname === "/health" ) {
471- return new Response ( JSON . stringify ( { ok : true } ) , {
495+ return new Response ( JSON . stringify ( { ok : true , workspaceDirectory } ) , {
472496 status : 200 ,
473497 headers : { "Content-Type" : "application/json" } ,
474498 } ) ;
@@ -605,7 +629,7 @@ async function ensureCursorProxyServer(workspaceDirectory: string, toolRouter?:
605629
606630 const stdout = ( stdoutText || "" ) . trim ( ) ;
607631 const stderr = ( stderrText || "" ) . trim ( ) ;
608- const exitCode = child . exitCode ;
632+ const exitCode = await child . exited ;
609633 log . debug ( "cursor-agent completed (bun non-stream)" , {
610634 exitCode,
611635 stdoutChars : stdout . length ,
@@ -866,15 +890,16 @@ async function ensureCursorProxyServer(workspaceDirectory: string, toolRouter?:
866890 return ;
867891 }
868892
869- if ( child . exitCode !== 0 ) {
893+ const exitCode = await child . exited ;
894+ if ( exitCode !== 0 ) {
870895 const stderrText = await new Response ( child . stderr ) . text ( ) ;
871896 const errSource = ( stderrText || "" ) . trim ( )
872- || `cursor-agent exited with code ${ String ( child . exitCode ?? "unknown" ) } and no output` ;
897+ || `cursor-agent exited with code ${ String ( exitCode ?? "unknown" ) } and no output` ;
873898 const parsed = parseAgentError ( errSource ) ;
874899 const msg = formatErrorForUser ( parsed ) ;
875900 log . error ( "cursor-cli streaming failed" , {
876901 type : parsed . type ,
877- code : child . exitCode ,
902+ code : exitCode ,
878903 } ) ;
879904 const errChunk = createChatCompletionChunk ( id , created , model , msg , true ) ;
880905 controller . enqueue ( encoder . encode ( `data: ${ JSON . stringify ( errChunk ) } \n\n` ) ) ;
@@ -883,7 +908,7 @@ async function ensureCursorProxyServer(workspaceDirectory: string, toolRouter?:
883908 }
884909
885910 log . debug ( "cursor-agent completed (bun stream)" , {
886- exitCode : child . exitCode ,
911+ exitCode,
887912 } ) ;
888913 const doneChunk = createChatCompletionChunk ( id , created , model , "" , true ) ;
889914 controller . enqueue ( encoder . encode ( `data: ${ JSON . stringify ( doneChunk ) } \n\n` ) ) ;
@@ -918,8 +943,12 @@ async function ensureCursorProxyServer(workspaceDirectory: string, toolRouter?:
918943 try {
919944 const res = await fetch ( `http://${ CURSOR_PROXY_HOST } :${ CURSOR_PROXY_DEFAULT_PORT } /health` ) . catch ( ( ) => null ) ;
920945 if ( res && res . ok ) {
921- g [ key ] . baseURL = CURSOR_PROXY_DEFAULT_BASE_URL ;
922- return CURSOR_PROXY_DEFAULT_BASE_URL ;
946+ const payload = await res . json ( ) . catch ( ( ) => null ) ;
947+ if ( isReusableProxyHealthPayload ( payload , workspaceDirectory ) ) {
948+ state . baseURL = CURSOR_PROXY_DEFAULT_BASE_URL ;
949+ state . baseURLByWorkspace ! [ normalizedWorkspace ] = CURSOR_PROXY_DEFAULT_BASE_URL ;
950+ return CURSOR_PROXY_DEFAULT_BASE_URL ;
951+ }
923952 }
924953 } catch {
925954 // ignore
@@ -936,7 +965,7 @@ async function ensureCursorProxyServer(workspaceDirectory: string, toolRouter?:
936965
937966 if ( url . pathname === "/health" ) {
938967 res . writeHead ( 200 , { "Content-Type" : "application/json" } ) ;
939- res . end ( JSON . stringify ( { ok : true } ) ) ;
968+ res . end ( JSON . stringify ( { ok : true , workspaceDirectory } ) ) ;
940969 return ;
941970 }
942971
@@ -1396,7 +1425,8 @@ async function ensureCursorProxyServer(workspaceDirectory: string, toolRouter?:
13961425 } ) ;
13971426
13981427 const baseURL = `http://${ CURSOR_PROXY_HOST } :${ CURSOR_PROXY_DEFAULT_PORT } /v1` ;
1399- g [ key ] . baseURL = baseURL ;
1428+ state . baseURL = baseURL ;
1429+ state . baseURLByWorkspace ! [ normalizedWorkspace ] = baseURL ;
14001430 return baseURL ;
14011431 } catch ( error : any ) {
14021432 if ( error ?. code !== "EADDRINUSE" ) {
@@ -1408,8 +1438,12 @@ async function ensureCursorProxyServer(workspaceDirectory: string, toolRouter?:
14081438 try {
14091439 const res = await fetch ( `http://${ CURSOR_PROXY_HOST } :${ CURSOR_PROXY_DEFAULT_PORT } /health` ) . catch ( ( ) => null ) ;
14101440 if ( res && res . ok ) {
1411- g [ key ] . baseURL = CURSOR_PROXY_DEFAULT_BASE_URL ;
1412- return CURSOR_PROXY_DEFAULT_BASE_URL ;
1441+ const payload = await res . json ( ) . catch ( ( ) => null ) ;
1442+ if ( isReusableProxyHealthPayload ( payload , workspaceDirectory ) ) {
1443+ state . baseURL = CURSOR_PROXY_DEFAULT_BASE_URL ;
1444+ state . baseURLByWorkspace ! [ normalizedWorkspace ] = CURSOR_PROXY_DEFAULT_BASE_URL ;
1445+ return CURSOR_PROXY_DEFAULT_BASE_URL ;
1446+ }
14131447 }
14141448 } catch {
14151449 // ignore
@@ -1425,7 +1459,8 @@ async function ensureCursorProxyServer(workspaceDirectory: string, toolRouter?:
14251459
14261460 const addr = server . address ( ) as any ;
14271461 const baseURL = `http://${ CURSOR_PROXY_HOST } :${ addr . port } /v1` ;
1428- g [ key ] . baseURL = baseURL ;
1462+ state . baseURL = baseURL ;
1463+ state . baseURLByWorkspace ! [ normalizedWorkspace ] = baseURL ;
14291464 return baseURL ;
14301465 }
14311466}
0 commit comments