Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 77a3d19

Browse files
committed
fix(agent/agentcontainers): do not reassign proc.agent until success
1 parent 87d052e commit 77a3d19

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

agent/agentcontainers/api.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,15 +1479,16 @@ func (api *API) maybeInjectSubAgentIntoContainerLocked(ctx context.Context, dc c
14791479
originalName := subAgentConfig.Name
14801480

14811481
for attempt := 1; attempt <= maxAttemptsToNameAgent; attempt++ {
1482-
if proc.agent, err = client.Create(ctx, subAgentConfig); err == nil {
1482+
agent, err := client.Create(ctx, subAgentConfig)
1483+
if err == nil {
1484+
proc.agent = agent // Only reassign on success.
14831485
if api.usingWorkspaceFolderName[dc.WorkspaceFolder] {
14841486
api.devcontainerNames[dc.Name] = true
14851487
delete(api.usingWorkspaceFolderName, dc.WorkspaceFolder)
14861488
}
14871489

14881490
break
14891491
}
1490-
14911492
// NOTE(DanielleMaywood):
14921493
// Ordinarily we'd use `errors.As` here, but it didn't appear to work. Not
14931494
// sure if this is because of the communication protocol? Instead I've opted

agent/agentcontainers/subagent.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func (a *subAgentAPIClient) List(ctx context.Context) ([]SubAgent, error) {
188188
return agents, nil
189189
}
190190

191-
func (a *subAgentAPIClient) Create(ctx context.Context, agent SubAgent) (SubAgent, error) {
191+
func (a *subAgentAPIClient) Create(ctx context.Context, agent SubAgent) (_ SubAgent, err error) {
192192
a.logger.Debug(ctx, "creating sub agent", slog.F("name", agent.Name), slog.F("directory", agent.Directory))
193193

194194
displayApps := make([]agentproto.CreateSubAgentRequest_DisplayApp, 0, len(agent.DisplayApps))
@@ -233,19 +233,27 @@ func (a *subAgentAPIClient) Create(ctx context.Context, agent SubAgent) (SubAgen
233233
if err != nil {
234234
return SubAgent{}, err
235235
}
236+
defer func() {
237+
if err != nil {
238+
// Best effort.
239+
_, _ = a.api.DeleteSubAgent(ctx, &agentproto.DeleteSubAgentRequest{
240+
Id: resp.GetAgent().GetId(),
241+
})
242+
}
243+
}()
236244

237-
agent.Name = resp.Agent.Name
238-
agent.ID, err = uuid.FromBytes(resp.Agent.Id)
245+
agent.Name = resp.GetAgent().GetName()
246+
agent.ID, err = uuid.FromBytes(resp.GetAgent().GetId())
239247
if err != nil {
240-
return agent, err
248+
return SubAgent{}, err
241249
}
242-
agent.AuthToken, err = uuid.FromBytes(resp.Agent.AuthToken)
250+
agent.AuthToken, err = uuid.FromBytes(resp.GetAgent().GetAuthToken())
243251
if err != nil {
244-
return agent, err
252+
return SubAgent{}, err
245253
}
246254

247-
for _, appError := range resp.AppCreationErrors {
248-
app := apps[appError.Index]
255+
for _, appError := range resp.GetAppCreationErrors() {
256+
app := apps[appError.GetIndex()]
249257

250258
a.logger.Warn(ctx, "unable to create app",
251259
slog.F("agent_name", agent.Name),

0 commit comments

Comments
 (0)