@@ -144,6 +144,7 @@ interface ManagedSession {
144144 lastActivityAt : number ;
145145 mockNodeDir : string ;
146146 config : SessionConfig ;
147+ needsRecreation : boolean ;
147148}
148149
149150function getClaudeCliPath ( ) : string {
@@ -168,7 +169,17 @@ export class AgentService extends TypedEventEmitter<AgentServiceEvents> {
168169
169170 public updateToken ( newToken : string ) : void {
170171 this . currentToken = newToken ;
171- log . info ( "Session token updated" ) ;
172+
173+ // Mark all sessions for recreation - they'll be recreated before the next prompt.
174+ // We don't recreate immediately because the subprocess may be mid-response or
175+ // waiting on a permission prompt. Recreation happens at a safe point.
176+ for ( const session of this . sessions . values ( ) ) {
177+ session . needsRecreation = true ;
178+ }
179+
180+ log . info ( "Token updated, marked sessions for recreation" , {
181+ sessionCount : this . sessions . size ,
182+ } ) ;
172183 }
173184
174185 /**
@@ -335,6 +346,7 @@ export class AgentService extends TypedEventEmitter<AgentServiceEvents> {
335346 const { clientStreams } = await agent . runTaskV2 ( taskId , taskRunId , {
336347 skipGitBranch : true ,
337348 framework,
349+ isReconnect,
338350 } ) ;
339351
340352 const connection = this . createClientConnection (
@@ -385,6 +397,7 @@ export class AgentService extends TypedEventEmitter<AgentServiceEvents> {
385397 lastActivityAt : Date . now ( ) ,
386398 mockNodeDir,
387399 config,
400+ needsRecreation : false ,
388401 } ;
389402
390403 this . sessions . set ( taskRunId , session ) ;
@@ -416,7 +429,7 @@ export class AgentService extends TypedEventEmitter<AgentServiceEvents> {
416429 throw new Error ( `Session not found for recreation: ${ taskRunId } ` ) ;
417430 }
418431
419- log . info ( "Recreating session due to auth error " , { taskRunId } ) ;
432+ log . info ( "Recreating session" , { taskRunId } ) ;
420433
421434 const config = existing . config ;
422435 this . cleanupSession ( taskRunId ) ;
@@ -438,6 +451,12 @@ export class AgentService extends TypedEventEmitter<AgentServiceEvents> {
438451 throw new Error ( `Session not found: ${ sessionId } ` ) ;
439452 }
440453
454+ // Recreate session if marked (token was refreshed while session was active)
455+ if ( session . needsRecreation ) {
456+ log . info ( "Recreating session before prompt (token refreshed)" , { sessionId } ) ;
457+ session = await this . recreateSession ( sessionId ) ;
458+ }
459+
441460 session . lastActivityAt = Date . now ( ) ;
442461
443462 try {
0 commit comments