runSubAgentInContainer runs the subagent process inside a dev container. The api.asyncWg must be incremented before calling this function, and it will be decremented when the subagent process completes or if an error occurs.
(ctx context.Context, logger slog.Logger, dc codersdk.WorkspaceAgentDevcontainer, proc subAgentProcess, agentPath string)
| 2141 | // function, and it will be decremented when the subagent process |
| 2142 | // completes or if an error occurs. |
| 2143 | func (api *API) runSubAgentInContainer(ctx context.Context, logger slog.Logger, dc codersdk.WorkspaceAgentDevcontainer, proc subAgentProcess, agentPath string) { |
| 2144 | container := dc.Container // Must not be nil. |
| 2145 | logger = logger.With( |
| 2146 | slog.F("agent_id", proc.agent.ID), |
| 2147 | ) |
| 2148 | |
| 2149 | defer func() { |
| 2150 | proc.stop() |
| 2151 | logger.Debug(ctx, "agent process cleanup complete") |
| 2152 | api.asyncWg.Done() |
| 2153 | }() |
| 2154 | |
| 2155 | logger.Info(ctx, "starting subagent in devcontainer") |
| 2156 | |
| 2157 | env := []string{ |
| 2158 | "CODER_AGENT_URL=" + api.subAgentURL, |
| 2159 | "CODER_AGENT_TOKEN=" + proc.agent.AuthToken.String(), |
| 2160 | } |
| 2161 | env = append(env, api.subAgentEnv...) |
| 2162 | err := api.dccli.Exec(proc.ctx, dc.WorkspaceFolder, dc.ConfigPath, agentPath, []string{"agent"}, |
| 2163 | WithExecContainerID(container.ID), |
| 2164 | WithRemoteEnv(env...), |
| 2165 | ) |
| 2166 | if err != nil && !errors.Is(err, context.Canceled) { |
| 2167 | logger.Error(ctx, "subagent process failed", slog.Error(err)) |
| 2168 | } else { |
| 2169 | logger.Info(ctx, "subagent process finished") |
| 2170 | } |
| 2171 | } |
| 2172 | |
| 2173 | func (api *API) Close() error { |
| 2174 | api.mu.Lock() |
no test coverage detected