@@ -32,10 +32,7 @@ export class WorkspaceStateMachine implements vscode.Disposable {
3232
3333 private agentId : string | undefined ;
3434
35- private buildLogSocket : {
36- socket : OneWayWebSocket < ProvisionerJobLog > | null ;
37- buildId : string | null ;
38- } = { socket : null , buildId : null } ;
35+ private buildLogSocket : OneWayWebSocket < ProvisionerJobLog > | null = null ;
3936
4037 private agentLogSocket : OneWayWebSocket < WorkspaceAgentLog [ ] > | null = null ;
4138
@@ -58,7 +55,7 @@ export class WorkspaceStateMachine implements vscode.Disposable {
5855 */
5956 async processWorkspace (
6057 workspace : Workspace ,
61- progress ? : vscode . Progress < { message ?: string } > ,
58+ progress : vscode . Progress < { message ?: string } > ,
6259 ) : Promise < boolean > {
6360 const workspaceName = createWorkspaceIdentifier ( workspace ) ;
6461
@@ -75,7 +72,7 @@ export class WorkspaceStateMachine implements vscode.Disposable {
7572 throw new Error ( `User declined to start ${ workspaceName } ` ) ;
7673 }
7774
78- progress ? .report ( { message : `Starting ${ workspaceName } ...` } ) ;
75+ progress . report ( { message : `Starting ${ workspaceName } ...` } ) ;
7976 this . logger . info ( `Starting ${ workspaceName } ...` ) ;
8077 const globalConfigDir = this . pathResolver . getGlobalConfigDir (
8178 this . parts . label ,
@@ -95,20 +92,19 @@ export class WorkspaceStateMachine implements vscode.Disposable {
9592 case "pending" :
9693 case "starting" :
9794 case "stopping" :
98- progress ?. report ( { message : "Waiting for workspace build..." } ) ;
95+ // Clear the agent ID since it could change after a restart
96+ this . agentId = undefined ;
97+ this . closeAgentLogSocket ( ) ;
98+ progress . report ( {
99+ message : `Waiting for workspace build (${ workspace . latest_build . status } )...` ,
100+ } ) ;
99101 this . logger . info ( `Waiting for ${ workspaceName } ...` ) ;
100102
101- if ( ! this . buildLogSocket . socket ) {
102- const socket = await streamBuildLogs (
103- this . workspaceClient ,
104- this . terminal . writeEmitter ,
105- workspace ,
106- ) ;
107- this . buildLogSocket = {
108- socket,
109- buildId : workspace . latest_build . id ,
110- } ;
111- }
103+ this . buildLogSocket ??= await streamBuildLogs (
104+ this . workspaceClient ,
105+ this . terminal . writeEmitter ,
106+ workspace ,
107+ ) ;
112108 return false ;
113109
114110 case "deleted" :
@@ -117,12 +113,6 @@ export class WorkspaceStateMachine implements vscode.Disposable {
117113 case "canceling" :
118114 this . closeBuildLogSocket ( ) ;
119115 throw new Error ( `${ workspaceName } is ${ workspace . latest_build . status } ` ) ;
120-
121- default :
122- this . closeBuildLogSocket ( ) ;
123- throw new Error (
124- `${ workspaceName } unknown status: ${ workspace . latest_build . status } ` ,
125- ) ;
126116 }
127117
128118 const agents = extractAgents ( workspace . latest_build . resources ) ;
@@ -144,33 +134,30 @@ export class WorkspaceStateMachine implements vscode.Disposable {
144134 throw new Error ( `Agent not found in ${ workspaceName } resources` ) ;
145135 }
146136
137+ const agentName = `${ workspaceName } /${ agent . name } ` ;
138+
147139 switch ( agent . status ) {
148140 case "connecting" :
149- progress ? .report ( {
150- message : `Waiting for agent ${ agent . name } to connect...` ,
141+ progress . report ( {
142+ message : `Waiting for agent ${ agentName } to connect...` ,
151143 } ) ;
152- this . logger . debug ( `Waiting for agent ${ agent . name } ...` ) ;
144+ this . logger . debug ( `Waiting for agent ${ agentName } ...` ) ;
153145 return false ;
154146
155147 case "disconnected" :
156- throw new Error ( `${ workspaceName } / ${ agent . name } disconnected` ) ;
148+ throw new Error ( `${ agentName } disconnected` ) ;
157149
158150 case "timeout" :
159- progress ? .report ( {
160- message : `Agent ${ agent . name } timed out, continuing to wait...` ,
151+ progress . report ( {
152+ message : `Agent ${ agentName } timed out, continuing to wait...` ,
161153 } ) ;
162154 this . logger . debug (
163- `Agent ${ agent . name } timed out, continuing to wait...` ,
155+ `Agent ${ agentName } timed out, continuing to wait...` ,
164156 ) ;
165157 return false ;
166158
167159 case "connected" :
168160 break ;
169-
170- default :
171- throw new Error (
172- `${ workspaceName } /${ agent . name } unknown status: ${ agent . status } ` ,
173- ) ;
174161 }
175162
176163 switch ( agent . lifecycle_state ) {
@@ -186,10 +173,10 @@ export class WorkspaceStateMachine implements vscode.Disposable {
186173 return true ;
187174 }
188175
189- progress ? .report ( {
190- message : `Waiting for agent ${ agent . name } startup scripts...` ,
176+ progress . report ( {
177+ message : `Waiting for ${ agentName } startup scripts...` ,
191178 } ) ;
192- this . logger . debug ( `Waiting for agent ${ agent . name } startup scripts...` ) ;
179+ this . logger . debug ( `Waiting for ${ agentName } startup scripts...` ) ;
193180
194181 this . agentLogSocket ??= await streamAgentLogs (
195182 this . workspaceClient ,
@@ -200,44 +187,41 @@ export class WorkspaceStateMachine implements vscode.Disposable {
200187 }
201188
202189 case "created" :
203- progress ? .report ( {
204- message : `Waiting for agent ${ agent . name } to start...` ,
190+ progress . report ( {
191+ message : `Waiting for ${ agentName } to start...` ,
205192 } ) ;
206- this . logger . debug (
207- `Waiting for ${ workspaceName } /${ agent . name } to start...` ,
208- ) ;
193+ this . logger . debug ( `Waiting for ${ agentName } to start...` ) ;
209194 return false ;
210195
211196 case "start_error" :
212197 this . closeAgentLogSocket ( ) ;
213198 this . logger . info (
214- `Agent ${ agent . name } startup script failed, but continuing...` ,
199+ `Agent ${ agentName } startup script failed, but continuing...` ,
215200 ) ;
216201 return true ;
217202
218203 case "start_timeout" :
219204 this . closeAgentLogSocket ( ) ;
220205 this . logger . info (
221- `Agent ${ agent . name } startup script timed out, but continuing...` ,
206+ `Agent ${ agentName } startup script timed out, but continuing...` ,
222207 ) ;
223208 return true ;
224209
210+ case "shutting_down" :
225211 case "off" :
226- this . closeAgentLogSocket ( ) ;
227- throw new Error ( `${ workspaceName } /${ agent . name } is off` ) ;
228-
229- default :
212+ case "shutdown_error" :
213+ case "shutdown_timeout" :
230214 this . closeAgentLogSocket ( ) ;
231215 throw new Error (
232- `${ workspaceName } / ${ agent . name } unknown lifecycle state: ${ agent . lifecycle_state } ` ,
216+ `Invalid lifecycle state ' ${ agent . lifecycle_state } ' for ${ agentName } ` ,
233217 ) ;
234218 }
235219 }
236220
237221 private closeBuildLogSocket ( ) : void {
238- if ( this . buildLogSocket . socket ) {
239- this . buildLogSocket . socket . close ( ) ;
240- this . buildLogSocket = { socket : null , buildId : null } ;
222+ if ( this . buildLogSocket ) {
223+ this . buildLogSocket . close ( ) ;
224+ this . buildLogSocket = null ;
241225 }
242226 }
243227
0 commit comments