2026-02-27 11:50:56 -05:00
package chatd
import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
"sync"
"time"
"charm.land/fantasy"
2026-03-11 14:33:15 -07:00
"charm.land/fantasy/providers/anthropic"
2026-02-27 11:50:56 -05:00
"github.com/google/uuid"
"github.com/sqlc-dev/pqtype"
2026-03-11 15:07:46 -07:00
"golang.org/x/sync/errgroup"
2026-02-27 11:50:56 -05:00
"golang.org/x/xerrors"
"cdr.dev/slog/v3"
2026-03-13 18:30:49 +01:00
"github.com/coder/coder/v2/coderd/chatd/chatcost"
2026-02-27 11:50:56 -05:00
"github.com/coder/coder/v2/coderd/chatd/chatloop"
"github.com/coder/coder/v2/coderd/chatd/chatprompt"
"github.com/coder/coder/v2/coderd/chatd/chatprovider"
"github.com/coder/coder/v2/coderd/chatd/chattool"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/db2sdk"
"github.com/coder/coder/v2/coderd/database/dbauthz"
"github.com/coder/coder/v2/coderd/database/pubsub"
coderdpubsub "github.com/coder/coder/v2/coderd/pubsub"
2026-02-28 23:40:17 -05:00
"github.com/coder/coder/v2/coderd/webpush"
2026-02-27 11:50:56 -05:00
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/codersdk/workspacesdk"
2026-03-13 19:49:34 +01:00
"github.com/coder/quartz"
2026-02-27 11:50:56 -05:00
)
const (
// DefaultPendingChatAcquireInterval is the default time between attempts to
// acquire pending chats.
DefaultPendingChatAcquireInterval = time . Second
// DefaultInFlightChatStaleAfter is the default age after which a running
// chat is considered stale and should be recovered.
DefaultInFlightChatStaleAfter = 5 * time . Minute
homeInstructionLookupTimeout = 5 * time . Second
instructionCacheTTL = 5 * time . Minute
2026-03-11 11:00:08 -07:00
chatHeartbeatInterval = 60 * time . Second
2026-02-27 11:50:56 -05:00
maxChatSteps = 1200
2026-03-06 13:02:25 -08:00
// maxStreamBufferSize caps the number of events buffered
// per chat during a single LLM step. When exceeded the
// oldest event is evicted so memory stays bounded.
maxStreamBufferSize = 10000
2026-02-27 11:50:56 -05:00
2026-02-27 15:25:40 -05:00
// staleRecoveryIntervalDivisor determines how often the stale
// recovery loop runs relative to the stale threshold. A value
// of 5 means recovery runs at 1/5 of the stale-after duration.
staleRecoveryIntervalDivisor = 5
2026-03-11 11:00:08 -07:00
// maxChatsPerAcquire is the maximum number of chats to
// acquire in a single processOnce call. Batching avoids
// waiting a full polling interval between acquisitions
// when many chats are pending.
maxChatsPerAcquire int32 = 10
2026-02-27 11:50:56 -05:00
defaultSubagentInstruction = "You are running as a delegated sub-agent chat. Complete the delegated task and provide clear, concise assistant responses for the parent agent."
)
// Server handles background processing of pending chats.
type Server struct {
cancel context . CancelFunc
closed chan struct {}
inflight sync . WaitGroup
db database . Store
workerID uuid . UUID
logger slog . Logger
2026-03-04 18:42:28 -05:00
subscribeFn SubscribeFn
2026-02-27 11:50:56 -05:00
agentConnFn AgentConnFunc
createWorkspaceFn chattool . CreateWorkspaceFn
2026-03-05 10:34:24 -05:00
startWorkspaceFn chattool . StartWorkspaceFn
2026-02-27 11:50:56 -05:00
pubsub pubsub . Pubsub
2026-02-28 23:40:17 -05:00
webpushDispatcher webpush . Dispatcher
2026-02-27 11:50:56 -05:00
providerAPIKeys chatprovider . ProviderAPIKeys
2026-03-11 11:00:08 -07:00
// chatStreams stores per-chat stream state. Using sync.Map
// gives each chat independent locking — concurrent chats
// never contend with each other.
chatStreams sync . Map // uuid.UUID -> *chatStreamState
2026-02-27 11:50:56 -05:00
// instructionCache caches home instruction file contents by
// workspace agent ID so we don't re-dial on every chat turn.
instructionCacheMu sync . Mutex
instructionCache map [ uuid . UUID ] cachedInstruction
// Configuration
pendingChatAcquireInterval time . Duration
inFlightChatStaleAfter time . Duration
}
type cachedInstruction struct {
instruction string
fetchedAt time . Time
}
// AgentConnFunc provides access to workspace agent connections.
type AgentConnFunc func ( ctx context . Context , agentID uuid . UUID ) ( workspacesdk . AgentConn , func (), error )
2026-03-04 18:42:28 -05:00
// SubscribeFn replaces the default local-only subscription with a
// multi-replica-aware implementation that merges pubsub notifications,
// remote relay streams, and local parts into a single event channel.
// When set, Subscribe delegates the event-merge goroutine to this
// function instead of using simple local forwarding.
//
// Parameters:
// - ctx: subscription lifetime context (canceled on unsubscribe).
// - params: all state needed to build the merged stream.
//
2026-03-06 13:02:25 -08:00
// Returns the merged event channel. Cleanup is driven by ctx
// cancellation — the merge goroutine tears down all relay state
// in its defer when ctx is done.
2026-03-04 18:42:28 -05:00
// Set by enterprise for HA deployments. Nil in AGPL single-replica.
type SubscribeFn func (
2026-02-27 11:50:56 -05:00
ctx context . Context ,
2026-03-04 18:42:28 -05:00
params SubscribeFnParams ,
2026-03-06 13:02:25 -08:00
) <- chan codersdk . ChatStreamEvent
2026-03-04 18:42:28 -05:00
// StatusNotification informs the enterprise relay manager of chat
// status changes so it can open or close relay connections.
type StatusNotification struct {
Status database . ChatStatus
WorkerID uuid . UUID
}
// SubscribeFnParams carries the state that the enterprise
// SubscribeFn implementation needs from the OSS Subscribe preamble.
type SubscribeFnParams struct {
ChatID uuid . UUID
Chat database . Chat
WorkerID uuid . UUID
StatusNotifications <- chan StatusNotification
RequestHeader http . Header
DB database . Store
Logger slog . Logger
}
2026-02-27 11:50:56 -05:00
type chatStreamState struct {
2026-03-11 11:00:08 -07:00
mu sync . Mutex
2026-02-27 11:50:56 -05:00
buffer [] codersdk . ChatStreamEvent
buffering bool
subscribers map [ uuid . UUID ] chan codersdk . ChatStreamEvent
}
// MaxQueueSize is the maximum number of queued user messages per chat.
const MaxQueueSize = 20
var (
// ErrMessageQueueFull indicates the per-chat queue limit was reached.
ErrMessageQueueFull = xerrors . New ( "chat message queue is full" )
// ErrEditedMessageNotFound indicates the edited message does not exist
// in the target chat.
ErrEditedMessageNotFound = xerrors . New ( "edited message not found" )
// ErrEditedMessageNotUser indicates a non-user message edit attempt.
ErrEditedMessageNotUser = xerrors . New ( "only user messages can be edited" )
2026-03-04 18:42:28 -05:00
// errChatTakenByOtherWorker is a sentinel used inside the
// processChat cleanup transaction to signal that another
// worker acquired the chat, so all post-TX side effects
// (status publish, pubsub, web push) must be skipped.
errChatTakenByOtherWorker = xerrors . New ( "chat acquired by another worker" )
2026-02-27 11:50:56 -05:00
)
// CreateOptions controls chat creation in the shared chat mutation path.
type CreateOptions struct {
OwnerID uuid . UUID
WorkspaceID uuid . NullUUID
ParentChatID uuid . NullUUID
RootChatID uuid . NullUUID
Title string
ModelConfigID uuid . UUID
2026-03-13 19:49:34 +01:00
ChatMode database . NullChatMode
2026-02-27 11:50:56 -05:00
SystemPrompt string
2026-03-13 17:53:26 +02:00
InitialUserContent [] codersdk . ChatMessagePart
2026-02-27 11:50:56 -05:00
}
// SendMessageBusyBehavior controls what happens when a chat is already active.
type SendMessageBusyBehavior string
const (
// SendMessageBusyBehaviorQueue queues user messages while the chat is busy.
SendMessageBusyBehaviorQueue SendMessageBusyBehavior = "queue"
2026-03-06 15:15:40 -08:00
// SendMessageBusyBehaviorInterrupt queues the message and
// interrupts the active run. The queued message is
// auto-promoted after the interrupted assistant response is
// persisted, ensuring correct message ordering.
2026-02-27 11:50:56 -05:00
SendMessageBusyBehaviorInterrupt SendMessageBusyBehavior = "interrupt"
)
// SendMessageOptions controls user message insertion with busy-state behavior.
type SendMessageOptions struct {
2026-03-13 17:53:26 +02:00
ChatID uuid . UUID
CreatedBy uuid . UUID
Content [] codersdk . ChatMessagePart
ModelConfigID * uuid . UUID
BusyBehavior SendMessageBusyBehavior
2026-02-27 11:50:56 -05:00
}
// SendMessageResult contains the outcome of user message processing.
type SendMessageResult struct {
Queued bool
QueuedMessage * database . ChatQueuedMessage
Message database . ChatMessage
Chat database . Chat
}
// EditMessageOptions controls in-place user message edits.
type EditMessageOptions struct {
ChatID uuid . UUID
2026-03-11 07:00:38 -07:00
CreatedBy uuid . UUID
2026-02-27 11:50:56 -05:00
EditedMessageID int64
2026-03-13 17:53:26 +02:00
Content [] codersdk . ChatMessagePart
2026-02-27 11:50:56 -05:00
}
// EditMessageResult contains the updated user message and chat status.
type EditMessageResult struct {
Message database . ChatMessage
Chat database . Chat
}
// PromoteQueuedOptions controls queued-message promotion.
type PromoteQueuedOptions struct {
ChatID uuid . UUID
2026-03-11 07:00:38 -07:00
CreatedBy uuid . UUID
2026-02-27 11:50:56 -05:00
QueuedMessageID int64
ModelConfigID * uuid . UUID
}
// PromoteQueuedResult contains post-promotion message metadata.
type PromoteQueuedResult struct {
PromotedMessage database . ChatMessage
}
// CreateChat creates a chat, inserts optional system prompt and initial user
// message, and moves the chat into pending status.
func ( p * Server ) CreateChat ( ctx context . Context , opts CreateOptions ) ( database . Chat , error ) {
if opts . OwnerID == uuid . Nil {
return database . Chat {}, xerrors . New ( "owner_id is required" )
}
if strings . TrimSpace ( opts . Title ) == "" {
return database . Chat {}, xerrors . New ( "title is required" )
}
if len ( opts . InitialUserContent ) == 0 {
return database . Chat {}, xerrors . New ( "initial user content is required" )
}
var chat database . Chat
txErr := p . db . InTx ( func ( tx database . Store ) error {
insertedChat , err := tx . InsertChat ( ctx , database . InsertChatParams {
OwnerID : opts . OwnerID ,
WorkspaceID : opts . WorkspaceID ,
ParentChatID : opts . ParentChatID ,
RootChatID : opts . RootChatID ,
LastModelConfigID : opts . ModelConfigID ,
Title : opts . Title ,
2026-03-13 19:49:34 +01:00
Mode : opts . ChatMode ,
2026-02-27 11:50:56 -05:00
})
if err != nil {
return xerrors . Errorf ( "insert chat: %w" , err )
}
systemPrompt := strings . TrimSpace ( opts . SystemPrompt )
if systemPrompt != "" {
2026-03-13 18:47:36 +02:00
systemContent , err := chatprompt . MarshalParts ([] codersdk . ChatMessagePart {
codersdk . ChatMessageText ( systemPrompt ),
})
2026-02-27 11:50:56 -05:00
if err != nil {
return xerrors . Errorf ( "marshal system prompt: %w" , err )
}
_ , err = tx . InsertChatMessage ( ctx , database . InsertChatMessageParams {
2026-03-11 07:00:38 -07:00
ChatID : insertedChat . ID ,
CreatedBy : uuid . NullUUID {},
2026-02-27 11:50:56 -05:00
ModelConfigID : uuid . NullUUID {
UUID : opts . ModelConfigID ,
Valid : true ,
},
2026-03-13 18:47:36 +02:00
Role : database . ChatMessageRoleSystem ,
ContentVersion : chatprompt . CurrentContentVersion ,
Content : systemContent ,
2026-02-27 11:50:56 -05:00
Visibility : database . ChatMessageVisibilityModel ,
InputTokens : sql . NullInt64 {},
OutputTokens : sql . NullInt64 {},
TotalTokens : sql . NullInt64 {},
ReasoningTokens : sql . NullInt64 {},
CacheCreationTokens : sql . NullInt64 {},
CacheReadTokens : sql . NullInt64 {},
ContextLimit : sql . NullInt64 {},
Compressed : sql . NullBool {},
2026-03-13 18:30:49 +01:00
TotalCostMicros : sql . NullInt64 {},
2026-02-27 11:50:56 -05:00
})
if err != nil {
return xerrors . Errorf ( "insert system message: %w" , err )
}
}
2026-03-13 17:53:26 +02:00
userContent , err := chatprompt . MarshalParts ( opts . InitialUserContent )
2026-02-27 11:50:56 -05:00
if err != nil {
return xerrors . Errorf ( "marshal initial user content: %w" , err )
}
_ , err = insertChatMessageWithStore ( ctx , tx , database . InsertChatMessageParams {
ChatID : insertedChat . ID ,
ModelConfigID : uuid . NullUUID {
UUID : opts . ModelConfigID ,
Valid : true ,
},
2026-03-13 18:47:36 +02:00
Role : database . ChatMessageRoleUser ,
ContentVersion : chatprompt . CurrentContentVersion ,
2026-02-27 11:50:56 -05:00
Content : userContent ,
2026-03-11 07:00:38 -07:00
CreatedBy : uuid . NullUUID { UUID : opts . OwnerID , Valid : opts . OwnerID != uuid . Nil },
2026-02-27 11:50:56 -05:00
Visibility : database . ChatMessageVisibilityBoth ,
InputTokens : sql . NullInt64 {},
OutputTokens : sql . NullInt64 {},
TotalTokens : sql . NullInt64 {},
ReasoningTokens : sql . NullInt64 {},
CacheCreationTokens : sql . NullInt64 {},
CacheReadTokens : sql . NullInt64 {},
ContextLimit : sql . NullInt64 {},
2026-03-13 18:30:49 +01:00
TotalCostMicros : sql . NullInt64 {},
2026-02-27 11:50:56 -05:00
Compressed : sql . NullBool {},
})
if err != nil {
return xerrors . Errorf ( "insert initial user message: %w" , err )
}
chat , err = setChatPendingWithStore ( ctx , tx , insertedChat . ID )
if err != nil {
return xerrors . Errorf ( "set chat pending: %w" , err )
}
if ! chat . RootChatID . Valid && ! chat . ParentChatID . Valid {
chat . RootChatID = uuid . NullUUID { UUID : chat . ID , Valid : true }
}
return nil
}, nil )
if txErr != nil {
return database . Chat {}, txErr
}
2026-03-16 08:41:32 -07:00
p . publishChatPubsubEvent ( chat , coderdpubsub . ChatEventKindCreated , nil )
2026-02-27 11:50:56 -05:00
return chat , nil
}
// SendMessage inserts a user message and optionally queues it while the chat
// is busy, then publishes stream + pubsub updates.
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 = SendMessageBusyBehaviorQueue
}
switch busyBehavior {
case SendMessageBusyBehaviorQueue , SendMessageBusyBehaviorInterrupt :
default :
return SendMessageResult {}, xerrors . Errorf ( "invalid busy behavior %q" , opts . BusyBehavior )
}
2026-03-13 17:53:26 +02:00
content , err := chatprompt . MarshalParts ( opts . Content )
2026-02-27 11:50:56 -05:00
if err != nil {
return SendMessageResult {}, xerrors . Errorf ( "marshal message content: %w" , err )
}
var (
result SendMessageResult
queuedMessagesSDK [] codersdk . ChatQueuedMessage
)
txErr := p . db . InTx ( func ( tx database . Store ) error {
lockedChat , err := tx . GetChatByIDForUpdate ( ctx , opts . ChatID )
if err != nil {
return xerrors . Errorf ( "lock chat: %w" , err )
}
modelConfigID := lockedChat . LastModelConfigID
if opts . ModelConfigID != nil {
modelConfigID = * opts . ModelConfigID
}
2026-03-06 15:15:40 -08:00
// Both queue and interrupt behaviors queue messages
// when the chat is busy. Interrupt additionally
// signals the running loop to stop so the queued
// message is promoted sooner. Crucially, this
// guarantees the interrupted assistant response is
// persisted (with a lower id/created_at) before the
// user message is promoted into chat_messages,
// preserving correct conversation order.
if shouldQueueUserMessage ( lockedChat . Status ) {
2026-02-27 11:50:56 -05:00
existingQueued , err := tx . GetChatQueuedMessages ( ctx , opts . ChatID )
if err != nil {
return xerrors . Errorf ( "get queued messages: %w" , err )
}
if len ( existingQueued ) >= MaxQueueSize {
return ErrMessageQueueFull
}
queued , err := tx . InsertChatQueuedMessage ( ctx , database . InsertChatQueuedMessageParams {
ChatID : opts . ChatID ,
Content : content . RawMessage ,
})
if err != nil {
return xerrors . Errorf ( "insert queued message: %w" , err )
}
queuedMessages , err := tx . GetChatQueuedMessages ( ctx , opts . ChatID )
if err != nil {
return xerrors . Errorf ( "get queued messages: %w" , err )
}
result . Queued = true
result . QueuedMessage = & queued
result . Chat = lockedChat
queuedMessagesSDK = db2sdk . ChatQueuedMessages ( queuedMessages )
return nil
}
message , updatedChat , err := insertUserMessageAndSetPending (
ctx ,
tx ,
lockedChat ,
modelConfigID ,
content ,
2026-03-11 07:00:38 -07:00
opts . CreatedBy ,
2026-02-27 11:50:56 -05:00
)
if err != nil {
return err
}
result . Message = message
result . Chat = updatedChat
return nil
}, nil )
if txErr != nil {
return SendMessageResult {}, txErr
}
if result . Queued {
p . publishEvent ( opts . ChatID , codersdk . ChatStreamEvent {
Type : codersdk . ChatStreamEventTypeQueueUpdate ,
ChatID : opts . ChatID ,
QueuedMessages : queuedMessagesSDK ,
})
p . publishChatStreamNotify ( opts . ChatID , coderdpubsub . ChatStreamNotifyMessage {
QueueUpdate : true ,
})
2026-03-06 15:15:40 -08:00
// For interrupt behavior, signal the running loop to
// stop. setChatWaiting publishes a status notification
// that the worker's control subscriber detects, causing
// it to cancel with ErrInterrupted. The deferred cleanup
// in processChat then auto-promotes the queued message
// after persisting the partial assistant response.
if busyBehavior == SendMessageBusyBehaviorInterrupt {
updatedChat , err := p . setChatWaiting ( ctx , opts . ChatID )
if err != nil {
// The message is already queued so the chat is
// not in a broken state — the user can still
// wait for the current run to finish. Log the
// error but don't fail the request.
p . logger . Error ( ctx , "failed to interrupt chat for queued message" ,
slog . F ( "chat_id" , opts . ChatID ),
slog . Error ( err ),
)
} else {
result . Chat = updatedChat
}
}
2026-02-27 11:50:56 -05:00
return result , nil
}
p . publishMessage ( opts . ChatID , result . Message )
p . publishStatus ( opts . ChatID , result . Chat . Status , result . Chat . WorkerID )
2026-03-16 08:41:32 -07:00
p . publishChatPubsubEvent ( result . Chat , coderdpubsub . ChatEventKindStatusChange , nil )
2026-02-27 11:50:56 -05:00
return result , nil
}
// EditMessage updates a user message in-place, truncates all following messages,
// clears queued messages, and moves the chat into pending status.
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" )
}
2026-03-13 17:53:26 +02:00
content , err := chatprompt . MarshalParts ( opts . Content )
2026-02-27 11:50:56 -05:00
if err != nil {
return EditMessageResult {}, xerrors . Errorf ( "marshal message content: %w" , err )
}
var result EditMessageResult
txErr := p . db . InTx ( func ( tx database . Store ) error {
_ , err := tx . GetChatByIDForUpdate ( ctx , opts . ChatID )
if err != nil {
return xerrors . Errorf ( "lock chat: %w" , err )
}
existing , err := tx . GetChatMessageByID ( ctx , opts . EditedMessageID )
if err != nil {
if errors . Is ( err , sql . ErrNoRows ) {
return ErrEditedMessageNotFound
}
return xerrors . Errorf ( "get edited message: %w" , err )
}
if existing . ChatID != opts . ChatID {
return ErrEditedMessageNotFound
}
2026-03-13 18:47:36 +02:00
if existing . Role != database . ChatMessageRoleUser {
2026-02-27 11:50:56 -05:00
return ErrEditedMessageNotUser
}
updatedMessage , err := tx . UpdateChatMessageByID ( ctx , database . UpdateChatMessageByIDParams {
ModelConfigID : uuid . NullUUID {},
Content : content ,
ID : opts . EditedMessageID ,
})
if err != nil {
return xerrors . Errorf ( "update chat message: %w" , err )
}
err = tx . DeleteChatMessagesAfterID ( ctx , database . DeleteChatMessagesAfterIDParams {
ChatID : opts . ChatID ,
AfterID : opts . EditedMessageID ,
})
if err != nil {
return xerrors . Errorf ( "delete later chat messages: %w" , err )
}
err = tx . DeleteAllChatQueuedMessages ( ctx , opts . ChatID )
if err != nil {
return xerrors . Errorf ( "delete queued messages: %w" , err )
}
updatedChat , err := tx . UpdateChatStatus ( ctx , database . UpdateChatStatusParams {
ID : opts . ChatID ,
Status : database . ChatStatusPending ,
WorkerID : uuid . NullUUID {},
StartedAt : sql . NullTime {},
HeartbeatAt : sql . NullTime {},
2026-02-28 12:27:26 -05:00
LastError : sql . NullString {},
2026-02-27 11:50:56 -05:00
})
if err != nil {
return xerrors . Errorf ( "set chat pending: %w" , err )
}
result . Message = updatedMessage
result . Chat = updatedChat
return nil
}, nil )
if txErr != nil {
return EditMessageResult {}, txErr
}
2026-03-06 13:02:25 -08:00
p . publishEditedMessage ( opts . ChatID , result . Message )
2026-02-27 11:50:56 -05:00
p . publishEvent ( opts . ChatID , codersdk . ChatStreamEvent {
Type : codersdk . ChatStreamEventTypeQueueUpdate ,
QueuedMessages : [] codersdk . ChatQueuedMessage {},
})
p . publishChatStreamNotify ( opts . ChatID , coderdpubsub . ChatStreamNotifyMessage {
QueueUpdate : true ,
})
p . publishStatus ( opts . ChatID , result . Chat . Status , result . Chat . WorkerID )
2026-03-16 08:41:32 -07:00
p . publishChatPubsubEvent ( result . Chat , coderdpubsub . ChatEventKindStatusChange , nil )
2026-02-27 11:50:56 -05:00
return result , nil
}
2026-02-27 16:46:19 -05:00
// ArchiveChat archives a chat and all descendants, then broadcasts a deleted event.
func ( p * Server ) ArchiveChat ( ctx context . Context , chatID uuid . UUID ) error {
2026-02-27 11:50:56 -05:00
if chatID == uuid . Nil {
return xerrors . New ( "chat_id is required" )
}
chat , err := p . db . GetChatByID ( ctx , chatID )
if err != nil {
return xerrors . Errorf ( "get chat: %w" , err )
}
2026-03-02 12:00:00 -05:00
if err := p . db . ArchiveChatByID ( ctx , chatID ); err != nil {
return xerrors . Errorf ( "archive chat: %w" , err )
2026-02-27 11:50:56 -05:00
}
2026-03-16 08:41:32 -07:00
p . publishChatPubsubEvent ( chat , coderdpubsub . ChatEventKindDeleted , nil )
2026-02-27 11:50:56 -05:00
return nil
}
2026-03-06 13:02:25 -08:00
// UnarchiveChat unarchives a chat and publishes a created event so sidebar
// clients are notified that the chat has reappeared.
func ( p * Server ) UnarchiveChat ( ctx context . Context , chatID uuid . UUID ) error {
if chatID == uuid . Nil {
return xerrors . New ( "chat_id is required" )
}
chat , err := p . db . GetChatByID ( ctx , chatID )
if err != nil {
return xerrors . Errorf ( "get chat: %w" , err )
}
if err := p . db . UnarchiveChatByID ( ctx , chatID ); err != nil {
return xerrors . Errorf ( "unarchive chat: %w" , err )
}
2026-03-16 08:41:32 -07:00
p . publishChatPubsubEvent ( chat , coderdpubsub . ChatEventKindCreated , nil )
2026-03-06 13:02:25 -08:00
return nil
}
2026-02-27 11:50:56 -05:00
// DeleteQueued removes a queued user message and publishes the queue update.
func ( p * Server ) DeleteQueued (
ctx context . Context ,
chatID uuid . UUID ,
queuedMessageID int64 ,
) error {
if chatID == uuid . Nil {
return xerrors . New ( "chat_id is required" )
}
2026-03-06 13:02:25 -08:00
var queuedMessages [] database . ChatQueuedMessage
var queueLoadedOK bool
txErr := p . db . InTx ( func ( tx database . Store ) error {
// Lock the chat row to prevent processChat from
// auto-promoting a message the user intended to delete.
if _ , err := tx . GetChatByIDForUpdate ( ctx , chatID ); err != nil {
return xerrors . Errorf ( "lock chat: %w" , err )
}
err := tx . DeleteChatQueuedMessage ( ctx , database . DeleteChatQueuedMessageParams {
ID : queuedMessageID ,
ChatID : chatID ,
})
if err != nil {
return xerrors . Errorf ( "delete queued message: %w" , err )
}
var err2 error
queuedMessages , err2 = tx . GetChatQueuedMessages ( ctx , chatID )
if err2 != nil {
p . logger . Warn ( ctx , "failed to load queued messages after delete" ,
slog . F ( "chat_id" , chatID ),
slog . F ( "queued_message_id" , queuedMessageID ),
slog . Error ( err2 ),
)
// Non-fatal: the delete succeeded, so we still commit.
return nil
}
queueLoadedOK = true
2026-02-27 11:50:56 -05:00
return nil
2026-03-06 13:02:25 -08:00
}, nil )
if txErr != nil {
return txErr
2026-02-27 11:50:56 -05:00
}
2026-03-06 13:02:25 -08:00
if queueLoadedOK {
p . publishEvent ( chatID , codersdk . ChatStreamEvent {
Type : codersdk . ChatStreamEventTypeQueueUpdate ,
QueuedMessages : db2sdk . ChatQueuedMessages ( queuedMessages ),
})
}
// Always notify subscribers so they can re-fetch, even if we
// failed to load the updated queue payload above.
2026-02-27 11:50:56 -05:00
p . publishChatStreamNotify ( chatID , coderdpubsub . ChatStreamNotifyMessage {
QueueUpdate : true ,
})
return nil
}
// PromoteQueued promotes a queued message into chat history and marks the chat pending.
func ( p * Server ) PromoteQueued (
ctx context . Context ,
opts PromoteQueuedOptions ,
) ( PromoteQueuedResult , error ) {
if opts . ChatID == uuid . Nil {
return PromoteQueuedResult {}, xerrors . New ( "chat_id is required" )
}
var (
result PromoteQueuedResult
promoted database . ChatMessage
updatedChat database . Chat
remainingQueue [] database . ChatQueuedMessage
)
txErr := p . db . InTx ( func ( tx database . Store ) error {
lockedChat , err := tx . GetChatByIDForUpdate ( ctx , opts . ChatID )
if err != nil {
return xerrors . Errorf ( "lock chat: %w" , err )
}
modelConfigID := lockedChat . LastModelConfigID
if opts . ModelConfigID != nil {
modelConfigID = * opts . ModelConfigID
}
queuedMessages , err := tx . GetChatQueuedMessages ( ctx , opts . ChatID )
if err != nil {
return xerrors . Errorf ( "get queued messages: %w" , err )
}
var (
targetContent json . RawMessage
found bool
)
for _ , qm := range queuedMessages {
if qm . ID == opts . QueuedMessageID {
targetContent = qm . Content
found = true
break
}
}
if ! found {
return xerrors . New ( "queued message not found" )
}
err = tx . DeleteChatQueuedMessage ( ctx , database . DeleteChatQueuedMessageParams {
ID : opts . QueuedMessageID ,
ChatID : opts . ChatID ,
})
if err != nil {
return xerrors . Errorf ( "delete queued message: %w" , err )
}
promoted , updatedChat , err = insertUserMessageAndSetPending (
ctx ,
tx ,
lockedChat ,
modelConfigID ,
pqtype . NullRawMessage {
RawMessage : targetContent ,
Valid : len ( targetContent ) > 0 ,
},
2026-03-11 07:00:38 -07:00
opts . CreatedBy ,
2026-02-27 11:50:56 -05:00
)
if err != nil {
return err
}
remainingQueue , err = tx . GetChatQueuedMessages ( ctx , opts . ChatID )
if err != nil {
return xerrors . Errorf ( "get remaining queue: %w" , err )
}
result . PromotedMessage = promoted
return nil
}, nil )
if txErr != nil {
return PromoteQueuedResult {}, txErr
}
p . publishEvent ( opts . ChatID , codersdk . ChatStreamEvent {
Type : codersdk . ChatStreamEventTypeQueueUpdate ,
QueuedMessages : db2sdk . ChatQueuedMessages ( remainingQueue ),
})
p . publishChatStreamNotify ( opts . ChatID , coderdpubsub . ChatStreamNotifyMessage {
QueueUpdate : true ,
})
p . publishMessage ( opts . ChatID , promoted )
p . publishStatus ( opts . ChatID , updatedChat . Status , updatedChat . WorkerID )
2026-03-16 08:41:32 -07:00
p . publishChatPubsubEvent ( updatedChat , coderdpubsub . ChatEventKindStatusChange , nil )
2026-02-27 11:50:56 -05:00
return result , nil
}
// InterruptChat interrupts execution, sets waiting status, and broadcasts status updates.
func ( p * Server ) InterruptChat (
ctx context . Context ,
chat database . Chat ,
) database . Chat {
if chat . ID == uuid . Nil {
return chat
}
updatedChat , err := p . setChatWaiting ( ctx , chat . ID )
if err != nil {
p . logger . Error ( ctx , "failed to mark chat as waiting" ,
slog . F ( "chat_id" , chat . ID ),
slog . Error ( err ),
)
return chat
}
return updatedChat
}
// RefreshStatus loads the latest chat status and publishes it to stream subscribers.
func ( p * Server ) RefreshStatus ( ctx context . Context , chatID uuid . UUID ) error {
if chatID == uuid . Nil {
return xerrors . New ( "chat_id is required" )
}
chat , err := p . db . GetChatByID ( ctx , chatID )
if err != nil {
return xerrors . Errorf ( "get chat: %w" , err )
}
p . publishStatus ( chat . ID , chat . Status , chat . WorkerID )
return nil
}
func setChatPendingWithStore (
ctx context . Context ,
store database . Store ,
chatID uuid . UUID ,
) ( database . Chat , error ) {
chat , err := store . GetChatByID ( ctx , chatID )
if err != nil {
return database . Chat {}, xerrors . Errorf ( "get chat: %w" , err )
}
if chat . Status == database . ChatStatusPending {
return chat , nil
}
updatedChat , err := store . UpdateChatStatus ( ctx , database . UpdateChatStatusParams {
ID : chat . ID ,
Status : database . ChatStatusPending ,
WorkerID : uuid . NullUUID {},
StartedAt : sql . NullTime {},
HeartbeatAt : sql . NullTime {},
2026-02-28 12:27:26 -05:00
LastError : sql . NullString {},
2026-02-27 11:50:56 -05:00
})
if err != nil {
return database . Chat {}, xerrors . Errorf ( "set chat pending: %w" , err )
}
return updatedChat , nil
}
func ( p * Server ) setChatWaiting ( ctx context . Context , chatID uuid . UUID ) ( database . Chat , error ) {
2026-03-04 18:42:28 -05:00
var updatedChat database . Chat
err := p . db . InTx ( func ( tx database . Store ) error {
locked , lockErr := tx . GetChatByIDForUpdate ( ctx , chatID )
if lockErr != nil {
return xerrors . Errorf ( "lock chat for waiting: %w" , lockErr )
}
// If the chat has already transitioned to pending (e.g.
// SendMessage with interrupt behavior), don't overwrite
// it — the pending status takes priority so the new
// message gets processed.
if locked . Status == database . ChatStatusPending {
updatedChat = locked
return nil
}
var updateErr error
updatedChat , updateErr = tx . UpdateChatStatus ( ctx , database . UpdateChatStatusParams {
ID : chatID ,
Status : database . ChatStatusWaiting ,
WorkerID : uuid . NullUUID {},
StartedAt : sql . NullTime {},
HeartbeatAt : sql . NullTime {},
LastError : sql . NullString {},
})
return updateErr
}, nil )
2026-02-27 11:50:56 -05:00
if err != nil {
return database . Chat {}, err
}
p . publishStatus ( chatID , updatedChat . Status , updatedChat . WorkerID )
2026-03-16 08:41:32 -07:00
p . publishChatPubsubEvent ( updatedChat , coderdpubsub . ChatEventKindStatusChange , nil )
2026-02-27 11:50:56 -05:00
return updatedChat , nil
}
func insertChatMessageWithStore (
ctx context . Context ,
store database . Store ,
params database . InsertChatMessageParams ,
) ( database . ChatMessage , error ) {
message , err := store . InsertChatMessage ( ctx , params )
if err != nil {
return database . ChatMessage {}, xerrors . Errorf ( "insert chat message: %w" , err )
}
return message , nil
}
func insertUserMessageAndSetPending (
ctx context . Context ,
store database . Store ,
lockedChat database . Chat ,
modelConfigID uuid . UUID ,
content pqtype . NullRawMessage ,
2026-03-11 07:00:38 -07:00
createdBy uuid . UUID ,
2026-02-27 11:50:56 -05:00
) ( database . ChatMessage , database . Chat , error ) {
message , err := insertChatMessageWithStore ( ctx , store , database . InsertChatMessageParams {
ChatID : lockedChat . ID ,
ModelConfigID : uuid . NullUUID { UUID : modelConfigID , Valid : true },
2026-03-13 18:47:36 +02:00
Role : database . ChatMessageRoleUser ,
ContentVersion : chatprompt . CurrentContentVersion ,
2026-02-27 11:50:56 -05:00
Content : content ,
2026-03-11 07:00:38 -07:00
CreatedBy : uuid . NullUUID { UUID : createdBy , Valid : createdBy != uuid . Nil },
2026-02-27 11:50:56 -05:00
Visibility : database . ChatMessageVisibilityBoth ,
InputTokens : sql . NullInt64 {},
OutputTokens : sql . NullInt64 {},
TotalTokens : sql . NullInt64 {},
ReasoningTokens : sql . NullInt64 {},
CacheCreationTokens : sql . NullInt64 {},
CacheReadTokens : sql . NullInt64 {},
ContextLimit : sql . NullInt64 {},
2026-03-13 18:30:49 +01:00
TotalCostMicros : sql . NullInt64 {},
2026-02-27 11:50:56 -05:00
Compressed : sql . NullBool {},
})
if err != nil {
return database . ChatMessage {}, database . Chat {}, err
}
if lockedChat . Status == database . ChatStatusPending {
return message , lockedChat , nil
}
updatedChat , err := store . UpdateChatStatus ( ctx , database . UpdateChatStatusParams {
ID : lockedChat . ID ,
Status : database . ChatStatusPending ,
WorkerID : uuid . NullUUID {},
StartedAt : sql . NullTime {},
HeartbeatAt : sql . NullTime {},
2026-02-28 12:27:26 -05:00
LastError : sql . NullString {},
2026-02-27 11:50:56 -05:00
})
if err != nil {
return database . ChatMessage {}, database . Chat {}, xerrors . Errorf ( "set chat pending: %w" , err )
}
return message , updatedChat , nil
}
// shouldQueueUserMessage reports whether a user message should be
// queued while a chat is active.
func shouldQueueUserMessage ( status database . ChatStatus ) bool {
switch status {
case database . ChatStatusRunning , database . ChatStatusPending :
return true
default :
return false
}
}
// Config configures a chat processor.
type Config struct {
Logger slog . Logger
Database database . Store
ReplicaID uuid . UUID
2026-03-04 18:42:28 -05:00
SubscribeFn SubscribeFn
2026-02-27 11:50:56 -05:00
PendingChatAcquireInterval time . Duration
InFlightChatStaleAfter time . Duration
AgentConn AgentConnFunc
CreateWorkspace chattool . CreateWorkspaceFn
2026-03-05 10:34:24 -05:00
StartWorkspace chattool . StartWorkspaceFn
2026-02-27 11:50:56 -05:00
Pubsub pubsub . Pubsub
ProviderAPIKeys chatprovider . ProviderAPIKeys
2026-02-28 23:40:17 -05:00
WebpushDispatcher webpush . Dispatcher
2026-02-27 11:50:56 -05:00
}
// New creates a new chat processor. The processor polls for pending
// chats and processes them. It is the caller's responsibility to call Close
// on the returned instance.
func New ( cfg Config ) * Server {
ctx , cancel := context . WithCancel ( context . Background ())
pendingChatAcquireInterval := cfg . PendingChatAcquireInterval
if pendingChatAcquireInterval == 0 {
pendingChatAcquireInterval = DefaultPendingChatAcquireInterval
}
inFlightChatStaleAfter := cfg . InFlightChatStaleAfter
if inFlightChatStaleAfter == 0 {
inFlightChatStaleAfter = DefaultInFlightChatStaleAfter
}
workerID := cfg . ReplicaID
if workerID == uuid . Nil {
workerID = uuid . New ()
}
p := & Server {
cancel : cancel ,
closed : make ( chan struct {}),
db : cfg . Database ,
workerID : workerID ,
logger : cfg . Logger . Named ( "chat-processor" ),
2026-03-04 18:42:28 -05:00
subscribeFn : cfg . SubscribeFn ,
2026-02-27 11:50:56 -05:00
agentConnFn : cfg . AgentConn ,
createWorkspaceFn : cfg . CreateWorkspace ,
2026-03-05 10:34:24 -05:00
startWorkspaceFn : cfg . StartWorkspace ,
2026-02-27 11:50:56 -05:00
pubsub : cfg . Pubsub ,
2026-02-28 23:40:17 -05:00
webpushDispatcher : cfg . WebpushDispatcher ,
2026-02-27 11:50:56 -05:00
providerAPIKeys : cfg . ProviderAPIKeys ,
instructionCache : make ( map [ uuid . UUID ] cachedInstruction ),
pendingChatAcquireInterval : pendingChatAcquireInterval ,
inFlightChatStaleAfter : inFlightChatStaleAfter ,
}
2026-03-02 15:57:04 +00:00
//nolint:gocritic // The chat processor uses a scoped chatd context.
ctx = dbauthz . AsChatd ( ctx )
2026-02-27 11:50:56 -05:00
go p . start ( ctx )
return p
}
func ( p * Server ) start ( ctx context . Context ) {
defer close ( p . closed )
2026-02-27 15:25:40 -05:00
// Recover stale chats on startup and periodically thereafter
// to handle chats orphaned by crashed or redeployed workers.
2026-02-27 11:50:56 -05:00
p . recoverStaleChats ( ctx )
2026-02-27 15:25:40 -05:00
acquireTicker := time . NewTicker ( p . pendingChatAcquireInterval )
defer acquireTicker . Stop ()
staleRecoveryInterval := p . inFlightChatStaleAfter / staleRecoveryIntervalDivisor
staleTicker := time . NewTicker ( staleRecoveryInterval )
defer staleTicker . Stop ()
2026-02-27 11:50:56 -05:00
for {
select {
case <- ctx . Done ():
return
2026-02-27 15:25:40 -05:00
case <- acquireTicker . C :
2026-02-27 11:50:56 -05:00
p . processOnce ( ctx )
2026-02-27 15:25:40 -05:00
case <- staleTicker . C :
p . recoverStaleChats ( ctx )
2026-02-27 11:50:56 -05:00
}
}
}
func ( p * Server ) processOnce ( ctx context . Context ) {
2026-03-10 07:22:39 -07:00
if ctx . Err () != nil {
return
}
2026-03-11 11:00:08 -07:00
// We detach from the server lifetime to prevent a
// phantom-acquire race: when the server context is
// canceled, the pq driver's watchCancel goroutine
// races with the actual query on the wire. Using a
// context that cannot be canceled ensures the driver
// sees the query result if Postgres executed it.
2026-03-10 07:22:39 -07:00
acquireCtx , acquireCancel := context . WithTimeout (
context . WithoutCancel ( ctx ), 10 * time . Second ,
)
2026-03-11 11:00:08 -07:00
chats , err := p . db . AcquireChats ( acquireCtx , database . AcquireChatsParams {
2026-02-27 11:50:56 -05:00
StartedAt : time . Now (),
WorkerID : p . workerID ,
2026-03-11 11:00:08 -07:00
NumChats : maxChatsPerAcquire ,
2026-02-27 11:50:56 -05:00
})
2026-03-11 11:00:08 -07:00
acquireCancel ()
2026-02-27 11:50:56 -05:00
if err != nil {
2026-03-11 11:00:08 -07:00
p . logger . Error ( ctx , "failed to acquire chats" , slog . Error ( err ))
return
}
if len ( chats ) == 0 {
2026-02-27 11:50:56 -05:00
return
}
2026-03-11 11:00:08 -07:00
// If the server context was canceled while we were
// acquiring, release the chats back to pending.
2026-03-10 07:22:39 -07:00
if ctx . Err () != nil {
releaseCtx , releaseCancel := context . WithTimeout (
context . WithoutCancel ( ctx ), 10 * time . Second ,
)
2026-03-11 11:00:08 -07:00
for _ , chat := range chats {
_ , updateErr := p . db . UpdateChatStatus ( releaseCtx , database . UpdateChatStatusParams {
ID : chat . ID ,
Status : database . ChatStatusPending ,
WorkerID : uuid . NullUUID {},
StartedAt : sql . NullTime {},
HeartbeatAt : sql . NullTime {},
LastError : sql . NullString {},
})
if updateErr != nil {
p . logger . Error ( ctx , "failed to release chat acquired during shutdown" ,
slog . F ( "chat_id" , chat . ID ), slog . Error ( updateErr ))
}
2026-03-10 07:22:39 -07:00
}
2026-03-11 11:00:08 -07:00
releaseCancel ()
2026-03-10 07:22:39 -07:00
return
}
2026-03-11 11:00:08 -07:00
for _ , chat := range chats {
p . inflight . Add ( 1 )
go func () {
defer p . inflight . Done ()
p . processChat ( ctx , chat )
}()
}
2026-02-27 11:50:56 -05:00
}
func ( p * Server ) publishToStream ( chatID uuid . UUID , event codersdk . ChatStreamEvent ) {
2026-03-11 11:00:08 -07:00
state := p . getOrCreateStreamState ( chatID )
state . mu . Lock ()
2026-02-27 11:50:56 -05:00
if event . Type == codersdk . ChatStreamEventTypeMessagePart {
if ! state . buffering {
2026-03-11 11:00:08 -07:00
p . cleanupStreamIfIdle ( chatID , state )
state . mu . Unlock ()
2026-02-27 11:50:56 -05:00
return
}
2026-03-06 13:02:25 -08:00
if len ( state . buffer ) >= maxStreamBufferSize {
p . logger . Warn ( context . Background (), "chat stream buffer full, dropping oldest event" ,
slog . F ( "chat_id" , chatID ), slog . F ( "buffer_size" , len ( state . buffer )))
state . buffer = state . buffer [ 1 :]
}
2026-02-27 11:50:56 -05:00
state . buffer = append ( state . buffer , event )
}
subscribers := make ([] chan codersdk . ChatStreamEvent , 0 , len ( state . subscribers ))
for _ , ch := range state . subscribers {
subscribers = append ( subscribers , ch )
}
2026-03-11 11:00:08 -07:00
state . mu . Unlock ()
2026-02-27 11:50:56 -05:00
for _ , ch := range subscribers {
select {
case ch <- event :
default :
p . logger . Warn ( context . Background (), "dropping chat stream event" ,
slog . F ( "chat_id" , chatID ), slog . F ( "type" , event . Type ))
}
}
2026-03-06 13:02:25 -08:00
// Clean up the stream entry if it was created by
2026-03-11 11:00:08 -07:00
// getOrCreateStreamState but has no subscribers and is not
2026-03-06 13:02:25 -08:00
// actively buffering (e.g. publish with no watchers).
2026-03-11 11:00:08 -07:00
state . mu . Lock ()
p . cleanupStreamIfIdle ( chatID , state )
state . mu . Unlock ()
2026-02-27 11:50:56 -05:00
}
func ( p * Server ) subscribeToStream ( chatID uuid . UUID ) (
[] codersdk . ChatStreamEvent ,
<- chan codersdk . ChatStreamEvent ,
func (),
) {
2026-03-11 11:00:08 -07:00
state := p . getOrCreateStreamState ( chatID )
state . mu . Lock ()
2026-02-27 11:50:56 -05:00
snapshot := append ([] codersdk . ChatStreamEvent ( nil ), state . buffer ... )
id := uuid . New ()
ch := make ( chan codersdk . ChatStreamEvent , 128 )
state . subscribers [ id ] = ch
2026-03-11 11:00:08 -07:00
state . mu . Unlock ()
2026-02-27 11:50:56 -05:00
cancel := func () {
2026-03-11 11:00:08 -07:00
state . mu . Lock ()
// Remove the subscriber but do not close the channel.
// publishToStream copies subscriber references under
// the per-chat lock then sends outside; closing here
// races with that send and can panic. The channel
// becomes unreachable once removed and will be GC'd.
delete ( state . subscribers , id )
p . cleanupStreamIfIdle ( chatID , state )
state . mu . Unlock ()
2026-02-27 11:50:56 -05:00
}
return snapshot , ch , cancel
}
2026-03-11 11:00:08 -07:00
// getOrCreateStreamState returns the per-chat stream state,
// creating one atomically if it doesn't exist. The returned
// state has its own mutex — callers must lock state.mu for
// access.
func ( p * Server ) getOrCreateStreamState ( chatID uuid . UUID ) * chatStreamState {
if val , ok := p . chatStreams . Load ( chatID ); ok {
state , _ := val .( * chatStreamState )
return state
2026-02-27 11:50:56 -05:00
}
2026-03-11 11:00:08 -07:00
val , _ := p . chatStreams . LoadOrStore ( chatID , & chatStreamState {
subscribers : make ( map [ uuid . UUID ] chan codersdk . ChatStreamEvent ),
})
state , _ := val .( * chatStreamState )
return state
2026-02-27 11:50:56 -05:00
}
2026-03-11 11:00:08 -07:00
// cleanupStreamIfIdle removes the chat entry from the sync.Map
// when there are no subscribers and the stream is not buffering.
// The caller must hold state.mu.
func ( p * Server ) cleanupStreamIfIdle ( chatID uuid . UUID , state * chatStreamState ) {
if ! state . buffering && len ( state . subscribers ) == 0 {
p . chatStreams . Delete ( chatID )
2026-02-27 11:50:56 -05:00
}
}
func ( p * Server ) Subscribe (
ctx context . Context ,
chatID uuid . UUID ,
requestHeader http . Header ,
2026-03-02 16:31:04 -05:00
afterMessageID int64 ,
2026-02-27 11:50:56 -05:00
) (
[] codersdk . ChatStreamEvent ,
<- chan codersdk . ChatStreamEvent ,
func (),
bool ,
) {
if p == nil {
return nil , nil , nil , false
}
if ctx == nil {
ctx = context . Background ()
}
// Subscribe to local stream for message_parts (ephemeral).
localSnapshot , localParts , localCancel := p . subscribeToStream ( chatID )
2026-03-04 18:42:28 -05:00
// Merge all event sources.
2026-02-27 11:50:56 -05:00
mergedCtx , mergedCancel := context . WithCancel ( ctx )
mergedEvents := make ( chan codersdk . ChatStreamEvent , 128 )
2026-03-04 18:42:28 -05:00
2026-02-27 11:50:56 -05:00
var allCancels [] func ()
allCancels = append ( allCancels , localCancel )
2026-03-04 18:42:28 -05:00
// Subscribe to pubsub for durable events (status, messages,
// queue updates, errors). When pubsub is nil (e.g. in-memory
// single-instance) we skip this and deliver all local events.
2026-03-06 13:02:25 -08:00
//
// This MUST happen before the DB queries below so that any
// notification published between the query and the subscription
// is not lost (subscribe-first-then-query pattern).
2026-03-04 18:42:28 -05:00
var notifications <- chan coderdpubsub . ChatStreamNotifyMessage
var errCh <- chan error
2026-02-27 11:50:56 -05:00
if p . pubsub != nil {
2026-03-04 18:42:28 -05:00
notifyCh := make ( chan coderdpubsub . ChatStreamNotifyMessage , 10 )
errNotifyCh := make ( chan error , 1 )
notifications = notifyCh
errCh = errNotifyCh
2026-02-27 11:50:56 -05:00
2026-03-04 18:42:28 -05:00
listener := func ( _ context . Context , message [] byte , listenErr error ) {
if listenErr != nil {
2026-02-27 11:50:56 -05:00
select {
case <- mergedCtx . Done ():
2026-03-04 18:42:28 -05:00
case errNotifyCh <- listenErr :
2026-02-27 11:50:56 -05:00
}
return
}
var notify coderdpubsub . ChatStreamNotifyMessage
if unmarshalErr := json . Unmarshal ( message , & notify ); unmarshalErr != nil {
select {
case <- mergedCtx . Done ():
2026-03-04 18:42:28 -05:00
case errNotifyCh <- xerrors . Errorf ( "unmarshal chat stream notify: %w" , unmarshalErr ):
2026-02-27 11:50:56 -05:00
}
return
}
select {
case <- mergedCtx . Done ():
2026-03-04 18:42:28 -05:00
case notifyCh <- notify :
2026-02-27 11:50:56 -05:00
}
}
2026-03-04 18:42:28 -05:00
if pubsubCancel , pubsubErr := p . pubsub . SubscribeWithErr (
2026-02-27 11:50:56 -05:00
coderdpubsub . ChatStreamNotifyChannel ( chatID ),
listener ,
2026-03-04 18:42:28 -05:00
); pubsubErr == nil {
2026-02-27 11:50:56 -05:00
allCancels = append ( allCancels , pubsubCancel )
} else {
2026-03-04 18:42:28 -05:00
p . logger . Warn ( ctx , "failed to subscribe to chat stream notifications" ,
2026-02-27 11:50:56 -05:00
slog . F ( "chat_id" , chatID ),
2026-03-04 18:42:28 -05:00
slog . Error ( pubsubErr ),
2026-02-27 11:50:56 -05:00
)
}
2026-03-04 18:42:28 -05:00
}
2026-02-27 11:50:56 -05:00
2026-03-06 13:02:25 -08:00
// Build initial snapshot synchronously. The pubsub subscription
// is already active so no notifications can be lost during this
// window.
initialSnapshot := make ([] codersdk . ChatStreamEvent , 0 )
// Add local message_parts to snapshot
for _ , event := range localSnapshot {
if event . Type == codersdk . ChatStreamEventTypeMessagePart {
initialSnapshot = append ( initialSnapshot , event )
}
}
// Load initial messages from DB. When afterMessageID > 0 the
// caller already has messages up to that ID (e.g. from the REST
// endpoint), so we only fetch newer ones to avoid sending
// duplicate data.
messages , err := p . db . GetChatMessagesByChatID ( ctx , database . GetChatMessagesByChatIDParams {
ChatID : chatID ,
AfterID : afterMessageID ,
})
if err != nil {
p . logger . Error ( ctx , "failed to load initial chat messages" ,
slog . Error ( err ),
slog . F ( "chat_id" , chatID ),
)
initialSnapshot = append ( initialSnapshot , codersdk . ChatStreamEvent {
Type : codersdk . ChatStreamEventTypeError ,
ChatID : chatID ,
Error : & codersdk . ChatStreamError { Message : "failed to load initial snapshot" },
})
} else {
for _ , msg := range messages {
sdkMsg := db2sdk . ChatMessage ( msg )
initialSnapshot = append ( initialSnapshot , codersdk . ChatStreamEvent {
Type : codersdk . ChatStreamEventTypeMessage ,
ChatID : chatID ,
Message : & sdkMsg ,
})
}
}
// Load initial queue.
queued , err := p . db . GetChatQueuedMessages ( ctx , chatID )
if err != nil {
p . logger . Error ( ctx , "failed to load initial queued messages" ,
slog . Error ( err ),
slog . F ( "chat_id" , chatID ),
)
initialSnapshot = append ( initialSnapshot , codersdk . ChatStreamEvent {
Type : codersdk . ChatStreamEventTypeError ,
ChatID : chatID ,
Error : & codersdk . ChatStreamError { Message : "failed to load initial snapshot" },
})
} else if len ( queued ) > 0 {
initialSnapshot = append ( initialSnapshot , codersdk . ChatStreamEvent {
Type : codersdk . ChatStreamEventTypeQueueUpdate ,
ChatID : chatID ,
QueuedMessages : db2sdk . ChatQueuedMessages ( queued ),
})
}
// Get initial chat state to determine if we need a relay.
chat , chatErr := p . db . GetChatByID ( ctx , chatID )
// Include the current chat status in the snapshot so the
// frontend can gate message_part processing correctly from
// the very first batch, without waiting for a separate REST
// query.
if chatErr != nil {
p . logger . Error ( ctx , "failed to load initial chat state" ,
slog . Error ( chatErr ),
slog . F ( "chat_id" , chatID ),
)
initialSnapshot = append ( initialSnapshot , codersdk . ChatStreamEvent {
Type : codersdk . ChatStreamEventTypeError ,
ChatID : chatID ,
Error : & codersdk . ChatStreamError { Message : "failed to load initial snapshot" },
})
} else {
statusEvent := codersdk . ChatStreamEvent {
Type : codersdk . ChatStreamEventTypeStatus ,
ChatID : chatID ,
Status : & codersdk . ChatStreamStatus {
Status : codersdk . ChatStatus ( chat . Status ),
},
}
// Prepend so the frontend sees the status before any
// message_part events.
initialSnapshot = append ([] codersdk . ChatStreamEvent { statusEvent }, initialSnapshot ... )
}
// Track the last message ID we've seen for DB queries.
// Initialize from afterMessageID so that when the caller passes
// afterMessageID > 0 but no new messages exist yet, the first
// pubsub catch-up doesn't re-fetch already-seen messages.
lastMessageID := afterMessageID
if len ( messages ) > 0 {
lastMessageID = messages [ len ( messages ) - 1 ]. ID
}
2026-03-04 18:42:28 -05:00
// When an enterprise SubscribeFn is provided and the chat
// lookup succeeded, call it to get relay events (message_parts
// from remote replicas). OSS now owns pubsub subscription,
// message catch-up, queue updates, and status forwarding;
// enterprise only manages relay dialing.
var relayEvents <- chan codersdk . ChatStreamEvent
var statusNotifications chan StatusNotification
2026-03-06 13:02:25 -08:00
if p . subscribeFn != nil && chatErr == nil {
2026-03-04 18:42:28 -05:00
statusNotifications = make ( chan StatusNotification , 10 )
2026-03-06 13:02:25 -08:00
relayEvents = p . subscribeFn ( mergedCtx , SubscribeFnParams {
2026-03-04 18:42:28 -05:00
ChatID : chatID ,
Chat : chat ,
WorkerID : p . workerID ,
StatusNotifications : statusNotifications ,
RequestHeader : requestHeader ,
DB : p . db ,
Logger : p . logger ,
})
}
hasPubsub := false
if p . pubsub != nil {
// hasPubsub is only true when we actually subscribed
// successfully above (allCancels will contain the pubsub
// cancel func in that case).
hasPubsub = len ( allCancels ) > 1
}
//nolint:nestif
go func () {
defer close ( mergedEvents )
if statusNotifications != nil {
defer close ( statusNotifications )
}
for {
select {
case <- mergedCtx . Done ():
return
case psErr := <- errCh :
p . logger . Error ( mergedCtx , "chat stream pubsub error" ,
slog . F ( "chat_id" , chatID ),
slog . Error ( psErr ),
)
2026-02-27 11:50:56 -05:00
select {
2026-03-04 18:42:28 -05:00
case mergedEvents <- codersdk . ChatStreamEvent {
Type : codersdk . ChatStreamEventTypeError ,
ChatID : chatID ,
Error : & codersdk . ChatStreamError {
Message : psErr . Error (),
},
}:
2026-02-27 11:50:56 -05:00
case <- mergedCtx . Done ():
2026-03-04 18:42:28 -05:00
}
return
case notify := <- notifications :
2026-03-06 15:11:05 -08:00
if notify . AfterMessageID > 0 || notify . FullRefresh {
afterID := lastMessageID
if notify . FullRefresh {
afterID = 0
}
2026-03-04 18:42:28 -05:00
newMessages , msgErr := p . db . GetChatMessagesByChatID ( mergedCtx , database . GetChatMessagesByChatIDParams {
ChatID : chatID ,
2026-03-06 15:11:05 -08:00
AfterID : afterID ,
2026-03-04 18:42:28 -05:00
})
if msgErr != nil {
p . logger . Warn ( mergedCtx , "failed to get chat messages after pubsub notification" ,
slog . F ( "chat_id" , chatID ),
slog . Error ( msgErr ),
)
} else {
for _ , msg := range newMessages {
sdkMsg := db2sdk . ChatMessage ( msg )
2026-02-27 11:50:56 -05:00
select {
case <- mergedCtx . Done ():
return
case mergedEvents <- codersdk . ChatStreamEvent {
2026-03-04 18:42:28 -05:00
Type : codersdk . ChatStreamEventTypeMessage ,
ChatID : chatID ,
Message : & sdkMsg ,
2026-02-27 11:50:56 -05:00
}:
}
2026-03-04 18:42:28 -05:00
lastMessageID = msg . ID
2026-02-27 11:50:56 -05:00
}
}
}
2026-03-04 18:42:28 -05:00
if notify . Status != "" {
status := database . ChatStatus ( notify . Status )
select {
case <- mergedCtx . Done ():
return
case mergedEvents <- codersdk . ChatStreamEvent {
Type : codersdk . ChatStreamEventTypeStatus ,
ChatID : chatID ,
Status : & codersdk . ChatStreamStatus { Status : codersdk . ChatStatus ( status )},
}:
}
// Notify enterprise relay manager if present.
if statusNotifications != nil {
workerID := uuid . Nil
if notify . WorkerID != "" {
if parsed , parseErr := uuid . Parse ( notify . WorkerID ); parseErr == nil {
workerID = parsed
}
}
select {
case statusNotifications <- StatusNotification { Status : status , WorkerID : workerID }:
case <- mergedCtx . Done ():
return
}
}
}
if notify . Error != "" {
select {
case <- mergedCtx . Done ():
return
case mergedEvents <- codersdk . ChatStreamEvent {
Type : codersdk . ChatStreamEventTypeError ,
ChatID : chatID ,
Error : & codersdk . ChatStreamError {
Message : notify . Error ,
},
}:
}
}
if notify . QueueUpdate {
queuedMsgs , queueErr := p . db . GetChatQueuedMessages ( mergedCtx , chatID )
if queueErr != nil {
p . logger . Warn ( mergedCtx , "failed to get queued messages after pubsub notification" ,
slog . F ( "chat_id" , chatID ),
slog . Error ( queueErr ),
)
} else {
select {
case <- mergedCtx . Done ():
return
case mergedEvents <- codersdk . ChatStreamEvent {
Type : codersdk . ChatStreamEventTypeQueueUpdate ,
ChatID : chatID ,
QueuedMessages : db2sdk . ChatQueuedMessages ( queuedMsgs ),
}:
}
}
}
case event , ok := <- localParts :
if ! ok {
localParts = nil
// Local parts channel closed. If pubsub is
// active we continue with pubsub-driven events.
// Otherwise terminate.
if ! hasPubsub {
return
}
continue
}
if hasPubsub {
// Only forward message_part events from local
// (durable events come via pubsub).
if event . Type == codersdk . ChatStreamEventTypeMessagePart {
select {
case <- mergedCtx . Done ():
return
case mergedEvents <- event :
}
}
} else {
// No pubsub: forward all event types.
select {
case <- mergedCtx . Done ():
return
case mergedEvents <- event :
}
}
case event , ok := <- relayEvents :
if ! ok {
relayEvents = nil
continue
}
2026-02-27 11:50:56 -05:00
select {
case <- mergedCtx . Done ():
return
case mergedEvents <- event :
}
}
2026-03-04 18:42:28 -05:00
}
}()
2026-02-27 11:50:56 -05:00
cancel := func () {
mergedCancel ()
for _ , cancelFn := range allCancels {
if cancelFn != nil {
cancelFn ()
}
}
}
return initialSnapshot , mergedEvents , cancel , true
}
func ( p * Server ) publishEvent ( chatID uuid . UUID , event codersdk . ChatStreamEvent ) {
if event . ChatID == uuid . Nil {
event . ChatID = chatID
}
p . publishToStream ( chatID , event )
}
func ( p * Server ) publishStatus ( chatID uuid . UUID , status database . ChatStatus , workerID uuid . NullUUID ) {
p . publishEvent ( chatID , codersdk . ChatStreamEvent {
Type : codersdk . ChatStreamEventTypeStatus ,
Status : & codersdk . ChatStreamStatus { Status : codersdk . ChatStatus ( status )},
})
notify := coderdpubsub . ChatStreamNotifyMessage {
Status : string ( status ),
}
if workerID . Valid {
notify . WorkerID = workerID . UUID . String ()
}
p . publishChatStreamNotify ( chatID , notify )
}
// publishChatStreamNotify broadcasts a per-chat stream notification via
// PostgreSQL pubsub so that all replicas can read updates from the database.
func ( p * Server ) publishChatStreamNotify ( chatID uuid . UUID , notify coderdpubsub . ChatStreamNotifyMessage ) {
if p . pubsub == nil {
return
}
payload , err := json . Marshal ( notify )
if err != nil {
p . logger . Error ( context . Background (), "failed to marshal chat stream notify" ,
slog . F ( "chat_id" , chatID ),
slog . Error ( err ),
)
return
}
if err := p . pubsub . Publish ( coderdpubsub . ChatStreamNotifyChannel ( chatID ), payload ); err != nil {
p . logger . Error ( context . Background (), "failed to publish chat stream notify" ,
slog . F ( "chat_id" , chatID ),
slog . Error ( err ),
)
}
}
// publishChatPubsubEvent broadcasts a chat lifecycle event via PostgreSQL
// pubsub so that all replicas can push updates to watching clients.
2026-03-16 08:41:32 -07:00
func ( p * Server ) publishChatPubsubEvent ( chat database . Chat , kind coderdpubsub . ChatEventKind , diffStatus * codersdk . ChatDiffStatus ) {
2026-02-27 11:50:56 -05:00
if p . pubsub == nil {
return
}
sdkChat := codersdk . Chat {
ID : chat . ID ,
OwnerID : chat . OwnerID ,
Title : chat . Title ,
Status : codersdk . ChatStatus ( chat . Status ),
CreatedAt : chat . CreatedAt ,
UpdatedAt : chat . UpdatedAt ,
}
if chat . ParentChatID . Valid {
parentChatID := chat . ParentChatID . UUID
sdkChat . ParentChatID = & parentChatID
}
if chat . RootChatID . Valid {
rootChatID := chat . RootChatID . UUID
sdkChat . RootChatID = & rootChatID
} else if ! chat . ParentChatID . Valid {
rootChatID := chat . ID
sdkChat . RootChatID = & rootChatID
}
if chat . WorkspaceID . Valid {
sdkChat . WorkspaceID = & chat . WorkspaceID . UUID
}
2026-03-16 08:41:32 -07:00
if diffStatus != nil {
sdkChat . DiffStatus = diffStatus
}
2026-02-27 11:50:56 -05:00
event := coderdpubsub . ChatEvent {
Kind : kind ,
Chat : sdkChat ,
}
payload , err := json . Marshal ( event )
if err != nil {
p . logger . Error ( context . Background (), "failed to marshal chat pubsub event" ,
slog . F ( "chat_id" , chat . ID ),
slog . Error ( err ),
)
return
}
if err := p . pubsub . Publish ( coderdpubsub . ChatEventChannel ( chat . OwnerID ), payload ); err != nil {
p . logger . Error ( context . Background (), "failed to publish chat pubsub event" ,
slog . F ( "chat_id" , chat . ID ),
slog . F ( "kind" , kind ),
slog . Error ( err ),
)
}
}
2026-02-27 18:06:54 -05:00
// PublishDiffStatusChange broadcasts a diff_status_change event for
// the given chat so that watching clients know to re-fetch the diff
// status. This is called from the HTTP layer after the diff status
// is updated in the database.
func ( p * Server ) PublishDiffStatusChange ( ctx context . Context , chatID uuid . UUID ) error {
if p . pubsub == nil {
return nil
}
chat , err := p . db . GetChatByID ( ctx , chatID )
if err != nil {
return xerrors . Errorf ( "get chat: %w" , err )
}
2026-03-16 08:41:32 -07:00
dbStatus , err := p . db . GetChatDiffStatusByChatID ( ctx , chatID )
if err != nil {
return xerrors . Errorf ( "get chat diff status: %w" , err )
}
sdkStatus := db2sdk . ChatDiffStatus ( chatID , & dbStatus )
p . publishChatPubsubEvent ( chat , coderdpubsub . ChatEventKindDiffStatusChange , & sdkStatus )
2026-02-27 18:06:54 -05:00
return nil
}
2026-02-27 11:50:56 -05:00
func ( p * Server ) publishError ( chatID uuid . UUID , message string ) {
message = strings . TrimSpace ( message )
if message == "" {
return
}
p . publishEvent ( chatID , codersdk . ChatStreamEvent {
Type : codersdk . ChatStreamEventTypeError ,
Error : & codersdk . ChatStreamError { Message : message },
})
p . publishChatStreamNotify ( chatID , coderdpubsub . ChatStreamNotifyMessage {
Error : message ,
})
}
func processingFailureReason ( err error ) ( string , bool ) {
if err == nil {
return "" , false
}
reason := strings . TrimSpace ( err . Error ())
if reason == "" {
return "" , false
}
return reason , true
}
func panicFailureReason ( recovered any ) string {
var reason string
switch typed := recovered .( type ) {
case string :
reason = strings . TrimSpace ( typed )
case error :
reason = strings . TrimSpace ( typed . Error ())
default :
reason = strings . TrimSpace ( fmt . Sprint ( typed ))
}
if reason == "" || reason == "<nil>" {
return "chat processing panicked"
}
return "chat processing panicked: " + reason
}
func ( p * Server ) publishMessage ( chatID uuid . UUID , message database . ChatMessage ) {
sdkMessage := db2sdk . ChatMessage ( message )
p . publishEvent ( chatID , codersdk . ChatStreamEvent {
Type : codersdk . ChatStreamEventTypeMessage ,
Message : & sdkMessage ,
})
p . publishChatStreamNotify ( chatID , coderdpubsub . ChatStreamNotifyMessage {
AfterMessageID : message . ID - 1 ,
})
}
2026-03-06 13:02:25 -08:00
// publishEditedMessage is like publishMessage but uses
// AfterMessageID=0 so remote subscribers re-fetch from the
// beginning, ensuring the edit is never silently dropped.
func ( p * Server ) publishEditedMessage ( chatID uuid . UUID , message database . ChatMessage ) {
sdkMessage := db2sdk . ChatMessage ( message )
p . publishEvent ( chatID , codersdk . ChatStreamEvent {
Type : codersdk . ChatStreamEventTypeMessage ,
Message : & sdkMessage ,
})
p . publishChatStreamNotify ( chatID , coderdpubsub . ChatStreamNotifyMessage {
2026-03-06 15:11:05 -08:00
FullRefresh : true ,
2026-03-06 13:02:25 -08:00
})
}
2026-03-13 17:53:26 +02:00
func ( p * Server ) publishMessagePart ( chatID uuid . UUID , role codersdk . ChatMessageRole , part codersdk . ChatMessagePart ) {
2026-02-27 11:50:56 -05:00
if part . Type == "" {
return
}
2026-03-13 17:53:26 +02:00
// Strip internal-only fields before client delivery.
// Mirrors db2sdk.chatMessageParts stripping for REST.
part . StripInternal ()
2026-02-27 11:50:56 -05:00
p . publishEvent ( chatID , codersdk . ChatStreamEvent {
Type : codersdk . ChatStreamEventTypeMessagePart ,
MessagePart : & codersdk . ChatStreamMessagePart {
Role : role ,
Part : part ,
},
})
}
func shouldCancelChatFromControlNotification (
notify coderdpubsub . ChatStreamNotifyMessage ,
workerID uuid . UUID ,
) bool {
status := database . ChatStatus ( strings . TrimSpace ( notify . Status ))
switch status {
case database . ChatStatusWaiting , database . ChatStatusPending , database . ChatStatusError :
return true
case database . ChatStatusRunning :
worker := strings . TrimSpace ( notify . WorkerID )
if worker == "" {
return false
}
notifyWorkerID , err := uuid . Parse ( worker )
if err != nil {
return false
}
return notifyWorkerID != workerID
default :
return false
}
}
func ( p * Server ) subscribeChatControl (
ctx context . Context ,
chatID uuid . UUID ,
cancel context . CancelCauseFunc ,
logger slog . Logger ,
) func () {
if p . pubsub == nil {
return nil
}
listener := func ( _ context . Context , message [] byte , err error ) {
if err != nil {
logger . Warn ( ctx , "chat control pubsub error" , slog . Error ( err ))
return
}
var notify coderdpubsub . ChatStreamNotifyMessage
if unmarshalErr := json . Unmarshal ( message , & notify ); unmarshalErr != nil {
logger . Warn ( ctx , "failed to unmarshal chat control notify" , slog . Error ( unmarshalErr ))
return
}
if shouldCancelChatFromControlNotification ( notify , p . workerID ) {
cancel ( chatloop . ErrInterrupted )
}
}
controlCancel , err := p . pubsub . SubscribeWithErr (
coderdpubsub . ChatStreamNotifyChannel ( chatID ),
listener ,
)
if err != nil {
logger . Warn ( ctx , "failed to subscribe to chat control notifications" , slog . Error ( err ))
return nil
}
return controlCancel
}
2026-03-06 21:05:26 +02:00
// chatFileResolver returns a FileResolver that fetches chat file
// content from the database by ID.
func ( p * Server ) chatFileResolver () chatprompt . FileResolver {
return func ( ctx context . Context , ids [] uuid . UUID ) ( map [ uuid . UUID ] chatprompt . FileData , error ) {
files , err := p . db . GetChatFilesByIDs ( ctx , ids )
if err != nil {
return nil , err
}
result := make ( map [ uuid . UUID ] chatprompt . FileData , len ( files ))
for _ , f := range files {
result [ f . ID ] = chatprompt . FileData {
Data : f . Data ,
MediaType : f . Mimetype ,
}
}
return result , nil
}
}
2026-02-27 11:50:56 -05:00
func ( p * Server ) processChat ( ctx context . Context , chat database . Chat ) {
logger := p . logger . With ( slog . F ( "chat_id" , chat . ID ))
logger . Info ( ctx , "processing chat request" )
chatCtx , cancel := context . WithCancelCause ( ctx )
defer cancel ( nil )
controlCancel := p . subscribeChatControl ( chatCtx , chat . ID , cancel , logger )
defer func () {
if controlCancel != nil {
controlCancel ()
}
}()
// Periodically update the heartbeat so other replicas know this
// worker is still alive. The goroutine stops when chatCtx is
// canceled (either by completion or interruption).
go func () {
ticker := time . NewTicker ( chatHeartbeatInterval )
defer ticker . Stop ()
for {
select {
case <- chatCtx . Done ():
return
case <- ticker . C :
rows , err := p . db . UpdateChatHeartbeat ( chatCtx , database . UpdateChatHeartbeatParams {
ID : chat . ID ,
WorkerID : p . workerID ,
})
if err != nil {
logger . Warn ( chatCtx , "failed to update chat heartbeat" , slog . Error ( err ))
continue
}
if rows == 0 {
cancel ( chatloop . ErrInterrupted )
return
}
}
}
}()
2026-03-03 16:27:59 -05:00
// Start buffering stream events BEFORE publishing the running
// status. This closes a race where a subscriber sees
// status=running but misses message_part events because
// buffering hasn't started yet — the subscriber gets an empty
// snapshot and publishToStream drops message_parts while
// buffering is false.
2026-03-11 11:00:08 -07:00
streamState := p . getOrCreateStreamState ( chat . ID )
streamState . mu . Lock ()
streamState . buffer = nil
streamState . buffering = true
streamState . mu . Unlock ()
2026-03-03 16:27:59 -05:00
defer func () {
2026-03-11 11:00:08 -07:00
streamState . mu . Lock ()
streamState . buffer = nil
streamState . buffering = false
p . cleanupStreamIfIdle ( chat . ID , streamState )
streamState . mu . Unlock ()
2026-03-03 16:27:59 -05:00
}()
2026-02-27 11:50:56 -05:00
p . publishStatus ( chat . ID , database . ChatStatusRunning , uuid . NullUUID {
UUID : p . workerID ,
Valid : true ,
})
2026-02-28 12:27:26 -05:00
// Determine the final status and last error to set when we're done.
2026-02-27 11:50:56 -05:00
status := database . ChatStatusWaiting
2026-03-05 04:08:17 -05:00
wasInterrupted := false
2026-02-28 12:27:26 -05:00
lastError := ""
2026-02-27 11:50:56 -05:00
remainingQueuedMessages := [] database . ChatQueuedMessage {}
shouldPublishQueueUpdate := false
2026-03-06 13:02:25 -08:00
var promotedMessage * database . ChatMessage
2026-02-27 11:50:56 -05:00
defer func () {
2026-02-27 15:25:40 -05:00
// Use a context that is not canceled by Close() so we can
// reliably update the chat status in the database during
// graceful shutdown.
cleanupCtx := context . WithoutCancel ( ctx )
2026-02-27 11:50:56 -05:00
// Handle panics gracefully.
if r := recover (); r != nil {
2026-02-27 15:25:40 -05:00
logger . Error ( cleanupCtx , "panic during chat processing" , slog . F ( "panic" , r ))
2026-02-28 12:27:26 -05:00
lastError = panicFailureReason ( r )
p . publishError ( chat . ID , lastError )
2026-02-27 11:50:56 -05:00
status = database . ChatStatusError
}
// Check for queued messages and auto-promote the next one.
// This must be done atomically with the status update to avoid
// races with the promote endpoint (which also sets status to
// pending). We use a transaction with FOR UPDATE to ensure we
// don't overwrite a status change made by another caller.
err := p . db . InTx ( func ( tx database . Store ) error {
// Re-read the chat status under lock — another caller
// (e.g. promote) may have already set it to pending.
2026-02-27 15:25:40 -05:00
latestChat , lockErr := tx . GetChatByIDForUpdate ( cleanupCtx , chat . ID )
2026-02-27 11:50:56 -05:00
if lockErr != nil {
return xerrors . Errorf ( "lock chat for release: %w" , lockErr )
}
2026-03-04 18:42:28 -05:00
// If another worker has already acquired this chat,
// bail out — we must not overwrite their running
// status or publish spurious events.
if latestChat . Status == database . ChatStatusRunning &&
latestChat . WorkerID . Valid &&
latestChat . WorkerID . UUID != p . workerID {
return errChatTakenByOtherWorker
}
2026-02-27 11:50:56 -05:00
// If someone else already set the chat to pending (e.g.
// the promote endpoint), don't overwrite it — just clear
// the worker and let the processor pick it back up.
2026-03-06 13:02:25 -08:00
if latestChat . Status == database . ChatStatusPending {
2026-02-27 11:50:56 -05:00
status = database . ChatStatusPending
} else if status == database . ChatStatusWaiting {
// Try to auto-promote the next queued message.
2026-02-27 15:25:40 -05:00
nextQueued , popErr := tx . PopNextQueuedMessage ( cleanupCtx , chat . ID )
2026-02-27 11:50:56 -05:00
if popErr == nil {
2026-02-27 15:25:40 -05:00
msg , insertErr := tx . InsertChatMessage ( cleanupCtx , database . InsertChatMessageParams {
2026-03-13 18:47:36 +02:00
ChatID : chat . ID ,
ModelConfigID : uuid . NullUUID { UUID : latestChat . LastModelConfigID , Valid : true },
Role : database . ChatMessageRoleUser ,
ContentVersion : chatprompt . CurrentContentVersion ,
2026-02-27 11:50:56 -05:00
Content : pqtype . NullRawMessage {
RawMessage : nextQueued . Content ,
Valid : len ( nextQueued . Content ) > 0 ,
},
2026-03-11 07:00:38 -07:00
CreatedBy : uuid . NullUUID { UUID : chat . OwnerID , Valid : chat . OwnerID != uuid . Nil },
2026-02-27 11:50:56 -05:00
Visibility : database . ChatMessageVisibilityBoth ,
InputTokens : sql . NullInt64 {},
OutputTokens : sql . NullInt64 {},
TotalTokens : sql . NullInt64 {},
ReasoningTokens : sql . NullInt64 {},
CacheCreationTokens : sql . NullInt64 {},
CacheReadTokens : sql . NullInt64 {},
ContextLimit : sql . NullInt64 {},
2026-03-13 18:30:49 +01:00
TotalCostMicros : sql . NullInt64 {},
2026-02-27 11:50:56 -05:00
Compressed : sql . NullBool {},
})
if insertErr != nil {
2026-02-27 15:25:40 -05:00
logger . Error ( cleanupCtx , "failed to promote queued message" ,
2026-02-27 11:50:56 -05:00
slog . F ( "queued_message_id" , nextQueued . ID ), slog . Error ( insertErr ))
} else {
status = database . ChatStatusPending
2026-03-06 13:02:25 -08:00
promotedMessage = & msg
2026-02-27 11:50:56 -05:00
2026-02-27 15:25:40 -05:00
remaining , qErr := tx . GetChatQueuedMessages ( cleanupCtx , chat . ID )
2026-02-27 11:50:56 -05:00
if qErr == nil {
remainingQueuedMessages = remaining
shouldPublishQueueUpdate = true
}
}
}
}
2026-02-27 15:25:40 -05:00
_ , updateErr := tx . UpdateChatStatus ( cleanupCtx , database . UpdateChatStatusParams {
2026-02-27 11:50:56 -05:00
ID : chat . ID ,
Status : status ,
WorkerID : uuid . NullUUID {},
StartedAt : sql . NullTime {},
HeartbeatAt : sql . NullTime {},
2026-02-28 12:27:26 -05:00
LastError : sql . NullString { String : lastError , Valid : lastError != "" },
2026-02-27 11:50:56 -05:00
})
return updateErr
}, nil )
2026-03-04 18:42:28 -05:00
if errors . Is ( err , errChatTakenByOtherWorker ) {
// Another worker owns this chat now — skip all
// post-TX side effects (status publish, pubsub,
// web push) to avoid overwriting their state.
return
}
2026-02-27 11:50:56 -05:00
if err != nil {
2026-02-27 15:25:40 -05:00
logger . Error ( cleanupCtx , "failed to release chat" , slog . Error ( err ))
2026-03-06 13:02:25 -08:00
return
2026-02-27 11:50:56 -05:00
}
2026-03-06 13:02:25 -08:00
if promotedMessage != nil {
p . publishMessage ( chat . ID , * promotedMessage )
}
if shouldPublishQueueUpdate {
2026-02-27 11:50:56 -05:00
p . publishEvent ( chat . ID , codersdk . ChatStreamEvent {
Type : codersdk . ChatStreamEventTypeQueueUpdate ,
QueuedMessages : db2sdk . ChatQueuedMessages ( remainingQueuedMessages ),
})
p . publishChatStreamNotify ( chat . ID , coderdpubsub . ChatStreamNotifyMessage {
QueueUpdate : true ,
})
}
p . publishStatus ( chat . ID , status , uuid . NullUUID {})
2026-02-27 15:06:36 -05:00
// Re-read the chat from the database to pick up any title
// changes made during processing (e.g. AI-generated titles
// from maybeGenerateChatTitle). The local `chat` variable
// is a value copy and won't reflect updates made in runChat.
2026-02-28 21:38:16 -05:00
if freshChat , readErr := p . db . GetChatByID ( cleanupCtx , chat . ID ); readErr == nil {
2026-02-27 15:06:36 -05:00
chat = freshChat
} else {
2026-02-28 21:38:16 -05:00
logger . Warn ( cleanupCtx , "failed to re-read chat for status event" ,
2026-02-27 15:06:36 -05:00
slog . F ( "chat_id" , chat . ID ), slog . Error ( readErr ))
}
2026-02-27 11:50:56 -05:00
chat . Status = status
2026-03-16 08:41:32 -07:00
p . publishChatPubsubEvent ( chat , coderdpubsub . ChatEventKindStatusChange , nil )
2026-02-28 23:40:17 -05:00
2026-03-10 15:14:15 +00:00
if ! wasInterrupted {
p . maybeSendPushNotification ( cleanupCtx , chat , status , lastError , logger )
2026-02-28 23:40:17 -05:00
}
2026-02-27 11:50:56 -05:00
}()
if err := p . runChat ( chatCtx , chat , logger ); err != nil {
if errors . Is ( err , chatloop . ErrInterrupted ) || errors . Is ( context . Cause ( chatCtx ), chatloop . ErrInterrupted ) {
logger . Info ( ctx , "chat interrupted" )
status = database . ChatStatusWaiting
2026-03-05 04:08:17 -05:00
wasInterrupted = true
2026-02-27 11:50:56 -05:00
return
}
2026-02-28 17:14:11 -05:00
if isShutdownCancellation ( ctx , chatCtx , err ) {
logger . Info ( ctx , "chat canceled during shutdown; returning to pending" )
status = database . ChatStatusPending
lastError = ""
return
}
2026-02-27 11:50:56 -05:00
logger . Error ( ctx , "failed to process chat" , slog . Error ( err ))
if reason , ok := processingFailureReason ( err ); ok {
2026-02-28 12:27:26 -05:00
lastError = reason
p . publishError ( chat . ID , lastError )
2026-02-27 11:50:56 -05:00
}
status = database . ChatStatusError
return
}
2026-03-03 11:34:08 -05:00
// If runChat completed successfully but the server context was
// canceled (e.g. during Close()), the chat should be returned
// to pending so another replica can pick it up. There is a
// race where the LLM stream finishes just as the server is
// shutting down — the HTTP response completes before context
// cancellation propagates, so runChat returns nil instead of
// a context.Canceled error. Without this check the chat would
// be marked "waiting" and never retried.
if ctx . Err () != nil {
logger . Info ( ctx , "chat completed during shutdown; returning to pending" )
status = database . ChatStatusPending
lastError = ""
return
}
2026-02-27 11:50:56 -05:00
}
2026-02-28 17:14:11 -05:00
func isShutdownCancellation (
serverCtx context . Context ,
chatCtx context . Context ,
err error ,
) bool {
if err == nil {
return false
}
// During Close(), the server context is canceled. In-flight chats should
// be returned to pending so another replica can retry them.
if serverCtx . Err () == nil {
return false
}
if errors . Is ( err , context . Canceled ) {
return true
}
return errors . Is ( context . Cause ( chatCtx ), context . Canceled )
}
2026-02-27 11:50:56 -05:00
func ( p * Server ) runChat (
ctx context . Context ,
chat database . Chat ,
logger slog . Logger ,
) error {
2026-03-11 15:07:46 -07:00
var (
model fantasy . LanguageModel
modelConfig database . ChatModelConfig
providerKeys chatprovider . ProviderAPIKeys
callConfig codersdk . ChatModelCallConfig
messages [] database . ChatMessage
)
2026-02-27 11:50:56 -05:00
2026-03-11 15:07:46 -07:00
var g errgroup . Group
g . Go ( func () error {
var err error
model , modelConfig , providerKeys , err = p . resolveChatModel ( ctx , chat )
if err != nil {
return err
2026-02-27 11:50:56 -05:00
}
2026-03-11 15:07:46 -07:00
if len ( modelConfig . Options ) > 0 {
if err := json . Unmarshal ( modelConfig . Options , & callConfig ); err != nil {
return xerrors . Errorf ( "parse model call config: %w" , err )
}
}
return nil
})
g . Go ( func () error {
var err error
messages , err = p . db . GetChatMessagesForPromptByChatID ( ctx , chat . ID )
if err != nil {
return xerrors . Errorf ( "get chat messages: %w" , err )
}
return nil
})
if err := g . Wait (); err != nil {
return err
2026-02-27 11:50:56 -05:00
}
2026-02-27 15:06:36 -05:00
// Fire title generation asynchronously so it doesn't block the
// chat response. It uses a detached context so it can finish
// even after the chat processing context is canceled.
2026-03-13 19:49:34 +01:00
// Snapshot model so the goroutine doesn't race with the
// model = cuModel reassignment below.
titleModel := model
2026-02-28 21:38:16 -05:00
p . inflight . Add ( 1 )
go func () {
defer p . inflight . Done ()
2026-03-13 19:49:34 +01:00
p . maybeGenerateChatTitle ( context . WithoutCancel ( ctx ), chat , messages , titleModel , providerKeys , logger )
2026-02-28 21:38:16 -05:00
}()
2026-02-27 11:50:56 -05:00
2026-03-11 14:33:15 -07:00
prompt , err := chatprompt . ConvertMessagesWithFiles ( ctx , messages , p . chatFileResolver (), logger )
2026-02-27 11:50:56 -05:00
if err != nil {
return xerrors . Errorf ( "build chat prompt: %w" , err )
}
if chat . ParentChatID . Valid {
prompt = chatprompt . InsertSystem ( prompt , defaultSubagentInstruction )
}
2026-03-13 19:49:34 +01:00
// Detect computer-use subagent via the mode column.
isComputerUse := chat . Mode . Valid && chat . Mode . ChatMode == database . ChatModeComputerUse
2026-03-03 16:27:59 -05:00
// NOTE: Buffering was already started in processChat before
// the running status was published, so message_part events
// are captured from the moment subscribers can see
// status=running. The deferred cleanup also lives in
// processChat.
2026-02-27 11:50:56 -05:00
currentChat := chat
2026-02-28 19:07:17 -05:00
loadChatSnapshot := func (
loadCtx context . Context ,
chatID uuid . UUID ,
) ( database . Chat , error ) {
2026-03-02 15:57:04 +00:00
return p . db . GetChatByID ( loadCtx , chatID )
2026-02-28 19:07:17 -05:00
}
2026-02-27 11:50:56 -05:00
var (
chatStateMu sync . Mutex
workspaceMu sync . Mutex
conn workspacesdk . AgentConn
releaseConn func ()
)
closeConn := func () {
if releaseConn != nil {
releaseConn ()
releaseConn = nil
}
}
defer closeConn ()
getWorkspaceConn := func ( ctx context . Context ) ( workspacesdk . AgentConn , error ) {
chatStateMu . Lock ()
if conn != nil {
currentConn := conn
chatStateMu . Unlock ()
return currentConn , nil
}
chatSnapshot := currentChat
chatStateMu . Unlock ()
if p . agentConnFn == nil {
return nil , xerrors . New ( "workspace agent connector is not configured" )
}
2026-02-28 19:07:17 -05:00
if ! chatSnapshot . WorkspaceID . Valid {
refreshedChat , refreshErr := refreshChatWorkspaceSnapshot (
ctx ,
chatSnapshot ,
loadChatSnapshot ,
)
if refreshErr != nil {
return nil , refreshErr
}
if refreshedChat . WorkspaceID . Valid {
chatStateMu . Lock ()
currentChat = refreshedChat
chatSnapshot = refreshedChat
chatStateMu . Unlock ()
}
}
2026-02-28 16:46:51 -05:00
if ! chatSnapshot . WorkspaceID . Valid {
return nil , xerrors . New ( "chat has no workspace" )
2026-02-27 11:50:56 -05:00
}
2026-02-28 16:46:51 -05:00
agents , err := p . db . GetWorkspaceAgentsInLatestBuildByWorkspaceID (
2026-03-02 15:57:04 +00:00
ctx ,
2026-02-28 16:46:51 -05:00
chatSnapshot . WorkspaceID . UUID ,
)
if err != nil || len ( agents ) == 0 {
2026-02-27 11:50:56 -05:00
return nil , xerrors . New ( "chat has no workspace agent" )
}
2026-02-28 16:46:51 -05:00
agentConn , agentRelease , err := p . agentConnFn ( ctx , agents [ 0 ]. ID )
2026-02-27 11:50:56 -05:00
if err != nil {
return nil , xerrors . Errorf ( "connect to workspace agent: %w" , err )
}
chatStateMu . Lock ()
if conn == nil {
conn = agentConn
releaseConn = agentRelease
2026-03-06 10:47:55 +01:00
var ancestorIDs [] string
if chatSnapshot . ParentChatID . Valid {
ancestorIDs = append ( ancestorIDs , chatSnapshot . ParentChatID . UUID . String ())
}
ancestorJSON , err := json . Marshal ( ancestorIDs )
if err != nil {
logger . Warn ( ctx , "failed to marshal ancestor chat IDs" , slog . Error ( err ))
ancestorJSON = [] byte ( "[]" )
}
agentConn . SetExtraHeaders ( http . Header {
workspacesdk . CoderChatIDHeader : { chatSnapshot . ID . String ()},
workspacesdk . CoderAncestorChatIDsHeader : { string ( ancestorJSON )},
})
2026-03-06 13:02:25 -08:00
chatStateMu . Unlock ()
2026-02-27 11:50:56 -05:00
return agentConn , nil
}
currentConn := conn
chatStateMu . Unlock ()
agentRelease ()
return currentConn , nil
}
2026-03-11 15:07:46 -07:00
var instruction , resolvedUserPrompt string
var g2 errgroup . Group
g2 . Go ( func () error {
instruction = p . resolveInstructions ( ctx , chat , getWorkspaceConn )
return nil
})
g2 . Go ( func () error {
resolvedUserPrompt = p . resolveUserPrompt ( ctx , chat . OwnerID )
return nil
})
_ = g2 . Wait ()
if instruction != "" {
2026-02-27 14:21:23 -05:00
prompt = chatprompt . InsertSystem ( prompt , instruction )
}
2026-03-11 15:07:46 -07:00
if resolvedUserPrompt != "" {
prompt = chatprompt . InsertSystem ( prompt , resolvedUserPrompt )
2026-03-10 08:17:52 -07:00
}
2026-02-27 11:50:56 -05:00
2026-03-10 08:17:52 -07:00
// Use the model config's context_limit as a fallback when the LLM // provider doesn't include context_limit in its response metadata
2026-02-27 11:50:56 -05:00
// (which is the common case).
modelConfigContextLimit := modelConfig . ContextLimit
persistStep := func ( persistCtx context . Context , step chatloop . PersistedStep ) error {
2026-03-10 06:01:45 -07:00
// If the chat context has been canceled, bail out before
// inserting any messages. We distinguish the cause so that
// the caller can tell an intentional interruption (e.g.
// EditMessage, user stop) from a server shutdown:
// - ErrInterrupted cause → return ErrInterrupted
// (processChat sets status = waiting).
// - Any other cause (e.g. context.Canceled during
// Close()) → return the original context error so
// isShutdownCancellation can match and set status =
// pending, allowing another replica to retry.
2026-03-06 13:02:25 -08:00
if persistCtx . Err () != nil {
2026-03-10 06:01:45 -07:00
if errors . Is ( context . Cause ( persistCtx ), chatloop . ErrInterrupted ) {
return chatloop . ErrInterrupted
}
return persistCtx . Err ()
2026-03-06 13:02:25 -08:00
}
2026-02-27 11:50:56 -05:00
// Split the step content into assistant blocks and tool
// result blocks so they can be stored as separate messages
2026-03-12 06:49:53 -07:00
// with the appropriate roles. Provider-executed tool results
// (e.g. web_search) stay in the assistant content because
// the LLM provider expects them inline in the assistant
// turn, not as separate tool messages.
2026-02-27 11:50:56 -05:00
var assistantBlocks [] fantasy . Content
var toolResults [] fantasy . ToolResultContent
for _ , block := range step . Content {
if tr , ok := fantasy . AsContentType [ fantasy . ToolResultContent ]( block ); ok {
2026-03-12 06:49:53 -07:00
if ! tr . ProviderExecuted {
toolResults = append ( toolResults , tr )
continue
}
2026-02-27 11:50:56 -05:00
}
if trPtr , ok := fantasy . AsContentType [ * fantasy . ToolResultContent ]( block ); ok && trPtr != nil {
2026-03-12 06:49:53 -07:00
if ! trPtr . ProviderExecuted {
toolResults = append ( toolResults , * trPtr )
continue
}
2026-02-27 11:50:56 -05:00
}
assistantBlocks = append ( assistantBlocks , block )
}
2026-03-06 13:02:25 -08:00
var insertedMessages [] database . ChatMessage
err := p . db . InTx ( func ( tx database . Store ) error {
2026-03-06 15:11:05 -08:00
// Verify this worker still owns the chat before
// inserting messages. This closes the race where
// EditMessage truncates history and clears worker_id
// while persistInterruptedStep (which uses an
// uncancelable context) is still running.
lockedChat , lockErr := tx . GetChatByIDForUpdate ( persistCtx , chat . ID )
if lockErr != nil {
return xerrors . Errorf ( "lock chat for persist: %w" , lockErr )
}
if ! lockedChat . WorkerID . Valid || lockedChat . WorkerID . UUID != p . workerID {
return chatloop . ErrInterrupted
}
2026-03-06 13:02:25 -08:00
if len ( assistantBlocks ) > 0 {
2026-03-13 17:53:26 +02:00
sdkParts := make ([] codersdk . ChatMessagePart , 0 , len ( assistantBlocks ))
for _ , block := range assistantBlocks {
sdkParts = append ( sdkParts , chatprompt . PartFromContent ( block ))
}
assistantContent , marshalErr := chatprompt . MarshalParts ( sdkParts )
2026-03-06 13:02:25 -08:00
if marshalErr != nil {
return marshalErr
}
hasUsage := step . Usage != ( fantasy . Usage {})
2026-03-13 18:30:49 +01:00
var usageForCost codersdk . ChatMessageUsage
if hasUsage {
// Only populate fields that the provider explicitly
// reported. Nil fields tell the calculator "no data"
// vs zero meaning "reported as zero tokens."
if step . Usage . InputTokens != 0 {
usageForCost . InputTokens = int64Ptr ( step . Usage . InputTokens )
}
if step . Usage . OutputTokens != 0 {
usageForCost . OutputTokens = int64Ptr ( step . Usage . OutputTokens )
}
if step . Usage . ReasoningTokens != 0 {
usageForCost . ReasoningTokens = int64Ptr ( step . Usage . ReasoningTokens )
}
if step . Usage . CacheCreationTokens != 0 {
usageForCost . CacheCreationTokens = int64Ptr ( step . Usage . CacheCreationTokens )
}
if step . Usage . CacheReadTokens != 0 {
usageForCost . CacheReadTokens = int64Ptr ( step . Usage . CacheReadTokens )
}
}
totalCostMicros := chatcost . CalculateTotalCostMicros ( usageForCost , callConfig . Cost )
2026-03-06 13:02:25 -08:00
assistantMessage , insertErr := tx . InsertChatMessage ( persistCtx , database . InsertChatMessageParams {
2026-03-13 18:47:36 +02:00
ChatID : chat . ID ,
CreatedBy : uuid . NullUUID {},
ModelConfigID : uuid . NullUUID { UUID : modelConfig . ID , Valid : true },
Role : database . ChatMessageRoleAssistant ,
ContentVersion : chatprompt . CurrentContentVersion ,
Content : assistantContent ,
Visibility : database . ChatMessageVisibilityBoth ,
InputTokens : usageNullInt64 ( step . Usage . InputTokens , hasUsage ),
OutputTokens : usageNullInt64 ( step . Usage . OutputTokens , hasUsage ),
TotalTokens : usageNullInt64 ( step . Usage . TotalTokens , hasUsage ),
2026-03-06 13:02:25 -08:00
ReasoningTokens : usageNullInt64 (
step . Usage . ReasoningTokens ,
hasUsage ,
),
CacheCreationTokens : usageNullInt64 (
step . Usage . CacheCreationTokens ,
hasUsage ,
),
CacheReadTokens : usageNullInt64 ( step . Usage . CacheReadTokens , hasUsage ),
ContextLimit : step . ContextLimit ,
Compressed : sql . NullBool {},
2026-03-13 18:30:49 +01:00
// TotalCostMicros is nullable: NULL means "unpriced"
// (pricing config was missing or no priced token
// breakdown available), while 0 means "priced at
// zero cost" (e.g., a free model).
TotalCostMicros : usageNullInt64Ptr ( totalCostMicros ),
2026-03-06 13:02:25 -08:00
})
if insertErr != nil {
return xerrors . Errorf ( "insert assistant message: %w" , insertErr )
}
insertedMessages = append ( insertedMessages , assistantMessage )
2026-02-27 11:50:56 -05:00
}
2026-03-06 13:02:25 -08:00
for _ , tr := range toolResults {
2026-03-13 17:53:26 +02:00
trPart := chatprompt . PartFromContent ( tr )
resultContent , marshalErr := chatprompt . MarshalParts ([] codersdk . ChatMessagePart { trPart })
2026-03-06 13:02:25 -08:00
if marshalErr != nil {
return marshalErr
}
toolMessage , insertErr := tx . InsertChatMessage ( persistCtx , database . InsertChatMessageParams {
ChatID : chat . ID ,
2026-03-11 07:00:38 -07:00
CreatedBy : uuid . NullUUID {},
2026-03-06 13:02:25 -08:00
ModelConfigID : uuid . NullUUID { UUID : modelConfig . ID , Valid : true },
2026-03-13 18:47:36 +02:00
Role : database . ChatMessageRoleTool ,
ContentVersion : chatprompt . CurrentContentVersion ,
2026-03-06 13:02:25 -08:00
Content : resultContent ,
Visibility : database . ChatMessageVisibilityBoth ,
InputTokens : sql . NullInt64 {},
OutputTokens : sql . NullInt64 {},
TotalTokens : sql . NullInt64 {},
ReasoningTokens : sql . NullInt64 {},
CacheCreationTokens : sql . NullInt64 {},
CacheReadTokens : sql . NullInt64 {},
ContextLimit : sql . NullInt64 {},
2026-03-13 18:30:49 +01:00
TotalCostMicros : sql . NullInt64 {},
2026-03-06 13:02:25 -08:00
Compressed : sql . NullBool {},
})
if insertErr != nil {
return xerrors . Errorf ( "insert tool result: %w" , insertErr )
}
insertedMessages = append ( insertedMessages , toolMessage )
2026-02-27 11:50:56 -05:00
}
2026-03-06 13:02:25 -08:00
return nil
}, nil )
if err != nil {
return xerrors . Errorf ( "persist step transaction: %w" , err )
2026-02-27 11:50:56 -05:00
}
2026-03-06 13:02:25 -08:00
for _ , msg := range insertedMessages {
p . publishMessage ( chat . ID , msg )
2026-02-27 11:50:56 -05:00
}
2026-02-28 16:51:04 -05:00
// Clear the stream buffer now that the step is
// persisted. Late-joining subscribers will load
// these messages from the database instead.
2026-03-11 11:00:08 -07:00
if val , ok := p . chatStreams . Load ( chat . ID ); ok {
if ss , ok := val .( * chatStreamState ); ok {
ss . mu . Lock ()
ss . buffer = nil
ss . mu . Unlock ()
}
2026-02-28 16:51:04 -05:00
}
2026-02-27 11:50:56 -05:00
return nil
}
2026-03-02 18:51:57 -05:00
// Apply the default MaxOutputTokens if the model config
// does not specify one.
if callConfig . MaxOutputTokens == nil {
2026-02-27 11:50:56 -05:00
maxOutputTokens := int64 ( 32_000 )
2026-03-02 18:51:57 -05:00
callConfig . MaxOutputTokens = & maxOutputTokens
2026-02-27 11:50:56 -05:00
}
2026-03-02 18:51:57 -05:00
// Generate the tool call ID up front so that the streaming
// parts and durable messages share the same identifier.
// Without this the client cannot correlate the
// "Summarizing..." tool call with the "Summarized" tool
// result.
2026-02-27 16:33:39 -05:00
compactionToolCallID := "chat_summarized_" + uuid . NewString ()
2026-02-27 11:50:56 -05:00
compactionOptions := & chatloop . CompactionOptions {
ThresholdPercent : modelConfig . CompressionThreshold ,
ContextLimit : modelConfig . ContextLimit ,
Persist : func (
persistCtx context . Context ,
result chatloop . CompactionResult ,
) error {
if err := p . persistChatContextSummary (
persistCtx ,
chat . ID ,
modelConfig . ID ,
2026-02-27 16:33:39 -05:00
compactionToolCallID ,
2026-02-27 11:50:56 -05:00
result ,
); err != nil {
return xerrors . Errorf ( "persist context summary: %w" , err )
}
logger . Info ( persistCtx , "chat context summarized" ,
slog . F ( "chat_id" , chat . ID ),
slog . F ( "threshold_percent" , result . ThresholdPercent ),
slog . F ( "usage_percent" , result . UsagePercent ),
slog . F ( "context_tokens" , result . ContextTokens ),
slog . F ( "context_limit" , result . ContextLimit ),
)
return nil
},
2026-03-02 18:51:57 -05:00
ToolCallID : compactionToolCallID ,
ToolName : "chat_summarized" ,
2026-03-13 17:53:26 +02:00
PublishMessagePart : func ( role codersdk . ChatMessageRole , part codersdk . ChatMessagePart ) {
p . publishMessagePart ( chat . ID , role , part )
2026-02-27 16:33:39 -05:00
},
2026-02-27 11:50:56 -05:00
OnError : func ( err error ) {
logger . Warn ( ctx , "failed to compact chat context" , slog . Error ( err ))
},
}
2026-03-13 19:49:34 +01:00
if isComputerUse {
// Override model for computer use subagent.
cuModel , cuErr := chatprovider . ModelFromConfig (
chattool . ComputerUseModelProvider ,
chattool . ComputerUseModelName ,
providerKeys ,
chatprovider . UserAgent (),
)
if cuErr != nil {
return xerrors . Errorf ( "resolve computer use model: %w" , cuErr )
}
model = cuModel
}
2026-02-27 11:50:56 -05:00
// Here are all the tools we have for the chat.
tools := [] fantasy . AgentTool {
chattool . ReadFile ( chattool . ReadFileOptions {
GetWorkspaceConn : getWorkspaceConn ,
}),
chattool . WriteFile ( chattool . WriteFileOptions {
GetWorkspaceConn : getWorkspaceConn ,
}),
chattool . EditFiles ( chattool . EditFilesOptions {
GetWorkspaceConn : getWorkspaceConn ,
}),
chattool . Execute ( chattool . ExecuteOptions {
GetWorkspaceConn : getWorkspaceConn ,
}),
2026-02-28 12:33:52 -05:00
chattool . ProcessOutput ( chattool . ProcessToolOptions {
GetWorkspaceConn : getWorkspaceConn ,
}),
chattool . ProcessList ( chattool . ProcessToolOptions {
GetWorkspaceConn : getWorkspaceConn ,
}),
chattool . ProcessSignal ( chattool . ProcessToolOptions {
GetWorkspaceConn : getWorkspaceConn ,
}),
2026-02-27 11:50:56 -05:00
}
2026-03-04 10:49:14 -05:00
// Only root chats (not delegated subagents) get workspace
// provisioning and subagent tools. Child agents must not
// create workspaces or spawn further subagents — they should
2026-02-28 16:30:25 -05:00
// focus on completing their delegated task.
if ! chat . ParentChatID . Valid {
2026-03-04 10:49:14 -05:00
tools = append ( tools ,
chattool . ListTemplates ( chattool . ListTemplatesOptions {
DB : p . db ,
OwnerID : chat . OwnerID ,
}),
chattool . ReadTemplate ( chattool . ReadTemplateOptions {
DB : p . db ,
OwnerID : chat . OwnerID ,
}),
chattool . CreateWorkspace ( chattool . CreateWorkspaceOptions {
DB : p . db ,
OwnerID : chat . OwnerID ,
ChatID : chat . ID ,
CreateFn : p . createWorkspaceFn ,
AgentConnFn : chattool . AgentConnFunc ( p . agentConnFn ),
WorkspaceMu : & workspaceMu ,
2026-03-13 19:53:20 +02:00
Logger : p . logger ,
2026-03-04 10:49:14 -05:00
}),
2026-03-05 10:34:24 -05:00
chattool . StartWorkspace ( chattool . StartWorkspaceOptions {
DB : p . db ,
OwnerID : chat . OwnerID ,
ChatID : chat . ID ,
StartFn : p . startWorkspaceFn ,
AgentConnFn : chattool . AgentConnFunc ( p . agentConnFn ),
WorkspaceMu : & workspaceMu ,
}),
2026-03-04 10:49:14 -05:00
)
2026-03-13 19:49:34 +01:00
tools = append ( tools , p . subagentTools ( ctx , func () database . Chat {
2026-02-28 16:30:25 -05:00
return chat
}) ... )
}
2026-02-27 11:50:56 -05:00
2026-03-11 14:33:15 -07:00
// Build provider-native tools (e.g., web search) based on
// the model configuration.
2026-03-13 19:49:34 +01:00
var providerTools [] chatloop . ProviderTool
2026-03-11 14:33:15 -07:00
if callConfig . ProviderOptions != nil {
providerTools = buildProviderTools ( model . Provider (), callConfig . ProviderOptions )
}
2026-03-13 19:49:34 +01:00
if isComputerUse {
providerTools = append ( providerTools , chatloop . ProviderTool {
Definition : chattool . ComputerUseProviderTool (
workspacesdk . DesktopDisplayWidth ,
workspacesdk . DesktopDisplayHeight ),
Runner : chattool . NewComputerUseTool (
workspacesdk . DesktopDisplayWidth ,
workspacesdk . DesktopDisplayHeight ,
getWorkspaceConn , quartz . NewReal (),
),
})
}
2026-03-02 18:51:57 -05:00
err = chatloop . Run ( ctx , chatloop . RunOptions {
Model : model ,
Messages : prompt ,
2026-03-13 19:49:34 +01:00
Tools : tools , MaxSteps : maxChatSteps ,
2026-03-02 18:51:57 -05:00
ModelConfig : callConfig ,
ProviderOptions : chatprovider . ProviderOptionsFromChatModelConfig ( model , callConfig . ProviderOptions ),
2026-03-11 14:33:15 -07:00
ProviderTools : providerTools ,
2026-02-27 11:50:56 -05:00
ContextLimitFallback : modelConfigContextLimit ,
PersistStep : persistStep ,
PublishMessagePart : func (
2026-03-13 17:53:26 +02:00
role codersdk . ChatMessageRole ,
2026-02-27 11:50:56 -05:00
part codersdk . ChatMessagePart ,
) {
2026-03-13 17:53:26 +02:00
p . publishMessagePart ( chat . ID , role , part )
2026-02-27 11:50:56 -05:00
},
Compaction : compactionOptions ,
2026-03-04 22:28:23 -05:00
ReloadMessages : func ( reloadCtx context . Context ) ([] fantasy . Message , error ) {
reloadedMsgs , err := p . db . GetChatMessagesForPromptByChatID ( reloadCtx , chat . ID )
if err != nil {
return nil , xerrors . Errorf ( "reload chat messages: %w" , err )
}
2026-03-11 14:33:15 -07:00
reloadedPrompt , err := chatprompt . ConvertMessagesWithFiles ( reloadCtx , reloadedMsgs , p . chatFileResolver (), logger )
2026-03-04 22:28:23 -05:00
if err != nil {
return nil , xerrors . Errorf ( "convert reloaded messages: %w" , err )
}
if chat . ParentChatID . Valid {
reloadedPrompt = chatprompt . InsertSystem ( reloadedPrompt , defaultSubagentInstruction )
}
2026-03-11 15:07:46 -07:00
var reloadInstruction , reloadUserPrompt string
var rg errgroup . Group
rg . Go ( func () error {
reloadInstruction = p . resolveInstructions ( reloadCtx , chat , getWorkspaceConn )
return nil
})
rg . Go ( func () error {
reloadUserPrompt = p . resolveUserPrompt ( reloadCtx , chat . OwnerID )
return nil
})
_ = rg . Wait ()
if reloadInstruction != "" {
reloadedPrompt = chatprompt . InsertSystem ( reloadedPrompt , reloadInstruction )
2026-03-04 22:28:23 -05:00
}
2026-03-11 15:07:46 -07:00
if reloadUserPrompt != "" {
reloadedPrompt = chatprompt . InsertSystem ( reloadedPrompt , reloadUserPrompt )
2026-03-10 08:17:52 -07:00
}
2026-03-04 22:28:23 -05:00
return reloadedPrompt , nil
},
2026-02-27 11:50:56 -05:00
2026-02-27 18:34:33 -05:00
OnRetry : func ( attempt int , retryErr error , delay time . Duration ) {
2026-03-11 11:00:08 -07:00
if val , ok := p . chatStreams . Load ( chat . ID ); ok {
if rs , ok := val .( * chatStreamState ); ok {
rs . mu . Lock ()
rs . buffer = nil
rs . mu . Unlock ()
}
2026-03-06 13:02:25 -08:00
}
2026-02-27 18:34:33 -05:00
logger . Warn ( ctx , "retrying LLM stream" ,
slog . F ( "attempt" , attempt ),
slog . F ( "delay" , delay . String ()),
slog . Error ( retryErr ),
)
p . publishEvent ( chat . ID , codersdk . ChatStreamEvent {
Type : codersdk . ChatStreamEventTypeRetry ,
ChatID : chat . ID ,
Retry : & codersdk . ChatStreamRetry {
Attempt : attempt ,
DelayMs : delay . Milliseconds (),
Error : retryErr . Error (),
RetryingAt : time . Now (). Add ( delay ),
},
})
},
2026-02-27 11:50:56 -05:00
OnInterruptedPersistError : func ( err error ) {
p . logger . Warn ( ctx , "failed to persist interrupted chat step" , slog . Error ( err ))
},
})
return err
}
2026-03-11 14:33:15 -07:00
// buildProviderTools creates provider-native tool definitions
// (like web search) based on the model configuration. These
// tools are executed server-side by the LLM provider.
2026-03-13 19:49:34 +01:00
func buildProviderTools ( _ string , options * codersdk . ChatModelProviderOptions ) [] chatloop . ProviderTool {
var tools [] chatloop . ProviderTool
2026-03-11 14:33:15 -07:00
if options . Anthropic != nil && options . Anthropic . WebSearchEnabled != nil && * options . Anthropic . WebSearchEnabled {
2026-03-13 19:49:34 +01:00
tools = append ( tools , chatloop . ProviderTool {
Definition : anthropic . WebSearchTool ( & anthropic . WebSearchToolOptions {
AllowedDomains : options . Anthropic . AllowedDomains ,
BlockedDomains : options . Anthropic . BlockedDomains ,
}),
})
2026-03-11 14:33:15 -07:00
}
if options . OpenAI != nil && options . OpenAI . WebSearchEnabled != nil && * options . OpenAI . WebSearchEnabled {
args := map [ string ] any {}
if options . OpenAI . SearchContextSize != nil && * options . OpenAI . SearchContextSize != "" {
args [ "search_context_size" ] = * options . OpenAI . SearchContextSize
}
if len ( options . OpenAI . AllowedDomains ) > 0 {
args [ "allowed_domains" ] = options . OpenAI . AllowedDomains
}
2026-03-13 19:49:34 +01:00
tools = append ( tools , chatloop . ProviderTool {
Definition : fantasy . ProviderDefinedTool {
ID : "web_search" ,
Name : "web_search" ,
Args : args ,
},
2026-03-11 14:33:15 -07:00
})
}
if options . Google != nil && options . Google . WebSearchEnabled != nil && * options . Google . WebSearchEnabled {
2026-03-13 19:49:34 +01:00
tools = append ( tools , chatloop . ProviderTool {
Definition : fantasy . ProviderDefinedTool {
ID : "web_search" ,
Name : "web_search" ,
},
2026-03-11 14:33:15 -07:00
})
}
return tools
}
2026-02-27 11:50:56 -05:00
// persistChatContextSummary persists a chat context summary to the database.
// This is invoked via the chat loop's compaction callback.
func ( p * Server ) persistChatContextSummary (
ctx context . Context ,
chatID uuid . UUID ,
modelConfigID uuid . UUID ,
2026-02-27 16:33:39 -05:00
toolCallID string ,
2026-02-27 11:50:56 -05:00
result chatloop . CompactionResult ,
) error {
if strings . TrimSpace ( result . SystemSummary ) == "" ||
strings . TrimSpace ( result . SummaryReport ) == "" {
return nil
}
2026-03-13 18:47:36 +02:00
systemContent , err := chatprompt . MarshalParts ([] codersdk . ChatMessagePart {
codersdk . ChatMessageText ( result . SystemSummary ),
})
2026-02-27 11:50:56 -05:00
if err != nil {
return xerrors . Errorf ( "encode system summary: %w" , err )
}
args , err := json . Marshal ( map [ string ] any {
"source" : "automatic" ,
"threshold_percent" : result . ThresholdPercent ,
})
if err != nil {
return xerrors . Errorf ( "encode summary tool args: %w" , err )
}
2026-03-13 17:53:26 +02:00
assistantContent , err := chatprompt . MarshalParts ([] codersdk . ChatMessagePart {
codersdk . ChatMessageToolCall ( toolCallID , "chat_summarized" , args ),
})
2026-02-27 11:50:56 -05:00
if err != nil {
return xerrors . Errorf ( "encode summary tool call: %w" , err )
}
2026-03-06 13:02:25 -08:00
summaryResult , err := json . Marshal ( map [ string ] any {
2026-02-27 11:50:56 -05:00
"summary" : result . SummaryReport ,
"source" : "automatic" ,
"threshold_percent" : result . ThresholdPercent ,
"usage_percent" : result . UsagePercent ,
"context_tokens" : result . ContextTokens ,
"context_limit_tokens" : result . ContextLimit ,
})
2026-03-06 13:02:25 -08:00
if err != nil {
return xerrors . Errorf ( "encode summary result payload: %w" , err )
2026-02-27 11:50:56 -05:00
}
2026-03-13 17:53:26 +02:00
toolResult , err := chatprompt . MarshalParts ([] codersdk . ChatMessagePart {
codersdk . ChatMessageToolResult ( toolCallID , "chat_summarized" , summaryResult , false ),
})
2026-02-27 11:50:56 -05:00
if err != nil {
return xerrors . Errorf ( "encode summary tool result: %w" , err )
}
2026-03-06 13:02:25 -08:00
var insertedMessages [] database . ChatMessage
txErr := p . db . InTx ( func ( tx database . Store ) error {
_ , txErr := tx . InsertChatMessage ( ctx , database . InsertChatMessageParams {
2026-03-13 18:47:36 +02:00
ChatID : chatID ,
CreatedBy : uuid . NullUUID {},
ModelConfigID : uuid . NullUUID { UUID : modelConfigID , Valid : true },
Role : database . ChatMessageRoleUser ,
ContentVersion : chatprompt . CurrentContentVersion ,
Content : systemContent ,
2026-03-06 13:02:25 -08:00
Visibility : database . ChatMessageVisibilityModel ,
Compressed : sql . NullBool { Bool : true , Valid : true },
InputTokens : sql . NullInt64 {},
OutputTokens : sql . NullInt64 {},
TotalTokens : sql . NullInt64 {},
ReasoningTokens : sql . NullInt64 {},
CacheCreationTokens : sql . NullInt64 {},
CacheReadTokens : sql . NullInt64 {},
ContextLimit : sql . NullInt64 {},
2026-03-13 18:30:49 +01:00
TotalCostMicros : sql . NullInt64 {},
2026-03-06 13:02:25 -08:00
})
if txErr != nil {
return xerrors . Errorf ( "insert hidden summary message: %w" , txErr )
}
assistantMessage , txErr := tx . InsertChatMessage ( ctx , database . InsertChatMessageParams {
2026-03-13 18:47:36 +02:00
ChatID : chatID ,
CreatedBy : uuid . NullUUID {},
ModelConfigID : uuid . NullUUID { UUID : modelConfigID , Valid : true },
Role : database . ChatMessageRoleAssistant ,
ContentVersion : chatprompt . CurrentContentVersion ,
Content : assistantContent ,
Visibility : database . ChatMessageVisibilityUser ,
2026-03-06 13:02:25 -08:00
Compressed : sql . NullBool {
Bool : true ,
Valid : true ,
},
InputTokens : sql . NullInt64 {},
OutputTokens : sql . NullInt64 {},
TotalTokens : sql . NullInt64 {},
ReasoningTokens : sql . NullInt64 {},
CacheCreationTokens : sql . NullInt64 {},
CacheReadTokens : sql . NullInt64 {},
ContextLimit : sql . NullInt64 {},
2026-03-13 18:30:49 +01:00
TotalCostMicros : sql . NullInt64 {},
2026-03-06 13:02:25 -08:00
})
if txErr != nil {
return xerrors . Errorf ( "insert summary tool call message: %w" , txErr )
}
insertedMessages = append ( insertedMessages , assistantMessage )
toolMessage , txErr := tx . InsertChatMessage ( ctx , database . InsertChatMessageParams {
2026-03-13 18:47:36 +02:00
ChatID : chatID ,
CreatedBy : uuid . NullUUID {},
ModelConfigID : uuid . NullUUID { UUID : modelConfigID , Valid : true },
Role : database . ChatMessageRoleTool ,
ContentVersion : chatprompt . CurrentContentVersion ,
Content : toolResult ,
Visibility : database . ChatMessageVisibilityBoth ,
2026-03-06 13:02:25 -08:00
Compressed : sql . NullBool {
Bool : true ,
Valid : true ,
},
InputTokens : sql . NullInt64 {},
OutputTokens : sql . NullInt64 {},
TotalTokens : sql . NullInt64 {},
ReasoningTokens : sql . NullInt64 {},
CacheCreationTokens : sql . NullInt64 {},
CacheReadTokens : sql . NullInt64 {},
ContextLimit : sql . NullInt64 {},
2026-03-13 18:30:49 +01:00
TotalCostMicros : sql . NullInt64 {},
2026-03-06 13:02:25 -08:00
})
if txErr != nil {
return xerrors . Errorf ( "insert summary tool result message: %w" , txErr )
}
insertedMessages = append ( insertedMessages , toolMessage )
return nil
}, nil )
if txErr != nil {
return txErr
2026-02-27 11:50:56 -05:00
}
2026-03-06 13:02:25 -08:00
// Publish after transaction commits to avoid notifying
// subscribers about messages that could be rolled back.
for _ , msg := range insertedMessages {
p . publishMessage ( chatID , msg )
}
2026-02-27 11:50:56 -05:00
return nil
}
func ( p * Server ) resolveChatModel (
ctx context . Context ,
chat database . Chat ,
2026-03-04 11:03:03 -05:00
) ( fantasy . LanguageModel , database . ChatModelConfig , chatprovider . ProviderAPIKeys , error ) {
2026-03-11 15:07:46 -07:00
var (
dbConfig database . ChatModelConfig
providers [] database . ChatProvider
)
2026-02-27 11:50:56 -05:00
2026-03-11 15:07:46 -07:00
var g errgroup . Group
g . Go ( func () error {
var err error
dbConfig , err = p . resolveModelConfig ( ctx , chat )
if err != nil {
return xerrors . Errorf ( "resolve model config: %w" , err )
}
return nil
})
g . Go ( func () error {
var err error
providers , err = p . db . GetEnabledChatProviders ( ctx )
if err != nil {
return xerrors . Errorf ( "get enabled chat providers: %w" , err )
}
return nil
})
if err := g . Wait (); err != nil {
return nil , database . ChatModelConfig {}, chatprovider . ProviderAPIKeys {}, err
2026-02-27 11:50:56 -05:00
}
dbProviders := make (
[] chatprovider . ConfiguredProvider , 0 , len ( providers ),
)
for _ , provider := range providers {
dbProviders = append ( dbProviders , chatprovider . ConfiguredProvider {
Provider : provider . Provider ,
APIKey : provider . APIKey ,
BaseURL : provider . BaseUrl ,
})
}
keys := chatprovider . MergeProviderAPIKeys (
p . providerAPIKeys , dbProviders ,
)
model , err := chatprovider . ModelFromConfig (
2026-03-13 15:12:04 +05:00
dbConfig . Provider , dbConfig . Model , keys , chatprovider . UserAgent (),
2026-02-27 11:50:56 -05:00
)
if err != nil {
2026-03-04 11:03:03 -05:00
return nil , database . ChatModelConfig {}, chatprovider . ProviderAPIKeys {}, xerrors . Errorf (
2026-02-27 11:50:56 -05:00
"create model: %w" , err ,
)
}
2026-03-04 11:03:03 -05:00
return model , dbConfig , keys , nil
2026-02-27 11:50:56 -05:00
}
// resolveModelConfig looks up the chat's model config by its
// LastModelConfigID. If the referenced config no longer exists
// (e.g. it was deleted), it falls back to the default model
// config. Returns an error when no usable config is available.
func ( p * Server ) resolveModelConfig (
ctx context . Context ,
chat database . Chat ,
) ( database . ChatModelConfig , error ) {
if chat . LastModelConfigID != uuid . Nil {
modelConfig , err := p . db . GetChatModelConfigByID (
ctx , chat . LastModelConfigID ,
)
if err == nil {
return modelConfig , nil
}
if ! xerrors . Is ( err , sql . ErrNoRows ) {
return database . ChatModelConfig {}, xerrors . Errorf (
"get chat model config %s: %w" ,
chat . LastModelConfigID , err ,
)
}
// Model config was deleted, fall through to default.
}
defaultConfig , err := p . db . GetDefaultChatModelConfig ( ctx )
if err != nil {
if xerrors . Is ( err , sql . ErrNoRows ) {
return database . ChatModelConfig {}, xerrors . New (
"no default chat model config is available" ,
)
}
return database . ChatModelConfig {}, xerrors . Errorf (
"get default chat model config: %w" , err ,
)
}
return defaultConfig , nil
}
2026-03-13 18:30:49 +01:00
func int64Ptr ( value int64 ) * int64 {
return & value
}
2026-02-27 11:50:56 -05:00
//nolint:revive // Boolean controls SQL NULL validity.
func usageNullInt64 ( value int64 , valid bool ) sql . NullInt64 {
if ! valid {
return sql . NullInt64 {}
}
return sql . NullInt64 {
Int64 : value ,
Valid : valid ,
}
}
2026-03-13 18:30:49 +01:00
func usageNullInt64Ptr ( v * int64 ) sql . NullInt64 {
if v == nil {
return sql . NullInt64 {}
}
return sql . NullInt64 { Int64 : * v , Valid : true }
}
2026-02-28 19:07:17 -05:00
func refreshChatWorkspaceSnapshot (
ctx context . Context ,
chat database . Chat ,
loadChat func ( context . Context , uuid . UUID ) ( database . Chat , error ),
) ( database . Chat , error ) {
if chat . WorkspaceID . Valid || loadChat == nil {
return chat , nil
}
refreshedChat , err := loadChat ( ctx , chat . ID )
if err != nil {
return chat , xerrors . Errorf ( "reload chat workspace state: %w" , err )
}
return refreshedChat , nil
}
2026-02-27 14:21:23 -05:00
// resolveInstructions returns the combined system instructions for the
// workspace agent. It reads the home-level (~/.coder/AGENTS.md) and
// working-directory-level (<pwd>/AGENTS.md) instruction files, combines
// them with agent metadata (OS, directory), and caches the result.
func ( p * Server ) resolveInstructions (
2026-02-27 11:50:56 -05:00
ctx context . Context ,
chat database . Chat ,
getWorkspaceConn func ( context . Context ) ( workspacesdk . AgentConn , error ),
) string {
2026-02-28 16:46:51 -05:00
if ! chat . WorkspaceID . Valid {
2026-02-27 14:21:23 -05:00
return ""
}
2026-02-28 16:46:51 -05:00
agents , agentsErr := p . db . GetWorkspaceAgentsInLatestBuildByWorkspaceID (
2026-03-02 15:57:04 +00:00
ctx ,
2026-02-28 16:46:51 -05:00
chat . WorkspaceID . UUID ,
)
if agentsErr != nil || len ( agents ) == 0 {
return ""
}
agentID := agents [ 0 ]. ID
2026-02-27 11:50:56 -05:00
p . instructionCacheMu . Lock ()
cached , ok := p . instructionCache [ agentID ]
p . instructionCacheMu . Unlock ()
if ok && time . Since ( cached . fetchedAt ) < instructionCacheTTL {
return cached . instruction
}
2026-02-27 14:21:23 -05:00
// Look up the agent's OS and working directory.
2026-03-02 15:57:04 +00:00
agent , err := p . db . GetWorkspaceAgentByID ( ctx , agentID )
2026-02-27 11:50:56 -05:00
if err != nil {
2026-02-27 14:21:23 -05:00
p . logger . Debug ( ctx , "failed to look up workspace agent for instruction context" ,
slog . F ( "agent_id" , agentID ),
2026-02-27 11:50:56 -05:00
slog . Error ( err ),
)
2026-02-27 14:21:23 -05:00
}
directory := agent . ExpandedDirectory
if directory == "" {
directory = agent . Directory
2026-02-27 11:50:56 -05:00
}
2026-02-27 14:21:23 -05:00
// Read instruction files from the workspace agent.
var sections [] instructionFileSection
if getWorkspaceConn != nil {
instructionCtx , cancel := context . WithTimeout ( ctx , homeInstructionLookupTimeout )
defer cancel ()
conn , connErr := getWorkspaceConn ( instructionCtx )
if connErr != nil {
p . logger . Debug ( ctx , "failed to resolve workspace connection for instruction files" ,
slog . F ( "chat_id" , chat . ID ),
slog . Error ( connErr ),
)
} else {
// ~/.coder/AGENTS.md
if content , source , truncated , err := readHomeInstructionFile ( instructionCtx , conn ); err != nil {
p . logger . Debug ( ctx , "failed to load home instruction file" ,
slog . F ( "chat_id" , chat . ID ), slog . Error ( err ))
} else if content != "" {
sections = append ( sections , instructionFileSection { content , source , truncated })
}
// <pwd>/AGENTS.md
if pwdPath := pwdInstructionFilePath ( directory ); pwdPath != "" {
if content , source , truncated , err := readInstructionFile ( instructionCtx , conn , pwdPath ); err != nil {
p . logger . Debug ( ctx , "failed to load working directory instruction file" ,
slog . F ( "chat_id" , chat . ID ), slog . F ( "directory" , directory ), slog . Error ( err ))
} else if content != "" {
sections = append ( sections , instructionFileSection { content , source , truncated })
}
}
}
2026-02-27 11:50:56 -05:00
}
2026-02-27 14:21:23 -05:00
instruction := formatSystemInstructions ( agent . OperatingSystem , directory , sections )
2026-02-27 11:50:56 -05:00
p . instructionCacheMu . Lock ()
p . instructionCache [ agentID ] = cachedInstruction {
instruction : instruction ,
fetchedAt : time . Now (),
}
p . instructionCacheMu . Unlock ()
return instruction
}
2026-03-10 08:17:52 -07:00
// resolveUserPrompt fetches the user's custom chat prompt from the
// database and wraps it in <user-instructions> tags. Returns empty
// string if no prompt is set.
func ( p * Server ) resolveUserPrompt ( ctx context . Context , userID uuid . UUID ) string {
raw , err := p . db . GetUserChatCustomPrompt ( ctx , userID )
if err != nil {
// sql.ErrNoRows is the normal "not set" case.
return ""
}
trimmed := strings . TrimSpace ( raw )
if trimmed == "" {
return ""
}
return "<user-instructions>\n" + trimmed + "\n</user-instructions>"
}
2026-02-27 11:50:56 -05:00
func ( p * Server ) recoverStaleChats ( ctx context . Context ) {
staleAfter := time . Now (). Add ( - p . inFlightChatStaleAfter )
staleChats , err := p . db . GetStaleChats ( ctx , staleAfter )
if err != nil {
p . logger . Error ( ctx , "failed to get stale chats" , slog . Error ( err ))
return
}
2026-03-06 13:02:25 -08:00
recovered := 0
2026-02-27 11:50:56 -05:00
for _ , chat := range staleChats {
p . logger . Info ( ctx , "recovering stale chat" , slog . F ( "chat_id" , chat . ID ))
2026-03-06 13:02:25 -08:00
// Use a transaction with FOR UPDATE to avoid a TOCTOU race:
// between GetStaleChats (a bare SELECT) and here, the chat's
// heartbeat may have been refreshed. We re-check freshness
// under the row lock before resetting.
err := p . db . InTx ( func ( tx database . Store ) error {
locked , lockErr := tx . GetChatByIDForUpdate ( ctx , chat . ID )
if lockErr != nil {
return xerrors . Errorf ( "lock chat for recovery: %w" , lockErr )
}
// Only recover chats that are still running.
// Between GetStaleChats and this lock, the chat
// may have completed normally.
if locked . Status != database . ChatStatusRunning {
p . logger . Debug ( ctx , "chat status changed since snapshot, skipping recovery" ,
slog . F ( "chat_id" , chat . ID ),
slog . F ( "status" , locked . Status ))
return nil
}
// Re-check: only recover if the chat is still stale.
// A valid heartbeat that is at or after the stale
// threshold means the chat was refreshed after our
// initial snapshot — skip it.
if locked . HeartbeatAt . Valid && ! locked . HeartbeatAt . Time . Before ( staleAfter ) {
p . logger . Debug ( ctx , "chat heartbeat refreshed since snapshot, skipping recovery" ,
slog . F ( "chat_id" , chat . ID ))
return nil
}
// Reset to pending so any replica can pick it up.
_ , updateErr := tx . UpdateChatStatus ( ctx , database . UpdateChatStatusParams {
ID : chat . ID ,
Status : database . ChatStatusPending ,
WorkerID : uuid . NullUUID {},
StartedAt : sql . NullTime {},
HeartbeatAt : sql . NullTime {},
LastError : sql . NullString {},
})
if updateErr != nil {
return updateErr
}
recovered ++
return nil
}, nil )
2026-02-27 11:50:56 -05:00
if err != nil {
p . logger . Error ( ctx , "failed to recover stale chat" ,
slog . F ( "chat_id" , chat . ID ), slog . Error ( err ))
}
}
2026-03-06 13:02:25 -08:00
if recovered > 0 {
p . logger . Info ( ctx , "recovered stale chats" , slog . F ( "count" , recovered ))
2026-02-27 11:50:56 -05:00
}
}
2026-03-10 15:14:15 +00:00
// maybeSendPushNotification sends a web push notification when an
// agent chat reaches a terminal state. For errors it dispatches
// synchronously; for successful completions it spawns a goroutine
// that generates a short LLM summary before dispatching. The caller
// is responsible for skipping interrupted chats.
func ( p * Server ) maybeSendPushNotification (
ctx context . Context ,
chat database . Chat ,
status database . ChatStatus ,
lastError string ,
logger slog . Logger ,
) {
if p . webpushDispatcher == nil || p . webpushDispatcher . PublicKey () == "" {
return
}
if chat . ParentChatID . Valid {
return
}
switch status {
case database . ChatStatusError :
pushBody := "Agent encountered an error."
if lastError != "" {
pushBody = lastError
}
p . dispatchPush ( ctx , chat , pushBody , status , logger )
case database . ChatStatusWaiting :
// Generate a push notification summary asynchronously
// using a cheap LLM model. This avoids blocking the
// deferred cleanup path while still providing a
// meaningful notification body.
p . inflight . Add ( 1 )
go func () {
defer p . inflight . Done ()
pushCtx := context . WithoutCancel ( ctx )
pushBody := "Agent has finished running."
msg , err := p . db . GetLastChatMessageByRole ( pushCtx , database . GetLastChatMessageByRoleParams {
ChatID : chat . ID ,
2026-03-13 18:47:36 +02:00
Role : database . ChatMessageRoleAssistant ,
2026-03-10 15:14:15 +00:00
})
if err == nil {
2026-03-13 18:47:36 +02:00
content , parseErr := chatprompt . ParseContent ( msg )
2026-03-10 15:14:15 +00:00
if parseErr == nil {
assistantText := strings . TrimSpace ( contentBlocksToText ( content ))
if assistantText != "" {
model , _ , keys , resolveErr := p . resolveChatModel ( pushCtx , chat )
if resolveErr == nil {
if summary := generatePushSummary ( pushCtx , chat . Title , assistantText , model , keys , logger ); summary != "" {
pushBody = summary
}
}
}
}
}
p . dispatchPush ( pushCtx , chat , pushBody , status , logger )
}()
}
}
func ( p * Server ) dispatchPush (
ctx context . Context ,
chat database . Chat ,
body string ,
status database . ChatStatus ,
logger slog . Logger ,
) {
pushMsg := codersdk . WebpushMessage {
Title : chat . Title ,
Body : body ,
Icon : "/favicon.ico" ,
Data : map [ string ] string { "url" : fmt . Sprintf ( "/agents/%s" , chat . ID )},
}
if err := p . webpushDispatcher . Dispatch ( ctx , chat . OwnerID , pushMsg ); err != nil {
logger . Warn ( ctx , "failed to send chat completion web push" ,
slog . F ( "chat_id" , chat . ID ),
slog . F ( "status" , status ),
slog . Error ( err ),
)
}
}
2026-02-27 11:50:56 -05:00
// Close stops the processor and waits for it to finish.
func ( p * Server ) Close () error {
p . cancel ()
<- p . closed
p . inflight . Wait ()
return nil
}