2026-02-27 11:50:56 -05:00
package chatloop
import (
"context"
"database/sql"
2026-04-23 19:58:38 +01:00
"encoding/base64"
2026-02-27 11:50:56 -05:00
"encoding/json"
"errors"
2026-04-08 12:42:03 -04:00
"maps"
2026-03-02 18:51:57 -05:00
"slices"
2026-02-27 11:50:56 -05:00
"strconv"
"strings"
2026-03-11 11:00:08 -07:00
"sync"
2026-02-27 18:34:33 -05:00
"time"
2026-04-02 10:07:01 -07:00
"unicode"
2026-02-27 11:50:56 -05:00
"charm.land/fantasy"
fantasyanthropic "charm.land/fantasy/providers/anthropic"
2026-03-02 18:51:57 -05:00
"charm.land/fantasy/schema"
2026-02-27 11:50:56 -05:00
"golang.org/x/xerrors"
2026-04-22 20:11:53 +10:00
"cdr.dev/slog/v3"
2026-04-08 12:42:03 -04:00
"github.com/coder/coder/v2/coderd/database/dbtime"
2026-04-20 12:27:16 +02:00
"github.com/coder/coder/v2/coderd/x/chatd/chatdebug"
2026-03-25 13:47:54 +11:00
"github.com/coder/coder/v2/coderd/x/chatd/chaterror"
2026-05-04 11:17:19 +02:00
"github.com/coder/coder/v2/coderd/x/chatd/chatopenai"
2026-03-23 17:34:43 +00:00
"github.com/coder/coder/v2/coderd/x/chatd/chatprompt"
"github.com/coder/coder/v2/coderd/x/chatd/chatretry"
2026-04-28 12:45:23 +02:00
"github.com/coder/coder/v2/coderd/x/chatd/chatsanitize"
2026-04-22 20:11:53 +10:00
"github.com/coder/coder/v2/coderd/x/chatd/chattool"
2026-02-27 11:50:56 -05:00
"github.com/coder/coder/v2/codersdk"
2026-04-10 00:40:09 +10:00
"github.com/coder/quartz"
2026-02-27 11:50:56 -05:00
)
const (
interruptedToolResultErrorMessage = "tool call was interrupted before it produced a result"
2026-03-04 22:28:23 -05:00
// maxCompactionRetries limits how many times the post-run
// compaction safety net can re-enter the step loop. This
// prevents infinite compaction loops when the model keeps
// hitting the context limit after summarization.
maxCompactionRetries = 3
2026-03-25 13:47:54 +11:00
// defaultStartupTimeout bounds how long an individual
// model attempt may spend starting to respond before
// the attempt is canceled and retried.
defaultStartupTimeout = 60 * time . Second
2026-02-27 11:50:56 -05:00
)
2026-03-25 13:47:54 +11:00
var (
2026-04-08 11:54:44 -04:00
ErrInterrupted = xerrors . New ( "chat interrupted" )
ErrDynamicToolCall = xerrors . New ( "dynamic tool call" )
2026-04-16 11:12:01 +02:00
// ErrStopAfterTool is returned when a tool listed in
// StopAfterTools produces a successful result, indicating
// the run should terminate cleanly after persistence.
ErrStopAfterTool = xerrors . New ( "stop after tool" )
2026-03-25 13:47:54 +11:00
errStartupTimeout = xerrors . New (
"chat response did not start before the startup timeout" ,
)
)
2026-02-27 11:50:56 -05:00
2026-04-08 11:54:44 -04:00
// PendingToolCall describes a tool call that targets a dynamic
// tool. These calls are not executed by the chatloop; instead
// they are persisted so the caller can fulfill them externally.
type PendingToolCall struct {
ToolCallID string
ToolName string
Args string
}
2026-02-27 11:50:56 -05:00
// PersistedStep contains the full content of a completed or
// interrupted agent step. Content includes both assistant blocks
2026-03-02 18:51:57 -05:00
// (text, reasoning, tool calls) and tool result blocks. The
// persistence layer is responsible for splitting these into
// separate database messages by role.
2026-02-27 11:50:56 -05:00
type PersistedStep struct {
2026-03-24 14:57:40 +01:00
Content [] fantasy . Content
Usage fantasy . Usage
ContextLimit sql . NullInt64
ProviderResponseID string
2026-03-18 10:57:35 -04:00
// Runtime is the wall-clock duration of this step,
// covering LLM streaming, tool execution, and retries.
// Zero indicates the duration was not measured (e.g.
// interrupted steps).
Runtime time . Duration
2026-04-08 11:54:44 -04:00
// PendingDynamicToolCalls lists tool calls that target
// dynamic tools. When non-empty the chatloop exits with
// ErrDynamicToolCall so the caller can execute them
// externally and resume the loop.
PendingDynamicToolCalls [] PendingToolCall
2026-04-08 12:42:03 -04:00
// ToolCallCreatedAt maps tool-call IDs to the time
// the model emitted each tool call. Applied by the
// persistence layer to set CreatedAt on persisted
// tool-call ChatMessageParts.
ToolCallCreatedAt map [ string ] time . Time
// ToolResultCreatedAt maps tool-call IDs to the time
// each tool result was produced (or interrupted).
// Applied by the persistence layer to set CreatedAt
// on persisted tool-result ChatMessageParts.
ToolResultCreatedAt map [ string ] time . Time
2026-05-18 12:30:30 -04:00
// ReasoningStartedAt and ReasoningCompletedAt are parallel
// slices indexed by the occurrence order of reasoning
// content in Content. The persistence layer walks reasoning
// parts in order and applies these timestamps to the
// corresponding ChatMessageParts so the frontend can render
// reasoning duration. Reasoning parts have no provider-side
// stable ID, so order is the only correlation we have.
ReasoningStartedAt [] time . Time
ReasoningCompletedAt [] time . Time
2026-02-27 11:50:56 -05:00
}
// RunOptions configures a single streaming chat loop run.
type RunOptions struct {
2026-03-02 18:51:57 -05:00
Model fantasy . LanguageModel
Messages [] fantasy . Message
Tools [] fantasy . AgentTool
MaxSteps int
2026-03-25 13:47:54 +11:00
// StartupTimeout bounds how long each model attempt may
// spend opening the provider stream and waiting for its
// first stream part before the attempt is canceled and
// retried. Zero uses the production default.
StartupTimeout time . Duration
2026-04-10 00:40:09 +10:00
// Clock creates startup guard timers. In production use a
// real clock; tests can inject quartz.NewMock(t) to make
// startup timeout behavior deterministic.
Clock quartz . Clock
2026-02-27 11:50:56 -05:00
ActiveTools [] string
ContextLimitFallback int64
2026-04-08 11:54:44 -04:00
// DynamicToolNames lists tool names that are handled
// externally. When the model invokes one of these tools
// the chatloop persists partial results and exits with
// ErrDynamicToolCall instead of executing the tool.
DynamicToolNames map [ string ] bool
2026-04-16 11:12:01 +02:00
// StopAfterTools lists tool names that, when they produce a
// successful result, cause the run to stop after persisting
// the current step. This is used for plan turns where
// propose_plan should terminate the run on success.
StopAfterTools map [ string ] struct {}
2026-04-30 14:08:59 +02:00
// ExclusiveToolNames lists tool names that must be called
// alone in a batch. When any exclusive tool appears
// alongside other locally-executed tools, every tool in the
// batch receives a policy error and nothing executes.
ExclusiveToolNames map [ string ] bool
2026-04-08 11:54:44 -04:00
2026-03-02 18:51:57 -05:00
// ModelConfig holds per-call LLM parameters (temperature,
// max tokens, etc.) read from the chat model configuration.
ModelConfig codersdk . ChatModelCallConfig
// ProviderOptions are provider-specific call options
// converted from ModelConfig.ProviderOptions. This is a
// separate field because the conversion requires knowledge
// of the provider, which lives in chatd, not chatloop.
ProviderOptions fantasy . ProviderOptions
2026-03-13 19:49:34 +01:00
// ProviderTools are provider-native tools (like web search
// and computer use) whose definitions are passed directly
// to the provider API. When a ProviderTool has a non-nil
// Runner, tool calls are executed locally; otherwise the
// provider handles execution (e.g. web search).
ProviderTools [] ProviderTool
2026-03-11 14:33:15 -07:00
2026-02-27 11:50:56 -05:00
PersistStep func ( context . Context , PersistedStep ) error
PublishMessagePart func (
2026-03-13 17:53:26 +02:00
role codersdk . ChatMessageRole ,
2026-02-27 11:50:56 -05:00
part codersdk . ChatMessagePart ,
)
2026-04-22 17:19:56 +01:00
// Callers should attach correlation fields (chat_id, owner_id, etc.)
// using Logger.With before passing the logger in.
2026-04-22 20:11:53 +10:00
Logger slog . Logger
2026-03-24 14:57:40 +01:00
Compaction * CompactionOptions
ReloadMessages func ( context . Context ) ([] fantasy . Message , error )
DisableChainMode func ()
2026-05-11 17:43:40 +01:00
// PrepareMessages is called at least once before each LLM step
// with the current message history. If it returns non-nil, the
// returned slice replaces messages for this and all subsequent
// steps.
2026-04-15 16:41:15 -04:00
// Used to inject system context that becomes available mid-loop
// (e.g. AGENTS.md after create_workspace).
2026-05-11 17:43:40 +01:00
// NOTE: It may be called more than once per step in case of a
// retry, so callbacks should avoid duplicating messages.
2026-04-15 16:41:15 -04:00
PrepareMessages func ([] fantasy . Message ) [] fantasy . Message
2026-02-27 11:50:56 -05:00
2026-05-12 00:30:56 -04:00
// PrepareTools is called once before each LLM step with the
// current tool list. If it returns non-nil, the returned slice
// replaces opts.Tools for this and all subsequent steps, and any
// new tool names are appended to opts.ActiveTools so they become
// callable immediately. Used to inject tools that become available
// mid-turn (e.g. workspace MCP tools discovered after
// create_workspace).
//
// The chatloop tracks whether tools have already been replaced so
// PrepareTools is not retried on subsequent steps once it has
// returned a non-nil slice. Callbacks may still be invoked on later
// steps when they previously returned nil.
PrepareTools func ([] fantasy . AgentTool ) [] fantasy . AgentTool
2026-02-27 18:34:33 -05:00
// OnRetry is called before each retry attempt when the LLM
// stream fails with a retryable error. It provides the attempt
2026-03-25 13:47:54 +11:00
// number, raw error, normalized classification, and backoff
// delay so callers can publish status events to connected
// clients. Callers should also clear any buffered stream state
// from the failed attempt in this callback to avoid sending
// duplicated content.
2026-02-27 18:34:33 -05:00
OnRetry chatretry . OnRetryFn
2026-02-27 11:50:56 -05:00
OnInterruptedPersistError func ( error )
2026-04-15 19:53:10 +01:00
// Metrics records Prometheus metrics for the chatd subsystem.
// When nil, no metrics are recorded.
Metrics * Metrics
// BuiltinToolNames lists tool names that are built into chatd.
BuiltinToolNames map [ string ] bool
2026-02-27 11:50:56 -05:00
}
2026-03-13 19:49:34 +01:00
// ProviderTool pairs a provider-native tool definition with an
// optional local executor. When Runner is nil the tool is fully
// provider-executed (e.g. web search). When Runner is non-nil
// the definition is sent to the API but execution is handled
// locally (e.g. computer use).
type ProviderTool struct {
Definition fantasy . Tool
Runner fantasy . AgentTool
2026-05-04 20:30:50 +02:00
// ResultProviderMetadata extracts provider-specific metadata from successful
// local runner responses. The chat loop attaches returned metadata to the tool
// result sent back to the model. OpenAI computer-use uses this to request
// original screenshot detail for image results.
ResultProviderMetadata func ( response fantasy . ToolResponse ) fantasy . ProviderMetadata
2026-03-13 19:49:34 +01:00
}
2026-03-02 18:51:57 -05:00
// stepResult holds the accumulated output of a single streaming
// step. Since we own the stream consumer, all content is tracked
2026-04-24 23:57:30 +02:00
// directly here, no shadow draft state needed.
2026-03-02 18:51:57 -05:00
type stepResult struct {
2026-05-18 12:30:30 -04:00
content [] fantasy . Content
usage fantasy . Usage
providerMetadata fantasy . ProviderMetadata
finishReason fantasy . FinishReason
toolCalls [] fantasy . ToolCallContent
shouldContinue bool
toolCallCreatedAt map [ string ] time . Time
toolResultCreatedAt map [ string ] time . Time
reasoningStartedAt [] time . Time
reasoningCompletedAt [] time . Time
2026-03-02 18:51:57 -05:00
}
// toResponseMessages converts step content into messages suitable
// for appending to the conversation. Mirrors fantasy's
// toResponseMessages logic.
func ( r stepResult ) toResponseMessages () [] fantasy . Message {
var assistantParts [] fantasy . MessagePart
var toolParts [] fantasy . MessagePart
for _ , c := range r . content {
switch c . GetType () {
case fantasy . ContentTypeText :
text , ok := fantasy . AsContentType [ fantasy . TextContent ]( c )
2026-03-19 12:10:54 -04:00
if ! ok || strings . TrimSpace ( text . Text ) == "" {
2026-03-02 18:51:57 -05:00
continue
}
assistantParts = append ( assistantParts , fantasy . TextPart {
Text : text . Text ,
ProviderOptions : fantasy . ProviderOptions ( text . ProviderMetadata ),
})
case fantasy . ContentTypeReasoning :
reasoning , ok := fantasy . AsContentType [ fantasy . ReasoningContent ]( c )
2026-05-18 15:20:33 +10:00
if ! ok {
continue
}
opts := fantasy . ProviderOptions ( reasoning . ProviderMetadata )
if strings . TrimSpace ( reasoning . Text ) == "" && ! chatsanitize . HasAnthropicSignedReasoningOptions ( opts ) {
2026-03-02 18:51:57 -05:00
continue
}
assistantParts = append ( assistantParts , fantasy . ReasoningPart {
Text : reasoning . Text ,
2026-05-18 15:20:33 +10:00
ProviderOptions : opts ,
2026-03-02 18:51:57 -05:00
})
case fantasy . ContentTypeToolCall :
toolCall , ok := fantasy . AsContentType [ fantasy . ToolCallContent ]( c )
if ! ok {
continue
}
assistantParts = append ( assistantParts , fantasy . ToolCallPart {
ToolCallID : toolCall . ToolCallID ,
ToolName : toolCall . ToolName ,
Input : toolCall . Input ,
ProviderExecuted : toolCall . ProviderExecuted ,
ProviderOptions : fantasy . ProviderOptions ( toolCall . ProviderMetadata ),
})
case fantasy . ContentTypeFile :
file , ok := fantasy . AsContentType [ fantasy . FileContent ]( c )
if ! ok {
continue
}
assistantParts = append ( assistantParts , fantasy . FilePart {
Data : file . Data ,
MediaType : file . MediaType ,
ProviderOptions : fantasy . ProviderOptions ( file . ProviderMetadata ),
})
case fantasy . ContentTypeSource :
// Sources are metadata about references; they don't
// need to be included in conversation messages.
continue
case fantasy . ContentTypeToolResult :
result , ok := fantasy . AsContentType [ fantasy . ToolResultContent ]( c )
if ! ok {
continue
}
2026-03-12 13:22:09 -07:00
part := fantasy . ToolResultPart {
2026-03-11 14:33:15 -07:00
ToolCallID : result . ToolCallID ,
Output : result . Result ,
ProviderExecuted : result . ProviderExecuted ,
ProviderOptions : fantasy . ProviderOptions ( result . ProviderMetadata ),
2026-03-12 13:22:09 -07:00
}
// Provider-executed tool results (e.g. web_search)
// must stay in the assistant message so the result
// block appears inline after the corresponding
// server_tool_use block. This matches the persistence
// layer in chatd.go which keeps them in
// assistantBlocks.
if result . ProviderExecuted {
assistantParts = append ( assistantParts , part )
} else {
toolParts = append ( toolParts , part )
}
2026-03-02 18:51:57 -05:00
default :
continue
}
}
var messages [] fantasy . Message
if len ( assistantParts ) > 0 {
messages = append ( messages , fantasy . Message {
Role : fantasy . MessageRoleAssistant ,
Content : assistantParts ,
})
}
if len ( toolParts ) > 0 {
messages = append ( messages , fantasy . Message {
Role : fantasy . MessageRoleTool ,
Content : toolParts ,
})
}
return messages
}
// reasoningState accumulates reasoning content and provider
// metadata while the stream is in flight.
type reasoningState struct {
2026-05-18 12:30:30 -04:00
text string
options fantasy . ProviderMetadata
startedAt time . Time
2026-03-02 18:51:57 -05:00
}
// Run executes the chat step-stream loop and delegates
// persistence/publishing to callbacks.
func Run ( ctx context . Context , opts RunOptions ) error {
2026-02-27 11:50:56 -05:00
if opts . Model == nil {
2026-03-02 18:51:57 -05:00
return xerrors . New ( "chat model is required" )
2026-02-27 11:50:56 -05:00
}
if opts . PersistStep == nil {
2026-03-02 18:51:57 -05:00
return xerrors . New ( "persist step callback is required" )
2026-02-27 11:50:56 -05:00
}
if opts . MaxSteps <= 0 {
opts . MaxSteps = 1
}
2026-03-25 13:47:54 +11:00
if opts . StartupTimeout <= 0 {
opts . StartupTimeout = defaultStartupTimeout
}
2026-04-10 00:40:09 +10:00
if opts . Clock == nil {
opts . Clock = quartz . NewReal ()
}
2026-04-15 19:53:10 +01:00
if opts . Metrics == nil {
opts . Metrics = NopMetrics ()
}
2026-02-27 11:50:56 -05:00
2026-03-13 17:53:26 +02:00
publishMessagePart := func ( role codersdk . ChatMessageRole , part codersdk . ChatMessagePart ) {
2026-02-27 11:50:56 -05:00
if opts . PublishMessagePart == nil {
return
}
opts . PublishMessagePart ( role , part )
}
2026-03-11 14:33:15 -07:00
tools := buildToolDefinitions ( opts . Tools , opts . ActiveTools , opts . ProviderTools )
2026-02-27 11:50:56 -05:00
2026-03-02 18:51:57 -05:00
messages := opts . Messages
var lastUsage fantasy . Usage
var lastProviderMetadata fantasy . ProviderMetadata
2026-03-24 14:57:40 +01:00
needsFullHistoryReload := false
reloadFullHistory := func ( stage string ) error {
if opts . ReloadMessages == nil {
return nil
}
reloaded , err := opts . ReloadMessages ( ctx )
if err != nil {
return xerrors . Errorf ( "reload messages %s: %w" , stage , err )
}
messages = reloaded
return nil
}
2026-03-02 18:51:57 -05:00
2026-03-06 13:02:25 -08:00
totalSteps := 0
// When totalSteps reaches MaxSteps the inner loop exits immediately
// (its condition is false), stoppedByModel stays false, and the
// post-loop guard breaks the outer compaction loop.
2026-03-04 22:28:23 -05:00
for compactionAttempt := 0 ; ; compactionAttempt ++ {
alreadyCompacted := false
// stoppedByModel is true when the inner step loop
// exited because the model produced no tool calls
// (shouldContinue was false). This distinguishes a
// natural stop from hitting MaxSteps.
stoppedByModel := false
// compactedOnFinalStep tracks whether compaction
// occurred on the very step where the model stopped.
// Only in that case should we re-enter, because the
// agent never had a chance to use the compacted context.
compactedOnFinalStep := false
2026-03-02 18:51:57 -05:00
2026-03-06 13:02:25 -08:00
for step := 0 ; totalSteps < opts . MaxSteps ; step ++ {
totalSteps ++
2026-04-15 19:53:10 +01:00
provider := opts . Model . Provider ()
2026-04-17 16:16:30 +01:00
modelName := opts . Model . Model ()
opts . Metrics . StepsTotal . WithLabelValues ( provider , modelName ). Inc ()
2026-03-18 10:57:35 -04:00
stepStart := time . Now ()
2026-05-12 00:30:56 -04:00
if opts . PrepareTools != nil {
if updated := opts . PrepareTools ( opts . Tools ); updated != nil {
opts . ActiveTools = mergeNewToolNames (
opts . ActiveTools , opts . Tools , updated ,
)
opts . Tools = updated
tools = buildToolDefinitions (
opts . Tools , opts . ActiveTools , opts . ProviderTools ,
)
}
}
2026-05-11 17:43:40 +01:00
var prepared [] fantasy . Message
2026-05-18 15:20:33 +10:00
var prepareErr error
messages , prepared , prepareErr = prepareMessagesForRequest (
2026-05-11 17:43:40 +01:00
ctx , opts , messages , provider , modelName , step , totalSteps ,
2026-04-24 23:57:30 +02:00
)
2026-05-18 15:20:33 +10:00
if prepareErr != nil {
return xerrors . Errorf ( "prepare prompt: %w" , prepareErr )
}
2026-04-17 16:16:30 +01:00
opts . Metrics . MessageCount . WithLabelValues ( provider , modelName ). Observe ( float64 ( len ( prepared )))
opts . Metrics . PromptSizeBytes . WithLabelValues ( provider , modelName ). Observe ( float64 ( EstimatePromptSize ( prepared )))
2026-04-15 16:41:15 -04:00
2026-03-04 22:28:23 -05:00
call := fantasy . Call {
Prompt : prepared ,
Tools : tools ,
MaxOutputTokens : opts . ModelConfig . MaxOutputTokens ,
Temperature : opts . ModelConfig . Temperature ,
TopP : opts . ModelConfig . TopP ,
TopK : opts . ModelConfig . TopK ,
PresencePenalty : opts . ModelConfig . PresencePenalty ,
FrequencyPenalty : opts . ModelConfig . FrequencyPenalty ,
ProviderOptions : opts . ProviderOptions ,
2026-03-02 18:51:57 -05:00
}
2026-03-04 22:28:23 -05:00
var result stepResult
2026-05-18 15:20:33 +10:00
var retryPrepareErr error
2026-04-20 12:27:16 +02:00
stepCtx := chatdebug . ReuseStep ( ctx )
err := chatretry . Retry ( stepCtx , func ( retryCtx context . Context ) error {
2026-05-18 15:20:33 +10:00
if retryPrepareErr != nil {
return retryPrepareErr
}
2026-03-25 13:47:54 +11:00
attempt , streamErr := guardedStream (
retryCtx ,
2026-04-15 19:53:10 +01:00
provider ,
2026-04-17 16:16:30 +01:00
modelName ,
2026-04-10 00:40:09 +10:00
opts . Clock ,
2026-03-25 13:47:54 +11:00
opts . StartupTimeout ,
func ( attemptCtx context . Context ) ( fantasy . StreamResponse , error ) {
return opts . Model . Stream ( attemptCtx , call )
},
2026-04-15 19:53:10 +01:00
opts . Metrics ,
2026-03-25 13:47:54 +11:00
)
2026-03-04 22:28:23 -05:00
if streamErr != nil {
return streamErr
}
2026-03-25 13:47:54 +11:00
defer attempt . release ()
2026-03-04 22:28:23 -05:00
var processErr error
2026-03-25 13:47:54 +11:00
result , processErr = processStepStream (
attempt . ctx ,
attempt . stream ,
publishMessagePart ,
)
return attempt . finish ( processErr )
}, func (
attempt int ,
retryErr error ,
classified chatretry . ClassifiedError ,
delay time . Duration ,
) {
2026-03-04 22:28:23 -05:00
// Reset result from the failed attempt so the next
// attempt starts clean.
result = stepResult {}
2026-04-17 16:16:30 +01:00
// Record before OnRetry so a panicking callback can't
// drop the sample. The metric's provider label comes
// from the outer local; WithProvider only affects the
// classified payload handed to OnRetry.
classified = classified . WithProvider ( provider )
opts . Metrics . RecordStreamRetry ( provider , modelName , classified )
2026-05-11 17:43:40 +01:00
if classified . ChainBroken {
if chatopenai . HasPreviousResponseID ( opts . ProviderOptions ) {
opts . ProviderOptions = chatopenai . ClearPreviousResponseID ( opts . ProviderOptions )
}
if chatopenai . HasPreviousResponseID ( call . ProviderOptions ) {
call . ProviderOptions = chatopenai . ClearPreviousResponseID ( call . ProviderOptions )
}
if opts . DisableChainMode != nil {
opts . DisableChainMode ()
}
if opts . ReloadMessages != nil {
reloaded , err := opts . ReloadMessages ( ctx )
if err != nil {
opts . Logger . Warn ( ctx ,
"chain-broken recovery: reload messages failed" ,
slog . Error ( err ),
)
} else {
// Reloaded history replaces the prompt prepared before
// the failed attempt, so run the same preparation
// pipeline used by normal provider requests.
2026-05-18 15:20:33 +10:00
var (
reloadedCanonical [] fantasy . Message
retryPrompt [] fantasy . Message
prepareErr error
)
call . Prompt = nil
reloadedCanonical , retryPrompt , prepareErr = prepareMessagesForRequest (
2026-05-11 17:43:40 +01:00
ctx , opts , reloaded , provider , modelName , step , totalSteps ,
)
2026-05-18 15:20:33 +10:00
if prepareErr != nil {
retryPrepareErr = prepareErr
} else {
messages = reloadedCanonical
call . Prompt = retryPrompt
}
2026-05-11 17:43:40 +01:00
}
}
}
2026-03-04 22:28:23 -05:00
if opts . OnRetry != nil {
2026-03-25 13:47:54 +11:00
opts . OnRetry ( attempt , retryErr , classified , delay )
2026-03-04 22:28:23 -05:00
}
})
if err != nil {
if errors . Is ( err , ErrInterrupted ) {
2026-03-02 18:51:57 -05:00
persistInterruptedStep ( ctx , opts , & result )
return ErrInterrupted
}
2026-05-18 15:20:33 +10:00
if retryPrepareErr != nil && errors . Is ( err , retryPrepareErr ) {
return xerrors . Errorf ( "prepare prompt: %w" , err )
}
2026-03-04 22:28:23 -05:00
return xerrors . Errorf ( "stream response: %w" , err )
2026-03-02 18:51:57 -05:00
}
2026-03-04 22:28:23 -05:00
// Execute tools before persisting so that tool results
// are included in the persisted step content. The
// persistence layer splits assistant and tool-result
// blocks into separate database messages by role.
var toolResults [] fantasy . ToolResultContent
if result . shouldContinue {
2026-04-30 14:08:59 +02:00
var err error
toolResults , err = executeToolsForStep ( ctx , opts , & result , provider , modelName , step , stepStart , publishMessagePart )
if err != nil {
return err
2026-03-12 13:59:16 -07:00
}
}
2026-03-04 22:28:23 -05:00
// Extract context limit from provider metadata.
2026-04-24 23:57:30 +02:00
contextLimit := extractContextLimitWithFallback (
result . providerMetadata ,
opts . ContextLimitFallback ,
)
2026-04-28 12:45:23 +02:00
result . content = chatsanitize . SanitizeAnthropicProviderToolStepContent (
2026-04-24 23:57:30 +02:00
ctx , opts . Logger , provider , modelName ,
"normal_persist" , step , result . finishReason , result . content ,
)
if len ( result . content ) == 0 {
lastUsage = result . usage
lastProviderMetadata = result . providerMetadata
stoppedByModel = true
break
2026-03-04 22:28:23 -05:00
}
2026-04-24 23:57:30 +02:00
2026-03-12 13:59:16 -07:00
// Persist the step. If persistence fails because
// the chat was interrupted between the previous
// check and here, fall back to the interrupt-safe
// path so partial content is not lost.
2026-03-04 22:28:23 -05:00
if err := opts . PersistStep ( ctx , PersistedStep {
2026-05-18 12:30:30 -04:00
Content : result . content ,
Usage : result . usage ,
ContextLimit : contextLimit ,
ProviderResponseID : chatopenai . ExtractResponseIDIfStored ( opts . ProviderOptions , result . providerMetadata ),
Runtime : time . Since ( stepStart ),
ToolCallCreatedAt : result . toolCallCreatedAt ,
ToolResultCreatedAt : result . toolResultCreatedAt ,
ReasoningStartedAt : result . reasoningStartedAt ,
ReasoningCompletedAt : result . reasoningCompletedAt ,
2026-03-04 22:28:23 -05:00
}); err != nil {
2026-03-12 13:59:16 -07:00
if errors . Is ( err , ErrInterrupted ) {
persistInterruptedStep ( ctx , opts , & result )
return ErrInterrupted
}
2026-03-04 22:28:23 -05:00
return xerrors . Errorf ( "persist step: %w" , err )
}
lastUsage = result . usage
lastProviderMetadata = result . providerMetadata
2026-04-16 11:12:01 +02:00
// Check if any executed tool triggers an early stop.
if shouldStopAfterTools ( opts . StopAfterTools , toolResults ) {
tryCompactOnExit ( ctx , opts , result . usage , result . providerMetadata )
return ErrStopAfterTool
}
2026-03-24 14:57:40 +01:00
// When chain mode is active (PreviousResponseID set), exit
// it after persisting the first chained step. Continuation
// steps include tool-result messages, which fantasy rejects
// when previous_response_id is set, so we must leave chain
// mode and reload the full history before the next call.
2026-03-06 13:02:25 -08:00
stepMessages := result . toResponseMessages ()
2026-05-04 11:17:19 +02:00
if chatopenai . HasPreviousResponseID ( opts . ProviderOptions ) {
opts . ProviderOptions = chatopenai . ClearPreviousResponseID ( opts . ProviderOptions )
2026-03-24 14:57:40 +01:00
if opts . DisableChainMode != nil {
opts . DisableChainMode ()
}
switch {
case opts . ReloadMessages != nil :
if err := reloadFullHistory ( "after chain mode exit" ); err != nil {
return err
}
needsFullHistoryReload = false
default :
messages = append ( messages , stepMessages ... )
needsFullHistoryReload = false
}
} else {
messages = append ( messages , stepMessages ... )
}
if needsFullHistoryReload && ! result . shouldContinue &&
opts . ReloadMessages != nil {
if err := reloadFullHistory ( "before final compaction after chain mode exit" ); err != nil {
return err
}
needsFullHistoryReload = false
}
2026-03-06 13:02:25 -08:00
2026-03-04 22:28:23 -05:00
// Inline compaction.
2026-03-24 14:57:40 +01:00
if ! needsFullHistoryReload && opts . Compaction != nil && opts . ReloadMessages != nil {
2026-03-04 22:28:23 -05:00
did , compactErr := tryCompact (
ctx ,
opts . Model ,
opts . Compaction ,
opts . ContextLimitFallback ,
result . usage ,
result . providerMetadata ,
messages ,
2026-03-02 18:51:57 -05:00
)
2026-04-17 16:16:30 +01:00
opts . Metrics . RecordCompaction ( provider , modelName , did , compactErr )
2026-03-04 22:28:23 -05:00
if compactErr != nil && opts . Compaction . OnError != nil {
opts . Compaction . OnError ( compactErr )
}
2026-04-15 19:53:10 +01:00
2026-03-04 22:28:23 -05:00
if did {
alreadyCompacted = true
compactedOnFinalStep = true
2026-03-24 14:57:40 +01:00
if err := reloadFullHistory ( "after compaction" ); err != nil {
return err
2026-03-04 22:28:23 -05:00
}
}
2026-03-02 18:51:57 -05:00
}
2026-03-04 22:28:23 -05:00
if ! result . shouldContinue {
stoppedByModel = true
break
2026-02-27 11:50:56 -05:00
}
2026-03-04 22:28:23 -05:00
// The agent is continuing with tool calls, so any
// prior compaction has already been consumed.
compactedOnFinalStep = false
2026-02-27 11:50:56 -05:00
}
2026-03-24 14:57:40 +01:00
if needsFullHistoryReload && stoppedByModel && opts . ReloadMessages != nil {
if err := reloadFullHistory ( "before post-run compaction after chain mode exit" ); err != nil {
return err
}
needsFullHistoryReload = false
}
2026-03-04 22:28:23 -05:00
// Post-run compaction safety net: if we never compacted
// during the loop, try once at the end.
2026-03-24 14:57:40 +01:00
if ! needsFullHistoryReload && ! alreadyCompacted && opts . Compaction != nil && opts . ReloadMessages != nil {
2026-03-04 22:28:23 -05:00
did , err := tryCompact (
2026-03-02 18:51:57 -05:00
ctx ,
opts . Model ,
opts . Compaction ,
opts . ContextLimitFallback ,
2026-03-04 22:28:23 -05:00
lastUsage ,
lastProviderMetadata ,
2026-03-02 18:51:57 -05:00
messages ,
)
2026-04-17 16:16:30 +01:00
opts . Metrics . RecordCompaction ( opts . Model . Provider (), opts . Model . Model (), did , err )
2026-03-04 22:28:23 -05:00
if err != nil {
if opts . Compaction . OnError != nil {
opts . Compaction . OnError ( err )
}
2026-03-02 18:51:57 -05:00
}
if did {
2026-03-04 22:28:23 -05:00
compactedOnFinalStep = true
2026-02-27 11:50:56 -05:00
}
2026-02-27 18:34:33 -05:00
}
2026-03-04 22:28:23 -05:00
// Re-enter the step loop when compaction fired on the
// model's final step. This lets the agent continue
// working with fresh summarized context instead of
// stopping. When the inner loop continued after inline
// compaction (tool-call steps kept going), the agent
// already used the compacted context, so no re-entry
// is needed. Limit retries to prevent infinite loops.
if compactedOnFinalStep && stoppedByModel &&
opts . ReloadMessages != nil &&
compactionAttempt < maxCompactionRetries {
reloaded , reloadErr := opts . ReloadMessages ( ctx )
if reloadErr != nil {
return xerrors . Errorf ( "reload messages after compaction: %w" , reloadErr )
2026-02-27 11:50:56 -05:00
}
2026-03-04 22:28:23 -05:00
messages = reloaded
continue
2026-02-27 11:50:56 -05:00
}
2026-03-04 22:28:23 -05:00
break
2026-02-27 11:50:56 -05:00
}
2026-03-02 18:51:57 -05:00
return nil
}
2026-05-11 17:43:40 +01:00
// prepareMessagesForRequest applies the prompt preparation pipeline used
// immediately before sending messages to a provider. It returns the
// possibly updated canonical messages and an independent provider-ready
2026-05-18 15:20:33 +10:00
// prompt. When preparation fails, the prompt result is nil and err is the
// terminal prompt-preparation failure.
2026-05-11 17:43:40 +01:00
func prepareMessagesForRequest (
ctx context . Context ,
opts RunOptions ,
messages [] fantasy . Message ,
provider string ,
modelName string ,
step int ,
totalSteps int ,
2026-05-18 15:20:33 +10:00
) ( canonical [] fantasy . Message , prompt [] fantasy . Message , err error ) {
2026-05-11 17:43:40 +01:00
canonical = messages
if opts . PrepareMessages != nil {
if updated := opts . PrepareMessages ( canonical ); updated != nil {
canonical = updated
}
}
// Copy messages so provider-specific caching mutations don't leak
// back to the canonical message slice.
prompt = slices . Clone ( canonical )
prompt , sanitizeStats := chatsanitize . SanitizeAnthropicProviderToolHistory ( provider , prompt )
chatsanitize . LogAnthropicProviderToolSanitization (
ctx , opts . Logger , "pre_request" , provider , modelName , sanitizeStats ,
slog . F ( "step_index" , step ),
slog . F ( "total_steps" , totalSteps ),
)
2026-05-18 15:20:33 +10:00
prompt , err = chatsanitize . ApplyAnthropicProviderToolGuard (
2026-05-11 17:43:40 +01:00
ctx , opts . Logger , provider , modelName , prompt ,
)
2026-05-18 15:20:33 +10:00
if err != nil {
err = chaterror . WithClassification (
xerrors . Errorf ( "apply anthropic provider tool guard: %w" , err ),
chaterror . ClassifiedError {
Message : "The chat continuation failed due to an internal state mismatch. This is not a configuration or billing issue. Start a new chat to continue." ,
Detail : "Anthropic replay diagnostic: match=provider_tool_guard_postcondition_failed." ,
Kind : codersdk . ChatErrorKindGeneric ,
Provider : provider ,
Retryable : false ,
},
)
return canonical , nil , err
}
2026-05-11 17:43:40 +01:00
if shouldApplyAnthropicPromptCaching ( opts . Model ) {
addAnthropicPromptCaching ( prompt )
}
2026-05-18 15:20:33 +10:00
return canonical , prompt , nil
2026-05-11 17:43:40 +01:00
}
2026-03-25 13:47:54 +11:00
// guardedAttempt owns an attempt-scoped context and startup guard
// around a provider stream. release is idempotent and frees the
// attempt-scoped timer/context. finish canonicalizes startup timeout
// errors before the retry loop classifies them.
type guardedAttempt struct {
ctx context . Context
stream fantasy . StreamResponse
release func ()
finish func ( error ) error
}
// startupGuard arbitrates whether an attempt times out during
// stream startup. Exactly one outcome wins: the timer cancels
// the attempt, or the first-part path disarms the timer.
type startupGuard struct {
2026-04-10 00:40:09 +10:00
timer * quartz . Timer
2026-03-25 13:47:54 +11:00
cancel context . CancelCauseFunc
once sync . Once
}
func newStartupGuard (
2026-04-10 00:40:09 +10:00
clock quartz . Clock ,
2026-03-25 13:47:54 +11:00
timeout time . Duration ,
cancel context . CancelCauseFunc ,
) * startupGuard {
guard := & startupGuard { cancel : cancel }
2026-04-10 00:40:09 +10:00
guard . timer = clock . AfterFunc ( timeout , guard . onTimeout , "startupGuard" )
2026-03-25 13:47:54 +11:00
return guard
}
func ( g * startupGuard ) onTimeout () {
g . once . Do ( func () {
g . cancel ( errStartupTimeout )
})
}
func ( g * startupGuard ) Disarm () {
g . once . Do ( func () {
g . timer . Stop ()
})
}
func classifyStartupTimeout (
attemptCtx context . Context ,
provider string ,
err error ,
) error {
if ! errors . Is ( context . Cause ( attemptCtx ), errStartupTimeout ) {
return err
}
if err == nil {
err = errStartupTimeout
}
return chaterror . WithClassification ( err , chaterror . ClassifiedError {
2026-05-06 11:57:48 +10:00
Kind : codersdk . ChatErrorKindStartupTimeout ,
2026-03-25 13:47:54 +11:00
Provider : provider ,
Retryable : true ,
})
}
func guardedStream (
parent context . Context ,
2026-04-17 16:16:30 +01:00
provider , model string ,
2026-04-10 00:40:09 +10:00
clock quartz . Clock ,
2026-03-25 13:47:54 +11:00
timeout time . Duration ,
openStream func ( context . Context ) ( fantasy . StreamResponse , error ),
2026-04-15 19:53:10 +01:00
metrics * Metrics ,
2026-03-25 13:47:54 +11:00
) ( guardedAttempt , error ) {
attemptCtx , cancelAttempt := context . WithCancelCause ( parent )
2026-04-10 00:40:09 +10:00
guard := newStartupGuard ( clock , timeout , cancelAttempt )
2026-03-25 13:47:54 +11:00
var releaseOnce sync . Once
release := func () {
releaseOnce . Do ( func () {
guard . Disarm ()
cancelAttempt ( nil )
})
}
2026-04-15 19:53:10 +01:00
streamStart := clock . Now ()
2026-03-25 13:47:54 +11:00
stream , err := openStream ( attemptCtx )
if err != nil {
err = classifyStartupTimeout ( attemptCtx , provider , err )
release ()
return guardedAttempt {}, err
}
2026-04-15 19:53:10 +01:00
recordTTFT := sync . OnceFunc ( func () {
2026-04-17 16:16:30 +01:00
metrics . TTFTSeconds . WithLabelValues ( provider , model ). Observe (
2026-04-15 19:53:10 +01:00
clock . Since ( streamStart ). Seconds (),
)
})
2026-03-25 13:47:54 +11:00
return guardedAttempt {
ctx : attemptCtx ,
stream : fantasy . StreamResponse ( func ( yield func ( fantasy . StreamPart ) bool ) {
for part := range stream {
guard . Disarm ()
2026-04-15 19:53:10 +01:00
recordTTFT ()
2026-03-25 13:47:54 +11:00
if ! yield ( part ) {
return
}
}
}),
release : release ,
finish : func ( err error ) error {
return classifyStartupTimeout ( attemptCtx , provider , err )
},
}, nil
}
2026-03-02 18:51:57 -05:00
// processStepStream consumes a fantasy StreamResponse and
// accumulates all content into a stepResult. Callbacks fire
// inline and their errors propagate directly.
func processStepStream (
ctx context . Context ,
stream fantasy . StreamResponse ,
2026-03-13 17:53:26 +02:00
publishMessagePart func ( codersdk . ChatMessageRole , codersdk . ChatMessagePart ),
2026-03-02 18:51:57 -05:00
) ( stepResult , error ) {
var result stepResult
activeToolCalls := make ( map [ string ] * fantasy . ToolCallContent )
activeTextContent := make ( map [ string ] string )
activeReasoningContent := make ( map [ string ] reasoningState )
// Track tool names by ID for input delta publishing.
toolNames := make ( map [ string ] string )
for part := range stream {
switch part . Type {
case fantasy . StreamPartTypeTextStart :
activeTextContent [ part . ID ] = ""
case fantasy . StreamPartTypeTextDelta :
if _ , exists := activeTextContent [ part . ID ]; exists {
activeTextContent [ part . ID ] += part . Delta
}
2026-03-13 17:53:26 +02:00
publishMessagePart ( codersdk . ChatMessageRoleAssistant , codersdk . ChatMessageText ( part . Delta ))
2026-03-02 18:51:57 -05:00
case fantasy . StreamPartTypeTextEnd :
if text , exists := activeTextContent [ part . ID ]; exists {
result . content = append ( result . content , fantasy . TextContent {
Text : text ,
ProviderMetadata : part . ProviderMetadata ,
})
delete ( activeTextContent , part . ID )
}
case fantasy . StreamPartTypeReasoningStart :
activeReasoningContent [ part . ID ] = reasoningState {
2026-05-18 12:30:30 -04:00
text : part . Delta ,
options : part . ProviderMetadata ,
startedAt : dbtime . Now (),
2026-03-02 18:51:57 -05:00
}
case fantasy . StreamPartTypeReasoningDelta :
if active , exists := activeReasoningContent [ part . ID ]; exists {
active . text += part . Delta
2026-05-18 15:20:33 +10:00
if len ( part . ProviderMetadata ) > 0 {
active . options = part . ProviderMetadata
}
2026-03-02 18:51:57 -05:00
activeReasoningContent [ part . ID ] = active
}
2026-03-13 17:53:26 +02:00
publishMessagePart ( codersdk . ChatMessageRoleAssistant , codersdk . ChatMessageReasoning ( part . Delta ))
2026-03-02 18:51:57 -05:00
case fantasy . StreamPartTypeReasoningEnd :
if active , exists := activeReasoningContent [ part . ID ]; exists {
2026-05-18 15:20:33 +10:00
if len ( part . ProviderMetadata ) > 0 {
2026-03-02 18:51:57 -05:00
active . options = part . ProviderMetadata
}
content := fantasy . ReasoningContent {
Text : active . text ,
ProviderMetadata : active . options ,
}
result . content = append ( result . content , content )
2026-05-18 12:30:30 -04:00
result . reasoningStartedAt = append ( result . reasoningStartedAt , active . startedAt )
result . reasoningCompletedAt = append ( result . reasoningCompletedAt , dbtime . Now ())
2026-03-02 18:51:57 -05:00
delete ( activeReasoningContent , part . ID )
}
case fantasy . StreamPartTypeToolInputStart :
activeToolCalls [ part . ID ] = & fantasy . ToolCallContent {
ToolCallID : part . ID ,
ToolName : part . ToolCallName ,
Input : "" ,
ProviderExecuted : part . ProviderExecuted ,
}
if strings . TrimSpace ( part . ToolCallName ) != "" {
toolNames [ part . ID ] = part . ToolCallName
}
case fantasy . StreamPartTypeToolInputDelta :
2026-03-11 14:33:15 -07:00
var providerExecuted bool
2026-03-02 18:51:57 -05:00
if toolCall , exists := activeToolCalls [ part . ID ]; exists {
toolCall . Input += part . Delta
2026-03-11 14:33:15 -07:00
providerExecuted = toolCall . ProviderExecuted
2026-03-02 18:51:57 -05:00
}
toolName := toolNames [ part . ID ]
2026-03-13 17:53:26 +02:00
publishMessagePart ( codersdk . ChatMessageRoleAssistant , codersdk . ChatMessagePart {
2026-03-11 14:33:15 -07:00
Type : codersdk . ChatMessagePartTypeToolCall ,
ToolCallID : part . ID ,
ToolName : toolName ,
ArgsDelta : part . Delta ,
ProviderExecuted : providerExecuted ,
2026-03-02 18:51:57 -05:00
})
case fantasy . StreamPartTypeToolInputEnd :
// No callback needed; the full tool call arrives in
// StreamPartTypeToolCall.
case fantasy . StreamPartTypeToolCall :
tc := fantasy . ToolCallContent {
ToolCallID : part . ID ,
ToolName : part . ToolCallName ,
Input : part . ToolCallInput ,
ProviderExecuted : part . ProviderExecuted ,
ProviderMetadata : part . ProviderMetadata ,
}
result . toolCalls = append ( result . toolCalls , tc )
result . content = append ( result . content , tc )
if strings . TrimSpace ( part . ToolCallName ) != "" {
toolNames [ part . ID ] = part . ToolCallName
}
// Clean up active tool call tracking.
delete ( activeToolCalls , part . ID )
2026-04-08 12:42:03 -04:00
// Record when the model emitted this tool call
// so the persisted part carries an accurate
// timestamp for duration computation.
now := dbtime . Now ()
if result . toolCallCreatedAt == nil {
result . toolCallCreatedAt = make ( map [ string ] time . Time )
}
result . toolCallCreatedAt [ part . ID ] = now
ssePart := chatprompt . PartFromContent ( tc )
ssePart . CreatedAt = & now
2026-03-02 18:51:57 -05:00
publishMessagePart (
2026-03-13 17:53:26 +02:00
codersdk . ChatMessageRoleAssistant ,
2026-04-08 12:42:03 -04:00
ssePart ,
2026-03-02 18:51:57 -05:00
)
case fantasy . StreamPartTypeSource :
sourceContent := fantasy . SourceContent {
SourceType : part . SourceType ,
ID : part . ID ,
URL : part . URL ,
Title : part . Title ,
ProviderMetadata : part . ProviderMetadata ,
}
result . content = append ( result . content , sourceContent )
publishMessagePart (
2026-03-13 17:53:26 +02:00
codersdk . ChatMessageRoleAssistant ,
2026-03-02 18:51:57 -05:00
chatprompt . PartFromContent ( sourceContent ),
)
2026-03-11 14:33:15 -07:00
case fantasy . StreamPartTypeToolResult :
// Provider-executed tool results (e.g. web search)
// are emitted by the provider and added directly
// to the step content for multi-turn round-tripping.
// This mirrors fantasy's agent.go accumulation logic.
if part . ProviderExecuted {
tr := fantasy . ToolResultContent {
ToolCallID : part . ID ,
ToolName : part . ToolCallName ,
ProviderExecuted : part . ProviderExecuted ,
ProviderMetadata : part . ProviderMetadata ,
}
result . content = append ( result . content , tr )
2026-04-08 12:42:03 -04:00
now := dbtime . Now ()
if result . toolResultCreatedAt == nil {
result . toolResultCreatedAt = make ( map [ string ] time . Time )
}
result . toolResultCreatedAt [ part . ID ] = now
ssePart := chatprompt . PartFromContent ( tr )
ssePart . CreatedAt = & now
2026-03-11 14:33:15 -07:00
publishMessagePart (
2026-03-13 17:53:26 +02:00
codersdk . ChatMessageRoleTool ,
2026-04-08 12:42:03 -04:00
ssePart ,
2026-03-11 14:33:15 -07:00
)
}
2026-03-02 18:51:57 -05:00
case fantasy . StreamPartTypeFinish :
result . usage = part . Usage
result . finishReason = part . FinishReason
result . providerMetadata = part . ProviderMetadata
case fantasy . StreamPartTypeError :
2026-03-18 07:48:28 -04:00
// Detect interruption: the stream may surface the
// cancel as context.Canceled or propagate the
// ErrInterrupted cause directly, depending on
// the provider implementation.
if errors . Is ( context . Cause ( ctx ), ErrInterrupted ) &&
( errors . Is ( part . Error , context . Canceled ) || errors . Is ( part . Error , ErrInterrupted )) {
2026-03-02 18:51:57 -05:00
// Flush in-progress content so that
// persistInterruptedStep has access to partial
// text, reasoning, and tool calls that were
// still streaming when the interrupt arrived.
flushActiveState (
& result ,
activeTextContent ,
activeReasoningContent ,
activeToolCalls ,
toolNames ,
)
return result , ErrInterrupted
}
return result , part . Error
}
}
2026-03-18 07:48:28 -04:00
// The stream iterator may stop yielding parts without
// producing a StreamPartTypeError when the context is
// canceled (e.g. some providers close the response body
// silently). Detect this case and flush partial content
// so that persistInterruptedStep can save it.
if ctx . Err () != nil &&
errors . Is ( context . Cause ( ctx ), ErrInterrupted ) {
flushActiveState (
& result ,
activeTextContent ,
activeReasoningContent ,
activeToolCalls ,
toolNames ,
)
return result , ErrInterrupted
}
2026-03-11 14:33:15 -07:00
hasLocalToolCalls := false
for _ , tc := range result . toolCalls {
if ! tc . ProviderExecuted {
hasLocalToolCalls = true
break
}
}
result . shouldContinue = hasLocalToolCalls &&
2026-03-02 18:51:57 -05:00
result . finishReason == fantasy . FinishReasonToolCalls
2026-02-27 11:50:56 -05:00
return result , nil
}
2026-03-11 11:00:08 -07:00
// executeTools runs all tool calls concurrently after the stream
// completes. Results are published via onResult in the original
// tool-call order after all tools finish, preserving deterministic
// event ordering for SSE subscribers.
2026-03-02 18:51:57 -05:00
func executeTools (
ctx context . Context ,
allTools [] fantasy . AgentTool ,
2026-04-16 11:12:01 +02:00
activeTools [] string ,
2026-03-13 19:49:34 +01:00
providerTools [] ProviderTool ,
2026-03-02 18:51:57 -05:00
toolCalls [] fantasy . ToolCallContent ,
2026-04-15 19:53:10 +01:00
metrics * Metrics ,
2026-04-22 17:19:56 +01:00
logger slog . Logger ,
2026-04-17 16:16:30 +01:00
provider , model string ,
2026-04-15 19:53:10 +01:00
builtinToolNames map [ string ] bool ,
2026-04-08 12:42:03 -04:00
onResult func ( fantasy . ToolResultContent , time . Time ),
2026-03-02 18:51:57 -05:00
) [] fantasy . ToolResultContent {
if len ( toolCalls ) == 0 {
return nil
2026-02-27 11:50:56 -05:00
}
2026-03-11 14:33:15 -07:00
// Filter out provider-executed tool calls. These were
// handled server-side by the LLM provider (e.g., web
// search) and their results are already in the stream
// content.
localToolCalls := make ([] fantasy . ToolCallContent , 0 , len ( toolCalls ))
for _ , tc := range toolCalls {
if ! tc . ProviderExecuted {
localToolCalls = append ( localToolCalls , tc )
}
}
if len ( localToolCalls ) == 0 {
return nil
}
2026-03-02 18:51:57 -05:00
toolMap := make ( map [ string ] fantasy . AgentTool , len ( allTools ))
for _ , t := range allTools {
toolMap [ t . Info (). Name ] = t
2026-02-27 11:50:56 -05:00
}
2026-04-16 11:12:01 +02:00
providerRunnerNames := make ( map [ string ] struct {}, len ( providerTools ))
2026-05-04 20:30:50 +02:00
resultProviderMetadata := make (
map [ string ] func ( fantasy . ToolResponse ) fantasy . ProviderMetadata ,
len ( providerTools ),
)
2026-03-13 19:49:34 +01:00
// Include runners from provider tools so locally-executed
// provider tools (e.g. computer use) can be dispatched.
for _ , pt := range providerTools {
2026-05-04 20:30:50 +02:00
if pt . Runner == nil {
continue
}
name := pt . Runner . Info (). Name
toolMap [ name ] = pt . Runner
providerRunnerNames [ name ] = struct {}{}
if pt . ResultProviderMetadata != nil {
resultProviderMetadata [ name ] = pt . ResultProviderMetadata
2026-03-13 19:49:34 +01:00
}
}
2026-03-02 18:51:57 -05:00
2026-03-11 14:33:15 -07:00
results := make ([] fantasy . ToolResultContent , len ( localToolCalls ))
2026-04-08 12:42:03 -04:00
completedAt := make ([] time . Time , len ( localToolCalls ))
2026-03-11 11:00:08 -07:00
var wg sync . WaitGroup
2026-03-11 14:33:15 -07:00
wg . Add ( len ( localToolCalls ))
for i , tc := range localToolCalls {
2026-04-08 12:42:03 -04:00
go func () {
2026-03-11 11:00:08 -07:00
defer wg . Done ()
defer func () {
if r := recover (); r != nil {
results [ i ] = fantasy . ToolResultContent {
ToolCallID : tc . ToolCallID ,
ToolName : tc . ToolName ,
Result : fantasy . ToolResultOutputContentError {
Error : xerrors . Errorf ( "tool panicked: %v" , r ),
},
}
}
2026-04-08 12:42:03 -04:00
// Record when this tool completed (or panicked).
// Captured per-goroutine so parallel tools get
// accurate individual completion times.
completedAt [ i ] = dbtime . Now ()
2026-03-11 11:00:08 -07:00
}()
2026-05-04 20:30:50 +02:00
results [ i ] = executeSingleTool (
ctx ,
toolMap ,
tc ,
metrics ,
logger ,
provider ,
model ,
builtinToolNames ,
activeTools ,
providerRunnerNames ,
resultProviderMetadata ,
)
2026-04-08 12:42:03 -04:00
}()
2026-03-11 11:00:08 -07:00
}
wg . Wait ()
// Publish results in the original tool-call order so SSE
// subscribers see a deterministic event sequence.
if onResult != nil {
2026-04-08 12:42:03 -04:00
for i , tr := range results {
onResult ( tr , completedAt [ i ])
2026-03-02 18:51:57 -05:00
}
2026-02-27 11:50:56 -05:00
}
2026-03-02 18:51:57 -05:00
return results
}
2026-04-30 14:08:59 +02:00
// executeToolsForStep runs the tool-execution phase of a single
// chatloop step. It enforces the exclusive-tool policy, partitions
// built-in versus dynamic tool calls, dispatches built-in tools, and
// when dynamic tool calls are present persists the step and returns
// ErrDynamicToolCall so the caller can execute them externally.
// Returns the tool results to append to the step, or an error that the
// caller must propagate (ErrInterrupted, ErrDynamicToolCall, ctx.Err(),
// or a persistence failure).
func executeToolsForStep (
ctx context . Context ,
opts RunOptions ,
result * stepResult ,
provider , modelName string ,
step int ,
stepStart time . Time ,
publishMessagePart func ( codersdk . ChatMessageRole , codersdk . ChatMessagePart ),
) ([] fantasy . ToolResultContent , error ) {
// Check for context cancellation before starting tool
// execution. If the chat was interrupted between stream
// completion and here, persist what we have and bail out.
if ctx . Err () != nil {
if errors . Is ( context . Cause ( ctx ), ErrInterrupted ) {
persistInterruptedStep ( ctx , opts , result )
return nil , ErrInterrupted
}
return nil , ctx . Err ()
}
// Enforce exclusivity across ALL locally-executable tool
// calls (both built-in and dynamic) before partitioning.
// Checking only the built-in partition would let the model
// bypass the policy by mixing an exclusive tool with a
// dynamic tool: the exclusive tool would still run and the
// dynamic call would still be handed to the caller for
// external execution, breaking the planning-only contract.
localCandidates := make ([] fantasy . ToolCallContent , 0 , len ( result . toolCalls ))
for _ , tc := range result . toolCalls {
if ! tc . ProviderExecuted {
localCandidates = append ( localCandidates , tc )
}
}
policyResults , exclusiveViolation := applyExclusiveToolPolicy (
localCandidates ,
opts . ExclusiveToolNames ,
opts . Metrics ,
provider ,
modelName ,
)
if exclusiveViolation {
now := dbtime . Now ()
for _ , tr := range policyResults {
recordToolResultTimestamp ( result , tr . ToolCallID , now )
publishToolAttachments ( ctx , opts . Logger , tr , now , publishMessagePart )
ssePart := chatprompt . PartFromContentWithLogger ( ctx , opts . Logger , tr )
ssePart . CreatedAt = & now
publishMessagePart ( codersdk . ChatMessageRoleTool , ssePart )
}
for _ , tr := range policyResults {
result . content = append ( result . content , tr )
}
// Mirror the post-execution interruption check used by the
// non-policy path: if the chat was interrupted while we
// synthesized policy errors, route through
// persistInterruptedStep so the synthesized results are not
// dropped when the regular PersistStep path fails on a
// canceled context.
if ctx . Err () != nil {
if errors . Is ( context . Cause ( ctx ), ErrInterrupted ) {
persistInterruptedStep ( ctx , opts , result )
return nil , ErrInterrupted
}
return nil , ctx . Err ()
}
// Fall through to the normal persistence path so the loop
// continues with error results that the model can observe
// and retry. Skip partitioning, execution, and
// pending-dynamic persistence.
return policyResults , nil
}
// Partition tool calls into built-in and dynamic.
var builtinCalls , dynamicCalls [] fantasy . ToolCallContent
if len ( opts . DynamicToolNames ) > 0 {
for _ , tc := range result . toolCalls {
if opts . DynamicToolNames [ tc . ToolName ] {
dynamicCalls = append ( dynamicCalls , tc )
} else {
builtinCalls = append ( builtinCalls , tc )
}
}
} else {
builtinCalls = result . toolCalls
}
// Execute only built-in tools.
toolResults := executeTools ( ctx , opts . Tools , opts . ActiveTools , opts . ProviderTools , builtinCalls , opts . Metrics , opts . Logger , provider , modelName , opts . BuiltinToolNames , func ( tr fantasy . ToolResultContent , completedAt time . Time ) {
recordToolResultTimestamp ( result , tr . ToolCallID , completedAt )
publishToolAttachments ( ctx , opts . Logger , tr , completedAt , publishMessagePart )
ssePart := chatprompt . PartFromContentWithLogger ( ctx , opts . Logger , tr )
ssePart . CreatedAt = & completedAt
publishMessagePart ( codersdk . ChatMessageRoleTool , ssePart )
})
for _ , tr := range toolResults {
result . content = append ( result . content , tr )
}
// If dynamic tools were called, persist what we have
// (assistant + built-in results) and exit so the caller can
// execute them externally.
if len ( dynamicCalls ) > 0 {
// Strip Anthropic provider-executed tool calls without
// matching results before persisting so the action-required
// step does not carry a malformed tool-call history into
// downstream provider requests.
result . content = chatsanitize . SanitizeAnthropicProviderToolStepContent (
ctx , opts . Logger , provider , modelName ,
"dynamic_tool_persist" , step , result . finishReason , result . content ,
)
if err := persistPendingDynamicStep ( ctx , opts , result , stepStart , dynamicCalls ); err != nil {
return nil , err
}
tryCompactOnExit ( ctx , opts , result . usage , result . providerMetadata )
return nil , ErrDynamicToolCall
}
// Check for interruption after tool execution. Tools that
// were canceled mid-flight produce error results via ctx
// cancellation. Persist the full step (assistant blocks +
// tool results) through the interrupt-safe path so nothing
// is lost.
if ctx . Err () != nil {
if errors . Is ( context . Cause ( ctx ), ErrInterrupted ) {
persistInterruptedStep ( ctx , opts , result )
return nil , ErrInterrupted
}
return nil , ctx . Err ()
}
return toolResults , nil
}
// persistPendingDynamicStep persists a step that has pending dynamic
// tool calls awaiting external execution. Returns ErrInterrupted when
// persistence fails because the chat was interrupted.
func persistPendingDynamicStep (
ctx context . Context ,
opts RunOptions ,
result * stepResult ,
stepStart time . Time ,
dynamicCalls [] fantasy . ToolCallContent ,
) error {
pending := make ([] PendingToolCall , 0 , len ( dynamicCalls ))
for _ , dc := range dynamicCalls {
pending = append ( pending , PendingToolCall {
ToolCallID : dc . ToolCallID ,
ToolName : dc . ToolName ,
Args : dc . Input ,
})
}
contextLimit := extractContextLimitWithFallback ( result . providerMetadata , opts . ContextLimitFallback )
if err := opts . PersistStep ( ctx , PersistedStep {
Content : result . content ,
Usage : result . usage ,
ContextLimit : contextLimit ,
2026-05-04 11:17:19 +02:00
ProviderResponseID : chatopenai . ExtractResponseIDIfStored ( opts . ProviderOptions , result . providerMetadata ),
2026-04-30 14:08:59 +02:00
Runtime : time . Since ( stepStart ),
PendingDynamicToolCalls : pending ,
2026-05-18 12:30:30 -04:00
ReasoningStartedAt : result . reasoningStartedAt ,
ReasoningCompletedAt : result . reasoningCompletedAt ,
2026-04-30 14:08:59 +02:00
}); err != nil {
if errors . Is ( err , ErrInterrupted ) {
persistInterruptedStep ( ctx , opts , result )
return ErrInterrupted
}
return xerrors . Errorf ( "persist step: %w" , err )
}
return nil
}
// applyExclusiveToolPolicy checks whether toolCalls violate the
// exclusive-tool policy declared by exclusiveToolNames. When a
// violation is detected it synthesizes deterministic policy-error
// results for every tool call and records size/error metrics so the
// exclusivity failure mode is visible to operators. Returns
// (results, true) on violation; (nil, false) otherwise.
func applyExclusiveToolPolicy (
toolCalls [] fantasy . ToolCallContent ,
exclusiveToolNames map [ string ] bool ,
metrics * Metrics ,
provider , model string ,
) ([] fantasy . ToolResultContent , bool ) {
blockingToolName , ok := firstExclusiveToolName ( toolCalls , exclusiveToolNames )
if ! ok {
return nil , false
}
results := exclusiveToolPolicyResults ( toolCalls , exclusiveToolNames , blockingToolName )
for _ , tr := range results {
recordToolResultMetrics ( metrics , provider , model , tr )
}
return results , true
}
// recordToolResultMetrics observes tool result size and increments
// tool_errors_total when the result carries an error output. Mirrors
// the metric-recording defer in executeSingleTool so that synthetic
// results (e.g. exclusive-tool policy errors) contribute to operator
// visibility.
func recordToolResultMetrics ( metrics * Metrics , provider , model string , tr fantasy . ToolResultContent ) {
if metrics == nil {
return
}
label := tr . ToolName
if label == "" {
label = "unknown"
}
metrics . ToolResultSizeBytes . WithLabelValues ( provider , model , label ). Observe (
float64 ( ToolResultSize ( tr )),
)
if _ , ok := tr . Result .( fantasy . ToolResultOutputContentError ); ok {
metrics . RecordToolError ( provider , model , label )
}
}
func firstExclusiveToolName (
toolCalls [] fantasy . ToolCallContent ,
exclusiveToolNames map [ string ] bool ,
) ( string , bool ) {
if len ( toolCalls ) <= 1 || len ( exclusiveToolNames ) == 0 {
return "" , false
}
for _ , tc := range toolCalls {
if exclusiveToolNames [ tc . ToolName ] {
return tc . ToolName , true
}
}
return "" , false
}
func exclusiveToolPolicyResults (
toolCalls [] fantasy . ToolCallContent ,
exclusiveToolNames map [ string ] bool ,
blockingToolName string ,
) [] fantasy . ToolResultContent {
results := make ([] fantasy . ToolResultContent , len ( toolCalls ))
for i , tc := range toolCalls {
message := exclusiveToolSkippedErrorMessage ( blockingToolName )
if exclusiveToolNames [ tc . ToolName ] {
message = exclusiveToolMustRunAloneErrorMessage ( tc . ToolName )
}
results [ i ] = fantasy . ToolResultContent {
ToolCallID : tc . ToolCallID ,
ToolName : tc . ToolName ,
Result : fantasy . ToolResultOutputContentError {
Error : xerrors . New ( message ),
},
}
}
return results
}
func exclusiveToolMustRunAloneErrorMessage ( toolName string ) string {
return toolName + " must be called alone, without other tools in the same batch. Retry with only the " + toolName + " call."
}
func exclusiveToolSkippedErrorMessage ( toolName string ) string {
return "this tool was skipped because " + toolName + " must run alone in its batch. Retry your tool calls without " + toolName + ", or call " + toolName + " separately first."
}
2026-03-02 18:51:57 -05:00
// executeSingleTool executes one tool call and converts the
// response into a ToolResultContent.
func executeSingleTool (
ctx context . Context ,
toolMap map [ string ] fantasy . AgentTool ,
tc fantasy . ToolCallContent ,
2026-04-15 19:53:10 +01:00
metrics * Metrics ,
2026-04-22 17:19:56 +01:00
logger slog . Logger ,
2026-04-17 16:16:30 +01:00
provider , model string ,
2026-04-15 19:53:10 +01:00
builtinToolNames map [ string ] bool ,
2026-04-16 11:12:01 +02:00
activeTools [] string ,
providerRunnerNames map [ string ] struct {},
2026-05-04 20:30:50 +02:00
resultProviderMetadata map [ string ] func ( fantasy . ToolResponse ) fantasy . ProviderMetadata ,
2026-03-02 18:51:57 -05:00
) fantasy . ToolResultContent {
result := fantasy . ToolResultContent {
ToolCallID : tc . ToolCallID ,
ToolName : tc . ToolName ,
ProviderExecuted : false ,
}
2026-04-15 19:53:10 +01:00
defer func () {
2026-04-22 17:19:56 +01:00
metricLabel := tc . ToolName
if metricLabel == "" {
metricLabel = "unknown"
2026-04-15 19:53:10 +01:00
}
2026-04-22 17:19:56 +01:00
metrics . ToolResultSizeBytes . WithLabelValues ( provider , model , metricLabel ). Observe (
2026-04-15 19:53:10 +01:00
float64 ( ToolResultSize ( result )),
)
2026-04-22 17:19:56 +01:00
if _ , ok := result . Result .( fantasy . ToolResultOutputContentError ); ok {
metrics . RecordToolError ( provider , model , metricLabel )
}
2026-04-15 19:53:10 +01:00
}()
2026-03-02 18:51:57 -05:00
2026-04-22 17:19:56 +01:00
_ , isProviderRunner := providerRunnerNames [ tc . ToolName ]
if ! isProviderRunner && ! isToolActive ( tc . ToolName , activeTools ) {
2026-04-16 11:12:01 +02:00
result . Result = fantasy . ToolResultOutputContentError {
Error : xerrors . New ( "Tool not active in this turn: " + tc . ToolName ),
}
return result
}
2026-03-02 18:51:57 -05:00
tool , exists := toolMap [ tc . ToolName ]
if ! exists {
result . Result = fantasy . ToolResultOutputContentError {
Error : xerrors . New ( "Tool not found: " + tc . ToolName ),
}
return result
}
2026-04-22 17:19:56 +01:00
logger . Debug ( ctx , "tool execution" ,
slog . F ( "tool_name" , tc . ToolName ),
slog . F ( "tool_call_id" , tc . ToolCallID ),
slog . F ( "builtin" , builtinToolNames [ tc . ToolName ]),
slog . F ( "is_provider_runner" , isProviderRunner ),
)
2026-03-02 18:51:57 -05:00
resp , err := tool . Run ( ctx , fantasy . ToolCall {
ID : tc . ToolCallID ,
Name : tc . ToolName ,
Input : tc . Input ,
})
if err != nil {
result . Result = fantasy . ToolResultOutputContentError {
Error : err ,
}
result . ClientMetadata = resp . Metadata
2026-04-22 17:19:56 +01:00
logger . Error ( ctx , "tool execution failed" ,
slog . F ( "tool_name" , tc . ToolName ),
slog . F ( "tool_call_id" , tc . ToolCallID ),
slog . Error ( err ),
)
2026-03-02 18:51:57 -05:00
return result
}
result . ClientMetadata = resp . Metadata
switch {
case resp . IsError :
result . Result = fantasy . ToolResultOutputContentError {
Error : xerrors . New ( resp . Content ),
}
2026-04-22 17:19:56 +01:00
logger . Info ( ctx , "tool returned error result" ,
slog . F ( "tool_name" , tc . ToolName ),
slog . F ( "tool_call_id" , tc . ToolCallID ),
slog . F ( "tool_error" , resp . Content ),
)
2026-03-02 18:51:57 -05:00
case resp . Type == "image" || resp . Type == "media" :
result . Result = fantasy . ToolResultOutputContentMedia {
2026-04-23 19:58:38 +01:00
Data : base64 . StdEncoding . EncodeToString ( resp . Data ),
2026-03-02 18:51:57 -05:00
MediaType : resp . MediaType ,
2026-04-23 19:58:38 +01:00
Text : strings . ToValidUTF8 ( resp . Content , "\uFFFD" ),
2026-03-02 18:51:57 -05:00
}
default :
result . Result = fantasy . ToolResultOutputContentText {
2026-04-23 19:58:38 +01:00
Text : strings . ToValidUTF8 ( resp . Content , "\uFFFD" ),
2026-03-02 18:51:57 -05:00
}
2026-02-27 11:50:56 -05:00
}
2026-05-04 20:30:50 +02:00
if _ , isError := result . Result .( fantasy . ToolResultOutputContentError ); isError {
return result
}
if len ( result . ProviderMetadata ) == 0 {
if callback := resultProviderMetadata [ tc . ToolName ]; callback != nil {
metadata := callback ( resp )
if len ( metadata ) > 0 {
result . ProviderMetadata = metadata
}
}
}
2026-02-27 11:50:56 -05:00
return result
}
2026-03-02 18:51:57 -05:00
// flushActiveState moves any in-progress text, reasoning, and
// tool calls from the active tracking maps into result.content
// and result.toolCalls. This is called on interruption so that
// partial content from an incomplete stream is available for
// persistence.
func flushActiveState (
result * stepResult ,
activeText map [ string ] string ,
activeReasoning map [ string ] reasoningState ,
activeToolCalls map [ string ] * fantasy . ToolCallContent ,
toolNames map [ string ] string ,
) {
// Flush partial text content.
for _ , text := range activeText {
if text != "" {
result . content = append ( result . content , fantasy . TextContent { Text : text })
}
}
2026-05-18 12:30:30 -04:00
// Flush partial reasoning content. The matching
// completedAt is filled in here with the interruption
// time so partial reasoning shows the time spent before
// the interruption.
flushedAt := dbtime . Now ()
2026-03-02 18:51:57 -05:00
for _ , rs := range activeReasoning {
2026-05-18 15:20:33 +10:00
if rs . text == "" && ! chatsanitize . HasAnthropicSignedReasoningOptions ( fantasy . ProviderOptions ( rs . options )) {
continue
2026-03-02 18:51:57 -05:00
}
2026-05-18 15:20:33 +10:00
result . content = append ( result . content , fantasy . ReasoningContent {
Text : rs . text ,
ProviderMetadata : rs . options ,
})
2026-05-18 12:30:30 -04:00
result . reasoningStartedAt = append ( result . reasoningStartedAt , rs . startedAt )
result . reasoningCompletedAt = append ( result . reasoningCompletedAt , flushedAt )
2026-03-02 18:51:57 -05:00
}
// Flush in-progress tool calls. These haven't received a
// StreamPartTypeToolCall yet, so they only exist in
// activeToolCalls. We add them to both content and toolCalls
// so persistInterruptedStep can generate synthetic error
// results for them.
for id , tc := range activeToolCalls {
if tc == nil {
continue
}
// Prefer the tool name from the toolNames map since
// ToolInputStart may provide a cleaner name.
toolName := tc . ToolName
if name , ok := toolNames [ id ]; ok && strings . TrimSpace ( name ) != "" {
toolName = name
}
flushed := fantasy . ToolCallContent {
ToolCallID : tc . ToolCallID ,
ToolName : toolName ,
Input : tc . Input ,
ProviderExecuted : tc . ProviderExecuted ,
}
result . content = append ( result . content , flushed )
result . toolCalls = append ( result . toolCalls , flushed )
}
}
2026-04-24 23:57:30 +02:00
// persistInterruptedStep saves durable content from a partial stream.
2026-05-18 15:20:33 +10:00
// Provider-executed calls without results are removed because their result
// metadata cannot be synthesized safely, except when removal would mutate
// signed Anthropic replay state.
2026-03-02 18:51:57 -05:00
func persistInterruptedStep (
ctx context . Context ,
opts RunOptions ,
result * stepResult ,
) {
if result == nil || ( len ( result . content ) == 0 && len ( result . toolCalls ) == 0 ) {
return
}
2026-04-24 23:57:30 +02:00
provider := ""
modelName := ""
if opts . Model != nil {
provider = opts . Model . Provider ()
modelName = opts . Model . Model ()
}
2026-04-28 12:45:23 +02:00
var sanitizeStats chatsanitize . AnthropicProviderToolSanitizationStats
result . content , sanitizeStats = chatsanitize . SanitizeAnthropicProviderToolContent ( provider , result . content )
chatsanitize . LogAnthropicProviderToolSanitization (
2026-04-24 23:57:30 +02:00
ctx , opts . Logger , "interrupted_persist" , provider , modelName , sanitizeStats ,
)
2026-03-02 18:51:57 -05:00
// Track which tool calls already have results in the content.
answeredToolCalls := make ( map [ string ] struct {})
for _ , c := range result . content {
tr , ok := fantasy . AsContentType [ fantasy . ToolResultContent ]( c )
if ok && tr . ToolCallID != "" {
answeredToolCalls [ tr . ToolCallID ] = struct {}{}
}
}
2026-04-08 12:42:03 -04:00
// Copy existing timestamps and add result timestamps for
// interrupted tool calls so the frontend can show partial
// duration.
toolCallCreatedAt := maps . Clone ( result . toolCallCreatedAt )
if toolCallCreatedAt == nil {
toolCallCreatedAt = make ( map [ string ] time . Time )
}
toolResultCreatedAt := maps . Clone ( result . toolResultCreatedAt )
if toolResultCreatedAt == nil {
toolResultCreatedAt = make ( map [ string ] time . Time )
}
2026-03-02 18:51:57 -05:00
// Build combined content: all accumulated content + synthetic
// interrupted results for any unanswered tool calls.
content := make ([] fantasy . Content , 0 , len ( result . content ))
content = append ( content , result . content ... )
2026-04-08 12:42:03 -04:00
interruptedAt := dbtime . Now ()
2026-03-02 18:51:57 -05:00
for _ , tc := range result . toolCalls {
if tc . ToolCallID == "" {
continue
}
if _ , exists := answeredToolCalls [ tc . ToolCallID ]; exists {
continue
}
2026-04-28 12:45:23 +02:00
if chatsanitize . IsAnthropicProviderExecutedToolCall ( provider , tc ) {
2026-04-24 23:57:30 +02:00
continue
}
2026-03-02 18:51:57 -05:00
content = append ( content , fantasy . ToolResultContent {
2026-03-11 14:33:15 -07:00
ToolCallID : tc . ToolCallID ,
ToolName : tc . ToolName ,
ProviderExecuted : tc . ProviderExecuted ,
2026-03-02 18:51:57 -05:00
Result : fantasy . ToolResultOutputContentError {
Error : xerrors . New ( interruptedToolResultErrorMessage ),
},
})
2026-04-08 12:42:03 -04:00
// Only stamp synthetic results; don't clobber
// timestamps from tools that completed before
// the interruption arrived.
if _ , exists := toolResultCreatedAt [ tc . ToolCallID ]; ! exists {
toolResultCreatedAt [ tc . ToolCallID ] = interruptedAt
}
2026-03-02 18:51:57 -05:00
answeredToolCalls [ tc . ToolCallID ] = struct {}{}
}
2026-04-24 23:57:30 +02:00
if len ( content ) == 0 {
return
}
2026-03-02 18:51:57 -05:00
persistCtx := context . WithoutCancel ( ctx )
if err := opts . PersistStep ( persistCtx , PersistedStep {
2026-05-18 12:30:30 -04:00
Content : content ,
ToolCallCreatedAt : toolCallCreatedAt ,
ToolResultCreatedAt : toolResultCreatedAt ,
ReasoningStartedAt : result . reasoningStartedAt ,
ReasoningCompletedAt : result . reasoningCompletedAt ,
2026-03-02 18:51:57 -05:00
}); err != nil {
if opts . OnInterruptedPersistError != nil {
opts . OnInterruptedPersistError ( err )
}
}
}
2026-04-08 11:54:44 -04:00
// tryCompactOnExit runs compaction when the chatloop is about
// to exit early (e.g. via ErrDynamicToolCall). The normal
// inline and post-run compaction paths are unreachable in
// early-exit scenarios, so this ensures the context window
// doesn't grow unbounded.
func tryCompactOnExit (
ctx context . Context ,
opts RunOptions ,
usage fantasy . Usage ,
metadata fantasy . ProviderMetadata ,
) {
if opts . Compaction == nil || opts . ReloadMessages == nil {
return
}
reloaded , err := opts . ReloadMessages ( ctx )
if err != nil {
return
}
2026-04-15 19:53:10 +01:00
did , compactErr := tryCompact (
2026-04-08 11:54:44 -04:00
ctx ,
opts . Model ,
opts . Compaction ,
opts . ContextLimitFallback ,
usage ,
metadata ,
reloaded ,
)
2026-04-17 16:16:30 +01:00
opts . Metrics . RecordCompaction ( opts . Model . Provider (), opts . Model . Model (), did , compactErr )
2026-04-08 11:54:44 -04:00
if compactErr != nil && opts . Compaction . OnError != nil {
opts . Compaction . OnError ( compactErr )
}
}
2026-04-16 11:12:01 +02:00
func isToolActive ( name string , activeTools [] string ) bool {
return len ( activeTools ) == 0 || slices . Contains ( activeTools , name )
}
2026-05-12 00:30:56 -04:00
// mergeNewToolNames returns activeTools augmented with any tool names
// from newTools that are not present in oldTools and not already in
// activeTools. This keeps newly injected tools (e.g. via PrepareTools)
// callable even when activeTools is non-empty.
//
// When activeTools is empty, all tools are already active and the slice
// is returned unchanged.
func mergeNewToolNames ( activeTools [] string , oldTools , newTools [] fantasy . AgentTool ) [] string {
if len ( activeTools ) == 0 {
return activeTools
}
old := make ( map [ string ] struct {}, len ( oldTools ))
for _ , t := range oldTools {
old [ t . Info (). Name ] = struct {}{}
}
active := make ( map [ string ] struct {}, len ( activeTools ))
for _ , name := range activeTools {
active [ name ] = struct {}{}
}
for _ , t := range newTools {
name := t . Info (). Name
if _ , alreadyActive := active [ name ]; alreadyActive {
continue
}
if _ , existedBefore := old [ name ]; existedBefore {
continue
}
activeTools = append ( activeTools , name )
active [ name ] = struct {}{}
}
return activeTools
}
2026-03-02 18:51:57 -05:00
// buildToolDefinitions converts AgentTool definitions into the
// fantasy.Tool slice expected by fantasy.Call. When activeTools
2026-03-11 14:33:15 -07:00
// is non-empty, only function tools whose name appears in the
2026-03-13 19:49:34 +01:00
// list are included. Provider tool definitions are always
// appended unconditionally.
func buildToolDefinitions ( tools [] fantasy . AgentTool , activeTools [] string , providerTools [] ProviderTool ) [] fantasy . Tool {
prepared := make ([] fantasy . Tool , 0 , len ( tools ) + len ( providerTools ))
2026-03-02 18:51:57 -05:00
for _ , tool := range tools {
info := tool . Info ()
2026-04-16 11:12:01 +02:00
if ! isToolActive ( info . Name , activeTools ) {
2026-03-02 18:51:57 -05:00
continue
}
2026-03-13 19:49:34 +01:00
2026-03-02 18:51:57 -05:00
inputSchema := map [ string ] any {
"type" : "object" ,
"properties" : info . Parameters ,
2026-03-24 18:29:41 -04:00
}
// Only include "required" when non-empty so that a nil slice
// never serializes to null, which OpenAI rejects.
if len ( info . Required ) > 0 {
inputSchema [ "required" ] = info . Required
2026-03-02 18:51:57 -05:00
}
schema . Normalize ( inputSchema )
prepared = append ( prepared , fantasy . FunctionTool {
Name : info . Name ,
Description : info . Description ,
InputSchema : inputSchema ,
ProviderOptions : tool . ProviderOptions (),
})
}
2026-03-13 19:49:34 +01:00
for _ , pt := range providerTools {
prepared = append ( prepared , pt . Definition )
}
2026-03-02 18:51:57 -05:00
return prepared
}
2026-04-16 11:12:01 +02:00
// shouldStopAfterTools returns true if any tool result in the
// slice matches a name in stopTools and produced a successful
// (non-error) result.
func shouldStopAfterTools ( stopTools map [ string ] struct {}, results [] fantasy . ToolResultContent ) bool {
if len ( stopTools ) == 0 {
return false
}
for _ , tr := range results {
if _ , ok := stopTools [ tr . ToolName ]; ! ok {
continue
}
if _ , isErr := tr . Result .( fantasy . ToolResultOutputContentError ); ! isErr {
return true
}
}
return false
}
2026-02-27 11:50:56 -05:00
func shouldApplyAnthropicPromptCaching ( model fantasy . LanguageModel ) bool {
if model == nil {
return false
}
return model . Provider () == fantasyanthropic . Name
}
2026-03-02 18:51:57 -05:00
// addAnthropicPromptCaching mutates messages in-place, setting
// ProviderOptions for Anthropic prompt caching on the last system
// message and the final two messages.
func addAnthropicPromptCaching ( messages [] fantasy . Message ) {
2026-02-27 11:50:56 -05:00
for i := range messages {
messages [ i ]. ProviderOptions = nil
}
providerOption := fantasy . ProviderOptions {
fantasyanthropic . Name : & fantasyanthropic . ProviderCacheControlOptions {
CacheControl : fantasyanthropic . CacheControl { Type : "ephemeral" },
},
}
lastSystemRoleIdx := - 1
systemMessageUpdated := false
for i , msg := range messages {
if msg . Role == fantasy . MessageRoleSystem {
lastSystemRoleIdx = i
} else if ! systemMessageUpdated && lastSystemRoleIdx >= 0 {
messages [ lastSystemRoleIdx ]. ProviderOptions = providerOption
systemMessageUpdated = true
}
if i > len ( messages ) - 3 {
messages [ i ]. ProviderOptions = providerOption
}
}
}
2026-04-08 12:42:03 -04:00
// recordToolResultTimestamp lazily initializes the
// toolResultCreatedAt map on the stepResult and records
// the completion timestamp for the given tool-call ID.
func recordToolResultTimestamp ( result * stepResult , toolCallID string , ts time . Time ) {
if result . toolResultCreatedAt == nil {
result . toolResultCreatedAt = make ( map [ string ] time . Time )
}
result . toolResultCreatedAt [ toolCallID ] = ts
}
2026-04-22 20:11:53 +10:00
func publishToolAttachments (
ctx context . Context ,
logger slog . Logger ,
tr fantasy . ToolResultContent ,
createdAt time . Time ,
publishMessagePart func ( codersdk . ChatMessageRole , codersdk . ChatMessagePart ),
) {
attachments , err := chattool . AttachmentsFromMetadata ( tr . ClientMetadata )
if err != nil {
logger . Warn ( ctx , "skipping malformed tool attachment metadata" ,
slog . F ( "tool_name" , tr . ToolName ),
slog . F ( "tool_call_id" , tr . ToolCallID ),
slog . Error ( err ),
)
return
}
for _ , attachment := range attachments {
filePart := codersdk . ChatMessageFile (
attachment . FileID ,
attachment . MediaType ,
attachment . Name ,
)
filePart . CreatedAt = & createdAt
publishMessagePart ( codersdk . ChatMessageRoleAssistant , filePart )
}
}
2026-02-27 11:50:56 -05:00
func extractContextLimit ( metadata fantasy . ProviderMetadata ) sql . NullInt64 {
if len ( metadata ) == 0 {
return sql . NullInt64 {}
}
encoded , err := json . Marshal ( metadata )
if err != nil || len ( encoded ) == 0 {
return sql . NullInt64 {}
}
var payload any
if err := json . Unmarshal ( encoded , & payload ); err != nil {
return sql . NullInt64 {}
}
limit , ok := findContextLimitValue ( payload )
if ! ok {
return sql . NullInt64 {}
}
return sql . NullInt64 {
Int64 : limit ,
Valid : true ,
}
}
2026-04-24 23:57:30 +02:00
func extractContextLimitWithFallback ( metadata fantasy . ProviderMetadata , fallback int64 ) sql . NullInt64 {
contextLimit := extractContextLimit ( metadata )
if contextLimit . Valid || fallback <= 0 {
return contextLimit
}
return sql . NullInt64 {
Int64 : fallback ,
Valid : true ,
}
}
2026-02-27 11:50:56 -05:00
func findContextLimitValue ( value any ) ( int64 , bool ) {
var (
limit int64
found bool
)
collectContextLimitValues ( value , func ( candidate int64 ) {
if ! found || candidate > limit {
limit = candidate
found = true
}
})
return limit , found
}
func collectContextLimitValues ( value any , onValue func ( int64 )) {
switch typed := value .( type ) {
case map [ string ] any :
for key , child := range typed {
if isContextLimitKey ( key ) {
if numeric , ok := numericContextLimitValue ( child ); ok {
onValue ( numeric )
}
}
collectContextLimitValues ( child , onValue )
}
case [] any :
for _ , child := range typed {
collectContextLimitValues ( child , onValue )
}
}
}
func isContextLimitKey ( key string ) bool {
normalized := normalizeMetadataKey ( key )
if normalized == "" {
return false
}
switch normalized {
case
"contextlimit" ,
"contextwindow" ,
"contextlength" ,
"maxcontext" ,
"maxcontexttokens" ,
"maxinputtokens" ,
"maxinputtoken" ,
"inputtokenlimit" :
return true
}
2026-04-02 10:07:01 -07:00
words := metadataKeyWords ( key )
if ! slices . Contains ( words , "context" ) {
return false
}
if slices . Contains ( words , "limit" ) {
return true
}
if slices . Contains ( words , "window" ) {
return slices . Contains ( words , "size" ) || slices . Contains ( words , "max" )
}
if slices . Contains ( words , "length" ) {
return slices . Contains ( words , "max" )
}
return ( slices . Contains ( words , "token" ) || slices . Contains ( words , "tokens" )) &&
( slices . Contains ( words , "max" ) || slices . Contains ( words , "limit" ))
2026-02-27 11:50:56 -05:00
}
func normalizeMetadataKey ( key string ) string {
var b strings . Builder
b . Grow ( len ( key ))
for _ , r := range key {
switch {
case r >= 'a' && r <= 'z' :
_ , _ = b . WriteRune ( r )
case r >= 'A' && r <= 'Z' :
_ , _ = b . WriteRune ( r + ( 'a' - 'A' ))
case r >= '0' && r <= '9' :
_ , _ = b . WriteRune ( r )
}
}
return b . String ()
}
2026-04-02 10:07:01 -07:00
func metadataKeyWords ( key string ) [] string {
words := make ([] string , 0 , 4 )
var current strings . Builder
flush := func () {
if current . Len () == 0 {
return
}
words = append ( words , current . String ())
current . Reset ()
}
var prev rune
var hasPrev bool
for _ , r := range key {
if ! unicode . IsLetter ( r ) {
flush ()
hasPrev = false
continue
}
if hasPrev && unicode . IsUpper ( r ) && unicode . IsLower ( prev ) {
flush ()
}
_ , _ = current . WriteRune ( unicode . ToLower ( r ))
prev = r
hasPrev = true
}
flush ()
return words
}
2026-02-27 11:50:56 -05:00
func numericContextLimitValue ( value any ) ( int64 , bool ) {
switch typed := value .( type ) {
case int64 :
return positiveInt64 ( typed )
case int32 :
return positiveInt64 ( int64 ( typed ))
case int :
return positiveInt64 ( int64 ( typed ))
case float64 :
casted := int64 ( typed )
if typed > 0 && float64 ( casted ) == typed {
return casted , true
}
case string :
parsed , err := strconv . ParseInt ( strings . TrimSpace ( typed ), 10 , 64 )
if err == nil {
return positiveInt64 ( parsed )
}
case json . Number :
parsed , err := typed . Int64 ()
if err == nil {
return positiveInt64 ( parsed )
}
}
return 0 , false
}
func positiveInt64 ( value int64 ) ( int64 , bool ) {
if value <= 0 {
return 0 , false
}
return value , true
}