Skip to content

feat(agent/agentcontainers): allow auto start for discovered containers #19040

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions agent/agentcontainers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ func WithCommandEnv(ce CommandEnv) Option {
strings.HasPrefix(s, "CODER_WORKSPACE_AGENT_URL=") ||
strings.HasPrefix(s, "CODER_AGENT_TOKEN=") ||
strings.HasPrefix(s, "CODER_AGENT_AUTH=") ||
strings.HasPrefix(s, "CODER_AGENT_DEVCONTAINERS_ENABLE=")
strings.HasPrefix(s, "CODER_AGENT_DEVCONTAINERS_ENABLE=") ||
strings.HasPrefix(s, "CODER_AGENT_DEVCONTAINERS_PROJECT_DISCOVERY_ENABLE=")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed this in a previous PR #19016

})
return shell, dir, env, nil
}
Expand Down Expand Up @@ -524,23 +525,41 @@ func (api *API) discoverDevcontainersInProject(projectPath string) error {

workspaceFolder := strings.TrimSuffix(path, relativeConfigPath)

logger.Debug(api.ctx, "discovered dev container project", slog.F("workspace_folder", workspaceFolder))
logger := logger.With(slog.F("workspace_folder", workspaceFolder))
logger.Debug(api.ctx, "discovered dev container project")

api.mu.Lock()
if _, found := api.knownDevcontainers[workspaceFolder]; !found {
logger.Debug(api.ctx, "adding dev container project", slog.F("workspace_folder", workspaceFolder))
logger.Debug(api.ctx, "adding dev container project")

dc := codersdk.WorkspaceAgentDevcontainer{
ID: uuid.New(),
Name: "", // Updated later based on container state.
WorkspaceFolder: workspaceFolder,
ConfigPath: path,
Status: "", // Updated later based on container state.
Status: codersdk.WorkspaceAgentDevcontainerStatusStopped,
Dirty: false, // Updated later based on config file changes.
Container: nil,
}

config, err := api.dccli.ReadConfig(api.ctx, workspaceFolder, path, []string{})
if err != nil {
logger.Error(api.ctx, "read project configuration", slog.Error(err))
} else if config.Configuration.Customizations.Coder.AutoStart {
dc.Status = codersdk.WorkspaceAgentDevcontainerStatusStarting
}

api.knownDevcontainers[workspaceFolder] = dc
api.broadcastUpdatesLocked()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll now broadcast updates as we find the dev containers. This makes it feel a little quicker when there is a lot of files being searched through.


if dc.Status == codersdk.WorkspaceAgentDevcontainerStatusStarting {
api.asyncWg.Add(1)
go func() {
defer api.asyncWg.Done()

_ = api.CreateDevcontainer(dc.WorkspaceFolder, dc.ConfigPath)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I suppose it returns an error, should we log it somewhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CreateDevcontainer already does a reasonable amount of logging. I don't think the error returned has any additional information to what is already logged.

}()
}
}
api.mu.Unlock()
}
Expand Down
246 changes: 246 additions & 0 deletions agent/agentcontainers/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3568,4 +3568,250 @@ func TestDevcontainerDiscovery(t *testing.T) {
// This is implicitly handled by `testutil.Logger` failing when it
// detects an error has been logged.
})

t.Run("AutoStart", func(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice tests!

t.Parallel()

tests := []struct {
name string
agentDir string
fs map[string]string
expectDevcontainerCount int
setupMocks func(mDCCLI *acmock.MockDevcontainerCLI)
}{
{
name: "SingleEnabled",
agentDir: "/home/coder",
expectDevcontainerCount: 1,
fs: map[string]string{
"/home/coder/.git/HEAD": "",
"/home/coder/.devcontainer/devcontainer.json": "",
},
setupMocks: func(mDCCLI *acmock.MockDevcontainerCLI) {
gomock.InOrder(
// Given: This dev container has auto start enabled.
mDCCLI.EXPECT().ReadConfig(gomock.Any(),
"/home/coder",
"/home/coder/.devcontainer/devcontainer.json",
[]string{},
).Return(agentcontainers.DevcontainerConfig{
Configuration: agentcontainers.DevcontainerConfiguration{
Customizations: agentcontainers.DevcontainerCustomizations{
Coder: agentcontainers.CoderCustomization{
AutoStart: true,
},
},
},
}, nil),

// Then: We expect it to be started.
mDCCLI.EXPECT().Up(gomock.Any(),
"/home/coder",
"/home/coder/.devcontainer/devcontainer.json",
gomock.Any(),
).Return("", nil),
)
},
},
{
name: "SingleDisabled",
agentDir: "/home/coder",
expectDevcontainerCount: 1,
fs: map[string]string{
"/home/coder/.git/HEAD": "",
"/home/coder/.devcontainer/devcontainer.json": "",
},
setupMocks: func(mDCCLI *acmock.MockDevcontainerCLI) {
gomock.InOrder(
// Given: This dev container has auto start disabled.
mDCCLI.EXPECT().ReadConfig(gomock.Any(),
"/home/coder",
"/home/coder/.devcontainer/devcontainer.json",
[]string{},
).Return(agentcontainers.DevcontainerConfig{
Configuration: agentcontainers.DevcontainerConfiguration{
Customizations: agentcontainers.DevcontainerCustomizations{
Coder: agentcontainers.CoderCustomization{
AutoStart: false,
},
},
},
}, nil),

// Then: We expect it to _not_ be started.
mDCCLI.EXPECT().Up(gomock.Any(),
"/home/coder",
"/home/coder/.devcontainer/devcontainer.json",
gomock.Any(),
).Return("", nil).Times(0),
)
},
},
{
name: "OneEnabledOneDisabled",
agentDir: "/home/coder",
expectDevcontainerCount: 2,
fs: map[string]string{
"/home/coder/.git/HEAD": "",
"/home/coder/.devcontainer/devcontainer.json": "",
"/home/coder/project/.devcontainer.json": "",
},
setupMocks: func(mDCCLI *acmock.MockDevcontainerCLI) {
gomock.InOrder(
// Given: This dev container has auto start enabled.
mDCCLI.EXPECT().ReadConfig(gomock.Any(),
"/home/coder",
"/home/coder/.devcontainer/devcontainer.json",
[]string{},
).Return(agentcontainers.DevcontainerConfig{
Configuration: agentcontainers.DevcontainerConfiguration{
Customizations: agentcontainers.DevcontainerCustomizations{
Coder: agentcontainers.CoderCustomization{
AutoStart: true,
},
},
},
}, nil),

// Then: We expect it to be started.
mDCCLI.EXPECT().Up(gomock.Any(),
"/home/coder",
"/home/coder/.devcontainer/devcontainer.json",
gomock.Any(),
).Return("", nil),
)

gomock.InOrder(
// Given: This dev container has auto start disabled.
mDCCLI.EXPECT().ReadConfig(gomock.Any(),
"/home/coder/project",
"/home/coder/project/.devcontainer.json",
[]string{},
).Return(agentcontainers.DevcontainerConfig{
Configuration: agentcontainers.DevcontainerConfiguration{
Customizations: agentcontainers.DevcontainerCustomizations{
Coder: agentcontainers.CoderCustomization{
AutoStart: false,
},
},
},
}, nil),

// Then: We expect it to _not_ be started.
mDCCLI.EXPECT().Up(gomock.Any(),
"/home/coder/project",
"/home/coder/project/.devcontainer.json",
gomock.Any(),
).Return("", nil).Times(0),
)
},
},
{
name: "MultipleEnabled",
agentDir: "/home/coder",
expectDevcontainerCount: 2,
fs: map[string]string{
"/home/coder/.git/HEAD": "",
"/home/coder/.devcontainer/devcontainer.json": "",
"/home/coder/project/.devcontainer.json": "",
},
setupMocks: func(mDCCLI *acmock.MockDevcontainerCLI) {
gomock.InOrder(
// Given: This dev container has auto start enabled.
mDCCLI.EXPECT().ReadConfig(gomock.Any(),
"/home/coder",
"/home/coder/.devcontainer/devcontainer.json",
[]string{},
).Return(agentcontainers.DevcontainerConfig{
Configuration: agentcontainers.DevcontainerConfiguration{
Customizations: agentcontainers.DevcontainerCustomizations{
Coder: agentcontainers.CoderCustomization{
AutoStart: true,
},
},
},
}, nil),

// Then: We expect it to be started.
mDCCLI.EXPECT().Up(gomock.Any(),
"/home/coder",
"/home/coder/.devcontainer/devcontainer.json",
gomock.Any(),
).Return("", nil),
)

gomock.InOrder(
// Given: This dev container has auto start enabled.
mDCCLI.EXPECT().ReadConfig(gomock.Any(),
"/home/coder/project",
"/home/coder/project/.devcontainer.json",
[]string{},
).Return(agentcontainers.DevcontainerConfig{
Configuration: agentcontainers.DevcontainerConfiguration{
Customizations: agentcontainers.DevcontainerCustomizations{
Coder: agentcontainers.CoderCustomization{
AutoStart: true,
},
},
},
}, nil),

// Then: We expect it to be started.
mDCCLI.EXPECT().Up(gomock.Any(),
"/home/coder/project",
"/home/coder/project/.devcontainer.json",
gomock.Any(),
).Return("", nil),
)
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

var (
ctx = testutil.Context(t, testutil.WaitShort)
logger = testutil.Logger(t)
mClock = quartz.NewMock(t)
mDCCLI = acmock.NewMockDevcontainerCLI(gomock.NewController(t))

r = chi.NewRouter()
)

// Given: We setup our mocks. These mocks handle our expectations for these
// tests. If there are missing/unexpected mock calls, the test will fail.
tt.setupMocks(mDCCLI)

api := agentcontainers.NewAPI(logger,
agentcontainers.WithClock(mClock),
agentcontainers.WithWatcher(watcher.NewNoop()),
agentcontainers.WithFileSystem(initFS(t, tt.fs)),
agentcontainers.WithManifestInfo("owner", "workspace", "parent-agent", "/home/coder"),
agentcontainers.WithContainerCLI(&fakeContainerCLI{}),
agentcontainers.WithDevcontainerCLI(mDCCLI),
agentcontainers.WithProjectDiscovery(true),
)
api.Start()
defer api.Close()
r.Mount("/", api.Routes())

// When: All expected dev containers have been found.
require.Eventuallyf(t, func() bool {
req := httptest.NewRequest(http.MethodGet, "/", nil).WithContext(ctx)
rec := httptest.NewRecorder()
r.ServeHTTP(rec, req)

got := codersdk.WorkspaceAgentListContainersResponse{}
err := json.NewDecoder(rec.Body).Decode(&got)
require.NoError(t, err)

return len(got.Devcontainers) >= tt.expectDevcontainerCount
}, testutil.WaitShort, testutil.IntervalFast, "dev containers never found")

// Then: We expect the mock infra to not fail.
})
}
})
}
1 change: 1 addition & 0 deletions agent/agentcontainers/devcontainercli.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ type CoderCustomization struct {
Apps []SubAgentApp `json:"apps,omitempty"`
Name string `json:"name,omitempty"`
Ignore bool `json:"ignore,omitempty"`
AutoStart bool `json:"autoStart,omitempty"`
}

type DevcontainerWorkspace struct {
Expand Down
Loading