Files
magpie/internal/gateway/relay_upstream_failed_test.go
T
ggbdpq 8d7439a1a0 gateway: a translated system turn is folded for a Responses upstream, and a relay's "Upstream request failed" 400 fails a group over (#4632)
Two faults, one turn. Claude Code 2.1.296 and later send their
environment as a message with role "system" inside messages
(Request.envInTurns). The Chat and Anthropic builds fold such a message
into the system prompt (responseInstructions); buildResponses wrote the
role as it came, and a relay whose Responses endpoint fronts the ChatGPT
backend — which takes user, assistant and developer alone — answered
every translated turn 400 "Upstream request failed". That 400 matched
none of the wordings that make a 4xx retryable, so the holdWriter held
it as final and the group's second member never ran: the relay's answer
went to the agent, every turn.

What changed: buildResponses folds a translated request's system and
developer messages the way the Chat and Anthropic builds do, keyed on
the read. A Responses client's own instruction items are kept by the
read (#2532) and go where they stood; so do a codex account's, which
its send says as developer ones in place (codexBody — the fold would
move them out of conversation order, so buildFor marks the account's
requests kept). A translated request's system message is the harness's,
so it is said in the instructions, and one after a reply is told as a
user <system-reminder>, as on every other target. A relay's own word
that the provider behind it turned the request away ("sub2api: Upstream
request failed", type upstream_error — the words the Codex compaction
already reads, #866, #1511) joins the wordings that make a 400 or 422
retryable (retryable) and fail a group over (shapeRefused): the next
member is asked, and nobody rests. A 400 that says nothing of the kind
still ends the route, as a bad request would fail on every member
alike.

Run: TestClaudeSystemTurnSaidInTheInstructionsOnResponses and
TestGroupFailsOverARelaysUpstreamFailed, red at upstream/main 77e853588
and green here; TestUpstreamFailedWordsFailARelayOver's guards (a 401,
403 or 429 with the words, a plain bad request, and an
insufficient_quota beside them keep their reading); the codex account's
own shape (TestCodexAccountUpstream, both clients), the instruction
roles contract (TestResponsesInstructionRolesThroughSearch,
TestResponsesInstructionsOtherAPIsUnchanged), the compaction, group and
retry families, the full gateway package, gofmt, vet on darwin, linux
and windows, and builds of all three (-tags nogui).

Generated-by: GLM-5.3-Flash (ZCode)
2026-11-01 22:23:51 +08:00

79 lines
3.3 KiB
Go

package gateway
import (
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// upstreamFailed400 is a relay's own word that the provider behind it
// turned the request away: sub2api's answer to a translated Claude Code
// turn whose system message its Codex backend refused (#4632). It is not
// the client's mistake, and not the relay seat's: the group's next member
// is asked, and the seat rests for nothing.
const upstreamFailed400 = `{"error":{"message":"sub2api: Upstream request failed","type":"upstream_error"}}`
// A relay answering 400 "Upstream request failed" hands the request to the
// group's next member, and nobody rests (#4632: the group's second member
// never ran, and every turn died on the first seat). A 400 that says
// nothing of the kind still ends the route, as any bad request would fail
// on every member alike.
func TestGroupFailsOverARelaysUpstreamFailed(t *testing.T) {
fresh(t)
a := &scripted{replies: []reply{{400, "", upstreamFailed400}}}
b := &scripted{replies: []reply{{200, "", chatOK}}}
scriptedOn(t, "a", provider.Responses, a)
scriptedOn(t, "b", provider.Chat, b)
if err := provider.SaveGroup(provider.Group{Name: "G", Members: []string{"a/m", "b/m"}, Routing: provider.Ordered}); err != nil {
t.Fatal(err)
}
s := New()
code, body := postAs(t, s, "", `{"model":"group/g","messages":[{"role":"user","content":"hi"}]}`)
if code != 200 || !strings.Contains(body, "hello") || 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 {
t.Fatalf("tries %+v", r.Tries)
}
if rest := r.Tries[0].Rest; rest != nil {
t.Fatalf("a relay's upstream failure was to rest nobody: %+v", r.Tries[0].Rest)
}
// the seat stayed live: the next group request asks it first again
code, body = postAs(t, s, "", `{"model":"group/g","messages":[{"role":"user","content":"hi"}]}`)
if code != 200 || !strings.Contains(body, "hello") || a.n != 2 || b.n != 2 {
t.Fatalf("next: %d %s (a %d, b %d)", code, body, a.n, b.n)
}
}
// The words fail a 400 or a 422 over, and nothing beside them: a status of
// the account's own (401, 403, 429), a plain bad request, and a relay that
// also says the quota is spent keep the reading they had.
func TestUpstreamFailedWordsFailARelayOver(t *testing.T) {
for _, x := range []struct {
status int
body string
want bool
}{
{400, upstreamFailed400, true},
// as magpie tells it on, with the relay's name before, and the
// relay's own type around it; and apex's, whose type said
// invalid_request_error (#1511)
{400, `{"error":{"message":"apex: Upstream request failed","type":"invalid_request_error"}}`, true},
{422, `{"error":{"message":"Upstream request failed"}}`, true},
// a status of the account's own keeps its reading
{401, upstreamFailed400, false},
{403, upstreamFailed400, false},
{429, upstreamFailed400, false},
// a plain bad request ends the route, as it would on every member
{400, `{"error":{"message":"Invalid parameter: model"}}`, false},
{400, `{"error":{"message":"Upstream request failed, insufficient_quota"}}`, false},
} {
if got := shapeRefused(x.status, []byte(x.body)); got != x.want {
t.Errorf("shapeRefused(%d, %s) = %v, want %v", x.status, x.body, got, x.want)
}
}
}