@@ -152,6 +152,26 @@ function resolveWorkspaceDirectory(worktree: string | undefined, directory: stri
152152 return dirCandidate || cwd || configPrefix ;
153153}
154154
155+ type ProxyRuntimeState = {
156+ baseURL ?: string ;
157+ baseURLByWorkspace ?: Record < string , string > ;
158+ } ;
159+
160+ export function normalizeWorkspaceForCompare ( pathValue : string ) : string {
161+ return resolve ( pathValue ) ;
162+ }
163+
164+ export function isReusableProxyHealthPayload ( payload : any , workspaceDirectory : string ) : boolean {
165+ if ( ! payload || payload . ok !== true ) {
166+ return false ;
167+ }
168+ if ( typeof payload . workspaceDirectory !== "string" || payload . workspaceDirectory . length === 0 ) {
169+ // Legacy proxies that do not expose workspace cannot be safely reused.
170+ return false ;
171+ }
172+ return normalizeWorkspaceForCompare ( payload . workspaceDirectory ) === normalizeWorkspaceForCompare ( workspaceDirectory ) ;
173+ }
174+
155175const FORCE_TOOL_MODE = process . env . CURSOR_ACP_FORCE !== "false" ;
156176const EMIT_TOOL_UPDATES = process . env . CURSOR_ACP_EMIT_TOOL_UPDATES === "true" ;
157177const FORWARD_TOOL_CALLS = process . env . CURSOR_ACP_FORWARD_TOOL_CALLS !== "false" ;
@@ -422,21 +442,25 @@ async function findFirstAllowedToolCallInOutput(
422442async function ensureCursorProxyServer ( workspaceDirectory : string , toolRouter ?: ToolRouter ) : Promise < string > {
423443 const key = getGlobalKey ( ) ;
424444 const g = globalThis as any ;
445+ const normalizedWorkspace = normalizeWorkspaceForCompare ( workspaceDirectory ) ;
446+ const state : ProxyRuntimeState = g [ key ] ?? { baseURL : "" , baseURLByWorkspace : { } } ;
447+ state . baseURLByWorkspace = state . baseURLByWorkspace ?? { } ;
448+ g [ key ] = state ;
425449
426- const existingBaseURL = g [ key ] ? .baseURL ;
450+ const existingBaseURL = state . baseURLByWorkspace [ normalizedWorkspace ] ?? state . baseURL ;
427451 if ( typeof existingBaseURL === "string" && existingBaseURL . length > 0 ) {
428452 return existingBaseURL ;
429453 }
430454
431455 // Mark as starting to avoid duplicate starts in-process.
432- g [ key ] = { baseURL : "" } ;
456+ state . baseURL = "" ;
433457
434458 const handler = async ( req : Request ) : Promise < Response > => {
435459 try {
436460 const url = new URL ( req . url ) ;
437461
438462 if ( url . pathname === "/health" ) {
439- return new Response ( JSON . stringify ( { ok : true } ) , {
463+ return new Response ( JSON . stringify ( { ok : true , workspaceDirectory } ) , {
440464 status : 200 ,
441465 headers : { "Content-Type" : "application/json" } ,
442466 } ) ;
@@ -560,7 +584,7 @@ async function ensureCursorProxyServer(workspaceDirectory: string, toolRouter?:
560584
561585 const stdout = ( stdoutText || "" ) . trim ( ) ;
562586 const stderr = ( stderrText || "" ) . trim ( ) ;
563- const exitCode = child . exitCode ;
587+ const exitCode = await child . exited ;
564588 log . debug ( "cursor-agent completed (bun non-stream)" , {
565589 exitCode,
566590 stdoutChars : stdout . length ,
@@ -808,15 +832,16 @@ async function ensureCursorProxyServer(workspaceDirectory: string, toolRouter?:
808832 return ;
809833 }
810834
811- if ( child . exitCode !== 0 ) {
835+ const exitCode = await child . exited ;
836+ if ( exitCode !== 0 ) {
812837 const stderrText = await new Response ( child . stderr ) . text ( ) ;
813838 const errSource = ( stderrText || "" ) . trim ( )
814- || `cursor-agent exited with code ${ String ( child . exitCode ?? "unknown" ) } and no output` ;
839+ || `cursor-agent exited with code ${ String ( exitCode ?? "unknown" ) } and no output` ;
815840 const parsed = parseAgentError ( errSource ) ;
816841 const msg = formatErrorForUser ( parsed ) ;
817842 log . error ( "cursor-cli streaming failed" , {
818843 type : parsed . type ,
819- code : child . exitCode ,
844+ code : exitCode ,
820845 } ) ;
821846 const errChunk = createChatCompletionChunk ( id , created , model , msg , true ) ;
822847 controller . enqueue ( encoder . encode ( `data: ${ JSON . stringify ( errChunk ) } \n\n` ) ) ;
@@ -825,7 +850,7 @@ async function ensureCursorProxyServer(workspaceDirectory: string, toolRouter?:
825850 }
826851
827852 log . debug ( "cursor-agent completed (bun stream)" , {
828- exitCode : child . exitCode ,
853+ exitCode,
829854 } ) ;
830855 const doneChunk = createChatCompletionChunk ( id , created , model , "" , true ) ;
831856 controller . enqueue ( encoder . encode ( `data: ${ JSON . stringify ( doneChunk ) } \n\n` ) ) ;
@@ -860,8 +885,12 @@ async function ensureCursorProxyServer(workspaceDirectory: string, toolRouter?:
860885 try {
861886 const res = await fetch ( `http://${ CURSOR_PROXY_HOST } :${ CURSOR_PROXY_DEFAULT_PORT } /health` ) . catch ( ( ) => null ) ;
862887 if ( res && res . ok ) {
863- g [ key ] . baseURL = CURSOR_PROXY_DEFAULT_BASE_URL ;
864- return CURSOR_PROXY_DEFAULT_BASE_URL ;
888+ const payload = await res . json ( ) . catch ( ( ) => null ) ;
889+ if ( isReusableProxyHealthPayload ( payload , workspaceDirectory ) ) {
890+ state . baseURL = CURSOR_PROXY_DEFAULT_BASE_URL ;
891+ state . baseURLByWorkspace ! [ normalizedWorkspace ] = CURSOR_PROXY_DEFAULT_BASE_URL ;
892+ return CURSOR_PROXY_DEFAULT_BASE_URL ;
893+ }
865894 }
866895 } catch {
867896 // ignore
@@ -878,7 +907,7 @@ async function ensureCursorProxyServer(workspaceDirectory: string, toolRouter?:
878907
879908 if ( url . pathname === "/health" ) {
880909 res . writeHead ( 200 , { "Content-Type" : "application/json" } ) ;
881- res . end ( JSON . stringify ( { ok : true } ) ) ;
910+ res . end ( JSON . stringify ( { ok : true , workspaceDirectory } ) ) ;
882911 return ;
883912 }
884913
@@ -1326,7 +1355,8 @@ async function ensureCursorProxyServer(workspaceDirectory: string, toolRouter?:
13261355 } ) ;
13271356
13281357 const baseURL = `http://${ CURSOR_PROXY_HOST } :${ CURSOR_PROXY_DEFAULT_PORT } /v1` ;
1329- g [ key ] . baseURL = baseURL ;
1358+ state . baseURL = baseURL ;
1359+ state . baseURLByWorkspace ! [ normalizedWorkspace ] = baseURL ;
13301360 return baseURL ;
13311361 } catch ( error : any ) {
13321362 if ( error ?. code !== "EADDRINUSE" ) {
@@ -1338,8 +1368,12 @@ async function ensureCursorProxyServer(workspaceDirectory: string, toolRouter?:
13381368 try {
13391369 const res = await fetch ( `http://${ CURSOR_PROXY_HOST } :${ CURSOR_PROXY_DEFAULT_PORT } /health` ) . catch ( ( ) => null ) ;
13401370 if ( res && res . ok ) {
1341- g [ key ] . baseURL = CURSOR_PROXY_DEFAULT_BASE_URL ;
1342- return CURSOR_PROXY_DEFAULT_BASE_URL ;
1371+ const payload = await res . json ( ) . catch ( ( ) => null ) ;
1372+ if ( isReusableProxyHealthPayload ( payload , workspaceDirectory ) ) {
1373+ state . baseURL = CURSOR_PROXY_DEFAULT_BASE_URL ;
1374+ state . baseURLByWorkspace ! [ normalizedWorkspace ] = CURSOR_PROXY_DEFAULT_BASE_URL ;
1375+ return CURSOR_PROXY_DEFAULT_BASE_URL ;
1376+ }
13431377 }
13441378 } catch {
13451379 // ignore
@@ -1355,7 +1389,8 @@ async function ensureCursorProxyServer(workspaceDirectory: string, toolRouter?:
13551389
13561390 const addr = server . address ( ) as any ;
13571391 const baseURL = `http://${ CURSOR_PROXY_HOST } :${ addr . port } /v1` ;
1358- g [ key ] . baseURL = baseURL ;
1392+ state . baseURL = baseURL ;
1393+ state . baseURLByWorkspace ! [ normalizedWorkspace ] = baseURL ;
13591394 return baseURL ;
13601395 }
13611396}
0 commit comments