From 0c6752dbdfea0e61e7753acdb08e3e46164d439e Mon Sep 17 00:00:00 2001 From: yetone Date: Tue, 13 Oct 2026 10:24:29 +0800 Subject: [PATCH] gateway: a too-long, shape or retired refusal read its Chinese escaped as \uXXXX too, and the translated thinking re-ask has its test (#1678) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Follow-up to ggbdpq's f16c1f7cd (#1678) after its review. Its four sites now read wordsOf(body); three more deciders whose word lists hold Chinese still matched the raw body, and one of its sites had no test: - overflowed: an escaped 上下文长度超过限制 400 wasn't read as the conversation too long. It is called from retryable (now decoded) and from the candidate loop on the held error, so it decodes itself. - shapeRefused: its negations (the quota and unserved words) missed escaped Chinese, so a decision group's raw postDecide body that also said 额度已用完 counted as a shape refusal and rested nobody. - tooLongUnsaid: an escaped 该模型不支持 400 past the window was told to the agent as a prompt too long, as its quota and unserved checks read the raw body. - forwardTranslated's alwaysThinks site had no test: breaking it left every test green. TestTranslatedThinkingOffRefused now carries the escaped #699 refusal. Run (worktree on origin/main ebd7f4313 + f16c1f7cd + df4ea0ff6, sandbox HOME, pinned caches): new TestEscapedWordsReadByEveryDecider passes with and without escaping; each of overflowed, shapeRefused, tooLongUnsaid and #1678's modelRetired put back to the raw body makes it FAIL naming that decider ("上下文长度超过限制 not overflowed", "a shape refusal that says the quota is spent rests nobody", "...told as too long: prompt is too long ... (Fake answered 400: 该模型不支持当前令牌计划)", "a retirement that says the quota is spent rests the model alone"). On main both sites now ask refusesThinkingOff (anthropic.go), which reads alwaysThinks decoded since the rebase of f16c1f7cd; putting it back to Match(b) makes TestTranslatedThinkingOffRefused/4 (and #1678's passthrough test) FAIL with 400 "Fake: 该模型始终支持思 考,不可关闭". gofmt -l clean; go vet ./..., GOOS=linux go vet -tags nogui ./... and GOOS=windows go vet ./... exit 0. go test -tags nogui -count=1 ./... (sandbox HOME): every package ok but internal/agent and internal/gateway, which hit go test's default 10m bound at load average ~24 from other sessions (the stacks show TestWSLMoreAgentsPick and TestThinkingStreamsThroughGroup running for 2m and 2s, no hang); both rerun alone with -timeout 40m: ok (gateway 669s, agent 600s). --- internal/gateway/fallback.go | 4 +++ internal/gateway/fallback_test.go | 39 ++++++++++++++++++++++++++++ internal/gateway/gateway.go | 3 ++- internal/gateway/wire_effort_test.go | 3 +++ 4 files changed, 48 insertions(+), 1 deletion(-) diff --git a/internal/gateway/fallback.go b/internal/gateway/fallback.go index bb89b6b52..b24738680 100644 --- a/internal/gateway/fallback.go +++ b/internal/gateway/fallback.go @@ -988,6 +988,8 @@ var shapeWords = regexp.MustCompile(`(?i)failed to deserialize|unknown (item |co // shapeRefused says a request failed over its shape alone: the next // member is asked, and this one doesn't rest, as nothing is wrong with it. func shapeRefused(status int, body []byte) bool { + // the words are matched decoded, too (#1669) + body = wordsOf(body) return (status == 400 || status == 422) && shapeWords.Match(body) && !quotaWords.Match(body) && !unservedWords.Match(body) && !refusedWords.Match(body) } @@ -1095,6 +1097,8 @@ func tooLongUnsaid(p provider.Provider, model string, tokens, status int, body [ default: return "", false } + // the words are matched decoded, too (#1669) + body = wordsOf(body) if overflowed(status, body) || failure(status, body) != failOther || quotaWords.Match(body) || unservedWords.Match(body) || refusedWords.Match(body) || provider.EdgeBlocked(body) || regionRefused(status, body) { return "", false diff --git a/internal/gateway/fallback_test.go b/internal/gateway/fallback_test.go index d5c92cace..1555c1418 100644 --- a/internal/gateway/fallback_test.go +++ b/internal/gateway/fallback_test.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/base64" "encoding/json" + "fmt" "io" "net/http" "net/http/httptest" @@ -263,6 +264,44 @@ func TestEscapedWordsReadByTheOtherDeciders(t *testing.T) { } } +// The rest of the deciders whose words are Chinese read them escaped as +// well: a conversation too long, a retired model or a shape refusal that +// also says the quota is spent, and a size inferred only when nothing +// else is said. Each case beside the same words unescaped. +func TestEscapedWordsReadByEveryDecider(t *testing.T) { + for _, esc := range []bool{true, false} { + say := func(s string) []byte { + if !esc { + return []byte(`{"error":{"message":"` + s + `"}}`) + } + q, _ := json.Marshal(s) + // as Python's json.dumps writes it + var b strings.Builder + for _, r := range string(q) { + if r > 127 { + fmt.Fprintf(&b, `\u%04x`, r) + } else { + b.WriteRune(r) + } + } + return []byte(`{"error":{"message":` + b.String() + `}}`) + } + if !overflowed(400, say("上下文长度超过限制")) { + t.Errorf("escaped %v: 上下文长度超过限制 not overflowed", esc) + } + if modelRetired(410, say("Model m1 has been deprecated, 额度已用完")) { + t.Errorf("escaped %v: a retirement that says the quota is spent rests the model alone", esc) + } + if shapeRefused(400, say("Unknown parameter: 'x'. 额度已用完")) { + t.Errorf("escaped %v: a shape refusal that says the quota is spent rests nobody", esc) + } + p := provider.Provider{ID: "fake", Name: "Fake", Contexts: map[string]int{"m": 100}} + if said, ok := tooLongUnsaid(p, "m", 1000, 400, say("该模型不支持当前令牌计划")); ok { + t.Errorf("escaped %v: a model the plan doesn't serve told as too long: %s", esc, said) + } + } +} + // Two ChatGPT accounts ticked: the one Codex is signed in to is out of // quota, so the request goes to the saved one, signed with its own tokens. func TestSubscriptionAccountsTakeOver(t *testing.T) { diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index bde12522f..cd88c9288 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -5802,7 +5802,8 @@ var tooLongRe = regexp.MustCompile(`(?i)context_length_exceeded|prompt is too lo // overflowed says a refused request was too long for the model — a // request error (400, 413, 422) whose words say so. func overflowed(status int, body []byte) bool { - return (status == 400 || status == 413 || status == 422) && tooLong(status, string(body)) + // the words are matched decoded, too (#1669): 上下文 escaped as \uXXXX + return (status == 400 || status == 413 || status == 422) && tooLong(status, string(wordsOf(body))) } // tooLong is whether a vendor's error says the conversation no longer diff --git a/internal/gateway/wire_effort_test.go b/internal/gateway/wire_effort_test.go index 57e9edebd..c6d3b3e2f 100644 --- a/internal/gateway/wire_effort_test.go +++ b/internal/gateway/wire_effort_test.go @@ -100,6 +100,9 @@ func TestTranslatedThinkingOffRefused(t *testing.T) { "thinking.type `disabled` is not supported by this model Request id: 0217", // Sub2API's gpt-6.1-sol reads disabled as reasoning effort none (#1465) `gpt-6.1-sol does not support reasoning effort \"none\"; use low, medium, high, xhigh or max`, + // #699's Chinese, escaped as \uXXXX the way a relay's + // json.dumps sends it (#1669) + `\u8be5\u6a21\u578b\u59cb\u7ec8\u652f\u6301\u601d\u8003\uff0c\u4e0d\u53ef\u5173\u95ed`, } { t.Run(fmt.Sprint(i), func(t *testing.T) { translatedThinkingOffRefused(t, refusal) }) }