Files
coder/coderd/x/chatd/runner_manager.go
T
J. Scott Miller fc0af7fbd8 feat: open a chat_turn span per prompt (#29771)
**TL;DR:** Wraps each prompt in a `chat_turn` root span with every
lifecycle stage parented under it. The span records one outcome per turn
(`completed`, `interrupted`, `error`, `abandoned`), and the new
`coderd_chatd_turn_outcomes_total` counter counts those outcomes. Turns
stay correct when tasks are canceled, replaced, or retried.

Each prompt runs inside a `chat_turn` root span owned by the runner
(`runnerTurnSpan`). The generation step, prepare, MCP connect, stream,
TTFT, provider attempt, commit, thinking, tool call, compaction, and
retry backoff stages are parented under it, activating the stages from
#29770. `chat_turn` spans are standalone roots: the turn runs on a
worker, often on another replica, and no trace context is persisted.

**Lifecycle**

- **Start.** Each generation loop iteration calls `Ensure`, which opens
the turn if none is open and returns a `turnToken`. The span is
backdated to the trigger: the latest of the last user prompt, a dynamic
tool's result message after it, and a pending compaction request. An
`acquisition` stage covers trigger to pickup.
- **Stale trigger / takeover.** A trigger at or before the previous
anchor opens the turn at now with no `acquisition` and counts
`stale_anchor`. A runner that took the chat over from an owner with a
stale heartbeat (`TakenOver`, from acquisition) opens its first turn at
now with no `acquisition`, so the dead owner's run is not reported as
pickup delay.
- **Newer prompt.** `Ensure` on the current turn with a newer trigger
closes it and opens a new one. The closed turn is `abandoned` unless the
task that ran it records an outcome before releasing it.
- **Finish.** After `FinishTurn` or `EnterRequiresAction` commits, the
step calls `Complete`; `Settle` closes the span after the step's own
stage ends, so the finishing step is inside the turn. A chat waiting in
`requires_action` is outside any turn; the generation resumed by its
tool results opens a new one.
- **Emission.** Closing a turn fixes its end time; its span is emitted
once every task that joined it (`Ensure` takes the task ID) has called
`Release`, which `runTask` does after the task returns. A finishing task
whose `FinishTurn` promoted a queued prompt therefore still counts its
turn as `completed` even when the promoted task's `Ensure` runs before
its `Complete`. `End` at runner exit emits whatever is left.
- **Failure.** A step that commits `FinishError`, or fails closed,
invalidates the turn as `error` after the commit. Errors the task runner
retries, including task timeouts, keep the same turn open; an expected,
non-retryable exit records no outcome and leaves the turn open until the
interrupt task, a newer prompt, or runner exit closes it. A canceled
generation task records no outcome and cannot obtain a turn token, so a
replaced task never touches its successor's turn.
- **Interrupt.** The interrupt task marks the open turn `interrupted`
before `FinishInterruption` commits and closes it after. Only a stop (by
the user or a parent agent) is `interrupted`; it ends with unset span
status, since error status is reserved for `error` turns. runner
shutdown, loss of ownership, and message edits close the turn as
`abandoned` with unset status.
- **Promotion.** Every promoting transition (`FinishTurn`,
`FinishInterruption`, a send to an errored chat, `PromoteQueued`)
returns the promoted message's queued time in its result, and one
`recordQueueWait` helper records a standalone `queue_wait` to the
promotion. The promoted turn opens at the promotion time.

`chat_turn` is observed on `coderd_chatd_stage_duration_seconds` only
for completed turns, so the histogram is completed-turn latency; every
turn still emits its span.

`coderd_chatd_turn_outcomes_total{outcome, chat_kind}` counts every
closed turn exactly once (`completed`, `interrupted`, `error`,
`abandoned`), matching the span's `turn_outcome`, under the
`chat-stage-metrics` experiment. Turn time accounting is in draft
#29772.

**Stages and identity**

- `tool_call` is a live stage around each local tool call in `chatloop`;
the tool runs on its context, and it ends errored only when the tool
fails to execute. Calls that never run (inactive, unknown, or rejected
by policy) record no stage.
- `mcp_connect` carries connected and failed server counts and ends
errored only when servers were configured and none connected.
- The compaction stage carries the model that runs the summary.
- `withStageIdentity` puts scope and chat kind on the turn context.
Detached background work runs in `background` scope as its own trace
roots.
- The `advisor` tool's nested model call appears only as its enclosing
`tool_call`.

ARCHITECTURE.md gains a "Lifecycle tracing" section.

<details>
<summary>Review decisions</summary>

- Turn identity stays timestamp-based with targeted fixes (clamped
acquisition, rotation on a newer trigger, invalidate after commit,
fail-closed invalidation, settle closing invalidated turns) plus a
takeover flag; keying turns on message IDs would not detect takeover and
cannot express interrupted/abandoned outcomes.
- Outcome classification: the interrupt task owns `interrupted`, and
cancellation or a fence mismatch alone never records an outcome, so
shutdown, ownership loss, and edits are `abandoned`. Only `error` turns
set error span status. Remaining ordering races between a step and an
interrupt are documented as known limitations in ARCHITECTURE.md.
- Turns are emitted after the tasks that ran them return, not when they
close, so an outcome a replaced task records while unwinding reaches its
own turn; the successor task is not delayed. Chosen over an
after-commit, pre-publish hook on `ChatMachine.Update`, which would fix
only the `FinishTurn` ordering.
- `requires_action` ends the turn rather than pausing it, so `chat_turn`
measures chatd time; a prompt with client tools counts as several turns.
- Promoted turns anchor at promotion time and `queue_wait` is always
standalone, so `chat_turn` never includes time spent queued behind
another turn.
- The former `capacity_wait` stage (#29805) was dropped: it was a
per-worker lower bound, excluded from accounting, and needed its own SQL
and bookkeeping. #29805's remaining helpers are folded into this PR.

</details>

Supersedes #28972, #28973, and #29805.

🤖 This PR was generated by Coder Agents on behalf of @jscottmiller.
2026-09-30 11:53:58 -05:00

535 lines
13 KiB
Go

package chatd
import (
"context"
"database/sql"
"encoding/json"
"sync"
"time"
"github.com/google/uuid"
"golang.org/x/xerrors"
"cdr.dev/slog/v3"
"github.com/coder/coder/v2/coderd/database"
coderdpubsub "github.com/coder/coder/v2/coderd/pubsub"
)
const shutdownCleanupTimeout = 5 * time.Second
type runnerKey struct {
ChatID uuid.UUID
RunnerID uuid.UUID
}
type runnerStateUpdate struct {
ChatID uuid.UUID
WorkerID *uuid.UUID
RunnerID *uuid.UUID
SnapshotVersion int64
HistoryVersion int64
QueueVersion int64
GenerationAttempt int64
Status database.ChatStatus
Archived bool
RequiresActionDeadlineAt sql.NullTime
}
type spawnRunnerRequest struct {
ChatID uuid.UUID
WorkerID uuid.UUID
RunnerID uuid.UUID
// TakenOver means the previous owner's heartbeat went stale.
TakenOver bool
}
type runnerRecord struct {
key runnerKey
workerID uuid.UUID
takenOver bool
cancel context.CancelFunc
done <-chan struct{}
stateCh chan runnerStateUpdate
mu sync.Mutex
unsubscribe func()
cleanupStarted bool
}
func (r *runnerRecord) setUnsubscribe(unsubscribe func()) bool {
r.mu.Lock()
if r.cleanupStarted {
r.mu.Unlock()
if unsubscribe != nil {
unsubscribe()
}
return false
}
r.unsubscribe = unsubscribe
r.mu.Unlock()
return true
}
func (r *runnerRecord) startCleanup() {
r.mu.Lock()
if r.cleanupStarted {
r.mu.Unlock()
return
}
r.cleanupStarted = true
unsubscribe := r.unsubscribe
r.unsubscribe = nil
r.mu.Unlock()
if unsubscribe != nil {
unsubscribe()
}
r.cancel()
}
type runnerManager struct {
server *Server
opts chatWorkerOptions
ctx context.Context
closed bool
spawnMu sync.Mutex
mu sync.Mutex
spawnCh chan spawnRunnerRequest
cleanupReqCh chan runnerKey
cleanupDoneCh chan runnerKey
runners map[runnerKey]*runnerRecord
runnersByChat map[uuid.UUID]map[uuid.UUID]*runnerRecord
cleaning map[runnerKey]*runnerRecord
wg sync.WaitGroup
}
func newRunnerManager(ctx context.Context, server *Server, opts chatWorkerOptions) *runnerManager {
return &runnerManager{
server: server,
opts: opts,
ctx: ctx,
spawnCh: make(chan spawnRunnerRequest, opts.RunnerManagerChannelSize),
cleanupReqCh: make(chan runnerKey, opts.RunnerManagerChannelSize),
cleanupDoneCh: make(chan runnerKey, opts.RunnerManagerChannelSize),
runners: make(map[runnerKey]*runnerRecord),
runnersByChat: make(map[uuid.UUID]map[uuid.UUID]*runnerRecord),
cleaning: make(map[runnerKey]*runnerRecord),
}
}
func (m *runnerManager) start() {
m.wg.Go(m.run)
m.wg.Go(m.databaseSyncLoop)
m.wg.Go(m.heartbeatLoop)
m.wg.Go(m.heartbeatCleanupLoop)
}
func (m *runnerManager) wait() {
m.wg.Wait()
}
func (m *runnerManager) idle() bool {
m.mu.Lock()
defer m.mu.Unlock()
return len(m.runners) == 0 && len(m.cleaning) == 0
}
func (m *runnerManager) Spawn(ctx context.Context, req spawnRunnerRequest) error {
m.spawnMu.Lock()
defer m.spawnMu.Unlock()
if m.closed {
return xerrors.New("chatworker: runner manager closed")
}
select {
case m.spawnCh <- req:
return nil
case <-ctx.Done():
return ctx.Err()
case <-m.ctx.Done():
return m.ctx.Err()
}
}
func (m *runnerManager) requestCleanup(ctx context.Context, key runnerKey) {
select {
case m.cleanupReqCh <- key:
case <-ctx.Done():
case <-m.ctx.Done():
}
}
func (m *runnerManager) RouteStateHint(ctx context.Context, state runnerStateUpdate) {
m.mu.Lock()
byRunner := m.runnersByChat[state.ChatID]
targets := make([]*runnerRecord, 0, len(byRunner))
for _, rec := range byRunner {
targets = append(targets, rec)
}
m.mu.Unlock()
for _, rec := range targets {
select {
case rec.stateCh <- state:
case <-rec.done:
// Only this runner exited; keep fanning out to the rest.
continue
case <-ctx.Done():
return
case <-m.ctx.Done():
return
default:
// stateCh is full; drop the hint for this runner.
}
}
}
func (m *runnerManager) run() {
for {
select {
case req := <-m.spawnCh:
m.handleSpawn(req)
case key := <-m.cleanupReqCh:
m.handleCleanupRequest(key)
case key := <-m.cleanupDoneCh:
m.handleCleanupDone(key)
case <-m.ctx.Done():
queued := m.closeAndDrainQueues()
m.cancelAll()
m.releaseOwnershipHints(queued)
return
}
}
}
func (m *runnerManager) handleSpawn(req spawnRunnerRequest) {
key := runnerKey{ChatID: req.ChatID, RunnerID: req.RunnerID}
m.mu.Lock()
if _, ok := m.runners[key]; ok {
// A duplicate spawn for a live runner indicates a logic error
// in the sync loop.
m.opts.Logger.Error(m.ctx, "invalid spawn request: chat runner already spawned", slog.F("key", key))
m.mu.Unlock()
return
}
if _, ok := m.cleaning[key]; ok {
// A duplicate spawn for a live runner indicates a logic error
// in the sync loop.
m.opts.Logger.Error(m.ctx, "invalid spawn request: chat runner in cleanup", slog.F("key", key))
m.mu.Unlock()
return
}
runnerCtx, cancel := context.WithCancel(m.ctx)
done := make(chan struct{})
rec := &runnerRecord{
key: key,
workerID: req.WorkerID,
takenOver: req.TakenOver,
cancel: cancel,
done: done,
stateCh: make(chan runnerStateUpdate, m.opts.StateChannelSize),
}
m.runners[key] = rec
if m.runnersByChat[req.ChatID] == nil {
m.runnersByChat[req.ChatID] = make(map[uuid.UUID]*runnerRecord)
}
m.runnersByChat[req.ChatID][req.RunnerID] = rec
m.mu.Unlock()
r := newRunner(runnerCtx, m, rec, m.opts)
m.wg.Go(func() {
defer close(done)
r.run()
})
}
func (m *runnerManager) closeAndDrainQueues() []runnerKey {
m.spawnMu.Lock()
defer m.spawnMu.Unlock()
m.closed = true
return m.drainQueues()
}
func (m *runnerManager) drainQueues() []runnerKey {
queued := make([]runnerKey, 0)
for {
select {
case req := <-m.spawnCh:
queued = append(queued, runnerKey{ChatID: req.ChatID, RunnerID: req.RunnerID})
case key := <-m.cleanupReqCh:
m.handleCleanupRequest(key)
case key := <-m.cleanupDoneCh:
m.handleCleanupDone(key)
default:
return queued
}
}
}
func (m *runnerManager) handleCleanupRequest(key runnerKey) {
m.mu.Lock()
rec, ok := m.runners[key]
if !ok {
m.mu.Unlock()
return
}
delete(m.runners, key)
if byChat := m.runnersByChat[key.ChatID]; byChat != nil {
delete(byChat, key.RunnerID)
if len(byChat) == 0 {
delete(m.runnersByChat, key.ChatID)
}
}
m.cleaning[key] = rec
m.mu.Unlock()
rec.startCleanup()
m.registerCleanupWaiter(key, rec)
}
func (m *runnerManager) registerCleanupWaiter(key runnerKey, rec *runnerRecord) {
m.wg.Go(func() {
<-rec.done
if m.ctx.Err() != nil {
m.mu.Lock()
delete(m.cleaning, key)
m.mu.Unlock()
return
}
select {
case m.cleanupDoneCh <- key:
case <-m.ctx.Done():
m.mu.Lock()
delete(m.cleaning, key)
m.mu.Unlock()
}
})
}
func (m *runnerManager) handleCleanupDone(key runnerKey) {
m.mu.Lock()
delete(m.cleaning, key)
m.mu.Unlock()
}
func (m *runnerManager) cancelAll() {
type cleanupTarget struct {
key runnerKey
rec *runnerRecord
}
m.mu.Lock()
active := make([]cleanupTarget, 0, len(m.runners))
cleaning := make([]*runnerRecord, 0, len(m.cleaning))
for _, rec := range m.cleaning {
cleaning = append(cleaning, rec)
}
for key, rec := range m.runners {
delete(m.runners, key)
m.cleaning[key] = rec
active = append(active, cleanupTarget{key: key, rec: rec})
}
clear(m.runnersByChat)
m.mu.Unlock()
keys := make([]runnerKey, 0, len(cleaning)+len(active))
for _, rec := range cleaning {
rec.startCleanup()
keys = append(keys, rec.key)
}
for _, target := range active {
target.rec.startCleanup()
m.registerCleanupWaiter(target.key, target.rec)
keys = append(keys, target.key)
}
m.releaseOwnershipHints(keys)
}
func (m *runnerManager) releaseOwnershipHints(keys []runnerKey) {
if len(keys) == 0 {
return
}
ctx, cancel := context.WithTimeout(context.WithoutCancel(m.ctx), shutdownCleanupTimeout)
defer cancel()
chatIDs := make([]uuid.UUID, 0, len(keys))
runnerIDs := make([]uuid.UUID, 0, len(keys))
uniqueChatIDs := make(map[uuid.UUID]struct{}, len(keys))
for _, key := range keys {
chatIDs = append(chatIDs, key.ChatID)
runnerIDs = append(runnerIDs, key.RunnerID)
uniqueChatIDs[key.ChatID] = struct{}{}
}
if _, err := m.opts.Store.BatchDeleteChatHeartbeats(ctx, database.BatchDeleteChatHeartbeatsParams{
ChatIds: chatIDs,
RunnerIds: runnerIDs,
}); err != nil {
m.opts.Logger.Warn(ctx, "chatworker shutdown heartbeat cleanup failed", slogError(err))
}
syncIDs := make([]uuid.UUID, 0, len(uniqueChatIDs))
for id := range uniqueChatIDs {
syncIDs = append(syncIDs, id)
}
chats, err := m.opts.Store.GetChatsByIDsForRunnerSync(ctx, syncIDs)
if err != nil {
m.opts.Logger.Warn(ctx, "chatworker shutdown ownership lookup failed", slogError(err))
}
snapshotByChat := make(map[uuid.UUID]int64, len(chats))
for _, chat := range chats {
snapshotByChat[chat.ID] = chat.SnapshotVersion
}
for _, key := range keys {
payload, err := json.Marshal(coderdpubsub.ChatStateOwnershipMessage{
ChatID: key.ChatID,
SnapshotVersion: snapshotByChat[key.ChatID],
})
if err != nil {
m.opts.Logger.Warn(ctx, "chatworker shutdown ownership marshal failed", slogError(err))
continue
}
if err := m.opts.Pubsub.Publish(coderdpubsub.ChatStateOwnershipChannel, payload); err != nil {
m.opts.Logger.Warn(ctx, "chatworker shutdown ownership publish failed", slogError(err))
}
}
}
func (m *runnerManager) snapshotRunnerKeys() []runnerKey {
m.mu.Lock()
defer m.mu.Unlock()
keys := make([]runnerKey, 0, len(m.runners))
for key := range m.runners {
keys = append(keys, key)
}
return keys
}
func (m *runnerManager) databaseSyncLoop() {
ticker := m.opts.Clock.NewTicker(m.opts.RunnerSyncInterval, "chatworker", "runner-sync")
defer ticker.Stop()
for {
select {
case <-ticker.C:
if err := m.syncOnce(m.ctx); err != nil {
m.opts.Logger.Warn(m.ctx, "chatworker runner sync failed", slogError(err))
}
case <-m.ctx.Done():
return
}
}
}
func (m *runnerManager) syncOnce(ctx context.Context) error {
keys := m.snapshotRunnerKeys()
if len(keys) == 0 {
return nil
}
idsByChat := make(map[uuid.UUID]struct{}, len(keys))
for _, key := range keys {
idsByChat[key.ChatID] = struct{}{}
}
chatIDs := make([]uuid.UUID, 0, len(idsByChat))
for id := range idsByChat {
chatIDs = append(chatIDs, id)
}
chats, err := m.opts.Store.GetChatsByIDsForRunnerSync(ctx, chatIDs)
if err != nil {
return xerrors.Errorf("get chats for runner sync: %w", err)
}
seen := make(map[uuid.UUID]struct{}, len(chats))
for _, chat := range chats {
seen[chat.ID] = struct{}{}
m.RouteStateHint(ctx, stateUpdateFromChat(chat))
}
for _, key := range keys {
if _, ok := seen[key.ChatID]; !ok {
m.requestCleanup(ctx, key)
}
}
return nil
}
func (m *runnerManager) heartbeatLoop() {
ticker := m.opts.Clock.NewTicker(m.opts.HeartbeatInterval, "chatworker", "heartbeat")
defer ticker.Stop()
for {
select {
case <-ticker.C:
if err := m.heartbeatOnce(m.ctx); err != nil {
m.opts.Logger.Warn(m.ctx, "chatworker heartbeat failed", slogError(err))
}
case <-m.ctx.Done():
return
}
}
}
func (m *runnerManager) heartbeatOnce(ctx context.Context) error {
keys := m.snapshotRunnerKeys()
if len(keys) == 0 {
return nil
}
chatIDs := make([]uuid.UUID, 0, len(keys))
runnerIDs := make([]uuid.UUID, 0, len(keys))
for _, key := range keys {
chatIDs = append(chatIDs, key.ChatID)
runnerIDs = append(runnerIDs, key.RunnerID)
}
return m.opts.Store.BatchUpsertChatHeartbeats(ctx, database.BatchUpsertChatHeartbeatsParams{
ChatIds: chatIDs,
RunnerIds: runnerIDs,
})
}
func (m *runnerManager) heartbeatCleanupLoop() {
ticker := m.opts.Clock.NewTicker(m.opts.HeartbeatCleanupInterval, "chatworker", "heartbeat-cleanup")
defer ticker.Stop()
for {
select {
case <-ticker.C:
if err := m.heartbeatCleanupOnce(m.ctx); err != nil {
m.opts.Logger.Warn(m.ctx, "chatworker heartbeat cleanup failed", slogError(err))
}
case <-m.ctx.Done():
return
}
}
}
func (m *runnerManager) heartbeatCleanupOnce(ctx context.Context) error {
_, err := m.opts.Store.DeleteStaleChatHeartbeats(ctx, m.opts.HeartbeatStaleSeconds)
return err
}
func stateUpdateFromChat(chat database.Chat) runnerStateUpdate {
var workerID *uuid.UUID
if chat.WorkerID.Valid {
id := chat.WorkerID.UUID
workerID = &id
}
var runnerID *uuid.UUID
if chat.RunnerID.Valid {
id := chat.RunnerID.UUID
runnerID = &id
}
return runnerStateUpdate{
ChatID: chat.ID,
WorkerID: workerID,
RunnerID: runnerID,
SnapshotVersion: chat.SnapshotVersion,
HistoryVersion: chat.HistoryVersion,
QueueVersion: chat.QueueVersion,
GenerationAttempt: chat.GenerationAttempt,
Status: chat.Status,
Archived: chat.Archived,
RequiresActionDeadlineAt: chat.RequiresActionDeadlineAt,
}
}
func slogError(err error) slog.Field {
return slog.Error(err)
}