Files
coder/coderd/x/chatd/modelcall.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

219 lines
7.2 KiB
Go

package chatd
import (
"context"
"charm.land/fantasy"
"golang.org/x/xerrors"
"cdr.dev/slog/v3"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbauthz"
"github.com/coder/coder/v2/coderd/util/ptr"
"github.com/coder/coder/v2/coderd/x/chatd/chatdebug"
"github.com/coder/coder/v2/coderd/x/chatd/chatprovider"
"github.com/coder/coder/v2/codersdk"
)
const defaultChatMaxOutputTokens = int64(32_000)
// fixedModelCall selects a provider/model pair that has no config row of its
// own (computer use).
type fixedModelCall struct {
providerType string
modelName string
callConfig codersdk.ChatModelCallConfig
}
type modelCallSpec struct {
// purpose labels resolver logs only; it does not affect call behavior.
purpose string
chat database.Chat
explicitConfig *database.ChatModelConfig
fixedModel *fixedModelCall
// requestedEffort overrides the config's default reasoning effort,
// clamped to the config's max.
requestedEffort *string
// maxOutputTokens overrides the config's output-token cap and the
// package default.
maxOutputTokens *int64
// chatdScopedRoute resolves the route with chatd scope. Deployment-wide
// override models must route for user-owned chats regardless of the
// caller's actor.
chatdScopedRoute bool
buildOptions modelBuildOptions
}
func chatRequestedEffort(chat database.Chat) *string {
if !chat.LastReasoningEffort.Valid {
return nil
}
return new(string(chat.LastReasoningEffort.ChatReasoningEffort))
}
// modelCallConfigParseError lets the advisor distinguish malformed options
// from route and client failures when deciding whether to fall back.
type modelCallConfigParseError struct{ err error }
func (e modelCallConfigParseError) Error() string {
return "parse model call config: " + e.err.Error()
}
func (e modelCallConfigParseError) Unwrap() error { return e.err }
type resolvedModelCall struct {
model chatprovider.Model
dbConfig database.ChatModelConfig
callConfig codersdk.ChatModelCallConfig
providerOptions fantasy.ProviderOptions
resolvedProvider string
resolvedModel string
route aiGatewayModelRoute
debugEnabled bool
}
// resolveModelCall is the single pipeline from a spec to a ready model
// client plus the call metadata flows need.
func (p *Server) resolveModelCall(ctx context.Context, spec modelCallSpec) (resolvedModelCall, error) {
out := resolvedModelCall{}
var modelName string
var configOptions []byte
switch {
case spec.fixedModel != nil:
modelName = spec.fixedModel.modelName
case spec.explicitConfig != nil:
out.dbConfig = *spec.explicitConfig
modelName = out.dbConfig.Model
configOptions = out.dbConfig.Options
default:
dbConfig, err := p.resolveModelConfig(ctx, spec.chat)
if err != nil {
return resolvedModelCall{}, xerrors.Errorf("resolve model config: %w", err)
}
if !dbConfig.Enabled {
return resolvedModelCall{}, xerrors.Errorf("chat model config %s is disabled", dbConfig.ID)
}
out.dbConfig = dbConfig
modelName = dbConfig.Model
configOptions = dbConfig.Options
}
// clientCallConfig drives client construction; out.callConfig drives
// per-call option derivation and comes from the chat model for computer
// use, whose fixed model has no config of its own.
clientCallConfig, err := parseModelConfigOptions(configOptions)
if err != nil {
return resolvedModelCall{}, modelCallConfigParseError{err: err}
}
if spec.fixedModel != nil {
out.callConfig = spec.fixedModel.callConfig
} else {
out.callConfig = clientCallConfig
}
switch {
case spec.maxOutputTokens != nil:
out.callConfig.MaxOutputTokens = spec.maxOutputTokens
case out.callConfig.MaxOutputTokens == nil:
out.callConfig.MaxOutputTokens = ptr.Ref(defaultChatMaxOutputTokens)
}
routeCtx := ctx
if spec.chatdScopedRoute {
//nolint:gocritic // Deployment-wide override models need chatd-scoped provider reads for user-owned chats.
routeCtx = dbauthz.AsChatd(ctx)
}
if spec.fixedModel != nil {
out.route, err = p.resolveModelRouteForProviderType(routeCtx, spec.chat.OwnerID, spec.fixedModel.providerType)
} else {
out.route, err = p.resolveModelRouteForConfig(routeCtx, spec.chat.OwnerID, out.dbConfig)
}
if err != nil {
return resolvedModelCall{}, err
}
// The resolved identity feeds metadata, logs, and debug labels. The
// client is constructed with the configured model string so gateway
// validation sees the name exactly as configured.
out.resolvedProvider, out.resolvedModel, err = chatprovider.ResolveModelWithProviderHint(
modelName,
out.route.ModelProviderHint,
)
if err != nil {
return resolvedModelCall{}, xerrors.Errorf("resolve model metadata: %w", err)
}
debugSvc := p.debugService()
out.debugEnabled = debugSvc != nil && debugSvc.IsEnabled(ctx, spec.chat.ID, spec.chat.OwnerID)
buildOpts := spec.buildOptions
buildOpts.RecordHTTP = out.debugEnabled
model, err := p.newModel(ctx, modelClientRequest{
Chat: spec.chat,
ModelName: modelName,
UserAgent: chatprovider.UserAgent(),
ExtraHeaders: chatprovider.CoderHeaders(spec.chat),
CallConfig: clientCallConfig,
}, out.route, buildOpts)
if err != nil {
return resolvedModelCall{}, xerrors.Errorf("create model: %w", err)
}
if out.debugEnabled {
model = model.WithLanguageModel(chatdebug.WrapModel(model.LanguageModel(), debugSvc, chatdebug.RecorderOptions{
ChatID: spec.chat.ID,
OwnerID: spec.chat.OwnerID,
}))
}
out.model = model
out.providerOptions = chatprovider.ProviderOptionsForCall(out.model, out.callConfig, spec.requestedEffort)
p.logger.Debug(ctx, "resolved model call",
slog.F("purpose", spec.purpose),
slog.F("chat_id", spec.chat.ID),
slog.F("provider", out.resolvedProvider),
slog.F("model", out.resolvedModel),
slog.F("debug_enabled", out.debugEnabled),
)
return out, nil
}
func (r resolvedModelCall) newCall() fantasy.Call {
return fantasy.Call{
ProviderOptions: r.providerOptions,
MaxOutputTokens: r.callConfig.MaxOutputTokens,
Temperature: r.callConfig.Temperature,
TopP: r.callConfig.TopP,
TopK: r.callConfig.TopK,
PresencePenalty: r.callConfig.PresencePenalty,
FrequencyPenalty: r.callConfig.FrequencyPenalty,
}
}
// compactionSummaryCall follows the resolved call template, except summaries
// must not call tools and must not carry the default output cap: the summary
// request is non-streaming, and the Anthropic SDK rejects non-streaming
// requests whose max_tokens implies a completion longer than ten minutes.
func compactionSummaryCall(resolved resolvedModelCall) fantasy.Call {
call := resolved.newCall()
toolChoiceNone := fantasy.ToolChoiceNone
call.ToolChoice = &toolChoiceNone
call.MaxOutputTokens = nil
return call
}
func (r resolvedModelCall) newObjectCall(schemaName, schemaDescription string, maxOutputTokens int64) fantasy.ObjectCall {
return fantasy.ObjectCall{
SchemaName: schemaName,
SchemaDescription: schemaDescription,
MaxOutputTokens: ptr.Ref(maxOutputTokens),
Temperature: r.callConfig.Temperature,
TopP: r.callConfig.TopP,
TopK: r.callConfig.TopK,
PresencePenalty: r.callConfig.PresencePenalty,
FrequencyPenalty: r.callConfig.FrequencyPenalty,
ProviderOptions: r.providerOptions,
}
}