fix(coderd/x/chatd): preserve tool definitions during compaction
Carry the prepared tool definitions from the parent generation request into compaction summaries so both calls use the same available tools. Move tool-definition construction into a shared helper and cover filtering, schema normalization, ordering, and option preservation.
This commit is contained in:
1 parent
6967259af4
commit
32d261f94f
8 files changed
+268
-93
No files matched your search
@@ -16,7 +16,6 @@ import (
|
||||
|
||||
"charm.land/fantasy"
|
||||
fantasyanthropic "charm.land/fantasy/providers/anthropic"
|
||||
"charm.land/fantasy/schema"
|
||||
"github.com/google/uuid"
|
||||
"golang.org/x/xerrors"
|
||||
|
||||
@@ -218,8 +217,11 @@ type GenerateCompactionOptions struct {
|
||||
ModelConfigID uuid.UUID
|
||||
|
||||
// SummaryCall is copied before GenerateCompaction attaches the summary
|
||||
// prompt.
|
||||
// prompt and prepared tool definitions.
|
||||
SummaryCall fantasy.Call
|
||||
// ToolDefinitions is copied from the parent generation request so the
|
||||
// summary call uses the exact same ordered definitions.
|
||||
ToolDefinitions []fantasy.Tool
|
||||
|
||||
PublishMessagePart func(codersdk.ChatMessageRole, codersdk.ChatMessagePart)
|
||||
|
||||
@@ -308,7 +310,7 @@ func GenerateAssistant(ctx context.Context, opts GenerateAssistantOptions) (Assi
|
||||
|
||||
call := opts.CallTemplate
|
||||
call.Prompt = prepared
|
||||
call.Tools = buildToolDefinitions(opts.Tools, opts.ActiveTools, opts.ProviderTools)
|
||||
call.Tools = BuildToolDefinitions(opts.Tools, opts.ActiveTools, opts.ProviderTools)
|
||||
|
||||
stepStart := opts.Clock.Now()
|
||||
if opts.OnModelStreamStart != nil {
|
||||
@@ -1482,49 +1484,6 @@ func isSerialToolCall(toolMap map[string]fantasy.AgentTool, toolNameAliases map[
|
||||
return ok && serial.SerialToolCalls()
|
||||
}
|
||||
|
||||
// buildToolDefinitions converts AgentTool definitions into the
|
||||
// fantasy.Tool slice expected by fantasy.Call. When activeTools
|
||||
// is non-empty, only function tools whose name appears in the
|
||||
// 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))
|
||||
for _, tool := range tools {
|
||||
info := tool.Info()
|
||||
if !isToolActive(info.Name, activeTools) {
|
||||
continue
|
||||
}
|
||||
|
||||
// Substitute an empty object for nil properties so that a tool
|
||||
// with no parameters never serializes "properties" to null,
|
||||
// which OpenAI rejects.
|
||||
properties := info.Parameters
|
||||
if properties == nil {
|
||||
properties = map[string]any{}
|
||||
}
|
||||
inputSchema := map[string]any{
|
||||
"type": "object",
|
||||
"properties": properties,
|
||||
}
|
||||
// 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
|
||||
}
|
||||
schema.Normalize(inputSchema)
|
||||
prepared = append(prepared, fantasy.FunctionTool{
|
||||
Name: info.Name,
|
||||
Description: info.Description,
|
||||
InputSchema: inputSchema,
|
||||
ProviderOptions: tool.ProviderOptions(),
|
||||
})
|
||||
}
|
||||
for _, pt := range providerTools {
|
||||
prepared = append(prepared, pt.Definition)
|
||||
}
|
||||
return prepared
|
||||
}
|
||||
|
||||
func shouldApplyAnthropicPromptCaching(model fantasy.LanguageModel) bool {
|
||||
if model == nil {
|
||||
return false
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package chatloop
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"iter"
|
||||
"testing"
|
||||
|
||||
@@ -80,49 +79,3 @@ func TestProcessStepStreamPersistsRedactedThinkingOnEnd(t *testing.T) {
|
||||
require.NotNil(t, metadata)
|
||||
require.Equal(t, "redacted-payload", metadata.RedactedData)
|
||||
}
|
||||
|
||||
// staticParametersTool returns a fixed ToolInfo, letting a test control
|
||||
// Parameters directly. fantasy.NewAgentTool always generates a non-nil
|
||||
// Parameters map, so it cannot reproduce the nil Parameters that MCP tool
|
||||
// wrappers report for a schema with an empty "properties" object.
|
||||
type staticParametersTool struct {
|
||||
fantasy.AgentTool
|
||||
info fantasy.ToolInfo
|
||||
}
|
||||
|
||||
func (t staticParametersTool) Info() fantasy.ToolInfo { return t.info }
|
||||
|
||||
func (staticParametersTool) ProviderOptions() fantasy.ProviderOptions { return nil }
|
||||
|
||||
// TestBuildToolDefinitionsNilPropertiesBecomesEmptyObject verifies that a
|
||||
// tool whose input schema has no properties (for example an MCP tool
|
||||
// reporting {"type": "object", "properties": {}}) still serializes
|
||||
// "properties" as an empty JSON object. A nil Parameters map would serialize
|
||||
// to null, which OpenAI rejects with "Invalid schema for function ... None is
|
||||
// not of type 'object'".
|
||||
func TestBuildToolDefinitionsNilPropertiesBecomesEmptyObject(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tool := staticParametersTool{
|
||||
info: fantasy.ToolInfo{
|
||||
Name: "document_graphql_schema",
|
||||
Description: "Run a GraphQL query",
|
||||
Parameters: nil,
|
||||
},
|
||||
}
|
||||
|
||||
defs := buildToolDefinitions([]fantasy.AgentTool{tool}, nil, nil)
|
||||
require.Len(t, defs, 1)
|
||||
|
||||
ft, ok := defs[0].(fantasy.FunctionTool)
|
||||
require.True(t, ok, "expected a fantasy.FunctionTool")
|
||||
|
||||
properties, ok := ft.InputSchema["properties"].(map[string]any)
|
||||
require.True(t, ok, "properties must be a map, got %T", ft.InputSchema["properties"])
|
||||
require.NotNil(t, properties, "properties must not be nil")
|
||||
|
||||
// Verify it serializes to {} not null.
|
||||
bs, err := json.Marshal(ft.InputSchema)
|
||||
require.NoError(t, err)
|
||||
require.JSONEq(t, `{"type":"object","properties":{}}`, string(bs))
|
||||
}
|
||||
@@ -96,6 +96,9 @@ type CompactionOptions struct {
|
||||
ResolvedModel string
|
||||
ModelConfigID uuid.UUID
|
||||
SummaryCall fantasy.Call
|
||||
// ToolDefinitions is copied from the parent generation request so the
|
||||
// summary call uses the exact same ordered definitions.
|
||||
ToolDefinitions []fantasy.Tool
|
||||
|
||||
// Force skips the threshold gate (including the threshold=100
|
||||
// disable and the zero-usage early return). Set for manual,
|
||||
@@ -233,6 +236,7 @@ func normalizedCompactionGenerateConfig(opts GenerateCompactionOptions) (Compact
|
||||
ResolvedModel: opts.ResolvedModel,
|
||||
ModelConfigID: opts.ModelConfigID,
|
||||
SummaryCall: opts.SummaryCall,
|
||||
ToolDefinitions: opts.ToolDefinitions,
|
||||
Force: opts.Force,
|
||||
Source: opts.Source,
|
||||
ToolCallID: opts.ToolCallID,
|
||||
@@ -447,6 +451,7 @@ func generateCompactionSummary(
|
||||
|
||||
call := options.SummaryCall
|
||||
call.Prompt = summaryPrompt
|
||||
call.Tools = options.ToolDefinitions
|
||||
response, err := model.Generate(summaryCtx, call)
|
||||
if err != nil {
|
||||
return "", xerrors.Errorf("generate summary text: %w", err)
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"time"
|
||||
|
||||
"charm.land/fantasy"
|
||||
fantasyopenai "charm.land/fantasy/providers/openai"
|
||||
"github.com/google/uuid"
|
||||
"github.com/sqlc-dev/pqtype"
|
||||
"github.com/stretchr/testify/require"
|
||||
@@ -215,6 +216,110 @@ func TestGenerateCompactionSummary_PanicFinalizesAsError(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateCompactionSummaryPreservesCallOptions(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
temperature := 0.2
|
||||
topP := 0.8
|
||||
topK := int64(40)
|
||||
presencePenalty := 0.1
|
||||
frequencyPenalty := 0.3
|
||||
reasoningEffort := fantasyopenai.ReasoningEffortMedium
|
||||
providerOptions := fantasy.ProviderOptions{
|
||||
fantasyopenai.Name: &fantasyopenai.ResponsesProviderOptions{
|
||||
ReasoningEffort: &reasoningEffort,
|
||||
},
|
||||
}
|
||||
toolDefinitions := []fantasy.Tool{
|
||||
fantasy.FunctionTool{Name: "read_file", InputSchema: map[string]any{"type": "object"}},
|
||||
fantasy.ProviderDefinedTool{ID: "web_search", Name: "web_search"},
|
||||
}
|
||||
messages := []fantasy.Message{
|
||||
textMessage(fantasy.MessageRoleSystem, "system prefix"),
|
||||
textMessage(fantasy.MessageRoleUser, "hello"),
|
||||
}
|
||||
originalMessages := append([]fantasy.Message(nil), messages...)
|
||||
var got fantasy.Call
|
||||
model := &chattest.FakeModel{
|
||||
ProviderName: "fake",
|
||||
ModelName: "fake-model",
|
||||
GenerateFn: func(_ context.Context, call fantasy.Call) (*fantasy.Response, error) {
|
||||
got = call
|
||||
return &fantasy.Response{Content: []fantasy.Content{fantasy.TextContent{Text: "summary"}}}, nil
|
||||
},
|
||||
}
|
||||
|
||||
toolChoice := fantasy.ToolChoiceNone
|
||||
summary, err := generateCompactionSummary(context.Background(), model, messages, CompactionOptions{
|
||||
SummaryPrompt: "summarize",
|
||||
SummaryCall: fantasy.Call{
|
||||
Temperature: &temperature,
|
||||
TopP: &topP,
|
||||
TopK: &topK,
|
||||
PresencePenalty: &presencePenalty,
|
||||
FrequencyPenalty: &frequencyPenalty,
|
||||
ProviderOptions: providerOptions,
|
||||
ToolChoice: &toolChoice,
|
||||
},
|
||||
ToolDefinitions: toolDefinitions,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "summary", summary)
|
||||
require.Equal(t, &temperature, got.Temperature)
|
||||
require.Equal(t, &topP, got.TopP)
|
||||
require.Equal(t, &topK, got.TopK)
|
||||
require.Equal(t, &presencePenalty, got.PresencePenalty)
|
||||
require.Equal(t, &frequencyPenalty, got.FrequencyPenalty)
|
||||
require.Equal(t, providerOptions, got.ProviderOptions)
|
||||
require.Equal(t, toolDefinitions, got.Tools)
|
||||
require.NotNil(t, got.ToolChoice)
|
||||
require.Equal(t, fantasy.ToolChoiceNone, *got.ToolChoice)
|
||||
require.Nil(t, got.MaxOutputTokens)
|
||||
require.Len(t, got.Prompt, 3)
|
||||
require.Equal(t, originalMessages, []fantasy.Message(got.Prompt[:2]))
|
||||
require.Equal(t, fantasy.MessageRoleUser, got.Prompt[2].Role)
|
||||
require.Equal(t, []fantasy.MessagePart{fantasy.TextPart{Text: "summarize"}}, got.Prompt[2].Content)
|
||||
require.Equal(t, originalMessages, messages)
|
||||
}
|
||||
|
||||
func TestGenerateCompactionSummaryUsesToolDefinitions(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
toolDefinitions := []fantasy.Tool{
|
||||
fantasy.FunctionTool{
|
||||
Name: "read_file",
|
||||
Description: "Read a file.",
|
||||
InputSchema: map[string]any{"type": "object"},
|
||||
},
|
||||
fantasy.ProviderDefinedTool{ID: "web_search", Name: "web_search"},
|
||||
}
|
||||
messages := []fantasy.Message{textMessage(fantasy.MessageRoleUser, "hello")}
|
||||
originalMessages := append([]fantasy.Message(nil), messages...)
|
||||
var got fantasy.Call
|
||||
model := &chattest.FakeModel{
|
||||
ProviderName: "fake",
|
||||
ModelName: "fake-model",
|
||||
GenerateFn: func(_ context.Context, call fantasy.Call) (*fantasy.Response, error) {
|
||||
got = call
|
||||
return &fantasy.Response{Content: []fantasy.Content{fantasy.TextContent{Text: "summary"}}}, nil
|
||||
},
|
||||
}
|
||||
|
||||
toolChoice := fantasy.ToolChoiceNone
|
||||
summary, err := generateCompactionSummary(context.Background(), model, messages, CompactionOptions{
|
||||
SummaryPrompt: "summarize",
|
||||
SummaryCall: fantasy.Call{ToolChoice: &toolChoice},
|
||||
ToolDefinitions: toolDefinitions,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "summary", summary)
|
||||
require.Equal(t, toolDefinitions, got.Tools)
|
||||
require.NotNil(t, got.ToolChoice)
|
||||
require.Equal(t, toolChoice, *got.ToolChoice)
|
||||
require.Len(t, got.Prompt, 2)
|
||||
require.Equal(t, originalMessages, messages)
|
||||
}
|
||||
|
||||
func TestGenerateCompactionSummary_UsesCallerContext(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
package chatloop
|
||||
|
||||
import (
|
||||
"charm.land/fantasy"
|
||||
"charm.land/fantasy/schema"
|
||||
)
|
||||
|
||||
// BuildToolDefinitions converts AgentTool definitions into the
|
||||
// fantasy.Tool slice expected by fantasy.Call. When activeTools
|
||||
// is non-empty, only function tools whose name appears in the
|
||||
// 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))
|
||||
for _, tool := range tools {
|
||||
info := tool.Info()
|
||||
if !isToolActive(info.Name, activeTools) {
|
||||
continue
|
||||
}
|
||||
|
||||
// Substitute an empty object for nil properties so that a tool
|
||||
// with no parameters never serializes "properties" to null,
|
||||
// which OpenAI rejects.
|
||||
properties := info.Parameters
|
||||
if properties == nil {
|
||||
properties = map[string]any{}
|
||||
}
|
||||
inputSchema := map[string]any{
|
||||
"type": "object",
|
||||
"properties": properties,
|
||||
}
|
||||
// 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
|
||||
}
|
||||
schema.Normalize(inputSchema)
|
||||
prepared = append(prepared, fantasy.FunctionTool{
|
||||
Name: info.Name,
|
||||
Description: info.Description,
|
||||
InputSchema: inputSchema,
|
||||
ProviderOptions: tool.ProviderOptions(),
|
||||
})
|
||||
}
|
||||
for _, pt := range providerTools {
|
||||
prepared = append(prepared, pt.Definition)
|
||||
}
|
||||
return prepared
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package chatloop
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"charm.land/fantasy"
|
||||
fantasyopenai "charm.land/fantasy/providers/openai"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// staticParametersTool returns a fixed ToolInfo, letting a test control
|
||||
// Parameters directly. fantasy.NewAgentTool always generates a non-nil
|
||||
// Parameters map, so it cannot reproduce the nil Parameters that MCP tool
|
||||
// wrappers report for a schema with an empty "properties" object.
|
||||
type staticParametersTool struct {
|
||||
fantasy.AgentTool
|
||||
info fantasy.ToolInfo
|
||||
providerOptions fantasy.ProviderOptions
|
||||
}
|
||||
|
||||
func (t staticParametersTool) Info() fantasy.ToolInfo { return t.info }
|
||||
|
||||
func (t staticParametersTool) ProviderOptions() fantasy.ProviderOptions { return t.providerOptions }
|
||||
|
||||
// TestBuildToolDefinitionsNilPropertiesBecomesEmptyObject verifies that a
|
||||
// tool whose input schema has no properties (for example an MCP tool
|
||||
// reporting {"type": "object", "properties": {}}) still serializes
|
||||
// "properties" as an empty JSON object. A nil Parameters map would serialize
|
||||
// to null, which OpenAI rejects with "Invalid schema for function ... None is
|
||||
// not of type 'object'".
|
||||
func TestBuildToolDefinitionsNilPropertiesBecomesEmptyObject(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tool := staticParametersTool{
|
||||
info: fantasy.ToolInfo{
|
||||
Name: "document_graphql_schema",
|
||||
Description: "Run a GraphQL query",
|
||||
Parameters: nil,
|
||||
},
|
||||
}
|
||||
|
||||
defs := BuildToolDefinitions([]fantasy.AgentTool{tool}, nil, nil)
|
||||
require.Len(t, defs, 1)
|
||||
|
||||
ft, ok := defs[0].(fantasy.FunctionTool)
|
||||
require.True(t, ok, "expected a fantasy.FunctionTool")
|
||||
|
||||
properties, ok := ft.InputSchema["properties"].(map[string]any)
|
||||
require.True(t, ok, "properties must be a map, got %T", ft.InputSchema["properties"])
|
||||
require.NotNil(t, properties, "properties must not be nil")
|
||||
|
||||
// Verify it serializes to {} not null.
|
||||
bs, err := json.Marshal(ft.InputSchema)
|
||||
require.NoError(t, err)
|
||||
require.JSONEq(t, `{"type":"object","properties":{}}`, string(bs))
|
||||
}
|
||||
|
||||
func TestBuildToolDefinitionsFiltersInactiveToolsAndAppendsProviderTools(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
active := staticParametersTool{info: fantasy.ToolInfo{Name: "active"}}
|
||||
inactive := staticParametersTool{info: fantasy.ToolInfo{Name: "inactive"}}
|
||||
provider := fantasy.ProviderDefinedTool{ID: "web_search", Name: "web_search"}
|
||||
|
||||
defs := BuildToolDefinitions(
|
||||
[]fantasy.AgentTool{active, inactive},
|
||||
[]string{"active"},
|
||||
[]ProviderTool{{Definition: provider}},
|
||||
)
|
||||
|
||||
require.Len(t, defs, 2)
|
||||
function, ok := defs[0].(fantasy.FunctionTool)
|
||||
require.True(t, ok)
|
||||
require.Equal(t, "active", function.Name)
|
||||
require.Equal(t, provider, defs[1])
|
||||
}
|
||||
|
||||
func TestBuildToolDefinitionsPreservesFunctionToolOptions(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
providerOptions := fantasy.ProviderOptions{
|
||||
fantasyopenai.Name: &fantasyopenai.ProviderOptions{},
|
||||
}
|
||||
tool := staticParametersTool{
|
||||
info: fantasy.ToolInfo{Name: "test_tool"},
|
||||
providerOptions: providerOptions,
|
||||
}
|
||||
|
||||
defs := BuildToolDefinitions([]fantasy.AgentTool{tool}, nil, nil)
|
||||
require.Len(t, defs, 1)
|
||||
function, ok := defs[0].(fantasy.FunctionTool)
|
||||
require.True(t, ok)
|
||||
require.Equal(t, "test_tool", function.Name)
|
||||
require.Equal(t, providerOptions, function.ProviderOptions)
|
||||
}
|
||||
@@ -701,6 +701,7 @@ func (server *Server) prepareGeneration(
|
||||
builtinToolNames[chattool.FindToolsName] = true
|
||||
}
|
||||
|
||||
toolDefinitions := chatloop.BuildToolDefinitions(tools, activeToolNames, providerTools)
|
||||
toolNameToConfigID := make(map[string]uuid.UUID)
|
||||
for _, t := range tools {
|
||||
if mcpTool, ok := t.(mcpclient.MCPToolIdentifier); ok {
|
||||
@@ -748,6 +749,7 @@ func (server *Server) prepareGeneration(
|
||||
ModelConfigID: modelConfig.ID,
|
||||
StepUsage: compactionStepUsage,
|
||||
SummaryCall: compactionSummaryCall(resolved),
|
||||
ToolDefinitions: toolDefinitions,
|
||||
}
|
||||
|
||||
// workspaceCtx.currentChatSnapshot may carry a freshly persisted
|
||||
|
||||
@@ -16,6 +16,7 @@ import (
|
||||
"github.com/coder/coder/v2/coderd/database"
|
||||
"github.com/coder/coder/v2/coderd/database/dbgen"
|
||||
"github.com/coder/coder/v2/coderd/database/dbtestutil"
|
||||
"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/chatprovider"
|
||||
"github.com/coder/coder/v2/coderd/x/chatd/chatstate"
|
||||
@@ -165,6 +166,11 @@ func TestPrepareGenerationClampsRequestedReasoningEffortToMax(t *testing.T) {
|
||||
require.True(t, ok, "%T", prepared.CallTemplate.ProviderOptions[fantasyopenai.Name])
|
||||
require.NotNil(t, providerOptions.ReasoningEffort)
|
||||
require.Equal(t, fantasyopenai.ReasoningEffortMedium, *providerOptions.ReasoningEffort)
|
||||
require.NotNil(t, prepared.Compaction.Options.ToolDefinitions)
|
||||
require.Equal(t,
|
||||
chatloop.BuildToolDefinitions(prepared.Tools, prepared.ActiveTools, prepared.ProviderTools),
|
||||
prepared.Compaction.Options.ToolDefinitions,
|
||||
)
|
||||
|
||||
require.NotNil(t, providerOptions.User)
|
||||
require.Equal(t, "turn-options-sentinel", *providerOptions.User)
|
||||
|
||||
Reference in new issue
Block a user