gateway: Command Code's bare 400 (a trace id, no reason) hands a group's request to the next member, not ends it (#2313, lqisgod)

Command Code's Provider API answers some requests with
{"message":"invalid request error trace_id: <hex>","type":"invalid_request_error"}:
a 400 (a 422 the week before), no code, no param. retryable() read it as
the request's own fault, so a group stopped there and its later members
were never asked: six requests in a row on lqisgod's wb-flash group, while
the fourth member answered the same ~660K prompt that hour.

What it turns away is the request's size, not the key: lqisgod's A/B on
one agent conversation (~748K tokens of history and tool calls, same bytes
otherwise) answered 200 with max_completion_tokens 64000 and this 400 with
384000, the max output Command Code lists for the model, so history plus
asked-for output is over a limit below the 1M context it lists. Its own
tracker has more of the kind (CommandCodeAI/command-code#960, #976).
Another member whose model has room may take the same request, so it is
asked, as for a shape another API can take.

unsaidWords matches that message alone, as the vendor sends it, wrapped by
a client in front of it, or told on after a provider's name. It goes the
way shapeWords do (shapeRefused, retryable): the next member is asked once,
Command Code doesn't rest, and the last member's error reaches the agent.
No retry of the same member is added. Its overflow ("The input is longer
than the model's context length trace_id: …") and a refusal naming a param
aren't matched. The keyed "commandcode" preset and the moved plan go
through the same gateway decision.

Tested: TestGroupFailsOverCommandCodesBare400 (streamed group request,
reporter's literal body; the 422 and wrapped forms from #960/#952; overflow,
param and longer-message negatives). With fallback.go put back it fails:
"400 ... COMMANDCODE: invalid request error trace_id: ... (a 1, b 0)".
go test -run 'TestGroupFailsOver|Shape' ./internal/gateway passes.

Full suite: SUITE_LINE
This commit is contained in:
yetone
2026-10-15 23:08:01 +08:00
parent 94c70a2dd9
commit 75035fa202
3 changed files with 71 additions and 3 deletions
File diff suppressed because one or more lines are too long
+19 -2
View File
@@ -1078,12 +1078,29 @@ func promptRefused(status int, body []byte) bool {
// for the model isn't one.
var shapeWords = regexp.MustCompile(`(?i)failed to deserialize|unknown (item |content |input )?(type|variant|field|parameter)|unknown_parameter|unrecognized (request argument|field|parameter)|extra (inputs|fields) are not permitted|additional properties are not allowed|schema exceeds the maximum (nesting )?depth|schema[^"]{0,60}\bnesting depth\b`)
// unsaidWords are Command Code's Provider API turning a request away with
// nothing but a trace id: {"message":"invalid request error trace_id:
// <hex>","type":"invalid_request_error"}, a 400 (a 422 before), no code,
// no param (#2313, lqisgod). What it turns away is the request's size:
// the reporter's A/B on one agent conversation (~748K tokens of history
// and tools) answered 200 with max_completion_tokens 64000 and this 400
// with 384000, the model's own listed max output, so the history and the
// output asked for together are over a limit below the 1M context it
// lists; its tracker has more of the kind (CommandCodeAI/command-code#960,
// #976). The key and the account are fine, and another member, whose
// model has room for it, may well take the same request: it is asked,
// once, as for shapeWords, and Command Code doesn't rest. Its overflow
// that says so names the model's context length before the trace id, and
// isn't one. The message may come after a provider's name ("Command Code:
// "), as magpie tells it on.
var unsaidWords = regexp.MustCompile(`(?i)(^|"|\\"|: )invalid request error trace_id: [0-9a-f]{16,}(\\?"|\s*$)`)
// 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) &&
return (status == 400 || status == 422) && (shapeWords.Match(body) || unsaidWords.Match(body)) &&
!quotaWords.Match(body) && !unservedWords.Match(body) && !refusedWords.Match(body)
}
@@ -1153,7 +1170,7 @@ func retryable(status int, body []byte) bool {
// in front of zcode.z.ai): another provider goes another way
return true
case status == 400, status == 422:
return quotaWords.Match(body) || unservedWords.Match(body) || refusedWords.Match(body) || shapeWords.Match(body) || regionWords.Match(body)
return quotaWords.Match(body) || unservedWords.Match(body) || refusedWords.Match(body) || shapeWords.Match(body) || unsaidWords.Match(body) || regionWords.Match(body)
}
return false
}
+51
View File
@@ -93,3 +93,54 @@ func TestGroupFailsOverASchemaTooDeep(t *testing.T) {
t.Fatalf("tries %+v", r.Tries)
}
}
// commandCodeBare400 is Command Code's Provider API turning a request away
// with nothing but its trace id, as lqisgod's usage.jsonl has it (#2313).
const commandCodeBare400 = `{"message":"invalid request error trace_id: 251c3c29761c17fdc459480ed9c68932","type":"invalid_request_error"}`
// Command Code's bare 400 is its backend's, not the request's: the same
// bytes sent again answer 200 (CommandCodeAI/command-code#960). The
// group's next member is asked and Command Code doesn't rest; before, the
// route ended on it with the later members never asked (#2313).
func TestGroupFailsOverCommandCodesBare400(t *testing.T) {
fresh(t)
a := &scripted{replies: []reply{{400, "", commandCodeBare400}}}
b := &scripted{replies: []reply{{200, "text/event-stream", chatStreamOK}}}
scriptedOn(t, "commandcode", provider.Chat, a)
scriptedOn(t, "b", provider.Chat, b)
if err := provider.SaveGroup(provider.Group{Name: "G", Members: []string{"commandcode/m", "b/m"}, Routing: provider.Ordered}); err != nil {
t.Fatal(err)
}
s := New()
code, body := postAs(t, s, "", `{"model":"group/g","stream":true,"messages":[{"role":"user","content":"hi"}]}`)
if code != 200 || !strings.Contains(body, "from b") || a.n != 1 || b.n != 1 {
t.Fatalf("%d %s (a %d, b %d)", code, body, a.n, b.n)
}
r := s.trace.routes[len(s.trace.routes)-1]
if len(r.Tries) != 2 || r.Tries[0].Status != 400 || r.Tries[0].Fail != failShape || r.Tries[0].Rest != nil {
t.Fatalf("tries %+v", r.Tries)
}
for _, x := range []struct {
status int
body string
want bool
}{
{400, commandCodeBare400, true},
{400, `Command Code: ` + commandCodeBare400, true},
// as a client behind Command Code saw it, wrapped, and its 422
// of the week before (CommandCodeAI/command-code#952)
{400, `{"message":"{\"message\":\"invalid request error trace_id: a050673b3298a747a15895803095bcf0\",\"type\":\"invalid_request_error\"}\n","type":"invalid_request_error"}`, true},
{422, `{"message":"{\"message\":\"invalid request error trace_id: f9965636e3da815215348ef4ddf2ae63\",\"type\":\"invalid_request_error\"}\n","type":"server_error"}`, true},
// its overflow, and a parameter it names, are the request's
{400, `{"message":"The input is longer than the model's context length trace_id: e811a6f10da868000cc12a043c07a73b","type":"invalid_request_error"}`, false},
{400, `{"error":{"message":"Invalid option: expected one of \"low\"|\"medium\"|\"high\"|\"xhigh\"|\"max\"","type":"invalid_request_error","param":"reasoning_effort"}}`, false},
// a message that says more after its trace id
{400, `{"message":"invalid request error trace_id: 251c3c29761c17fdc459480ed9c68932: messages.3.content too long","type":"invalid_request_error"}`, false},
{200, commandCodeBare400, false},
} {
if got := shapeRefused(x.status, []byte(x.body)); got != x.want {
t.Errorf("shapeRefused(%d, %s) = %v", x.status, x.body, got)
}
}
}