Files
coder/coderd/x/chatd/generation_internal_test.go
T
Michael Suchacz 39c4bd6ced refactor(coderd/x/chatd): drop the unused runnerCtx from the runner debug turn (#31010)
Deletes `runnerDebugTurn.runnerCtx` and the context parameter of
`newRunnerDebugTurn`. The field's only reader was the
`LaunchRunHeartbeat` call in `ensureLocked`, which #30988 deleted with
the run heartbeats, so nothing has read it since.

**Why it is safe:** the constructor set the field and no code read it.
The debug turn still uses the context of each call to `Ensure`,
`Context` and `Finalize`, which this PR does not change. `runner.go` and
10 tests stop passing a context, and four of those tests drop the
`context.WithCancel` that they created only for this parameter.

Validation:
- `go build ./coderd/x/chatd/...` and `go vet ./coderd/x/chatd/...`
pass.
- The 25 chatd debug-run tests, `TestRecordGenerationFinishFailure`,
`TestSessionStartDispatchFailureFinishesGeneration` and
`TestStartGeneration_CancelsDeletedToolCallsOnAgent` pass under `-race`.
- `golangci-lint run --fix=false ./coderd/x/chatd/` reports 0 issues,
and the pre-commit hook passed.

## Stack

This stack deletes the stale debug-run sweep, then simplifies the
chatdebug debug-run, stream wrapper and step numbering code, drops the
indexes the sweep used, and last deletes a runner debug field that lost
its only reader with the heartbeats. The debug-run and stream wrapper
refactors each sit on a test-only layer that pins the behavior they
keep.

1. #30988 Delete the chat debug run and step heartbeats
2. #30989 Delete `FinalizeStale` and the stale threshold
3. #30995 Delete the `FinalizeStaleChatDebugRows` query
4. #30933 Pin the debug runs that turns, titles and status labels record
5. #30934 Create and finalize debug runs through one helper
6. #30980 Table the stream wrapper tests and cover their status rules
7. #30981 Share one finalize-once lifecycle between the stream wrappers
8. #31007 Number chat debug steps in the database
9. #31008 Delete the process-global step counters
10. #31009 Drop the chat debug stale-sweep indexes
11. This PR: Drop the unused `runnerCtx` from the runner debug turn

> Xum, an AI agent, opened this PR for @ibetitsmike and wrote this
description.
2026-10-22 07:55:42 +00:00

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.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)
})
}