Skip to content

feat: add AI provider schema expansion - #25412

Merged
ibetitsmike merged 7 commits into
mainfrom
mike/ai-providers/schema-expand
May 22, 2026
Merged

ibetitsmike merged 7 commits into
mainfrom
mike/ai-providers/schema-expand

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented May 16, 2026 •

Copy link
Copy Markdown
Collaborator

Mux working on behalf of Mike.

Stack Context

This stack migrates Agents chat provider configuration from legacy chat provider tables to the shared AI provider model.

Active Stack

This PR body is the source of truth for the active stack order and Graphite links. Stack-listing comments are intentionally removed to avoid drift.

Order PR Graphite Branch Scope
1 #25412 Graphite mike/ai-providers/schema-expand Schema expansion, user AI provider keys, model config provider IDs, and data backfill.
2 #25414 Graphite mike/ai-providers/backend-cutover Chatd runtime reads AI providers and AI provider keys.
3 #25415 Graphite mike/ai-providers/frontend-cutover API and Agents settings UI use AI provider backed data.
4 #25416 Graphite mike/ai-providers/legacy-cleanup Remove legacy chat provider tables and compatibility code.

PR #25411 is the merged docs glossary and is not in the active stack. PR #25413 is no longer part of the active stack.

What?

  • Extends AI provider schema for Agents provider types and user-scoped AI provider keys.
  • Adds chat_model_configs.ai_provider_id so model configs can target concrete provider instances.
  • Backfills legacy chat providers into ai_providers using preserved IDs and agents-* names.
  • Copies encrypted provider and user keys into the AI provider key tables without decrypting them.
  • Runs a one-time backfill in 000504_ai_providers_backfill without adding sync triggers between legacy and AI provider tables.
  • Extends deleted-user cleanup for user AI provider keys while preserving existing user skill cleanup.
  • Adds dbcrypt rotation support and migration coverage for the new user key table.
  • Improves migration coverage for 000504_ai_providers_backfill to assert pre-backfill, post-backfill, and down-migration state across multiple legacy providers.

Validation

  • make gen
  • go test ./coderd -run TestUserSkillSoftDeleteCleanup -count=1
  • go test -count=1 ./coderd/database/migrations -run 'TestMigrate/(Once|UpDownUp)'
  • make pre-commit via git hook
  • Targeted coderd, dbauthz, dbcrypt, and frontend lint checks on the combined stack

@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

/coder-agents-review

@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: 28b6e9d6aa

ℹ️ 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 enterprise/dbcrypt/cliutil.go Resolved

@coder-agents-review coder-agents-review Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

First-pass review (Netero). This is a mechanical scan only; the full review panel has not yet reviewed this PR. The panel will review after these findings are addressed.

The schema expansion is well-structured: naming is consistent with the existing chat-provider patterns, dbauthz tests cover the authorization layer, and the cliutil rotation/decrypt extensions follow the established per-table crypto pattern.

1 P0, 1 P1, 1 Nit, 1 Note.

The P0 (gen failure) and P1 (missing dbcrypt tests) should be resolved before the panel spends parallel review time.

"5 of 7 query methods have no callers outside infrastructure. They exist for the next PR in the stack. If the stack stalls, these are orphaned exports."

🤖 This review was automatically generated with Coder Agents.

Comment thread enterprise/dbcrypt/dbcrypt.go Resolved
Comment thread coderd/database/dbauthz/dbauthz.go Resolved
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

/coder-agents-review

1 similar comment
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

/coder-agents-review

@coder-agents-review coder-agents-review Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Round 2 is blocked on one unresolved finding. DEREM-2 and DEREM-3 were addressed in commit 2fc890fe7 (tests added, comment added). DEREM-4 (informational note about dead code) is dropped.

DEREM-1 (P0): CI gen check was failing in round 1. The thread was resolved without explanation and no commit targets gen sync. The PR was rebased and CI is currently pending (gen is no longer listed as failed), so the rebase may have resolved it. Could you confirm whether make gen produces a clean diff? The full review panel will proceed once this is verified.

🤖 This review was automatically generated with Coder Agents.

@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

Mux working on behalf of Mike.

Confirmed: I ran make gen on the current mike/ai-providers/schema-expand head after the latest docs-only merge, then ran git diff --exit-code --stat. Both exited 0, so generated files are clean.

@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

/coder-agents-review

@coder-agents-review coder-agents-review Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Round 3 (Netero). All R1 findings verified as addressed (DEREM-1 gen check confirmed clean via make gen; DEREM-2/DEREM-3 fixed in 2fc890fe7). This is still a first-pass review; the full review panel has not yet reviewed this PR.

1 P2. The panel will review after this finding is addressed.

"Encrypted user_ai_provider_keys rows with orphaned cipher references will survive the purge. Since the column has CHECK (api_key != ''), you cannot blank them; they must be deleted."


enterprise/dbcrypt/cliutil.go:470

P2 [DEREM-5] sqlDeleteEncryptedUserTokens handles every other encrypted table (user_links, external_auth_links, user_chat_provider_keys, user_secrets, chat_providers, ai_providers, ai_provider_keys) but omits the new user_ai_provider_keys table. The Delete function executes this SQL as the last-resort path when encryption keys are lost. Encrypted user_ai_provider_keys rows with orphaned cipher references will survive the purge. Since the column has CHECK (api_key != ''), you cannot blank them; they must be deleted.

Add DELETE FROM user_ai_provider_keys WHERE api_key_key_id IS NOT NULL; between the ai_provider_keys delete and the COMMIT. This corroborates the unresolved external finding (Codex) on this same file. (Netero)

🤖

🤖 This review was automatically generated with Coder Agents.

@coder-agents-review coder-agents-review Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Round 4 is blocked. DEREM-5 (P2) has had no author response or fix since it was raised in round 3.

DEREM-5: sqlDeleteEncryptedUserTokens omits the new user_ai_provider_keys table. The coder server dbcrypt delete recovery path will leave encrypted rows behind, and the CHECK (api_key != '') constraint means they cannot be blanked. This also corroborates the unresolved Codex finding on the same file.

To unblock: push a fix adding the missing DELETE statement, or respond to the finding (acknowledge, contest, or defer with a ticket). The full review panel is waiting on this.

🤖 This review was automatically generated with Coder Agents.

@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

/coder-agents-review

@coder-agents-review coder-agents-review Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Round 5, first panel review. 20 reviewers. All prior Netero findings (DEREM-1 through DEREM-5) verified as addressed.

This is a well-scoped schema expansion. The structural parallel with user_chat_provider_keys across every layer (queries, dbauthz, dbcrypt, tests) makes the change easy to audit. Authorization boundaries are correct: user-facing methods gate on personal actions, admin/maintenance paths gate on ResourceAIProvider. Encryption follows the established dbcrypt pattern with thorough test coverage. The migration ordering handles dependencies correctly, and the down migration reverses cleanly.

1 P2, 1 P3, 3 Nit, 3 Note.

The P2 (soft-delete trigger gap) is a data hygiene issue that grows with user churn. The P3 and nits are naming/documentation improvements.

Pariston: "I tried four different framings of this problem and could not build a case against the author's approach. The problem is correctly understood, the solution is proportional, and the fix is at the right causal level."

Process note (Mafu-san): the initial commit shipped dbcrypt wrappers and cliutil rotation/decrypt but missed tests (DEREM-2) and the DELETE cleanup (DEREM-5). Both were caught by review and fixed well. The dbauthz tests in the same initial commit prove the agent knows tests are expected; it skipped one category, not all. The fixes themselves are thorough (164 lines of test code, precise 2-line DELETE).


enterprise/dbcrypt/cliutil.go:505

P3 [DEREM-7] The PR added DELETE FROM user_ai_provider_keys to sqlDeleteEncryptedUserTokens but left the error message (line 505) as "delete encrypted tokens and chat provider keys" and the log (line 507) as "deleted encrypted user tokens and chat provider API keys". The SQL now touches 8 tables. An operator debugging a failed delete sees a message that covers about a third of the blast radius.

This is a pre-existing gap (predates this PR for user_secrets, ai_providers, ai_provider_keys) that this PR extends by one more table. Update both strings to reflect the actual scope. (Leorio)

🤖

enterprise/dbcrypt/cliutil.go:497

Nit [DEREM-10] The Delete doc comment says "deletes all user tokens" but the SQL purges 8 tables. The constant name sqlDeleteEncryptedUserTokens similarly understates scope. Someone deciding whether to invoke this destructive function needs to know the blast radius. Pre-existing gap, but this PR extends it. (Leorio, Gon)

🤖

🤖 This review was automatically generated with Coder Agents.

Comment thread enterprise/dbcrypt/cliutil.go Resolved
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

/coder-agents-review

1 similar comment
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

/coder-agents-review

@coder-agents-review coder-agents-review Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Round 6 is blocked. No author response or code changes for any R5 panel findings (only docs-only merges since R5).

Open findings requiring response:

DEREM-6 (P2): delete_deleted_user_resources() trigger does not clean up user_ai_provider_keys on user soft-delete. Orphaned encrypted rows accumulate and tax every dbcrypt rotation cycle. Fix: add DELETE FROM user_ai_provider_keys WHERE user_id = OLD.id; to the trigger.

DEREM-7 (P3): Error/log messages in Delete don't mention user AI provider keys (pre-existing gap this PR extends).

DEREM-8, 9, 10 (Nit): Naming and documentation improvements (GetUserAIProviderKeyByProviderID name, GetUserAIProviderKeys purpose comment, Delete doc/constant scope).

To unblock: push fixes, or respond to the findings (acknowledge, contest, or defer with a ticket). DEREM-6 is the key item.

🤖 This review was automatically generated with Coder Agents.

@coder-agents-review coder-agents-review Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Round 7 is blocked. Same 5 findings have been silent for 2 consecutive rounds since the panel review (R5). No code changes or author responses, only docs merges.

DEREM-6 (P2) remains the key item: delete_deleted_user_resources() trigger needs DELETE FROM user_ai_provider_keys WHERE user_id = OLD.id; to clean up orphaned encrypted rows on user soft-delete.

DEREM-7 (P3), DEREM-8/9/10 (Nit) are also open. Any response (fix, acknowledge, contest, or defer with ticket) unblocks.

🤖 This review was automatically generated with Coder Agents.

@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

/coder-agents-review

@coder-agents-review coder-agents-review Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Round 8. All findings resolved.

DEREM-6 (P2, soft-delete trigger) and DEREM-9 (Nit, purpose comment) addressed in commit 64402b5e4. The trigger now cleans up user_ai_provider_keys on user soft-delete, and GetUserAIProviderKeys has a purpose comment matching its sibling.

DEREM-7 (P3), DEREM-8 (Nit), DEREM-10 (Nit) dropped. All three concern pre-existing naming/documentation gaps in cliutil.go or debatable naming conventions that this PR marginally extends but did not introduce. The author's selective addressing (fixed every P0-P2 and the actionable Nit, left pre-existing gaps) is a reasonable scope boundary for a stack PR.

CI is green. 13 findings across 8 rounds: 7 fixed by author, 6 dropped by orchestrator. No open findings remain.

🤖 This review was automatically generated with Coder Agents.

@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

Mux working on behalf of Mike.

Responses to the latest coder-agents-review findings:

  • DEREM-6: fixed in 64402b5e46. The 000498 migration now replaces delete_deleted_user_resources() so user soft-delete removes user_ai_provider_keys, and the down migration restores the previous trigger body before dropping the table.
  • DEREM-8: acknowledged. I kept the generated sqlc name unchanged in this slice to avoid a broad generated rename, but the query parameters and authz wrapper require both user_id and ai_provider_id.
  • DEREM-9: fixed in 64402b5e46. GetUserAIProviderKeys now has the same dbcrypt-rotation intent comment as the provider-scoped bulk key query.
  • DEREM-11: acknowledged. I am keeping user AI provider key rotation on the dbcrypt bulk path for this schema slice. The wrapper is covered by the dbcrypt tests added in this PR.
  • DEREM-12: fixed in 64402b5e46. UpsertUserAIProviderKey now documents that id and created_at are insert-path values and are preserved on conflict.

@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

/coder-agents-review

@coder-agents-review coder-agents-review Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Round 9. Author responses received, all dispositions confirmed. Review complete.

DEREM-8 (Nit, query naming): author acknowledged, keeping name to avoid broad generated rename. DEREM-11 (Note, rotation model): author acknowledged, keeping bulk rotation by design. DEREM-12 (Note, upsert params): author added clarifying comment in 64402b5e4.

13 findings across 9 rounds. 7 fixed by author, 2 acknowledged by author, 4 dropped by orchestrator. No open findings. CI green.

🤖 This review was automatically generated with Coder Agents.

@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

/coder-agents-review

@coder-agents-review coder-agents-review Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Round 10 re-review (4 panel reviewers). New commit 95499f5c0 removes a redundant index (idx_user_ai_provider_keys_user_id); the UNIQUE constraint on (user_id, ai_provider_id) already provides a leading-column B-tree usable for user_id-only lookups.

Netero and all 4 panel reviewers: no findings. 13 findings across 10 rounds, all closed. CI pending.

Meruem: "No implicit assumptions found that the code does not express."

🤖 This review was automatically generated with Coder Agents.

@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

Mux requested this Codex review on behalf of Mike.

@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@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: 8c1df773d0

ℹ️ 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/database/models.go Resolved
Comment thread enterprise/dbcrypt/dbcrypt.go Resolved
@ibetitsmike
ibetitsmike force-pushed the mike/ai-providers/docs-decisions branch from 0729220 to 9060800 Compare May 18, 2026 15:28
@ibetitsmike
ibetitsmike force-pushed the mike/ai-providers/schema-expand branch 2 times, most recently from 06c0b16 to 222d76b Compare May 18, 2026 18:09
Base automatically changed from mike/ai-providers/docs-decisions to main May 19, 2026 23:37
@ibetitsmike
ibetitsmike force-pushed the mike/ai-providers/schema-expand branch 6 times, most recently from be8a005 to 9422812 Compare May 20, 2026 13:15
@ibetitsmike
ibetitsmike requested a review from johnstcn May 20, 2026 13:17
… name collision

The 000504 backfill previously raised an exception when a live ai_providers
row already used an 'agents-*' name. That scenario should not occur since
no other process writes to ai_providers before this migration, but the
RAISE EXCEPTION path left no recovery option.

The backfill now soft-deletes any conflicting live row (freeing the name
under the partial unique index) and inserts the chat_providers-derived
row as authoritative. Adds a migration test covering the override path.
@ibetitsmike
ibetitsmike force-pushed the mike/ai-providers/schema-expand branch from 9422812 to 2f67dd7 Compare May 21, 2026 22:49
@ibetitsmike
ibetitsmike merged commit 40878ee into main May 22, 2026
26 checks passed
@ibetitsmike
ibetitsmike deleted the mike/ai-providers/schema-expand branch May 22, 2026 00:16
@github-actions github-actions Bot locked and limited conversation to collaborators May 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.

3 participants