Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 20 additions & 77 deletions aibridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,18 @@ import (
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"sync"
"time"

"github.com/google/uuid"
"github.com/hashicorp/go-multierror"
"github.com/sony/gobreaker/v2"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
"golang.org/x/net/http/httpguts"
"golang.org/x/xerrors"

"cdr.dev/slog/v3"
"github.com/coder/coder/v2/aibridge/circuitbreaker"
"github.com/coder/coder/v2/aibridge/clientmeta"
aibcontext "github.com/coder/coder/v2/aibridge/context"
"github.com/coder/coder/v2/aibridge/intercept"
"github.com/coder/coder/v2/aibridge/mcp"
Expand All @@ -29,7 +26,6 @@ import (
"github.com/coder/coder/v2/aibridge/recorder"
"github.com/coder/coder/v2/aibridge/routing"
"github.com/coder/coder/v2/aibridge/tracing"
agplaibridge "github.com/coder/coder/v2/coderd/aibridge"
"github.com/coder/quartz"
)

Expand Down Expand Up @@ -79,39 +75,26 @@ func NewRequestBridge(ctx context.Context, providers []provider.Provider, rec re
continue
}

// Create per-provider circuit breaker if configured
cfg := prov.CircuitBreakerConfig()
providerName := prov.Name()
onChange := func(endpoint, model string, from, to gobreaker.State) {
logger.Info(context.Background(), "circuit breaker state change",
slog.F("provider", providerName),
slog.F("endpoint", endpoint),
slog.F("model", model),
slog.F("from", from.String()),
slog.F("to", to.String()),
)
if m != nil {
m.CircuitBreakerState.WithLabelValues(providerName, endpoint, model).Set(circuitbreaker.StateToGaugeValue(to))
if to == gobreaker.StateOpen {
m.CircuitBreakerTrips.WithLabelValues(providerName, endpoint, model).Inc()
}
}
}
cbs := circuitbreaker.NewProviderCircuitBreakers(providerName, cfg, onChange, m)
cbs := circuitbreaker.NewProviderCircuitBreakersWithObservability(
prov.Name(), prov.CircuitBreakerConfig(), logger, m,
)

// Add the known provider-specific routes which are bridged (i.e. intercepted and augmented).
for _, path := range prov.BridgedRoutes() {
handler := newInterceptionProcessor(prov, cbs, rec, mcpProxy, logger, m, tracer)
route, err := url.JoinPath(prov.RoutePrefix(), path)
if err != nil {
logger.Error(ctx, "failed to join path",
slog.Error(err),
slog.F("provider", providerName),
slog.F("provider", prov.Name()),
slog.F("prefix", prov.RoutePrefix()),
slog.F("path", path),
)
return nil, xerrors.Errorf("failed to configure provider '%v': failed to join bridged path: %w", providerName, err)
return nil, xerrors.Errorf("failed to configure provider '%v': failed to join bridged path: %w", prov.Name(), err)
}
handler := newInterceptionProcessor(
prov, cbs, rec, mcpProxy, logger, m, tracer,
strings.TrimPrefix(route, "/"+prov.Name()),
)
mux.Handle(route, handler)
}

Expand All @@ -125,11 +108,11 @@ func NewRequestBridge(ctx context.Context, providers []provider.Provider, rec re
if err != nil {
logger.Error(ctx, "failed to join path",
slog.Error(err),
slog.F("provider", providerName),
slog.F("provider", prov.Name()),
slog.F("prefix", prov.RoutePrefix()),
slog.F("path", path),
)
return nil, xerrors.Errorf("failed to configure provider '%v': failed to join passed through path: %w", providerName, err)
return nil, xerrors.Errorf("failed to configure provider '%v': failed to join passed through path: %w", prov.Name(), err)
}
mux.Handle(route, http.StripPrefix(prov.RoutePrefix(), ftr))
}
Expand All @@ -156,7 +139,7 @@ func WithClock(clock quartz.Clock) RequestBridgeOption {
// newInterceptionProcessor returns an [http.HandlerFunc] which is capable of creating a new interceptor and processing a given request
// using [Provider] p, recording all usage events using [Recorder] rec.
// If cbs is non-nil, circuit breaker protection is applied per endpoint/model tuple.
func newInterceptionProcessor(p provider.Provider, cbs *circuitbreaker.ProviderCircuitBreakers, rec recorder.Recorder, mcpProxy mcp.ServerProxier, logger slog.Logger, m *metrics.Metrics, tracer trace.Tracer) http.HandlerFunc {
func newInterceptionProcessor(p provider.Provider, cbs *circuitbreaker.ProviderCircuitBreakers, rec recorder.Recorder, mcpProxy mcp.ServerProxier, logger slog.Logger, m *metrics.Metrics, tracer trace.Tracer, route string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx, span := tracer.Start(r.Context(), "Intercept")
defer span.End()
Expand All @@ -167,7 +150,6 @@ func newInterceptionProcessor(p provider.Provider, cbs *circuitbreaker.ProviderC
sessionID := GuessSessionID(client, r)

if isWebSocketUpgrade(r) {
route := strings.TrimPrefix(r.URL.Path, fmt.Sprintf("/%s", p.Name()))
logger.Debug(ctx, "rejecting unsupported WebSocket upgrade",
slog.F("provider", p.Name()),
slog.F("route", route),
Expand Down Expand Up @@ -259,7 +241,6 @@ func newInterceptionProcessor(p provider.Provider, cbs *circuitbreaker.ProviderC
return
}

route := strings.TrimPrefix(r.URL.Path, fmt.Sprintf("/%s", p.Name()))
log := logger.With(
slog.F("route", route),
slog.F("provider", p.Name()),
Expand Down Expand Up @@ -288,13 +269,13 @@ func newInterceptionProcessor(p provider.Provider, cbs *circuitbreaker.ProviderC
errType, errMsg := categorizeInterceptionError(p, execErr)
if execErr != nil {
if m != nil {
m.InterceptionCount.WithLabelValues(p.Name(), interceptor.Model(), metrics.InterceptionCountStatusFailed, route, r.Method, actor.ID, string(client)).Add(1)
m.InterceptionCount.WithLabelValues(p.Name(), interceptor.Model(), metrics.InterceptionCountStatusFailed, route, routing.MetricMethod(r.Method), actor.ID, string(client)).Add(1)

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.

P2 [CRF-83] The CRF-35 fix bounded the method and route labels on this line and left interceptor.Model(), a raw client string, as a label on three metric families and half a never-evicted map key. (Killua P2, Mafu-san P2)

Re-raise gate: round 3 recorded this (Mafuuu, P4) and dropped it from posting because it predated the diff, with the note that round 4 may raise it if the diff touches it. Round 4 touches it. This commit rewrote this exact expression, turning r.Method into routing.MetricMethod(r.Method) on lines 272 and 278 and precomputing route at mux registration, and left interceptor.Model() alone in the same argument list. aibridge/bridge.go also now builds its breakers through this PR's new constructor, which makes it the shared construction path for both modes.

Model() is the raw model field of the request JSON (intercept/messages/reqpayload.go:114-116 is gjson.GetBytes(p, messagesReqPathModel).Str). No allowlist, no clamp. model is in baseLabels, so it is stamped on InterceptionCount, InterceptionsInflight, and the 7-bucket InterceptionDuration, roughly ten series per distinct string. aibridge/metrics/metrics.go:55 states the assumed bound out loud: "Pessimistic cardinality: N provider names, 5 models, ...". This commit made the methods and routes halves of that comment true and left the models half false. (Mafu-san)

Measured, not estimated: 100,000 unique model values retained 53,418,792 bytes, 534 bytes each, via runtime.ReadMemStats around a loop of Get calls on a real ProviderCircuitBreakers. The length term is worse than the count term: key is one copy of the model string and gobreaker.Settings.Name is a second, both retained for process lifetime, and nothing clamps model before the breaker sees it while the body cap is 32 MiB. (Killua)

I verified the retention path rather than the measurement: circuitbreaker.go:103 builds key := endpoint + ":" + model, stores it with LoadOrStore in a sync.Map, and there is no delete anywhere in the file. The breaker is also created before the upstream call, so a model the provider would reject still allocates a permanent entry. The metric half is unconditional wherever coderd supplies a registry; the breaker half needs a non-nil CircuitBreakerConfig().

This is the interception path serving production today, and any authenticated user can drive it. Fix: bound model the way MetricMethod bounds the method, with an allowlist of configured or known model identifiers and one other fallback. That closes the label and the breaker key together, and it is smaller than the change that introduced MetricMethod. Proxy mode is clean here; it passes the empty model, which is CRF-93.

🤖

}
span.SetStatus(codes.Error, fmt.Sprintf("interception failed: %v", execErr))
log.Warn(credCtx, "interception failed", slog.Error(execErr), slog.F("error_type", string(errType)))
} else {
if m != nil {
m.InterceptionCount.WithLabelValues(p.Name(), interceptor.Model(), metrics.InterceptionCountStatusCompleted, route, r.Method, actor.ID, string(client)).Add(1)
m.InterceptionCount.WithLabelValues(p.Name(), interceptor.Model(), metrics.InterceptionCountStatusCompleted, route, routing.MetricMethod(r.Method), actor.ID, string(client)).Add(1)
}
log.Debug(credCtx, "interception ended")
}
Expand All @@ -313,9 +294,7 @@ func newInterceptionProcessor(p provider.Provider, cbs *circuitbreaker.ProviderC

// isWebSocketUpgrade reports whether r is a WebSocket opening handshake.
func isWebSocketUpgrade(r *http.Request) bool {
return r.Method == http.MethodGet &&
httpguts.HeaderValuesContainsToken(r.Header.Values("Connection"), "upgrade") &&
httpguts.HeaderValuesContainsToken(r.Header.Values("Upgrade"), "websocket")
return clientmeta.IsWebSocketUpgrade(r)
}

// ServeHTTP exposes the internal http.Handler, which has all [Provider]s' routes registered.
Expand Down Expand Up @@ -347,44 +326,8 @@ func (b *RequestBridge) Shutdown(ctx context.Context) error {
return err
}

// extractAgentFirewallHeaders reads and parses the Agent Firewall
// correlation headers from the request. Both headers must be present
// together with a valid UUID session ID and a non-negative int32
// sequence number, or both must be absent. Partial or malformed headers
// return an error so the caller can reject the request (fail closed).
func extractAgentFirewallHeaders(r *http.Request) (sessionID *string, seqNumber *int32, err error) {
rawSessionID := r.Header.Get(agplaibridge.HeaderAgentFirewallSessionID)
rawSeqNumber := r.Header.Get(agplaibridge.HeaderAgentFirewallSequenceNumber)

hasSessionID := rawSessionID != ""
hasSeqNumber := rawSeqNumber != ""

switch {
case !hasSessionID && !hasSeqNumber:
// Neither header present; request did not traverse Agent Firewall.
return nil, nil, nil
case hasSessionID && !hasSeqNumber:
return nil, nil, xerrors.Errorf("agent firewall session ID header present without sequence number")
case !hasSessionID && hasSeqNumber:
return nil, nil, xerrors.Errorf("agent firewall sequence number header present without session ID")
}

// Both headers present; validate the session ID is a UUID. Storing an
// invalid value would silently drop the firewall correlation to NULL
// downstream, so reject it here instead.
if _, parseErr := uuid.Parse(rawSessionID); parseErr != nil {
return nil, nil, xerrors.Errorf("invalid agent firewall session ID %q: %w", rawSessionID, parseErr)
}

// Parse the sequence number.
n, err := strconv.ParseInt(rawSeqNumber, 10, 32)
if err != nil {
return nil, nil, xerrors.Errorf("invalid agent firewall sequence number %q: %w", rawSeqNumber, err)
}
if n < 0 {
return nil, nil, xerrors.Errorf("invalid agent firewall sequence number %q: must be non-negative", rawSeqNumber)
}

n32 := int32(n)
return &rawSessionID, &n32, nil
// extractAgentFirewallHeaders returns validated Agent Firewall correlation

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.

P3 [CRF-109] Two unexported single-call wrappers over clientmeta, each with a doc comment restating the callee's, and no compatibility argument available for either. (Zoro)

isWebSocketUpgrade (bridge.go:296) returns clientmeta.IsWebSocketUpgrade(r) and its only caller is bridge.go:152. extractAgentFirewallHeaders (this line) returns clientmeta.ExtractAgentFirewallHeaders(r) and its only caller is bridge.go:168. grep -rn finds no other reference except a comment in agent_firewall_internal_test.go:90 naming the old function in prose. (Zoro)

These are unexported, so unlike the Client* aliases the panel closed 6/6 in CRF-24, they are invisible to external importers and the compatibility defense does not reach them. What is left is a facade that makes the interception path read as if aibridge still owns preflight validation.

Gon reached the second one from the comment side and found the related defect: "returns validated Agent Firewall correlation metadata without exposing raw malformed values in errors" is a claim about the callee's invariant, which is implemented and documented in clientmeta/request.go:43-59, parked in a file that cannot enforce it. Gon rated that one below the comment-audit default on the grounds that the padding may not cost a reader anything, which is fair; the wrapper's existence is the more substantive half.

Verified: call clientmeta.IsWebSocketUpgrade(r) at :152 and clientmeta.ExtractAgentFirewallHeaders(r) at :168, delete both functions. The package already imports clientmeta. go build ./aibridge/... passes and go test ./aibridge/ is green. (Zoro)

If isWebSocketUpgrade is removed, CRF-110 is the better fix for its call site than a straight substitution.

🤖

// metadata without exposing raw malformed values in errors.
func extractAgentFirewallHeaders(r *http.Request) (*string, *int32, error) {
return clientmeta.ExtractAgentFirewallHeaders(r)
}
151 changes: 0 additions & 151 deletions aibridge/bridge_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

"cdr.dev/slog/v3/sloggers/slogtest"
"github.com/coder/coder/v2/aibridge/mcpmock"
agplaibridge "github.com/coder/coder/v2/coderd/aibridge"
"github.com/coder/coder/v2/testutil"
)

Expand All @@ -24,156 +23,6 @@ func (b *RequestBridge) InflightRequests() int {
return b.inflight.active
}

func TestIsWebSocketUpgrade(t *testing.T) {
t.Parallel()

tests := []struct {
name string
method string
connection string
upgrade string
want bool
}{
{name: "websocket upgrade", method: http.MethodGet, connection: "keep-alive, Upgrade", upgrade: "WebSocket", want: true},
{name: "non-GET request", method: http.MethodPost, connection: "Upgrade", upgrade: "websocket", want: false},
{name: "missing connection upgrade", method: http.MethodGet, connection: "keep-alive", upgrade: "websocket", want: false},
{name: "different upgrade protocol", method: http.MethodGet, connection: "Upgrade", upgrade: "h2c", want: false},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

req, err := http.NewRequestWithContext(t.Context(), tc.method, "/", nil)
require.NoError(t, err)
req.Header.Set("Connection", tc.connection)
req.Header.Set("Upgrade", tc.upgrade)

assert.Equal(t, tc.want, isWebSocketUpgrade(req))
})
}
}

func TestExtractAgentFirewallHeaders(t *testing.T) {
t.Parallel()

const validSessionID = "e5f6a7b8-1234-5678-9abc-def012345678"

ptr := func(s string) *string { return &s }

cases := []struct {
name string
// sessionID and seqNumber set the corresponding headers when
// non-nil. A nil value leaves the header unset.
sessionID *string
seqNumber *string

wantErr bool
errContains string
wantSession *string
wantSeq *int32
}{
{
name: "both headers present",
sessionID: ptr(validSessionID),
seqNumber: ptr("42"),
wantSession: ptr(validSessionID),
wantSeq: int32Ptr(42),
},
{
name: "no headers present",
},
{
name: "only session ID returns error",
sessionID: ptr(validSessionID),
wantErr: true,
errContains: "without sequence number",
},
{
name: "only sequence number returns error",
seqNumber: ptr("7"),
wantErr: true,
errContains: "without session ID",
},
{
name: "sequence number zero",
sessionID: ptr(validSessionID),
seqNumber: ptr("0"),
wantSession: ptr(validSessionID),
wantSeq: int32Ptr(0),
},
{
name: "invalid session ID returns error",
sessionID: ptr("not-a-uuid"),
seqNumber: ptr("42"),
wantErr: true,
errContains: "invalid agent firewall session ID",
},
{
name: "invalid sequence number returns error",
sessionID: ptr(validSessionID),
seqNumber: ptr("not-a-number"),
wantErr: true,
errContains: "invalid agent firewall sequence number",
},
{
name: "negative sequence number returns error",
sessionID: ptr(validSessionID),
seqNumber: ptr("-1"),
wantErr: true,
errContains: "must be non-negative",
},
{
name: "sequence number exceeding int32 range returns error",
sessionID: ptr(validSessionID),
seqNumber: ptr("2147483648"), // max int32 + 1
wantErr: true,
errContains: "invalid agent firewall sequence number",
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

req, err := http.NewRequestWithContext(t.Context(), http.MethodPost, "/", nil)
require.NoError(t, err)
if tc.sessionID != nil {
req.Header.Set(agplaibridge.HeaderAgentFirewallSessionID, *tc.sessionID)
}
if tc.seqNumber != nil {
req.Header.Set(agplaibridge.HeaderAgentFirewallSequenceNumber, *tc.seqNumber)
}

sessionID, seqNumber, extractErr := extractAgentFirewallHeaders(req)

if tc.wantErr {
require.Error(t, extractErr)
assert.Contains(t, extractErr.Error(), tc.errContains)
assert.Nil(t, sessionID)
assert.Nil(t, seqNumber)
return
}

require.NoError(t, extractErr)
if tc.wantSession == nil {
assert.Nil(t, sessionID)
} else {
require.NotNil(t, sessionID)
assert.Equal(t, *tc.wantSession, *sessionID)
}
if tc.wantSeq == nil {
assert.Nil(t, seqNumber)
} else {
require.NotNil(t, seqNumber)
assert.Equal(t, *tc.wantSeq, *seqNumber)
}
})
}
}

func int32Ptr(n int32) *int32 { return &n }

// TestRequestBridgeShutdownDoesNotWaitForCanceledHandler verifies shutdown
// returns even when a canceled handler remains blocked.
func TestRequestBridgeShutdownDoesNotWaitForCanceledHandler(t *testing.T) {
Expand Down
Loading
Loading