Files
coder/coderd/x/chatd/worker_internal_test.go
T
Michael Suchacz ac824cd235 chore(coderd): remove dead code from chatd (#29008)
Removes production code in `coderd/x/chatd` that nothing reaches, the
tests that only existed to exercise it, and option/config fields that
callers filled in but chatd never read. No runtime behaviour changes;
the only edits outside `coderd/x/chatd` drop the now-nonexistent fields
at their three call sites (`coderd/coderd.go`, `coderd/exp_chats.go`,
`coderd/coderdtest`).

Net: 42 files, +147 / -1065.

Verification method for every item: `golang.org/x/tools/cmd/deadcode`
(with and without `-test`) rooted at `cmd/coder`, `enterprise/cmd/coder`
and every package that imports chatd; a `go/packages` pass counting
`TypesInfo.Uses` per declared type, const, var, field and method across
the same roots, split into test and non-test use sites, plus a
read-vs-write split for struct fields; `rg` over the whole repo
including `site/src` for json tag names; then `go build ./...`, `go vet
./coderd/x/chatd/...`, `golangci-lint run ./coderd/x/chatd/... ./coderd/
./coderd/coderdtest/`, and `go test ./coderd/x/chatd/...` (all 20
packages) locally.

<details>
<summary>Evidence per deletion</summary>

| Removed | What it was | Why unused |
|---|---|---|
| `subagent.go` `resolveConfiguredModelOverride`, `parseModelOverride`,
`parsedModelOverride` (~135 lines) +
`TestResolveConfiguredModelOverride_*`, `TestParseModelOverride` |
raw-string model override resolver | Only callers were its two tests;
production resolves overrides through `resolveOrganizationModelOverride`
(`title_override.go`, `compaction_override.go`, `subagent.go`). |
| `chatd.go` `removeSkillIndexMessages`, `isSkillIndexMessage` +
`TestSkillIndexRefreshReplacesStaleAliases` | skill-index refresh
helpers | Zero production references; the test simulated a refresh path
no production code performs. |
| `chatd.go` `chatMessage`, `newChatMessage`, `withCreatedBy`,
`appendMessageFields`, `BuildSingleChatMessageInsertParams` (~90 lines)
| insert-params builder | Exported, but every caller was a `chatd_test`
file (6 sites). Replaced by a 25-line `singleChatMessageInsertParams`
helper in `chatd_helpers_test.go`. |
| `chatd.go` `Server.closeInflightAdmission` | one-line wrapper around
`inflightClosed.Store(true)` | Single call site (`Close`); inlined. |
| `chatd.go` `Server.workerID`, `instructionLookupTimeout`,
`pendingChatAcquireInterval`, `maxChatsPerAcquire`,
`chatHeartbeatInterval` | Server struct fields | Assigned in `New` and
never read anywhere (the values reach the worker through
`chatWorkerOptions`, not these fields). |
| `Config.InstructionLookupTimeout`, `homeInstructionLookupTimeout`,
`coderd.Options.ChatdInstructionLookupTimeout`,
`coderdtest.Options.ChatdInstructionLookupTimeout` | timeout plumbing |
The value only landed in the dead `Server.instructionLookupTimeout`; no
CLI flag sets it and nothing reads it. |
| `PromoteQueuedOptions.CreatedBy` | option field | Set by
`coderd/exp_chats.go` and three tests, never read in chatd (promotion
keeps the queued row's `created_by`). |
| `SubmitToolResultsOptions.DynamicTools` | option field | Set by
`coderd/exp_chats.go` (which re-read `chat.DynamicTools` just to pass
it) and three tests, never read in chatd. |
| `attachments.go` `buildAssistantPartsForPersist` (whole file) |
assistant part builder | Stale copy left by #26270; production uses
`buildAssistantParts` in `message_conversion.go`. Its six tests were
retargeted to the live function (now
`message_conversion_internal_test.go`) and a dead-copy assertion in
`subagent_internal_test.go` was dropped. |
| `chatloop.RunOptions` (94 lines) | old loop options struct | Only two
of 23 fields were ever populated, for one internal call to
`prepareMessagesForRequest`, which now takes `model, logger` directly
and no longer returns the ignored canonical slice.
`GenerateAssistantOptions` / `ExecuteLocalToolsOptions` are the live
types. |
| `chatloop.ErrDynamicToolCall`, `ErrStopAfterTool`, `PendingToolCall`,
`PersistedStep.PendingDynamicToolCalls` | dynamic-tool exit protocol |
Zero references outside their declarations (one stale comment in
`chatdebug/service.go` updated). |
| `chatloop.CompactionOptions.Persist`, `.OnError` | callbacks |
`Persist` had zero references; `OnError` was set only by two tests that
asserted it was never invoked. |
| `chatloop` `stepResult.shouldContinue` + its computation | flag |
Written once, never read. |
| `chatloop.StateWaiting` | metric label const | Zero references. |
| `generation.go` `generationOutcome.InsertedMessages`,
`runnerActionMessage` | outcome field | Built on every commit, never
read. |
| `chatstate` `ExecutionState.IsArchived`, `AllOwnershipStates`,
`OwnershipState`/`StateU`/`StateO`, `TransitionAcquire` | state-machine
leftovers | Zero references (the ownership consts became dead once
`AllOwnershipStates` went). |
| `chatstate` `ExecutionState.QueueNonEmpty` | predicate | Only
referenced by the test asserting its own table; the sibling `IsRunnable`
check in that test is kept. |
| `chatdebug.Attempt.RetryClassification`, `.RetryDelayMs` | json fields
| Never assigned anywhere, `omitempty`, and
`retry_classification`/`retry_delay_ms` do not appear in `site/src` or
`typesGenerated.ts`. |
| `chatdebug.RunContext.Kind`, `StepContext.HistoryTipMessageID`,
`RecorderOptions.Provider`/`.Model`, `stepHandle.opts`/`.usage`/`.err` |
context/handle fields | Written (from `chatd.go`,
`chatloop/compaction.go`, `modelcall.go`, `recorder.go`) and never read.
The DB run row keeps its own `kind` column. |
| `chatprovider.MergeProviderAPIKeys` +
`TestMergeProviderAPIKeys_PreservesProviderRegions` | key merge |
Superseded by `ResolveUserProviderKeys`, whose region test already
covers the same behaviour. |
| `chatprovider` `providerCreationError` | one-line `xerrors.Errorf`
wrapper | Single call site; inlined with identical message. |
| `chatretry.IsRetryable` | classifier wrapper | Zero references. |
| `computeruse.IsTool` + `TestComputerUseTool` | one-line forwarder to
`fantasyopenai.IsComputerUseTool` | No production caller; two test
assertions now call the fantasy function directly, and the dropped test
only asserted the library's own constructor. |
| `chatprompt.ToolResultToPart` | one-line forwarder to
`codersdk.ChatMessageToolResult` | Single call site; inlined. |
| `subagent_catalog.go` `formatSubagentDefinitions` | forwarder with
`nil` overrides | Single call site; inlined. |
| `worker.go` `chatWorker.chatWorkerID` | getter | Only tests called it;
they read `opts.WorkerID` directly now. |
| `chattest.AnthropicErrorResponse`, `OpenAIServerErrorResponse`,
`ChatMessageWithParts` (`messages.go` removed) | test fixtures | Zero
references. |
| `chattest` `AnthropicRequest.Options`, `OpenAIRequest.Options` |
`json:",inline"` map fields | `encoding/json` has no inline tag (the
TODO on each said so); never populated, never read. |

</details>

<details>
<summary>Considered and kept</summary>

- `chatstate.CreateChat` (test-only thin wrapper over `insertChat`,
production uses `CreateChatWithID`): documented package API with 25 test
call sites; not worth the churn.
- `chatprompt.MarshalContent` / `MarshalToolResult`: test-only, but they
deliberately produce legacy-format DB rows so the read path's legacy
handling stays covered.
- `chatstate.AllowedExecutionTransitionsFrom` / `AllowedInputStates` /
`AllowedExecutionTransitionOutputs`, `chatdebug.StepFromContext`,
`WithClock`, `WithStaleThreshold`, `WaitUntilIdleForTest`,
`PublishBuffer.BufferedChannels`, `chattool.NewWorkspaceMCPTool`: test
seams that tests actively use to reach otherwise unexported state.
- `AgentCapacityLimiter` interface (one production implementation):
tests inject fakes.
- `applyHookResultMessages` / `appendHookResultMessages`: a named pair
over one placement enum, both used.
- Nil-receiver guards on `*Metrics`, `*Result`, `*streamSyncPoller`,
`*runnerDebugTurn`: nil is a supported "disabled" value on those paths.
- Tests flagged by heuristics as assertion-light
(`TestNopMetrics_DoesNotPanic`, `TestStepHandleFinish_NilHandle`,
`TestRetry_OnRetryNilDoesNotPanic`, the `chatstate`
synthetic-cancellation tests that assert through helpers): they cover
real nil-safety or delegate assertions to helpers; kept.

</details>

<details>
<summary>Out-of-scope findings (not changed here)</summary>

- `chatdebug.Service.FinalizeStale` has had no production caller since
#26270 removed the background sweep loop; only its tests call it, and
comments in `chatd_debug.go` and `database/querier.go` still describe
the loop. Deleting it would erase the implementation a fix needs, so it
is left for a decision.
- `coderd_chatd_chats` (gauge) and
`coderd_chatd_stream_buffer_dropped_total` (counter) are registered and
documented in `docs/admin/integrations/prometheus.md` but never set or
incremented in production; `Metrics.RecordStreamBufferDropped` is
test-only. Removing them changes the exported metric set and the docs,
so they are left alone.

</details>

## Declined review findings

None yet.

> Xum (AI agent) opened this PR on behalf of @ibetitsmike.
2026-09-05 11:39:10 +02:00

354 lines
12 KiB
Go

package chatd //nolint:testpackage // Tests unexported chat worker internals.
import (
"context"
"database/sql"
"errors"
"testing"
"time"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/testutil"
"github.com/coder/quartz"
)
func TestWorker_NewRequiresTaskStarterOrMessagePartBuffer(t *testing.T) {
t.Parallel()
f := newWorkerTestFixture(t)
_, err := newChatWorker(newUnstartedServer(t, f.pubsub, f.db), chatWorkerOptions{WorkerID: uuid.New(), Store: f.db, Pubsub: f.pubsub})
require.ErrorContains(t, err, "task starter or message part buffer is required")
}
func TestWorker_NewRequiresServer(t *testing.T) {
t.Parallel()
f := newWorkerTestFixture(t)
_, err := newChatWorker(nil, testOptions(t, f, newRecordingTaskStarter()))
require.ErrorContains(t, err, "server is required")
}
func TestWorker_NewRequiresWorkerID(t *testing.T) {
t.Parallel()
f := newWorkerTestFixture(t)
opts := testOptions(t, f, newRecordingTaskStarter())
opts.WorkerID = uuid.Nil
_, err := newChatWorker(newUnstartedServer(t, f.pubsub, f.db), opts)
require.ErrorContains(t, err, "worker ID is required")
}
func TestWorker_UsesConfiguredWorkerID(t *testing.T) {
t.Parallel()
f := newWorkerTestFixture(t)
starter := newRecordingTaskStarter()
opts := testOptions(t, f, starter)
workerID := opts.WorkerID
worker, err := newChatWorker(newUnstartedServer(t, f.pubsub, f.db), opts)
require.NoError(t, err)
require.Equal(t, workerID, worker.opts.WorkerID)
require.NoError(t, worker.Start(context.Background()))
require.Equal(t, workerID, worker.opts.WorkerID)
require.NoError(t, worker.Close())
}
func TestWorker_AcquiresRunnableChatFromOwnershipHint(t *testing.T) {
t.Parallel()
f := newWorkerTestFixture(t)
chat := f.createRunningChat(t)
starter := newRecordingTaskStarter()
worker := startWorker(t, testOptions(t, f, starter))
call := starter.waitCall(t, taskKindGeneration, chat.ID)
require.Equal(t, worker.opts.WorkerID, call.input.WorkerID)
require.Equal(t, database.ChatStatusRunning, call.input.Status)
require.NotEqual(t, uuid.Nil, call.input.RunnerID)
latest, err := f.db.GetChatByID(testutil.Context(t, testutil.WaitShort), chat.ID)
require.NoError(t, err)
require.Equal(t, worker.opts.WorkerID, latest.WorkerID.UUID)
require.Equal(t, call.input.RunnerID, latest.RunnerID.UUID)
_, err = f.db.GetChatHeartbeat(testutil.Context(t, testutil.WaitShort), database.GetChatHeartbeatParams{
ChatID: chat.ID,
RunnerID: call.input.RunnerID,
})
require.NoError(t, err)
}
func TestWorker_AcquiresRequiresActionChatFromOwnershipHint(t *testing.T) {
t.Parallel()
f := newWorkerTestFixture(t)
chat := f.createRequiresActionChat(t)
starter := newRecordingTaskStarter()
startWorker(t, testOptions(t, f, starter))
call := starter.waitCall(t, taskKindRequiresActionTimeout, chat.ID)
require.Equal(t, database.ChatStatusRequiresAction, call.input.Status)
require.True(t, call.input.RequiresActionDeadlineAt.Valid)
}
func TestWorker_SkipsFreshlyOwnedChat(t *testing.T) {
t.Parallel()
f := newWorkerTestFixture(t)
chat := f.createRunningChat(t)
otherWorker := uuid.New()
otherRunner := uuid.New()
acquireChat(t, f, chat.ID, otherWorker, otherRunner)
starter := newRecordingTaskStarter()
worker := startWorker(t, testOptions(t, f, starter))
worker.Wake()
starter.assertNoCall(t)
latest, err := f.db.GetChatByID(testutil.Context(t, testutil.WaitShort), chat.ID)
require.NoError(t, err)
require.Equal(t, otherWorker, latest.WorkerID.UUID)
require.Equal(t, otherRunner, latest.RunnerID.UUID)
}
func TestWorker_ReacquiresStaleOwnedChat(t *testing.T) {
t.Parallel()
f := newWorkerTestFixture(t)
chat := f.createRunningChat(t)
deadWorker := uuid.New()
deadRunner := uuid.New()
acquireChat(t, f, chat.ID, deadWorker, deadRunner)
makeHeartbeatStale(t, f, chat.ID, deadRunner)
starter := newBlockingTaskStarter(false)
worker := startWorker(t, testOptions(t, f, starter))
call := starter.waitCall(t, taskKindGeneration, chat.ID)
require.Equal(t, worker.opts.WorkerID, call.input.WorkerID)
require.Equal(t, database.ChatStatusRunning, call.input.Status)
require.NotEqual(t, deadRunner, call.input.RunnerID)
latest, err := f.db.GetChatByID(testutil.Context(t, testutil.WaitShort), chat.ID)
require.NoError(t, err)
require.Equal(t, worker.opts.WorkerID, latest.WorkerID.UUID)
require.Equal(t, call.input.RunnerID, latest.RunnerID.UUID)
require.NotEqual(t, deadWorker, latest.WorkerID.UUID)
require.NotEqual(t, deadRunner, latest.RunnerID.UUID)
_, err = f.db.GetChatHeartbeat(testutil.Context(t, testutil.WaitShort), database.GetChatHeartbeatParams{
ChatID: chat.ID,
RunnerID: call.input.RunnerID,
})
require.NoError(t, err)
}
func TestWorker_TwoWorkersRaceSingleOwner(t *testing.T) {
t.Parallel()
f := newWorkerTestFixture(t)
chat := f.createRunningChat(t)
firstStarter := newRecordingTaskStarter()
secondStarter := newRecordingTaskStarter()
first := startWorker(t, testOptions(t, f, firstStarter))
second := startWorker(t, testOptions(t, f, secondStarter))
call := waitAnyTaskCall(t, firstStarter, secondStarter, taskKindGeneration, chat.ID)
require.Contains(t, []uuid.UUID{first.opts.WorkerID, second.opts.WorkerID}, call.input.WorkerID)
firstStarter.assertNoCall(t)
secondStarter.assertNoCall(t)
latest, err := f.db.GetChatByID(testutil.Context(t, testutil.WaitShort), chat.ID)
require.NoError(t, err)
require.True(t, latest.WorkerID.Valid)
require.True(t, latest.RunnerID.Valid)
require.Equal(t, call.input.WorkerID, latest.WorkerID.UUID)
require.Equal(t, call.input.RunnerID, latest.RunnerID.UUID)
}
func TestWorker_AcquisitionBatchSizeLimitsSuccessfulAcquisitions(t *testing.T) {
t.Parallel()
f := newWorkerTestFixture(t)
first := f.createRunningChat(t)
second := f.createRunningChat(t)
third := f.createRunningChat(t)
starter := newRecordingTaskStarter()
opts := testOptions(t, f, starter)
opts.AcquisitionBatchSize = 1
worker := startWorker(t, opts)
want := map[uuid.UUID]bool{first.ID: true, second.ID: true, third.ID: true}
for range 3 {
call := starter.waitCall(t, taskKindGeneration, uuid.Nil)
delete(want, call.input.ChatID)
starter.assertNoCall(t)
worker.Wake()
}
require.Empty(t, want)
}
func TestWorker_DoesNotAcquireIdleOrArchivedChats(t *testing.T) {
t.Parallel()
f := newWorkerTestFixture(t)
waiting := f.createRunningChat(t)
finishTurn(t, f, waiting.ID)
errorChat := f.createRunningChat(t)
forceExecutionStateAndPublish(t, f, errorChat.ID, database.ChatStatusError, false)
archived := f.createRunningChat(t)
forceExecutionStateAndPublish(t, f, archived.ID, database.ChatStatusRunning, true)
starter := newRecordingTaskStarter()
worker := startWorker(t, testOptions(t, f, starter))
worker.Wake()
starter.assertNoCall(t)
}
func TestWorker_HeartbeatLoopRefreshesActiveRunnerHeartbeat(t *testing.T) {
t.Parallel()
f := newWorkerTestFixture(t)
chat := f.createRunningChat(t)
clock := quartz.NewMock(t)
heartbeatTrap := clock.Trap().NewTicker("chatworker", "heartbeat")
defer heartbeatTrap.Close()
starter := newBlockingTaskStarter(false)
opts := testOptions(t, f, starter)
opts.Clock = clock
opts.HeartbeatInterval = time.Minute
startWorker(t, opts)
heartbeatTrap.MustWait(testutil.Context(t, testutil.WaitLong)).MustRelease(testutil.Context(t, testutil.WaitLong))
call := starter.waitCall(t, taskKindGeneration, chat.ID)
oldHeartbeat := makeHeartbeatStale(t, f, chat.ID, call.input.RunnerID)
clock.Advance(time.Minute).MustWait(testutil.Context(t, testutil.WaitLong))
testutil.Eventually(testutil.Context(t, testutil.WaitLong), t, func(ctx context.Context) bool {
heartbeat, err := f.db.GetChatHeartbeat(ctx, database.GetChatHeartbeatParams{
ChatID: chat.ID,
RunnerID: call.input.RunnerID,
})
return err == nil && heartbeat.HeartbeatAt.After(oldHeartbeat)
}, testutil.IntervalFast, "heartbeat should be refreshed")
}
func TestWorker_HeartbeatCleanupDeletesStaleRows(t *testing.T) {
t.Parallel()
f := newWorkerTestFixture(t)
chat := f.createRunningChat(t)
finishTurn(t, f, chat.ID)
runnerID := uuid.New()
require.NoError(t, f.db.UpsertChatHeartbeat(testutil.Context(t, testutil.WaitShort), database.UpsertChatHeartbeatParams{
ChatID: chat.ID,
RunnerID: runnerID,
}))
makeHeartbeatStale(t, f, chat.ID, runnerID)
clock := quartz.NewMock(t)
cleanupTrap := clock.Trap().NewTicker("chatworker", "heartbeat-cleanup")
defer cleanupTrap.Close()
starter := newRecordingTaskStarter()
opts := testOptions(t, f, starter)
opts.Clock = clock
opts.HeartbeatCleanupInterval = time.Minute
startWorker(t, opts)
cleanupTrap.MustWait(testutil.Context(t, testutil.WaitLong)).MustRelease(testutil.Context(t, testutil.WaitLong))
clock.Advance(time.Minute).MustWait(testutil.Context(t, testutil.WaitLong))
testutil.Eventually(testutil.Context(t, testutil.WaitLong), t, func(ctx context.Context) bool {
_, err := f.db.GetChatHeartbeat(ctx, database.GetChatHeartbeatParams{
ChatID: chat.ID,
RunnerID: runnerID,
})
return errors.Is(err, sql.ErrNoRows)
}, testutil.IntervalFast)
}
func TestWorker_CloseDeletesOwnedHeartbeatsAndPublishesOwnershipHints(t *testing.T) {
t.Parallel()
f := newWorkerTestFixture(t)
first := f.createRunningChat(t)
second := f.createRunningChat(t)
starter := newBlockingTaskStarter(false)
pubsub := newRecordingPubsub(f.pubsub)
opts := testOptions(t, f, starter)
opts.Pubsub = pubsub
worker := startWorker(t, opts)
callsByChat := make(map[uuid.UUID]taskCall)
for range 2 {
call := starter.waitCall(t, taskKindGeneration, uuid.Nil)
callsByChat[call.input.ChatID] = call
}
require.Contains(t, callsByChat, first.ID)
require.Contains(t, callsByChat, second.ID)
require.NoError(t, worker.Close())
for _, call := range callsByChat {
_, err := f.db.GetChatHeartbeat(testutil.Context(t, testutil.WaitShort), database.GetChatHeartbeatParams{
ChatID: call.input.ChatID,
RunnerID: call.input.RunnerID,
})
require.ErrorIs(t, err, sql.ErrNoRows)
}
messages := pubsub.ownershipMessages(t)
seen := make(map[uuid.UUID]bool)
for _, msg := range messages {
seen[msg.ChatID] = true
require.NotZero(t, msg.SnapshotVersion)
}
require.True(t, seen[first.ID], "expected ownership hint for first runner")
require.True(t, seen[second.ID], "expected ownership hint for second runner")
}
func TestWorker_CloseIsIdempotentAndDoesNotBlock(t *testing.T) {
t.Parallel()
f := newWorkerTestFixture(t)
chat := f.createRunningChat(t)
starter := newBlockingTaskStarter(false)
worker := startWorker(t, testOptions(t, f, starter))
call := starter.waitCall(t, taskKindGeneration, chat.ID)
closed := make(chan error, 1)
go func() {
if err := worker.Close(); err != nil {
closed <- err
return
}
closed <- worker.Close()
}()
select {
case err := <-closed:
require.NoError(t, err)
case <-time.After(testutil.WaitLong):
t.Fatal("worker close did not return")
}
select {
case <-call.ctx.Done():
case <-time.After(testutil.WaitLong):
t.Fatal("active task was not canceled")
}
}
func waitAnyTaskCall(
t *testing.T,
first *recordingTaskStarter,
second *recordingTaskStarter,
kind taskKind,
chatID uuid.UUID,
) taskCall {
t.Helper()
deadline := time.After(testutil.WaitLong)
for {
select {
case call := <-first.callCh:
if call.kind == kind && call.input.ChatID == chatID {
return call
}
case call := <-second.callCh:
if call.kind == kind && call.input.ChatID == chatID {
return call
}
case <-deadline:
t.Fatal("timed out waiting for either worker to start task")
return taskCall{}
}
}
}
func requireTaskCanceled(t *testing.T, call taskCall) {
t.Helper()
select {
case <-call.ctx.Done():
require.True(t, errors.Is(call.ctx.Err(), context.Canceled))
case <-time.After(testutil.WaitLong):
t.Fatal("task context was not canceled")
}
}