Files
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

176 lines
5.5 KiB
Go

package chatd
import (
"context"
"encoding/json"
"errors"
"github.com/google/uuid"
"github.com/sqlc-dev/pqtype"
"golang.org/x/xerrors"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/x/chatd/chathooks"
"github.com/coder/coder/v2/coderd/x/chatd/chatstate"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/codersdk/x/agenthooks"
)
// applyHookResultMessages inserts hook event rows before the step's
// own rows so injected model context precedes the assistant content it
// steers; providers require tool results to directly follow the
// assistant tool calls.
func applyHookResultMessages(
messages stepMessagesForCommit,
results []*chathooks.Result,
modelConfigID uuid.UUID,
) (stepMessagesForCommit, error) {
return insertHookResultMessages(messages, results, modelConfigID, hookRowsBeforeStep)
}
func appendHookResultMessages(
messages stepMessagesForCommit,
results []*chathooks.Result,
modelConfigID uuid.UUID,
) (stepMessagesForCommit, error) {
return insertHookResultMessages(messages, results, modelConfigID, hookRowsAfterStep)
}
type hookRowPlacement int
const (
hookRowsBeforeStep hookRowPlacement = iota
hookRowsAfterStep
)
func insertHookResultMessages(
messages stepMessagesForCommit,
results []*chathooks.Result,
modelConfigID uuid.UUID,
placement hookRowPlacement,
) (stepMessagesForCommit, error) {
rows, err := chathooks.EventMessagesForResults(results, modelConfigID)
if err != nil {
return stepMessagesForCommit{}, err
}
if len(rows) > 0 {
if placement == hookRowsBeforeStep {
messages.Messages = append(rows, messages.Messages...)
} else {
messages.Messages = append(messages.Messages, rows...)
}
}
return messages, nil
}
func (p *Server) handleUserPromptDispatchError(ctx context.Context, chatID uuid.UUID, dispatchErr error) error {
return p.handleAPIDispatchError(ctx, chatID, agenthooks.EventUserPromptSubmit, dispatchErr)
}
func (p *Server) handleAPIDispatchError(ctx context.Context, chatID uuid.UUID, eventType agenthooks.EventType, dispatchErr error) error {
lastError, ok := chathooks.DispatchErrorMessage(eventType, dispatchErr)
if !ok {
return dispatchErr
}
encoded, marshalErr := json.Marshal(codersdk.ChatError{
Message: lastError,
Kind: codersdk.ChatErrorKindHookDispatchFailed,
})
if marshalErr != nil {
return errors.Join(dispatchErr, xerrors.Errorf("encode hook dispatch error: %w", marshalErr))
}
machine := p.newChatMachine(chatID)
failedChat, err := machine.Update(ctx, func(tx *chatstate.Tx, _ database.Store, snapshot *chatstate.ChatSnapshot) error {
// Park only idle chats. FinishError is also allowed from running
// states, but a running chat keeps its active turn and the
// request error alone surfaces to the caller.
if snapshot.Chat.Status != database.ChatStatusWaiting {
return chatstate.ErrTransitionNotAllowed
}
_, err := tx.FinishError(chatstate.FinishErrorInput{
Snapshot: snapshot,
LastError: pqtype.NullRawMessage{RawMessage: encoded, Valid: true},
})
return err
})
if errors.Is(err, chatstate.ErrTransitionNotAllowed) {
return dispatchErr
}
if err != nil {
return errors.Join(dispatchErr, xerrors.Errorf("fail idle chat after hook dispatch: %w", err))
}
p.publishChatPubsubEvent(failedChat, codersdk.ChatWatchEventKindStatusChange, nil)
return dispatchErr
}
type dynamicPostToolUseState struct {
chat database.Chat
modelConfigID uuid.UUID
toolNames map[string]string
}
func loadDynamicPostToolUseState(
ctx context.Context,
machine *chatstate.ChatMachine,
opts SubmitToolResultsOptions,
) (dynamicPostToolUseState, error) {
var state dynamicPostToolUseState
err := machine.ReadSnapshot(func(store database.Store) error {
chat, err := store.GetChatByID(ctx, opts.ChatID)
if err != nil {
return xerrors.Errorf("load chat: %w", err)
}
if chat.Archived {
return ErrChatArchived
}
if chat.Status != database.ChatStatusRequiresAction {
return &ToolResultStatusConflictError{ActualStatus: chat.Status}
}
messages, err := store.GetChatMessagesByChatID(ctx, database.GetChatMessagesByChatIDParams{
ChatID: opts.ChatID,
AfterID: 0,
})
if err != nil {
return xerrors.Errorf("load chat messages: %w", err)
}
_, pending, _, err := unresolvedToolCallsFromHistory(messages, dynamicToolNamesFromChat(chat))
if err != nil {
return xerrors.Errorf("load pending dynamic tool calls: %w", err)
}
toolNames := make(map[string]string, len(pending))
for _, call := range pending {
toolNames[call.ToolCallID] = call.ToolName
}
if err := validateSubmittedToolResults(opts.Results, toolNames); err != nil {
return err
}
modelConfigID := opts.ModelConfigID
if modelConfigID == uuid.Nil {
modelConfigID = chat.LastModelConfigID
}
state = dynamicPostToolUseState{
chat: chat,
modelConfigID: modelConfigID,
toolNames: toolNames,
}
return nil
})
return state, err
}
// validateSubmittedToolResults rejects invalid results before hook dispatch,
// using the same rules as CompleteRequiresAction.
func validateSubmittedToolResults(results []codersdk.ToolResult, toolNames map[string]string) error {
inputs := make([]chatstate.ToolResultInput, 0, len(results))
for _, result := range results {
inputs = append(inputs, chatstate.ToolResultInput{
ToolCallID: result.ToolCallID,
Output: result.Output,
})
}
if invalid := chatstate.ValidateToolResults(inputs, toolNames); invalid != nil {
return translateToolResultValidationError(invalid)
}
return nil
}