From 2cd828c162a09cce0d98716aafb30ff881b87dfe Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Wed, 25 Nov 2026 14:15:20 +0000 Subject: [PATCH] fix(router): decision call skips settled requests and ignores client cancels Live mode no longer calls the decision model when a floor or keyword rule already fixes the tier. A client cancel during the call no longer counts toward the breaker. --- smart_router_classify.go | 3 ++- smart_router_decision.go | 3 +++ smart_router_decision_assist_test.go | 15 +++++++++++- smart_router_decision_test.go | 34 ++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 2 deletions(-) diff --git a/smart_router_classify.go b/smart_router_classify.go index 3673853..8a83ce7 100644 --- a/smart_router_classify.go +++ b/smart_router_classify.go @@ -563,7 +563,8 @@ func classifySmartV2With(rt *smartRouter, req map[string]any, endpoint string, f rank, reason, next = smartSessionRoute(&rt.session, top, f, sc.sess, verdict, sc.now, askHash) out.hardRank = smartMaxInt(out.hardRank, next.pin) } - if assist != nil && reason == smartReasonScore && f.found && smartNearBoundary(rt, verdict.score) { + if assist != nil && reason == smartReasonScore && f.found && smartNearBoundary(rt, verdict.score) && + (!rt.decision.live || smartMaxInt(floorRank, kwRank) < smartMinInt(rank+1, top)) { ans := assist(f) newRank, outcome := applyDecision(rt, rank, ans) out.heuristicRank, out.decisionOutcome = rank, outcome diff --git a/smart_router_decision.go b/smart_router_decision.go index 85a1c8c..c1788d4 100644 --- a/smart_router_decision.go +++ b/smart_router_decision.go @@ -347,6 +347,9 @@ func (g *Gateway) askDecisionModel(ctx context.Context, rt *smartRouter, state s } rank, prob, outcome := g.callDecisionModel(ctx, rt, state) if outcome != "" { + if ctx.Err() != nil { + return decisionAnswer{outcome: decisionError} + } cfg.dec.record(false) return decisionAnswer{outcome: outcome} } diff --git a/smart_router_decision_assist_test.go b/smart_router_decision_assist_test.go index 3800d50..a521349 100644 --- a/smart_router_decision_assist_test.go +++ b/smart_router_decision_assist_test.go @@ -137,12 +137,25 @@ func TestClassifyDecisionSessionPinnedSkips(t *testing.T) { assert.Equal(t, -1, cls.decisionTier) } -func TestClassifyDecisionFloorStillRaises(t *testing.T) { +func TestClassifyDecisionLiveSkipsSettledRequest(t *testing.T) { rt := assistRouter(t, 1000, true, false) req := userReq(assistAsk) req["reasoning_effort"] = "high" stub := &stubAssist{ans: decisionAnswer{rank: 0, prob: 990}} cls := classifySmartWith(rt, req, cacheEndpointChat, nil, nil, stub.fn) + assert.Equal(t, 0, stub.calls) + assert.Equal(t, 2, cls.rank) + assert.Equal(t, smartReasonFloor, cls.reason) + assert.Equal(t, "", cls.decisionOutcome) +} + +func TestClassifyDecisionShadowCallsEvenWhenSettled(t *testing.T) { + rt := assistRouter(t, 1000, false, false) + req := userReq(assistAsk) + req["reasoning_effort"] = "high" + stub := &stubAssist{ans: decisionAnswer{rank: 0, prob: 990}} + cls := classifySmartWith(rt, req, cacheEndpointChat, nil, nil, stub.fn) + assert.Equal(t, 1, stub.calls) assert.Equal(t, 2, cls.rank) assert.Equal(t, smartReasonFloor, cls.reason) } diff --git a/smart_router_decision_test.go b/smart_router_decision_test.go index 588f605..e1ec587 100644 --- a/smart_router_decision_test.go +++ b/smart_router_decision_test.go @@ -324,6 +324,40 @@ func TestRouterDecisionTimeout(t *testing.T) { } } +func TestRouterDecisionClientCancelSkipsBreaker(t *testing.T) { + started := make(chan struct{}, 8) + release := make(chan struct{}) + var slow atomic.Bool + slow.Store(true) + f := newDecisionFixture(t, 3, "2s", func(w http.ResponseWriter, r *http.Request) { + if slow.Load() { + started <- struct{}{} + select { + case <-r.Context().Done(): + case <-release: + } + return + } + okHandler(w, r) + }) + t.Cleanup(func() { close(release) }) + for i := 0; i < 3; i++ { + ctx, cancel := context.WithCancel(context.Background()) + go func() { + <-started + cancel() + }() + if a := f.g.askDecisionModel(ctx, f.rt, "c"+strconv.Itoa(i)); a.outcome != decisionError { + t.Fatalf("cancel %d outcome %q", i, a.outcome) + } + } + slow.Store(false) + before := f.hitCount() + if a := f.g.askDecisionModel(context.Background(), f.rt, "after"); a.outcome != "" || f.hitCount() != before+1 { + t.Fatalf("breaker opened by client cancels: outcome %q", a.outcome) + } +} + func TestRouterDecisionBreaker(t *testing.T) { var fail atomic.Bool fail.Store(true)