Skip to content

chore: move to be shared gateway types and helpers - #29718

Merged
pawbana merged 4 commits into
mainfrom
pawel/aigov-615-reverse-proxy-handler-with-recorder-lifecycle-and-key-1
Sep 28, 2026
Merged

pawbana merged 4 commits into
mainfrom
pawel/aigov-615-reverse-proxy-handler-with-recorder-lifecycle-and-key-1

Conversation

@pawbana

@pawbana pawbana commented Sep 22, 2026 •

Copy link
Copy Markdown
Collaborator

Moves shared client identification, credential handling, header processing, and error categorization into reusable packages for AI Gateway handlers. Existing callers use the destination packages directly; runtime behavior and serialized values are unchanged.

Code moves

What moved From To
Client, all Client* constants, and GuessClient aibridge/client.go aibridge/client/client.go; the type is now client.Type and constants drop the Client prefix, e.g. client.ClaudeCode
GuessSessionID, cleanRef, and the Claude session-ID regex aibridge/session.go aibridge/client/session.go
isWebSocketUpgrade aibridge/bridge.go aibridge/headers/headers.go, exported as IsWebSocketUpgrade
extractAgentFirewallHeaders aibridge/bridge.go aibridge/headers/headers.go, exported as ExtractAgentFirewallHeaders
Credential, BYOK, AWSSigV4, CentralizedPool, their methods, AsBYOK, AsCentralizedPool, and WithCredentialInfo aibridge/intercept/credential.go aibridge/credential/credential.go
CredentialKind and centralized/BYOK constants aibridge/intercept/credential.go aibridge/credential/credential.go, renamed Kind, KindCentralized, and KindBYOK
AuthHeaderXAPIKey and AuthHeaderAuthorization aibridge/intercept/credential.go aibridge/headers/headers.go
ExtractBearerToken aibridge/utils/auth.go aibridge/headers/headers.go
hintFailoverKey and hintAWSChainKey aibridge/intercept/credential.go aibridge/credential/credential.go, exported as HintFailoverKey and HintAWSChainKey
categorizeInterceptionError, errorCategorizer, and the error-message size limit aibridge/interception_error.go aibridge/interceptionerror/error.go, renamed Categorize and ErrorCategorizer
Inline streaming http.Transport configuration aibridge/passthrough.go aibridge/utils/http.go, extracted as NewStreamingTransport
Actor header prefix, ActorIDHeader, ActorMetadataHeader, IsActorHeader, and headersFromActor aibridge/intercept/actor_headers.go aibridge/headers/headers.go; the prefix is exported as ActorHeaderPrefix
PrepareClientHeaders, BuildUpstreamHeaders, and the stripped-header lists aibridge/intercept/client_headers.go aibridge/headers/headers.go
Inline breaker state-change logging/metrics callback aibridge/bridge.go aibridge/circuitbreaker/circuitbreaker.go, inside NewProviderCircuitBreakers

Test moves

From To
aibridge/client_test.go aibridge/client/client_test.go
aibridge/session_test.go aibridge/client/session_test.go
WebSocket and Agent Firewall tests in aibridge/bridge_internal_test.go aibridge/headers/headers_test.go
aibridge/intercept/credential_test.go aibridge/credential/credential_test.go
aibridge/interception_error_internal_test.go aibridge/interceptionerror/error_test.go; test names now match Categorize
aibridge/intercept/actor_headers_internal_test.go aibridge/headers/headers_internal_test.go
aibridge/intercept/client_headers_test.go aibridge/headers/headers_test.go
aibridge/utils/auth_test.go aibridge/headers/headers_test.go

Associated test helpers moved with their tests. Moved cases and assertions are preserved. Added coverage checks public actor-header helpers and the extracted streaming transport configuration.

Related adjustments and review decisions
  • Consolidated header helpers into one production file, one public test file, and one internal test file, with constants and variables before functions.
  • NewProviderCircuitBreakers takes a logger instead of a callback and installs the existing logging/metrics callback internally. There is no exported callback-taking constructor. Tests use the production constructor; the state-change test now checks the open-state gauge and trip counter with provider/endpoint/model labels rather than capturing callback arguments.
  • BuildUpstreamHeaders takes SendActorHeaders as a bool instead of intercept.Config, so aibridge/headers does not import intercept.
  • InterceptionRecord.CredentialKind uses credential.Kind instead of string. Conversion happens at DRPC/logging boundaries.
  • Updated imports, callers, and tests to use the destination packages directly, including the Bedrock code brought in by the rebase. The old source files are removed rather than retained as compatibility wrappers.
  • Updated a SQL comment's symbol reference and regenerated its Go copy. No SQL logic changed.

Refs AIGOV-615.

Generated by Coder Agents on behalf of @pawbana.

@linear-code

linear-code Bot commented Sep 22, 2026

Copy link
Copy Markdown

AIGOV-615

@pawbana pawbana removed their assignment Sep 22, 2026
@pawbana
pawbana force-pushed the pawel/aigov-615-reverse-proxy-handler-with-recorder-lifecycle-and-key-1 branch 5 times, most recently from bf128bd to 4d80be1 Compare September 22, 2026 15:54
@pawbana pawbana changed the title refactor(aibridge): move shared gateway metadata and helpers chore: move shared gateway metadata and helpers Sep 22, 2026
@pawbana pawbana changed the title chore: move shared gateway metadata and helpers chore: move shared gateway types and helpers Sep 22, 2026
@pawbana pawbana changed the title chore: move shared gateway types and helpers chore: move to be shared gateway types and helpers Sep 22, 2026
@pawbana
pawbana marked this pull request as ready for review September 22, 2026 16:14
@pawbana
pawbana added this pull request to stack #29736 September 22, 2026 16:14
@pawbana
pawbana force-pushed the pawel/aigov-615-reverse-proxy-handler-with-recorder-lifecycle-and-key-1 branch from d102580 to c48c2bb Compare September 24, 2026 11:53
@pawbana
pawbana requested a review from johnstcn September 24, 2026 12:19

@johnstcn johnstcn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Refactor LGTM, just want to point out potential conflict with #29839 and stack

// onChange is called when circuit state changes.
// metrics is used to record circuit breaker reject counts (can be nil).
func NewProviderCircuitBreakers(provider string, cfg *config.CircuitBreaker, onChange func(endpoint, model string, from, to gobreaker.State), m *metrics.Metrics) *ProviderCircuitBreakers {
func NewProviderCircuitBreakersWithCallback(provider string, cfg *config.CircuitBreaker, onChange func(endpoint, model string, from, to gobreaker.State), m *metrics.Metrics) *ProviderCircuitBreakers {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks like this exported function is only used in tests?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yes IIRC the NewProviderCircuitBreakersWithCallback is test only. I could leave constructor as it was, where callback was created by caller but the production code always sets the same callback so it made sense to me to move it into the constructor.
Other option would be to export some DefaultCallback function and keep old function signature.

@pawbana
pawbana removed this pull request from stack #29736 September 24, 2026 17:36

@ssncferreira ssncferreira left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Overall LGTM, I have a few questions and suggestions, but nothing blocking. Maybe some are already addressed in upstack PRs.

Comment thread aibridge/client/request.go Outdated
@@ -0,0 +1,61 @@
package client

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Would it make more sense to call this file headers.go, given that the helpers are mostly on headers? And maybe moving some of the client_headers.go logic to here (except what is needed for interception)? I assume some of that logic will be needed for the reverse proxy. Would that make sense?

Comment thread aibridge/client/session_test.go Outdated
{
name: "claude_code_header_takes_precedence",
client: aibridge.ClientClaudeCode,
client: client.ClientClaudeCode,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: could we rename these consts to look like client.ClaudeCode? Also, client.Client from above reads off 😕

Comment thread aibridge/intercept/credential.go Outdated
"context"

"cdr.dev/slog/v3"
"github.com/coder/coder/v2/aibridge/credential"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Couldn't we move all this logic to the new aibridge/credential/credential.go?

t.Parallel()

gotType, gotMsg := categorizeInterceptionError(tc.cat, tc.err)
gotType, gotMsg := interceptionerror.Categorize(tc.cat, tc.err)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: should we update the name of the test to match the new function name?

Comment thread aibridge/utils/http.go Outdated
"time"
)

const (

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Does this actor logic belong here? 🤔 Shouldn't it be in the actor_headers.go file?

@pawbana
pawbana force-pushed the pawel/aigov-615-reverse-proxy-handler-with-recorder-lifecycle-and-key-1 branch from c48c2bb to bdf0943 Compare September 25, 2026 20:50
Move the actor header prefix, name builders, matcher, and actor-to-header
mapping out of utils and intercept into a dedicated headers package so
request handlers can share them without depending on intercept.
@pawbana
pawbana force-pushed the pawel/aigov-615-reverse-proxy-handler-with-recorder-lifecycle-and-key-1 branch 2 times, most recently from 7cec86a to 8e5c7c5 Compare September 28, 2026 09:32
@pawbana
pawbana enabled auto-merge (squash) September 28, 2026 09:33
@pawbana
pawbana disabled auto-merge September 28, 2026 09:35
@pawbana
pawbana force-pushed the pawel/aigov-615-reverse-proxy-handler-with-recorder-lifecycle-and-key-1 branch from 8e5c7c5 to 18de0b0 Compare September 28, 2026 09:37
@pawbana
pawbana enabled auto-merge (squash) September 28, 2026 09:46
@pawbana
pawbana disabled auto-merge September 28, 2026 09:46
@pawbana
pawbana enabled auto-merge (squash) September 28, 2026 09:47
@pawbana
pawbana merged commit 382a876 into main Sep 28, 2026
32 of 33 checks passed
@pawbana
pawbana deleted the pawel/aigov-615-reverse-proxy-handler-with-recorder-lifecycle-and-key-1 branch September 28, 2026 09:50
@github-actions github-actions Bot locked and limited conversation to collaborators Sep 28, 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