Skip to content

feat: route chatd provider traffic through aibridge#25629

Draft
ibetitsmike wants to merge 15 commits into
mainfrom
mike/chatd-aibridge-9rwr
Draft

feat: route chatd provider traffic through aibridge#25629
ibetitsmike wants to merge 15 commits into
mainfrom
mike/chatd-aibridge-9rwr

Conversation

@ibetitsmike
Copy link
Copy Markdown
Collaborator

@ibetitsmike ibetitsmike commented May 22, 2026

Routes chatd model calls backed by concrete AI Provider rows through the in-process aibridge transport by default, with a deployment option to temporarily use direct provider routing. This includes selected chat models, advisor overrides, title and quickgen paths, subagent overrides, and computer use model selection.

Stores the initiating API key ID on chat turns and propagates it through request context for aibridge delegated auth, while preserving BYOK provider headers. Adds the chat turn API key migration and generated config, API, and CLI docs. Pending chats without API key metadata may need retrying or temporary direct routing.

Depends on #25625.

Mux working on Mike's behalf.

@github-actions
Copy link
Copy Markdown

Docs preview

📖 View docs preview for docs/reference/api/general.md

@ibetitsmike
Copy link
Copy Markdown
Collaborator Author

/coder-agents-review

@coder-agents-review
Copy link
Copy Markdown

coder-agents-review Bot commented May 22, 2026

Review posted | Chat
Requested: 2026-05-22 20:31 UTC by @ibetitsmike

Review history
  • R1 (2026-05-22), 1 P1, COMMENT. Review
  • R4 (2026-05-22): 4 reviewers, 2 Nit, 3 P1, 2 P2, 14 P3, APPROVE. Review
  • R6 (2026-05-23): 3 reviewers, 2 Nit, 3 P1, 2 P2, 15 P3, APPROVE. Review
  • R7 (2026-05-23): 3 reviewers, 2 Nit, 3 P1, 2 P2, 15 P3, APPROVE. Review
  • R8 (2026-05-23): 3 reviewers, 2 Nit, 3 P1, 2 P2, 15 P3, APPROVE. Review

deep-review v0.5.0 | Round 8 | a896227..a7bc8e5

Last posted: Round 8, 22 findings (3 P1, 2 P2, 15 P3, 2 Nit), APPROVE. Review

Finding inventory

Findings

# Sev Status Location Summary Round Reviewer Posted
CRF-1 P1 Author fixed (62c8785) computer_use.go:95 Computer use passes empty modelRoute to newModelFromConfig, fails when AI Gateway routing enabled R1 Netero Yes
CRF-2 P1 Author fixed (c6395ce) aibridge_routing.go:255 Synthetic AIProvider drops BaseUrl, breaking all custom-endpoint providers and causing 5+ test failures R2 Zoro P2, Bisky P1, Meruem P1 Yes
CRF-3 P1 Author fixed (c6395ce) chatd.go:2173 EditMessage creates user message without API key ID, breaking AI Gateway routing for edited messages R2 Pariston Yes
CRF-4 P3 Author fixed (c6395ce) quickgen.go:197 shortTextCandidate.route never populated; quickgen debug recording silently disabled under aibridge routing R2 Hisoka P2, Chopper P3, Kite P3, Knov P3, Mafuuu P3, Ryosuke P3 Yes
CRF-5 P2 Author fixed (c6395ce) chatd.go:531 Tests exercise dead non-strict wrappers instead of production strict variants; strict error promotion has zero test coverage R2 Meruem P2, Zoro P4 Yes
CRF-6 P3 Author fixed (c6395ce) codersdk/deployment.go:4053 User-facing description uses internal process name "chatd" R2 Leorio Yes
CRF-7 P3 Author fixed (c6395ce) aibridge_routing.go:131 formatProvider naming misleads; actually the fantasy SDK provider name R2 Gon Yes
CRF-8 P3 Author fixed (c6395ce) aibridge_routing.go:140 "coder-aibridge" magic placeholder API key without named constant or explanation R2 Gon Yes
CRF-9 P3 Author fixed (c6395ce) chatd.go:344 Strict suffix inverts Go naming conventions; error-returning variant is the primary production method R2 Gon Yes
CRF-10 P3 Author fixed (c6395ce) aibridge_routing.go:145 aibridgeModelProviderInput unconditionally overwrites top-level API key fields from ByProvider, which can be empty R2 Hisoka P3, Razor P3 Yes
CRF-11 P3 Author fixed (c6395ce) aibridge_routing.go:74 Pre-migration chats with NULL api_key_id fail under default AI Gateway routing R2 Hisoka P3, Kite P4, Kurapika P3 Yes
CRF-12 P3 Author fixed (c6395ce) chatd.go:3388 prepareManualTitleDebugRun suppresses valid debug model when modelRouteForConfig fails in non-aibridge mode R2 Kite Yes
CRF-13 P3 Author fixed (c6395ce) queries/chats.sql:468 GetLatestChatUserMessageAPIKeyID CTE misses compressed summary boundary index R2 Knuckle Yes
CRF-14 P3 Author fixed (c6395ce) queries/chats.sql:463 No SQL-level integration test for GetLatestChatUserMessageAPIKeyID R2 Knuckle Yes
CRF-15 P2 Author fixed (c6395ce) aibridge_routing.go:198 activeTurnAPIKeyIDFromMessages and subagent API key propagation have zero test coverage R2 Bisky Yes
CRF-16 Nit Author fixed (c6395ce) chatd.go:4167 AIBridgeRoutingEnabled vs AIGatewayRoutingEnabled naming inconsistency on adjacent surfaces R2 Gon, Leorio Yes
CRF-17 Nit Author fixed (c6395ce) aibridge_routing.go:35 Exported fields on unexported modelRoute type R2 Razor Yes
CRF-18 P3 Author fixed (f9bdd07) quickgen.go:72 preferredShortTextCandidates nil return under aibridge routing has no regression test R3 Bisky Yes
CRF-19 P3 Author fixed (f9bdd07) migrations/000507_chat_turn_api_key_id.up.sql:2 FK ON DELETE SET NULL can nullify post-migration api_key_id, extending CRF-11 scope R3 Hisoka Yes
CRF-20 P3 Author fixed (222eb40) chatd.go:438 resolveAdvisorModelOverrideOrFallback and newAdvisorRuntimeOrFallback are dead production code (test-only helpers) R4 Netero Yes
CRF-21 P3 Author fixed (222eb40) aibridge_routing.go:233 modelRouteForConfig error omits model config ID and model name R4 Chopper Yes
CRF-22 P3 Author fixed (9e3e54c) aibridge_routing_internal_test.go:125 GetLatestChatUserMessageAPIKeyID SQL test only covers compressed-summary-present branch R5 Bisky Yes

Contested and acknowledged

(none)

Round log

Round 1

Netero-only. 1 P1. Reviewed against a896227..6bd8e03.

Round 2

Panel (19 reviewers). CRF-1 addressed. 2 P1, 2 P2, 8 P3, 2 Nit new. Reviewed against a896227..06d1d1f.

Round 3

Panel (7 reviewers). CRF-2 through CRF-17 addressed. 2 P3 new. Reviewed against a896227..c6395ce.

Round 4

Panel (4 reviewers). CRF-18, CRF-19 addressed. 2 P3 new. Reviewed against a896227..f9bdd07.

Round 5

Panel (3 reviewers). CRF-20, CRF-21 addressed. 1 P3 new. Reviewed against a896227..222eb40.

Round 6

Panel (3 reviewers). CRF-22 addressed. No new findings. Reviewed against a896227..9e3e54c.

Round 7

Panel (3 reviewers). No open findings, no new findings. Reviewed against a896227..ba57518.

Round 8

Panel (3 reviewers). No open findings, no new findings. Reviewed against a896227..a7bc8e5.

About deep-review

CRF = Coder Review Finding (P0-P4, Nit, Note)

Reviewer Focus
Bisky tests
Chopper ops/errors
Churn-guard change verification
Ging language modernization
Gon naming
Hisoka edge cases
Killua perf
Kite change integrity
Knov contracts
Knuckle SQL
Kurapika security
Leorio docs
Luffy product
Mafu-san process
Mafuuu contracts
Melody dispatch/pairing
Meruem structural
Nami frontend
Netero mechanical checks
Pariston premise testing
Pen-botter product gaps
Razor verification
Robin duplication
Ryosuke Go arch
Takumi concurrency
Zoro shape

🤖 Managed by Coder Agents.

Copy link
Copy Markdown

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

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 first pass only; the full review panel has not yet reviewed this PR.

One P1: computer use model resolution fails when AI Gateway routing is enabled (the default).

This is a substantial routing refactor that introduces a clean aibridge transport layer, delegated API key propagation, and a well-tested migration. The error handling patterns and test coverage for the new routing code are solid. Addressing the computer use regression below will unblock the full panel review.

🤖 This review was automatically generated with Coder Agents.

Comment thread coderd/x/chatd/computer_use.go Outdated
@ibetitsmike
Copy link
Copy Markdown
Collaborator Author

/coder-agents-review

Copy link
Copy Markdown

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

Choose a reason for hiding this comment

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

Full panel review (19 reviewers). CRF-1 (R1 P1, computer use empty route) was addressed cleanly in 62c8785. Two new P1s, two P2s, eight P3s, two nits.

The centralized model construction through newModelFromConfig is a strong structural win: all model paths now route through a single choke point, making the aibridge routing decision explicit at every call site. The modelRoute value type, the migration, the API key context propagation chain, and the debug-aware model wrapping are well-designed. The test coverage for the new routing code (fail-closed subcases, context propagation, provider mapping) is thorough.

Two blockers: (1) the synthetic AIProvider at line 255 drops BaseUrl, breaking every custom-endpoint provider when the fallback flag CODER_CHAT_AI_GATEWAY_ROUTING_ENABLED=false is used, and silently confirmed by 5+ test failures; (2) EditMessage is the only user-message creation site that was not updated with .withAPIKeyID(), breaking AI Gateway routing for edited messages under default config.

"The old pattern's safety argument died when the preconditions changed." (Hisoka, on the BaseUrl loss)


coderd/x/chatd/chatd.go:2173

P1 [CRF-3] EditMessage creates a replacement user message with .withCreatedBy(opts.CreatedBy) but omits .withAPIKeyID(). Every other user-message creation site was updated: CreateChat (line 1732), insertUserMessageAndSetPending (line 4098), tryAutoPromoteQueuedMessage (line 5923), createChildSubagentChatWithOptions (subagent.go:1092). EditMessage is the only sibling missed.

With AI Gateway routing enabled (default), newModelFromConfig falls back to activeTurnAPIKeyID(ctx, chat.ID), which queries the latest user message's api_key_id. The edited message is the latest user message and its api_key_id is NULL, so the query returns empty, and newModelFromConfig returns "AI Gateway routing requires the active turn API key ID." The chat turn fails.

Also, EditMessageOptions (line 1559) lacks an APIKeyID field, and the API handler at coderd/exp_chats.go does not pass apiKey.ID.

(Pariston)

🤖

🤖 This review was automatically generated with Coder Agents.

Comment thread coderd/x/chatd/aibridge_routing.go Outdated
Comment thread coderd/x/chatd/quickgen.go
Comment thread coderd/x/chatd/chatd.go Outdated
Comment thread codersdk/deployment.go Outdated
Comment thread coderd/x/chatd/aibridge_routing.go Outdated
Comment thread coderd/database/queries/chats.sql
Comment thread coderd/database/queries/chats.sql
Comment thread coderd/x/chatd/aibridge_routing.go Outdated
Comment thread coderd/x/chatd/chatd.go Outdated
Comment thread coderd/x/chatd/aibridge_routing.go Outdated
@ibetitsmike ibetitsmike force-pushed the mike/chatd-aibridge-9rwr branch from 5e69d18 to c6395ce Compare May 22, 2026 22:42
@ibetitsmike
Copy link
Copy Markdown
Collaborator Author

/coder-agents-review

Copy link
Copy Markdown

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

Choose a reason for hiding this comment

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

Round 3 panel review (7 reviewers). All 16 open findings from R2 are addressed. Two new P3s.

The fixes are thorough. CRF-2 (BaseUrl drop) is resolved by adding baseURL to modelRoute via modelRouteFromAIProvider, with a test confirming the round-trip. CRF-3 (EditMessage missing API key) is wired end-to-end from the HTTP handler through to the DB. CRF-5 (strict wrapper naming) and CRF-9 (naming conventions) were consolidated into a clean rename swap. CRF-10 (key overwrite) now clones before mutation and conditionally copies. CRF-15 (test coverage) replaced activeTurnAPIKeyIDFromMessages with activeTurnAPIKeyIDFromStore and added five integration tests covering the subagent propagation paths.

Two minor items below. Neither blocks merge.

PS. This PR depends on #25625 for the aibridged handler-side delegated auth. Without it, the default-enabled aibridge routing sends the placeholder key to a handler that doesn't understand it. The deployment toggle mitigates, but verifying the merge ordering matters.

"Good stones." (Bisky, on the new test suite)

🤖 This review was automatically generated with Coder Agents.

Comment thread coderd/x/chatd/quickgen.go
Comment thread coderd/database/migrations/000507_chat_turn_api_key_id.up.sql
@ibetitsmike ibetitsmike force-pushed the mike/chatd-aibridge-9rwr branch from c6395ce to f9bdd07 Compare May 22, 2026 23:11
@ibetitsmike
Copy link
Copy Markdown
Collaborator Author

/coder-agents-review

Copy link
Copy Markdown

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

Choose a reason for hiding this comment

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

Round 4 panel review (4 reviewers). CRF-18 and CRF-19 addressed. Two new P3s below, neither blocking.

All 19 findings from the first three rounds have been addressed. The R2 blockers (BaseUrl loss, EditMessage missing API key) are properly fixed and tested. The routing centralization through newModelFromConfig, the API key propagation chain, the fail-closed error handling, and the test coverage are all solid. The migration, config plumbing, and naming are clean.

Two minor items: the OrFallback test helpers live in production code instead of a test file, and one error message could include the model config ID to speed up operator troubleshooting. Neither affects correctness.

PS. Merge ordering with #25625 remains important; without the handler-side delegated auth, the default-enabled routing sends a placeholder key that no handler validates.

"Genuine." (Bisky, on the aibridge test suite)

🤖 This review was automatically generated with Coder Agents.

Comment thread coderd/x/chatd/chatd.go Outdated
Comment thread coderd/x/chatd/aibridge_routing.go
@ibetitsmike ibetitsmike force-pushed the mike/chatd-aibridge-9rwr branch from f9bdd07 to 222eb40 Compare May 22, 2026 23:44
@ibetitsmike
Copy link
Copy Markdown
Collaborator Author

/coder-agents-review

Copy link
Copy Markdown

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

Choose a reason for hiding this comment

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

Round 5 panel review (3 reviewers). CRF-20 and CRF-21 addressed. One new P3 below, not blocking.

All 21 prior findings resolved. The test helper relocation (CRF-20) and error message improvement (CRF-21) are clean. One minor test coverage observation: the SQL integration test for GetLatestChatUserMessageAPIKeyID only exercises the compressed-summary-present branch. The no-summary branch is straightforward but untested.

The R4 APPROVE stands. 22 findings across 5 rounds, 21 resolved, 1 open P3.

🤖 This review was automatically generated with Coder Agents.

Comment thread coderd/x/chatd/aibridge_routing_internal_test.go
@ibetitsmike ibetitsmike force-pushed the mike/chatd-aibridge-9rwr branch from 222eb40 to 9e3e54c Compare May 23, 2026 00:13
@ibetitsmike
Copy link
Copy Markdown
Collaborator Author

/coder-agents-review

Copy link
Copy Markdown

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

Choose a reason for hiding this comment

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

Round 6 confirmation (3 reviewers). CRF-22 addressed (no-summary SQL test branch added). Netero and all three panel reviewers report no new findings.

All 22 findings across 6 rounds are resolved. Zero open items. Clean bill.

🤖 This review was automatically generated with Coder Agents.

@ibetitsmike
Copy link
Copy Markdown
Collaborator Author

/coder-agents-review

Copy link
Copy Markdown

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

Choose a reason for hiding this comment

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

Round 7 confirmation (3 reviewers). Zero open findings coming in, zero new findings from Netero or the panel. All 22 findings across 7 rounds remain resolved.

Clean bill. The prior APPROVEs (R4, R5, R6) stand.

🤖 This review was automatically generated with Coder Agents.

@ibetitsmike
Copy link
Copy Markdown
Collaborator Author

/coder-agents-review

Copy link
Copy Markdown

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

Choose a reason for hiding this comment

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

Round 8 confirmation (3 reviewers). Third consecutive clean round. Zero open findings, zero new findings from Netero or the panel. All 22 findings across 8 rounds remain resolved.

🤖 This review was automatically generated with Coder Agents.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant