Files
coder/coderd/x/chatd/chat_mutations.go
T
Callum Styan d412c38afb perf: validate chat transitions from the locked row (#30886)
One of the main costs in terms of DB load from chats is extra round
trips during transactions. This results in locks being held for longer
periods of time -> an increase in lock waits for chats related queries.

One of the opportunities for reducing some of the round trips is to
eliminate extra chat row reads after we have just read the row in order
to lock it.

In this PR `ChatMachine.Update` now seeds the transaction struct with
the row returned by `LockChatAndBumpSnapshotVersion`, plus a queue count
taken right after it. We can then pass it into `Update` as an optional
parameter validate against it, so neither re-reads the chat row from the
database while the row lock is held.

`updateChat` (send, edit, promote, tool results, interrupt, compact,
clear, reconcile), hook failure, family archive, and the worker's
generation and task transitions use it instead of reloading the chat
under the lock.

This PR relative to main:
|measurement | main | this PR | percent change |
|-- | -- | -- | -- |
| Round trips under lock  | 180.9 | 142.0 | -39% |
| Statements under lock  | 86.1 | 66.6 | -23% |
| GetChatByID under lock | 54.1 | 35.8 | -34% |
| GetChatByID total | 75.5 | 57.1 | -25% |
| Statements  | 235.8 | 216.2 | -8% |

Generated by Coder Agents on behalf of @cstyan.
2026-11-13 09:59:39 -07:00

1017 lines
35 KiB
Go

package chatd
import (
"context"
"database/sql"
"errors"
"fmt"
"time"
"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/x/agenthooks/dispatch"
"github.com/coder/coder/v2/coderd/x/chatd/chatdebug"
"github.com/coder/coder/v2/coderd/x/chatd/chathooks"
"github.com/coder/coder/v2/coderd/x/chatd/chatprompt"
"github.com/coder/coder/v2/coderd/x/chatd/chatstate"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/codersdk/x/agenthooks"
)
// updateChat runs transition in one chatstate transaction, then publishes
// and returns the row Update reads after the transition, so the result and
// the sidebar watch event carry the transition's snapshot bump. It
// publishes only that watch event (chat:update consumers handle stream
// side effects).
func (p *Server) updateChat(
ctx context.Context,
chatID uuid.UUID,
transition func(tx *chatstate.Tx, store database.Store, snapshot *chatstate.ChatSnapshot) error,
) (database.Chat, error) {
published, err := p.newChatMachine(chatID).Update(ctx, transition)
if err != nil {
return database.Chat{}, err
}
p.publishChatPubsubEvent(published, codersdk.ChatWatchEventKindStatusChange, nil)
return published, nil
}
// SendMessage admits a user message through the chatstate.SendMessage
// transition. A requested model config is validated before the
// transaction (see validateModelConfigOverride); the other admission
// checks and writes run in the transition's transaction, so they commit
// or roll back together.
func (p *Server) SendMessage(
ctx context.Context,
opts SendMessageOptions,
) (SendMessageResult, error) {
if opts.ChatID == uuid.Nil {
return SendMessageResult{}, xerrors.New("chat_id is required")
}
if len(opts.Content) == 0 {
return SendMessageResult{}, xerrors.New("content is required")
}
busyBehavior := opts.BusyBehavior
if busyBehavior == "" {
busyBehavior = SendMessageBusyBehaviorSteer
}
switch busyBehavior {
case SendMessageBusyBehaviorQueue, SendMessageBusyBehaviorSteer:
default:
return SendMessageResult{}, xerrors.Errorf("invalid busy behavior %q", opts.BusyBehavior)
}
contentParts, err := p.resolveChatSkillInvocations(ctx, opts.ChatID, opts.Content)
if err != nil {
return SendMessageResult{}, err
}
var (
chat database.Chat
requestedModel uuid.NullUUID
)
if p.hooks.Enabled() || opts.ModelConfigID != uuid.Nil {
chat, err = p.loadChatForAdmission(ctx, opts.ChatID)
if err != nil {
return SendMessageResult{}, err
}
requestedModel, err = validateModelConfigOverride(ctx, p.db, chat.OrganizationID, opts.ModelConfigID)
if err != nil {
return SendMessageResult{}, err
}
}
if p.hooks.Enabled() {
turnID := uuid.New()
// Check queue capacity before dispatch; the transaction
// rechecks it under lock.
queuedCount, err := p.db.CountChatQueuedMessages(ctx, opts.ChatID)
if err != nil {
return SendMessageResult{}, xerrors.Errorf("count queued messages: %w", err)
}
queue, err := chatstate.LoadQueueState(ctx, p.db, opts.ChatID)
if err != nil {
return SendMessageResult{}, xerrors.Errorf("load queue state: %w", err)
}
if err := chatstate.CheckSendMessageQueueCapacity(chat, queue, busyBehaviorToChatState(busyBehavior), queuedCount, p.chatLimits.MaxQueuedMessagesPerChat); err != nil {
return SendMessageResult{}, err
}
promptMessage, err := chathooks.UserPromptMessage(contentParts)
if err != nil {
return SendMessageResult{}, err
}
promptResult, err := p.hooks.Trigger(ctx, chathooks.ChatFor(chat, &turnID), promptMessage, agenthooks.EventUserPromptSubmit, dispatch.CapacityClassAdmission)
if err != nil {
return SendMessageResult{}, p.handleUserPromptDispatchError(ctx, opts.ChatID, chathooks.UserPromptDenial(err))
}
contentParts, _, err = chathooks.ComposeUserPromptContent(contentParts, promptResult)
if err != nil {
return SendMessageResult{}, err
}
}
content, err := chatprompt.MarshalParts(contentParts)
if err != nil {
return SendMessageResult{}, xerrors.Errorf("marshal message content: %w", err)
}
var (
result SendMessageResult
promotedQueuedAt time.Time
)
refreshed, err := p.updateChat(ctx, opts.ChatID, func(tx *chatstate.Tx, store database.Store, snapshot *chatstate.ChatSnapshot) error {
lockedChat := snapshot.Chat
if lockedChat.Archived {
return ErrChatArchived
}
if opts.PlanMode != nil {
var err error
lockedChat, err = store.UpdateChatPlanModeByID(ctx, database.UpdateChatPlanModeByIDParams{
PlanMode: *opts.PlanMode,
ID: opts.ChatID,
})
if err != nil {
return xerrors.Errorf("update chat plan mode: %w", err)
}
}
modelConfigID := requestedModel.UUID
if !requestedModel.Valid {
// The fallback reads the chat's last model, so it runs under
// the row lock.
modelConfigID, err = resolveFallbackModelConfigID(ctx, store, lockedChat, lockedChat.LastModelConfigID)
if err != nil {
return err
}
}
lockedChat, err = p.applyRequestedMCPServerIDs(ctx, store, lockedChat, opts.MCPServerIDs)
if err != nil {
return err
}
if opts.InlineMCPServers != nil {
if err := chatstate.ReplaceInlineMCPServers(ctx, store, lockedChat.ID, *opts.InlineMCPServers); err != nil {
return xerrors.Errorf("replace inline MCP servers: %w", err)
}
}
messageCreatedBy := opts.CreatedBy
if messageCreatedBy == uuid.Nil {
messageCreatedBy = lockedChat.OwnerID
}
firstUserTurn := false
if lockedChat.Title == chatprompt.DefaultChatTitle {
visible, err := store.GetChatMessagesByChatIDAscPaginated(ctx, database.GetChatMessagesByChatIDAscPaginatedParams{
ChatID: opts.ChatID,
LimitVal: 1,
})
if err != nil {
return xerrors.Errorf("probe visible chat messages: %w", err)
}
firstUserTurn = len(visible) == 0
}
// Queue capacity is enforced inside tx.SendMessage; this
// wrapper only propagates the typed error.
message := userMessage(content, modelConfigID, messageCreatedBy, opts.ReasoningEffort)
sendResult, err := tx.SendMessage(chatstate.SendMessageInput{
// The writes above may have made the snapshot stale, so the
// transition reads the chat itself.
Snapshot: nil,
Message: message,
BusyBehavior: busyBehaviorToChatState(busyBehavior),
MaxQueueSize: p.chatLimits.MaxQueuedMessagesPerChat,
FileIDs: chatprompt.FileIDs(contentParts),
MaxFileLinks: p.chatLimits.MaxAttachmentsPerChat,
})
if err != nil {
return err
}
if sendResult.QueuedMessage != nil {
result.Queued = true
result.QueuedMessage = sendResult.QueuedMessage
} else if len(sendResult.InsertedMessages) > 0 {
// The state machine prepends synthetic tool-result
// cancellation messages; the user message is always
// last in the inserted slice.
result.Message = sendResult.InsertedMessages[len(sendResult.InsertedMessages)-1]
}
// A send to an errored or paused chat can also insert queued
// messages; report those inserts so clients can update their
// caches.
result.InsertedMessages = sendResult.InsertedMessages
promotedQueuedAt = sendResult.PromotedQueuedAt
if firstUserTurn && !result.Queued {
result.FirstUserTurn = true
// Persist the fallback title with the message so a failed
// async title call cannot leave the placeholder forever.
firstMessage := []database.ChatMessage{result.Message}
pasteText, err := titlePasteText(ctx, store, firstMessage)
if err != nil {
return err
}
if titleText, ok := titleInput(lockedChat, firstMessage, pasteText); ok {
if _, err := store.UpdateChatTitleByID(ctx, database.UpdateChatTitleByIDParams{
ID: opts.ChatID,
Title: chatprompt.FallbackTitle(titleText),
TitleSource: database.ChatTitleSourceFallback,
}); err != nil {
return xerrors.Errorf("update fallback chat title: %w", err)
}
}
}
return nil
})
if err != nil {
return SendMessageResult{}, err
}
result.Chat = refreshed
if result.FirstUserTurn && result.Chat.Title != chatprompt.DefaultChatTitle {
p.publishChatPubsubEvent(result.Chat, codersdk.ChatWatchEventKindTitleChange, nil)
}
p.recordQueueWait(ctx, result.Chat, promotedQueuedAt)
return result, nil
}
// loadChatForAdmission loads a chat for checks that run before a
// message transaction and rejects archived chats. Callers must recheck
// archived under the row lock.
func (p *Server) loadChatForAdmission(ctx context.Context, chatID uuid.UUID) (database.Chat, error) {
chat, err := p.db.GetChatByID(ctx, chatID)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return database.Chat{}, chatstate.ErrChatNotFound
}
return database.Chat{}, xerrors.Errorf("load chat: %w", err)
}
if chat.Archived {
return database.Chat{}, ErrChatArchived
}
return chat, nil
}
func validateEditTarget(ctx context.Context, store database.Store, chatID uuid.UUID, messageID int64) (database.ChatMessage, error) {
target, err := store.GetChatMessageByID(ctx, messageID)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return database.ChatMessage{}, ErrEditedMessageNotFound
}
return database.ChatMessage{}, xerrors.Errorf("get edited message: %w", err)
}
if target.ChatID != chatID || target.Deleted {
return database.ChatMessage{}, ErrEditedMessageNotFound
}
if target.Role != database.ChatMessageRoleUser {
return database.ChatMessage{}, ErrEditedMessageNotUser
}
return target, nil
}
// EditMessage replaces an earlier user message and discards the
// active-history suffix through chatstate.EditMessage. A requested
// model config is validated before the transaction; the other
// admission checks and writes run in the transition's transaction.
func (p *Server) EditMessage(
ctx context.Context,
opts EditMessageOptions,
) (EditMessageResult, error) {
if opts.ChatID == uuid.Nil {
return EditMessageResult{}, xerrors.New("chat_id is required")
}
if opts.EditedMessageID <= 0 {
return EditMessageResult{}, xerrors.New("edited_message_id is required")
}
if len(opts.Content) == 0 {
return EditMessageResult{}, xerrors.New("content is required")
}
contentParts, err := p.resolveChatSkillInvocations(ctx, opts.ChatID, opts.Content)
if err != nil {
return EditMessageResult{}, err
}
var (
chat database.Chat
requestedModel uuid.NullUUID
sessionStartHookResult *chathooks.Result
)
if p.hooks.Enabled() || opts.ModelConfigID != uuid.Nil {
chat, err = p.loadChatForAdmission(ctx, opts.ChatID)
if err != nil {
return EditMessageResult{}, err
}
// The transaction rechecks the target under the lock.
if _, err := validateEditTarget(ctx, p.db, opts.ChatID, opts.EditedMessageID); err != nil {
return EditMessageResult{}, err
}
requestedModel, err = validateModelConfigOverride(ctx, p.db, chat.OrganizationID, opts.ModelConfigID)
if err != nil {
return EditMessageResult{}, err
}
}
if p.hooks.Enabled() {
turnID := uuid.New()
sessionStartHookResult, err = p.hooks.Trigger(ctx, chathooks.ChatFor(chat, &turnID), chathooks.Message{Source: chathooks.SessionStartSourceClear}, agenthooks.EventSessionStart, dispatch.CapacityClassAdmission)
if err != nil {
return EditMessageResult{}, p.handleAPIDispatchError(ctx, opts.ChatID, agenthooks.EventSessionStart, err)
}
promptMessage, err := chathooks.UserPromptMessage(contentParts)
if err != nil {
return EditMessageResult{}, err
}
promptResult, err := p.hooks.Trigger(ctx, chathooks.ChatFor(chat, &turnID), promptMessage, agenthooks.EventUserPromptSubmit, dispatch.CapacityClassAdmission)
if err != nil {
return EditMessageResult{}, p.handleUserPromptDispatchError(ctx, opts.ChatID, chathooks.UserPromptDenial(err))
}
contentParts, _, err = chathooks.ComposeUserPromptContent(contentParts, promptResult)
if err != nil {
return EditMessageResult{}, err
}
}
content, err := chatprompt.MarshalParts(contentParts)
if err != nil {
return EditMessageResult{}, xerrors.Errorf("marshal message content: %w", err)
}
var (
result EditMessageResult
editedMsg database.ChatMessage
)
refreshed, err := p.updateChat(ctx, opts.ChatID, func(tx *chatstate.Tx, store database.Store, snapshot *chatstate.ChatSnapshot) error {
lockedChat := snapshot.Chat
if lockedChat.Archived {
return ErrChatArchived
}
// The target feeds the post-commit debug cleanup below; the
// transition itself revalidates ownership and role.
target, err := validateEditTarget(ctx, store, opts.ChatID, opts.EditedMessageID)
if err != nil {
return err
}
editedMsg = target
lockedChat, err = p.applyRequestedMCPServerIDs(ctx, store, lockedChat, opts.MCPServerIDs)
if err != nil {
return err
}
modelOverride := requestedModel
if !modelOverride.Valid {
// Without an explicit override the transition preserves
// the edited message's original model, which may have been
// disabled since; resolve it like a normal message send.
preserved := uuid.Nil
if target.ModelConfigID.Valid {
preserved = target.ModelConfigID.UUID
}
resolved, err := resolveFallbackModelConfigID(ctx, store, lockedChat, preserved)
if err != nil {
return err
}
if resolved != preserved {
modelOverride = uuid.NullUUID{UUID: resolved, Valid: true}
}
}
modelConfigID := target.ModelConfigID.UUID
if modelOverride.Valid {
modelConfigID = modelOverride.UUID
}
// The prompt response already rides in the replacement content;
// only the session_start(clear) response needs transcript rows.
// They insert after the replacement so a later edit's suffix
// truncation cleans them up.
suffixMessages, err := chathooks.EventMessages(sessionStartHookResult, modelConfigID)
if err != nil {
return err
}
var reasoningEffortOverride database.NullChatReasoningEffort
if opts.ReasoningEffort != nil && *opts.ReasoningEffort != "" {
reasoningEffortOverride = database.NullChatReasoningEffort{ChatReasoningEffort: database.ChatReasoningEffort(*opts.ReasoningEffort), Valid: true}
}
editResult, err := tx.EditMessage(chatstate.EditMessageInput{
// The writes above may have made the snapshot stale, so the
// transition reads the chat itself.
Snapshot: nil,
MessageID: opts.EditedMessageID,
SuffixMessages: suffixMessages,
CreatedBy: opts.CreatedBy,
Content: content,
ModelConfigIDOverride: modelOverride,
ReasoningEffortOverride: reasoningEffortOverride,
FileIDs: chatprompt.FileIDs(contentParts),
MaxFileLinks: p.chatLimits.MaxAttachmentsPerChat,
})
if err != nil {
if errors.Is(err, chatstate.ErrEditedMessageNotUser) {
return ErrEditedMessageNotUser
}
return err
}
result.Message = editResult.ReplacementMessage
inserted := make([]database.ChatMessage, 0, len(editResult.CancellationMessages)+len(editResult.SuffixMessages)+1)
inserted = append(inserted, editResult.CancellationMessages...)
inserted = append(inserted, editResult.ReplacementMessage)
inserted = append(inserted, editResult.SuffixMessages...)
result.InsertedMessages = inserted
result.DeletedMessageIDs = editResult.DeletedMessageIDs
return nil
})
if err != nil {
return EditMessageResult{}, err
}
result.Chat = refreshed
// An interrupted worker may still flush debug rows after the edit;
// a short bounded retry converges without waiting for dbpurge's
// much longer debug retention window. Cut off at the DB-stamped
// updated_at, the clock that also stamps replacement-turn rows,
// minus debugCleanupClockSkew so replica drift cannot delete the
// replacement turn's rows.
editCutoff := refreshed.UpdatedAt.Add(-debugCleanupClockSkew)
p.scheduleDebugCleanup(
ctx,
"failed to delete chat debug rows after edit",
[]slog.Field{
slog.F("chat_id", opts.ChatID),
slog.F("edited_message_id", editedMsg.ID),
},
func(cleanupCtx context.Context, debugSvc *chatdebug.Service) error {
_, err := debugSvc.DeleteAfterMessageID(cleanupCtx, opts.ChatID, editedMsg.ID-1, editCutoff)
return err
},
)
return result, nil
}
// ErrArchiveRequiresRootChat is returned by [Server.ArchiveChat] and
// [Server.UnarchiveChat] when the supplied chat is a child chat.
// Archive state changes must always target the root chat so the
// whole family flips together.
var ErrArchiveRequiresRootChat = xerrors.New(
"chat archive state can only be changed on the root chat",
)
// ArchiveChat archives a root chat and every child in its family
// through the chatstate state machine. The transition is atomic over
// the whole family: either every member is archived or none is. The
// state machine only permits archive from the idle / error execution
// states (W, E0, E1); active members cause a state conflict that the
// HTTP handler maps to a client error.
//
// Child chats must not be archived independently. ArchiveChat
// rejects them with [ErrArchiveRequiresRootChat] so callers cannot
// silently break the parent-implies-child archive invariant.
//
//nolint:staticcheck // Receiver name matches the other Server methods in this file.
func (p *Server) ArchiveChat(ctx context.Context, chat database.Chat) error {
return p.setChatFamilyArchived(ctx, chat, true)
}
// UnarchiveChat unarchives a root chat and every child in its family
// through the chatstate state machine. Like ArchiveChat the cascade
// is atomic; ChildChat unarchive attempts are rejected with
// [ErrArchiveRequiresRootChat].
func (p *Server) UnarchiveChat(ctx context.Context, chat database.Chat) error {
return p.setChatFamilyArchived(ctx, chat, false)
}
// setChatFamilyArchived applies SetArchived(archived) to every chat
// in chat's family through chatstate. The transaction-captured
// family rows feed the post-commit debug cleanup and sidebar watch
// events.
//
//nolint:revive // Existing API takes the target archive state as a boolean.
func (p *Server) setChatFamilyArchived(ctx context.Context, chat database.Chat, archived bool) error {
if chat.ID == uuid.Nil {
return xerrors.New("chat_id is required")
}
if chat.ParentChatID.Valid {
return ErrArchiveRequiresRootChat
}
familyChats, err := chatstate.SetFamilyArchived(ctx, p.db, p.pubsub, chatstate.SetFamilyArchivedInput{
RootID: chat.ID,
Archived: archived,
})
if err != nil {
return err
}
watchKind := codersdk.ChatWatchEventKindCreated
if archived {
p.scheduleArchiveDebugCleanup(ctx, familyChats)
watchKind = codersdk.ChatWatchEventKindDeleted
}
p.publishChatPubsubEvents(familyChats, watchKind)
return nil
}
// MoveChatToProject moves a root chat into a project, or out of its
// project when projectID is NULL. Its subagent chats follow because they
// use the root's project. While the chat or a subagent chat is not idle,
// it returns [chatstate.ErrTransitionNotAllowed], unless the chat is
// already in that project, which changes nothing.
func (p *Server) MoveChatToProject(ctx context.Context, chat database.Chat, projectID uuid.NullUUID) error {
if chat.ID == uuid.Nil {
return xerrors.New("chat_id is required")
}
if chat.ParentChatID.Valid {
return chatstate.ErrChatNotRoot
}
familyChats, err := chatstate.SetFamilyProject(ctx, p.db, p.pubsub, chatstate.SetFamilyProjectInput{
RootID: chat.ID,
ProjectID: projectID,
})
if err != nil {
return err
}
p.publishChatPubsubEvents(familyChats, codersdk.ChatWatchEventKindProjectChange)
return nil
}
// DeleteQueued removes a queued user message through the chatstate
// state machine. Stream side effects are handled by chat:update
// consumers; a status change publishes the sidebar watch event.
func (p *Server) DeleteQueued(
ctx context.Context,
chatID uuid.UUID,
queuedMessageID int64,
) error {
if chatID == uuid.Nil {
return xerrors.New("chat_id is required")
}
var deleted chatstate.DeleteQueuedMessageResult
_, err := p.newChatMachine(chatID).Update(ctx, func(tx *chatstate.Tx, _ database.Store, snapshot *chatstate.ChatSnapshot) error {
if snapshot.Chat.Archived {
return ErrChatArchived
}
var err error
deleted, err = tx.DeleteQueuedMessage(chatstate.DeleteQueuedMessageInput{
Snapshot: snapshot,
QueuedMessageID: queuedMessageID,
})
return err
})
if err != nil {
return err
}
if deleted.Chat != nil {
p.publishChatPubsubEvent(*deleted.Chat, codersdk.ChatWatchEventKindStatusChange, nil)
p.recordQueueWait(ctx, *deleted.Chat, deleted.PromotedQueuedAt)
}
return nil
}
// EditQueuedMessageOptions configures [Server.EditQueuedMessage]. Zero
// values leave the corresponding attribute unchanged.
type EditQueuedMessageOptions struct {
ChatID uuid.UUID
QueuedMessageID int64
Content []codersdk.ChatMessagePart
ModelConfigID uuid.UUID
ReasoningEffort *string
Editing *bool
}
// EditQueuedMessage changes a queued row's content, edit marker, or both
// through the chatstate state machine. Stream side effects are handled
// by chat:update consumers; a status change publishes the sidebar watch
// event.
func (p *Server) EditQueuedMessage(
ctx context.Context,
opts EditQueuedMessageOptions,
) error {
if opts.ChatID == uuid.Nil {
return xerrors.New("chat_id is required")
}
if opts.QueuedMessageID <= 0 {
return xerrors.New("queued_message_id is required")
}
if len(opts.Content) == 0 && opts.Editing == nil {
return xerrors.New("content or editing is required")
}
contentParts, err := p.resolveChatSkillInvocations(ctx, opts.ChatID, opts.Content)
if err != nil {
return err
}
var chat database.Chat
input := chatstate.EditQueuedMessageInput{
QueuedMessageID: opts.QueuedMessageID,
Editing: opts.Editing,
MaxFileLinks: p.chatLimits.MaxAttachmentsPerChat,
}
if len(contentParts) > 0 && (p.hooks.Enabled() || opts.ModelConfigID != uuid.Nil) {
chat, err = p.loadChatForAdmission(ctx, opts.ChatID)
if err != nil {
return err
}
// The transition rechecks the queued row under the lock.
if _, err := p.db.GetChatQueuedMessageByID(ctx, database.GetChatQueuedMessageByIDParams{
ID: opts.QueuedMessageID,
ChatID: opts.ChatID,
}); err != nil {
if errors.Is(err, sql.ErrNoRows) {
return chatstate.ErrQueuedMessageNotFound
}
return xerrors.Errorf("load queued message: %w", err)
}
input.ModelConfigIDOverride, err = validateModelConfigOverride(ctx, p.db, chat.OrganizationID, opts.ModelConfigID)
if err != nil {
return err
}
}
if len(contentParts) > 0 && p.hooks.Enabled() {
turnID := uuid.New()
promptMessage, err := chathooks.UserPromptMessage(contentParts)
if err != nil {
return err
}
promptResult, err := p.hooks.Trigger(ctx, chathooks.ChatFor(chat, &turnID), promptMessage, agenthooks.EventUserPromptSubmit, dispatch.CapacityClassAdmission)
if err != nil {
// A chat with a queued row is never idle, so a failed
// dispatch does not park it in error: only the edit fails.
return chathooks.UserPromptDenial(err)
}
contentParts, _, err = chathooks.ComposeUserPromptContent(contentParts, promptResult)
if err != nil {
return err
}
}
if len(contentParts) > 0 {
content, err := chatprompt.MarshalParts(contentParts)
if err != nil {
return xerrors.Errorf("marshal message content: %w", err)
}
input.Content = content.RawMessage
input.FileIDs = chatprompt.FileIDs(contentParts)
if opts.ReasoningEffort != nil && *opts.ReasoningEffort != "" {
input.ReasoningEffortOverride = database.NullChatReasoningEffort{
ChatReasoningEffort: database.ChatReasoningEffort(*opts.ReasoningEffort),
Valid: true,
}
}
}
var edited chatstate.EditQueuedMessageResult
machine := p.newChatMachine(opts.ChatID)
_, err = machine.Update(ctx, func(tx *chatstate.Tx, _ database.Store, snapshot *chatstate.ChatSnapshot) error {
if snapshot.Chat.Archived {
return ErrChatArchived
}
input.Snapshot = snapshot
var err error
edited, err = tx.EditQueuedMessage(input)
return err
})
if err != nil {
return err
}
if edited.Chat != nil {
p.publishChatPubsubEvent(*edited.Chat, codersdk.ChatWatchEventKindStatusChange, nil)
p.recordQueueWait(ctx, *edited.Chat, edited.PromotedQueuedAt)
}
return nil
}
// PromoteQueued moves a queued message to the steer lane. From E1, E1P,
// and P it delivers the steer lane in the same transaction. From other
// states the steer lane waits for the next delivery point. It never
// interrupts.
func (p *Server) PromoteQueued(
ctx context.Context,
opts PromoteQueuedOptions,
) error {
if opts.ChatID == uuid.Nil {
return xerrors.New("chat_id is required")
}
var promotedQueuedAt time.Time
refreshed, err := p.updateChat(ctx, opts.ChatID, func(tx *chatstate.Tx, _ database.Store, snapshot *chatstate.ChatSnapshot) error {
if snapshot.Chat.Archived {
return ErrChatArchived
}
steered, err := tx.SteerQueuedMessage(chatstate.SteerQueuedMessageInput{
Snapshot: snapshot,
QueuedMessageID: opts.QueuedMessageID,
})
if err != nil {
return err
}
promotedQueuedAt = steered.PromotedQueuedAt
return nil
})
if err != nil {
return err
}
p.recordQueueWait(ctx, refreshed, promotedQueuedAt)
return nil
}
// SubmitToolResults dispatches hooks before completing the
// requires_action transition.
func (p *Server) SubmitToolResults(
ctx context.Context,
opts SubmitToolResultsOptions,
) error {
var hookSuffix []chatstate.Message
if p.hooks.Enabled() {
state, err := loadDynamicPostToolUseState(ctx, p.newChatMachine(opts.ChatID), opts)
if err != nil {
return err
}
for _, result := range opts.Results {
response, err := p.hooks.Trigger(ctx, chathooks.ChatFor(state.chat, nil), chathooks.DynamicPostToolUseMessage(result, state.toolNames[result.ToolCallID]), agenthooks.EventPostToolUse, dispatch.CapacityClassGeneration)
if err != nil {
// Leave pending calls intact so the client can resubmit after recovery.
return chathooks.GenerationDispatchError(agenthooks.EventPostToolUse, err)
}
responseMessages, err := chathooks.EventMessages(response, state.modelConfigID)
if err != nil {
return err
}
hookSuffix = append(hookSuffix, responseMessages...)
}
}
_, err := p.updateChat(ctx, opts.ChatID, func(tx *chatstate.Tx, _ database.Store, snapshot *chatstate.ChatSnapshot) error {
locked := snapshot.Chat
if locked.Archived {
return ErrChatArchived
}
toolResults := make([]chatstate.ToolResultInput, 0, len(opts.Results))
for _, result := range opts.Results {
toolResults = append(toolResults, chatstate.ToolResultInput{
ToolCallID: result.ToolCallID,
Output: result.Output,
IsError: result.IsError,
})
}
modelConfigID := opts.ModelConfigID
if modelConfigID == uuid.Nil {
modelConfigID = locked.LastModelConfigID
}
if _, err := tx.CompleteRequiresAction(chatstate.CompleteRequiresActionInput{
Snapshot: snapshot,
CreatedBy: opts.UserID,
ModelConfigID: modelConfigID,
Results: toolResults,
SuffixMessages: hookSuffix,
}); err != nil {
if !errors.Is(err, chatstate.ErrInvalidState) &&
locked.Status != database.ChatStatusRequiresAction &&
errors.Is(err, chatstate.ErrTransitionNotAllowed) {
return &ToolResultStatusConflictError{ActualStatus: locked.Status}
}
return xerrors.Errorf("complete requires action: %w", err)
}
return nil
})
if err != nil {
var conflict *ToolResultStatusConflictError
if errors.As(err, &conflict) {
return conflict
}
return translateToolResultValidationError(err)
}
return nil
}
// translateToolResultValidationError converts a chatstate tool-result
// validation error into the legacy chatd.ToolResultValidationError
// shape so HTTP handlers preserve their existing response detail. If
// err is not a tool-result validation error, it is returned
// unchanged.
func translateToolResultValidationError(err error) error {
var v *chatstate.ToolResultValidationError
if !errors.As(err, &v) {
return err
}
switch {
case xerrors.Is(v, chatstate.ErrToolResultDuplicate):
return &ToolResultValidationError{
Message: "Duplicate tool_call_id in results.",
Detail: fmt.Sprintf("Duplicate tool call ID %q.", v.ToolCallID),
}
case xerrors.Is(v, chatstate.ErrToolResultMissing):
return &ToolResultValidationError{
Message: "Missing tool result.",
Detail: fmt.Sprintf("Missing result for tool call %q.", v.ToolCallID),
}
case xerrors.Is(v, chatstate.ErrToolResultUnexpected):
return &ToolResultValidationError{
Message: "Unexpected tool result.",
Detail: fmt.Sprintf("No pending tool call with ID %q.", v.ToolCallID),
}
case xerrors.Is(v, chatstate.ErrToolResultInvalidJSON):
return &ToolResultValidationError{
Message: "Tool result output must be valid JSON.",
Detail: fmt.Sprintf("Output for tool call %q is not valid JSON.", v.ToolCallID),
}
default:
return err
}
}
// InterruptChat interrupts execution through the chatstate.Interrupt
// transition. Active runs land in `interrupting`; requires-action
// chats synthesize cancellation messages and return to running.
//
// Returns the post-transition chat, whether the call interrupted
// anything, and an error so callers can map state conflicts
// deliberately. A chat with nothing to interrupt, such as an idle
// chat, is returned unchanged without an error.
func (p *Server) InterruptChat(
ctx context.Context,
chat database.Chat,
) (database.Chat, bool, error) {
if chat.ID == uuid.Nil {
return chat, false, xerrors.New("chat_id is required")
}
var result chatstate.InterruptResult
refreshed, err := p.updateChat(ctx, chat.ID, func(tx *chatstate.Tx, _ database.Store, snapshot *chatstate.ChatSnapshot) error {
var err error
result, err = tx.Interrupt(chatstate.InterruptInput{
Snapshot: snapshot,
Reason: "Tool execution interrupted by user",
})
return err
})
if err != nil {
return chat, false, err
}
p.recordQueueWait(ctx, refreshed, result.PromotedQueuedAt)
if result.FinishedInterruption {
p.afterInlineInterruption(ctx, refreshed)
}
return refreshed, result.Interrupted, nil
}
// afterInlineInterruption applies the side effects a worker applies
// after it finishes an interruption, for an interruption that a
// transition finished inline because no worker owned the chat. The
// transition already committed, so a failure is logged.
func (p *Server) afterInlineInterruption(ctx context.Context, chat database.Chat) {
if err := p.afterInterruptionOutcome(ctx, interruptionOutcome{
Chat: chat,
Kind: runnerActionKindFinishInterruption,
}); err != nil {
p.logger.Warn(ctx, "interruption post-outcome side effects failed",
slog.F("chat_id", chat.ID), slog.Error(err))
}
}
// CompactChat records a manual compaction request through the
// chatstate.RequestCompaction transition and wakes workers. The chat
// must be idle (waiting) or errored; the request clears any stored
// error. The worker then generates and commits the compaction summary
// through the normal generation loop, bypassing the usage threshold,
// and the chat returns to waiting with no assistant follow-up unless
// queued messages remain or a post_compact hook commits a
// user-visible message.
//
// Returns the post-transition chat and an error so callers can map
// state conflicts deliberately: archived chats return ErrChatArchived,
// generating chats return a chatstate.ErrTransitionNotAllowed wrapper,
// and chats with no compactable conversation return
// ErrNothingToCompact.
func (p *Server) CompactChat(
ctx context.Context,
chat database.Chat,
) (database.Chat, error) {
if chat.ID == uuid.Nil {
return chat, xerrors.New("chat_id is required")
}
refreshed, err := p.updateChat(ctx, chat.ID, func(tx *chatstate.Tx, store database.Store, snapshot *chatstate.ChatSnapshot) error {
if snapshot.Chat.Archived {
return ErrChatArchived
}
// Run the transition before content and usage validation so busy
// chats surface the state conflict first.
if _, err := tx.RequestCompaction(chatstate.RequestCompactionInput{Snapshot: snapshot}); err != nil {
return err
}
// Reject requests with nothing to compact inside the same
// transaction (rolling back the transition) so no LLM call
// is ever started for an empty or already-compacted chat.
// This also covers a double-/compact.
messages, err := store.GetChatMessagesByChatID(ctx, database.GetChatMessagesByChatIDParams{
ChatID: chat.ID,
AfterID: 0,
})
if err != nil {
return xerrors.Errorf("load chat messages: %w", err)
}
boundary := latestContextBoundaryIndex(messages)
if _, ok := firstUncompressedAssistantAfter(messages, boundary); !ok {
return ErrNothingToCompact
}
return nil
})
if err != nil {
return chat, err
}
return refreshed, nil
}
// ClearChat commits a manual context reset through the
// chatstate.ClearContext transition. Unlike CompactChat it is fully
// synchronous: the boundary rows commit in this transaction with no
// model call, the transcript is preserved, and any stored error is
// cleared. Archived chats return ErrChatArchived, busy chats return a
// chatstate.ErrTransitionNotAllowed wrapper, and chats with nothing
// after the latest context boundary return ErrNothingToClear.
func (p *Server) ClearChat(
ctx context.Context,
chat database.Chat,
) (database.Chat, error) {
if chat.ID == uuid.Nil {
return chat, xerrors.New("chat_id is required")
}
refreshed, err := p.updateChat(ctx, chat.ID, func(tx *chatstate.Tx, store database.Store, snapshot *chatstate.ChatSnapshot) error {
if snapshot.Chat.Archived {
return ErrChatArchived
}
// Read the pre-clear history before the transition inserts the
// boundary rows; eligibility is evaluated afterwards so busy
// chats surface the state conflict first.
messages, err := store.GetChatMessagesByChatID(ctx, database.GetChatMessagesByChatIDParams{
ChatID: chat.ID,
AfterID: 0,
})
if err != nil {
return xerrors.Errorf("load chat messages: %w", err)
}
clearMessages, err := buildClearMessages(buildClearMessagesInput{
modelConfigID: snapshot.Chat.LastModelConfigID,
toolCallID: "chat_cleared_" + uuid.NewString(),
})
if err != nil {
return xerrors.Errorf("build clear messages: %w", err)
}
if _, err := tx.ClearContext(chatstate.ClearContextInput{Snapshot: snapshot, Messages: clearMessages}); err != nil {
return err
}
// Reject no-op clears inside the same transaction so an empty
// or already-cleared chat never gains a duplicate boundary.
boundary := latestContextBoundaryIndex(messages)
if !hasClearableMessageAfter(messages, boundary) {
return ErrNothingToClear
}
return nil
})
if err != nil {
return chat, err
}
return refreshed, nil
}
// ReconcileInvalidStateChat recovers a chat stuck in an invalid
// execution-state combination by running the
// chatstate.ReconcileInvalidState transition. The chat reaches an
// error state (E0, E1, or E1P); queued messages are preserved and
// pending dynamic-tool calls are closed with synthetic cancellations.
//
// Returns the post-transition chat. When the chat is not actually in an
// invalid state the transition returns a wrapped
// chatstate.ErrTransitionNotAllowed; a missing chat returns
// chatstate.ErrChatNotFound. Callers map these to deliberate HTTP
// responses.
func (p *Server) ReconcileInvalidStateChat(
ctx context.Context,
chat database.Chat,
) (database.Chat, error) {
if chat.ID == uuid.Nil {
return chat, xerrors.New("chat_id is required")
}
refreshed, err := p.updateChat(ctx, chat.ID, func(tx *chatstate.Tx, _ database.Store, snapshot *chatstate.ChatSnapshot) error {
_, err := tx.ReconcileInvalidState(chatstate.ReconcileInvalidStateInput{Snapshot: snapshot})
return err
})
if err != nil {
return chat, err
}
return refreshed, nil
}