Files
magpie/internal/gateway/stream_failure_response_test.go
T
yetone 29bfd5dc8b gateway: a Responses stream magpie ends is a whole response.failed (plugins#852)
When a Responses stream ended short or was cut, passthrough wrote a
response.failed with only object, status and error. Grok Build reads
the Responses stream through async-openai's types. Their Response needs
id, created_at, model, object, output and status, and the event needs
sequence_number. So a Grok plugin stream cut by the vendor ended in
"serialization error: missing field `created_at`" instead of the error
(lxyer's report behind plugins#852).

Now passthrough writes a whole response.failed:
- usageSniffer keeps the stream's response id, created_at and the next
  sequence_number;
- if responsesTextCalls renumbered the events on the way out, the
  failure follows the last number the client got;
- id, created_at, model and the error are filled in.
Before any event there is a fresh resp_ id and the time now.
The Chat and Gemini failures are unchanged. The translator paths
already end through responsesEncoder, which writes a whole response,
and nothing else writes a Responses streamFailure.

Run:
- On mote, at this tree: gofmt -l; go vet ./... on darwin, linux nogui
  and windows; builds for all three; go test -tags nogui -timeout 45m
  ./...: 87 packages ok, one red,
  TestCancelledSignInEndsItsFetch in internal/plugin, which this change
  doesn't reach (open as #5264). That run was at the base 0fc3455e2.
  Rebased on 00198b26e (#5655, gateway): gofmt -l, vet on all three
  systems and go test ./internal/gateway again: ok. Rebased again on
  cdf734757 (automode text, OpenMausBot): vet, build and
  -run 'StreamFailure|AutoMode|Automode|Anthropic' ./internal/gateway ok.
- Red on the old code (the tests at 0fc3455e2's gateway.go and
  sniff.go): TestResponsesStreamFailureIsAWholeResponse ("no
  sequence_number", "response.created_at missing") and
  TestResponsesStreamFailureBeforeAnyEvent.
- Real Grok Build 1.0.67 on mote (`grok -p`, api_backend responses)
  against a fake server replaying each ending:
  - the old frame: "missing field 'created_at'";
  - a whole response without sequence_number: "missing field
    'sequence_number'";
  - the new frame, after text, after response.created, and bare: read,
    and retried as server_error, with no serde error.

Not tried: Grok Build 1.0.77 (the reporter's), and a live Grok stream
cut by the vendor.
2026-11-08 11:03:09 +08:00

117 lines
4.3 KiB
Go

package gateway
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// lastResponsesEvent is the last data line of a Responses stream, parsed.
func lastResponsesEvent(t *testing.T, body string) map[string]any {
t.Helper()
var last string
for _, line := range strings.Split(body, "\n") {
if d, ok := strings.CutPrefix(line, "data: "); ok && strings.HasPrefix(d, "{") {
last = d
}
}
var v map[string]any
if err := json.Unmarshal([]byte(last), &v); err != nil {
t.Fatalf("last event %q: %v\n%s", last, err, body)
}
return v
}
// wholeFailedResponse checks a response.failed as async-openai's
// ResponseFailedEvent reads it (Grok Build's serde): the event's
// sequence_number, and a Response with id, object, created_at, model,
// output and status. Grok Build 1.0.67 said "missing field `created_at`"
// and then "missing field `sequence_number`" for each one gone.
func wholeFailedResponse(t *testing.T, v map[string]any) map[string]any {
t.Helper()
if v["type"] != "response.failed" {
t.Fatalf("last event is %v, not response.failed: %v", v["type"], v)
}
if _, ok := v["sequence_number"].(float64); !ok {
t.Errorf("no sequence_number: %v", v)
}
r, _ := v["response"].(map[string]any)
for _, k := range []string{"id", "object", "status"} {
if s, _ := r[k].(string); s == "" {
t.Errorf("response.%s missing: %v", k, r)
}
}
if _, ok := r["model"].(string); !ok {
t.Errorf("response.model missing: %v", r)
}
if c, _ := r["created_at"].(float64); c <= 0 {
t.Errorf("response.created_at missing: %v", r)
}
if _, ok := r["output"].([]any); !ok {
t.Errorf("response.output not a list: %v", r)
}
if e, _ := r["error"].(map[string]any); e["message"] == nil {
t.Errorf("response.error missing: %v", r)
}
return r
}
// A Responses stream that ends short after its output began is ended
// with a response.failed a Responses client can read: the response the
// stream began (its id and created_at), numbered after its last event.
// magpie's own had only object, status and error, and Grok Build failed
// the turn on it with "serialization error: missing field `created_at`"
// instead of saying the error (plugins#852).
func TestResponsesStreamFailureIsAWholeResponse(t *testing.T) {
fresh(t)
up := &scripted{replies: []reply{{200, "text/event-stream", sse(
`event: response.created`,
`data: {"type":"response.created","sequence_number":0,"response":{"id":"resp_up1","object":"response","created_at":1762000000,"status":"in_progress","model":"m","output":[]}}`,
`event: response.output_item.added`,
`data: {"type":"response.output_item.added","sequence_number":1,"output_index":0,"item":{"id":"msg_1","type":"message","role":"assistant","status":"in_progress","content":[]}}`,
`event: response.output_text.delta`,
`data: {"type":"response.output_text.delta","sequence_number":2,"item_id":"msg_1","output_index":0,"content_index":0,"delta":"pong"}`,
)}}}
scriptedOn(t, "a", provider.Responses, up)
gw := httptest.NewServer(New().Handler())
t.Cleanup(gw.Close)
req, _ := http.NewRequest("POST", gw.URL+"/v1/responses", strings.NewReader(`{"model":"a/m","stream":true,"input":"hi"}`))
req.Header.Set("Content-Type", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
b, _ := io.ReadAll(res.Body)
body := string(b)
if res.StatusCode != 200 || !strings.Contains(body, "pong") {
t.Fatalf("%d %s", res.StatusCode, body)
}
v := lastResponsesEvent(t, body)
r := wholeFailedResponse(t, v)
if r["id"] != "resp_up1" || r["created_at"] != float64(1762000000) {
t.Errorf("not the response the stream began: %v", r)
}
if v["sequence_number"] != float64(3) {
t.Errorf("sequence_number %v, want 3 (after the stream's last)", v["sequence_number"])
}
}
// With nothing of the response said yet, the failure is still whole: an
// id and created_at of its own, numbered first.
func TestResponsesStreamFailureBeforeAnyEvent(t *testing.T) {
b := streamFailure(provider.Responses, "x: connection lost mid-reply (unexpected EOF)")
r := wholeFailedResponse(t, lastResponsesEvent(t, string(b)))
if id, _ := r["id"].(string); !strings.HasPrefix(id, "resp_") {
t.Errorf("id %q", id)
}
if !strings.HasPrefix(string(b), "event: response.failed\n") {
t.Errorf("%s", b)
}
}