fix(router): send the head of the system prompt and ask to the decision model

The decision state now holds the first 1024 bytes of the system prompt and the first 8192 bytes of the newest ask, cut at a character boundary. The classifier scoring inputs stay the same.
This commit is contained in:
2026-11-25 14:15:51 +00:00
parent 2cd828c162
commit 21e9bc84d1
5 changed files with 47 additions and 23 deletions
+2 -7
View File
@@ -266,16 +266,11 @@ type decisionResponse struct {
// decisionState is the text the decision model sees and the cache key source.
func decisionState(system, ask string) string {
if len(ask) > decisionAskCap {
ask = ask[:decisionAskCap]
}
ask = smartRuneHead(ask, decisionAskCap)
if system == "" {
return ask
}
if len(system) > decisionSystemCap {
system = system[:decisionSystemCap]
}
return system + "\n\n" + ask
return smartRuneHead(system, decisionSystemCap) + "\n\n" + ask
}
func decisionRequestBody(rt *smartRouter, state string) []byte {
+9
View File
@@ -3,6 +3,7 @@ package traefikllmgateway
import (
"context"
"net/http"
"strings"
"testing"
"time"
@@ -160,6 +161,14 @@ func TestClassifyDecisionShadowCallsEvenWhenSettled(t *testing.T) {
assert.Equal(t, smartReasonFloor, cls.reason)
}
func TestExtractSmartV2KeepsRawHeads(t *testing.T) {
long := strings.Repeat("a", 6000) + strings.Repeat("b", 6000)
req := map[string]any{"messages": []any{msg("system", strings.Repeat("s", 900)+strings.Repeat("t", 900)), msg("user", long)}}
f := extractSmartV2(req, cacheEndpointChat)
assert.Equal(t, strings.Repeat("a", 6000)+strings.Repeat("b", 2192), f.ask)
assert.Equal(t, strings.Repeat("s", 900)+strings.Repeat("t", 124), f.sysHead)
}
func TestExtractSmartV2KeepsRawAsk(t *testing.T) {
f := extractSmartV2(userReq("Fix this:\n```\ncode\n```"), cacheEndpointChat)
assert.Equal(t, "Fix this:\n```\ncode\n```", f.ask)
+8
View File
@@ -13,6 +13,7 @@ import (
"sync/atomic"
"testing"
"time"
"unicode/utf8"
)
func decisionInt(v int) *int { return &v }
@@ -270,6 +271,13 @@ func TestRouterDecisionState(t *testing.T) {
if got := decisionState("", ask); got != ask[:8192] {
t.Fatalf("empty system len %d", len(got))
}
if got := decisionState(sys, ask); !strings.HasPrefix(got, sys[:1024]+"\n\n"+ask[:8]) {
t.Fatal("head of system and ask not kept")
}
multi := strings.Repeat("é", 600)
if got := decisionState(multi, multi); !utf8.ValidString(got) {
t.Fatal("cut inside a rune")
}
}
func TestRouterDecisionStatusErrors(t *testing.T) {
+27 -15
View File
@@ -76,20 +76,20 @@ type smartFeatures struct {
// prose is the ask without fenced code and paste paragraphs; lower is its lowercase form.
prose, lower string
system string
// ask is the raw newest ask, bounded, for the decision model.
ask string
intent int
hard int
codeBytes int
logBytes int
msgCount int
questions int
toolSeen int
lists int
promptBytes int
outTokens int64
jsonMode int
nonLatin bool
// sysHead and ask are raw heads of the system text and newest ask, for the decision model.
sysHead, ask string
intent int
hard int
codeBytes int
logBytes int
msgCount int
questions int
toolSeen int
lists int
promptBytes int
outTokens int64
jsonMode int
nonLatin bool
// foreign marks prose the English markers cannot classify.
foreign bool
truncated bool
@@ -145,6 +145,7 @@ func extractSmartV2(req map[string]any, endpoint string) smartFeatures {
}
}
}
f.sysHead = smartRuneHead(f.system, decisionSystemCap)
f.system = boundSmartText(f.system, smartSystemCap)
ask := f.findAsk(msgs)
@@ -152,12 +153,23 @@ func extractSmartV2(req map[string]any, endpoint string) smartFeatures {
f.truncated = len(msgs) > smartAskScanMax
return f
}
f.ask = boundSmartText(ask, smartTextCap)
f.ask = smartRuneHead(ask, decisionAskCap)
f.splitAsk(ask)
f.classifyIntent()
return f
}
// smartRuneHead returns the first limit bytes of s, moved back to a rune start.
func smartRuneHead(s string, limit int) string {
if len(s) <= limit {
return s
}
for limit > 0 && !utf8.RuneStart(s[limit]) {
limit--
}
return s[:limit]
}
// findAsk scans newest first for the user message that carries text and no tool
// parts, and records tool-loop state on the way. It avoids `continue` inside the
// switch: yaegi v0.16.1 ends the whole loop there.
+1 -1
View File
@@ -594,7 +594,7 @@ func (g *Gateway) classifyWithSession(ctx context.Context, rt *smartRouter, req
var assist smartAssist
if rt.decision != nil {
assist = func(f *smartFeatures) decisionAnswer {
return g.askDecisionModel(ctx, rt, decisionState(f.system, f.ask))
return g.askDecisionModel(ctx, rt, decisionState(f.sysHead, f.ask))
}
}
if !rt.session.active() || g.routerSessions == nil {