gateway: a decision model's intent counts by its option probabilities, not its own confidence field (@VersunPan on X)

Cause: readJev took an intent (and kept a level over the turn before's)
only when the answer's `confidence` was at least jevSure (0.4). That
scale is System One's: 0 for an even spread, 1 for one option certain
(provider.Confidence, (top - 1/n) / (1 - 1/n)). Other decision models
fill the field their own way. Cloudflare's @cf/cloudflare/clef-flash
compresses it: "提醒我明天下午三点给 Dora 打电话" came back light at
0.2866 while its top probability was about 0.68, so everyday requests
matched no rule and fell through to the group's default (Opus).
MLX-Serve's clef-flash-4bit says its top probability (0.68-0.95) and
passed. Both picked the same choice 15 of 15.

Fix: sureOf reads sureness from the answer's `probabilities` when it
gives two or more numbers, on System One's scale. Cloudflare's
clef-flash output schema makes probabilities (summing to 1) and
confidence required for a choice answer, and calls confidence only
"derived from the probabilities". Jev's own confidence is that same
formula, so Jev's decisions don't change. An answer without
probabilities is read by its confidence as before. A near-even spread
still comes out near 0 and matches nothing. The threshold stays 0.4.
The level path, which falls back to the turn before's level when
unsure, uses sureOf too. Verdict.Sure, which the Routing page shows, is
now the value the decision used. The effort pick reads only `score`, so
it is unchanged. The GUI line "An intent it isn't sure of matches no
rule" stays accurate.

Tests (decide_sure_test.go):
- TestClefFlashSureByProbabilities runs end to end through Workers AI's
  envelope at /ai/run/@cf/cloudflare/clef-flash, with answers shaped as
  that schema has them. Light at 0.68/0.14/0.10/0.08 with confidence
  0.2866 now routes to its rule (Sure 0.573). A 0.28/0.25/0.24/0.23
  spread matches none.
- TestJevLevelSureByProbabilities: a clear level is taken over the turn
  before's, a torn one keeps it, and an answer with no probabilities
  falls back to its confidence.

Run: gofmt -l (clean). go vet ./internal/gateway on darwin, GOOS=linux
and GOOS=windows. go test ./internal/gateway -count=1 -timeout 40m: ok
(2086s at load ~250. A first run hit go test's default 10m package
timeout under that load).

Broken by hand:
- readJev back on a.Confidence: both tests red. The CF case went to
  "from ka" with Sure 0.2866, the reporter's miss.
- sureOf floored at 0.5: the near-even spread and the torn level went
  red.

Not tried: a real Workers AI call. The fixture's spread is built from
the reporter's numbers (sure 0.2866, MLX top 0.68, MAE 1.6%), not from
saved bytes of that request.
This commit is contained in:
yetone
2026-10-14 19:54:31 +08:00
parent 430871d632
commit fcd6f35198
2 changed files with 123 additions and 8 deletions
+32 -8
View File
@@ -30,9 +30,32 @@ import (
)
// jevSure is how confident Jev must be of an intent for it to count.
// Confidence runs from 0, every option as likely, to 1, one certain.
// Confidence runs from 0, every option as likely, to 1, one certain, as
// System One gives it (provider.Confidence), read from the answer's
// probabilities wherever it has them (sureOf).
const jevSure = 0.4
// sureOf is how sure an answer is of its choice on jevSure's scale: from
// the probability it gave each option where it gave two or more, else the
// confidence it said. A decision model's own confidence field is
// calibrated its own way: Cloudflare's clef-flash says 0.2866 of a
// 提醒我明天下午三点给 Dora 打电话 it put at about 0.68 (@VersunPan on X),
// where the same model run in MLX-Serve says its top probability, and Jev
// says the top over an even spread. Its probabilities say the same thing on
// every one, and an even spread of them is still 0.
func sureOf(confidence float64, ps map[string]any) float64 {
n := 0
for _, v := range ps {
if _, ok := v.(float64); ok {
n++
}
}
if n < 2 {
return confidence
}
return provider.Confidence(ps)
}
// noIntent is the option Jev picks for a message that is none of the
// intents.
const noIntent = "none of these"
@@ -189,9 +212,10 @@ func readJev(b []byte, intents []string, levels bool, prev before) (verdict, err
var out struct {
Model string `json:"model"`
Answers map[string]struct {
Choice string `json:"choice"`
Score float64 `json:"score"`
Confidence float64 `json:"confidence"`
Choice string `json:"choice"`
Score float64 `json:"score"`
Confidence float64 `json:"confidence"`
Probabilities map[string]any `json:"probabilities"`
} `json:"answers"`
Usage struct {
Input int `json:"input_tokens"`
@@ -203,8 +227,8 @@ func readJev(b []byte, intents []string, levels bool, prev before) (verdict, err
}
var v verdict
if a, ok := out.Answers["intent"]; ok && len(intents) > 0 && !levels {
v.Sure = a.Confidence
if a.Choice != noIntent && a.Confidence >= jevSure {
v.Sure = sureOf(a.Confidence, a.Probabilities)
if a.Choice != noIntent && v.Sure >= jevSure {
v.Intent = intentNamed(intents, a.Choice)
}
}
@@ -213,9 +237,9 @@ func readJev(b []byte, intents []string, levels bool, prev before) (verdict, err
// is torn, the conversation stays at the level of the turn before
// (继续,再加一个基准测试 after a 复杂任务 came out 0.54 to 0.46),
// and a turn with none before takes the likelier
v.Sure = a.Confidence
v.Sure = sureOf(a.Confidence, a.Probabilities)
v.Intent = intentNamed(intents, a.Choice)
if was := intentNamed(intents, prev.Intent); a.Confidence < jevSure && was != "" {
if was := intentNamed(intents, prev.Intent); v.Sure < jevSure && was != "" {
v.Intent = was
}
}
+91
View File
@@ -0,0 +1,91 @@
package gateway
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// Cloudflare's clef-flash says a confidence of its own calibration, far
// below its probabilities (@VersunPan on X: 提醒我明天下午三点给 Dora 打电话
// came out light at 0.2866, the same model in MLX-Serve 0.68, its top
// choice right 15 of 15): the intent counts by its probabilities, on Jev's
// scale, so it matches its rule; a spread near even still matches none.
// The answers come in Workers AI's envelope, shaped as clef-flash's output
// schema has them (type, choice, probabilities, confidence).
func TestClefFlashSureByProbabilities(t *testing.T) {
var mu sync.Mutex
var spread map[string]any
var conf float64
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/client/v4/accounts/acc7/ai/run/@cf/cloudflare/clef-flash" {
http.Error(w, `{"success":false,"errors":[{"message":"No route for that URI"}]}`, 400)
return
}
var q map[string]any
json.NewDecoder(r.Body).Decode(&q)
mu.Lock()
ps, c := spread, conf
mu.Unlock()
answers := map[string]any{}
for id, x := range q["questions"].(map[string]any) {
switch {
case id == "levels":
answers[id] = map[string]any{"type": "noul", "noul": 0.04}
case id == "intent":
answers[id] = map[string]any{"type": "choice", "choice": "light", "probabilities": ps, "confidence": c}
case id == "level":
answers[id] = map[string]any{"type": "choice", "choice": "light", "probabilities": map[string]float64{"light": 0.7, "heavy": 0.2, "text": 0.1}, "confidence": 0.3}
case x.(map[string]any)["type"] == "score":
answers[id] = map[string]any{"type": "score", "score": 0.3, "legend": map[string]any{"0": "low"},
"probabilities": map[string]float64{"0": 0.75, "1": 0.2, "2": 0.05, "3": 0}, "confidence": 0.31}
}
}
b, _ := json.Marshal(map[string]any{"model": "clef-flash", "answers": answers, "usage": map[string]int{"input_tokens": 212, "output_tokens": 0}})
w.Write([]byte(`{"success":true,"errors":[],"messages":[],"result":` + string(b) + `}`))
}))
defer up.Close()
s := jevAt(t, up.URL+"/client/v4/accounts/acc7/ai/run", "jv/@cf/cloudflare/clef-flash",
provider.Rule{Use: "b/big", Intent: "light"}, provider.Rule{Use: "a/small", Intent: "heavy"}, provider.Rule{Use: "a/small", Intent: "text"})
mu.Lock()
spread, conf = map[string]any{"light": 0.68, "heavy": 0.14, "text": 0.1, noIntent: 0.08}, 0.2866
mu.Unlock()
out, r := postOK(t, s, "s1", chat("提醒我明天下午三点给 Dora 打电话", nil, 0, ""))
if c := r.Rule.Classified; !strings.Contains(out, "from kb") || c.Intent != "light" || c.Sure < 0.57 || c.Sure > 0.58 {
t.Fatalf("sure by its probabilities: %s %+v %+v", out, r.Rule, c)
}
mu.Lock()
spread, conf = map[string]any{"light": 0.28, "heavy": 0.25, "text": 0.24, noIntent: 0.23}, 0.03
mu.Unlock()
out, r = postOK(t, s, "s2", chat("嗯", nil, 0, ""))
if c := r.Rule.Classified; strings.Contains(out, "from kb") || c.Intent != "" || c.Sure > 0.05 {
t.Fatalf("a spread near even: %s %+v %+v", out, r.Rule, c)
}
}
// Levels go by the probabilities too: a clear new level is taken over the
// turn before's though its confidence field is low, and a torn one still
// keeps the turn before's.
func TestJevLevelSureByProbabilities(t *testing.T) {
in := []string{"简单任务", "复杂任务"}
clear := []byte(`{"model":"clef-flash","answers":{"level":{"type":"choice","choice":"简单任务","probabilities":{"简单任务":0.86,"复杂任务":0.14},"confidence":0.31}},"usage":{"input_tokens":180,"output_tokens":0}}`)
if v, err := readJev(clear, in, true, before{Intent: "复杂任务"}); err != nil || v.Intent != "简单任务" || v.Sure < 0.71 || v.Sure > 0.73 {
t.Fatalf("clear: %+v %v", v, err)
}
torn := []byte(`{"model":"clef-flash","answers":{"level":{"type":"choice","choice":"简单任务","probabilities":{"简单任务":0.54,"复杂任务":0.46},"confidence":0.02}},"usage":{"input_tokens":180,"output_tokens":0}}`)
if v, _ := readJev(torn, in, true, before{Intent: "复杂任务"}); v.Intent != "复杂任务" {
t.Fatalf("torn: %+v", v)
}
// an answer without probabilities is as sure as its confidence says,
// as Jev's are
if v, _ := readJev([]byte(`{"answers":{"intent":{"type":"choice","choice":"复杂任务","confidence":0.43,"probabilities":{}}}}`), in, false, before{}); v.Intent != "复杂任务" || v.Sure != 0.43 {
t.Fatalf("no probabilities: %+v", v)
}
}