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.
This commit is contained in:
2026-11-25 14:15:20 +00:00
parent 8c01f15ee5
commit 2cd828c162
4 changed files with 53 additions and 2 deletions
+2 -1
View File
@@ -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
+3
View File
@@ -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}
}
+14 -1
View File
@@ -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)
}
+34
View File
@@ -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)