Skip to content

Commit bacdc28

Browse files
authored
fix(cli)!: enforce selection for multiple agents rather than use randomness (#18427)
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), we have now made this an error state and require the specifying of agent when there is more than one (either normal agent or sub agent). This aligns with the behavior of e.g. Coder Desktop. Fixes coder/internal#696
1 parent 63b5f0b commit bacdc28

File tree

2 files changed

+109
-17
lines changed

2 files changed

+109
-17
lines changed

cli/ssh.go

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -925,36 +925,33 @@ 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+
)
929932
for _, resource := range resources {
930-
agents = append(agents, resource.Agents...)
933+
for _, agent := range resource.Agents {
934+
availableNames = append(availableNames, agent.Name)
935+
agents = append(agents, agent)
936+
}
931937
}
932938
if len(agents) == 0 {
933939
return codersdk.WorkspaceAgent{}, xerrors.Errorf("workspace %q has no agents", workspace.Name)
934940
}
941+
slices.Sort(availableNames)
935942
if agentName != "" {
936943
for _, otherAgent := range agents {
937944
if otherAgent.Name != agentName {
938945
continue
939946
}
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)
947+
return otherAgent, nil
945948
}
949+
return codersdk.WorkspaceAgent{}, xerrors.Errorf("agent not found by name %q, available agents: %v", agentName, availableNames)
946950
}
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-
}
951+
if len(agents) == 1 {
952+
return agents[0], nil
956953
}
957-
return workspaceAgent, nil
954+
return codersdk.WorkspaceAgent{}, xerrors.Errorf("multiple agents found, please specify the agent name, available agents: %v", availableNames)
958955
}
959956

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

cli/ssh_internal_test.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"time"
1212

1313
gliderssh "github.com/gliderlabs/ssh"
14+
"github.com/google/uuid"
1415
"github.com/stretchr/testify/assert"
1516
"github.com/stretchr/testify/require"
1617
"golang.org/x/crypto/ssh"
@@ -346,3 +347,97 @@ func newAsyncCloser(ctx context.Context, t *testing.T) *asyncCloser {
346347
started: make(chan struct{}),
347348
}
348349
}
350+
351+
func Test_getWorkspaceAgent(t *testing.T) {
352+
t.Parallel()
353+
354+
createWorkspaceWithAgents := func(agents []codersdk.WorkspaceAgent) codersdk.Workspace {
355+
return codersdk.Workspace{
356+
Name: "test-workspace",
357+
LatestBuild: codersdk.WorkspaceBuild{
358+
Resources: []codersdk.WorkspaceResource{
359+
{
360+
Agents: agents,
361+
},
362+
},
363+
},
364+
}
365+
}
366+
367+
createAgent := func(name string) codersdk.WorkspaceAgent {
368+
return codersdk.WorkspaceAgent{
369+
ID: uuid.New(),
370+
Name: name,
371+
}
372+
}
373+
374+
t.Run("SingleAgent_NoNameSpecified", func(t *testing.T) {
375+
t.Parallel()
376+
agent := createAgent("main")
377+
workspace := createWorkspaceWithAgents([]codersdk.WorkspaceAgent{agent})
378+
379+
result, err := getWorkspaceAgent(workspace, "")
380+
require.NoError(t, err)
381+
assert.Equal(t, agent.ID, result.ID)
382+
assert.Equal(t, "main", result.Name)
383+
})
384+
385+
t.Run("MultipleAgents_NoNameSpecified", func(t *testing.T) {
386+
t.Parallel()
387+
agent1 := createAgent("main1")
388+
agent2 := createAgent("main2")
389+
workspace := createWorkspaceWithAgents([]codersdk.WorkspaceAgent{agent1, agent2})
390+
391+
_, err := getWorkspaceAgent(workspace, "")
392+
require.Error(t, err)
393+
assert.Contains(t, err.Error(), "multiple agents found")
394+
assert.Contains(t, err.Error(), "available agents: [main1 main2]")
395+
})
396+
397+
t.Run("AgentNameSpecified_Found", func(t *testing.T) {
398+
t.Parallel()
399+
agent1 := createAgent("main1")
400+
agent2 := createAgent("main2")
401+
workspace := createWorkspaceWithAgents([]codersdk.WorkspaceAgent{agent1, agent2})
402+
403+
result, err := getWorkspaceAgent(workspace, "main1")
404+
require.NoError(t, err)
405+
assert.Equal(t, agent1.ID, result.ID)
406+
assert.Equal(t, "main1", result.Name)
407+
})
408+
409+
t.Run("AgentNameSpecified_NotFound", func(t *testing.T) {
410+
t.Parallel()
411+
agent1 := createAgent("main1")
412+
agent2 := createAgent("main2")
413+
workspace := createWorkspaceWithAgents([]codersdk.WorkspaceAgent{agent1, agent2})
414+
415+
_, err := getWorkspaceAgent(workspace, "nonexistent")
416+
require.Error(t, err)
417+
assert.Contains(t, err.Error(), `agent not found by name "nonexistent"`)
418+
assert.Contains(t, err.Error(), "available agents: [main1 main2]")
419+
})
420+
421+
t.Run("NoAgents", func(t *testing.T) {
422+
t.Parallel()
423+
workspace := createWorkspaceWithAgents([]codersdk.WorkspaceAgent{})
424+
425+
_, err := getWorkspaceAgent(workspace, "")
426+
require.Error(t, err)
427+
assert.Contains(t, err.Error(), `workspace "test-workspace" has no agents`)
428+
})
429+
430+
t.Run("AvailableAgentNames_SortedCorrectly", func(t *testing.T) {
431+
t.Parallel()
432+
// Define agents in non-alphabetical order.
433+
agent2 := createAgent("zod")
434+
agent1 := createAgent("clark")
435+
agent3 := createAgent("krypton")
436+
workspace := createWorkspaceWithAgents([]codersdk.WorkspaceAgent{agent2, agent1, agent3})
437+
438+
_, err := getWorkspaceAgent(workspace, "nonexistent")
439+
require.Error(t, err)
440+
// Available agents should be sorted alphabetically.
441+
assert.Contains(t, err.Error(), "available agents: [clark krypton zod]")
442+
})
443+
}

0 commit comments

Comments
 (0)