Files
coder/coderd/x/chatd/chattool/stopworkspace.go
T
Ethan e254086494 fix(coderd/x/chatd/chattool): return database errors from workspace tools (#30822)
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.
2026-10-19 14:02:47 +11:00

191 lines
6.2 KiB
Go

package chattool
import (
"context"
"sync"
"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/httpapi/httperror"
"github.com/coder/coder/v2/codersdk"
)
// StopWorkspaceFn stops a workspace by creating a new build with
// the "stop" transition.
type StopWorkspaceFn func(
ctx context.Context,
ownerID uuid.UUID,
workspaceID uuid.UUID,
req codersdk.CreateWorkspaceBuildRequest,
) (codersdk.WorkspaceBuild, error)
// StopWorkspaceOptions configures the stop_workspace tool.
type StopWorkspaceOptions struct {
OwnerID uuid.UUID
StopFn StopWorkspaceFn
WorkspaceMu *sync.Mutex
OnChatUpdated func(database.Chat)
Logger slog.Logger
}
type stopWorkspaceArgs struct{}
// StopWorkspace returns a tool that stops the workspace associated
// with the current chat. The tool is idempotent when the workspace is
// already stopped. db must not be nil and chatID must not be uuid.Nil.
func StopWorkspace(db database.Store, chatID uuid.UUID, options StopWorkspaceOptions) fantasy.AgentTool {
return fantasy.NewAgentTool(
"stop_workspace",
"Stop the chat's workspace and wait for the stop build to complete. "+
"If another workspace build is already in progress, this waits "+
"for that build first, then stops the workspace if needed. "+
"After waiting, this tool is idempotent if the workspace is "+
"already stopped or the in-progress build stopped it. Use "+
"this when the "+
"user explicitly asks to stop the workspace, or when a "+
"workspace-agent error tells you to stop and then start the "+
"workspace. Stopping a workspace terminates running processes "+
"and may discard unsaved in-memory state. This tool does not "+
"delete the workspace.",
func(ctx context.Context, _ stopWorkspaceArgs, _ fantasy.ToolCall) (fantasy.ToolResponse, error) {
if options.StopFn == nil {
return fantasy.NewTextErrorResponse("workspace stopper is not configured"), nil
}
// Serialize with create_workspace and start_workspace to
// prevent lifecycle races.
if options.WorkspaceMu != nil {
options.WorkspaceMu.Lock()
defer options.WorkspaceMu.Unlock()
}
chat, err := db.GetChatByID(ctx, chatID)
if err != nil {
return fantasy.NewTextErrorResponse(
xerrors.Errorf("load chat: %w", err).Error(),
), nil
}
if !chat.WorkspaceID.Valid {
return fantasy.NewTextErrorResponse(
"chat has no workspace; use create_workspace first",
), nil
}
ws, err := db.GetWorkspaceByID(ctx, chat.WorkspaceID.UUID)
if err != nil {
return workspaceLoadErrorResponse(err), nil
}
if ws.Deleted {
return fantasy.NewTextErrorResponse(
"workspace was deleted; use create_workspace to make a new one",
), nil
}
build, job, err := latestWorkspaceBuildAndJob(ctx, db, ws.ID)
if err != nil {
return fantasy.NewTextErrorResponse(err.Error()), nil
}
// If a build is already in progress, wait for it before
// deciding whether a stop build is still needed.
switch job.JobStatus {
case database.ProvisionerJobStatusPending,
database.ProvisionerJobStatusRunning,
database.ProvisionerJobStatusCanceling:
if err := publishBuildBinding(ctx, db, chatID, ws.ID, build.ID, options.OnChatUpdated); err != nil {
return fantasy.NewTextErrorResponse(err.Error()), nil
}
waitErr := waitForBuild(ctx, db, build.ID)
// Re-read after waiting because another transition may
// have completed while this tool was blocked.
ws, err = db.GetWorkspaceByID(ctx, ws.ID)
if err != nil {
return workspaceLoadErrorResponse(err), nil
}
if ws.Deleted {
return fantasy.NewTextErrorResponse(
"workspace was deleted; use create_workspace to make a new one",
), nil
}
build, job, err = latestWorkspaceBuildAndJob(ctx, db, ws.ID)
if err != nil {
return fantasy.NewTextErrorResponse(err.Error()), nil
}
// The fresh job row is authoritative. A wait error can
// be stale if the build reached a terminal state while the
// wait context was ending.
if waitErr != nil && !provisionerJobTerminal(job.JobStatus) {
return buildToolResponse(newBuildError(
xerrors.Errorf("waiting for in-progress build: %w", waitErr).Error(),
build.ID,
)), nil
}
}
if job.JobStatus == database.ProvisionerJobStatusSucceeded &&
build.Transition == database.WorkspaceTransitionStop {
if chat.BuildID.UUID != build.ID {
if err := publishBuildBinding(ctx, db, chatID, ws.ID, build.ID, options.OnChatUpdated); err != nil {
return fantasy.NewTextErrorResponse(err.Error()), nil
}
}
result := map[string]any{
"stopped": true,
"workspace_name": ws.Name,
}
setNoBuild(result, uuid.Nil)
return toolResponse(result), nil
}
ownerCtx, ownerErr := asOwner(ctx, db, options.OwnerID)
if ownerErr != nil {
return fantasy.NewTextErrorResponse(ownerErr.Error()), nil
}
stopBuild, err := options.StopFn(ownerCtx, options.OwnerID, ws.ID, codersdk.CreateWorkspaceBuildRequest{
Transition: codersdk.WorkspaceTransitionStop,
})
if err != nil {
if responseErr, ok := httperror.IsResponder(err); ok {
_, resp := responseErr.Response()
return toolResponse(responseErrorResult(resp)), nil
}
return fantasy.NewTextErrorResponse(
xerrors.Errorf("stop workspace: %w", err).Error(),
), nil
}
if err := publishBuildBinding(ctx, db, chatID, ws.ID, stopBuild.ID, options.OnChatUpdated); err != nil {
return fantasy.NewTextErrorResponse(err.Error()), nil
}
if err := waitForBuild(ctx, db, stopBuild.ID); err != nil {
return buildToolResponse(newBuildError(
xerrors.Errorf("workspace stop build failed: %w", err).Error(),
stopBuild.ID,
)), nil
}
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)
}
result := map[string]any{
"stopped": true,
"workspace_name": ws.Name,
}
setBuildID(result, stopBuild.ID)
return toolResponse(result), nil
})
}