Repository navigation
refactor: add dbgen chat generators and migrate test boilerplate - #24497
Conversation
There was a problem hiding this comment.
Pull request overview
Adds dbgen helpers for chat-related tables and migrates test suites away from hand-rolled chat/provider/model/message inserts.
Changes:
- Introduce new
dbgengenerators:Chat,ChatMessage,ChatModelConfig, andChatProvider, plus unit tests for defaults/overrides. - Refactor many tests across
coderd/andenterprise/to seed chat data viadbgeninstead of rawInsertChat*calls. - Simplify several chat test helper functions by delegating inserts to
dbgen.
Reviewed changes
Copilot reviewed 20 out of 20 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| enterprise/dbcrypt/dbcrypt_internal_test.go | Uses dbgen.ChatProvider instead of raw provider insert in encryption/key tests. |
| enterprise/coderd/x/chatd/usagelimit_test.go | Migrates provider/model/chat/message seeding to dbgen for usage limit tests. |
| enterprise/coderd/x/chatd/chatd_test.go | Refactors chat dependency + waiting chat seed helpers to use dbgen. |
| coderd/x/gitsync/worker_test.go | Switches chat FK-chain setup (provider/model/chat) to dbgen. |
| coderd/x/chatd/subagent_test.go | Updates helper invocation to new seedChatDependencies signature. |
| coderd/x/chatd/subagent_internal_test.go | Migrates provider/model/message inserts to dbgen and keeps raw insert where Enabled=false is needed. |
| coderd/x/chatd/subagent_context_internal_test.go | Replaces bulk InsertChatMessages seeding with repeated dbgen.ChatMessage calls. |
| coderd/x/chatd/recording_internal_test.go | Uses dbgen.Chat to insert parent/child chats for recording tests. |
| coderd/x/chatd/chattool/startworkspace_test.go | Migrates chat inserts and provider/model-config seeding to dbgen. |
| coderd/x/chatd/chatprompt/chatprompt_test.go | Migrates provider/model/chat/message DB round-trip setup to dbgen. |
| coderd/x/chatd/chatdebug/service_test.go | Switches provider/model/chat/message seeding to dbgen. |
| coderd/x/chatd/chatd_test.go | Replaces many raw chat/message/provider/model-config inserts with dbgen and refactors seed helpers. |
| coderd/workspaceagents_chat_context_test.go | Migrates chat model config creation and message inserts to dbgen.ChatModelConfig/dbgen.ChatMessage. |
| coderd/workspaceagents_chat_context_internal_test.go | Uses dbgen.ChatProvider/dbgen.ChatModelConfig for model-config fixture setup. |
| coderd/workspaceagents_active_chat_internal_test.go | Uses dbgen.Chat in active agent chat definition tests. |
| coderd/telemetry/telemetry_test.go | Migrates telemetry fixtures (providers/model configs/chats/messages) to dbgen. |
| coderd/httpmw/chatparam_test.go | Uses dbgen for provider/model/chat setup in middleware tests. |
| coderd/exp_chats_test.go | Migrates multiple chat/message inserts for experimental chat API tests to dbgen. |
| coderd/database/dbgen/dbgen_test.go | Adds generator coverage for the new chat-related dbgen helpers. |
| coderd/database/dbgen/dbgen.go | Implements the new chat-related dbgen generators. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
All four Copilot comments addressed in 7f8f366:
|
johnstcn
left a comment
There was a problem hiding this comment.
might also want to add helpers for stuff like this https://github.com/coder/coder/pull/24280/changes#r3109086721
7f8f366 to
5ad887b
Compare
…ChatProvider generators Four new dbgen helpers for seeding chat test data. No more hand-rolling 19-element token arrays for a single message.
RIP boilerplate, you won't be missed. Every InsertChat, InsertChatMessages, InsertChatProvider, and InsertChatModelConfig call in test files now uses the shiny new dbgen generators. Zero raw chat inserts remain outside the database package itself.
takeFirst(false, true) == true, so dbgen.ChatModelConfig can't seed Enabled=false. Use raw InsertChatModelConfig where tests explicitly need a disabled model.
- Default ChatMessage content to [] (JSON array) not {} (object),
matching content version 1 expectations.
- Use raw InsertChatProvider where tests need CentralApiKeyEnabled=false
or APIKey="" since takeFirst treats these zero values as unset.
71cab23 to
e67c437
Compare
|
/coder-agents-review |
There was a problem hiding this comment.
First-pass mechanical review (Netero). The full review panel has not yet reviewed this PR; these are defects the author should address before the panel spends parallel review time.
The migration is well-structured: dbgen generators follow existing takeFirst/genCtx conventions, test call sites are consistently updated, and the net LoC reduction is clean. One P2, two P3s, one Nit.
The P2 is an incomplete migration: six helper functions kept ctx context.Context parameters that are no longer referenced after the raw DB calls were replaced with dbgen. The non-enterprise seedChatDependencies was correctly cleaned up, making these inconsistencies visible.
"Each unused ctx forces every caller to pass a value that does nothing, and the next contributor will wonder whether it matters." (Netero)
enterprise/coderd/x/chatd/chatd_test.go:92
P2 [DEREM-1] Six helper functions retained unused ctx context.Context parameters after the migration replaced raw DB calls with dbgen. The non-enterprise seedChatDependencies correctly removed ctx, making these inconsistencies visible.
Affected functions:
enterprise/coderd/x/chatd/chatd_test.go:seedChatDependencies(line 92)enterprise/coderd/x/chatd/chatd_test.go:seedWaitingChat(line 123)coderd/x/chatd/subagent_internal_test.go:insertAssistantMessage(line 2143)coderd/exp_chats_test.go:insertAssistantCostMessage(line 219)coderd/x/chatd/chatdebug/service_test.go:insertChat(line 1108)coderd/x/chatd/chatdebug/service_test.go:insertMessage(line 1127)
None of these reference ctx in their bodies. Remove the parameter and cascade to callers. (Netero)
🤖
🤖 This review was automatically generated with Coder Agents.
…t.Helper, stray blank line)
|
All four findings from the coder-agents review addressed in 8a03222:
|
|
/coder-agents-review |
There was a problem hiding this comment.
Full panel review (12 reviewers). R1 findings (DEREM-1 through DEREM-4) all verified as addressed in 8a03222.
The migration is well-executed: five generators follow existing takeFirst/genCtx/munge conventions, 18 test files are consistently updated, and the net LoC reduction is clean. The munge pattern is correctly applied everywhere a zero-value field needs to be explicitly set. CI passes.
Two concerns. First, the PR description claims "zero raw chat inserts remain outside coderd/database/" but two survive in integration_responses_test.go (P2). Second, the unused ctx cleanup from DEREM-1 missed several more functions beyond the one Netero caught in R1 (P3). Both are the same pattern: class-level fixes applied instance-by-instance.
Severity count: 1 P2, 4 P3, 3 Nit from the panel, plus 1 P3 and 2 Nit carried from Netero R2.
"The function signature is a lie. Someone reading
seedInternalChatDeps(ctx, t, db)thinks context cancellation governs the DB setup. It doesn't." (Leorio)
coderd/x/chatd/integration_responses_test.go:421
P2 [DEREM-9] insertOpenAIResponsesChat uses raw db.InsertChat directly. The PR description states "Zero raw chat inserts remain outside coderd/database/ itself," but this function and insertOpenAIResponsesMessages (line 466) were not migrated.
insertOpenAIResponsesChat is a single-insert with no special semantics; dbgen.Chat handles all its fields. The batch insertOpenAIResponsesMessages has a structural reason to stay (builds variadic InsertChatMessagesParams that dbgen.ChatMessage can't express). Either migrate the single insert and update the description to note the batch exception, or correct the completeness claim.
"Running
grep -rn 'db\.InsertChat\b' --include='*_test.go'outsidecoderd/database/would have falsified the claim before it was written." (Mafu-san)
(Mafu-san P1, Mafuuu P3, Pariston P4, Ryosuke P3, Robin P3, Kite P4)
🤖
coderd/x/chatd/subagent_internal_test.go:175
P3 [DEREM-10] Five more helper functions retain unused ctx context.Context parameters after migration to dbgen, same class as DEREM-1/DEREM-5:
seedInternalChatDepshere (39 callers)seedChatatchatdebug/service_test.go:1085(7 callers)createAgentChatContextChatatworkspaceagents_chat_context_test.go:990createAgentChatContextChildChatatworkspaceagents_chat_context_test.go:1011insertOpenAIResponsesModelConfigatintegration_responses_test.go:386
All five call only dbgen functions (which use genCtx internally); ctx is never referenced. Note: insertAgentChatTestModelConfig (line 91 in workspaceagents_chat_context_internal_test.go) uses _ context.Context deliberately since it's exported via export_test.go; that one is fine.
(Leorio P3, Ryosuke P3, Kite P3)
🤖
coderd/x/chatd/chatd_test.go:1572
P3 [DEREM-5] insertParentWithActiveChild still accepts ctx context.Context but never references it. The body calls only dbgen.Chat, which uses genCtx internally. Remove the parameter and update the two call sites (lines 1558, 1609). insertParentWithArchivedChild still needs ctx for db.ArchiveChatByID, so only this function is affected. (Netero, confirmed by Hisoka, Gon, Kite)
🤖
🤖 This review was automatically generated with Coder Agents.
|
Resolved the remaining body findings from review
Validation:
|
|
/coder-agents-review |
There was a problem hiding this comment.
Re-review (4 reviewers). All 10 R2 findings verified as addressed. Netero clean.
The migration is in good shape. All single-insert raw calls are now migrated to dbgen. The generator tests are thorough (DEREM-12 fix verified: all token fields, Content, Compressed, ProviderResponseID covered). The takeFirst zero-value hazard is correctly handled everywhere via munge callbacks. Named constants replace magic numbers. The ctx cleanup is nearly complete.
One P3 remains: insertAgentChatTestModelConfig has the same unused-ctx pattern as DEREM-5/DEREM-10 (blanked to _ context.Context instead of removed). One Nit: the PR description should note the surviving batch InsertChatMessages exception.
"The export boundary does not change the analysis; those callers are all in the same PR's scope and follow the same mechanical edit pattern." (Bisky)
coderd/x/chatd/integration_responses_test.go:463
Nit [DEREM-16] The PR description still claims "Zero raw chat inserts remain outside coderd/database/ itself" and "Replace every raw InsertChat, InsertChatMessages..." The batch insertOpenAIResponsesMessages here legitimately stays raw (builds variadic InsertChatMessagesParams that dbgen.ChatMessage can't express). Either qualify the PR description to note this exception, or add a brief comment here explaining why this helper is exempt. (Mafu-san, Mafuuu)
🤖
🤖 This review was automatically generated with Coder Agents.
Resolved in b83762c |
Summary
Add dbgen helpers for chat, chat messages, model configs, and providers, then migrate chat-related test setup away from hand-rolled single-row inserts.
Changes
takeFirst/genCtxconventions.InsertChatMessagesbecause it builds variadicInsertChatMessagesParamsthatdbgen.ChatMessagecannot express.