Skip to content

Commit 647181e

Browse files
committed
fix(cli)!: select sub agent if available and error on multiple agents
In the past we randomly selected workspace agent if there were multiple. Unless both are running on the same machine with the same configuration, this would be very confusing behavior for a user. With the introduction of sub agents (devcontainer agents), it was decided prioritize them if present. Similarly as before, selecting a devcontainer randomly would be confusing. We now error out if the agent name is not specified and there are multiple agents. Fixes coder/internal#696
1 parent 5e3a225 commit 647181e

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

cli/ssh.go

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -925,36 +925,44 @@ func getWorkspaceAndAgent(ctx context.Context, inv *serpent.Invocation, client *
925925
func getWorkspaceAgent(workspace codersdk.Workspace, agentName string) (workspaceAgent codersdk.WorkspaceAgent, err error) {
926926
resources := workspace.LatestBuild.Resources
927927

928-
agents := make([]codersdk.WorkspaceAgent, 0)
928+
var (
929+
availableNames []string
930+
agents []codersdk.WorkspaceAgent
931+
subAgents []codersdk.WorkspaceAgent
932+
)
929933
for _, resource := range resources {
930-
agents = append(agents, resource.Agents...)
934+
for _, agent := range resource.Agents {
935+
availableNames = append(availableNames, agent.Name)
936+
if agent.ParentID.UUID == uuid.Nil {
937+
agents = append(agents, agent)
938+
} else {
939+
subAgents = append(subAgents, agent)
940+
}
941+
}
931942
}
932943
if len(agents) == 0 {
933944
return codersdk.WorkspaceAgent{}, xerrors.Errorf("workspace %q has no agents", workspace.Name)
934945
}
946+
slices.Sort(availableNames)
935947
if agentName != "" {
948+
agents = append(agents, subAgents...)
936949
for _, otherAgent := range agents {
937950
if otherAgent.Name != agentName {
938951
continue
939952
}
940-
workspaceAgent = otherAgent
941-
break
942-
}
943-
if workspaceAgent.ID == uuid.Nil {
944-
return codersdk.WorkspaceAgent{}, xerrors.Errorf("agent not found by name %q", agentName)
953+
return otherAgent, nil
945954
}
955+
return codersdk.WorkspaceAgent{}, xerrors.Errorf("agent not found by name %q, available agents: %v", agentName, availableNames)
946956
}
947-
if workspaceAgent.ID == uuid.Nil {
948-
if len(agents) > 1 {
949-
workspaceAgent, err = cryptorand.Element(agents)
950-
if err != nil {
951-
return codersdk.WorkspaceAgent{}, err
952-
}
953-
} else {
954-
workspaceAgent = agents[0]
955-
}
957+
if len(subAgents) == 1 {
958+
return subAgents[0], nil
959+
} else if len(subAgents) > 1 {
960+
return codersdk.WorkspaceAgent{}, xerrors.Errorf("multiple sub-agents found, please specify the agent name, available agents: %v", availableNames)
961+
}
962+
if len(agents) == 1 {
963+
return agents[0], nil
956964
}
957-
return workspaceAgent, nil
965+
return codersdk.WorkspaceAgent{}, xerrors.Errorf("multiple agents found, please specify the agent name, available agents: %v", availableNames)
958966
}
959967

960968
// Attempt to poll workspace autostop. We write a per-workspace lockfile to

0 commit comments

Comments
 (0)