@@ -63,7 +63,7 @@ class CoderRemoteProvider(
6363) : RemoteProvider(" Coder" ) {
6464 // Current polling job.
6565 private var pollJob: Job ? = null
66- private val lastEnvironments = mutableSetOf <CoderRemoteEnvironment >()
66+ internal val lastEnvironments = mutableListOf <CoderRemoteEnvironment >()
6767
6868 private val settings = context.settingsStore.readOnly()
6969
@@ -116,29 +116,7 @@ class CoderRemoteProvider(
116116 while (isActive) {
117117 try {
118118 context.logger.debug(" Fetching workspace agents from ${client.url} " )
119- val resolvedEnvironments = client.workspaces().flatMap { ws ->
120- // Agents are not included in workspaces that are off
121- // so fetch them separately.
122- when (ws.latestBuild.status) {
123- WorkspaceStatus .RUNNING -> ws.latestBuild.resources
124- else -> emptyList()
125- }.ifEmpty {
126- client.resources(ws)
127- }.flatMap { resource ->
128- resource.agents?.distinctBy {
129- // There can be duplicates with coder_agent_instance.
130- // TODO: Can we just choose one or do they hold
131- // different information?
132- it.name
133- }?.map { agent ->
134- lastEnvironments.firstOrNull { it.id == " ${ws.name} .${agent.name} " }
135- ?.also {
136- // If we have an environment already, update that.
137- it.update(ws, agent)
138- } ? : CoderRemoteEnvironment (context, client, cli, ws, agent)
139- } ? : emptyList()
140- }
141- }.toSet().sortedBy { it.id }
119+ val resolvedEnvironments = resolveWorkspaceEnvironments(client, cli)
142120
143121 // In case we logged out while running the query.
144122 if (! isActive) {
@@ -202,6 +180,42 @@ class CoderRemoteProvider(
202180 }
203181 }
204182
183+ /* *
184+ * Resolves workspace agents into remote environments.
185+ *
186+ * For each workspace:
187+ * - If running, uses agents from the latest build resources
188+ * - If not running, fetches resources separately
189+ *
190+ * @return a sorted list of resolved remote environments
191+ */
192+ internal suspend fun resolveWorkspaceEnvironments (
193+ client : CoderRestClient ,
194+ cli : CoderCLIManager ,
195+ ): List <CoderRemoteEnvironment > {
196+ return client.workspaces().flatMap { ws ->
197+ // Agents are not included in workspaces that are off
198+ // so fetch them separately.
199+ val resources = when (ws.latestBuild.status) {
200+ WorkspaceStatus .RUNNING -> ws.latestBuild.resources
201+ else -> emptyList()
202+ }.ifEmpty {
203+ client.resources(ws)
204+ }
205+ resources
206+ .flatMap { it.agents ? : emptyList() }
207+ .distinctBy { it.name }
208+ .map { agent ->
209+ lastEnvironments.firstOrNull { it.id == " ${ws.name} .${agent.name} " }
210+ ?.also {
211+ // If we have an environment already, update that.
212+ it.update(ws, agent)
213+ } ? : CoderRemoteEnvironment (context, client, cli, ws, agent)
214+ }
215+
216+ }.sortedBy { it.id }
217+ }
218+
205219 /* *
206220 * Stop polling, clear the client and environments, then go back to the
207221 * first page.
0 commit comments