From 77a3d1948192305912909ca40bc3cd9288baf844 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Thu, 26 Jun 2025 15:18:52 +0000 Subject: [PATCH] fix(agent/agentcontainers): do not reassign proc.agent until success --- agent/agentcontainers/api.go | 5 +++-- agent/agentcontainers/subagent.go | 24 ++++++++++++++++-------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/agent/agentcontainers/api.go b/agent/agentcontainers/api.go index 0f8fd3e380932..7d8b54f8a1b10 100644 --- a/agent/agentcontainers/api.go +++ b/agent/agentcontainers/api.go @@ -1479,7 +1479,9 @@ func (api *API) maybeInjectSubAgentIntoContainerLocked(ctx context.Context, dc c originalName := subAgentConfig.Name for attempt := 1; attempt <= maxAttemptsToNameAgent; attempt++ { - if proc.agent, err = client.Create(ctx, subAgentConfig); err == nil { + agent, err := client.Create(ctx, subAgentConfig) + if err == nil { + proc.agent = agent // Only reassign on success. if api.usingWorkspaceFolderName[dc.WorkspaceFolder] { api.devcontainerNames[dc.Name] = true delete(api.usingWorkspaceFolderName, dc.WorkspaceFolder) @@ -1487,7 +1489,6 @@ func (api *API) maybeInjectSubAgentIntoContainerLocked(ctx context.Context, dc c break } - // NOTE(DanielleMaywood): // Ordinarily we'd use `errors.As` here, but it didn't appear to work. Not // sure if this is because of the communication protocol? Instead I've opted diff --git a/agent/agentcontainers/subagent.go b/agent/agentcontainers/subagent.go index 42df7080a890a..7d7603feef21d 100644 --- a/agent/agentcontainers/subagent.go +++ b/agent/agentcontainers/subagent.go @@ -188,7 +188,7 @@ func (a *subAgentAPIClient) List(ctx context.Context) ([]SubAgent, error) { return agents, nil } -func (a *subAgentAPIClient) Create(ctx context.Context, agent SubAgent) (SubAgent, error) { +func (a *subAgentAPIClient) Create(ctx context.Context, agent SubAgent) (_ SubAgent, err error) { a.logger.Debug(ctx, "creating sub agent", slog.F("name", agent.Name), slog.F("directory", agent.Directory)) displayApps := make([]agentproto.CreateSubAgentRequest_DisplayApp, 0, len(agent.DisplayApps)) @@ -233,19 +233,27 @@ func (a *subAgentAPIClient) Create(ctx context.Context, agent SubAgent) (SubAgen if err != nil { return SubAgent{}, err } + defer func() { + if err != nil { + // Best effort. + _, _ = a.api.DeleteSubAgent(ctx, &agentproto.DeleteSubAgentRequest{ + Id: resp.GetAgent().GetId(), + }) + } + }() - agent.Name = resp.Agent.Name - agent.ID, err = uuid.FromBytes(resp.Agent.Id) + agent.Name = resp.GetAgent().GetName() + agent.ID, err = uuid.FromBytes(resp.GetAgent().GetId()) if err != nil { - return agent, err + return SubAgent{}, err } - agent.AuthToken, err = uuid.FromBytes(resp.Agent.AuthToken) + agent.AuthToken, err = uuid.FromBytes(resp.GetAgent().GetAuthToken()) if err != nil { - return agent, err + return SubAgent{}, err } - for _, appError := range resp.AppCreationErrors { - app := apps[appError.Index] + for _, appError := range resp.GetAppCreationErrors() { + app := apps[appError.GetIndex()] a.logger.Warn(ctx, "unable to create app", slog.F("agent_name", agent.Name),