issennknight on Discord (#general): an agent asked for deepseek-v4.1-flash, which goes out as deepseek-v4.1-flash-1017 (settings' ModelIDs). The vendor's reply said -1017, magpie passed it back, and the agent warned that the model didn't match. Before, only MemberModel (#822) or X-Magpie-Response-Model: member rewrote a reply's model, and only to the group member's id. Now the default is nameAsked. When magpie sent the request under another name than the agent asked for (a model's own id, a routing group, a provider/model id), the reply names the asked model. That covers Chat and Anthropic `model`, Responses `response.model` (created, in_progress, completed), Anthropic `message.model` (message_start) and Gemini `modelVersion`, streamed and whole, translated paths and plugins included: it all goes through serve's one memberWriter. A vendor that sent back the wire name as asked keeps its own name, a dated -20261017 too. Usage rows, recent calls and the Routing page still record served = the vendor's name: the writer sits outside captureResponseWriter. Claude Code, Claude Desktop and Codex get the asked name (or the vendor's), never the member's id, as before. Streams are rewritten one complete line at a time (memberLines). A run with no "model" in it is written through as the same slice. Only a JSON reply that has a target at decide time is held whole, which is what MemberModel already did. A JSON body that doesn't parse (a reply cut short) goes on as it came. A request to a remote magpie now sends X-Magpie-Response-Model: vendor (passOnCaller). That magpie names the vendor's model, as before, so this one still records what served the call (TestRemoteMagpieGroupNotSwapped went red without it). Existing tests that held a native reply byte for byte now expect only its model renamed to the asked id (namedAs in member_test.go): passthrough_severed, stream_accounting, refusal, refusal_thinking, thinking_signature and vertex. The archive keeps the reply as the vendor sent it, under the vendor's name (archive_test). Also dropped: the openai-model header code. serve never relays that header, so the test for it could not pass. Run (mote, at this head rebased on origin/main15066f84f): - go test -tags nogui -count=1 -timeout 45m ./...: every package ok except internal/gui: TestSettingsSaveKeepsWhatItDoesNotSend (#4514, SearchAll fromb09349d17) and TestColdTurnsTaken (#4510). Both are filed, and neither touches this change. Three other sessions' suites ran on mote at the same time. The first full run (base80bcef18f) had internal/gateway red in 10 tests: the byte-for-byte tests above, the remote-magpie served test and the truncated JSON body. Each one was fixed as described. - Rebased on544b91ca8(no gateway files changed in between): go build ./..., go vet, then go test ./internal/gateway ./internal/settings ./internal/provider: all ok. - go vet -tags nogui ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...: clean (cgo warnings only); gofmt -l clean. - go test -race -count=3 on the member and served tests: ok (122s). - the 10 formerly red tests with TestReplyNames, TestGeminiReplyNames, TestMemberLine, TestMemberWriter and Masked, after the rebase: ok. Red on the old code (the new tests over a git archive of the base): - TestReplyNamesAskedModel: 36 subtests fail (own id, group and provider id, x 6 protocol paths, x streamed or not), e.g. `the reply names "deepseek-v4.1-flash-1017", want "deepseek-v4.1-flash"`. - TestGeminiReplyNamesAskedModel: both subtests fail (modelVersion -1017). Broken by hand: dropping `|| m.wire == m.asked` from target() fails "as asked, dated" in 12 subtests. Real use: Codex 0.165.0 (codex exec --json) under a sandbox HOME and CODEX_HOME on mote. A fake Responses vendor always answered deepseek-v4.1-flash-1017, and a tee between Codex and `magpie serve` logged what Codex got. providers.json had d with deepseek-v4.1-flash and group fast; settings.json had modelIDs d/deepseek-v4.1-flash -> deepseek-v4.1-flash-1017. - old build: the vendor was asked for -1017; Codex got "model": "deepseek-v4.1-flash-1017" in response.created, in_progress and completed, both for -m deepseek-v4.1-flash and for -m group/fast. - new build: the vendor was still asked for -1017; Codex got "deepseek-v4.1-flash" and "group/fast"; the usage rows still say served deepseek-v4.1-flash-1017. Codex answered "pong" both times. Codex 0.165 itself shows no warning for a body model either way (its "model rerouted" reads the openai-model header), so the agent behind the report's warning was not the one run. Not tried: the reporter's own agent (not named) and Claude Code; the Responses WebSocket path (responses_ws.go's own frames); a compressed reply, which still goes through as it is; Antigravity's effort-variant wire, where the wire differs from the asked name on purpose.
358 lines
20 KiB
Go
358 lines
20 KiB
Go
package gateway
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/usage"
|
|
)
|
|
|
|
// A reply the vendor's safety filter ended before anything was said in it
|
|
// (#248): Anthropic's stop_reason "refusal" after an empty text block, as
|
|
// Claude answered Codex through a group, 2 tokens out and the whole prompt
|
|
// read from the cache.
|
|
var anthropicRefusal = sse(`event: message_start`+"\n"+`data: {"type":"message_start","message":{"id":"m1","role":"assistant","model":"m","content":[],"usage":{"input_tokens":3,"cache_read_input_tokens":237000,"cache_creation_input_tokens":47000}}}`,
|
|
`event: content_block_start`+"\n"+`data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}`,
|
|
`event: ping`+"\n"+`data: {"type":"ping"}`,
|
|
`event: content_block_delta`+"\n"+`data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":""}}`,
|
|
`event: content_block_stop`+"\n"+`data: {"type":"content_block_stop","index":0}`,
|
|
`event: message_delta`+"\n"+`data: {"type":"message_delta","delta":{"stop_reason":"refusal","stop_sequence":null},"usage":{"output_tokens":2}}`,
|
|
`event: message_stop`+"\n"+`data: {"type":"message_stop"}`)
|
|
|
|
var anthropicAnswer = sse(`event: message_start`+"\n"+`data: {"type":"message_start","message":{"id":"m2","role":"assistant","model":"m","content":[],"usage":{"input_tokens":3}}}`,
|
|
`event: content_block_start`+"\n"+`data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}`,
|
|
`event: content_block_delta`+"\n"+`data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"from b"}}`,
|
|
`event: content_block_stop`+"\n"+`data: {"type":"content_block_stop","index":0}`,
|
|
`event: message_delta`+"\n"+`data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":4}}`,
|
|
`event: message_stop`+"\n"+`data: {"type":"message_stop"}`)
|
|
|
|
// Chat Completions' content_filter, with nothing said before it.
|
|
var chatFiltered = sse(`data: {"id":"c1","choices":[{"index":0,"delta":{"role":"assistant","content":""}}]}`,
|
|
`data: {"id":"c1","choices":[{"index":0,"delta":{},"finish_reason":"content_filter"}]}`,
|
|
`data: {"id":"c1","choices":[],"usage":{"prompt_tokens":3,"completion_tokens":0}}`,
|
|
`data: [DONE]`)
|
|
|
|
// Responses' incomplete for content_filter, with an empty message begun.
|
|
var responsesFiltered = sse(`event: response.created`+"\n"+`data: {"type":"response.created","response":{"id":"resp_1","status":"in_progress","output":[]}}`,
|
|
`event: response.output_item.added`+"\n"+`data: {"type":"response.output_item.added","output_index":0,"item":{"id":"msg_1","type":"message","role":"assistant","content":[]}}`,
|
|
`event: response.content_part.added`+"\n"+`data: {"type":"response.content_part.added","item_id":"msg_1","output_index":0,"content_index":0,"part":{"type":"output_text","text":""}}`,
|
|
`event: response.incomplete`+"\n"+`data: {"type":"response.incomplete","response":{"id":"resp_1","status":"incomplete","incomplete_details":{"reason":"content_filter"},"output":[],"usage":{"input_tokens":3,"output_tokens":0,"total_tokens":3}}}`)
|
|
|
|
func refusalGroup(t *testing.T, members ...string) {
|
|
t.Helper()
|
|
if err := provider.SaveGroup(provider.Group{Name: "G", Members: members, Routing: provider.Ordered}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func responsesOn(t *testing.T, id string, s *scripted) {
|
|
t.Helper()
|
|
up := httptest.NewServer(s)
|
|
t.Cleanup(up.Close)
|
|
if err := provider.Save(provider.Provider{ID: id, Name: strings.ToUpper(id), Key: "k", Models: []string{"m"}, Responses: up.URL + "/v1"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func sendTo(s *Server, path, body string) (int, string) {
|
|
rec := httptest.NewRecorder()
|
|
s.Handler().ServeHTTP(rec, httptest.NewRequest("POST", path, strings.NewReader(body)))
|
|
return rec.Code, rec.Body.String()
|
|
}
|
|
|
|
const codexAsk = `{"model":"group/g","stream":true,"input":[{"role":"user","content":[{"type":"input_text","text":"hi"}]}]}`
|
|
|
|
// Codex asking a group whose first member's safety filter refuses with
|
|
// nothing said gets the next member's answer, not an empty reply it asks
|
|
// again for five times (#248); the member that refused doesn't rest, and
|
|
// what its refusal cost is logged as failed.
|
|
func TestRefusalFailsOverToTheNextMember(t *testing.T) {
|
|
fresh(t)
|
|
a := &scripted{replies: []reply{{200, "text/event-stream", anthropicRefusal}}}
|
|
b := &scripted{replies: []reply{{200, "text/event-stream", anthropicAnswer}}}
|
|
for id, script := range map[string]*scripted{"a": a, "b": b} {
|
|
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Request-Id", "req-"+id)
|
|
script.ServeHTTP(w, r)
|
|
}))
|
|
t.Cleanup(up.Close)
|
|
if err := provider.Save(provider.Provider{ID: id, Name: id, Key: "k", Models: []string{"m"}, Anthropic: up.URL}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
refusalGroup(t, "a/m", "b/m")
|
|
s := New()
|
|
rec := httptest.NewRecorder()
|
|
req := httptest.NewRequest("POST", "/v1/responses", strings.NewReader(codexAsk))
|
|
req.Header.Set(SessionHeader, "override-session")
|
|
req.Header.Set("Session_id", "native-session")
|
|
s.Handler().ServeHTTP(rec, req)
|
|
code, body := rec.Code, rec.Body.String()
|
|
if code != 200 || !strings.Contains(body, "from b") || strings.Contains(body, "content_filter") || 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.Usage) != 2 || r.Usage[0].Provider != "a" || r.Usage[0].CacheRead != 237000 || r.Usage[0].CacheWrite != 47000 || r.Usage[0].Output != 2 || r.Usage[1].Provider != "b" || r.Usage[1].Output != 4 {
|
|
t.Fatalf("refusal and answer accounting: %+v", r.Usage)
|
|
}
|
|
if len(r.Tries) != 2 || r.Tries[0].Fail != failRefused || r.Tries[0].Rest != nil || r.Tries[0].Status != 400 || r.Status != 200 {
|
|
t.Fatalf("tries: %+v", r.Tries)
|
|
}
|
|
if _, ok := restOf("a"); ok || s.resting("a") {
|
|
t.Fatal("a refusal set the member aside")
|
|
}
|
|
recs := usage.Load(time.Time{})
|
|
if len(recs) != 2 || recs[0].Provider != "a" || recs[0].Status != 400 || recs[0].CacheRead != 237000 || recs[0].Output != 2 ||
|
|
recs[1].Provider != "b" || recs[1].Status != 200 {
|
|
t.Fatalf("usage: %+v", recs)
|
|
}
|
|
|
|
if recs[0].Error == "" || recs[0].Error != keepMsg(r.Tries[0].Error) || recs[1].Error != "" {
|
|
t.Fatalf("refusal reason missing or leaked into the successful attempt: %+v", recs)
|
|
}
|
|
|
|
for i, r := range recs {
|
|
if r.Session != "override-session" || r.NativeSession != "native-session" || r.RequestID != []string{"req-a", "req-b"}[i] || r.Endpoint != "/v1/responses → /v1/messages" {
|
|
t.Fatalf("attempt %d lost request identity: %+v", i, r)
|
|
}
|
|
}
|
|
|
|
if recs[0].ResponseID != "" || recs[1].ResponseID != "resp_m2" {
|
|
t.Fatalf("only the delivered reply has a client response ID: %+v", recs)
|
|
}
|
|
|
|
for _, rec := range recs {
|
|
if rec.RouteID != r.ID || rec.RouteID == 0 {
|
|
t.Fatalf("usage route %d, want %d", rec.RouteID, r.ID)
|
|
}
|
|
}
|
|
|
|
// and on an Anthropic client's own API, relayed as it came
|
|
fresh(t)
|
|
a = &scripted{replies: []reply{{200, "text/event-stream", anthropicRefusal}}}
|
|
b = &scripted{replies: []reply{{200, "text/event-stream", anthropicAnswer}}}
|
|
scriptedOn(t, "a", provider.Anthropic, a)
|
|
scriptedOn(t, "b", provider.Anthropic, b)
|
|
refusalGroup(t, "a/m", "b/m")
|
|
code, body = sendTo(New(), "/v1/messages", `{"model":"group/g","max_tokens":10,"stream":true,"messages":[{"role":"user","content":"hi"}]}`)
|
|
if code != 200 || !strings.Contains(body, "from b") || strings.Contains(body, `"refusal"`) || a.n != 1 || b.n != 1 {
|
|
t.Fatalf("messages: %d %s (a %d, b %d)", code, body, a.n, b.n)
|
|
}
|
|
|
|
// not streamed: the whole reply is read for it
|
|
fresh(t)
|
|
a = &scripted{replies: []reply{{200, "", `{"id":"m1","type":"message","role":"assistant","model":"m","content":[],"stop_reason":"refusal","usage":{"input_tokens":3,"output_tokens":2}}`}}}
|
|
b = &scripted{replies: []reply{{200, "", `{"id":"m2","type":"message","role":"assistant","model":"m","content":[{"type":"text","text":"from b"}],"stop_reason":"end_turn","usage":{"input_tokens":3,"output_tokens":2}}`}}}
|
|
scriptedOn(t, "a", provider.Anthropic, a)
|
|
scriptedOn(t, "b", provider.Anthropic, b)
|
|
refusalGroup(t, "a/m", "b/m")
|
|
code, body = sendTo(New(), "/v1/messages", `{"model":"group/g","max_tokens":10,"messages":[{"role":"user","content":"hi"}]}`)
|
|
if code != 200 || !strings.Contains(body, "from b") || strings.Contains(body, `"refusal"`) || a.n != 1 || b.n != 1 {
|
|
t.Fatalf("whole: %d %s (a %d, b %d)", code, body, a.n, b.n)
|
|
}
|
|
}
|
|
|
|
// OpenAI's content_filter, by Chat Completions' finish_reason or a
|
|
// Responses reply incomplete for it, fails over the same way.
|
|
func TestContentFilterFailsOver(t *testing.T) {
|
|
for _, x := range []struct {
|
|
name string
|
|
setup func(t *testing.T, s *scripted)
|
|
reply string
|
|
path string
|
|
ask string
|
|
}{
|
|
{"chat to codex", func(t *testing.T, s *scripted) { scriptedOn(t, "a", provider.Chat, s) }, chatFiltered, "/v1/responses", codexAsk},
|
|
{"chat to chat", func(t *testing.T, s *scripted) { scriptedOn(t, "a", provider.Chat, s) }, chatFiltered, "/v1/chat/completions",
|
|
`{"model":"group/g","stream":true,"messages":[{"role":"user","content":"hi"}]}`},
|
|
{"responses to codex", func(t *testing.T, s *scripted) { responsesOn(t, "a", s) }, responsesFiltered, "/v1/responses", codexAsk},
|
|
{"responses failed", func(t *testing.T, s *scripted) { responsesOn(t, "a", s) },
|
|
sse(`event: response.created`+"\n"+`data: {"type":"response.created","response":{"id":"resp_1"}}`,
|
|
`event: response.failed`+"\n"+`data: {"type":"response.failed","response":{"id":"resp_1","status":"failed","error":{"code":"content_filter","message":"The response was filtered"}}}`),
|
|
"/v1/responses", codexAsk},
|
|
{"chat whole", func(t *testing.T, s *scripted) { scriptedOn(t, "a", provider.Chat, s) },
|
|
`{"id":"c1","choices":[{"index":0,"message":{"role":"assistant","content":null},"finish_reason":"content_filter"}]}`, "/v1/chat/completions",
|
|
`{"model":"group/g","messages":[{"role":"user","content":"hi"}]}`},
|
|
} {
|
|
t.Run(x.name, func(t *testing.T) {
|
|
fresh(t)
|
|
ctype := "text/event-stream"
|
|
if strings.HasPrefix(x.reply, "{") {
|
|
ctype = ""
|
|
}
|
|
a := &scripted{replies: []reply{{200, ctype, x.reply}}}
|
|
b := &scripted{replies: []reply{{200, "text/event-stream", anthropicAnswer}}}
|
|
x.setup(t, a)
|
|
scriptedOn(t, "b", provider.Anthropic, b)
|
|
refusalGroup(t, "a/m", "b/m")
|
|
s := New()
|
|
code, body := sendTo(s, x.path, x.ask)
|
|
if code != 200 || !strings.Contains(body, "from b") || strings.Contains(body, "content_filter") || a.n != 1 || b.n != 1 {
|
|
t.Fatalf("%d %s (a %d, b %d)", code, body, a.n, b.n)
|
|
}
|
|
if r := s.trace.routes[len(s.trace.routes)-1]; r.Tries[0].Fail != failRefused {
|
|
t.Fatalf("tries: %+v", r.Tries)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// With nobody else to ask, the agent is told it was refused, as a request
|
|
// not to send again as it is — not handed an empty reply that looks whole —
|
|
// and the usage says it failed. An agent on Anthropic's API has the
|
|
// vendor's refusal itself, which says so in its own words.
|
|
func TestRefusalWithNobodyLeftIsAnError(t *testing.T) {
|
|
fresh(t)
|
|
a := &scripted{replies: []reply{{200, "text/event-stream", anthropicRefusal}}}
|
|
scriptedOn(t, "a", provider.Anthropic, a)
|
|
refusalGroup(t, "a/m")
|
|
s := New()
|
|
code, body := sendTo(s, "/v1/responses", codexAsk)
|
|
if code != 400 || !strings.Contains(body, "m refused this request (safety filter") || strings.Contains(body, "response.incomplete") || a.n != 1 {
|
|
t.Fatalf("%d %s (a %d)", code, body, a.n)
|
|
}
|
|
if recs := usage.Load(time.Time{}); len(recs) != 1 || recs[0].Status != 400 || recs[0].CacheRead != 237000 {
|
|
t.Fatalf("usage: %+v", recs)
|
|
}
|
|
if r := s.trace.routes[len(s.trace.routes)-1]; r.Status != 400 || r.Tries[0].Fail != failRefused || r.Tries[0].Rest != nil {
|
|
t.Fatalf("route: %+v", r)
|
|
}
|
|
|
|
// a model asked by its own name, on Anthropic's API: the vendor's
|
|
// refusal as it said it, for Claude Code to switch to its fallback
|
|
// model on, which a 400 isn't to it (DD on Discord)
|
|
fresh(t)
|
|
a = &scripted{replies: []reply{{200, "text/event-stream", anthropicRefusal}}}
|
|
scriptedOn(t, "a", provider.Anthropic, a)
|
|
code, body = sendTo(New(), "/v1/messages", `{"model":"a/m","max_tokens":10,"stream":true,"messages":[{"role":"user","content":"hi"}]}`)
|
|
if code != 200 || !strings.Contains(body, `"stop_reason":"refusal"`) || !strings.Contains(body, "message_stop") || strings.Contains(body, "refused this request") || a.n != 1 {
|
|
t.Fatalf("messages: %d %s (a %d)", code, body, a.n)
|
|
}
|
|
|
|
// and on Chat's
|
|
fresh(t)
|
|
a = &scripted{replies: []reply{{200, "text/event-stream", chatFiltered}}}
|
|
scriptedOn(t, "a", provider.Chat, a)
|
|
code, body = sendTo(New(), "/v1/chat/completions", `{"model":"a/m","stream":true,"messages":[{"role":"user","content":"hi"}]}`)
|
|
if code != 400 || !strings.Contains(body, "safety filter: content_filter") || a.n != 1 {
|
|
t.Fatalf("chat: %d %s (a %d)", code, body, a.n)
|
|
}
|
|
}
|
|
|
|
// A refusal after some of the reply was said can't be taken back: the
|
|
// agent gets it as it came, and nobody else is asked. A reply that ends as
|
|
// usual is untouched.
|
|
func TestRefusalAfterContentPassesThrough(t *testing.T) {
|
|
fresh(t)
|
|
late := sse(`event: message_start`+"\n"+`data: {"type":"message_start","message":{"id":"m1","role":"assistant","model":"m","content":[]}}`,
|
|
`event: content_block_start`+"\n"+`data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}`,
|
|
`event: content_block_delta`+"\n"+`data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"half"}}`,
|
|
`event: message_delta`+"\n"+`data: {"type":"message_delta","delta":{"stop_reason":"refusal"},"usage":{"output_tokens":2}}`,
|
|
`event: message_stop`+"\n"+`data: {"type":"message_stop"}`)
|
|
a := &scripted{replies: []reply{{200, "text/event-stream", late}}}
|
|
b := &scripted{replies: []reply{{200, "text/event-stream", anthropicAnswer}}}
|
|
scriptedOn(t, "a", provider.Anthropic, a)
|
|
scriptedOn(t, "b", provider.Anthropic, b)
|
|
refusalGroup(t, "a/m", "b/m")
|
|
code, body := sendTo(New(), "/v1/messages", `{"model":"group/g","max_tokens":10,"stream":true,"messages":[{"role":"user","content":"hi"}]}`)
|
|
if code != 200 || body != namedAs(late, "group/g") || b.n != 0 {
|
|
t.Fatalf("messages: %d %s (b %d)", code, body, b.n)
|
|
}
|
|
// translated for Codex, it is the incomplete reply it was
|
|
a.n = 0
|
|
code, body = sendTo(New(), "/v1/responses", codexAsk)
|
|
if code != 200 || !strings.Contains(body, "half") || !strings.Contains(body, `"content_filter"`) || b.n != 0 {
|
|
t.Fatalf("responses: %d %s (b %d)", code, body, b.n)
|
|
}
|
|
|
|
// an answer as usual, as it came
|
|
fresh(t)
|
|
a = &scripted{replies: []reply{{200, "text/event-stream", anthropicAnswer}}}
|
|
scriptedOn(t, "a", provider.Anthropic, a)
|
|
scriptedOn(t, "b", provider.Anthropic, b)
|
|
refusalGroup(t, "a/m", "b/m")
|
|
code, body = sendTo(New(), "/v1/messages", `{"model":"group/g","max_tokens":10,"stream":true,"messages":[{"role":"user","content":"hi"}]}`)
|
|
if code != 200 || body != namedAs(anthropicAnswer, "group/g") || b.n != 0 {
|
|
t.Fatalf("answer: %d %s (b %d)", code, body, b.n)
|
|
}
|
|
whole := `{"id":"m2","type":"message","role":"assistant","model":"m","content":[{"type":"text","text":"whole"}],"stop_reason":"end_turn","usage":{"input_tokens":3,"output_tokens":2}}`
|
|
a.replies = []reply{{200, "", whole}}
|
|
code, body = sendTo(New(), "/v1/messages", `{"model":"group/g","max_tokens":10,"messages":[{"role":"user","content":"hi"}]}`)
|
|
if code != 200 || strings.TrimSpace(body) != namedAs(whole, "group/g") || b.n != 0 {
|
|
t.Fatalf("whole: %d %s (b %d)", code, body, b.n)
|
|
}
|
|
}
|
|
|
|
// A safety filter's refusal that arrives once the reply has begun goes to
|
|
// the agent as it came — nobody else is asked, the reply already reached
|
|
// it — and it is the vendor refusing the turn, as one before anything was
|
|
// said is: nobody rests for it, and the account is asked again as any
|
|
// other (#248).
|
|
func TestRefusalMidReplyRestsNobody(t *testing.T) {
|
|
fresh(t)
|
|
late := sse(`data: {"id":"c1","choices":[{"index":0,"delta":{"role":"assistant","content":"half"}}]}`,
|
|
`data: {"error":{"code":"content_filter","message":"The response was filtered"}}`,
|
|
`data: [DONE]`)
|
|
a := &scripted{replies: []reply{{200, "text/event-stream", late}}}
|
|
b := &scripted{replies: []reply{{200, "", chatOK}}}
|
|
scriptedOn(t, "a", provider.Chat, a)
|
|
scriptedOn(t, "b", provider.Chat, b)
|
|
refusalGroup(t, "a/m", "b/m")
|
|
s := New()
|
|
code, body := sendTo(s, "/v1/chat/completions", `{"model":"group/g","stream":true,"messages":[{"role":"user","content":"hi"}]}`)
|
|
if code != 200 || !strings.Contains(body, "half") || !strings.Contains(body, "content_filter") || b.n != 0 {
|
|
t.Fatalf("chat: %d %s (b %d)", code, body, b.n)
|
|
}
|
|
r := s.trace.routes[len(s.trace.routes)-1]
|
|
if len(r.Tries) != 1 || r.Tries[0].Fail != failRefused || r.Tries[0].Rest != nil || r.Tries[0].Status != 200 {
|
|
t.Fatalf("tries: %+v", r.Tries)
|
|
}
|
|
if s.resting("a") {
|
|
t.Fatal("a refusal after content set the member aside")
|
|
}
|
|
}
|
|
|
|
func TestStreamEventRefusals(t *testing.T) {
|
|
for _, x := range []struct {
|
|
ev string
|
|
kind int
|
|
}{
|
|
{`event: content_block_start` + "\n" + `data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}`, eventLead},
|
|
{`event: content_block_start` + "\n" + `data: {"type":"content_block_start","index":0,"content_block":{"type":"tool_use","id":"t","name":"x","input":{}}}`, eventContent},
|
|
{`event: content_block_start` + "\n" + `data: {"type":"content_block_start","index":0,"content_block":{"type":"thinking","thinking":""}}`, eventThinking},
|
|
{`event: content_block_delta` + "\n" + `data: {"type":"content_block_delta","index":0,"delta":{"type":"signature_delta","signature":"s"}}`, eventLead},
|
|
{`event: content_block_delta` + "\n" + `data: {"type":"content_block_delta","index":0,"delta":{"type":"thinking_delta","thinking":"hm"}}`, eventThinking},
|
|
{`event: content_block_delta` + "\n" + `data: {"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":""}}`, eventContent},
|
|
{`event: message_delta` + "\n" + `data: {"type":"message_delta","delta":{"stop_reason":"refusal"}}`, eventRefusal},
|
|
{`event: message_delta` + "\n" + `data: {"type":"message_delta","delta":{"stop_reason":"end_turn"}}`, eventContent},
|
|
{`data: {"id":"c","choices":[{"index":0,"delta":{},"finish_reason":"content_filter"}]}`, eventRefusal},
|
|
{`data: {"id":"c","choices":[{"index":0,"delta":{"content":"hi"},"finish_reason":"content_filter"}]}`, eventContent},
|
|
{`data: {"id":"c","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}`, eventContent},
|
|
{`data: {"type":"response.output_item.added","item":{"type":"reasoning","summary":[]}}`, eventThinking},
|
|
{`data: {"type":"response.reasoning_summary_text.delta","delta":"**Plan**"}`, eventThinking},
|
|
{`data: {"id":"c","choices":[{"index":0,"delta":{"reasoning_content":"hm"}}]}`, eventThinking},
|
|
{`data: {"id":"c","choices":[{"index":0,"delta":{"reasoning_content":"hm"},"finish_reason":"content_filter"}]}`, eventRefusal},
|
|
{`data: {"candidates":[{"content":{"parts":[{"text":"hm","thought":true}]}}]}`, eventThinking},
|
|
{`data: {"type":"response.output_item.added","item":{"type":"function_call","name":"x"}}`, eventContent},
|
|
{`data: {"type":"response.output_item.done","item":{"type":"message","content":[{"type":"output_text","text":"hi"}]}}`, eventContent},
|
|
{`data: {"type":"response.output_text.delta","delta":""}`, eventLead},
|
|
{`data: {"type":"response.reasoning_summary_part.added","part":{"type":"summary_text","text":""}}`, eventThinking},
|
|
{`data: {"type":"response.incomplete","response":{"status":"incomplete","incomplete_details":{"reason":"content_filter"}}}`, eventRefusal},
|
|
{`data: {"type":"response.incomplete","response":{"status":"incomplete","incomplete_details":{"reason":"max_output_tokens"}}}`, eventContent},
|
|
{`data: {"type":"response.completed","response":{"status":"completed"}}`, eventContent},
|
|
{`data: {"type":"error","error":{"code":"content_filter","message":"filtered"}}`, eventRefusalError},
|
|
{`data: {"candidates":[{"content":{"parts":[]},"finishReason":"SAFETY"}]}`, eventRefusal},
|
|
{`data: {"candidates":[{"content":{"parts":[{"text":"hi"}]},"finishReason":"SAFETY"}]}`, eventContent},
|
|
{`data: {"candidates":[{"content":{"parts":[{"text":"hi"}]}}]}`, eventContent},
|
|
{`data: {"promptFeedback":{"blockReason":"PROHIBITED_CONTENT"}}`, eventRefusal},
|
|
} {
|
|
if k, _, _ := streamEvent([]byte(x.ev)); k != x.kind {
|
|
t.Errorf("streamEvent(%s) = %d, want %d", x.ev, k, x.kind)
|
|
}
|
|
}
|
|
}
|