Renames the turn preparation step after what it returns: the environment for one turn. Only names change, plus one small change to how callers release the environment. - `generation_preparer.go` becomes `turn_environment.go`, and its test file becomes `turn_environment_internal_test.go`. GitHub shows both as renames. A comment in `compactionTriggers.test.ts` that names the test file follows the rename. - `prepareGeneration` becomes `buildTurnEnvironment`, and its result type `generationPrepared` becomes `turnEnvironment`. Four `TestPrepareGeneration*` tests become `TestBuildTurnEnvironment*`. - The `Cleanup` field becomes the unexported `cleanup`, and callers use `Close()`. `Close()` does nothing on a zero value. The type stays in `generation.go` for now. #30870 moves it next to `buildTurnEnvironment`. Validation: build and vet pass, the renamed tests and the `toolinput` tests pass, and the pre-commit hook passed. ## Stack This stack splits #28757, as requested in review. It moves the code that prepares a chat turn into one file, `turn_environment.go`, and moves the tool policy tests from full turns to a table test. chatd behaves as on `main`, except for the debug log in #30865. 1. #30864 Remove the no-op `allowedBehaviorToolNames` filter 2. #30865 Log workspace agent lookup errors in turn preparation 3. #30866 Rename `prepareGeneration` to `buildTurnEnvironment` 4. #30867 Cover the turn tool policy at `buildTurnEnvironment` 5. #30868 Remove seven end-to-end tool policy tests 6. #30869 Name the `*Server` receiver `p` in `turn_environment.go` 7. #30870 Move the turn preparation helpers into `turn_environment.go` > Xum, an AI agent, opened this PR for @ibetitsmike and wrote this description.
194 lines
7.5 KiB
Go
194 lines
7.5 KiB
Go
package chatd //nolint:testpackage // Exercises unexported generation helpers.
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"charm.land/fantasy"
|
|
"github.com/stretchr/testify/require"
|
|
"go.opentelemetry.io/otel/attribute"
|
|
"golang.org/x/xerrors"
|
|
|
|
"github.com/coder/coder/v2/coderd/x/chatd/chatdebug"
|
|
"github.com/coder/coder/v2/coderd/x/chatd/chaterror"
|
|
"github.com/coder/coder/v2/coderd/x/chatd/chatloop"
|
|
"github.com/coder/coder/v2/coderd/x/chatd/chatstate"
|
|
"github.com/coder/coder/v2/coderd/x/chatd/chattest"
|
|
"github.com/coder/coder/v2/testutil"
|
|
)
|
|
|
|
func TestExclusiveBatchRejected(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
call := func(name string) fantasy.ToolCallContent {
|
|
return fantasy.ToolCallContent{ToolCallID: "call_" + name, ToolName: name}
|
|
}
|
|
exclusive := map[string]bool{"advisor": true}
|
|
|
|
cases := []struct {
|
|
name string
|
|
toolCalls []fantasy.ToolCallContent
|
|
exclusives map[string]bool
|
|
want bool
|
|
}{
|
|
{name: "ExclusiveAlone", toolCalls: []fantasy.ToolCallContent{call("advisor")}, exclusives: exclusive},
|
|
{name: "NoExclusive", toolCalls: []fantasy.ToolCallContent{call("execute"), call("read_file")}, exclusives: exclusive},
|
|
{name: "NoExclusiveNames", toolCalls: []fantasy.ToolCallContent{call("advisor"), call("execute")}},
|
|
{
|
|
name: "ExclusiveMixed",
|
|
toolCalls: []fantasy.ToolCallContent{call("advisor"), call("execute")},
|
|
exclusives: exclusive,
|
|
want: true,
|
|
},
|
|
}
|
|
|
|
for _, test := range cases {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
require.Equal(t, test.want, exclusiveBatchRejected(test.toolCalls, test.exclusives))
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestCompactionMetricIdentity(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
compaction := &generationCompaction{
|
|
Options: chatloop.GenerateCompactionOptions{
|
|
Model: &chattest.FakeModel{ProviderName: "anthropic", ModelName: "claude-sonnet-4-5"},
|
|
},
|
|
}
|
|
|
|
provider, model := compactionMetricIdentity(compaction)
|
|
require.Equal(t, "anthropic", provider)
|
|
require.Equal(t, "claude-sonnet-4-5", model)
|
|
|
|
// With an override, metrics use the prepare-time identity, not the
|
|
// chat model carried by the options.
|
|
compaction.Override = &resolvedModelOverride{
|
|
ResolvedProvider: "openai",
|
|
ResolvedModel: "gpt-4.1-mini",
|
|
}
|
|
provider, model = compactionMetricIdentity(compaction)
|
|
require.Equal(t, "openai", provider)
|
|
require.Equal(t, "gpt-4.1-mini", model)
|
|
}
|
|
|
|
func TestCompactionStillOverLimitError(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
err := compactionStillOverLimitError(compactionTriggerSourceOrganization, true, 70, 100)
|
|
require.ErrorIs(t, err, errCompactionStillOverLimit)
|
|
require.Equal(t, "organization trigger at 70% of 100 tokens: compaction left the chat above the compaction limit", err.Error())
|
|
|
|
const (
|
|
chatMessage = "Conversation compaction could not reduce the history below your compaction threshold. Raise the compaction threshold in settings, or start a new conversation."
|
|
overrideMessage = "Conversation compaction could not reduce the history below 80% of the organization override's context window. Start a new conversation, or ask an administrator to choose an override model with a larger context window."
|
|
bothMessage = "Conversation compaction could not reduce the history below your compaction threshold or 80% of the organization override's context window. Start a new conversation, or raise your compaction threshold in settings and ask an administrator to choose an override model with a larger context window."
|
|
)
|
|
require.Equal(t, chatMessage, chaterror.Classify(compactionStillOverLimitError(compactionTriggerSourceChat, false, 70, 100)).Message)
|
|
require.Equal(t, overrideMessage, chaterror.Classify(compactionStillOverLimitError(compactionTriggerSourceOrganization, false, 70, 100)).Message)
|
|
// Neither setting alone clears history that reaches both points.
|
|
require.Equal(t, bothMessage, chaterror.Classify(compactionStillOverLimitError(compactionTriggerSourceChat, true, 70, 100)).Message)
|
|
require.Equal(t, bothMessage, chaterror.Classify(compactionStillOverLimitError(compactionTriggerSourceOrganization, true, 70, 100)).Message)
|
|
}
|
|
|
|
func TestGenerationCompactionContextLimit(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
require.EqualValues(t, 0, generationCompactionContextLimit(nil))
|
|
|
|
// The decision path must see the prepare-time compaction limit (the
|
|
// binding trigger's context limit), not the chat model's limit.
|
|
compaction := &generationCompaction{
|
|
Options: chatloop.GenerateCompactionOptions{ContextLimit: 50_000},
|
|
}
|
|
require.EqualValues(t, 50_000, generationCompactionContextLimit(compaction))
|
|
}
|
|
|
|
func TestRecordGenerationFinishFailure(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
err error
|
|
wantRecorded bool
|
|
}{
|
|
{
|
|
name: "TerminalFailureRecordsError",
|
|
err: normalizeTaskTransitionError(chatstate.ErrTransitionNotAllowed, "finish generation error"),
|
|
wantRecorded: true,
|
|
},
|
|
{
|
|
name: "ExpectedExitSkips",
|
|
err: normalizeTaskTransitionError(errTaskExpectedExit, "finish generation error"),
|
|
wantRecorded: false,
|
|
},
|
|
{
|
|
name: "RetryableSkips",
|
|
err: normalizeTaskTransitionError(xerrors.New("transient infrastructure failure"), "finish generation error"),
|
|
wantRecorded: false,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
turn := newRunnerDebugTurn(testutil.Context(t, testutil.WaitShort), testutil.Logger(t))
|
|
recordGenerationFinishFailure(turn, tt.err)
|
|
require.Equal(t, tt.wantRecorded, turn.statusSet)
|
|
if tt.wantRecorded {
|
|
require.Equal(t, chatdebug.StatusError, turn.status)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestRecordThinkingStages(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
// For bedrock, provider is the wire protocol, not the provider type.
|
|
prepared := turnEnvironment{
|
|
StageModel: chatloop.StageModel{Provider: "anthropic", ProviderType: "bedrock", Model: "claude", Effort: "high"},
|
|
}
|
|
|
|
t.Run("PairsByIndex", func(t *testing.T) {
|
|
t.Parallel()
|
|
tracer, recorder := newStageTestTracer(t)
|
|
starter := &taskStarter{server: &Server{stages: tracer}}
|
|
base := time.Now().Add(-time.Minute)
|
|
|
|
starter.recordThinkingStages(t.Context(), prepared, chatloop.PersistedStep{
|
|
ReasoningStartedAt: []time.Time{base, base.Add(10 * time.Second)},
|
|
ReasoningCompletedAt: []time.Time{base.Add(2 * time.Second), base.Add(15 * time.Second)},
|
|
})
|
|
|
|
ended := recorder.Ended()
|
|
require.Len(t, ended, 2)
|
|
require.Equal(t, base.UTC(), ended[0].StartTime().UTC())
|
|
require.Equal(t, base.Add(2*time.Second).UTC(), ended[0].EndTime().UTC())
|
|
require.Equal(t, base.Add(10*time.Second).UTC(), ended[1].StartTime().UTC())
|
|
require.Equal(t, base.Add(15*time.Second).UTC(), ended[1].EndTime().UTC())
|
|
for _, span := range ended {
|
|
require.Equal(t, string(chatloop.StageThinking), span.Name())
|
|
require.Contains(t, span.Attributes(), attribute.String(chatloop.AttrProvider, "anthropic"))
|
|
require.Contains(t, span.Attributes(), attribute.String(chatloop.AttrProviderType, "bedrock"))
|
|
require.Contains(t, span.Attributes(), attribute.String(chatloop.AttrModel, "claude"))
|
|
require.Contains(t, span.Attributes(), attribute.String(chatloop.AttrReasoningEffort, "high"))
|
|
}
|
|
})
|
|
|
|
t.Run("StopsAtFirstUnpairedStart", func(t *testing.T) {
|
|
t.Parallel()
|
|
tracer, recorder := newStageTestTracer(t)
|
|
starter := &taskStarter{server: &Server{stages: tracer}}
|
|
base := time.Now().Add(-time.Minute)
|
|
|
|
starter.recordThinkingStages(t.Context(), prepared, chatloop.PersistedStep{
|
|
ReasoningStartedAt: []time.Time{base, base.Add(10 * time.Second), base.Add(20 * time.Second)},
|
|
ReasoningCompletedAt: []time.Time{base.Add(2 * time.Second)},
|
|
})
|
|
|
|
require.Len(t, recorder.Ended(), 1)
|
|
})
|
|
}
|