gateway: error words escaped as \uXXXX are read by the other deciders too
What changed: #1669 taught failure(), resetsWritten and the per-model rest's other-models check to read a JSON error whose encoder escapes what isn't ASCII (Python's json.dumps does by default), but the other word-list deciders still matched the raw body: - modelRefused and modelRetired (fallback.go): an escaped 模型不支持 refusal rests the whole key or account (a cooldown, longer on repeats) instead of the model alone (#1235's class), and the escaped quota words of their negation are missed the same way. The candidate loop's restAfterMarked feeds them the vendor's body as it arrived, and the decision group's decideFailed the raw postDecide body; a chat's rewritten error carries the decoded message, which is why the chat path was spared. - retryable (fallback.go): an escaped 额度已用完 on a 400 reads as not retryable, so a decision group returns the error instead of asking its next decision model. - alwaysThinks (gateway.go, both 400 learn-again sites): a vendor's escaped 思考不可关闭 refusal is not asked again with thinking left to the model (#699's class), so the agent sees the 400 every turn. All three now match on wordsOf(body), as failure() already does. Run: - New TestAnthropicPassthroughAlwaysThinksInEscapedChinese sends the escaped refusal through /v1/messages: on the old code 400 after 1 call (the re-ask not taken), with the change 200 after 2. - New TestEscapedWordsReadByTheOtherDeciders: modelRefused(403, escaped 模型不支持) and retryable(400, escaped 额度已用完) are false on the old code, true with it; the same refusals unescaped stay true. - Both fail with the fix stashed, pass restored. go test -tags nogui -count=1 ./internal/gateway: ok. The named tests and the guards they touch (TestAnthropicPassthroughAlwaysThinksInChinese, TestModelNotInPlanRestsTheModelNotTheKey, TestFailureReadsEscapedWords, TestResetsWritten, TestWrittenResetRestsUntilThen) also -race -count=5: ok. go test -tags nogui -count=1 ./... under a temp HOME: ok. gofmt -l internal/ empty. go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...: clean but the trayimage_darwin.go cgo -Wenum-conversion warning, as on main. Builds: darwin, linux (-tags nogui), windows. Rebased by yetone onto main, where both learn-again sites ask refusesThinkingOff (anthropic.go, #1465): the decoding moved there. Co-authored-by: yetone <yetoneful@gmail.com>
This commit is contained in:
committed by
yetone
co-authored by
yetone
parent
a64b3a8d46
commit
4ed379f116
@@ -365,7 +365,8 @@ var alwaysThinks = regexp.MustCompile(`(?i)always engages in thinking|thinking (
|
||||
// it takes: Sub2API's gpt-6.1-sol, "gpt-6.1-sol does not support reasoning
|
||||
// effort \"none\"; use low, medium, high, xhigh or max" (#1465).
|
||||
func refusesThinkingOff(b []byte) bool {
|
||||
return alwaysThinks.Match(b) || mandatoryReasoning.Match(b) || thinkingOffRefused.Match(b) ||
|
||||
// alwaysThinks' words are matched decoded, too (#1678): 思考 escaped as \uXXXX
|
||||
return alwaysThinks.Match(wordsOf(b)) || mandatoryReasoning.Match(b) || thinkingOffRefused.Match(b) ||
|
||||
effortLevelsNamed.Match(b) && noneQuoted.Match(b)
|
||||
}
|
||||
|
||||
|
||||
@@ -921,6 +921,8 @@ func modelRefused(status int, body []byte) bool {
|
||||
default:
|
||||
return false
|
||||
}
|
||||
// the words are matched decoded, too (#1669)
|
||||
body = wordsOf(body)
|
||||
return unservedWords.Match(body) && !quotaWords.Match(body) && !refusedWords.Match(body)
|
||||
}
|
||||
|
||||
@@ -945,6 +947,8 @@ func modelRetired(status int, body []byte) bool {
|
||||
default:
|
||||
return false
|
||||
}
|
||||
// the words are matched decoded, too (#1669)
|
||||
body = wordsOf(body)
|
||||
return retiredWords.Match(body) && !quotaWords.Match(body)
|
||||
}
|
||||
|
||||
@@ -1037,6 +1041,8 @@ func elsewhere(left []candidate, c candidate) []candidate {
|
||||
// that failed this way: the vendor was busy, out of quota or failing, or
|
||||
// this key or provider can't serve it — not the request itself at fault.
|
||||
func retryable(status int, body []byte) bool {
|
||||
// the words are matched decoded, too (#1669)
|
||||
body = wordsOf(body)
|
||||
switch {
|
||||
case overflowed(status, body):
|
||||
// a conversation too long for the model is as long on every
|
||||
|
||||
@@ -243,6 +243,26 @@ func TestModelNotInPlanRestsTheModelNotTheKey(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// The same words said escaped as \uXXXX (a relay whose encoder escapes
|
||||
// what isn't ASCII, #1669) are read by the other deciders too: what
|
||||
// rests the model alone, and what another provider may answer.
|
||||
func TestEscapedWordsReadByTheOtherDeciders(t *testing.T) {
|
||||
unserved := []byte(`{"error":{"code":7,"message":"\u8be5\u6a21\u578b\u4e0d\u652f\u6301\u5f53\u524d\u4ee4\u724c\u8ba1\u5212"}}`)
|
||||
if !modelRefused(403, unserved) {
|
||||
t.Errorf("escaped 模型不支持: modelRefused false; the key would rest for its model")
|
||||
}
|
||||
if !modelRefused(403, []byte(`{"error":{"message":"该模型不支持当前令牌计划"}}`)) {
|
||||
t.Errorf("the same refusal unescaped: modelRefused false")
|
||||
}
|
||||
quota := []byte(`{"error":{"message":"\u989d\u5ea6\u5df2\u7528\u5b8c\uff0c\u8bf7\u660e\u5929\u518d\u8bd5","type":"invalid_request_error"}}`)
|
||||
if !retryable(400, quota) {
|
||||
t.Errorf("escaped 额度已用完: retryable false; the next decision model isn't asked")
|
||||
}
|
||||
if !retryable(400, []byte(`{"error":{"message":"额度已用完,请明天再试","type":"invalid_request_error"}}`)) {
|
||||
t.Errorf("the same refusal unescaped: retryable false")
|
||||
}
|
||||
}
|
||||
|
||||
// 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) {
|
||||
|
||||
@@ -711,6 +711,23 @@ func TestAnthropicPassthroughMiniMaxArkThinkingOffRefused(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// The same refusal, escaped as \uXXXX the way a relay whose encoder
|
||||
// escapes what isn't ASCII sends it (#1669): asked again all the same.
|
||||
func TestAnthropicPassthroughAlwaysThinksInEscapedChinese(t *testing.T) {
|
||||
f := &fake{t: t, ctype: "application/json", reply: `{"id":"msg","type":"message","content":[]}`}
|
||||
f.refuse = func(b []byte) (int, string) {
|
||||
if bytes.Contains(b, []byte(`"disabled"`)) {
|
||||
return 400, `{"type":"error","error":{"type":"invalid_request_error","message":"\u8be5\u6a21\u578b\u59cb\u7ec8\u652f\u6301\u601d\u8003\uff0c\u4e0d\u53ef\u5173\u95ed"}}`
|
||||
}
|
||||
return 0, ""
|
||||
}
|
||||
setup(t, provider.Anthropic, f)
|
||||
code, body := post(t, "/v1/messages", `{"model":"m1","max_tokens":5,"messages":[{"role":"user","content":"title?"}],"thinking":{"type":"disabled"}}`)
|
||||
if code != 200 || f.calls != 2 || bytes.Contains(f.got, []byte("thinking")) || !bytes.Contains(f.got, []byte(`"title?"`)) {
|
||||
t.Fatalf("%d %s after %d calls, last sent %s", code, body, f.calls, f.got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpenRouterMandatoryReasoningRetries(t *testing.T) {
|
||||
const refusal = `{"error":{"message":"OpenRouter: Reasoning is mandatory for this endpoint and cannot be disabled.","type":"invalid_request_error"},"type":"error"}`
|
||||
for _, tc := range []struct {
|
||||
|
||||
Reference in New Issue
Block a user