Skip to content

fix(coderd): allow deleting chat providers used in historical chats - #24568

Merged
ethanndickson merged 3 commits into
mainfrom
ethan/delete-chat-provider-historical-usage
Apr 22, 2026
Merged

ethanndickson merged 3 commits into
mainfrom
ethan/delete-chat-provider-historical-usage

Conversation

@ethanndickson

@ethanndickson ethanndickson commented Apr 21, 2026 •

Copy link
Copy Markdown
Member

Drop the chat_model_configs.provider -> chat_providers.provider foreign key and soft-delete model configs when their provider is removed. The provider row is now hard-deleted inside a transaction that also tombstones its model configs and promotes a replacement default when needed.

Historical chats and messages keep pointing at the soft-deleted model config rows, which are hidden from live/admin queries but still resolve for read. The runtime chat path already falls back to the default model config when a soft-deleted config is looked up.

Replaces the lost FK validation in the create/update model-config handlers with an explicit provider lookup that returns the existing Chat provider is not configured. 400.

UX

Admin deleting a chat provider that has historical usage

  • Before: blocked with 400 Provider models are still referenced by existing chats. Admins had no in-product way to remove a provider that had ever been used.
  • After: delete succeeds (204). Any model configs under that provider are soft-deleted. If the removed provider owned the default model config, one of the remaining live configs is auto-promoted to the new default. The promotion is deterministic (ensureDefaultChatModelConfig picks the first live config by provider ASC, model ASC, updated_at DESC, id DESC); there is no picker, and no toast or response detail names which config became the new default.

End users with chats that used a deleted provider's model

  • Old chats still open and their history still renders unchanged.
  • Sending a new turn in such a chat silently falls back to the current default model. No banner or warning tells the user the original model is gone.
  • The model picker no longer lists the deleted model.
  • If no default model config exists at all after the delete, sending a new turn fails with no default chat model config is available.

Admin creating or updating a model config against a provider that is not configured

  • Same as before: 400 Chat provider is not configured. Only the detection mechanism changed (explicit FOR UPDATE lookup inside the transaction, which also serializes against a concurrent provider delete).

Admin updating a model config whose row disappears mid-transaction

  • Now returns the standard 404 Resource not found or you do not have access to this resource instead of the previous 500 that leaked sql: no rows in result set in the detail. Unrelated internal races (for example a race on the promoted default candidate) are still reported as 500 so they are not misclassified as "your target is gone".

Closes CODAGT-23

Drop the chat_model_configs.provider -> chat_providers.provider foreign
key and soft-delete model configs when their provider is removed. The
provider row is now hard-deleted inside a transaction that also
tombstones its model configs and promotes a replacement default when
needed.

Historical chats and messages keep pointing at the soft-deleted model
config rows, which are hidden from live/admin queries but still resolve
for read. The runtime chat path already falls back to the default model
config when a soft-deleted config is looked up.

Replaces the lost FK validation in the create/update model-config
handlers with an explicit provider lookup that returns the existing
`Chat provider is not configured.` 400.

Closes CODAGT-23
@ethanndickson
ethanndickson force-pushed the ethan/delete-chat-provider-historical-usage branch from b424bed to 3c22da5 Compare April 21, 2026 13:24
@ethanndickson
ethanndickson marked this pull request as ready for review April 21, 2026 13:29

@ethanndickson ethanndickson left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice fix. Dropping the FK and pairing FOR UPDATE on the provider row with an explicit requireChatProviderForModelConfig check is the right shape: the create/delete race is serialized, the runtime chatd fallback already absorbs the soft-delete, and replacing database.IsForeignKeyViolation(err) with the errChatProviderNotConfigured sentinel tightens error classification.

One P0 merge-blocker (missing dbauthz test entries, CI is already failing on it), four P2s worth looking at (lock class, invariant-moved-to-handler, inlined cascade, test coverage gap), a few P3s, and nits across 9 inline comments. This review contains findings that may need attention before merge.

Comment thread coderd/database/dbauthz/dbauthz.go Resolved
Comment thread coderd/exp_chats.go Resolved
Comment thread coderd/exp_chats_test.go Resolved
Comment thread coderd/exp_chats.go Resolved
Comment thread coderd/exp_chats.go Resolved
Comment thread coderd/exp_chats_test.go Resolved
Add the missing dbauthz method-suite coverage for the three new chat
provider wrapper methods so the accounting test reflects their RBAC
contracts and CI can pass again.

Map sql.ErrNoRows from updateChatModelConfig's transaction to 404
instead of leaking a 500 when the target row disappears mid-update.

Add coverage for deleting the only provider when its sole default model
config is historically referenced, and document the rollback migration's
stub-provider backfill assumptions.
@ethanndickson

Copy link
Copy Markdown
Member Author

@codex review

@ethanndickson ethanndickson left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for turning the round-1 findings around quickly. Verified: the three TestMethodTestSuite entries, SuccessWithHistoricalChatsAndNoReplacementConfig, the rollback comment, and the sql.ErrNoRows → 404 mapping in updateChatModelConfig all do what they claim, and the new failNextUpdateChatModelConfigStore is the correct shape for intercepting an in-tx method.

Fresh review found one P3 and a few nits across 5 inline comments.

Comment thread coderd/exp_chats.go Resolved
Comment thread coderd/exp_chats_test.go Resolved
Comment thread coderd/exp_chats_test.go Resolved
Comment thread coderd/exp_chats.go Resolved

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: cb4b9d76d9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread coderd/exp_chats.go Resolved
@ethanndickson

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ethanndickson
ethanndickson merged commit ad19065 into main Apr 22, 2026
30 checks passed
@ethanndickson
ethanndickson deleted the ethan/delete-chat-provider-historical-usage branch April 22, 2026 09:34
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 22, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants