Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat(agent/agentcontainers): auto detect dev containers#18950

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
DanielleMaywood merged 13 commits intomainfromdanielle/detect-devcontainer-projects
Jul 22, 2025
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
13 commits
Select commitHold shift + click to select a range
f528eb3
feat(agent/agentcontainers): auto detect dev containers
DanielleMaywoodJul 16, 2025
c3620e2
chore: add tests and fix bug
DanielleMaywoodJul 21, 2025
5d352c9
fix: some issues i left in
DanielleMaywoodJul 21, 2025
b895f49
fix: gate project discovery behind a flag
DanielleMaywoodJul 22, 2025
21f1acc
fix: disable project discovery for cli TestSSH_Container tests
DanielleMaywoodJul 22, 2025
8c8b46b
fix: disable project discovery in more tests
DanielleMaywoodJul 22, 2025
d463ed5
fix: disable dev container tests on windows
DanielleMaywoodJul 22, 2025
2f13214
fix: only run project discovery when agentDirectory is not empty
DanielleMaywoodJul 22, 2025
a000330
chore: call RefreshContainers after discovery has finished
DanielleMaywoodJul 22, 2025
4170809
chore: drop `/` from `.git` in `filepath.Join`
DanielleMaywoodJul 22, 2025
92c857f
chore: replace fragment syntax with strings
DanielleMaywoodJul 22, 2025
225fe38
test: add another storybook to cover config folder used as name
DanielleMaywoodJul 22, 2025
a5ec3c4
feat: add agent env flag to enable/disable project discovery
DanielleMaywoodJul 22, 2025
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
2 changes: 1 addition & 1 deletionagent/agent.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1168,7 +1168,7 @@ func (a *agent) handleManifest(manifestOK *checkpoint) func(ctx context.Context,
// return existing devcontainers but actual container detection
// and creation will be deferred.
a.containerAPI.Init(
agentcontainers.WithManifestInfo(manifest.OwnerName, manifest.WorkspaceName, manifest.AgentName),
agentcontainers.WithManifestInfo(manifest.OwnerName, manifest.WorkspaceName, manifest.AgentName, manifest.Directory),
agentcontainers.WithDevcontainers(manifest.Devcontainers, manifest.Scripts),
agentcontainers.WithSubAgentClient(agentcontainers.NewSubAgentClientFromAPI(a.logger, aAPI)),
)
Expand Down
143 changes: 139 additions & 4 deletionsagent/agentcontainers/api.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/fs"
"maps"
"net/http"
"os"
Expand All@@ -21,6 +22,7 @@ import (
"github.com/fsnotify/fsnotify"
"github.com/go-chi/chi/v5"
"github.com/google/uuid"
"github.com/spf13/afero"
"golang.org/x/xerrors"

"cdr.dev/slog"
Expand DownExpand Up@@ -56,10 +58,12 @@ type API struct {
cancel context.CancelFunc
watcherDone chan struct{}
updaterDone chan struct{}
discoverDone chan struct{}
updateTrigger chan chan error // Channel to trigger manual refresh.
updateInterval time.Duration // Interval for periodic container updates.
logger slog.Logger
watcher watcher.Watcher
fs afero.Fs
execer agentexec.Execer
commandEnv CommandEnv
ccli ContainerCLI
Expand All@@ -71,9 +75,12 @@ type API struct {
subAgentURL string
subAgentEnv []string

ownerName string
workspaceName string
parentAgent string
projectDiscovery bool // If we should perform project discovery or not.

ownerName string
workspaceName string
parentAgent string
agentDirectory string

mu sync.RWMutex // Protects the following fields.
initDone chan struct{} // Closed by Init.
Expand DownExpand Up@@ -192,11 +199,12 @@ func WithSubAgentEnv(env ...string) Option {

// WithManifestInfo sets the owner name, and workspace name
// for the sub-agent.
func WithManifestInfo(owner, workspace, parentAgent string) Option {
func WithManifestInfo(owner, workspace, parentAgent, agentDirectory string) Option {
return func(api *API) {
api.ownerName = owner
api.workspaceName = workspace
api.parentAgent = parentAgent
api.agentDirectory = agentDirectory
}
}

Expand DownExpand Up@@ -261,6 +269,21 @@ func WithWatcher(w watcher.Watcher) Option {
}
}

// WithFileSystem sets the file system used for discovering projects.
func WithFileSystem(fileSystem afero.Fs) Option {
return func(api *API) {
api.fs = fileSystem
}
}

// WithProjectDiscovery sets if the API should attempt to discover
// projects on the filesystem.
func WithProjectDiscovery(projectDiscovery bool) Option {
return func(api *API) {
api.projectDiscovery = projectDiscovery
}
}

// ScriptLogger is an interface for sending devcontainer logs to the
// controlplane.
type ScriptLogger interface {
Expand DownExpand Up@@ -331,6 +354,9 @@ func NewAPI(logger slog.Logger, options ...Option) *API {
api.watcher = watcher.NewNoop()
}
}
if api.fs == nil {
api.fs = afero.NewOsFs()
}
if api.subAgentClient.Load() == nil {
var c SubAgentClient = noopSubAgentClient{}
api.subAgentClient.Store(&c)
Expand DownExpand Up@@ -372,13 +398,119 @@ func (api *API) Start() {
return
}

if api.projectDiscovery && api.agentDirectory != "" {
api.discoverDone = make(chan struct{})

go api.discover()
}

api.watcherDone = make(chan struct{})
api.updaterDone = make(chan struct{})

go api.watcherLoop()
go api.updaterLoop()
}

func (api *API) discover() {
defer close(api.discoverDone)
defer api.logger.Debug(api.ctx, "project discovery finished")
api.logger.Debug(api.ctx, "project discovery started")

if err := api.discoverDevcontainerProjects(); err != nil {
api.logger.Error(api.ctx, "discovering dev container projects", slog.Error(err))
}

if err := api.RefreshContainers(api.ctx); err != nil {
api.logger.Error(api.ctx, "refreshing containers after discovery", slog.Error(err))
}
}

func (api *API) discoverDevcontainerProjects() error {
isGitProject, err := afero.DirExists(api.fs, filepath.Join(api.agentDirectory, ".git"))
if err != nil {
return xerrors.Errorf(".git dir exists: %w", err)
}

// If the agent directory is a git project, we'll search
// the project for any `.devcontainer/devcontainer.json`
// files.
if isGitProject {
return api.discoverDevcontainersInProject(api.agentDirectory)
}

// The agent directory is _not_ a git project, so we'll
// search the top level of the agent directory for any
// git projects, and search those.
entries, err := afero.ReadDir(api.fs, api.agentDirectory)
if err != nil {
return xerrors.Errorf("read agent directory: %w", err)
}

for _, entry := range entries {
if !entry.IsDir() {
continue
}

isGitProject, err = afero.DirExists(api.fs, filepath.Join(api.agentDirectory, entry.Name(), ".git"))
if err != nil {
return xerrors.Errorf(".git dir exists: %w", err)
}

// If this directory is a git project, we'll search
// it for any `.devcontainer/devcontainer.json` files.
if isGitProject {
if err := api.discoverDevcontainersInProject(filepath.Join(api.agentDirectory, entry.Name())); err != nil {
return err
}
}
}

return nil
}

func (api *API) discoverDevcontainersInProject(projectPath string) error {
devcontainerConfigPaths := []string{
"/.devcontainer/devcontainer.json",
"/.devcontainer.json",
}

return afero.Walk(api.fs, projectPath, func(path string, info fs.FileInfo, _ error) error {
if info.IsDir() {
return nil
}

for _, relativeConfigPath := range devcontainerConfigPaths {
if !strings.HasSuffix(path, relativeConfigPath) {
continue
}

workspaceFolder := strings.TrimSuffix(path, relativeConfigPath)

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

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

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

api.knownDevcontainers[workspaceFolder] = dc
}
api.mu.Unlock()
}

return nil
})
}

func (api *API) watcherLoop() {
defer close(api.watcherDone)
defer api.logger.Debug(api.ctx, "watcher loop stopped")
Expand DownExpand Up@@ -1808,6 +1940,9 @@ func (api *API) Close() error {
if api.updaterDone != nil {
<-api.updaterDone
}
if api.discoverDone != nil {
<-api.discoverDone
}

// Wait for all async tasks to complete.
api.asyncWg.Wait()
Expand Down
Loading
Loading

[8]ページ先頭

©2009-2025 Movatter.jp