`chatd.WaitUntilIdleForTest` waited up to 10 seconds for the chat worker
to go idle, dropped the timeout error, and then drained tracked work, so
a test whose worker never went idle passed after a silent 10-second
stall. The hook now takes the caller's `ctx` and returns an error when
`ctx` ends before the worker is idle or the tracked work finishes. The
26 callers that expect success wrap it in `require.NoError`. The poll
loop moves from `chatWorker.WaitIdle`, whose only caller was the hook,
into `testhooks.go`. It polls on a real 10 ms ticker instead of the
server clock, which a mock clock never advances.
## Why it is safe
- The idle check is unchanged: the hook reads the runner manager under
the worker's lock and returns at once when the worker or manager is nil.
The drain still calls `drainInflight`, now in a goroutine so that `ctx`
bounds it; that goroutine exits once `Close` cancels the tracked work.
- `TestWaitUntilIdleForTestTimeout` pins both error returns without a
database: it passes an expired `ctx` while one runner is open
(`RunnerOpen`) or one tracked task is in flight (`InflightWork`) and
requires `context.DeadlineExceeded`. Changing both returns to `return
nil` fails both subtests with `Expected error with "context deadline
exceeded" in chain but got nil`; changing one fails only its subtest.
- Red-green for the idle wait: with `runnerManager.idle` forced to
return `false`, `TestCreateChatImmediatelyProcessesNewChat` passes on
`main` after 10.2 s and fails here at its 25 s context with `wait for
the chat worker to go idle: context deadline exceeded`.
- Red-green for the drain: on this PR's first head, whose drain ignored
`ctx`, a probe holding one tracked task open stayed blocked 2 s past a
100 ms context, then returned `nil`.
- Hidden timeouts: on `main`, with the hook instrumented to report
errors and idle waits over 1 s, all 119 hook calls in the packages below
returned in under 1 s without an error.
## Review focus
Callers now pass their own test context instead of getting a fresh 10 s
one. Every direct caller, and every caller of
`coderdtest.WaitForChatSettled` and `waitForChatProcessed`, passes
`testutil.WaitLong` or longer, so the budget grows, but the rest of the
test shares it.
The PR adds 100 lines and removes 54: the drain goroutine is 9 of them
and the new test 39. Without the goroutine, a stalled title or summary
call holds the hook past its deadline.
## Validation
- `go test ./coderd/x/chatd/ -count=3`: pass.
- `go test ./coderd/ -count=3 -run '<every test in exp_chats_test.go,
exp_chats_workspace_file_test.go, and exp_chats_hooks_test.go>'`: pass.
- `go test ./codersdk/toolsdk/ -count=3 -run
'^(TestChatTools|TestCreateChatOrganizationDefaultModel)$'`: pass.
- `go test -race ./coderd/x/chatd/ -run
'^TestWaitUntilIdleForTestTimeout$' -count=50`: pass.
- Mutants and the probe use `go test -overlay`; a copy with a syntax
error fails to build, so the overlay applies.
- `make pre-commit` passed.
> Xum, an AI agent, opened this PR for @ibetitsmike and wrote this
description.
This PR deletes `coderd.Options.ChatProviderAPIKeys`, its
`coderdtest.Options` copy, and the
`coderdtest.OpenAICompatProviderAPIKeys` and
`coderdtest.FakeOpenAICompatProviderAPIKeys` helpers. Only tests set the
option. `newChatTestOptions` and 27 other chat, MCP, and toolsdk
fixtures used it to start a fake OpenAI-compatible server whose fallback
keys no model call reads.
## Why it is safe
- Model calls never read the fallback keys. `newModel` builds the client
from `fantasyConfigForAIBridge`, which returns AI Gateway placeholder
keys (`coderd/x/chatd/model_routing_aibridge.go`).
- Key resolution for a selected provider passes empty fallback keys
(`resolveUserProviderAPIKeysForProvider` in `chatd.go`), and so does the
HTTP availability check in `coderd/exp_chats.go`. Active model configs
always have an `ai_provider_id` (the
`chat_model_configs_ai_provider_required_when_active` check constraint).
- The only path that merges `chatd.Config.ProviderAPIKeys` is the
provider-type lookup for the computer-use subagent. It asks only for
`anthropic` or `openai`, and the test keys were stored only under
`openai-compat`.
- Model-backed tests get their key and base URL from the DB provider
that `CreateOpenAICompatChatModel` creates.
- `git grep -n 'ChatProviderAPIKeys\|OpenAICompatProviderAPIKeys'`
matches nothing after the change.
## Review focus
`coderd.go` no longer passes `ProviderAPIKeys` to `chatd.Config`.
Production never set `ChatProviderAPIKeys`, so chatd gets the zero value
it always got in production. `chatd.Config.ProviderAPIKeys` stays for
now, because chatd internal tests still set it directly.
## Validation
Each suite ran before the change (on `e289c6fe011`) and after it, with
the same results:
- `go test ./coderd/ -run '(?i)chat|mcp' -count=1 -p 4 -parallel 4 -v`:
pass, 163 top-level tests, 899 PASS lines, 0 failures, same test set
both times.
- `go test ./codersdk/toolsdk/ -count=1 -parallel 4 -v`: pass, 234 PASS
lines, 0 failures.
- `go test ./coderd/coderdtest/... -count=1 -v`: pass, 350 PASS lines, 0
failures.
- `go build ./...`, `go vet` on the touched packages and
`./enterprise/coderd/...`, and `golangci-lint run --fix=false ./coderd/
./coderd/coderdtest/... ./codersdk/toolsdk/...`: clean.
- `make pre-commit`: passed.
> Xum, an AI agent, opened this PR for @ibetitsmike and wrote this
description.
Right now, the only way to create a chat is through `POST
/api/v2/chats`. That's fine for clients, but an integration running
inside coderd (say, a Slack bot) would have to store or mint an API key
just to call its own server.
So, we'll pull chat creation out of the handler into a shared function,
and add `api.CreateChatAsUser` for in-process callers. They get the same
validation, errors, and audit entry as the endpoint, and the endpoint's
behaviour doesn't change. This is the same pattern `chatCreateWorkspace`
already uses to let chatd create workspaces as a user. I've also
exported the chat daemon for in-process callers.
Chat errors are now just values that carry the endpoint's status,
message, and cause, so in-process callers can use `errors.As` on them
(e.g. to spot a prompt hook denial) rather than decoding an HTTP
response. The audit entry is written in the background, as auto-archive
already does, rather than through a fake request. As with an API key, a
dormant user is activated on use, whilst a suspended or deleted user
gets a 403.
The PRs above this one do the same for sending, interrupting, and
updating chats.
## Summary
Move chat model management and runtime availability to
organization-scoped API routes. The API now identifies the organization
in every model-management operation, applies chat model RBAC and ACLs
within that organization, and returns only models the caller can use
there.
Depends on #27958.
## Problem
Chat model configurations now belong to organizations, but the existing
experimental management and discovery APIs are deployment-shaped. Their
routes do not identify an organization, SDK methods do not accept an
organization ID, and item operations address a model configuration by ID
alone.
That contract cannot safely expose organization-owned model
configurations. It also does not provide the organization-specific
provider information, authorization behavior, ACL management, or
recovery guidance needed by the settings and chat clients later in the
stack.
## Fix
Add organization-scoped chat model endpoints under:
```text
/api/experimental/organizations/{organization}/chats/models
```
The new API provides:
- Organization-scoped model listing and creation.
- Model get, update, and delete operations scoped by both organization
and model ID.
- Model ACL read and update operations.
- Organization-scoped runtime availability and provider status.
- Redacted provider descriptors for model-management clients.
- Chat model RBAC and row ACL enforcement for writes, runtime discovery,
and Chatd model selection.
- Recovery guidance when a chat's organization has no usable local
model.
Historical chats can retain a model reference owned by another
organization, but runtime selection uses an authorized local default
when one is available. Foreign, inaccessible, disabled, and otherwise
unusable models are not exposed as usable selections for the
organization.
The existing collection and availability routes remain as
default-organization compatibility endpoints in this layer:
```text
GET /api/experimental/chats/model-configs
POST /api/experimental/chats/model-configs
GET /api/experimental/chats/models
```
These compatibility routes do not provide access to models in
non-default organizations. The previous unscoped item update and delete
routes are removed because item operations must include the owning
organization.
## Breaking change
This changes the experimental HTTP API and Go SDK contracts for chat
model management.
Clients must migrate model-management and availability requests to the
organization-scoped routes and provide an organization identifier. In
the Go SDK, the following methods now require an organization ID:
- `ChatModelAvailability`
- `ChatModels`
- `CreateChatModel`
- `UpdateChatModel`
- `DeleteChatModel`
`ChatModels` also changes its return value from `[]ChatModel` to
`OrganizationChatModelsResponse`, which includes the organization models
and the redacted provider descriptors required by management clients.
New item and ACL methods likewise require both the organization ID and
model ID.
The default-organization compatibility routes reduce the immediate
impact for collection and availability callers, but they do not preserve
the previous item API or SDK method signatures. Generated clients and
direct consumers of the experimental API must be updated before adopting
this change.
_This pull request description was generated by Coder Agents._
Rename the admin-managed SDK resource from `ChatModelConfig` to
`ChatModel`. Rename catalog entries from `ChatModel` to
`ChatModelCatalogEntry` and replace `ChatModelsResponse` with
`ChatModelAvailabilityResponse`.
API routes and JSON fields do not change. Experimental SDK and OpenAPI
schema names do change.
Depends on #27957
_This pull request description was generated by Coder Agents._
The chatd state machine only recognizes `waiting`, `running`, `error`,
`requires_action`, and `interrupting`. Remove the unused `pending`,
`paused`, and `completed` values from the database enum, backend, SDK,
frontend, generated queries, and API docs.
Migration `000543_chat_status_remove_unused` remaps existing `pending`
rows to `running`, remaps `paused` and `completed` rows to `waiting`,
drops the obsolete `idx_chats_pending` index, and recreates
`chats_expanded` around the enum swap. It also removes the dead
`AcquireChats` query and all remaining query literals for the deleted
statuses.
**NOTE**: The enum swap can break chat queries from older replicas
during a mixed-version rollout because they still reference
`'pending'::chat_status`. Chats are experimental, so this PR accepts
that limited rollout window instead of adding a two-release expand and
contract sequence.
> This PR was authored by Mux (AI agent) on Mike's behalf.
The provider type already lives authoritatively in ai_providers.type,
reachable on every active row through ai_provider_id, which the
chat_model_configs_ai_provider_required_when_active CHECK makes
mandatory. The stored provider string was a denormalized copy the system
kept in sync with a startup backfill and no longer needs.
Every surface now derives provider type from the linked ai_providers
row. Telemetry is the one exception: it keeps emitting provider, now
sourced from ai_providers.type via a JOIN, so the BigQuery column and the
Nexus dashboards that read it are unaffected. The experimental HTTP/SDK
response drops provider and makes ai_provider_id required, since those
endpoints return only active configs; consumers resolve provider type
from ai_provider_id and the AI providers listing.
This ships in a single release with no compatibility window: production
reads the table via SELECT *, so a pre-drop binary fails config reads the
moment the column is gone. Operators must scale to zero before upgrading,
and there is no rollback.
Closes CODAGT-599
Chat tests previously constructed a real `openai` provider with a fake
API key and no `BaseURL`, so background title generation hit
`api.openai.com` and timed out under `-race`. The same root cause
produced several distinct flakes: title regeneration races with
synchronous `UpdateChat`/`ProposeChatTitle`, and pagination races
against `updated_at` bumps from real-network processing.
This moves the fake OpenAI-compatible provider and the chat-settle wait
into first-class `coderdtest` capabilities.
`coderd.Options.ChatProviderAPIKeys` is the new seam tests use to
redirect chat traffic to a local `httptest.Server`.
`coderdtest.WaitForChatSettled` replaces per-test waiters and drains
tracked chat-daemon work after the chat row leaves `pending`/`running`.
The `newChatClient*` constructors funnel through one options builder
that installs the fake provider before the coderd test server so cleanup
ordering is deterministic.
Closes https://github.com/coder/internal/issues/1528 & Closes ENG-2659
Closes https://github.com/coder/internal/issues/1480 & Closes CODAGT-359
Closes https://github.com/coder/internal/issues/1507 & Closes CODAGT-368
Relates to https://github.com/coder/internal/issues/1397 & Relates to
CODAGT-374