Relates to CODAGT-1330 `create_workspace`, `start_workspace` and `stop_workspace` read a fair bit from the database whilst they wait on a build: the chat workspace TTL setting, the workspace's agents, the agent's lifecycle state, and the chat row once the build is done. When one of those reads failed, we'd log it (or just ignore it) and carry on as if it hadn't, e.g. falling back to the template's TTL or leaving out the agent status, and the tool still reported success. A failed write of the new build ID to the chat binding was handled the same way. To fix, we'll return a tool error for those database errors, so the model knows the call failed and can retry. `start_workspace` and `stop_workspace` now also re-bind the chat when its build is stale, so a retried call repairs a binding write that failed. An invalid TTL setting, a workspace with no build yet, and agent selection errors all behave like they do today. A tool error doesn't stop the build. The tools only watch the build and the agent, so the build, the startup scripts and the turn all carry on. If the model calls the tool again, `create_workspace` finds the workspace through the chat binding, which #30663 commits with the workspace, and waits on it again rather than creating a second one. It only creates a replacement if the agent has since disconnected or timed out, like it does today. `start_workspace` just waits on the latest build again, and the other workspace tools find and dial the agent themselves. This follows on from #30663.
754 lines
24 KiB
Go
754 lines
24 KiB
Go
package chattool
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"charm.land/fantasy"
|
|
"github.com/google/uuid"
|
|
"golang.org/x/xerrors"
|
|
|
|
"cdr.dev/slog/v3"
|
|
"github.com/coder/coder/v2/coderd/database"
|
|
"github.com/coder/coder/v2/coderd/database/dbtime"
|
|
"github.com/coder/coder/v2/coderd/httpapi/httperror"
|
|
"github.com/coder/coder/v2/coderd/util/namesgenerator"
|
|
"github.com/coder/coder/v2/coderd/x/chatd/agentselect"
|
|
"github.com/coder/coder/v2/codersdk"
|
|
"github.com/coder/coder/v2/codersdk/workspacesdk"
|
|
)
|
|
|
|
const (
|
|
// buildPollInterval is how often we check if the workspace
|
|
// build has completed.
|
|
buildPollInterval = 2 * time.Second
|
|
// buildTimeout is the maximum time to wait for a workspace
|
|
// build to complete before giving up.
|
|
buildTimeout = 10 * time.Minute
|
|
// agentConnectTimeout is the maximum time to wait for the
|
|
// workspace agent to become reachable after a successful build.
|
|
agentConnectTimeout = 2 * time.Minute
|
|
// agentRetryInterval is how often we retry connecting to the
|
|
// workspace agent.
|
|
agentRetryInterval = 2 * time.Second
|
|
// agentAttemptTimeout is the timeout for a single connection
|
|
// attempt to the workspace agent during the retry loop.
|
|
agentAttemptTimeout = 5 * time.Second
|
|
// startupScriptTimeout is the maximum time to wait for the
|
|
// workspace agent's startup scripts to finish after the agent
|
|
// is reachable.
|
|
startupScriptTimeout = 10 * time.Minute
|
|
// startupScriptPollInterval is how often we check the agent's
|
|
// lifecycle state while waiting for startup scripts.
|
|
startupScriptPollInterval = 2 * time.Second
|
|
)
|
|
|
|
// CreateWorkspaceFn creates a workspace for the given owner and binds the
|
|
// chat to it in one transaction.
|
|
type CreateWorkspaceFn func(
|
|
ctx context.Context,
|
|
ownerID, chatID uuid.UUID,
|
|
req codersdk.CreateWorkspaceRequest,
|
|
) (codersdk.Workspace, database.Chat, error)
|
|
|
|
// AgentConnFunc provides access to workspace agent connections.
|
|
type AgentConnFunc func(
|
|
ctx context.Context,
|
|
agentID uuid.UUID,
|
|
) (workspacesdk.AgentConn, func(), error)
|
|
|
|
// CreateWorkspaceOptions configures the create_workspace tool.
|
|
type CreateWorkspaceOptions struct {
|
|
OwnerID uuid.UUID
|
|
CreateFn CreateWorkspaceFn
|
|
AgentConnFn AgentConnFunc
|
|
AgentInactiveDisconnectTimeout time.Duration
|
|
WorkspaceMu *sync.Mutex
|
|
OnChatUpdated func(database.Chat)
|
|
Logger slog.Logger
|
|
}
|
|
|
|
type createWorkspaceArgs struct {
|
|
TemplateID string `json:"template_id" description:"The UUIDv4 of the template to create the workspace from. Obtain this from list_templates."`
|
|
Name string `json:"name,omitempty" description:"The name of the workspace to create. If not provided, a random name will be generated."`
|
|
Parameters map[string]string `json:"parameters,omitempty" description:"Key-value pairs of template parameters to use when creating the workspace. Obtain available parameters from read_template when needed."`
|
|
PresetID string `json:"preset_id,omitempty" description:"The UUIDv4 of a template version preset to use. Obtain available presets from read_template when needed. When provided, the preset's parameters are applied automatically and the workspace may claim a prebuilt instance for faster startup."`
|
|
}
|
|
|
|
// CreateWorkspace returns a tool that creates a new workspace from a
|
|
// template. The tool is idempotent: if the chat already has a
|
|
// workspace that is building or running, it returns the existing
|
|
// workspace instead of creating a new one. A mutex prevents parallel
|
|
// calls from creating duplicate workspaces.
|
|
// db must not be nil and chatID must not be uuid.Nil.
|
|
func CreateWorkspace(db database.Store, organizationID, chatID uuid.UUID, options CreateWorkspaceOptions) fantasy.AgentTool {
|
|
return fantasy.NewAgentTool(
|
|
"create_workspace",
|
|
"Create a new workspace from a template when missing tools, skills, "+
|
|
"MCPs, or context prevent progress, when workspace-backed work is "+
|
|
"needed, or when the user explicitly asks for one. Use the "+
|
|
"workspace's available context and capabilities to continue the "+
|
|
"request. Readiness does not guarantee skills, MCP tools, or "+
|
|
"context have finished loading; use capabilities actually exposed "+
|
|
"and continue with workspace file and shell tools where possible. "+
|
|
"Do not recreate the workspace to retry discovery. In Plan Mode, "+
|
|
"workspace MCP tools remain unavailable; "+
|
|
"do not create a workspace solely to access them. Prefer existing "+
|
|
"tools and context when they are sufficient. Requires a "+
|
|
"template_id from list_templates; follow its "+NextStepField+" "+
|
|
"before calling. Optionally provide a name (one is generated if "+
|
|
"omitted), parameter values, and a preset_id from read_template "+
|
|
"to apply preset parameters and potentially claim a prebuilt "+
|
|
"workspace for faster startup. Idempotent: if the chat already "+
|
|
"has a workspace building or running, it is returned.",
|
|
func(ctx context.Context, args createWorkspaceArgs, _ fantasy.ToolCall) (fantasy.ToolResponse, error) {
|
|
if options.CreateFn == nil {
|
|
return fantasy.NewTextErrorResponse("workspace creator is not configured"), nil
|
|
}
|
|
|
|
templateIDStr := strings.TrimSpace(args.TemplateID)
|
|
if templateIDStr == "" {
|
|
return fantasy.NewTextErrorResponse("template_id is required; use list_templates to find one"), nil
|
|
}
|
|
templateID, err := uuid.Parse(templateIDStr)
|
|
if err != nil {
|
|
return fantasy.NewTextErrorResponse(
|
|
xerrors.Errorf("invalid template_id: %w", err).Error(),
|
|
), nil
|
|
}
|
|
|
|
// Serialize workspace creation to prevent parallel
|
|
// tool calls from creating duplicate workspaces.
|
|
if options.WorkspaceMu != nil {
|
|
options.WorkspaceMu.Lock()
|
|
defer options.WorkspaceMu.Unlock()
|
|
}
|
|
|
|
ownerID := options.OwnerID
|
|
|
|
// Check for an existing workspace on the chat.
|
|
check := options.checkExistingWorkspace(ctx, db, chatID)
|
|
if check.BuildErr != nil {
|
|
return buildFailureToolResponse(
|
|
ctx,
|
|
options.Logger,
|
|
db,
|
|
ownerID,
|
|
organizationID,
|
|
check.BuildAction,
|
|
check.BuildID,
|
|
check.BuildErr,
|
|
), nil
|
|
}
|
|
if check.Err != nil {
|
|
return fantasy.NewTextErrorResponse(check.Err.Error()), nil
|
|
}
|
|
if check.Done {
|
|
return toolResponse(check.Result), nil
|
|
}
|
|
|
|
// Set up dbauthz context for DB lookups.
|
|
ownerCtx, ownerErr := asOwner(ctx, db, ownerID)
|
|
if ownerErr != nil {
|
|
return fantasy.NewTextErrorResponse(ownerErr.Error()), nil
|
|
}
|
|
ctx = ownerCtx
|
|
|
|
// Verify the template belongs to the same org as the
|
|
// chat. Without this check the tool could silently
|
|
// bind a cross-org workspace to the chat.
|
|
tmpl, tmplErr := db.GetTemplateByID(ctx, templateID)
|
|
if tmplErr != nil {
|
|
return fantasy.NewTextErrorResponse(
|
|
xerrors.Errorf("look up template: %w", tmplErr).Error(),
|
|
), nil
|
|
}
|
|
if tmpl.OrganizationID != organizationID {
|
|
return fantasy.NewTextErrorResponse(
|
|
"template belongs to a different organization than this chat; " +
|
|
"use list_templates to find templates in the correct organization",
|
|
), nil
|
|
}
|
|
if !tmpl.AgentsAllowed {
|
|
return fantasy.NewTextErrorResponse(templateNotAvailableMessage), nil
|
|
}
|
|
|
|
hasExternalAgent, externalAgentErr := templateHasExternalAgent(ctx, db, tmpl)
|
|
if externalAgentErr != nil {
|
|
return fantasy.NewTextErrorResponse(
|
|
xerrors.Errorf("look up template version: %w", externalAgentErr).Error(),
|
|
), nil
|
|
}
|
|
if hasExternalAgent {
|
|
return fantasy.NewTextErrorResponse(createWorkspaceExternalAgentMessage), nil
|
|
}
|
|
|
|
var ttlMs *int64
|
|
raw, err := db.GetChatWorkspaceTTL(ctx)
|
|
if err != nil {
|
|
return fantasy.NewTextErrorResponse(
|
|
xerrors.Errorf("read chat workspace TTL: %w", err).Error(),
|
|
), nil
|
|
}
|
|
d, parseErr := codersdk.ParseChatWorkspaceTTL(raw)
|
|
if parseErr != nil {
|
|
options.Logger.Warn(ctx, "invalid chat workspace TTL setting, using template default",
|
|
slog.F("raw", raw),
|
|
slog.Error(parseErr),
|
|
)
|
|
} else if d > 0 {
|
|
ms := d.Milliseconds()
|
|
ttlMs = &ms
|
|
}
|
|
|
|
createReq := codersdk.CreateWorkspaceRequest{
|
|
TemplateID: templateID,
|
|
TTLMillis: ttlMs,
|
|
}
|
|
|
|
// Apply preset if provided.
|
|
presetIDStr := strings.TrimSpace(args.PresetID)
|
|
if presetIDStr != "" {
|
|
presetID, err := uuid.Parse(presetIDStr)
|
|
if err != nil {
|
|
return fantasy.NewTextErrorResponse(
|
|
xerrors.Errorf("invalid preset_id: %w", err).Error(),
|
|
), nil
|
|
}
|
|
createReq.TemplateVersionPresetID = presetID
|
|
}
|
|
|
|
name := strings.TrimSpace(args.Name)
|
|
if name == "" {
|
|
name = generatedWorkspaceName(tmpl.Name)
|
|
} else if err := codersdk.NameValid(name); err != nil {
|
|
name = generatedWorkspaceName(name)
|
|
}
|
|
createReq.Name = name
|
|
|
|
// Map parameters.
|
|
for k, v := range args.Parameters {
|
|
createReq.RichParameterValues = append(
|
|
createReq.RichParameterValues,
|
|
codersdk.WorkspaceBuildParameter{Name: k, Value: v},
|
|
)
|
|
}
|
|
|
|
workspace, updatedChat, err := createWorkspaceWithNameRetry(ctx, ownerID, chatID, createReq, options.CreateFn)
|
|
if err != nil {
|
|
if responseErr, ok := httperror.IsResponder(err); ok {
|
|
_, resp := responseErr.Response()
|
|
return toolResponse(responseErrorResult(resp)), nil
|
|
}
|
|
return fantasy.NewTextErrorResponse(err.Error()), nil
|
|
}
|
|
if options.OnChatUpdated != nil {
|
|
options.OnChatUpdated(updatedChat)
|
|
}
|
|
|
|
// Wait for the build to complete and the agent to
|
|
// come online so subsequent tools can use the
|
|
// workspace immediately.
|
|
buildID := workspace.LatestBuild.ID
|
|
if buildID != uuid.Nil {
|
|
if err := waitForBuild(ctx, db, buildID); err != nil {
|
|
return buildFailureToolResponse(
|
|
ctx,
|
|
options.Logger,
|
|
db,
|
|
ownerID,
|
|
organizationID,
|
|
buildFailureActionCreate,
|
|
buildID,
|
|
xerrors.Errorf("workspace build failed: %w", err),
|
|
), nil
|
|
}
|
|
}
|
|
|
|
result := map[string]any{
|
|
"created": true,
|
|
"workspace_name": workspace.FullName(),
|
|
}
|
|
setBuildID(result, buildID)
|
|
|
|
// Select the chat agent so follow-up tools wait on the
|
|
// intended workspace agent.
|
|
selectedAgent := database.WorkspaceAgent{}
|
|
agents, err := db.GetWorkspaceAgentsInLatestBuildByWorkspaceID(ctx, workspace.ID)
|
|
if err != nil {
|
|
return fantasy.NewTextErrorResponse(
|
|
xerrors.Errorf("get workspace agents: %w", err).Error(),
|
|
), nil
|
|
}
|
|
if len(agents) == 0 {
|
|
result["agent_status"] = "no_agent"
|
|
} else {
|
|
selected, selectErr := agentselect.FindChatAgent(agents)
|
|
if selectErr != nil {
|
|
result["agent_status"] = "selection_error"
|
|
result["agent_error"] = selectErr.Error()
|
|
} else {
|
|
selectedAgent = selected
|
|
}
|
|
}
|
|
|
|
// Wait for the agent to come online and startup scripts to finish.
|
|
if selectedAgent.ID != uuid.Nil {
|
|
agentStatus, err := waitForAgentReady(ctx, db, selectedAgent, options.AgentConnFn)
|
|
if err != nil {
|
|
return fantasy.NewTextErrorResponse(err.Error()), nil
|
|
}
|
|
for k, v := range agentStatus {
|
|
result[k] = v
|
|
}
|
|
}
|
|
|
|
// Re-fire after the agent is fully ready so callers
|
|
// can load instruction files (AGENTS.md) from the
|
|
// running agent. This must happen after
|
|
// waitForAgentReady — firing earlier (e.g. right
|
|
// after waitForBuild) races with the agent startup
|
|
// and the connection usually times out before the
|
|
// agent is reachable.
|
|
if options.OnChatUpdated != nil {
|
|
latest, err := db.GetChatByID(ctx, chatID)
|
|
if err != nil {
|
|
return fantasy.NewTextErrorResponse(
|
|
xerrors.Errorf("load chat: %w", err).Error(),
|
|
), nil
|
|
}
|
|
options.OnChatUpdated(latest)
|
|
}
|
|
|
|
return toolResponse(result), nil
|
|
})
|
|
}
|
|
|
|
// existingWorkspaceResult holds the outcome of checking for an
|
|
// existing workspace on the chat.
|
|
type existingWorkspaceResult struct {
|
|
// Result is the tool response map when Done is true.
|
|
Result map[string]any
|
|
// Done indicates the caller should return early.
|
|
Done bool
|
|
// BuildAction, BuildID, and BuildErr are set together when
|
|
// waitForBuild failed, so the caller can render the build
|
|
// failure through the shared response path.
|
|
BuildAction buildFailureAction
|
|
BuildID uuid.UUID
|
|
BuildErr error
|
|
// Err is non-nil when the check itself failed.
|
|
Err error
|
|
}
|
|
|
|
// checkExistingWorkspace checks whether the given chat
|
|
// already has a usable workspace. Returns an
|
|
// existingWorkspaceResult with Done set when the caller should
|
|
// return early (workspace exists and is alive or building).
|
|
// Returns Done unset if the caller should proceed with creation
|
|
// (workspace is dead or missing).
|
|
func (o CreateWorkspaceOptions) checkExistingWorkspace(
|
|
ctx context.Context,
|
|
db database.Store,
|
|
chatID uuid.UUID,
|
|
) existingWorkspaceResult {
|
|
agentConnFn := o.AgentConnFn
|
|
agentInactiveDisconnectTimeout := o.AgentInactiveDisconnectTimeout
|
|
|
|
chat, err := db.GetChatByID(ctx, chatID)
|
|
if err != nil {
|
|
return existingWorkspaceResult{Err: xerrors.Errorf("load chat: %w", err)}
|
|
}
|
|
if !chat.WorkspaceID.Valid {
|
|
return existingWorkspaceResult{}
|
|
}
|
|
|
|
ws, err := db.GetWorkspaceByID(ctx, chat.WorkspaceID.UUID)
|
|
if err != nil {
|
|
return existingWorkspaceResult{Err: xerrors.Errorf("load workspace: %w", err)}
|
|
}
|
|
// Workspace was soft-deleted — allow creation.
|
|
if ws.Deleted {
|
|
return existingWorkspaceResult{}
|
|
}
|
|
|
|
build, job, err := latestWorkspaceBuildAndJob(ctx, db, ws.ID)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return existingWorkspaceResult{}
|
|
}
|
|
if err != nil {
|
|
return existingWorkspaceResult{Err: err}
|
|
}
|
|
|
|
switch job.JobStatus {
|
|
case database.ProvisionerJobStatusPending,
|
|
database.ProvisionerJobStatusRunning:
|
|
// Build is in progress. Publish the build ID so the
|
|
// frontend can start streaming logs, then wait.
|
|
if err := publishBuildBinding(ctx, db, chatID, ws.ID, build.ID, o.OnChatUpdated); err != nil {
|
|
return existingWorkspaceResult{Err: err}
|
|
}
|
|
if err := waitForBuild(ctx, db, build.ID); err != nil {
|
|
action := buildFailureActionCreate
|
|
if build.Transition == database.WorkspaceTransitionStart {
|
|
action = buildFailureActionStart
|
|
}
|
|
return existingWorkspaceResult{
|
|
BuildAction: action,
|
|
BuildID: build.ID,
|
|
BuildErr: xerrors.Errorf("existing workspace build failed: %w", err),
|
|
}
|
|
}
|
|
result := map[string]any{
|
|
"created": false,
|
|
"workspace_name": ws.Name,
|
|
"status": "already_exists",
|
|
"message": "workspace build completed",
|
|
}
|
|
setBuildID(result, build.ID)
|
|
agents, err := db.GetWorkspaceAgentsInLatestBuildByWorkspaceID(ctx, ws.ID)
|
|
if err != nil {
|
|
return existingWorkspaceResult{Err: xerrors.Errorf("get workspace agents: %w", err)}
|
|
}
|
|
if len(agents) > 0 {
|
|
selected, selectErr := agentselect.FindChatAgent(agents)
|
|
if selectErr != nil {
|
|
o.Logger.Debug(ctx, "agent selection failed, falling back to first agent for readiness check",
|
|
slog.F("workspace_id", ws.ID),
|
|
slog.Error(selectErr),
|
|
)
|
|
selected = agents[0]
|
|
}
|
|
agentStatus, err := waitForAgentReady(ctx, db, selected, agentConnFn)
|
|
if err != nil {
|
|
return existingWorkspaceResult{Err: err}
|
|
}
|
|
for k, v := range agentStatus {
|
|
result[k] = v
|
|
}
|
|
}
|
|
return existingWorkspaceResult{Result: result, Done: true}
|
|
|
|
case database.ProvisionerJobStatusSucceeded:
|
|
// If the workspace was stopped, tell the model to use
|
|
// start_workspace instead of creating a new one.
|
|
if build.Transition == database.WorkspaceTransitionStop {
|
|
return existingWorkspaceResult{Result: map[string]any{
|
|
"created": false,
|
|
"workspace_name": ws.Name,
|
|
"status": "stopped",
|
|
"message": "workspace is stopped; use start_workspace to start it",
|
|
}, Done: true}
|
|
}
|
|
|
|
// Build succeeded — use the agent's recent DB-backed
|
|
// connection status to decide whether the workspace is
|
|
// still usable.
|
|
agents, err := db.GetWorkspaceAgentsInLatestBuildByWorkspaceID(ctx, ws.ID)
|
|
if err != nil {
|
|
return existingWorkspaceResult{Err: xerrors.Errorf("get workspace agents: %w", err)}
|
|
}
|
|
if len(agents) > 0 {
|
|
selected, selectErr := agentselect.FindChatAgent(agents)
|
|
if selectErr != nil {
|
|
o.Logger.Debug(ctx, "agent selection failed, falling back to first agent for status check",
|
|
slog.F("workspace_id", ws.ID),
|
|
slog.Error(selectErr),
|
|
)
|
|
selected = agents[0]
|
|
}
|
|
status := selected.Status(dbtime.Now(), agentInactiveDisconnectTimeout)
|
|
result := map[string]any{
|
|
"created": false,
|
|
"workspace_name": ws.Name,
|
|
"status": "already_exists",
|
|
}
|
|
|
|
switch status.Status {
|
|
case database.WorkspaceAgentStatusConnected:
|
|
result["message"] = "workspace is already running and recently connected"
|
|
agentStatus, err := waitForAgentReady(ctx, db, selected, nil)
|
|
if err != nil {
|
|
return existingWorkspaceResult{Err: err}
|
|
}
|
|
for k, v := range agentStatus {
|
|
result[k] = v
|
|
}
|
|
return existingWorkspaceResult{Result: result, Done: true}
|
|
case database.WorkspaceAgentStatusConnecting:
|
|
result["message"] = "workspace exists and the agent is still connecting"
|
|
agentStatus, err := waitForAgentReady(ctx, db, selected, agentConnFn)
|
|
if err != nil {
|
|
return existingWorkspaceResult{Err: err}
|
|
}
|
|
for k, v := range agentStatus {
|
|
result[k] = v
|
|
}
|
|
return existingWorkspaceResult{Result: result, Done: true}
|
|
case database.WorkspaceAgentStatusDisconnected,
|
|
database.WorkspaceAgentStatusTimeout:
|
|
// Agent is offline or never became ready - allow
|
|
// creation.
|
|
}
|
|
}
|
|
// No agent ID or no agent status — allow creation.
|
|
return existingWorkspaceResult{}
|
|
|
|
default:
|
|
// Failed, canceled, etc — allow creation.
|
|
return existingWorkspaceResult{}
|
|
}
|
|
}
|
|
|
|
// waitForBuild polls the specified build until its provisioner job
|
|
// completes or the context expires.
|
|
func waitForBuild(
|
|
ctx context.Context,
|
|
db database.Store,
|
|
buildID uuid.UUID,
|
|
) error {
|
|
buildCtx, cancel := context.WithTimeout(ctx, buildTimeout)
|
|
defer cancel()
|
|
|
|
ticker := time.NewTicker(buildPollInterval)
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
build, err := db.GetWorkspaceBuildByID(buildCtx, buildID)
|
|
if err != nil {
|
|
return xerrors.Errorf("get build: %w", err)
|
|
}
|
|
|
|
job, err := db.GetProvisionerJobByID(buildCtx, build.JobID)
|
|
if err != nil {
|
|
return xerrors.Errorf("get provisioner job: %w", err)
|
|
}
|
|
|
|
switch job.JobStatus {
|
|
case database.ProvisionerJobStatusSucceeded:
|
|
return nil
|
|
case database.ProvisionerJobStatusFailed:
|
|
errMsg := "build failed"
|
|
if job.Error.Valid {
|
|
errMsg = job.Error.String
|
|
}
|
|
var code codersdk.JobErrorCode
|
|
if job.ErrorCode.Valid {
|
|
code = codersdk.JobErrorCode(job.ErrorCode.String)
|
|
}
|
|
return &workspaceBuildError{message: errMsg, code: code}
|
|
case database.ProvisionerJobStatusCanceled:
|
|
return xerrors.New("build was canceled")
|
|
case database.ProvisionerJobStatusPending,
|
|
database.ProvisionerJobStatusRunning,
|
|
database.ProvisionerJobStatusCanceling:
|
|
// Still in progress — keep waiting.
|
|
default:
|
|
return xerrors.Errorf("unexpected job status: %s", job.JobStatus)
|
|
}
|
|
|
|
select {
|
|
case <-buildCtx.Done():
|
|
return xerrors.Errorf(
|
|
"timed out waiting for workspace build: %w",
|
|
buildCtx.Err(),
|
|
)
|
|
case <-ticker.C:
|
|
}
|
|
}
|
|
}
|
|
|
|
func templateHasExternalAgent(
|
|
ctx context.Context,
|
|
db database.Store,
|
|
tmpl database.Template,
|
|
) (bool, error) {
|
|
version, err := db.GetTemplateVersionByID(ctx, tmpl.ActiveVersionID)
|
|
if err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return false, nil
|
|
}
|
|
return false, err
|
|
}
|
|
return version.HasExternalAgent.Valid && version.HasExternalAgent.Bool, nil
|
|
}
|
|
|
|
// externalAgentReadyError returns the external-agent-specific error
|
|
// message when agent belongs to an external resource, or the empty
|
|
// string otherwise. Errors looking up the resource are treated as
|
|
// non-external so the caller falls back to the dial error.
|
|
func externalAgentReadyError(
|
|
ctx context.Context,
|
|
db database.Store,
|
|
agent database.WorkspaceAgent,
|
|
) string {
|
|
isExternal, err := IsExternalWorkspaceAgent(ctx, db, agent)
|
|
if err != nil || !isExternal {
|
|
return ""
|
|
}
|
|
return ExternalAgentUnavailableMessage(agent)
|
|
}
|
|
|
|
// waitForAgentReady waits for the workspace agent to become
|
|
// reachable and for its startup scripts to finish. It returns
|
|
// status fields suitable for merging into a tool response.
|
|
func waitForAgentReady(
|
|
ctx context.Context,
|
|
db database.Store,
|
|
agent database.WorkspaceAgent,
|
|
agentConnFn AgentConnFunc,
|
|
) (map[string]any, error) {
|
|
result := map[string]any{}
|
|
agentID := agent.ID
|
|
|
|
// Phase 1: retry connecting to the agent.
|
|
if agentConnFn != nil {
|
|
agentCtx, agentCancel := context.WithTimeout(ctx, agentConnectTimeout)
|
|
defer agentCancel()
|
|
|
|
ticker := time.NewTicker(agentRetryInterval)
|
|
defer ticker.Stop()
|
|
|
|
// Dials time out while a new agent boots, so they are not counted as
|
|
// unreachable. An agent that connected before should answer, so its
|
|
// failures are counted.
|
|
var attemptCause error
|
|
if !agent.FirstConnectedAt.Valid {
|
|
attemptCause = workspacesdk.ErrReadinessProbeTimeout
|
|
}
|
|
|
|
var lastErr error
|
|
for {
|
|
attemptCtx, attemptCancel := context.WithTimeoutCause(agentCtx, agentAttemptTimeout, attemptCause)
|
|
_, release, err := agentConnFn(attemptCtx, agentID)
|
|
attemptCancel()
|
|
if err == nil {
|
|
release()
|
|
break
|
|
}
|
|
lastErr = err
|
|
|
|
select {
|
|
case <-agentCtx.Done():
|
|
result["agent_status"] = "not_ready"
|
|
// External agents may need user action on a different
|
|
// host. Surface that guidance instead of the raw dial
|
|
// error after the retry window has elapsed. The retry
|
|
// loop itself is unchanged, so a Connecting external
|
|
// agent still gets the full window to come online.
|
|
if msg := externalAgentReadyError(ctx, db, agent); msg != "" {
|
|
result["agent_error"] = msg
|
|
} else {
|
|
result["agent_error"] = lastErr.Error()
|
|
}
|
|
return result, nil
|
|
case <-ticker.C:
|
|
}
|
|
}
|
|
}
|
|
|
|
// Phase 2: poll lifecycle until startup scripts finish.
|
|
scriptCtx, scriptCancel := context.WithTimeout(ctx, startupScriptTimeout)
|
|
defer scriptCancel()
|
|
|
|
ticker := time.NewTicker(startupScriptPollInterval)
|
|
defer ticker.Stop()
|
|
|
|
var lastState database.WorkspaceAgentLifecycleState
|
|
for {
|
|
row, err := db.GetWorkspaceAgentLifecycleStateByID(scriptCtx, agentID)
|
|
// A read cut short by scriptCtx is reported as the timeout below.
|
|
if err != nil && scriptCtx.Err() == nil {
|
|
return nil, xerrors.Errorf("get agent lifecycle state: %w", err)
|
|
}
|
|
if err == nil {
|
|
lastState = row.LifecycleState
|
|
switch lastState {
|
|
case database.WorkspaceAgentLifecycleStateCreated,
|
|
database.WorkspaceAgentLifecycleStateStarting:
|
|
// Still in progress, keep polling.
|
|
case database.WorkspaceAgentLifecycleStateReady:
|
|
return result, nil
|
|
default:
|
|
// Terminal non-ready state.
|
|
result["startup_scripts"] = "startup_scripts_failed"
|
|
result["lifecycle_state"] = string(lastState)
|
|
return result, nil
|
|
}
|
|
}
|
|
|
|
select {
|
|
case <-scriptCtx.Done():
|
|
if errors.Is(scriptCtx.Err(), context.DeadlineExceeded) {
|
|
result["startup_scripts"] = "startup_scripts_timeout"
|
|
} else {
|
|
result["startup_scripts"] = "startup_scripts_unknown"
|
|
}
|
|
return result, nil
|
|
case <-ticker.C:
|
|
}
|
|
}
|
|
}
|
|
|
|
func createWorkspaceWithNameRetry(
|
|
ctx context.Context,
|
|
ownerID, chatID uuid.UUID,
|
|
req codersdk.CreateWorkspaceRequest,
|
|
createFn CreateWorkspaceFn,
|
|
) (codersdk.Workspace, database.Chat, error) {
|
|
workspace, chat, err := createFn(ctx, ownerID, chatID, req)
|
|
if err == nil {
|
|
return workspace, chat, nil
|
|
}
|
|
if !isWorkspaceNameConflict(err) {
|
|
return codersdk.Workspace{}, database.Chat{}, err
|
|
}
|
|
|
|
req.Name = generatedWorkspaceName(req.Name)
|
|
return createFn(ctx, ownerID, chatID, req)
|
|
}
|
|
|
|
func isWorkspaceNameConflict(err error) bool {
|
|
responseErr, ok := httperror.IsResponder(err)
|
|
if !ok {
|
|
return false
|
|
}
|
|
status, resp := responseErr.Response()
|
|
if status != http.StatusConflict {
|
|
return false
|
|
}
|
|
for _, validation := range resp.Validations {
|
|
if validation.Field == "name" {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func generatedWorkspaceName(seed string) string {
|
|
base := codersdk.UsernameFrom(strings.TrimSpace(strings.ToLower(seed)))
|
|
if strings.TrimSpace(base) == "" {
|
|
base = "workspace"
|
|
}
|
|
|
|
suffix := strings.ReplaceAll(uuid.NewString(), "-", "")[:4]
|
|
if len(base) > 27 {
|
|
base = strings.Trim(base[:27], "-")
|
|
}
|
|
if base == "" {
|
|
base = "workspace"
|
|
}
|
|
|
|
name := fmt.Sprintf("%s-%s", base, suffix)
|
|
if err := codersdk.NameValid(name); err == nil {
|
|
return name
|
|
}
|
|
return namesgenerator.NameDigitWith("-")
|
|
}
|