@@ -150,7 +150,12 @@ export class NewAgentPresenter {
150150 if ( input . generationSettings ) {
151151 initConfig . generationSettings = input . generationSettings
152152 }
153- await agent . initSession ( sessionId , initConfig )
153+ try {
154+ await this . initializeSessionRuntime ( agent , sessionId , initConfig )
155+ } catch ( error ) {
156+ await this . cleanupFailedSessionInitialization ( agent , sessionId )
157+ throw error
158+ }
154159 console . log ( `[NewAgentPresenter] agent.initSession done` )
155160
156161 // Bind to window and emit activated
@@ -224,13 +229,18 @@ export class NewAgentPresenter {
224229 const sessionId = this . sessionManager . create ( agentId , 'New Chat' , projectDir , {
225230 isDraft : true
226231 } )
227- await this . ensureSessionRuntimeInitialized ( agent , sessionId , {
228- agentId,
229- providerId : 'acp' ,
230- modelId : agentId ,
231- projectDir,
232- permissionMode
233- } )
232+ try {
233+ await this . ensureSessionRuntimeInitialized ( agent , sessionId , {
234+ agentId,
235+ providerId : 'acp' ,
236+ modelId : agentId ,
237+ projectDir,
238+ permissionMode
239+ } )
240+ } catch ( error ) {
241+ await this . cleanupFailedSessionInitialization ( agent , sessionId )
242+ throw error
243+ }
234244 record = this . sessionManager . get ( sessionId )
235245 if ( ! record ) {
236246 throw new Error ( `Failed to read created ACP draft session: ${ sessionId } ` )
@@ -280,6 +290,12 @@ export class NewAgentPresenter {
280290 }
281291 }
282292 this . assertAcpSessionHasWorkdir ( providerId , session . projectDir ?? null )
293+ await this . syncAcpSessionWorkdir (
294+ providerId ,
295+ sessionId ,
296+ session . agentId ,
297+ session . projectDir ?? null
298+ )
283299 if ( agent . queuePendingInput ) {
284300 await agent . queuePendingInput ( sessionId , normalizedInput )
285301 return
@@ -329,6 +345,12 @@ export class NewAgentPresenter {
329345 }
330346 }
331347 this . assertAcpSessionHasWorkdir ( providerId , currentSession . projectDir ?? null )
348+ await this . syncAcpSessionWorkdir (
349+ providerId ,
350+ sessionId ,
351+ currentSession . agentId ,
352+ currentSession . projectDir ?? null
353+ )
332354 return await agent . queuePendingInput ( sessionId , normalizedInput )
333355 }
334356
@@ -465,7 +487,7 @@ export class NewAgentPresenter {
465487 )
466488
467489 try {
468- await agent . initSession ( targetSessionId , {
490+ await this . initializeSessionRuntime ( agent , targetSessionId , {
469491 agentId : sourceSession . agentId ,
470492 providerId : sourceState . providerId ,
471493 modelId : sourceState . modelId ,
@@ -1296,7 +1318,7 @@ export class NewAgentPresenter {
12961318 ) : Promise < void > {
12971319 const state = await agent . getSessionState ( sessionId )
12981320 if ( ! state ) {
1299- await agent . initSession ( sessionId , config )
1321+ await this . initializeSessionRuntime ( agent , sessionId , config )
13001322 return
13011323 }
13021324
@@ -1307,6 +1329,82 @@ export class NewAgentPresenter {
13071329 ) {
13081330 await agent . setPermissionMode ( sessionId , config . permissionMode )
13091331 }
1332+
1333+ await this . syncAcpSessionWorkdir (
1334+ config . providerId ,
1335+ sessionId ,
1336+ config . agentId ?? config . modelId ,
1337+ config . projectDir
1338+ )
1339+ }
1340+
1341+ private async initializeSessionRuntime (
1342+ agent : IAgentImplementation ,
1343+ sessionId : string ,
1344+ config : {
1345+ agentId ?: string
1346+ providerId : string
1347+ modelId : string
1348+ projectDir ?: string | null
1349+ permissionMode : PermissionMode
1350+ generationSettings ?: Partial < SessionGenerationSettings >
1351+ }
1352+ ) : Promise < void > {
1353+ await agent . initSession ( sessionId , config )
1354+ await this . syncAcpSessionWorkdir (
1355+ config . providerId ,
1356+ sessionId ,
1357+ config . agentId ?? config . modelId ,
1358+ config . projectDir ?? null
1359+ )
1360+ }
1361+
1362+ private async syncAcpSessionWorkdir (
1363+ providerId : string ,
1364+ conversationId : string ,
1365+ agentId : string ,
1366+ projectDir ?: string | null
1367+ ) : Promise < void > {
1368+ if ( providerId !== 'acp' ) {
1369+ return
1370+ }
1371+
1372+ const normalizedProjectDir = projectDir ?. trim ( )
1373+ if ( ! normalizedProjectDir ) {
1374+ return
1375+ }
1376+
1377+ try {
1378+ await this . llmProviderPresenter . setAcpWorkdir (
1379+ conversationId ,
1380+ resolveAcpAgentAlias ( agentId ) ,
1381+ normalizedProjectDir
1382+ )
1383+ } catch ( error ) {
1384+ console . warn ( '[NewAgentPresenter] Failed to sync ACP workdir for session:' , {
1385+ conversationId,
1386+ agentId,
1387+ projectDir : normalizedProjectDir ,
1388+ error
1389+ } )
1390+ throw error
1391+ }
1392+ }
1393+
1394+ private async cleanupFailedSessionInitialization (
1395+ agent : IAgentImplementation ,
1396+ sessionId : string
1397+ ) : Promise < void > {
1398+ try {
1399+ await agent . destroySession ( sessionId )
1400+ } catch ( cleanupError ) {
1401+ console . warn (
1402+ `[NewAgentPresenter] Failed to cleanup session runtime after initialization error ${ sessionId } :` ,
1403+ cleanupError
1404+ )
1405+ }
1406+
1407+ this . sessionManager . delete ( sessionId )
13101408 }
13111409
13121410 private buildExportConversation (
0 commit comments