fix(router): keep decision calls out of upstream accounting

This commit is contained in:
2026-11-25 13:43:46 +00:00
parent 73fc53a29b
commit d603780560
2 changed files with 41 additions and 1 deletions
+8 -1
View File
@@ -366,7 +366,7 @@ func (g *Gateway) callDecisionModel(ctx context.Context, rt *smartRouter, state
if body == nil {
return 0, 0, decisionError
}
callCtx, cancel := context.WithTimeout(ctx, cfg.timeout)
callCtx, cancel := context.WithTimeout(withoutUpstreamAccounting(ctx), cfg.timeout)
defer cancel()
probe := &http.Request{Header: http.Header{}}
oa.injectAuth(probe)
@@ -399,3 +399,10 @@ func decisionFailure(parent, call context.Context) string {
}
return decisionError
}
// withoutUpstreamAccounting hides the request's recorders so the decision call is not billed to the user's request.
func withoutUpstreamAccounting(ctx context.Context) context.Context {
ctx = context.WithValue(ctx, attemptRecorderCtxKey{}, nil)
ctx = context.WithValue(ctx, latencyRecorderCtxKey{}, nil)
return context.WithValue(ctx, firstByteCtxKey{}, nil)
}
+33
View File
@@ -446,3 +446,36 @@ func TestRouterDecisionOutcomeNames(t *testing.T) {
t.Fatalf("got %d outcomes", len(seen))
}
}
func TestRouterDecisionIgnoresRequestRecorders(t *testing.T) {
f := newDecisionFixture(t, 3, "", okHandler)
var attempts, latencies int64
ctx := withAttemptRecorder(context.Background(), func(*http.Response, error) { atomic.AddInt64(&attempts, 1) })
ctx = withLatencyRecorder(ctx, func(latencySample) { atomic.AddInt64(&latencies, 1) })
if a := f.g.askDecisionModel(ctx, f.rt, "s"); a.outcome != "" {
t.Fatalf("outcome %q", a.outcome)
}
if n := atomic.LoadInt64(&attempts); n != 0 {
t.Fatalf("attempt recorder called %d times", n)
}
if n := atomic.LoadInt64(&latencies); n != 0 {
t.Fatalf("latency recorder called %d times", n)
}
}
func TestRouterDecisionNonOpenAIProviderIsError(t *testing.T) {
f := newDecisionFixture(t, 3, "", okHandler)
f.g.adapters["gx10"] = &fakeAdapter{}
if a := f.g.askDecisionModel(context.Background(), f.rt, "s"); a.outcome != decisionError {
t.Fatalf("outcome %q", a.outcome)
}
}
func TestRouterDecisionParentCancelIsError(t *testing.T) {
f := newDecisionFixture(t, 3, "", okHandler)
ctx, cancel := context.WithCancel(context.Background())
cancel()
if a := f.g.askDecisionModel(ctx, f.rt, "s"); a.outcome != decisionError {
t.Fatalf("outcome %q", a.outcome)
}
}