Skip to content

Commit 050177b

Browse files
committed
add sub agent env and revert container id change
1 parent 9afa5ea commit 050177b

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

agent/agentcontainers/api.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ type API struct {
5959
scriptLogger func(logSourceID uuid.UUID) ScriptLogger
6060
subAgentClient SubAgentClient
6161
subAgentURL string
62+
subAgentEnv []string
6263

6364
mu sync.RWMutex
6465
closed bool
@@ -141,6 +142,13 @@ func WithSubAgentURL(url string) Option {
141142
}
142143
}
143144

145+
// WithSubAgent sets the environment variables for the sub-agent.
146+
func WithSubAgentEnv(env ...string) Option {
147+
return func(api *API) {
148+
api.subAgentEnv = env
149+
}
150+
}
151+
144152
// WithDevcontainers sets the known devcontainers for the API. This
145153
// allows the API to be aware of devcontainers defined in the workspace
146154
// agent manifest.
@@ -1147,12 +1155,14 @@ func (api *API) runSubAgentInContainer(ctx context.Context, dc codersdk.Workspac
11471155

11481156
logger.Info(ctx, "starting subagent in dev container")
11491157

1158+
env := []string{
1159+
"CODER_AGENT_URL=" + api.subAgentURL,
1160+
"CODER_AGENT_TOKEN=" + agent.AuthToken.String(),
1161+
}
1162+
env = append(env, api.subAgentEnv...)
11501163
err := api.dccli.Exec(agentCtx, dc.WorkspaceFolder, dc.ConfigPath, agentPath, []string{"agent"},
11511164
WithExecContainerID(container.ID),
1152-
WithRemoteEnv(
1153-
"CODER_AGENT_URL="+api.subAgentURL,
1154-
"CODER_AGENT_TOKEN="+agent.AuthToken.String(),
1155-
),
1165+
WithRemoteEnv(env...),
11561166
)
11571167
if err != nil && !errors.Is(err, context.Canceled) {
11581168
logger.Error(ctx, "subagent process failed", slog.Error(err))

agent/agentcontainers/devcontainercli.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,10 @@ func WithExecOutput(stdout, stderr io.Writer) DevcontainerCLIExecOptions {
6868
}
6969

7070
// WithExecContainerID sets the container ID to target a specific
71-
// container. Note that this option will unset the workspace folder to
72-
// ensure properties from the container are inherited correctly.
71+
// container.
7372
func WithExecContainerID(id string) DevcontainerCLIExecOptions {
7473
return func(o *devcontainerCLIExecConfig) {
75-
o.containerID = id
74+
o.args = append(o.args, "--container-id", id)
7675
}
7776
}
7877

@@ -168,10 +167,12 @@ func (d *devcontainerCLI) Exec(ctx context.Context, workspaceFolder, configPath
168167
logger := d.logger.With(slog.F("workspace_folder", workspaceFolder), slog.F("config_path", configPath))
169168

170169
args := []string{"exec"}
171-
switch {
172-
case conf.containerID != "":
173-
args = append(args, "--container-id", conf.containerID)
174-
case workspaceFolder != "":
170+
// For now, always set workspace folder even if --container-id is provided.
171+
// Otherwise the environment of exec will be incomplete, like `pwd` will be
172+
// /home/coder instead of /workspaces/coder. The downside is that the local
173+
// `devcontainer.json` config will overwrite settings serialized in the
174+
// container label.
175+
if workspaceFolder != "" {
175176
args = append(args, "--workspace-folder", workspaceFolder)
176177
}
177178
if configPath != "" {

0 commit comments

Comments
 (0)