@@ -32,10 +32,7 @@ export class WorkspaceStateMachine implements vscode.Disposable {
3232
3333private 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
4037private agentLogSocket :OneWayWebSocket < WorkspaceAgentLog [ ] > | null = null ;
4138
@@ -58,7 +55,7 @@ export class WorkspaceStateMachine implements vscode.Disposable {
5855 */
5956async processWorkspace (
6057workspace :Workspace ,
61- progress ? :vscode . Progress < { message ?:string } > ,
58+ progress :vscode . Progress < { message ?:string } > ,
6259) :Promise < boolean > {
6360const workspaceName = createWorkspaceIdentifier ( workspace ) ;
6461
@@ -75,7 +72,7 @@ export class WorkspaceStateMachine implements vscode.Disposable {
7572throw new Error ( `User declined to start${ workspaceName } ` ) ;
7673}
7774
78- progress ? .report ( { message :`Starting${ workspaceName } ...` } ) ;
75+ progress . report ( { message :`Starting${ workspaceName } ...` } ) ;
7976this . logger . info ( `Starting${ workspaceName } ...` ) ;
8077const globalConfigDir = this . pathResolver . getGlobalConfigDir (
8178this . parts . label ,
@@ -95,20 +92,19 @@ export class WorkspaceStateMachine implements vscode.Disposable {
9592case "pending" :
9693case "starting" :
9794case "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+ } ) ;
99101this . 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+ ) ;
112108return false ;
113109
114110case "deleted" :
@@ -117,12 +113,6 @@ export class WorkspaceStateMachine implements vscode.Disposable {
117113case "canceling" :
118114this . closeBuildLogSocket ( ) ;
119115throw 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
128118const agents = extractAgents ( workspace . latest_build . resources ) ;
@@ -144,33 +134,30 @@ export class WorkspaceStateMachine implements vscode.Disposable {
144134throw new Error ( `Agent not found in${ workspaceName } resources` ) ;
145135}
146136
137+ const agentName = `${ workspaceName } /${ agent . name } ` ;
138+
147139switch ( agent . status ) {
148140case "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 } ...` ) ;
153145return false ;
154146
155147case "disconnected" :
156- throw new Error ( `${ workspaceName } / ${ agent . name } disconnected` ) ;
148+ throw new Error ( `${ agentName } disconnected` ) ;
157149
158150case "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} ) ;
162154this . logger . debug (
163- `Agent${ agent . name } timed out, continuing to wait...` ,
155+ `Agent${ agentName } timed out, continuing to wait...` ,
164156) ;
165157return false ;
166158
167159case "connected" :
168160break ;
169-
170- default :
171- throw new Error (
172- `${ workspaceName } /${ agent . name } unknown status:${ agent . status } ` ,
173- ) ;
174161}
175162
176163switch ( agent . lifecycle_state ) {
@@ -186,10 +173,10 @@ export class WorkspaceStateMachine implements vscode.Disposable {
186173return true ;
187174}
188175
189- progress ? .report ( {
190- message :`Waiting foragent ${ agent . name } startup scripts...` ,
176+ progress . report ( {
177+ message :`Waiting for${ agentName } startup scripts...` ,
191178} ) ;
192- this . logger . debug ( `Waiting foragent ${ agent . name } startup scripts...` ) ;
179+ this . logger . debug ( `Waiting for${ agentName } startup scripts...` ) ;
193180
194181this . agentLogSocket ??= await streamAgentLogs (
195182this . workspaceClient ,
@@ -200,44 +187,41 @@ export class WorkspaceStateMachine implements vscode.Disposable {
200187}
201188
202189case "created" :
203- progress ? .report ( {
204- message :`Waiting foragent ${ 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...` ) ;
209194return false ;
210195
211196case "start_error" :
212197this . closeAgentLogSocket ( ) ;
213198this . logger . info (
214- `Agent${ agent . name } startup script failed, but continuing...` ,
199+ `Agent${ agentName } startup script failed, but continuing...` ,
215200) ;
216201return true ;
217202
218203case "start_timeout" :
219204this . closeAgentLogSocket ( ) ;
220205this . logger . info (
221- `Agent${ agent . name } startup script timed out, but continuing...` ,
206+ `Agent${ agentName } startup script timed out, but continuing...` ,
222207) ;
223208return true ;
224209
210+ case "shutting_down" :
225211case "off" :
226- this . closeAgentLogSocket ( ) ;
227- throw new Error ( `${ workspaceName } /${ agent . name } is off` ) ;
228-
229- default :
212+ case "shutdown_error" :
213+ case "shutdown_timeout" :
230214this . closeAgentLogSocket ( ) ;
231215throw 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
237221private 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