Files
coder/coderd/x/chatd/generation.go
T
Garrett Delfosse 74a15cbcaa feat: adopt OpenTelemetry GenAI semantic conventions for Coder Agents traces (#32411)
Aligns Coder Agents traces with the [OpenTelemetry GenAI semantic
conventions](https://opentelemetry.io/docs/specs/semconv/gen-ai/) so
customers can ingest per-user model usage, spend, and tool activity
without custom mapping. Scope follows the requesting team's Slack ask:
agent spans, inference spans with token usage, GenAI attributes,
per-user attribution, and visibility into tools that cannot be reached.

Closes CODAGT-1523.

## Changes

- Span names follow the conventions: `chat_turn` becomes `invoke_agent
coder`, `stream` becomes `chat {model}`, and `tool_call` becomes
`execute_tool {tool}`. Every stage span now carries a `stage` attribute
with the old stage name, which matches the Prometheus `stage` label.
Metrics are unchanged.
- GenAI spans carry `gen_ai.operation.name`, `gen_ai.conversation.id`
(the chat ID), `user.id` (the chat owner), and `coder.organization.id`,
`coder.workspace.id`, `coder.chat.parent_id`, `coder.chat.root_id`.
- `chat` spans are `CLIENT` spans with the provider, request model, and
`gen_ai.request.stream` set at creation (so samplers can see them), plus
requested max tokens and temperature, response ID (including OpenAI
Responses metadata on non-streaming calls), the finish reason the
provider reported (omitted when none, kept when Coder rejects the
response), `gen_ai.output.type=json` for native OpenAI structured
output, and token usage with cache read/write (only when nonzero) and
reasoning breakdown. `gen_ai.usage.input_tokens` includes cached tokens,
as the spec requires. Fantasy reports them separately (Google providers
use the OpenAI-compatible client, which does the same).
- `execute_tool` spans carry the tool name, call ID, and type
(`function` for built-in tools, `extension` for MCP and caller tools),
all set at creation.
- Every model call records a `chat` span: assistant streams
(`stage=stream`), and, as `stage=model_call`, the advisor, compaction
summaries plus titles, status labels, and chat summaries (via
`StageTracer.TraceModelCalls` in `resolveModelCall`). `model_call` has
no histogram, so the `stream` metric is unchanged.
- Failed spans get `error.type`. A tool that returns an error result, as
MCP clients do for an unreachable server, ends its span with
`error.type=tool_error` without recording the result text.
- GenAI attributes carry metadata only; prompts, responses, and tool
payloads are not recorded. The docs note that other spans and events in
the trace, such as AI Gateway spans and server log events, can contain
content.
- Docs state known limits: Anthropic server-tool responses undercount
input tokens in both spans and AI Gateway cost control (the client keeps
first-iteration occupancy), the OpenAI-compatible client drops reported
cache writes, request attributes are what Coder requested, and the
exporter is OTLP/gRPC, plaintext unless a certificate is configured.
- New docs page:
`docs/coder-agents/platform-controls/opentelemetry-tracing.md`.
- `coderd/x/chatd/ARCHITECTURE.md` gains a GenAI semantic conventions
subsection under Lifecycle tracing.

Existing attributes (`provider`, `model`, `chat_id`, `tool_name`, ...)
are kept so current queries on attributes keep working. Queries on span
names need to switch to the `stage` attribute or the new names.

<details>
<summary>Decision log</summary>

- **Convention version:** semconv v1.41.0, the last core release that
ships the GenAI attributes. Later releases moved them to
`semantic-conventions-genai`. The PR uses the Go constants from
`go.opentelemetry.io/otel/semconv/v1.41.0`, which is already in the
module graph. No experiment gate: chatd is already under `x/`, and the
docs page notes that the conventions may change.
- **Rename vs. attributes only:** spans are renamed because the
conventions define the span names. The `stage` attribute keeps lifecycle
identity stable for dashboards and tests.
- **Agent name:** `gen_ai.agent.name=coder` for root chats and subagents
alike. `chat_kind` and `coder.chat.parent_id` tell them apart.
- **Provider name mapping:** configured provider type to well-known
values (`openai`, `anthropic`, `azure.ai.openai`, `aws.bedrock`,
`gcp.gemini`). Anthropic providers with Bedrock settings report
`aws.bedrock`. The `invoke_agent` span gets the provider after `prepare`
resolves the turn's model, since the turn span starts first. Other types
(`openrouter`, `openai-compat`, `vercel`, `copilot`) are passed through
as-is.
- **No content capture:** `gen_ai.input.messages`,
`gen_ai.output.messages`, and tool arguments/results are opt-in in the
conventions and not needed for usage and spend attribution, so they are
left for a follow-up behind an explicit deployment flag.
- **Out of scope:** GenAI metrics (`gen_ai.client.token.usage`,
`gen_ai.client.operation.duration`), an aggregated token total on the
`invoke_agent` span, a cost attribute, span links between subagent and
parent traces, and correlation IDs with AI Gateway interceptions (an
open question in the issue). Subagent turns join the parent only through
`coder.chat.root_id`; a span link needs the spawning span context
persisted across chats and runners. Spans carry tokens, not cost; cost
stays in the existing cost API.
- **Identity snapshot:** chat identity is taken when the turn opens. A
workspace created mid-turn appears on spans from the next turn.

</details>

---

Generated by Coder Agents on behalf of @f0ssel.
2026-11-24 11:41:03 -05:00

1928 lines
74 KiB
Go

package chatd
import (
"context"
"encoding/json"
"errors"
"fmt"
"strings"
"time"
"charm.land/fantasy"
"github.com/google/uuid"
"github.com/sqlc-dev/pqtype"
"go.opentelemetry.io/otel/attribute"
"golang.org/x/xerrors"
"cdr.dev/slog/v3"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/x/agenthooks/dispatch"
"github.com/coder/coder/v2/coderd/x/chatd/chatdebug"
"github.com/coder/coder/v2/coderd/x/chatd/chaterror"
"github.com/coder/coder/v2/coderd/x/chatd/chathooks"
"github.com/coder/coder/v2/coderd/x/chatd/chatloop"
"github.com/coder/coder/v2/coderd/x/chatd/chatprompt"
"github.com/coder/coder/v2/coderd/x/chatd/chatretry"
"github.com/coder/coder/v2/coderd/x/chatd/chatstate"
"github.com/coder/coder/v2/coderd/x/chatd/chattool"
"github.com/coder/coder/v2/coderd/x/chatd/mcpclient"
"github.com/coder/coder/v2/coderd/x/chatd/messagepartbuffer"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/codersdk/workspacesdk"
"github.com/coder/coder/v2/codersdk/x/agenthooks"
)
// generationPrepareInput contains the committed state used to prepare one
// generation action.
type generationPrepareInput struct {
Chat database.Chat
Messages []database.ChatMessage
// RecordMCPConnectSummaries receives the preparation's per-server
// MCP connect outcomes as soon as the connect phase completes,
// with the debug context needed to create the run when no action
// ever reaches Ensure. Preparation invokes it directly (rather
// than returning the outcomes) so attempts that fail after
// connecting still record before their error discards the
// prepared state.
RecordMCPConnectSummaries func(
ctx context.Context,
chat database.Chat,
debug *generationDebug,
summaries []mcpclient.ConnectSummary,
)
// RecordStageModel receives the turn model as soon as it resolves,
// so a preparation that fails later still attributes its turn span.
RecordStageModel func(chatloop.StageModel)
}
// generationCompaction contains compaction inputs prepared for generation.
type generationCompaction struct {
// Override, when non-nil, is the compaction model override resolved at
// prepare time. Its model client is built in the compact action path,
// so construction failures cannot fail turns that never compact.
Override *resolvedModelOverride
// ChatModelConfig is the chat model's config, used to detect provider
// changes when sanitizing the compaction prompt.
ChatModelConfig database.ChatModelConfig
// TriggerSource names the trigger whose threshold and limit Options
// carry.
TriggerSource compactionTriggerSource
// OtherTrigger is the trigger that does not bind while the override
// trigger is enabled, and zero otherwise.
OtherTrigger compactionTrigger
Required bool
Options chatloop.GenerateCompactionOptions
PendingUserRows []database.ChatMessage
}
type generationDebug struct {
Enabled bool
Service *chatdebug.Service
Provider string
Model string
TriggerMessageID int64
HistoryTipMessageID int64
TriggerLabel string
ModelConfig database.ChatModelConfig
}
// generationOutcome describes a completed generation outcome.
type generationOutcome struct {
Chat database.Chat
Kind runnerActionKind
LastError string
}
type generationActionKind string
const (
generationActionExecuteLocalTools generationActionKind = "execute_local_tools"
generationActionEnterRequiresAction generationActionKind = "enter_requires_action"
generationActionFinishTurn generationActionKind = "finish_turn"
generationActionCompact generationActionKind = "compact"
generationActionGenerateAssistant generationActionKind = "generate_assistant"
)
type generationFinishReason string
const (
generationFinishReasonStopAfterTool generationFinishReason = "stop_after_tool"
generationFinishReasonComplete generationFinishReason = "complete"
generationFinishReasonMaxSteps generationFinishReason = "max_steps"
)
var errCompactionStillOverLimit = xerrors.New("compaction left the chat above the compaction limit")
// compactionStillOverLimitError wraps errCompactionStillOverLimit with a
// message naming the settings that can clear the binding trigger.
// otherOverLimit reports whether the history also reaches the other
// enabled trigger's point, so neither setting clears the error alone.
func compactionStillOverLimitError(source compactionTriggerSource, otherOverLimit bool, thresholdPercent int32, contextLimit int64) error {
message := "Conversation compaction could not reduce the history below your compaction threshold. Raise the compaction threshold in settings, or start a new conversation."
switch {
case otherOverLimit:
message = fmt.Sprintf("Conversation compaction could not reduce the history below your compaction threshold or %d%% of the organization override's context window. Start a new conversation, or raise your compaction threshold in settings and ask an administrator to choose an override model with a larger context window.", compactionOverrideWindowPercent)
case source == compactionTriggerSourceOrganization:
message = fmt.Sprintf("Conversation compaction could not reduce the history below %d%% of the organization override's context window. Start a new conversation, or ask an administrator to choose an override model with a larger context window.", compactionOverrideWindowPercent)
}
return chaterror.WithClassification(
xerrors.Errorf("%s trigger at %d%% of %d tokens: %w", source, thresholdPercent, contextLimit, errCompactionStillOverLimit),
chaterror.ClassifiedError{
Message: message,
Kind: codersdk.ChatErrorKindConfig,
},
)
}
type generationDecision struct {
kind generationActionKind
localToolCalls []fantasy.ToolCallContent
// observedToolCalls is the step's batch for chatloop step observers
// and halting tools: localToolCalls plus the calls answered before
// execution, in call order.
observedToolCalls []fantasy.ToolCallContent
finishReason generationFinishReason
// forced marks a compact action triggered by a manual
// compaction request rather than the usage threshold.
forced bool
toolCallMessageID int64 // ID of the assistant message holding localToolCalls
}
type generationRetryDecision struct {
retry bool
generationAttempt int64
delay time.Duration
}
var errRetryStateDecisionOnly = xerrors.New("retry state decision only")
// errTerminalGeneration marks a prepare or decide failure as terminal: a
// deterministic error where retrying cannot help. The generation loop
// finishes the turn with an error instead of retrying when an error
// unwraps to this sentinel.
var errTerminalGeneration = xerrors.New("terminal generation error")
type terminalGenerationError struct{ err error }
func (e terminalGenerationError) Error() string { return e.err.Error() }
func (e terminalGenerationError) Unwrap() error { return errors.Join(errTerminalGeneration, e.err) }
// terminalGeneration wraps err so the prepare/decide retry loop stops
// immediately and finishes the turn with an error.
func terminalGeneration(err error) error {
if err == nil {
return nil
}
return terminalGenerationError{err: err}
}
func isTerminalGeneration(err error) bool {
return errors.Is(err, errTerminalGeneration)
}
type generationDecisionInput struct {
chat database.Chat
messages []database.ChatMessage
dynamicToolNames map[string]bool
exclusiveToolNames map[string]bool
stopAfterTools map[string]struct{}
maxSteps int
compactionEnabled bool
compactionNeeded bool
compactionThresholdPercent int32
compactionContextLimit int64
compactionTriggerSource compactionTriggerSource
compactionOtherTrigger compactionTrigger
}
func decideGenerationAction(input generationDecisionInput) (generationDecision, error) {
if input.maxSteps < 1 {
return generationDecision{}, terminalGeneration(xerrors.Errorf("max steps must be positive, got %d", input.maxSteps))
}
stepCalls, answered, messageID, err := lastStepToolCalls(input.messages)
if err != nil {
return generationDecision{}, err
}
localCalls, dynamicCalls := unansweredToolCalls(stepCalls, answered, input.dynamicToolNames)
if len(localCalls) > 0 {
runsDynamic := len(dynamicCalls) > 0 && hasExclusiveToolCall(localCalls, input.exclusiveToolNames)
if runsDynamic {
for _, dynamicCall := range dynamicCalls {
localCalls = append(localCalls, fantasy.ToolCallContent{
ToolCallID: dynamicCall.ToolCallID,
ToolName: dynamicCall.ToolName,
Input: dynamicCall.Args,
})
}
}
// The results of executed calls commit together, so a call
// answered before its batch runs was rejected without running,
// for example by a pre_tool_use hook or for malformed input.
// Pending dynamic calls stay out unless they run here, because
// the client runs them.
observedCalls := make([]fantasy.ToolCallContent, 0, len(stepCalls))
for _, call := range stepCalls {
if !answered[call.ToolCallID] && input.dynamicToolNames[call.ToolName] && !runsDynamic {
continue
}
observedCalls = append(observedCalls, call)
}
return generationDecision{
kind: generationActionExecuteLocalTools,
localToolCalls: localCalls,
observedToolCalls: observedCalls,
toolCallMessageID: messageID,
}, nil
}
if len(dynamicCalls) > 0 {
return generationDecision{kind: generationActionEnterRequiresAction}, nil
}
// A manual compaction request wins over every non-tool decision:
// idle chats would otherwise finish the turn via the
// history-complete check before ever compacting. The request is
// ignored when nothing after the latest boundary is compactable
// (for example the history was edited between request and
// execution); the stale marker is then cleared by the terminal
// transition of this turn.
if input.chat.CompactionRequestedAt.Valid {
boundary := latestContextBoundaryIndex(input.messages)
if _, ok := firstUncompressedAssistantAfter(input.messages, boundary); ok {
return generationDecision{kind: generationActionCompact, forced: true}, nil
}
}
stopAfter, err := historyHasStopAfterToolResult(input.messages, input.stopAfterTools)
if err != nil {
return generationDecision{}, err
}
if stopAfter {
return generationDecision{kind: generationActionFinishTurn, finishReason: generationFinishReasonStopAfterTool}, nil
}
complete, err := currentHistoryComplete(input.messages)
if err != nil {
return generationDecision{}, err
}
if complete {
return generationDecision{kind: generationActionFinishTurn, finishReason: generationFinishReasonComplete}, nil
}
if currentTurnStepCount(input.messages) >= input.maxSteps {
return generationDecision{kind: generationActionFinishTurn, finishReason: generationFinishReasonMaxSteps}, nil
}
compactionRequirement := compactionRequirementNotNeeded
if input.compactionEnabled && input.compactionNeeded {
compactionRequirement = compactionRequirementNeeded
}
switch compactionStatusFromHistory(input.messages, compactionRequirement, input.compactionThresholdPercent, input.compactionContextLimit) {
case compactionStatusNeeded:
return generationDecision{kind: generationActionCompact}, nil
case compactionStatusAfterCompaction:
return generationDecision{kind: generationActionGenerateAssistant}, nil
case compactionStatusStillOverLimit:
return generationDecision{}, terminalGeneration(compactionStillOverLimitError(
input.compactionTriggerSource,
postCompactionOverLimit(input.messages, input.compactionOtherTrigger),
input.compactionThresholdPercent,
input.compactionContextLimit,
))
case compactionStatusNotNeeded:
return generationDecision{kind: generationActionGenerateAssistant}, nil
default:
return generationDecision{}, terminalGeneration(xerrors.New("unknown compaction status"))
}
}
func generationCompactionThreshold(compaction *generationCompaction) int32 {
if compaction == nil {
return 0
}
return compaction.Options.ThresholdPercent
}
// generationCompactionContextLimit returns the binding trigger's context
// limit. The still-over-limit check must use it, or a binding override
// trigger compacts repeatedly instead of returning errCompactionStillOverLimit.
func generationCompactionContextLimit(compaction *generationCompaction) int64 {
if compaction == nil {
return 0
}
return compaction.Options.ContextLimit
}
func generationCompactionOtherTrigger(compaction *generationCompaction) compactionTrigger {
if compaction == nil {
return compactionTrigger{}
}
return compaction.OtherTrigger
}
func generationCompactionTriggerSource(compaction *generationCompaction) compactionTriggerSource {
if compaction == nil {
return compactionTriggerSourceChat
}
return compaction.TriggerSource
}
func unresolvedToolCallsFromHistory(
messages []database.ChatMessage,
dynamicToolNames map[string]bool,
) ([]fantasy.ToolCallContent, []pendingDynamicToolCall, int64, error) {
stepCalls, answered, messageID, err := lastStepToolCalls(messages)
if err != nil {
return nil, nil, 0, err
}
localCalls, dynamicCalls := unansweredToolCalls(stepCalls, answered, dynamicToolNames)
return localCalls, dynamicCalls, messageID, nil
}
// lastStepToolCalls returns the tool calls of the last assistant message that
// are not provider-executed, in call order, with the IDs of the calls already
// answered and the message ID.
func lastStepToolCalls(messages []database.ChatMessage) ([]fantasy.ToolCallContent, map[string]bool, int64, error) {
assistantIndex := lastMessageIndex(messages, func(msg database.ChatMessage) bool {
return msg.Role == database.ChatMessageRoleAssistant
})
if assistantIndex == -1 {
return nil, nil, 0, nil
}
assistantParts, err := chatprompt.ParseContent(messages[assistantIndex])
if err != nil {
return nil, nil, 0, xerrors.Errorf("parse assistant message: %w", err)
}
answered, err := handledToolCallIDs(messages[assistantIndex+1:])
if err != nil {
return nil, nil, 0, err
}
calls := make([]fantasy.ToolCallContent, 0, len(assistantParts))
for _, part := range assistantParts {
if part.Type != codersdk.ChatMessagePartTypeToolCall || part.ProviderExecuted {
continue
}
calls = append(calls, fantasy.ToolCallContent{
ToolCallID: part.ToolCallID,
ToolName: part.ToolName,
Input: string(part.Args),
})
}
return calls, answered, messages[assistantIndex].ID, nil
}
// unansweredToolCalls splits the calls not yet answered into local calls and
// dynamic calls, which the client runs.
func unansweredToolCalls(
calls []fantasy.ToolCallContent,
answered map[string]bool,
dynamicToolNames map[string]bool,
) ([]fantasy.ToolCallContent, []pendingDynamicToolCall) {
localCalls := make([]fantasy.ToolCallContent, 0)
dynamicCalls := make([]pendingDynamicToolCall, 0)
for _, call := range calls {
if answered[call.ToolCallID] {
continue
}
if dynamicToolNames[call.ToolName] {
dynamicCalls = append(dynamicCalls, pendingDynamicToolCall{
ToolCallID: call.ToolCallID,
ToolName: call.ToolName,
Args: call.Input,
})
continue
}
localCalls = append(localCalls, call)
}
return localCalls, dynamicCalls
}
// exclusiveBatchRejected reports whether the exclusive-tool policy will
// reject the whole batch, which mirrors the condition chatloop applies
// when it decides that nothing in the batch may execute. Callers must ask
// before filtering the batch, because dropping calls from it can leave the
// exclusive call alone and admissible.
func exclusiveBatchRejected(toolCalls []fantasy.ToolCallContent, exclusiveToolNames map[string]bool) bool {
return len(toolCalls) > 1 && hasExclusiveToolCall(toolCalls, exclusiveToolNames)
}
func hasExclusiveToolCall(toolCalls []fantasy.ToolCallContent, exclusiveToolNames map[string]bool) bool {
if len(exclusiveToolNames) == 0 {
return false
}
for _, toolCall := range toolCalls {
if exclusiveToolNames[toolCall.ToolName] {
return true
}
}
return false
}
type sessionStartResult struct {
Chat database.Chat
}
func applySessionStartResponse(
ctx context.Context,
machine *chatstate.ChatMachine,
input chatWorkerTaskStartInput,
chat database.Chat,
result *chathooks.Result,
) (sessionStartResult, error) {
if result.GetModelContext() == "" && result.GetUserMessage() == "" {
return sessionStartResult{Chat: chat}, nil
}
eventMessages, err := chathooks.EventMessages(result, chat.LastModelConfigID)
if err != nil {
return sessionStartResult{}, err
}
updated, err := machine.Update(ctx, func(tx *chatstate.Tx, _ database.Store, snapshot *chatstate.ChatSnapshot) error {
if _, err := verifyLockedGenerationFence(snapshot, input, generationAttemptNotRequired); err != nil {
return xerrors.Errorf("verify generation fence for session_start response: %w", err)
}
if len(eventMessages) > 0 {
if _, err := tx.CommitStep(chatstate.CommitStepInput{Snapshot: snapshot, Messages: eventMessages}); err != nil {
return xerrors.Errorf("insert session_start response messages: %w", err)
}
}
return nil
})
if err != nil {
return sessionStartResult{}, normalizeTaskTransitionError(err, "apply session_start response")
}
return sessionStartResult{Chat: updated}, nil
}
// syncMemoryIndex commits the model-only memory index message the chat
// needs, if any, and returns the reloaded chat when it committed one. See
// memoryIndexMessage for when a message is due.
func (s *taskStarter) syncMemoryIndex(
ctx context.Context,
machine *chatstate.ChatMachine,
input chatWorkerTaskStartInput,
chat database.Chat,
history []database.ChatMessage,
) (sessionStartResult, error) {
message, ok, err := s.server.memoryIndexMessage(ctx, chat, history)
if err != nil {
// Memory is optional context; a lookup failure must not fail the turn.
s.server.logger.Warn(ctx, "failed to sync project memory index", slog.F("chat_id", chat.ID), slog.Error(err))
return sessionStartResult{}, nil
}
if !ok {
return sessionStartResult{}, nil
}
updated, err := machine.Update(ctx, func(tx *chatstate.Tx, _ database.Store, snapshot *chatstate.ChatSnapshot) error {
if _, err := verifyLockedGenerationFence(snapshot, input, generationAttemptNotRequired); err != nil {
return xerrors.Errorf("verify generation fence for memory index: %w", err)
}
if _, err := tx.CommitStep(chatstate.CommitStepInput{Snapshot: snapshot, Messages: []chatstate.Message{message}}); err != nil {
return xerrors.Errorf("insert memory index message: %w", err)
}
return nil
})
if err != nil {
return sessionStartResult{}, normalizeTaskTransitionError(err, "apply memory index")
}
return sessionStartResult{Chat: updated}, nil
}
func (s *taskStarter) startGenerationSession(
ctx context.Context,
machine *chatstate.ChatMachine,
input chatWorkerTaskStartInput,
chat database.Chat,
messages []database.ChatMessage,
) (result sessionStartResult, dispatched bool, err error) {
dispatched, complete, err := input.SessionStart.claim(ctx)
if err != nil {
return sessionStartResult{}, false, errors.Join(errTaskExpectedExit, xerrors.Errorf("claim session_start: %w", err))
}
if !dispatched {
return sessionStartResult{Chat: chat}, false, nil
}
completed := false
// Re-arm the claim until its response is applied so a replacement task
// can replay session_start effects.
defer func() { complete(completed) }()
response, err := s.server.hooks.Trigger(ctx, chathooks.ChatFor(chat, input.hookTurnID()), chathooks.Message{Source: chathooks.SessionStartSource(messages)}, agenthooks.EventSessionStart, dispatch.CapacityClassGeneration)
if err != nil {
return sessionStartResult{}, true, chathooks.GenerationDispatchError(agenthooks.EventSessionStart, err)
}
result, err = applySessionStartResponse(ctx, machine, input, chat, response)
if err != nil {
return sessionStartResult{}, true, err
}
completed = true
return result, true, nil
}
func (s *taskStarter) StartGeneration(ctx context.Context, input chatWorkerTaskStartInput) error {
if input.StopNudges == nil {
input.StopNudges = &stopNudgeTracker{}
}
if input.TurnID == uuid.Nil {
input.TurnID = uuid.New()
}
machine := chatstate.NewChatMachine(s.opts.Store, s.opts.Pubsub, input.ChatID)
deletedToolCallsChecked := false
for {
chat, messages, err := loadGenerationState(ctx, machine, input)
if err != nil {
return xerrors.Errorf("load generation state: %w", err)
}
if !deletedToolCallsChecked {
deletedToolCallsChecked = true
if currentTurnStepCount(messages) == 0 {
if err := s.cancelDeletedToolCalls(ctx, machine, input, chat, messages); err != nil {
return err
}
}
}
var turnCtx context.Context
turnCtx, input.TurnToken = input.TurnSpan.Ensure(ctx, input.TaskID, chat, turnTriggerTime(chat, messages))
var again bool
input, again, err = s.runGenerationStep(turnCtx, machine, input, chat, messages)
if again {
continue
}
// After the step's stage ends so the turn includes it.
input.TurnSpan.Settle(input.TurnToken)
return err
}
}
// cancelDeletedToolCalls sends the chat's agent a cancel request for each
// unresolved call in the last deleted assistant message between the turn's
// user message and the previous user message, if chattool.CanCancelToolCall
// accepts the call. Their results can no longer be committed. Agent request
// failures are logged and do not affect the turn.
func (s *taskStarter) cancelDeletedToolCalls(
ctx context.Context,
machine *chatstate.ChatMachine,
input chatWorkerTaskStartInput,
chat database.Chat,
messages []database.ChatMessage,
) error {
if s.server.agentConnFn == nil || !chat.AgentID.Valid {
return nil
}
userMessageIndex := lastUserPromptIndex(messages)
if userMessageIndex == -1 {
return nil
}
// EditMessage marks the edited user message and every later message as
// deleted, so they lie between the previous user message and this one.
params := database.GetDeletedChatMessagesFromLastAssistantParams{
ChatID: chat.ID,
UserMessageID: messages[userMessageIndex].ID,
}
if previous := lastUserPromptIndex(messages[:userMessageIndex]); previous != -1 {
params.PreviousUserMessageID = messages[previous].ID
}
var deleted []database.ChatMessage
err := machine.ReadSnapshot(func(store database.Store) error {
if _, err := loadChatForTask(ctx, store, input, database.ChatStatusRunning, taskFenceOptions{requireHistory: true}); err != nil {
return xerrors.Errorf("load chat for task: %w", err)
}
var err error
deleted, err = store.GetDeletedChatMessagesFromLastAssistant(ctx, params)
return err
})
if err != nil {
return normalizeTaskInfrastructureError(err, "load deleted messages to cancel tool calls on agent")
}
// The shared filter skips deleted rows, and all of these are deleted.
for i := range deleted {
deleted[i].Deleted = false
}
calls, ids := s.cancelableToolCallsFromHistory(ctx, chat, deleted)
s.cancelUnresolvedToolCalls(ctx, chat, calls, ids)
return nil
}
// runGenerationStep runs one step of a turn. again means reload state
// and run another step.
func (s *taskStarter) runGenerationStep(
ctx context.Context,
machine *chatstate.ChatMachine,
input chatWorkerTaskStartInput,
chat database.Chat,
messages []database.ChatMessage,
) (next chatWorkerTaskStartInput, again bool, err error) {
ctx, stepSpan := s.server.stages.Start(ctx, chatloop.StageGenerationStep,
attribute.String(chatloop.AttrChatID, input.ChatID.String()),
)
defer func() { stepSpan.End(err) }()
if s.server.hooks.Enabled() {
result, dispatched, err := s.startGenerationSession(ctx, machine, input, chat, messages)
if err != nil {
if errors.Is(err, errTaskExpectedExit) {
return input, false, err
}
return input, false, s.finishGenerationError(ctx, machine, input, err, generationAttemptNotRequired)
}
if dispatched {
input.HistoryVersion = result.Chat.HistoryVersion
return input, true, nil
}
}
if synced, err := s.syncMemoryIndex(ctx, machine, input, chat, messages); err != nil {
return input, false, err
} else if synced.Chat.ID != uuid.Nil {
input.HistoryVersion = synced.Chat.HistoryVersion
return input, true, nil
}
prepareInput := generationPrepareInput{
Chat: chat,
Messages: messages,
RecordMCPConnectSummaries: input.DebugTurn.RecordMCPConnectSummaries,
RecordStageModel: func(model chatloop.StageModel) {
input.TurnSpan.SetProvider(input.TurnToken, model)
},
}
prepareCtx, prepareSpan := s.server.stages.Start(ctx, chatloop.StagePrepare)
prepared, err := retryGenerationPhase(prepareCtx, s, "prepare", func() (turnEnvironment, error) {
return s.server.buildTurnEnvironment(prepareCtx, prepareInput)
})
if err == nil {
prepareSpan.SetModel(prepared.StageModel)
stepSpan.SetModel(prepared.StageModel)
}
prepareSpan.End(err)
if err != nil {
if errors.Is(err, errTaskExpectedExit) || errors.Is(err, errTaskRetryable) {
return input, false, xerrors.Errorf("prepare generation: %w", err)
}
return input, false, s.finishGenerationError(ctx, machine, input, err, generationAttemptNotRequired)
}
cleanup := prepared.Close
var decision generationDecision
if input.StopNudges.consume(stopNudgeKey(prepared.Messages)) {
decision = generationDecision{kind: generationActionGenerateAssistant}
} else {
decision, err = retryGenerationPhase(ctx, s, "decide", func() (generationDecision, error) {
return decideGenerationAction(generationDecisionInput{
chat: prepared.Chat,
messages: prepared.Messages,
dynamicToolNames: prepared.DynamicToolNames,
exclusiveToolNames: prepared.ExclusiveToolNames,
stopAfterTools: prepared.StopAfterTools,
maxSteps: prepared.MaxSteps,
compactionEnabled: prepared.Compaction != nil,
compactionNeeded: prepared.Compaction != nil && prepared.Compaction.Required,
compactionThresholdPercent: generationCompactionThreshold(prepared.Compaction),
compactionContextLimit: generationCompactionContextLimit(prepared.Compaction),
compactionTriggerSource: generationCompactionTriggerSource(prepared.Compaction),
compactionOtherTrigger: generationCompactionOtherTrigger(prepared.Compaction),
})
})
}
if err != nil {
cleanup()
if errors.Is(err, errTaskExpectedExit) || errors.Is(err, errTaskRetryable) {
return input, false, xerrors.Errorf("decide generation: %w", err)
}
if errors.Is(err, errCompactionStillOverLimit) && prepared.Compaction != nil {
metricProvider, metricModel := compactionMetricIdentity(prepared.Compaction)
s.server.metrics.RecordCompaction(
metricProvider,
metricModel,
false,
errCompactionStillOverLimit,
)
}
return input, false, s.finishGenerationError(ctx, machine, input, err, generationAttemptNotRequired)
}
stepSpan.SetAttributes(attribute.String(chatloop.AttrGenerationAction, string(decision.kind)))
var actionErr error
switch decision.kind {
case generationActionEnterRequiresAction:
cleanup()
return input, false, s.enterRequiresAction(ctx, machine, input)
case generationActionFinishTurn:
cleanup()
return input, false, s.finishGenerationTurn(ctx, machine, input, generationAttemptNotRequired)
case generationActionGenerateAssistant:
actionErr = s.generateAssistant(ctx, machine, input, prepared)
case generationActionExecuteLocalTools:
actionErr = s.executeLocalTools(ctx, machine, input, prepared, decision)
case generationActionCompact:
actionErr = s.generateCompaction(ctx, machine, input, prepared, compactionSourceForDecision(decision))
default:
return input, false, s.finishGenerationError(ctx, machine, input, xerrors.Errorf("unknown generation action %q", decision.kind), generationAttemptNotRequired)
}
cleanup()
if actionErr == nil {
return input, false, nil
}
// Task cancellation is handled by the runner, not here.
if ctx.Err() != nil && errors.Is(actionErr, context.Canceled) {
return input, false, errors.Join(errTaskExpectedExit, xerrors.Errorf("generation action: %w", actionErr), ctx.Err())
}
if errors.Is(actionErr, errTaskExpectedExit) {
return input, false, xerrors.Errorf("generation action: %w", actionErr)
}
classified := chaterror.Classify(actionErr)
if classified.Retryable {
action := decision.kind
decision, err := s.recordGenerationRetry(ctx, machine, input, classified)
if err != nil {
return input, false, xerrors.Errorf("record generation retry: %w", err)
}
if decision.retry {
s.opts.Logger.Warn(ctx, "chat generation retrying",
slog.F("chat_id", input.ChatID),
slog.F("worker_id", input.WorkerID),
slog.F("action", action),
slog.F("generation_attempt", decision.generationAttempt),
slog.F("delay", decision.delay),
slog.F("error_kind", classified.Kind),
slog.F("provider", classified.Provider),
slog.F("status_code", classified.StatusCode),
slogError(actionErr),
)
if err := s.waitGenerationRetry(ctx, decision.delay); err != nil {
return input, false, xerrors.Errorf("wait generation retry: %w", err)
}
return input, true, nil
}
return input, false, s.finishGenerationError(ctx, machine, input, actionErr, requireGenerationAttempt(decision.generationAttempt))
}
return input, false, s.finishGenerationError(ctx, machine, input, actionErr, generationAttemptNotRequired)
}
func loadGenerationState(
ctx context.Context,
machine *chatstate.ChatMachine,
input chatWorkerTaskStartInput,
) (database.Chat, []database.ChatMessage, error) {
var chat database.Chat
var messages []database.ChatMessage
err := machine.ReadSnapshot(func(store database.Store) error {
loadedChat, err := loadChatForTask(ctx, store, input, database.ChatStatusRunning, taskFenceOptions{requireHistory: true})
if err != nil {
return xerrors.Errorf("load chat for task: %w", err)
}
loadedMessages, err := store.GetChatMessagesByChatID(ctx, database.GetChatMessagesByChatIDParams{
ChatID: input.ChatID,
AfterID: 0,
})
if err != nil {
return xerrors.Errorf("load chat messages: %w", err)
}
chat = loadedChat
messages = loadedMessages
return nil
})
if err != nil {
return database.Chat{}, nil, normalizeTaskInfrastructureError(err, "lock chat for generation")
}
return chat, messages, nil
}
func (s *taskStarter) recordGenerationRetry(
ctx context.Context,
machine *chatstate.ChatMachine,
input chatWorkerTaskStartInput,
classified chaterror.ClassifiedError,
) (generationRetryDecision, error) {
var decision generationRetryDecision
var payload *codersdk.ChatStreamRetry
_, err := machine.Update(ctx, func(tx *chatstate.Tx, _ database.Store, snapshot *chatstate.ChatSnapshot) error {
chat, err := verifyLockedTaskFence(snapshot, input, database.ChatStatusRunning, taskFenceOptions{requireHistory: true})
if err != nil {
return xerrors.Errorf("verify task fence: %w", err)
}
decision.generationAttempt = chat.GenerationAttempt
if chat.GenerationAttempt <= 0 || chat.GenerationAttempt > int64(s.server.chatLimits.MaxGenerationRetries) {
decision.retry = false
return errRetryStateDecisionOnly
}
attempt := int(chat.GenerationAttempt)
delay := chatretry.Delay(attempt - 1)
if classified.RetryAfter > delay {
delay = classified.RetryAfter
}
decision.retry = true
decision.delay = delay
payload = chaterror.StreamRetryPayload(attempt, delay, classified)
if payload == nil {
return errRetryStateDecisionOnly
}
encoded, err := json.Marshal(payload)
if err != nil {
return xerrors.Errorf("marshal retry state: %w", err)
}
_, err = tx.RecordRetryState(chatstate.RecordRetryStateInput{
Snapshot: snapshot,
RetryState: pqtype.NullRawMessage{RawMessage: encoded, Valid: true},
})
if err != nil {
return xerrors.Errorf("record retry state: %w", err)
}
return nil
})
if errors.Is(err, errRetryStateDecisionOnly) {
return decision, nil
}
if err != nil {
return generationRetryDecision{}, normalizeTaskTransitionError(err, "record retry state")
}
return decision, nil
}
func (s *taskStarter) waitGenerationRetry(ctx context.Context, delay time.Duration) error {
_, span := s.server.stages.Start(ctx, chatloop.StageRetryBackoff)
timer := s.opts.Clock.NewTimer(delay, "chatworker", "generation-retry")
defer timer.Stop()
select {
case <-timer.C:
span.End(nil)
return nil
case <-ctx.Done():
err := errors.Join(errTaskExpectedExit, xerrors.Errorf("wait generation retry: %w", ctx.Err()))
span.End(err)
return err
}
}
const (
// generationPhaseMaxAttempts bounds how many times buildTurnEnvironment
// and decideGenerationAction run before the turn finishes with an
// error. Both phases are retried because buildTurnEnvironment performs
// I/O (DB reads, MCP connects, workspace dials) that can fail
// transiently.
generationPhaseMaxAttempts = 3
// generationPhaseBaseBackoff is the delay before the first retry. It
// doubles on each subsequent attempt.
generationPhaseBaseBackoff = 200 * time.Millisecond
)
func generationPhaseBackoff(attempt int) time.Duration {
d := generationPhaseBaseBackoff
for range attempt {
d *= 2
}
return d
}
// retryGenerationPhase runs fn up to generationPhaseMaxAttempts times. It
// returns early on success or on a terminal error (see terminalGeneration).
// Non-terminal errors are retried with exponential backoff. Context
// cancellation returns errTaskExpectedExit so shutdown does not write an
// error state. Task timeouts return a retryable task error so the runner can
// start a fresh attempt. When every attempt fails, the last error is returned.
func retryGenerationPhase[T any](ctx context.Context, starter *taskStarter, phase string, fn func() (T, error)) (T, error) {
var zero T
var lastErr error
for attempt := 0; attempt < generationPhaseMaxAttempts; attempt++ {
result, err := fn()
if err == nil {
return result, nil
}
if isTerminalGeneration(err) {
return zero, xerrors.Errorf("retryGenerationPhase terminal error: %w", err)
}
if ctx.Err() != nil {
return zero, errors.Join(errTaskExpectedExit, xerrors.Errorf("retryGenerationPhase %s: %w", phase, ctx.Err()))
}
lastErr = err
if attempt < generationPhaseMaxAttempts-1 {
delay := generationPhaseBackoff(attempt)
starter.opts.Logger.Warn(ctx, "chat generation phase retrying",
slog.F("phase", phase),
slog.F("attempt", attempt+1),
slog.F("max_attempts", generationPhaseMaxAttempts),
slog.F("delay", delay),
slogError(err),
)
if waitErr := starter.waitGenerationPhaseBackoff(ctx, delay); waitErr != nil {
return zero, waitErr
}
}
}
return zero, lastErr
}
func (s *taskStarter) waitGenerationPhaseBackoff(ctx context.Context, delay time.Duration) error {
timer := s.opts.Clock.NewTimer(delay, "chatworker", "generation-phase-retry")
defer timer.Stop()
select {
case <-timer.C:
return nil
case <-ctx.Done():
return errors.Join(errTaskExpectedExit, xerrors.Errorf("wait generation phase backoff: %w", ctx.Err()))
}
}
func (s *taskStarter) generateAssistant(
ctx context.Context,
machine *chatstate.ChatMachine,
input chatWorkerTaskStartInput,
prepared turnEnvironment,
) error {
attempt, delivered, err := s.beginAssistantGenerationAttempt(ctx, machine, input)
if err != nil {
return xerrors.Errorf("begin assistant generation attempt: %w", err)
}
if delivered {
return nil
}
defer attempt.closeEpisode()
runCtx := input.DebugTurn.Ensure(ctx, prepared.Chat, prepared.Debug)
outcome, err := chatloop.GenerateAssistant(runCtx, chatloop.GenerateAssistantOptions{
Model: prepared.Model.LanguageModel(),
ErrorProvider: prepared.ResolvedProvider,
Messages: prepared.Prompt,
Tools: prepared.Tools,
ActiveTools: prepared.ActiveTools,
ProviderTools: prepared.ProviderTools,
ContextLimitFallback: prepared.ContextLimitFallback,
CallTemplate: prepared.CallTemplate,
PublishMessagePart: attempt.publish,
OnModelStreamStart: attempt.startModelInvocation,
StreamSilenceTimeout: s.server.streamSilenceTimeout,
Logger: s.opts.Logger,
Clock: s.opts.Clock,
Metrics: s.server.metrics,
Stages: s.server.stages,
StageModel: prepared.StageModel,
})
if err != nil {
return xerrors.Errorf("generate assistant: %w", err)
}
s.recordThinkingStages(runCtx, prepared, outcome.Step)
if len(outcome.Step.Content) == 0 {
return s.finishGenerationTurn(ctx, machine, input, requireGenerationAttempt(attempt.number))
}
preflight, err := s.admitStepToolCalls(ctx, input, prepared, outcome.Step.Content)
if err != nil {
return err
}
outcome.Step.Content = chathooks.ApplyAdmittedToolCalls(outcome.Step.Content, preflight)
messages, err := buildCommitStepMessages(buildCommitStepMessagesInput{
modelConfigID: prepared.ModelConfigID,
step: stepDataFromPersisted(outcome.Step),
toolNameToConfigID: prepared.ToolNameToConfigID,
logger: s.opts.Logger,
contentVersion: chatprompt.CurrentContentVersion,
hookRewrittenToolCalls: preflight.Overrides,
reasoningProvenance: prepared.stepReasoningProvenance(),
})
if err != nil {
return s.finishGenerationError(ctx, machine, input, err, requireGenerationAttempt(attempt.number))
}
messages, err = appendHookResultMessages(messages, preflight.Results, prepared.ModelConfigID)
if err != nil {
return s.finishGenerationError(ctx, machine, input, err, requireGenerationAttempt(attempt.number))
}
return s.commitGenerationStep(ctx, machine, input, attempt.number, generationActionGenerateAssistant, messages, generationCommitHooks{})
}
// recordThinkingStages pairs reasoning start and completion timestamps
// by index.
func (s *taskStarter) recordThinkingStages(
ctx context.Context,
prepared turnEnvironment,
step chatloop.PersistedStep,
) {
for index, startedAt := range step.ReasoningStartedAt {
if index >= len(step.ReasoningCompletedAt) {
return
}
s.server.stages.Record(
ctx,
chatloop.StageThinking,
prepared.StageModel,
startedAt,
step.ReasoningCompletedAt[index],
nil,
)
}
}
func (s *taskStarter) admitStepToolCalls(
ctx context.Context,
input chatWorkerTaskStartInput,
prepared turnEnvironment,
content []fantasy.Content,
) (chathooks.PreToolUseExecutionResult, error) {
// Validate even without hooks: JSONB persistence discards duplicate keys,
// so execution cannot detect them after the assistant step is committed.
pending := chathooks.PendingToolCalls(content)
toolCalls := withoutResolvedToolCalls(content, pending)
if len(pending) == 0 || exclusiveBatchRejected(toolCalls, prepared.ExclusiveToolNames) {
return chathooks.PreToolUseExecutionResult{}, nil
}
// An admission error discards the whole batch before it can be
// committed, so its find_tools calls would otherwise never reach
// the executeLocalTools counter; count them at each error exit.
countBatch := func() {
if !prepared.BuiltinToolNames[chattool.FindToolsName] {
return
}
for _, toolCall := range toolCalls {
if toolCall.ToolName == chattool.FindToolsName {
s.server.metrics.FindToolsCallsTotal.Inc()
}
}
}
// Check the full batch first: a call removed below or already resolved
// still occupies its ID in the step, so filtering before this would hide
// the collision.
if err := chathooks.RejectDuplicateToolUseIDs(pending); err != nil {
countBatch()
return chathooks.PreToolUseExecutionResult{}, chathooks.GenerationDispatchError(agenthooks.EventPreToolUse, err)
}
valid, _, invalid := partitionInvalidToolCalls(prepared, toolCalls)
preflight, err := s.server.hooks.PreflightPendingToolCalls(ctx, chathooks.ChatFor(prepared.Chat, input.hookTurnID()), valid)
if err != nil {
countBatch()
return chathooks.PreToolUseExecutionResult{}, chathooks.GenerationDispatchError(agenthooks.EventPreToolUse, err)
}
if err := validateOverriddenToolInputs(prepared, preflight); err != nil {
countBatch()
return chathooks.PreToolUseExecutionResult{}, chathooks.GenerationDispatchError(agenthooks.EventPreToolUse, err)
}
preflight.Denied = append(preflight.Denied, invalid...)
// Calls denied at admission persist synthetic results with the
// assistant step, so they never surface as unresolved calls where
// executeLocalTools counts find_tools invocations; count them here.
if prepared.BuiltinToolNames[chattool.FindToolsName] {
for _, result := range preflight.Denied {
if result.ToolName == chattool.FindToolsName {
s.server.metrics.FindToolsCallsTotal.Inc()
}
}
}
return preflight, nil
}
// withoutResolvedToolCalls drops calls the step already answered with a result,
// such as calls cut off by the output token limit. Execution skips them the
// same way, so admission must not dispatch or deny them again.
func withoutResolvedToolCalls(content []fantasy.Content, toolCalls []fantasy.ToolCallContent) []fantasy.ToolCallContent {
resolved := make(map[string]bool)
for _, block := range content {
if toolResult, ok := asToolResultContent(block); ok {
resolved[toolResult.ToolCallID] = true
}
}
if len(resolved) == 0 {
return toolCalls
}
unresolved := make([]fantasy.ToolCallContent, 0, len(toolCalls))
for _, toolCall := range toolCalls {
if !resolved[toolCall.ToolCallID] {
unresolved = append(unresolved, toolCall)
}
}
return unresolved
}
// bufferToolBillingRecorder translates a dispatch index in the filtered batch
// back to the unresolved-call position the buffer episode keys timings by.
type bufferToolBillingRecorder struct {
allowedIndexes []int
recordStart func(callIndex int, startedAt time.Time)
recordComplete func(callIndex int, completedAt time.Time)
}
func (r *bufferToolBillingRecorder) RecordStart(dispatchIndex int, startedAt time.Time) {
if dispatchIndex < 0 || dispatchIndex >= len(r.allowedIndexes) {
return
}
r.recordStart(r.allowedIndexes[dispatchIndex], startedAt)
}
func (r *bufferToolBillingRecorder) RecordComplete(dispatchIndex int, completedAt time.Time) {
if dispatchIndex < 0 || dispatchIndex >= len(r.allowedIndexes) {
return
}
r.recordComplete(r.allowedIndexes[dispatchIndex], completedAt)
}
func (s *taskStarter) executeLocalTools(
ctx context.Context,
machine *chatstate.ChatMachine,
input chatWorkerTaskStartInput,
prepared turnEnvironment,
decision generationDecision,
) error {
exclusiveRejected := exclusiveBatchRejected(decision.localToolCalls, prepared.ExclusiveToolNames)
allowed := decision.localToolCalls
var allowedIndexes []int
var denied []fantasy.ToolResultContent
if !exclusiveRejected {
allowed, allowedIndexes, denied = partitionInvalidToolCalls(prepared, decision.localToolCalls)
}
// find_tools calls are counted here, at the single point every
// model-emitted call passes through, because rejections upstream of
// the tool (partition denials, hook denials, exclusive-policy
// batches) never reach its handler or OnCall.
if prepared.BuiltinToolNames[chattool.FindToolsName] {
for _, toolCall := range decision.localToolCalls {
if toolCall.ToolName == chattool.FindToolsName {
s.server.metrics.FindToolsCallsTotal.Inc()
}
}
}
attempt, err := s.beginGenerationAttempt(ctx, machine, input)
if err != nil {
return xerrors.Errorf("beginGenerationAttempt: %w", err)
}
defer attempt.closeEpisode()
provider := ""
modelName := ""
if prepared.Model.Valid() {
provider = prepared.Model.Provider()
modelName = prepared.Model.ModelID()
}
var outcome chatloop.PersistedStep
var spawnDispatchErr error
if len(allowed) > 0 {
var billingRecorder chatloop.ToolBillingRecorder
if !exclusiveRejected {
billingRecorder = &bufferToolBillingRecorder{
allowedIndexes: allowedIndexes,
recordStart: attempt.recordToolStart,
recordComplete: attempt.recordToolCompletion,
}
}
toolCallIDs := chattool.ToolCallIDs(input.ChatID, decision.toolCallMessageID, decision.localToolCalls)
outcome, err = chatloop.ExecuteLocalTools(ctx, chatloop.ExecuteLocalToolsOptions{
Tools: prepared.Tools,
ActiveTools: prepared.ActiveTools,
AllowInactiveTools: prepared.AllowInactiveTools,
ProviderTools: prepared.ProviderTools,
ToolCalls: allowed,
ObservedToolCalls: decision.observedToolCalls,
ExclusiveToolNames: prepared.ExclusiveToolNames,
BuiltinToolNames: prepared.BuiltinToolNames,
ModelProvider: provider,
ModelName: modelName,
ContextLimit: prepared.ContextLimitFallback,
UnbilledToolNames: unbilledSubagentToolNames,
BillingRecorder: billingRecorder,
ToolCallContext: func(ctx context.Context, tc fantasy.ToolCallContent) context.Context {
if id, ok := toolCallIDs[tc.ToolCallID]; ok {
return workspacesdk.WithToolCallID(ctx, id)
}
return ctx
},
Stages: s.server.stages,
StageModel: prepared.StageModel,
PublishMessagePart: attempt.publish,
Logger: s.opts.Logger,
Metrics: s.server.metrics,
Clock: s.opts.Clock,
})
if err != nil {
return xerrors.Errorf("execute local tools: %w", err)
}
// Subagent spawn admission dispatches user_prompt_submit inside
// the tool run; its failure surfaces as a tool result error. The
// step still commits so a sibling tool that already ran keeps its
// result and is not re-executed, and the turn fails afterwards.
if hookErr := chathooks.DispatchFailureFromResults(outcome.Content); hookErr != nil {
spawnDispatchErr = chathooks.GenerationDispatchError(agenthooks.EventUserPromptSubmit, hookErr)
}
}
postResults, postDispatchErr := s.server.hooks.PostToolUseResults(ctx, chathooks.ChatFor(prepared.Chat, input.hookTurnID()), outcome.Content)
for _, result := range denied {
outcome.Content = append(outcome.Content, result)
}
chathooks.RestoreToolCallOrder(outcome.Content, decision.localToolCalls)
step := stepDataFromPersisted(outcome)
messages, err := buildCommitStepMessages(buildCommitStepMessagesInput{
modelConfigID: prepared.ModelConfigID,
step: step,
toolNameToConfigID: prepared.ToolNameToConfigID,
logger: s.opts.Logger,
contentVersion: chatprompt.CurrentContentVersion,
reasoningProvenance: prepared.stepReasoningProvenance(),
})
if err != nil {
return s.finishGenerationError(ctx, machine, input, err, requireGenerationAttempt(attempt.number))
}
messages, err = appendHookResultMessages(messages, postResults, prepared.ModelConfigID)
if err != nil {
return s.finishGenerationError(ctx, machine, input, err, requireGenerationAttempt(attempt.number))
}
var postCommitErr error
switch {
case spawnDispatchErr != nil:
// Spawn admission is the causal root: post_tool_use only ran
// because the batch reached execution at all.
postCommitErr = spawnDispatchErr
if postDispatchErr != nil {
s.opts.Logger.Warn(ctx, "post_tool_use hook dispatch failed alongside spawn admission",
slog.F("chat_id", input.ChatID),
slog.F("worker_id", input.WorkerID),
slog.Error(postDispatchErr),
)
}
case postDispatchErr != nil:
postCommitErr = chathooks.GenerationDispatchError(agenthooks.EventPostToolUse, postDispatchErr)
}
return s.commitGenerationStep(ctx, machine, input, attempt.number, generationActionExecuteLocalTools, messages, generationCommitHooks{
PostCommitError: postCommitErr,
})
}
// compactionSourceForDecision maps a compact decision to the
// compaction source recorded in the summary messages. Manual
// requests also force the compaction past the usage-threshold gates.
func compactionSourceForDecision(decision generationDecision) chatloop.CompactionSource {
if decision.forced {
return chatloop.CompactionSourceManual
}
return chatloop.CompactionSourceAutomatic
}
func (s *taskStarter) generateCompaction(
ctx context.Context,
machine *chatstate.ChatMachine,
input chatWorkerTaskStartInput,
prepared turnEnvironment,
source chatloop.CompactionSource,
) error {
attempt, err := s.beginGenerationAttempt(ctx, machine, input)
if err != nil {
return xerrors.Errorf("beginGenerationAttempt: %w", err)
}
defer attempt.closeEpisode()
if prepared.Compaction == nil {
return s.finishGenerationError(ctx, machine, input, xerrors.New("compaction action missing options"), requireGenerationAttempt(attempt.number))
}
compactionOpts := prepared.Compaction.Options
metricProvider, metricModel := compactionMetricIdentity(prepared.Compaction)
compactionModel := prepared.StageModel
logger := s.server.logger.With(
slog.F("chat_id", prepared.Chat.ID),
slog.F("owner_id", prepared.Chat.OwnerID),
)
if override := prepared.Compaction.Override; override != nil {
// A usable override that fails to build is a hard generation failure.
overrideModel, err := s.server.resolveModelCall(ctx, modelCallSpec{
purpose: "compaction",
chat: prepared.Chat,
explicitConfig: &override.Config,
requestedEffort: override.ReasoningEffort,
chatdScopedRoute: true,
buildOptions: prepared.ModelBuildOptions,
})
if err != nil {
return xerrors.Errorf("build compaction model override: %w", err)
}
compactionModel = overrideModel.stageModel()
compactionOpts.Model = overrideModel.model.LanguageModel()
compactionOpts.ResolvedProvider = overrideModel.resolvedProvider
compactionOpts.ResolvedModel = overrideModel.resolvedModel
compactionOpts.ModelConfigID = overrideModel.dbConfig.ID
compactionOpts.SummaryContextLimit = override.Config.ContextLimit
compactionOpts.SummaryCall = compactionSummaryCall(overrideModel)
// Prompt caches are model-scoped and provider-native tools are
// model-specific, so unless the override resolves to the chat model
// itself its definitions buy the request nothing and can get it
// rejected.
if !sameCompactionProviderIdentity(prepared.Compaction.ChatModelConfig, overrideModel.dbConfig) ||
overrideModel.resolvedModel != prepared.Compaction.Options.ResolvedModel {
compactionOpts.ToolDefinitions = nil
}
compactionOpts.Messages = sanitizeCompactionPrompt(
ctx,
logger,
compactionOpts.Messages,
overrideModel.model,
prepared.Compaction.ChatModelConfig,
overrideModel.dbConfig,
prepared.ReasoningProvenance.Model,
)
}
preResult, err := s.server.hooks.Trigger(ctx, chathooks.ChatFor(prepared.Chat, input.hookTurnID()), chathooks.Message{}, agenthooks.EventPreCompact, dispatch.CapacityClassGeneration)
if err != nil {
return chathooks.GenerationDispatchError(agenthooks.EventPreCompact, err)
}
compactionOpts.SummaryHint = preResult.GetModelContext()
compactionOpts.PublishMessagePart = attempt.publish
compactionOpts.OnModelStreamStart = attempt.startModelInvocation
compactionOpts.Source = source
compactionOpts.Force = source == chatloop.CompactionSourceManual
compactionOpts.Clock = s.opts.Clock
compactionOpts.Logger = logger
// Attach the turn debug run so the compaction call records a child
// debug run; without it startCompactionDebugRun finds no parent and
// skips debug instrumentation entirely.
runCtx := input.DebugTurn.Ensure(ctx, prepared.Chat, prepared.Debug)
compactionCtx, compactionSpan := s.server.stages.Start(runCtx, chatloop.StageCompaction,
attribute.String(chatloop.AttrCompactionSource, string(source)),
)
compactionSpan.SetModel(compactionModel)
compactionOpts.Stages = s.server.stages
compactionOpts.StageModel = compactionModel
outcome, err := chatloop.GenerateCompaction(compactionCtx, compactionOpts)
compactionSpan.End(err)
if err != nil {
s.server.metrics.RecordCompaction(metricProvider, metricModel, false, err)
return xerrors.Errorf("generate compaction: %w", err)
}
if strings.TrimSpace(outcome.SystemSummary) == "" || strings.TrimSpace(outcome.SummaryReport) == "" {
err := xerrors.New("compaction produced no summary")
s.server.metrics.RecordCompaction(metricProvider, metricModel, false, err)
return s.finishGenerationError(ctx, machine, input, err, requireGenerationAttempt(attempt.number))
}
messages, err := buildCompactionMessages(buildCompactionMessagesInput{
modelConfigID: prepared.ModelConfigID,
toolCallID: compactionOpts.ToolCallID,
toolName: compactionOpts.ToolName,
compaction: outcome,
contentVersion: chatprompt.CurrentContentVersion,
pendingUserMessages: prepared.Compaction.PendingUserRows,
})
if err != nil {
s.server.metrics.RecordCompaction(metricProvider, metricModel, false, err)
return s.finishGenerationError(ctx, machine, input, err, requireGenerationAttempt(attempt.number))
}
// The summary hint already consumed the pre_compact model context.
persistedPreResult := &chathooks.Result{UserMessage: preResult.GetUserMessage()}
commitMessages, err := applyHookResultMessages(stepMessagesForCommit{
Messages: messages.Messages,
ConsumeCompactionRequest: true,
}, []*chathooks.Result{persistedPreResult}, prepared.ModelConfigID)
if err != nil {
s.server.metrics.RecordCompaction(metricProvider, metricModel, false, err)
return s.finishGenerationError(ctx, machine, input, err, requireGenerationAttempt(attempt.number))
}
// Hook effects and fail-closed errors must commit atomically with
// compaction; a separate commit races the runner and can be dropped
// on crash.
postResult, postDispatchErr := s.server.hooks.Trigger(ctx, chathooks.ChatFor(prepared.Chat, input.hookTurnID()), chathooks.Message{}, agenthooks.EventPostCompact, dispatch.CapacityClassGeneration)
var postCommitErr error
if postDispatchErr != nil {
postCommitErr = chathooks.GenerationDispatchError(agenthooks.EventPostCompact, postDispatchErr)
} else {
commitMessages, err = appendHookResultMessages(commitMessages, []*chathooks.Result{postResult}, prepared.ModelConfigID)
if err != nil {
s.server.metrics.RecordCompaction(metricProvider, metricModel, false, err)
return s.finishGenerationError(ctx, machine, input, err, requireGenerationAttempt(attempt.number))
}
}
err = s.commitGenerationStep(ctx, machine, input, attempt.number, generationActionCompact, commitMessages, generationCommitHooks{
PostCommitError: postCommitErr,
})
s.server.metrics.RecordCompaction(metricProvider, metricModel, err == nil, err)
if err != nil {
return xerrors.Errorf("commit compaction step: %w", err)
}
return nil
}
// compactionMetricIdentity returns the provider/model labels for compaction
// metrics. Override labels come from prepare-time resolution so events
// recorded before the override client is built (still-over-limit) match
// the compact action's own events.
func compactionMetricIdentity(compaction *generationCompaction) (provider, model string) {
if compaction.Override != nil {
return compaction.Override.ResolvedProvider, compaction.Override.ResolvedModel
}
return compactionProvider(compaction.Options), compactionModel(compaction.Options)
}
func compactionProvider(opts chatloop.GenerateCompactionOptions) string {
if opts.Model == nil {
return ""
}
return opts.Model.Provider()
}
func compactionModel(opts chatloop.GenerateCompactionOptions) string {
if opts.Model == nil {
return ""
}
return opts.Model.Model()
}
// generationAttempt groups the state a generation action needs after
// recording a new attempt.
type generationAttempt struct {
number int64
// publish streams a message part into the attempt's buffer episode.
publish func(codersdk.ChatMessageRole, codersdk.ChatMessagePart)
// startModelInvocation marks the start of the attempt's billable
// model invocation window on the buffer episode, so an interrupt
// can bill the window the step would have reported. It is always
// non-nil when beginGenerationAttempt succeeds.
startModelInvocation func()
// recordToolStart stamps an occurrence's actual start; serial calls may
// start after dispatch. It is always non-nil after beginGenerationAttempt.
recordToolStart func(callIndex int, startedAt time.Time)
// recordToolCompletion stamps an occurrence's completion. It is always
// non-nil after beginGenerationAttempt.
recordToolCompletion func(callIndex int, completedAt time.Time)
// closeEpisode closes the attempt's buffer episode. It is always
// non-nil when beginGenerationAttempt succeeds.
closeEpisode func()
}
func (s *taskStarter) beginGenerationAttempt(
ctx context.Context,
machine *chatstate.ChatMachine,
input chatWorkerTaskStartInput,
) (generationAttempt, error) {
var attempt int64
committed, err := machine.Update(ctx, func(tx *chatstate.Tx, _ database.Store, snapshot *chatstate.ChatSnapshot) error {
if _, err := verifyLockedTaskFence(snapshot, input, database.ChatStatusRunning, taskFenceOptions{requireHistory: true}); err != nil {
return xerrors.Errorf("verify task fence: %w", err)
}
result, err := tx.RecordGenerationAttempt(chatstate.RecordGenerationAttemptInput{Snapshot: snapshot})
if err != nil {
return xerrors.Errorf("tx.RecordGenerationAttempt: %w", err)
}
attempt = result.GenerationAttempt
return nil
})
if err != nil {
return generationAttempt{}, normalizeTaskTransitionError(err, "record generation attempt")
}
return s.openGenerationAttempt(ctx, input, committed, attempt)
}
// If delivered is true, the caller must return without generating:
// the history change starts a new generation task.
func (s *taskStarter) beginAssistantGenerationAttempt(
ctx context.Context,
machine *chatstate.ChatMachine,
input chatWorkerTaskStartInput,
) (attempt generationAttempt, delivered bool, err error) {
var number int64
var promotedQueuedAt time.Time
committed, err := machine.Update(ctx, func(tx *chatstate.Tx, _ database.Store, snapshot *chatstate.ChatSnapshot) error {
if _, err := verifyLockedTaskFence(snapshot, input, database.ChatStatusRunning, taskFenceOptions{requireHistory: true}); err != nil {
return xerrors.Errorf("verify task fence: %w", err)
}
result, err := tx.DeliverSteerMessages(chatstate.DeliverSteerMessagesInput{Snapshot: snapshot})
if err != nil {
return xerrors.Errorf("tx.DeliverSteerMessages: %w", err)
}
if len(result.DeliveredMessages) > 0 {
delivered = true
promotedQueuedAt = result.PromotedQueuedAt
return nil
}
attemptResult, err := tx.RecordGenerationAttempt(chatstate.RecordGenerationAttemptInput{})
if err != nil {
return xerrors.Errorf("tx.RecordGenerationAttempt: %w", err)
}
number = attemptResult.GenerationAttempt
return nil
})
if err != nil {
return generationAttempt{}, false, normalizeTaskTransitionError(err, "begin assistant generation attempt")
}
if delivered {
s.server.recordQueueWait(ctx, committed, promotedQueuedAt)
s.routeStateHint(ctx, stateUpdateFromChat(committed))
return generationAttempt{}, true, nil
}
attempt, err = s.openGenerationAttempt(ctx, input, committed, number)
return attempt, false, err
}
func (s *taskStarter) openGenerationAttempt(
ctx context.Context,
input chatWorkerTaskStartInput,
committed database.Chat,
attempt int64,
) (generationAttempt, error) {
key := messagepartbuffer.Key{
ChatID: input.ChatID,
HistoryVersion: committed.HistoryVersion,
GenerationAttempt: attempt,
}
if err := s.opts.MessagePartBuffer.CreateEpisode(key); err != nil && ctx.Err() == nil {
return generationAttempt{}, taskRetryableError{err: xerrors.Errorf("create message part episode: %w", err)}
}
return generationAttempt{
number: attempt,
publish: func(role codersdk.ChatMessageRole, part codersdk.ChatMessagePart) {
_ = s.opts.MessagePartBuffer.AddPart(key, role, part)
},
startModelInvocation: func() {
_ = s.opts.MessagePartBuffer.StartModelInvocation(key)
},
recordToolStart: func(callIndex int, startedAt time.Time) {
_ = s.opts.MessagePartBuffer.RecordToolStart(key, callIndex, startedAt)
},
recordToolCompletion: func(callIndex int, completedAt time.Time) {
_ = s.opts.MessagePartBuffer.RecordToolCompletion(key, callIndex, completedAt)
},
closeEpisode: func() {
_ = s.opts.MessagePartBuffer.CloseEpisode(key)
},
}, nil
}
type generationCommitHooks struct {
PostCommitError error
}
func (s *taskStarter) commitGenerationStep(
ctx context.Context,
machine *chatstate.ChatMachine,
input chatWorkerTaskStartInput,
attempt int64,
kind generationActionKind,
messages stepMessagesForCommit,
commitHooks generationCommitHooks,
) error {
if len(messages.Messages) == 0 {
if commitHooks.PostCommitError != nil {
return s.finishGenerationError(ctx, machine, input, commitHooks.PostCommitError, requireGenerationAttempt(attempt))
}
return s.finishGenerationTurn(ctx, machine, input, requireGenerationAttempt(attempt))
}
failClosed := commitHooks.PostCommitError != nil
var postCommitLastError pqtype.NullRawMessage
var postCommitMessage string
if commitHooks.PostCommitError != nil {
classified := chaterror.Classify(commitHooks.PostCommitError)
s.opts.Logger.Warn(ctx, "chat generation failed",
slog.F("chat_id", input.ChatID),
slog.F("worker_id", input.WorkerID),
slog.F("generation_attempt", input.GenerationAttempt),
slog.F("error_kind", classified.Kind),
slog.F("provider", classified.Provider),
slog.F("status_code", classified.StatusCode),
slog.F("retryable", classified.Retryable),
slog.Error(commitHooks.PostCommitError),
)
postCommitLastError, postCommitMessage = generationLastError(commitHooks.PostCommitError)
}
commitCtx, commitSpan := s.server.stages.Start(ctx, chatloop.StageCommit,
attribute.String(chatloop.AttrGenerationAction, string(kind)),
attribute.Int64(chatloop.AttrGenerationAttempt, attempt),
)
committed, err := machine.Update(commitCtx, func(tx *chatstate.Tx, _ database.Store, snapshot *chatstate.ChatSnapshot) error {
if _, err := verifyLockedGenerationFence(snapshot, input, requireGenerationAttempt(attempt)); err != nil {
return xerrors.Errorf("verify generation fence: %w", err)
}
if _, err := tx.CommitStep(chatstate.CommitStepInput{
Snapshot: snapshot,
Messages: messages.Messages,
ConsumeCompactionRequest: messages.ConsumeCompactionRequest,
}); err != nil {
return xerrors.Errorf("tx.CommitStep: %w", err)
}
// The fail-closed hook error must commit atomically with the
// step; a separate commit races the runner and can be dropped
// on crash.
if failClosed {
if _, err := tx.FinishError(chatstate.FinishErrorInput{LastError: postCommitLastError}); err != nil {
return xerrors.Errorf("tx.FinishError: %w", err)
}
}
return nil
})
commitSpan.End(err)
if err != nil {
return normalizeTaskTransitionError(err, "commit generation step")
}
if failClosed {
input.TurnSpan.Invalidate(input.TurnToken, chatloop.TurnOutcomeError, commitHooks.PostCommitError)
input.DebugTurn.RecordOutcome(chatdebug.StatusError)
postCommitCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), postCommitWatchPublishTimeout)
defer cancel()
if err := s.publishWatchAndRoute(postCommitCtx, committed, codersdk.ChatWatchEventKindStatusChange); err != nil {
return xerrors.Errorf("publish watch and route: %w", err)
}
return s.afterGenerationOutcome(postCommitCtx, generationOutcome{
Chat: committed,
Kind: runnerActionKindFinishError,
LastError: postCommitMessage,
})
}
s.routeStateHint(ctx, stateUpdateFromChat(committed))
return s.afterGenerationOutcome(ctx, generationOutcome{
Chat: committed,
Kind: runnerActionKind(kind),
})
}
func (s *taskStarter) enterRequiresAction(
ctx context.Context,
machine *chatstate.ChatMachine,
input chatWorkerTaskStartInput,
) error {
committed, err := machine.Update(ctx, func(tx *chatstate.Tx, _ database.Store, snapshot *chatstate.ChatSnapshot) error {
if _, err := verifyLockedTaskFence(snapshot, input, database.ChatStatusRunning, taskFenceOptions{requireHistory: true}); err != nil {
return xerrors.Errorf("verify task fence: %w", err)
}
if _, err := tx.EnterRequiresAction(chatstate.EnterRequiresActionInput{Snapshot: snapshot}); err != nil {
return xerrors.Errorf("tx.EnterRequiresAction: %w", err)
}
return nil
})
if err != nil {
return normalizeTaskTransitionError(err, "enter requires action")
}
// Submitted tool results open the next turn.
input.TurnSpan.Complete(input.TurnToken)
if err := s.publishWatchAndRoute(ctx, committed, codersdk.ChatWatchEventKindActionRequired); err != nil {
return xerrors.Errorf("publish watch and route: %w", err)
}
return s.afterGenerationOutcome(ctx, generationOutcome{
Chat: committed,
Kind: runnerActionKindEnterRequiresAction,
})
}
// generationAttemptFence controls whether a terminal generation
// transition also verifies that the chat's GenerationAttempt counter
// matches an expected value.
type generationAttemptFence struct {
required bool
attempt int64
}
// generationAttemptNotRequired skips the generation attempt fence; only the
// running-task fence is verified.
var generationAttemptNotRequired = generationAttemptFence{}
// requireGenerationAttempt returns a fence that also verifies the chat's
// generation attempt matches the given value.
func requireGenerationAttempt(attempt int64) generationAttemptFence {
return generationAttemptFence{required: true, attempt: attempt}
}
// loadChatForGeneration loads the chat and verifies the running-task fence,
// additionally verifying the generation attempt fence when required.
func loadChatForGeneration(
ctx context.Context,
store database.Store,
input chatWorkerTaskStartInput,
fence generationAttemptFence,
) (database.Chat, error) {
chat, err := loadChatForTask(ctx, store, input, database.ChatStatusRunning, taskFenceOptions{requireHistory: true})
if err != nil {
return database.Chat{}, err
}
return verifyGenerationFence(chat, fence)
}
// verifyLockedGenerationFence is loadChatForGeneration for
// [chatstate.ChatMachine.Update] callbacks; see verifyLockedTaskFence.
func verifyLockedGenerationFence(snapshot *chatstate.ChatSnapshot, input chatWorkerTaskStartInput, fence generationAttemptFence) (database.Chat, error) {
chat, err := verifyLockedTaskFence(snapshot, input, database.ChatStatusRunning, taskFenceOptions{requireHistory: true})
if err != nil {
return database.Chat{}, err
}
return verifyGenerationFence(chat, fence)
}
func verifyGenerationFence(chat database.Chat, fence generationAttemptFence) (database.Chat, error) {
if fence.required && chat.GenerationAttempt != fence.attempt {
return database.Chat{}, errors.Join(errTaskExpectedExit, xerrors.Errorf("generation fence mismatch: %d != %d", chat.GenerationAttempt, fence.attempt))
}
return chat, nil
}
// recordGenerationFinishFailure records an error outcome on the debug turn
// when a terminal generation transition fails, so the debug run is not
// finalized as interrupted when work was actually done. It skips expected
// exits (fence lost, chat deleted) where another task owns the turn, and
// retryable errors where a task retry will record the real outcome.
func recordGenerationFinishFailure(turn *runnerDebugTurn, err error) {
if errors.Is(err, errTaskExpectedExit) || errors.Is(err, errTaskRetryable) {
return
}
turn.RecordOutcome(chatdebug.StatusError)
}
func (s *taskStarter) completeGenerationTurn(
ctx context.Context,
input chatWorkerTaskStartInput,
committed database.Chat,
) error {
input.StopNudges.reset()
input.DebugTurn.RecordOutcome(chatdebug.StatusCompleted)
watchCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), postCommitWatchPublishTimeout)
defer cancel()
if err := s.publishWatchWithRetry(watchCtx, committed, codersdk.ChatWatchEventKindStatusChange); err != nil {
return xerrors.Errorf("publish watch and route: %w", err)
}
if err := s.afterGenerationOutcome(ctx, generationOutcome{
Chat: committed,
Kind: runnerActionKindFinishTurn,
}); err != nil {
return xerrors.Errorf("after generation outcome: %w", err)
}
s.routeStateHint(ctx, stateUpdateFromChat(committed))
return nil
}
func (s *taskStarter) finishGenerationTurnWithoutHook(
ctx context.Context,
machine *chatstate.ChatMachine,
input chatWorkerTaskStartInput,
fence generationAttemptFence,
) error {
var promotedQueuedAt time.Time
committed, err := machine.Update(ctx, func(tx *chatstate.Tx, _ database.Store, snapshot *chatstate.ChatSnapshot) error {
if _, err := verifyLockedGenerationFence(snapshot, input, fence); err != nil {
return xerrors.Errorf("verify generation fence: %w", err)
}
finishResult, err := tx.FinishTurn(chatstate.FinishTurnInput{Snapshot: snapshot})
if err != nil {
return xerrors.Errorf("tx.FinishTurn: %w", err)
}
promotedQueuedAt = finishResult.PromotedQueuedAt
return nil
})
if err != nil {
err := normalizeTaskTransitionError(err, "finish generation turn")
recordGenerationFinishFailure(input.DebugTurn, err)
return err
}
input.TurnSpan.Complete(input.TurnToken)
s.server.recordQueueWait(ctx, committed, promotedQueuedAt)
return s.completeGenerationTurn(ctx, input, committed)
}
func (s *taskStarter) finishGenerationTurn(
ctx context.Context,
machine *chatstate.ChatMachine,
input chatWorkerTaskStartInput,
fence generationAttemptFence,
) error {
if !s.server.hooks.Enabled() {
return s.finishGenerationTurnWithoutHook(ctx, machine, input, fence)
}
var chat database.Chat
var messages []database.ChatMessage
err := machine.ReadSnapshot(func(store database.Store) error {
loadedChat, err := loadChatForGeneration(ctx, store, input, fence)
if err != nil {
return xerrors.Errorf("load chat for stop hook: %w", err)
}
loadedMessages, err := store.GetChatMessagesByChatID(ctx, database.GetChatMessagesByChatIDParams{
ChatID: input.ChatID,
AfterID: 0,
})
if err != nil {
return xerrors.Errorf("load messages for stop hook: %w", err)
}
chat = loadedChat
messages = loadedMessages
return nil
})
if err != nil {
return normalizeTaskTransitionError(err, "load stop hook state")
}
response, err := s.server.hooks.Trigger(ctx, chathooks.ChatFor(chat, input.hookTurnID()), chathooks.Message{}, agenthooks.EventStop, dispatch.CapacityClassGeneration)
if err != nil {
return s.finishGenerationError(ctx, machine, input, chathooks.GenerationDispatchError(agenthooks.EventStop, err), fence)
}
stopMessages, err := chathooks.EventMessages(response, chat.LastModelConfigID)
if err != nil {
return s.finishGenerationError(ctx, machine, input, err, fence)
}
nudgeKey := stopNudgeKey(messages)
// Prompt conversion drops whitespace-only text parts, so a blank
// model context would buy a continuation that nudges nothing.
continueTurn := strings.TrimSpace(response.GetModelContext()) != "" && input.StopNudges.claim(nudgeKey)
var promotedQueuedAt time.Time
committed, err := machine.Update(ctx, func(tx *chatstate.Tx, _ database.Store, snapshot *chatstate.ChatSnapshot) error {
if _, err := verifyLockedGenerationFence(snapshot, input, fence); err != nil {
return xerrors.Errorf("verify generation fence: %w", err)
}
if len(stopMessages) > 0 {
if _, err := tx.CommitStep(chatstate.CommitStepInput{Snapshot: snapshot, Messages: stopMessages}); err != nil {
return xerrors.Errorf("commit stop hook messages: %w", err)
}
// CommitStep wrote the chat, so FinishTurn must read it again.
snapshot = nil
}
if !continueTurn {
finishResult, err := tx.FinishTurn(chatstate.FinishTurnInput{Snapshot: snapshot})
if err != nil {
return xerrors.Errorf("tx.FinishTurn: %w", err)
}
promotedQueuedAt = finishResult.PromotedQueuedAt
}
return nil
})
if err != nil {
if continueTurn {
input.StopNudges.cancel(nudgeKey)
}
err := normalizeTaskTransitionError(err, "finish generation turn")
recordGenerationFinishFailure(input.DebugTurn, err)
return err
}
if continueTurn {
s.routeStateHint(ctx, stateUpdateFromChat(committed))
return s.afterGenerationOutcome(ctx, generationOutcome{
Chat: committed,
Kind: runnerActionKind(generationActionGenerateAssistant),
})
}
input.TurnSpan.Complete(input.TurnToken)
s.server.recordQueueWait(ctx, committed, promotedQueuedAt)
return s.completeGenerationTurn(ctx, input, committed)
}
func (s *taskStarter) finishGenerationError(
ctx context.Context,
machine *chatstate.ChatMachine,
input chatWorkerTaskStartInput,
cause error,
fence generationAttemptFence,
) error {
classified := chaterror.Classify(cause)
// Log the unsanitized cause before persisting so administrators can
// diagnose the failure even when the classified user-facing message
// omits the underlying reason, and even if the persist below fails.
s.opts.Logger.Warn(ctx, "chat generation failed",
slog.F("chat_id", input.ChatID),
slog.F("worker_id", input.WorkerID),
slog.F("generation_attempt", input.GenerationAttempt),
slog.F("error_kind", classified.Kind),
slog.F("provider", classified.Provider),
slog.F("status_code", classified.StatusCode),
slog.F("retryable", classified.Retryable),
slog.Error(cause),
)
lastError, message := generationLastError(cause)
committed, err := machine.Update(ctx, func(tx *chatstate.Tx, _ database.Store, snapshot *chatstate.ChatSnapshot) error {
if _, err := verifyLockedGenerationFence(snapshot, input, fence); err != nil {
return xerrors.Errorf("verify generation fence: %w", err)
}
if _, err := tx.FinishError(chatstate.FinishErrorInput{Snapshot: snapshot, LastError: lastError}); err != nil {
return xerrors.Errorf("tx.FinishError: %w", err)
}
return nil
})
if err != nil {
err := normalizeTaskTransitionError(err, "finish generation error")
recordGenerationFinishFailure(input.DebugTurn, err)
return err
}
input.TurnSpan.Invalidate(input.TurnToken, chatloop.TurnOutcomeError, cause)
input.DebugTurn.RecordOutcome(chatdebug.StatusError)
if err := s.publishWatchAndRoute(ctx, committed, codersdk.ChatWatchEventKindStatusChange); err != nil {
return xerrors.Errorf("publish watch and route: %w", err)
}
return s.afterGenerationOutcome(ctx, generationOutcome{
Chat: committed,
Kind: runnerActionKindFinishError,
LastError: message,
})
}
func generationLastError(err error) (pqtype.NullRawMessage, string) {
if err == nil {
return pqtype.NullRawMessage{}, ""
}
classified := chaterror.Classify(err)
payload := chaterror.TerminalErrorPayload(classified)
if payload == nil {
payload = &codersdk.ChatError{Message: err.Error()}
}
encoded, marshalErr := json.Marshal(payload)
if marshalErr != nil {
return pqtype.NullRawMessage{}, payload.Message
}
return pqtype.NullRawMessage{RawMessage: encoded, Valid: true}, payload.Message
}
func (s *taskStarter) afterGenerationOutcome(ctx context.Context, outcome generationOutcome) error {
if err := s.server.afterGenerationOutcome(ctx, outcome); err != nil {
return taskRetryableError{err: xerrors.Errorf("generation post-outcome side effects: %w", err)}
}
return nil
}
func stepDataFromPersisted(step chatloop.PersistedStep) stepData {
return stepData{
Content: step.Content,
Usage: step.Usage,
ContextLimit: step.ContextLimit,
Runtime: step.Runtime,
ProviderResponseID: step.ProviderResponseID,
BatchRuntime: step.BatchRuntime,
BatchBilledCalls: step.BatchBilledCalls,
ToolCallCreatedAt: step.ToolCallCreatedAt,
ToolResultCreatedAt: step.ToolResultCreatedAt,
ReasoningStartedAt: step.ReasoningStartedAt,
ReasoningCompletedAt: step.ReasoningCompletedAt,
}
}